public void Setup()
		{
            mem = new MemoryArea(Address.Ptr32(0x00100000), new byte[1024]);
            var arch = new FakeArchitecture();
            this.program = new Program
            {
                Architecture = arch,
                SegmentMap = new SegmentMap(
                    mem.BaseAddress,  
                    new ImageSegment(".text", mem, AccessMode.ReadWriteExecute)),
                Platform = new DefaultPlatform(null, arch),
            };
            store = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
			store.EnsureExpressionTypeVariable(factory, globals);

			StructureType s = new StructureType(null, 0);
			s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

			TypeVariable tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
			EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);
			eqGlobals.DataType = s;
			globals.TypeVariable.DataType = new Pointer(eqGlobals, 4);
			globals.DataType = globals.TypeVariable.DataType;
		}
		public void Setup()
		{
            var image = new LoadedImage(Address.Ptr32(0x00100000), new byte[1024]);
            var arch = new FakeArchitecture();
            var program = new Program
            {
                Image = image,
                Architecture = arch,
                ImageMap = image.CreateImageMap(),
                Platform = new DefaultPlatform(null, arch),
            };
            store = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
			store.EnsureExpressionTypeVariable(factory, globals);

			StructureType s = new StructureType(null, 0);
			s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

			TypeVariable tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
			EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);
			eqGlobals.DataType = s;
			globals.TypeVariable.DataType = new Pointer(eqGlobals, 4);
			globals.DataType = globals.TypeVariable.DataType;

            tcr = new TypedConstantRewriter(program);
		}
Exemple #3
0
        public StructureMerger(ICollection<StructureType> structures, ICollection<EquivalenceClass> eqClasses)
		{
			this.structures = structures;
			this.eqClasses = eqClasses;
			foreach (EquivalenceClass eq in eqClasses)
			{
				if (eqMin == null)
					eqMin = eq;
				else if (eq.Number < eqMin.Number)
					eqMin = eq;
			}
		}
		public void Merge1()
		{
			TypeVariable tv1 = new TypeVariable(1);
			TypeVariable tv2 = new TypeVariable(2);
            StructureType s1 = new StructureType { Fields = { { 4, PrimitiveType.Pointer32 } } };
            StructureType s2 = new StructureType { Fields = { { 4, PrimitiveType.Pointer32 } } };
			EquivalenceClass c1 = new EquivalenceClass(tv1);
			EquivalenceClass c2 = new EquivalenceClass(tv2);
			c1.DataType = s1;
			c2.DataType = s2;
			StructureMerger sm = new StructureMerger(new StructureType[] { s1, s2 }, new EquivalenceClass[] { c1, c2 } );
			sm.Merge();
			Assert.AreEqual("(struct (4 ptr32 ptr0004))", c1.DataType.ToString());
		}
		public void Tmer_PointerToSingleItem()
		{
			var ptr = new Identifier("ptr", PrimitiveType.Word32, null);
			var tv = store.EnsureExpressionTypeVariable(factory, ptr);
			tv.OriginalDataType = new Pointer(point, 4);
			var eq = new EquivalenceClass(tv);
			eq.DataType = point;
			tv.DataType = new Pointer(eq, 4);

			TypedExpressionRewriter tmer = new TypedExpressionRewriter(program);
            var access = Wrap(new MemoryAccess(ptr, PrimitiveType.Word32));
            TypeVariable tvAccess = access.TypeVariable;
            tvAccess.DataType = PrimitiveType.Word32;
            Expression e = access.Accept(tmer);
			Assert.AreEqual("ptr->dw0000", e.ToString());
		}
        public void Tmer_PointerToSecondItemOfPoint()
        {
            Identifier ptr = new Identifier("ptr", PrimitiveType.Word32, null);
            store.EnsureExpressionTypeVariable(factory, ptr);
            EquivalenceClass eqPtr = new EquivalenceClass(ptr.TypeVariable);
            eqPtr.DataType = point;
            ptr.TypeVariable.OriginalDataType = new Pointer(point, 4);
            ptr.TypeVariable.DataType = new Pointer(eqPtr, 4);

            var c = Wrap(Constant.Word32(4));
            var bin = Wrap(new BinaryExpression(BinaryOperator.IAdd, PrimitiveType.Word32, ptr, c));
            var mem = Wrap(new MemoryAccess(bin, PrimitiveType.Word32));
            var tmer = new TypedExpressionRewriter(program);
            Expression e = mem.Accept(tmer);
            Assert.AreEqual("ptr->dw0004", e.ToString());
        }
 private void Globals(params StructureField[] fields)
 {
     var arch = new Mocks.FakeArchitecture();
     this.prog = new Program(
         image,
         image.CreateImageMap(),
         arch,
         new DefaultPlatform(null, arch));
     var globalStruct = new StructureType();
     globalStruct.Fields.AddRange(fields);
     prog.Globals.TypeVariable = new TypeVariable("globals_t", 1) { DataType = globalStruct };
     var eq = new EquivalenceClass(prog.Globals.TypeVariable);
     eq.DataType = globalStruct;
     var ptr = new Pointer(eq, 4);
     prog.Globals.TypeVariable.DataType = ptr;
 }
		public static bool ReplaceAll(TypeFactory factory, TypeStore store)
		{
			EquivalenceClass [] eqs = new EquivalenceClass[store.UsedEquivalenceClasses.Count];
			store.UsedEquivalenceClasses.CopyTo(eqs, 0);
			bool changed = false;
			for (int i = 0; i < eqs.Length; ++i)
			{
				if (eqs[i].DataType != null)
				{
					NestedComplexTypeExtractor nctr = new NestedComplexTypeExtractor(factory, store);
					eqs[i].DataType = eqs[i].DataType.Accept(nctr);
					changed |= nctr.Changed;
				}
			}
			return changed;
		}
Exemple #9
0
 public void HasCoincidentUnion()
 {
     var eq = new EquivalenceClass(
         new TypeVariable(42),
         new UnionType(null, null,
             PrimitiveType.SegPtr32, PrimitiveType.Word16));
     var s = new StructureType(null, 0)
     {
         Fields =
         {
             { 0, eq },
             { 0, PrimitiveType.SegmentSelector }
         }
     };
     TypeTransformer trans = new TypeTransformer(factory, null, null);
     Assert.IsTrue(trans.HasCoincidentFields(s));
 }
        public void NotMatch()
        {
            TypeVariable t1 = new TypeVariable(1);
            TypeVariable t2 = new TypeVariable(2);
            EquivalenceClass c1 = new EquivalenceClass(t1);
            EquivalenceClass c2 = new EquivalenceClass(t2);
            c1.DataType = new StructureType{ Fields = { { 4, PrimitiveType.Word16 } } };
            c2.DataType = new StructureType { Fields = { { 20, PrimitiveType.Word32 } } };
            t1.Class = c1;
            t2.Class = c2;

            UnionType u = new UnionType(null, null);
            u.Alternatives.Add(new Pointer(c1, 4));
            u.Alternatives.Add(new Pointer(PrimitiveType.Word16, 4));

            UnionPointersStructuresMatcher upsm = new UnionPointersStructuresMatcher();
            Assert.IsFalse(upsm.Match(u));
        }
		public void Setup()
		{
			store = new TypeStore();
			factory = new TypeFactory();
            globals = new Identifier("globals", PrimitiveType.Word32, null);
            StructureType point = new StructureType("Point", 0)
            {
                Fields = {
                    { 0, PrimitiveType.Word32, null },
			        { 4, PrimitiveType.Word32, null }
                }
            };
			TypeVariable tvPoint = store.CreateTypeVariable(factory);
            EquivalenceClass eq = new EquivalenceClass(tvPoint)
            {
                DataType = point
            };
			tvPoint.DataType = eq;
			ptrPoint = new Pointer(eq, 4);

            UnionType u = new UnionType("RealInt", null)
            {
                Alternatives = {
                    new UnionAlternative("w", PrimitiveType.Word32, 0),
			        new UnionAlternative("r", PrimitiveType.Real32, 1),
                }
            };
			TypeVariable tvUnion = store.CreateTypeVariable(factory);
            eq = new EquivalenceClass(tvUnion)
            {
                DataType = u
            };
			tvUnion.DataType = eq;
			ptrUnion = new Pointer(eq, 4);

            ptrInt = new Pointer(PrimitiveType.Int32, 4);
            ptrWord = new Pointer(PrimitiveType.Word32, 4);
            m = new ExpressionEmitter();
		}
        public void Setup()
        {
            mr = new MockRepository();
            arch = mr.Stub<IProcessorArchitecture>();
            arch.Stub(a => a.CreateImageReader(null, 0u)).IgnoreArguments().Do(new Func<LoadedImage, ulong, ImageReader>((i, o) => i.CreateLeReader(o)));
            arch.Replay();
            globalStruct = new StructureType
            {
            };
            globals_t = new TypeVariable("globals_t", 1) { DataType = globalStruct };
            globals = new Identifier("globals", PrimitiveType.Pointer32, null);

            eqLink = new EquivalenceClass(new TypeVariable(2));
            StructureType str = new StructureType
            {
                Fields = {
                    { 0, new Pointer(eqLink, 4)},
                    { 4, PrimitiveType.Int32 }
                }
            };
            eqLink.DataType = str;
        }
Exemple #13
0
 public void VisitEquivalenceClass(EquivalenceClass eq)
 {
     throw new NotImplementedException();
 }
Exemple #14
0
 public void VisitEquivalenceClass(EquivalenceClass eq)
 {
     throw new NotImplementedException();
 }
        public void GdwStructure()
        {
            Memory(0x1000)
                .WriteLeUInt16(4)
                .WriteLeUInt16(unchecked((ushort) -104));
            var eqStr = new EquivalenceClass(new TypeVariable(2));
            var str = new StructureType
            {
                Name = "Eq_2",
                Fields = {
                    { 0, PrimitiveType.Int16 },
                    { 2, PrimitiveType.Int16 },
                }
            };
            eqStr.DataType = str;
            Globals(
                Field(0x1000, eqStr));
            RunTest(
@"Eq_2 g_t1000 = 
{
    4,
    -104,
};
");
        }
Exemple #16
0
        public override DataType VisitEquivalenceClass(EquivalenceClass eq)
        {
            if (!classesVisited.Contains(eq))
            {
                classesVisited.Add(eq);
                if (eq.DataType != null)
                {
                    eq.DataType = eq.DataType.Accept(this);
                }
            }

            DataType dt = eq.DataType;
            PrimitiveType pr = dt as PrimitiveType;
            if (pr != null)
            {
                changed = true;
                return pr;
            }
            if (dt is VoidType)
            {
                changed = true;
                return dt;
            }
            if (dt is CodeType)
            {
                changed = true;
                return dt;
            }
            Pointer ptr = dt as Pointer;
            if (ptr != null)
            {
                changed = true;
                DataType pointee = eq;
                return factory.CreatePointer(pointee, ptr.Size);
            }
            MemberPointer mp = dt as MemberPointer;
            if (mp != null)
            {
                changed = true;
                return factory.CreateMemberPointer(mp.BasePointer, eq, mp.Size);
            }
            ArrayType array = dt as ArrayType;
            if (array != null)
            {
                changed = true;
                return factory.CreateArrayType(array.ElementType, array.Length);
            }
            EquivalenceClass eq2 = dt as EquivalenceClass;
            if (eq2 != null)
            {
                changed = true;
                return eq2;
            }
            return eq;
        }
        protected void setUp()
        {
            //test a combination from 3 elements
            structure = new TestCasesStructure();
            Element          elem1 = new Element(structure);
            EquivalenceClass eq11  = new EquivalenceClass(elem1);
            EquivalenceClass eq12  = new EquivalenceClass(elem1);
            EquivalenceClass eq13  = new EquivalenceClass(elem1);
            Element          elem2 = new Element(structure);
            EquivalenceClass eq21  = new EquivalenceClass(elem2);
            EquivalenceClass eq22  = new EquivalenceClass(elem2);
            EquivalenceClass eq23  = new EquivalenceClass(elem2);
            Element          elem3 = new Element(structure);
            EquivalenceClass eq31  = new EquivalenceClass(elem3);
            EquivalenceClass eq32  = new EquivalenceClass(elem3);
            EquivalenceClass eq33  = new EquivalenceClass(elem3);

            dep  = new Dependency(structure);
            dep2 = new Dependency(structure);
            dep3 = new Dependency(structure);
            foreach (Element element in structure.Elements)
            {
                dep.AddElement(element);
                foreach (EquivalenceClass equivalenceClass in element.EquivalenceClasses)
                {
                    dep.AddEquivalenceClass(equivalenceClass);
                }
            }

            Combination combi4;
            Combination combi3;
            Combination combi1;
            Combination combi2;

            combi1 = new Combination(dep);
            combi2 = new Combination(dep2);
            combi3 = new Combination(dep);
            combi4 = new Combination(dep3);

            combi1.AddEquivalenceClass(eq11);
            combi1.AddEquivalenceClass(eq22);
            combi1.AddEquivalenceClass(eq33);


            combi2.AddEquivalenceClass(eq21);
            combi2.AddEquivalenceClass(eq11);
            combi2.AddEquivalenceClass(eq32);

            combi3.AddEquivalenceClass(eq13);
            combi3.AddEquivalenceClass(eq23);
            combi3.AddCombination(combi4);


            test1 = new TestCase(structure);
            test2 = new TestCase(structure);
            test3 = new TestCase(structure);
            test4 = new TestCase(structure);
            test5 = new TestCase(structure);


            test1.AddCombination(combi1);
            test1.AddCombination(combi4);

            test2.AddCombination(combi3);
            test2.AddCombination(combi4);

            test3.AddCombination(combi2);
            test3.AddCombination(combi1);

            test4.AddCombination(combi3);

            test5.AddCombination(combi2);
            test5.AddCombination(combi4);
        }
Exemple #18
0
        private static IList <EquivalenceClass> BuildMergePattern(Combination p_combination, EquivalenceClass p_equivalenceClass)
        {
            List <EquivalenceClass> mergePattern = new List <EquivalenceClass>();

            mergePattern.Add(p_equivalenceClass);

            Element parentElement = p_equivalenceClass.ParentElement;

            //add all equivalence classes from the combination (that are does not have the same parent element)
            foreach (EquivalenceClass eClass in p_combination.EquivalenceClasses)
            {
                if (eClass.ParentElement != parentElement &&
                    !mergePattern.Contains(eClass))
                {
                    mergePattern.Add(eClass);
                }
            }
            //look also in the children combinations recursively
            foreach (Combination childCombination in p_combination.Combinations)
            {
                IList <EquivalenceClass> partOfMergePattern = BuildMergePattern(childCombination, p_equivalenceClass);
                //add those equivalence classes to the pattern
                foreach (EquivalenceClass equivalenceClass in partOfMergePattern)
                {
                    if (!mergePattern.Contains(equivalenceClass))
                    {
                        mergePattern.Add(equivalenceClass);
                    }
                }
            }
            return(mergePattern);
        }
Exemple #19
0
        public void TypeReference()
        {
            tyreffo = new TypeReferenceFormatter(new TextFormatter(sw), true);
            EquivalenceClass b = new EquivalenceClass(new TypeVariable(1));
            b.DataType = new StructureType("b", 0) { Fields = { { 4, PrimitiveType.Word32 } } };

            tyfo.Write(new Pointer(b, 2), "pb");
            Assert.AreEqual("b * pb", sw.ToString());
        }
Exemple #20
0
 public DataType VisitEquivalenceClass(EquivalenceClass eq)
 {
     return(eq);
 }
 public void CfScopeResolution()
 {
     var eq = new EquivalenceClass(new TypeVariable("Eq_2", 2));
     var sr = new ScopeResolution(eq);
     var e = new FieldAccess(PrimitiveType.Int32, sr, new StructureField(4, PrimitiveType.Int32, "i0004"));
     e.Accept(cf);
     Assert.AreEqual("Eq_2::i0004", sw.ToString());
 }
 public SerializedType VisitEquivalenceClass(EquivalenceClass eq)
 {
     throw new NotImplementedException();
 }
Exemple #23
0
 public void CompatiblePtrToArray()
 {
     var eq = new EquivalenceClass(new TypeVariable(3));
     var a = new ArrayType(eq, 0);
     var p1 = new Pointer(eq, 4);
     var p2 = new Pointer(a, 4);
     Assert.IsTrue(un.AreCompatible(p1, p2));
 }
Exemple #24
0
 public Expression VisitEquivalenceClass(EquivalenceClass eq)
 {
     throw new NotImplementedException();
 }
        public void CptTree()
        {
            var eqTreeNode = new EquivalenceClass(new TypeVariable(2));
            var str = new StructureType
            {
                Fields = {
                    { 0, PrimitiveType.UInt32 },
                    { 4, new Pointer(eqTreeNode, 4) },  // Left
                    { 8, new Pointer(eqTreeNode, 4) },  // Right
                }
            };
            eqTreeNode.DataType = str;

            Memory(0x10000)
                .WriteLeUInt32(0)               // 00: Padding
                .WriteLeUInt32(0)               // 04: Padding
                .WriteLeUInt32(0x746F6F52)      // 08: Ascii 'Root'
                .WriteLeUInt32(0x00010014)      // 0C: Ptr to Left node
                .WriteLeUInt32(0x00010020)      // 10: Right node
                .WriteLeUInt32(0x7466654C)      // 14: Ascii 'Left'
                .WriteLeUInt32(0x00000000)      // 18: no left node
                .WriteLeUInt32(0x00000000)      // 1C: no right node
                .WriteLeUInt32(0x65746952)      // 20: Ascii 'Rite'
                .WriteLeUInt32(0x00000000)      // 24: no left node
                .WriteLeUInt32(0x00000000);     // 28: no right node
            Root(0x10008, eqTreeNode);
            var cpt = new ConstantPointerTraversal(arch, globalStruct, image);
            cpt.Traverse();

            Assert.AreEqual(2, cpt.Discoveries.Count);
            Assert.AreEqual("10014: t10014: Eq_2", cpt.Discoveries[0].ToString());
            Assert.AreEqual("10020: t10020: Eq_2", cpt.Discoveries[1].ToString());
            Assert.AreEqual("10020: t10020: Eq_2", cpt.Discoveries[1].ToString());
        }
        public void GdwVisitLinkedList()
        {
            Memory(0x1000)
                .WriteLeUInt32(1)
                .WriteLeUInt32(0x1008)
                .WriteLeUInt32(2)
                .WriteLeUInt32(0x0000)
                .WriteLeUInt32(0x1000);
            var eqLink = new EquivalenceClass(new TypeVariable(2));
            var link = new StructureType
            {
                Name = "Eq_2",
                Fields = {
                    { 0, PrimitiveType.Int32 },
                    { 4, new Pointer(eqLink, 4) }
                }
            };
            eqLink.DataType = link;
            Globals(
                Field(0x1000, eqLink),
                Field(0x1008, eqLink),
                Field(0x1010, new Pointer(eqLink, 4)));
            RunTest(
@"Eq_2 g_t1000 = 
{
    1,
    &g_t1008,
};
Eq_2 g_t1008 = 
{
    2,
    null,
};
Eq_2* g_ptr1010 = &g_t1000;
");
        }
 public IEnumerable <WorkItem> VisitEquivalenceClass(EquivalenceClass eq)
 {
     return(eq.DataType.Accept(this));
 }