Beispiel #1
0
    def test_complex_text(self):
        from test.src import ctypes5_gen32
        model.registerModule(ctypes5_gen32)
        # struct a - basic types
        offset = self.offsets['struct_d'][0]
        m = self.mappings.get_mapping_for_address(offset)
        d = m.readStruct(offset, ctypes5_gen32.struct_d)
        ret = d.loadMembers(self.mappings, 10)
        self.assertTrue(ret)
        parser = text.RecursiveTextOutputter(self.mappings)
        out = parser.parse(d)
        # should not fail
        x = eval(out)

        self.assertEquals(len(x.keys()), 15)  # 14 + padding
        self.assertEquals(self.values['struct_d.a'], hex(x['a']))
        self.assertEquals(len(x['b'].keys()), 9)
        self.assertEquals(len(x['b2'].keys()), 8)
        self.assertEquals(int(self.values['struct_d.b.e']), x['b']['e'])
        self.assertEquals(int(self.values['struct_d.b2.e']), x['b2']['e'])

        for i in range(9):
            self.assertEquals(
                int(self.values['struct_d.c[%d].a' % (i)]), x['c'][i]['a'])
            self.assertEquals(
                int(self.values['struct_d.f[%d]' % (i)]), x['f'][i])
        self.assertEquals(int(self.values['struct_d.e']), x['e'])
        self.assertEquals(str(self.values['struct_d.i']), x['i'])
        return
Beispiel #2
0
    def test_complex(self):
        from test.src import ctypes5_gen32
        model.registerModule(ctypes5_gen32)
        # struct a - basic types
        offset = self.offsets['struct_d'][0]
        m = self.mappings.get_mapping_for_address(offset)
        d = m.readStruct(offset, ctypes5_gen32.struct_d)
        ret = d.loadMembers(self.mappings, 10)
        self.assertTrue(ret)

        import ctypes
        self.assertEquals(int(self.sizes['struct_d']), ctypes.sizeof(d))

        parser = python.PythonOutputter(self.mappings)
        obj = parser.parse(d)

        self.assertEquals(d.a.value, self.offsets['struct_d'][0])
        self.assertEquals(d.b.value, self.offsets['struct_d.b'][0])
        self.assertEquals(d.b2.value, self.offsets['struct_d.b2'][0])
        for i in range(9):
            self.assertEquals(
                int(self.values['struct_d.c[%d].a' % (i)]), obj.c[i].a)
            self.assertEquals(
                int(self.values['struct_d.f[%d]' % (i)]), obj.f[i])
        self.assertEquals(int(self.values['struct_d.e']), obj.e)
        self.assertEquals(str(self.values['struct_d.i']), obj.i)

        return
Beispiel #3
0
    def test_basic_types(self):
        from test.src import ctypes5_gen32
        model.registerModule(ctypes5_gen32)
        # struct a - basic types
        offset = self.offsets['struct_a'][0]
        m = self.mappings.get_mapping_for_address(offset)
        a = m.readStruct(offset, ctypes5_gen32.struct_a)
        ret = a.loadMembers(self.mappings, 10)
        self.assertTrue(ret)
        import ctypes
        self.assertEquals(int(self.sizes['struct_a']), ctypes.sizeof(a))

        parser = python.PythonOutputter(self.mappings)
        a = parser.parse(a)

        self.assertEquals(int(self.values['struct_a.a']), a.a)
        self.assertEquals(int(self.values['struct_a.b']), a.b)
        self.assertEquals(int(self.values['struct_a.c']), a.c)
        self.assertEquals(int(self.values['struct_a.d']), a.d)
        self.assertEquals(int(self.values['struct_a.e']), a.e)
        self.assertEquals(float(self.values['struct_a.f']), a.f)
        self.assertEquals(float(self.values['struct_a.g']), a.g)
        self.assertEquals(float(self.values['struct_a.h']), a.h)

        offset = self.offsets['union_au'][0]
        m = self.mappings.get_mapping_for_address(offset)
        au = m.readStruct(offset, ctypes5_gen32.union_au)
        ret = au.loadMembers(self.mappings, 10)
        self.assertTrue(ret)
        au = parser.parse(au)
        self.assertEquals(int(self.values['union_au.d']), au.d)
        self.assertEquals(float(self.values['union_au.g']), au.g)
        self.assertEquals(float(self.values['union_au.h']), au.h)

        return
Beispiel #4
0
    def test_bitfield(self):
        from test.src import ctypes5_gen32
        model.registerModule(ctypes5_gen32)
        # struct a - basic types
        offset = self.offsets['struct_c'][0]
        m = self.mappings.get_mapping_for_address(offset)
        c = m.readStruct(offset, ctypes5_gen32.struct_c)
        ret = c.loadMembers(self.mappings, 10)
        self.assertTrue(ret)

        import ctypes
        self.assertEquals(int(self.sizes['struct_c']), ctypes.sizeof(c))

        parser = python.PythonOutputter(self.mappings)
        c = parser.parse(c)

        self.assertEquals(int(self.values['struct_c.a1']), c.a1)
        self.assertEquals(int(self.values['struct_c.b1']), c.b1)
        self.assertEquals(int(self.values['struct_c.c1']), c.c1)
        self.assertEquals(int(self.values['struct_c.d1']), c.d1)
        self.assertEquals(str(self.values['struct_c.a2']), c.a2)
        self.assertEquals(int(self.values['struct_c.b2']), c.b2)
        self.assertEquals(int(self.values['struct_c.c2']), c.c2)
        self.assertEquals(int(self.values['struct_c.d2']), c.d2)
        self.assertEquals(int(self.values['struct_c.h']), c.h)

        return
 def setUp(self):
     model.reset()
     types.reload_ctypes(4, 4, 8)
     self.memdumpname = 'test/src/test-ctypes7.32.dump'
     self.classname = 'test.src.ctypes7.struct_Node'
     self._load_offsets_values(self.memdumpname)
     self.address = self.offsets['test1'][0]  # 0x8f40008
     # load layout in x32
     from test.src import ctypes7
     from test.src import ctypes7_gen32
     model.copyGeneratedClasses(ctypes7_gen32, ctypes7)
     model.registerModule(ctypes7)
     # apply constraints
     ctypes7.populate()
 def setUp(self):
     model.reset()
     types.reload_ctypes(8, 8, 16)
     self.memdumpname = 'test/src/test-ctypes7.64.dump'
     self.classname = 'test.src.ctypes7.struct_Node'
     self._load_offsets_values(self.memdumpname)
     self.address = self.offsets['test1'][0]  # 0x000000001b1e010
     # load layout in x64
     from test.src import ctypes7
     from test.src import ctypes7_gen64
     model.copyGeneratedClasses(ctypes7_gen64, ctypes7)
     model.registerModule(ctypes7)
     # apply constraints
     ctypes7.populate()
 def setUp(self):
     model.reset()
     self.mappings = dump_loader.load('test/src/test-ctypes6.32.dump')
     self.memdumpname = 'test/src/test-ctypes6.32.dump'
     self._load_offsets_values(self.memdumpname)
     sys.path.append('test/src/')
     from test.src import ctypes6
     from test.src import ctypes6_gen32
     model.copyGeneratedClasses(ctypes6_gen32, ctypes6)
     model.registerModule(ctypes6)
     # apply constraints
     ctypes6.populate(self.mappings.config)
     self.offset = self.offsets['test1'][0]
     self.m = self.mappings.get_mapping_for_address(self.offset)
     self.usual = self.m.readStruct(self.offset, ctypes6.struct_usual)
Beispiel #8
0
    def test_registerModule(self):
        from haystack import model
        model.reset()

        try:
            from test.structures import good
            from test.structures import good_gen
            from test.structures import bad_gen
            # copy bad_gen in good
            model.copyGeneratedClasses(bad_gen, good)
            model.copyGeneratedClasses(good_gen, good)
            self.assertIn('Struct1', good.__dict__)
            self.assertIn('Struct2', good.__dict__)
            self.assertNotIn('Struct1_py', good.__dict__)
            self.assertNotIn('expectedValues', good.Struct1.__dict__)
        except ImportError as e:
            self.fail(e)
        try:
            from test.structures import bad
            # test if module has members
            self.assertEquals(bad.BLOCK_SIZE, 16)
            self.assertIn('Struct1', bad.__dict__)
            self.assertIn('expectedValues', bad.Struct1.__dict__)
            # same Struct1 object is imported in bad and good
            self.assertIn('expectedValues', good.Struct1.__dict__)
            self.assertNotIn('expectedValues', good.Struct2.__dict__)
        except ImportError as e:
            self.fail(e)

        # test if register works (creates POPO)
        model.registerModule(bad)
        self.assertIn('Struct1_py', bad.__dict__)
        self.assertIn('expectedValues', bad.Struct1.__dict__)
        # POPO is not create in good
        self.assertNotIn('Struct1_py', good.__dict__)
        self.assertIn('expectedValues', good.Struct1.__dict__)
        self.assertNotIn('expectedValues', good.Struct2.__dict__)

        model.registerModule(good)  # creates POPO for the rest
        self.assertIn('Struct2_py', good.__dict__)
        self.assertIn('expectedValues', good.Struct1.__dict__)
        # expectedValues is in a function
        self.assertNotIn('expectedValues', good.Struct2.__dict__)

        # add an expectedValues
        good.populate()
        self.assertIn('expectedValues', good.Struct1.__dict__)
        self.assertIn('expectedValues', good.Struct2.__dict__)
 def setUp(self):
     model.reset()
     types.reload_ctypes(8, 8, 16)
     class MyConfig:
         def get_word_size(self):
             return 8
     self.memdumpname = 'test/src/test-ctypes6.64.dump'
     self.node_structname = 'test.src.ctypes6.struct_Node'
     self.usual_structname = 'test.src.ctypes6.struct_usual'
     self._load_offsets_values(self.memdumpname)
     self.address1 = self.offsets['test1'][0]  # struct_usual
     self.address2 = self.offsets['test2'][0]  # struct_Node
     self.address3 = self.offsets['test3'][0]  # struct_Node
     # load layout in x64
     from test.src import ctypes6
     from test.src import ctypes6_gen64
     model.copyGeneratedClasses(ctypes6_gen64, ctypes6)
     model.registerModule(ctypes6)
     # apply constraints
     ctypes6.populate(MyConfig())
Beispiel #10
0
    def test_basic_signed_types(self):
        from test.src import ctypes5_gen32
        model.registerModule(ctypes5_gen32)
        # struct a - basic types
        offset = self.offsets['union_b'][0]
        m = self.mappings.get_mapping_for_address(offset)
        b = m.readStruct(offset, ctypes5_gen32.union_b)
        ret = b.loadMembers(self.mappings, 10)
        self.assertTrue(ret)
        import ctypes
        parser = python.PythonOutputter(self.mappings)
        b = parser.parse(b)

        self.assertEquals(int(self.values['union_b.a']), b.a)
        self.assertEquals(int(self.values['union_b.b']), b.b)
        self.assertEquals(int(self.values['union_b.c']), b.c)
        self.assertEquals(int(self.values['union_b.d']), b.d)
        self.assertEquals(int(self.values['union_b.e']), b.e)
        # char 251
        self.assertEquals((self.values['union_b.g']), b.g)

        return
    ## do next_chunk
    mmap = model.is_valid_address_value(orig_addr, mappings)
    if not mmap:
      raise ValueError
    next_addr = orig_addr + self.real_size()
    # check if its in mappings
    if not model.is_valid_address_value(next_addr, mappings):
      return None,None
    next_chunk = mmap.readStruct(next_addr, malloc_chunk )
    model.keepRef( next_chunk, malloc_chunk, next_addr)
    return next_chunk, next_addr



malloc_chunk._fields_ = [
    ( 'prev_size' , UINT ), #  INTERNAL_SIZE_T
    ( 'size' , UINT ), #  INTERNAL_SIZE_T with some flags
    # totally virtual
   ]
# make subclass for empty or inuse..

# cant use 2** expectedValues, there is a mask on sizes...
malloc_chunk.expectedValues = {    }




model.registerModule(sys.modules[__name__])