Exemple #1
0
        private SourceFile(IApkFile apk, JarFile jar, ISpySettings settings, MapFileLookup mapFile, string singleFilePath = null)
        {
            this.apk = apk;
            this.jar = jar;
            this.settings = settings;
            this.mapFile = mapFile;
            this.singleFilePath = singleFilePath;

#if DEBUG  || ENABLE_SHOW_AST
            classLoader = new AssemblyClassLoader(module.OnClassLoaded);
            var modParams = new ModuleParameters
            {
                AssemblyResolver = new AssemblyResolver(new[] { Frameworks.Instance.FirstOrDefault().Folder }, classLoader, module.OnAssemblyLoaded),
                Kind = ModuleKind.Dll
            };
            assembly = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("spy", Version.Parse("1.0.0.0")), "main", modParams);
            classLoader.LoadAssembly(assembly);

            var dot42Assembly = modParams.AssemblyResolver.Resolve("dot42");

            // Force loading of classes
            if (jar != null)
            {
                foreach (var fileName in jar.ClassFileNames)
                {
                    OpenClass(fileName);
                }
            }
#endif
        }
        public MethodDisassembly(MethodDefinition methodDef, MapFileLookup mapFile = null, TypeEntry typeEntry = null, MethodEntry methodEntry = null)
        {
            _typeEntry = typeEntry;
            _methodEntry = methodEntry;
            _methodDef = methodDef;
            _mapFile = mapFile;

            JumpTargetOffsets=new HashSet<int>();
            ExceptionHandlerOffsets = new HashSet<int>();

            if (methodDef.Body != null)
            {
                foreach (var i in methodDef.Body.Instructions)
                {
                    var op = i.Operand as Instruction;
                    if (op != null)
                        JumpTargetOffsets.Add(op.Offset);
                }
                foreach (var e in methodDef.Body.Exceptions)
                {
                    foreach(var c in e.Catches)
                        ExceptionHandlerOffsets.Add(c.Instruction.Offset);
                    if (e.CatchAll != null)
                        ExceptionHandlerOffsets.Add(e.CatchAll.Offset);
                }
            }

            Format = FormatOptions.Default;
        }
 public DalvikDisassemblyProvider(DalvikProcess process, string apkPath, MapFileLookup mapFile)
 {
     _process = process;
     _apkPath = apkPath;
     _mapFile = mapFile;
     _dex = new Lazy<DexLookup>(LoadDex);
 }
Exemple #4
0
 /// <summary>
 /// Default ctor
 /// </summary>
 public DalvikProcess(Debugger debugger, MapFile mapFile, string apkPath)
 {
     ApkPath = apkPath;
     this.debugger = debugger;
     debugger.Process = this;
     this.mapFile = new MapFileLookup(mapFile);
     debugger.ConnectedChanged += OnDebuggerConnectionChanged;
     breakpointManager = new Lazy<DalvikBreakpointManager>(CreateBreakpointManager);
     exceptionManager = new Lazy<DalvikExceptionManager>(CreateExceptionManager);
     referenceTypeManager = new Lazy<DalvikReferenceTypeManager>(CreateReferenceTypeManager);
     threadManager = new Lazy<DalvikThreadManager>(CreateThreadManager);
     disassemblyProvider =
         new Lazy<DalvikDisassemblyProvider>(() => new DalvikDisassemblyProvider(this, ApkPath, this.mapFile));
 }
        public MethodBodyDisassemblyFormatter(MethodDefinition methodDef, MapFileLookup mapFile)
        {
            _methodDef = methodDef;
            _mapFile = mapFile;

            TypeEntry typeEntry = null;
            MethodEntry methodEntry = null;

            if (mapFile != null)
            {
                typeEntry = mapFile.GetTypeByNewName(methodDef.Owner.Fullname);
                if (typeEntry != null)
                {
                    methodEntry = typeEntry.FindDexMethod(methodDef.Name, methodDef.Prototype.ToSignature());
                }
            }

            _dissassembly = new MethodDisassembly(methodDef, mapFile, typeEntry, methodEntry);

            _sourceDocument = new Lazy<string[]>(() =>
            {
                if (_dissassembly.MethodEntry == null)
                    return null;
                var pos = _mapFile.GetSourceCodePositions(_dissassembly.MethodEntry).FirstOrDefault();
                if (pos == null)
                    return null;

                try
                {
                    return File.ReadAllLines(pos.Document.Path);
                }
                catch (Exception)
                {
                    return null;
                }
            });
        }
        private void Initialize(string dexFilename, string mapfile, Func<AssemblyDefinition, string> filenameFromAssembly)
        {
            try
            {
                var readDex = Task.Factory.StartNew(() => DexLib.Dex.Read(dexFilename));
                var readMap = Task.Factory.StartNew(() => new MapFileLookup(new MapFile(mapfile)));

                Task.WaitAll(readMap, readDex);
                var dex = readDex.Result;
                var map = readMap.Result;
                
                _modifiedDetector = new AssemblyModifiedDetector(filenameFromAssembly, map);

                foreach (var type in map.TypeEntries)
                {
                    if(type.ScopeId == null)
                        continue;
                    
                    var typeScopeId = GetTypeScopeId(type);
                    
                    // redirect the generated class if neccessary.
                    var dexType = type.Id == 0 ? map.GeneratedType : type;

                    foreach (var method in type.Methods)
                    {
                        if (type.ScopeId == null)
                            continue;
                        
                        var scopeKey = Tuple.Create(typeScopeId, method.ScopeId);
                        _methodsByScopeId[scopeKey] = Tuple.Create(dexType, method);
                    }
                }

                _dexLookup = new DexLookup(dex);

                _dex = dex;
                _map = map;
            }
            catch (Exception ex)
            {
                IsEnabled = false;
                DLog.Warning(DContext.CompilerCodeGenerator, "Unable to initialize compiler cache: {0}", ex.Message);
            }            
        }
Exemple #7
0
        public void CompileIfRequired(ilspy::Mono.Cecil.AssemblyDefinition assembly, bool stopBeforeGeneratingCode = false)
        {
            if (_compiler != null && _previousAssembly == assembly && (_isFullyCompiled || stopBeforeGeneratingCode))
                return;

            string assemblyFileName = assembly.MainModule.FullyQualifiedName;

            CompilationErrors = null;
            _compiler = null;

#if DEBUG
            var framework = Frameworks.Instance.GetBySdkVersion(15);
#else
            var framework = Frameworks.Instance.GetNewestVersion();
#endif
            string frameworkFolder = framework.Folder;

            var refFolders = new List<string> { frameworkFolder, Path.GetDirectoryName(assemblyFileName) };

            var module = new XModule();
            var classLoader = new AssemblyClassLoader(module.OnClassLoaded);
            var resolver = new AssemblyResolver(refFolders, classLoader, module.OnAssemblyLoaded);
            var parameter = new ReaderParameters(ReadingMode.Immediate)
            {
                   AssemblyResolver = resolver,ReadSymbols = true, SymbolReaderProvider = new SafeSymbolReaderProvider()
            };

            var assemblies = new[] { resolver.Load(assemblyFileName, parameter) }.ToList();
            List<AssemblyDefinition> references = new List<AssemblyDefinition>();
            
            if(assembly.MainModule.Name != "dot42.dll")
                references = new[] { resolver.Load(AssemblyConstants.SdkAssemblyName, parameter) }.ToList();
            
            foreach (var a in assemblies)
                references.Remove(a);

            var c = new AssemblyCompiler(CompilationMode.All, assemblies, references, new Table("pkg.name"),
                                         new NameConverter("pkg.name", ""), true, 
                                         new AssemblyClassLoader(file => { }), definition => null,
                                         new DexMethodBodyCompilerCache(), new HashSet<string>(), 
                                         module, _generateSetNextInstructionCode);

            c.StopCompilationBeforeGeneratingCode = stopBeforeGeneratingCode;
            c.StopAtFirstError = false;
            
            try
            {
                c.Compile();
            }
            catch (AggregateException ex)
            {
                CompilationErrors = ex.Flatten().InnerExceptions.Select(e => e.Message.Replace(": ", "\n//      ").Replace("; ", "\n//      &  ")).ToList();
            }

            if (c.MapFile != null)
            {
                c.MapFile.Optimize();
                MapFile = new MapFileLookup(c.MapFile);
            }

            _compiler = c;
            _previousAssembly = assembly;
            _isFullyCompiled = !stopBeforeGeneratingCode;
        }
 public AssemblyModifiedDetector(Func<AssemblyDefinition, string> filenameFromAssembly, MapFileLookup map)
 {
     _filenameFromAssembly = filenameFromAssembly;
     _map = map;
 }