public void Analyze(AssemblyDefinition assembly)
 {
     if (DebuggerOptions.Enabled)
     {
         assembly.Accept(this._documentsCollector);
     }
 }
        private static IEnumerable <AssemblyDefinition> ResolveWindowsRuntimeReferences(AssemblyDefinition assembly)
        {
            TypeReferenceVisitor visitor = new TypeReferenceVisitor(assembly.MainModule);

            assembly.Accept(visitor);
            return(visitor.ResolvedAssemblies);
        }
        /// <summary>
        ///     Modifies the current <paramref name="target" /> to support third-party method call interception.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="typeFilter">The filter that will determine the target types that will be modified.</param>
        /// <param name="hostMethodFilter">The filter that will determine the methods that will be modified on the target type.</param>
        /// <param name="methodCallFilter">
        ///     The filter that will determine which third-party methods will be intercepted on the
        ///     target type.
        /// </param>
        public static void InterceptMethodCalls(this AssemblyDefinition target,
                                                Func <TypeReference, bool> typeFilter,
                                                Func <MethodReference, bool> hostMethodFilter,
                                                Func <MethodReference, bool> methodCallFilter)
        {
            var rewriter = new InterceptMethodCalls(hostMethodFilter, methodCallFilter);

            target.Accept(new ImplementModifiableType(typeFilter));
            target.WeaveWith(rewriter);
        }
        /// <summary>
        ///     Intercepts all method bodies on the target item.
        /// </summary>
        /// <param name="target">The target to be modified.</param>
        /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
        public static void InterceptMethodBody(this AssemblyDefinition target,
                                               Func <MethodReference, bool> methodFilter)
        {
            var typeFilter = GetTypeFilter();

            target.Accept(new ImplementModifiableType(typeFilter));

            IMethodWeaver interceptMethodBody = new InterceptMethodBody(methodFilter);

            target.WeaveWith(interceptMethodBody);
        }
        /// <summary>
        ///     Modifies the current <paramref name="target" /> to support third-party method call interception for all method
        ///     calls made inside the target.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="methodCallFilter">
        ///     The <see cref="IMethodCallFilter" /> instance that determines the method calls that will
        ///     be intercepted.
        /// </param>
        /// <param name="hostMethodFilter">
        ///     The <see cref="IMethodFilter" /> instance that determines the host method calls that
        ///     will be modified
        /// </param>
        public static void InterceptMethodCalls(this AssemblyDefinition target,
                                                IMethodCallFilter methodCallFilter,
                                                IMethodFilter hostMethodFilter)
        {
            var module  = target.MainModule;
            var methods = module.Types.Where(t => GetDefaultTypeFilter()(t)).SelectMany(t => t.Methods)
                          .Where(hostMethodFilter.ShouldWeave).ToArray();

            var rewriter = new InterceptMethodCalls(methodCallFilter);

            target.Accept(new ImplementModifiableType(GetDefaultTypeFilter()));
            target.WeaveWith(rewriter);
        }
Example #6
0
        public Engine(MethodInfo method)
        {
            Type type = method.DeclaringType;
            AssemblyDefinition asm = AssemblyFactory.GetAssembly(type.Assembly.Location);
            var finder             = new MethodFinder(method);

            asm.Accept(finder);
            if (finder.Found)
            {
                this.EntryPoint = finder.Method;
            }
            else
            {
                throw new ArgumentException("Could not find the method");
            }
        }
Example #7
0
        public void Link()
        {
            outputAssembly = AssemblyFactory.GetAssembly(Assemblies [0]);
            mainModule     = outputAssembly.MainModule;
            if (mainModule.Name != MainModuleName)
            {
                Console.Error.WriteLine("Main module is not named \"" + MainModuleName + "\" in assembly " + outputAssembly.Name.FullName);
                Environment.Exit(1);
            }
            mainType = mainModule.Types [MainTypeName];
            if (mainType == null)
            {
                Console.Error.WriteLine("Main module does not contain type \"" + MainTypeName + "\" in assembly " + outputAssembly.Name.FullName);
                Environment.Exit(1);
            }
            outputAssembly.Accept(new StructureMerger(this, outputAssembly, outputAssembly));

            for (int i = 1; i < Assemblies.Count; i++)
            {
                AssemblyDefinition asm = AssemblyFactory.GetAssembly(Assemblies [i]);
                asm.Accept(new StructureMerger(this, outputAssembly, asm));
            }

            FixReflectionAfterMerge fix = new FixReflectionAfterMerge(this, outputAssembly, outputAssembly);

            fix.Process();

            nativeLibraries.AddExternalMethods(this);

            if (OutputIsExecutable)
            {
                outputAssembly.Kind       = AssemblyKind.Console;
                outputAssembly.EntryPoint = InternalSymbols.EntryPoint;
            }
            else
            {
                outputAssembly.Kind = AssemblyKind.Dll;
            }
            AssemblyFactory.SaveAssembly(outputAssembly, OutputPath);
        }
Example #8
0
        public static void Copy(AssemblyDefinition source, AssemblyDefinition target, AssemblyDefinition nsubstitute, string[] typesToCopy)
        {
            var processTypeResolver = new ProcessTypeResolver(source);
            var typeDefinitions     = processTypeResolver.Resolve(typesToCopy).ToList();

            // Copy in two passes to be able to move references within individual classes to other faked types.
            // Note that this functionality is not currently in use, but could prove useful for a later stage,
            // and rather than extracting the logic now, it is better to design it for this purpose up front.

            foreach (var type in typeDefinitions)
            {
                CopyType(target, type);
            }

            foreach (var type in typeDefinitions)
            {
                var typeDefinition = target.MainModule.Types.Single(t => t.FullName == k_PrefixName + type.FullName);
                CopyTypeMembers(target, type, typeDefinition);
            }

            var visitor = new MockInjectorVisitor(nsubstitute, target.MainModule);

            target.Accept(visitor);
        }
Example #9
0
        static void Process(MergeContext context)
        {
            AssemblyDefinition primary = null;

            try {
                primary = AssemblyFactory.GetAssembly(context.Assemblies [0]);

                for (int i = 1; i < context.Assemblies.Count; i++)
                {
                    AssemblyDefinition asm = AssemblyFactory.GetAssembly(context.Assemblies [i]);
                    asm.Accept(new StructureMerger(context, primary, asm));
                }
            } catch (FileNotFoundException e) {
                Error(e.Message);
            }

            FixReflectionAfterMerge fix = new FixReflectionAfterMerge(context, primary, primary);

            fix.Process();

            AssemblyFactory.SaveAssembly(primary, context.OutputPath);

            ConfigMerger.Process(context.Assemblies, context.OutputPath);
        }