示例#1
0
        public virtual Expression Decompile(IManagedMethod method, IList <Expression> arguments)
        {
            var elements = this.disassembler.Disassemble(method)
                           .Select(i => (IElement) new InstructionElement(i))
                           .ToList();

            var context = new DecompilationContext(this, method, i => arguments[i]);

            try {
                foreach (var step in this.pipeline.GetSteps())
                {
                    step.Apply(elements, context);
                }
            }
            catch (Exception ex) {
                throw new DecompilationException(
                          "Exception while interpreting "
                          + Environment.NewLine
                          + ElementHelper.ToString(elements, Indent.FourSpaces)
                          + Environment.NewLine
                          + ex.Message, ex
                          );
            }

            return(GetSingleExpression(elements));
        }
示例#2
0
 public virtual IEnumerable<Instruction> Disassemble(IManagedMethod method)
 {
     return this.instructionReaderFactory(
         method.GetBodyByteArray(),
         method.Context
     ).ReadAll();
 }
示例#3
0
 public virtual IEnumerable <Instruction> Disassemble(IManagedMethod method)
 {
     return(this.instructionReaderFactory(
                method.GetBodyByteArray(),
                method.Context
                ).ReadAll());
 }
示例#4
0
 private static LambdaExpression Decompile(IManagedMethod method)
 {
     return(new Decompiler(
                new TestDisassembler((bytes, context) => new InstructionReader(bytes, context)),
                new DefaultPipeline()
                ).Decompile(method));
 }
示例#5
0
        public override IEnumerable<Instruction> Disassemble(IManagedMethod method)
        {
            var assembled = method as TestMethod;
            if (assembled != null)
                return assembled.GetInstructions();

            return base.Disassemble(method);
        }
示例#6
0
        public void TestDecompilationResultHasCorrectParameters(IManagedMethod method)
        {
            var decompiled = Decompile(method);
            var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToList();
            if (!method.IsStatic)
                parameterTypes.Insert(0, method.DeclaringType);

            Assert.AreElementsSame(parameterTypes, decompiled.Parameters.Select(p => p.Type));
        }
示例#7
0
        public virtual LambdaExpression Decompile(IManagedMethod method)
        {
            var parameters = method.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name)).ToList();
            if (!method.IsStatic)
                parameters.Insert(0, Expression.Parameter(method.DeclaringType, "<this>"));

            var body = this.Decompile(method, parameters.Cast<Expression>().ToArray());
            return Expression.Lambda(body, parameters.ToArray());
        }
示例#8
0
        public override IEnumerable <Instruction> Disassemble(IManagedMethod method)
        {
            var assembled = method as TestMethod;

            if (assembled != null)
            {
                return(assembled.GetInstructions());
            }

            return(base.Disassemble(method));
        }
        private static void ApplyPipeline(IDecompilationPipeline pipeline, IList<IElement> elements, IManagedMethod method)
        {
            var parameters = method.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name)).ToList();
            if (!method.IsStatic)
                parameters.Insert(0, Expression.Parameter(method.DeclaringType, "<this>"));

            var context = new DecompilationContext(null, method, i => parameters[i]);
            foreach (var step in pipeline.GetSteps()) {
                step.Apply(elements, context);
            }
        }
示例#10
0
        public void TestDecompilationResultHasCorrectParameters(IManagedMethod method)
        {
            var decompiled     = Decompile(method);
            var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToList();

            if (!method.IsStatic)
            {
                parameterTypes.Insert(0, method.DeclaringType);
            }

            Assert.AreElementsSame(parameterTypes, decompiled.Parameters.Select(p => p.Type));
        }
 public void TestNoExceptionsAreThrownWhenDecompiling(IManagedMethod method, IList<Instruction> instructions)
 {
     var pipeline = new DefaultPipeline().Without<LambdaInliningVisitor>();
     var elements = instructions.Select(i => (IElement)new InstructionElement(i)).ToList();
     Assert.DoesNotThrow(() => {
         try {
             ApplyPipeline(pipeline, elements, method);
         }
         catch (NotSupportedException) {
         }
     });
 }
示例#12
0
        public virtual LambdaExpression Decompile(IManagedMethod method)
        {
            var parameters = method.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name)).ToList();

            if (!method.IsStatic)
            {
                parameters.Insert(0, Expression.Parameter(method.DeclaringType, "<this>"));
            }

            var body = this.Decompile(method, parameters.Cast <Expression>().ToArray());

            return(Expression.Lambda(body, parameters.ToArray()));
        }
示例#13
0
        public void TestNoExceptionsAreThrownWhenDecompiling(IManagedMethod method, IList <Instruction> instructions)
        {
            var pipeline = new DefaultPipeline().Without <LambdaInliningVisitor>();
            var elements = instructions.Select(i => (IElement) new InstructionElement(i)).ToList();

            Assert.DoesNotThrow(() => {
                try {
                    ApplyPipeline(pipeline, elements, method);
                }
                catch (NotSupportedException) {
                }
            });
        }
示例#14
0
        public void TestDecompilesTo(IManagedMethod method, IEnumerable<string> patterns)
        {
            var decompiled = Decompile(method);

            var parameterNames = decompiled.Parameters.Select(p => p.Name).ToArray();
            var expected = patterns.Select(
                p => p.Contains("{0}") ? string.Format(p, parameterNames) : p
            ).ToList();

            if (expected.Count <= 1) {
                Assert.AreEqual(expected.Single(), ToStringVisitor.ToString(decompiled));
            }
            else {
                Assert.Contains(expected, ToStringVisitor.ToString(decompiled));
            }
        }
示例#15
0
        public void TestDecompilesTo(IManagedMethod method, IEnumerable <string> patterns)
        {
            var decompiled = Decompile(method);

            var parameterNames = decompiled.Parameters.Select(p => p.Name).ToArray();
            var expected       = patterns.Select(
                p => p.Contains("{0}") ? string.Format(p, parameterNames) : p
                ).ToList();

            if (expected.Count <= 1)
            {
                Assert.AreEqual(expected.Single(), ToStringVisitor.ToString(decompiled));
            }
            else
            {
                Assert.Contains(expected, ToStringVisitor.ToString(decompiled));
            }
        }
示例#16
0
        public virtual Expression Decompile(IManagedMethod method, IList<Expression> arguments)
        {
            var elements = this.disassembler.Disassemble(method)
                                .Select(i => (IElement)new InstructionElement(i))
                                .ToList();

            var context = new DecompilationContext(this, method, i => arguments[i]);
            try {
                foreach (var step in this.pipeline.GetSteps()) {
                    step.Apply(elements, context);
                }
            }
            catch (Exception ex) {
                throw new DecompilationException(
                    "Exception while interpreting "
                    + Environment.NewLine
                    + ElementHelper.ToString(elements, Indent.FourSpaces)
                    + Environment.NewLine
                    + ex.Message, ex
                );
            }

            return GetSingleExpression(elements);
        }
示例#17
0
 public override void Initialize(DecompilationContext context)
 {
     this.method = context.Method;
 }
 public void TestAllInstructionsCanBeDisassembled(IManagedMethod method)
 {
     var disassembler = ExpressiveEngine.GetDisassembler();
     Assert.DoesNotThrow(() => disassembler.Disassemble(method));
 }
示例#19
0
        public void TestDecompilationResultIsCompilable(IManagedMethod method)
        {
            var decompiled = Decompile(method);

            Assert.DoesNotThrow(() => decompiled.Compile());
        }
示例#20
0
 public DecompilationContext(IDecompiler decompiler, IManagedMethod method, Func <int, Expression> getParameter)
 {
     this.Decompiler   = decompiler;
     this.Method       = method;
     this.GetParameter = getParameter;
 }
示例#21
0
 public DecompilationContext(IDecompiler decompiler, IManagedMethod method, Func<int, Expression> getParameter)
 {
     this.Decompiler = decompiler;
     this.Method = method;
     this.GetParameter = getParameter;
 }
示例#22
0
 public void TestDecompilationResultIsCompilable(IManagedMethod method)
 {
     var decompiled = Decompile(method);
     Assert.DoesNotThrow(() => decompiled.Compile());
 }
示例#23
0
        private static void ApplyPipeline(IDecompilationPipeline pipeline, IList <IElement> elements, IManagedMethod method)
        {
            var parameters = method.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name)).ToList();

            if (!method.IsStatic)
            {
                parameters.Insert(0, Expression.Parameter(method.DeclaringType, "<this>"));
            }

            var context = new DecompilationContext(null, method, i => parameters[i]);

            foreach (var step in pipeline.GetSteps())
            {
                step.Apply(elements, context);
            }
        }
示例#24
0
 private static LambdaExpression Decompile(IManagedMethod method)
 {
     return new Decompiler(
         new TestDisassembler((bytes, context) => new InstructionReader(bytes, context)),
         new DefaultPipeline()
     ).Decompile(method);
 }
示例#25
0
        public void TestAllInstructionsCanBeDisassembled(IManagedMethod method)
        {
            var disassembler = ExpressiveEngine.GetDisassembler();

            Assert.DoesNotThrow(() => disassembler.Disassemble(method));
        }