public static GraphProvider Create(MethodDef method)
        {
            var methodName = IdentifierEscaper.Escape(method.Name);

            switch (method.MethodBody)
            {
            case CilBody _:
            {
                var arch          = new CilArchitecture(method);
                var stateResolver = new CilStateTransitionResolver(arch);
                var cflowBuilder  = new SymbolicFlowGraphBuilder <Instruction>(arch, method.Body.Instructions, stateResolver);
                var cflow         = cflowBuilder.ConstructFlowGraph(0);
                return(new ControlFlowGraphProvider <Instruction>(methodName, cflow));
            }

            case NativeMethodBody _:
            {
                var cflow = IcedHelpers.ReadNativeMethodBody(method);
                return(new ControlFlowGraphProvider <Iced.Intel.Instruction>(methodName, cflow));
            }

            default:
                throw new Exception("Tried to create graph for method that has neither managed nor native body");
            }
        }
Exemple #2
0
        public void ConditionalBranchShouldHaveTwoOutgoingEdges()
        {
            var method = Helpers.GetTestMethod(typeof(TestClass), nameof(TestClass.GetIsEvenString));

            var arch         = new CilArchitecture(method);
            var resolver     = new CilStaticSuccessorResolver();
            var graphBuilder = new StaticFlowGraphBuilder <Instruction>(arch, arch.Method.Body.Instructions, resolver);

            var graph = graphBuilder.ConstructFlowGraph(0);

            Assert.Equal(2, graph.Entrypoint.OutDegree);
        }
        public void LdArgInstructionShouldMatchReadParameter(string name, string parameterName, int index)
        {
            var method = Helpers.GetTestMethod(typeof(TestClass), name);
            var arch   = new CilArchitecture(method);

            var param           = method.Parameters[index];
            var testInstruction = Instruction.Create(OpCodes.Ldarg, param);

            var readVariables = new IVariable[1];

            arch.GetReadVariables(testInstruction, readVariables);

            Assert.Equal(parameterName, readVariables[0].Name);
        }
Exemple #4
0
        public void BranchlessMethodShouldHaveSingleBlock()
        {
            var method = Helpers.GetTestMethod(typeof(TestClass), nameof(TestClass.GetConstantString));

            var arch         = new CilArchitecture(method);
            var resolver     = new CilStaticSuccessorResolver();
            var graphBuilder = new StaticFlowGraphBuilder <Instruction>(arch, arch.Method.Body.Instructions, resolver);

            var graph = graphBuilder.ConstructFlowGraph(0);

            Assert.Single(graph.Nodes);
            Assert.Empty(graph.GetEdges());
            Assert.Equal(0, graph.Entrypoint.OutDegree);
        }
        private void RunTest(string sExp, params byte[] bytes)
        {
            var image = new ByteMemoryArea(Address.Ptr32(0x0100000), bytes);
            var sc    = new ServiceContainer();

            sc.AddService <ITestGenerationService>(new UnitTestGenerationService(sc));
            var arch = new CilArchitecture {
                Services = sc,
            };
            var dasm = new CilDisassembler(arch, image.CreateLeReader(0)).GetEnumerator();

            Assert.IsTrue(dasm.MoveNext());
            var instr = dasm.Current;

            Assert.AreEqual(sExp, instr.ToString());
        }
        public void GetReadParametersInStaticContextShouldStartAtZeroIndex()
        {
            var module = ModuleDefinition.FromFile(typeof(TestClass).Assembly.Location);
            var type   = (TypeDefinition)module.LookupMember(typeof(TestClass).MetadataToken);
            var method = type.Methods
                         .First(m => m.Name == nameof(TestClass.StaticMethod));

            var architecture = new CilArchitecture(method.CilMethodBody);

            var readVariables = new IVariable[1];

            architecture.GetReadVariables(new CilInstruction(CilOpCodes.Ldarg_0), readVariables);

            Assert.Equal(new[] { method.Parameters[0] }, readVariables
                         .Cast <CilParameter>()
                         .Select(p => p.Parameter));
        }
        public void DataFlowGraphShouldBeCorrectOnConditional()
        {
            var method = Helpers.GetTestMethod(typeof(TestClass), nameof(TestClass.GetIsEvenString));

            var arch         = new CilArchitecture(method);
            var resolver     = new CilStateTransitionResolver(arch);
            var graphBuilder = new SymbolicFlowGraphBuilder <Instruction>(arch, arch.Method.Body.Instructions, resolver);

            _ = graphBuilder.ConstructFlowGraph(0);

            var graph = resolver.DataFlowGraph;

            // check that `arg0 % 2` is correctly turned into dfg
            var remNode = Assert.Single(graph.Nodes, n => n.StackDependencies.Count == 2);

            Assert.Single(remNode !.StackDependencies, n => Assert.Single(n) !.Node.Contents.IsLdarg());
            Assert.Single(remNode !.StackDependencies, n => Assert.Single(n) !.Node.Contents.IsLdcI4());
            Assert.Single(remNode.GetDependants());
        }