Example #1
0
    def test_offsetof(self):
        """returns the offset of a member fields in a record"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        class Y(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        o = my_utils.offsetof(Y, 'b')
        self.assertEquals(o, 8)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        o = my_utils.offsetof(X, 'b')
        self.assertEquals(o, 16)

        class X2(my_ctypes.Union):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        o = my_utils.offsetof(X2, 'b')
        self.assertEquals(o, 0)
        pass
Example #2
0
    def test_get_pointee_address(self):
        """tests get_pointee_address on host ctypes POINTER and haystack POINTER"""
        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        self.assertEquals(my_ctypes.sizeof(X), 17)
        i = X.from_buffer_copy(
            b'\xAA\xAA\xBB\xBB' +
            4 *
            '\xBB' +
            8 *
            '\x11' +
            '\xCC')
        a = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_ctypes.sizeof(i.p), 8)
        self.assertNotEquals(a, 0)
        self.assertEquals(a, 0x1111111111111111)  # 8*'\x11'
        # null pointer
        i = X.from_buffer_copy(
            b'\xAA\xAA\xBB\xBB' +
            4 *
            '\xBB' +
            8 *
            '\x00' +
            '\xCC')
        pnull = my_utils.get_pointee_address(i.p)
        self.assertEquals (my_utils.get_pointee_address(pnull), 0)

        # change arch, and retry
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)

        class Y(ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        self.assertEquals(my_ctypes.sizeof(Y), 9)
        i = Y.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\x11' + '\xCC')
        a = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_ctypes.sizeof(i.p), 4)
        self.assertNotEquals(a, 0)
        self.assertEquals(a, 0x11111111)  # 4*'\x11'
        # null pointer
        i = Y.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\x00' + '\xCC')
        pnull = my_utils.get_pointee_address(i.p)
        self.assertEquals (my_utils.get_pointee_address(pnull), 0)

        # non-pointer, and void null pointer
        my_ctypes = types.load_ctypes_default()
        i = my_ctypes.c_int(69)
        self.assertEquals (my_utils.get_pointee_address(i), 0)
        pnull = my_ctypes.c_void_p(0)
        self.assertEquals (my_utils.get_pointee_address(pnull), 0)

        pass
Example #3
0
    def test_array2bytes(self):
        """array to bytes"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(
            b'\x04' + 3 * b'\x00' + b'\x01' + 3 * b'\x00' + b'\x01' +
            3 * b'\x00' + b'\x01' + 3 * b'\x00' + b'\x02' + 3 * b'\x00' +
            7 * 4 * '\x00', x)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(
            b'\x04' + 7 * b'\x00' + b'\x01' + 7 * b'\x00' + b'\x01' +
            7 * b'\x00' + b'\x01' + 7 * b'\x00' + b'\x02' + 7 * b'\x00' +
            7 * 8 * '\x00', x)

        a = (my_ctypes.c_char * 12).from_buffer_copy('1234567890AB')
        x = my_utils.array2bytes(a)
        self.assertEquals(b'1234567890AB', x)

        # mimics what ctypes gives us on memory loading.
        a = b'1234567890AB'
        x = my_utils.array2bytes(a)
        self.assertEquals(b'1234567890AB', x)
        pass
Example #4
0
    def test_offsetof(self):
        """returns the offset of a member fields in a record"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        class Y(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        o = my_utils.offsetof(Y, 'b')
        self.assertEquals(o, 8)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        o = my_utils.offsetof(X, 'b')
        self.assertEquals(o, 16)

        class X2(my_ctypes.Union):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        o = my_utils.offsetof(X2, 'b')
        self.assertEquals(o, 0)
        pass
Example #5
0
    def test_container_of(self):
        """From a pointer to a member, returns the parent struct"""
        # depends on offsetof
        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        x = X()
        x.a = 1
        x.b = 2
        addr_b = my_ctypes.addressof(x) + 16  # a + p
        o = my_utils.container_of(addr_b, X, 'b')
        self.assertEquals(my_ctypes.addressof(o), my_ctypes.addressof(x))

        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        class Y(ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]
        y = Y()
        y.a = 1
        y.b = 2
        addr_b = my_ctypes.addressof(y) + 8  # a + p
        o = my_utils.container_of(addr_b, Y, 'b')
        self.assertEquals(my_ctypes.addressof(o), my_ctypes.addressof(y))
        pass
Example #6
0
    def test_array2bytes(self):
        """array to bytes"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(b'\x04' + 3 * b'\x00' +
                          b'\x01' + 3 * b'\x00' +
                          b'\x01' + 3 * b'\x00' +
                          b'\x01' + 3 * b'\x00' +
                          b'\x02' + 3 * b'\x00' +
                          7 * 4 * '\x00', x)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(b'\x04' + 7 * b'\x00' +
                          b'\x01' + 7 * b'\x00' +
                          b'\x01' + 7 * b'\x00' +
                          b'\x01' + 7 * b'\x00' +
                          b'\x02' + 7 * b'\x00' +
                          7 * 8 * '\x00', x)

        a = (my_ctypes.c_char * 12).from_buffer_copy('1234567890AB')
        x = my_utils.array2bytes(a)
        self.assertEquals(b'1234567890AB', x)

        # mimics what ctypes gives us on memory loading.
        a = b'1234567890AB'
        x = my_utils.array2bytes(a)
        self.assertEquals(b'1234567890AB', x)
        pass
Example #7
0
    def test_container_of(self):
        """From a pointer to a member, returns the parent struct"""
        # depends on offsetof
        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        x = X()
        x.a = 1
        x.b = 2
        addr_b = my_ctypes.addressof(x) + 16  # a + p
        o = my_utils.container_of(addr_b, X, 'b')
        self.assertEquals(my_ctypes.addressof(o), my_ctypes.addressof(x))

        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        class Y(ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        y = Y()
        y.a = 1
        y.b = 2
        addr_b = my_ctypes.addressof(y) + 8  # a + p
        o = my_utils.container_of(addr_b, Y, 'b')
        self.assertEquals(my_ctypes.addressof(o), my_ctypes.addressof(y))
        pass
Example #8
0
    def test_formatAddress(self):
        my_utils64 = utils.Utils(types.build_ctypes_proxy(8, 8, 16))
        my_utils32 = utils.Utils(types.build_ctypes_proxy(4, 4, 8))

        x = my_utils64.formatAddress(0x12345678)
        self.assertEquals('0x0000000012345678', x)
        # 32b
        x = my_utils32.formatAddress(0x12345678)
        self.assertEquals('0x12345678', x)
Example #9
0
    def test_formatAddress(self):
        my_utils64 = utils.Utils(types.build_ctypes_proxy(8, 8, 16))
        my_utils32 = utils.Utils(types.build_ctypes_proxy(4, 4, 8))

        x = my_utils64.formatAddress(0x12345678)
        self.assertEquals('0x0000000012345678', x)
        # 32b
        x = my_utils32.formatAddress(0x12345678)
        self.assertEquals('0x12345678', x)
 def setUp(self):
     from haystack import types
     types.build_ctypes_proxy(4, 4, 8)
     self.cpu_bits = '32'
     self.os_name = 'linux'
     self.tgts = []
     self.process = None
     self.tests = {"test1": "test-ctypes1.%d" % (32),
                   "test2": "test-ctypes2.%d" % (32),
                   "test3": "test-ctypes3.%d" % (32),
                   }
 def setUp(self):
     from haystack import types
     types.build_ctypes_proxy(4, 4, 8)
     self.cpu_bits = '32'
     self.os_name = 'linux'
     self.tgts = []
     self.process = None
     self.tests = {"test1": "test-ctypes1.%d" % (32),
                   "test2": "test-ctypes2.%d" % (32),
                   "test3": "test-ctypes3.%d" % (32),
                   }
Example #12
0
    def test_get_pointee_address(self):
        """tests get_pointee_address on host ctypes POINTER and haystack POINTER"""
        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        self.assertEquals(my_ctypes.sizeof(X), 17)
        i = X.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\xBB' + 8 * '\x11' +
                               '\xCC')
        a = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_ctypes.sizeof(i.p), 8)
        self.assertNotEquals(a, 0)
        self.assertEquals(a, 0x1111111111111111)  # 8*'\x11'
        # null pointer
        i = X.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\xBB' + 8 * '\x00' +
                               '\xCC')
        pnull = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_utils.get_pointee_address(pnull), 0)

        # change arch, and retry
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)

        class Y(ctypes.Structure):
            _pack_ = True
            _fields_ = [('a', my_ctypes.c_long),
                        ('p', my_ctypes.POINTER(my_ctypes.c_int)),
                        ('b', my_ctypes.c_ubyte)]

        self.assertEquals(my_ctypes.sizeof(Y), 9)
        i = Y.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\x11' + '\xCC')
        a = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_ctypes.sizeof(i.p), 4)
        self.assertNotEquals(a, 0)
        self.assertEquals(a, 0x11111111)  # 4*'\x11'
        # null pointer
        i = Y.from_buffer_copy(b'\xAA\xAA\xBB\xBB' + 4 * '\x00' + '\xCC')
        pnull = my_utils.get_pointee_address(i.p)
        self.assertEquals(my_utils.get_pointee_address(pnull), 0)

        # non-pointer, and void null pointer
        my_ctypes = types.load_ctypes_default()
        i = my_ctypes.c_int(69)
        self.assertEquals(my_utils.get_pointee_address(i), 0)
        pnull = my_ctypes.c_void_p(0)
        self.assertEquals(my_utils.get_pointee_address(pnull), 0)

        pass
Example #13
0
    def test_array2bytes(self):
        """array to bytes"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(
            b"\x04"
            + 3 * b"\x00"
            + b"\x01"
            + 3 * b"\x00"
            + b"\x01"
            + 3 * b"\x00"
            + b"\x01"
            + 3 * b"\x00"
            + b"\x02"
            + 3 * b"\x00"
            + 7 * 4 * "\x00",
            x,
        )

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        a = (my_ctypes.c_long * 12)(4, 1, 1, 1, 2)
        x = my_utils.array2bytes(a)
        self.assertEquals(
            b"\x04"
            + 7 * b"\x00"
            + b"\x01"
            + 7 * b"\x00"
            + b"\x01"
            + 7 * b"\x00"
            + b"\x01"
            + 7 * b"\x00"
            + b"\x02"
            + 7 * b"\x00"
            + 7 * 8 * "\x00",
            x,
        )

        a = (my_ctypes.c_char * 12).from_buffer_copy("1234567890AB")
        x = my_utils.array2bytes(a)
        self.assertEquals(b"1234567890AB", x)

        # mimics what ctypes gives us on memory loading.
        a = b"1234567890AB"
        x = my_utils.array2bytes(a)
        self.assertEquals(b"1234567890AB", x)
        pass
Example #14
0
    def test_pointer_type_cache(self):
        """test the comportment of _pointer_type_cache"""
        # between reset(), we keep the reference to the ctypes modules
        # and we don't the pointer cache, that we only share with the default
        # ctypes proxy instance
        ctypes = types.load_ctypes_default()

        class X(ctypes.Structure):
            pass

        self.assertNotIn(X, ctypes._pointer_type_cache.keys())
        ctypes.POINTER(X)
        self.assertIn(X, ctypes._pointer_type_cache.keys())

        # we keep the cache
        self.assertIn(X, ctypes._pointer_type_cache.keys())

        c4 = types.build_ctypes_proxy(4, 4, 8)
        c8 = types.build_ctypes_proxy(8, 8, 16)
        cd = types.load_ctypes_default()
        if c4 != cd:
            newarch = c4
        elif c8 != cd:
            newarch = c4
        else:
            raise RuntimeError("unmanaged case")

        # cd and ctypes share a cache
        self.assertIn(X, cd._pointer_type_cache.keys())
        # and cd.POINTER is actually ctypes.POINTER
        self.assertEquals(cd.POINTER, ctypes.POINTER)
        self.assertEquals(cd._pointer_type_cache, ctypes._pointer_type_cache)

        # but not other proxies
        self.assertNotIn(X, newarch._pointer_type_cache.keys())

        class Y(newarch.Structure):
            pass

        self.assertNotIn(Y, cd._pointer_type_cache.keys())
        self.assertNotIn(Y, ctypes._pointer_type_cache.keys())
        self.assertNotIn(Y, newarch._pointer_type_cache.keys())
        newarch.POINTER(Y)
        self.assertNotIn(Y, cd._pointer_type_cache.keys())
        self.assertNotIn(Y, ctypes._pointer_type_cache.keys())
        self.assertIn(Y, newarch._pointer_type_cache.keys())

        pass
Example #15
0
 def __init__(self,
              mappings,
              os_name=None,
              cpu_bits=None,
              word_size=None,
              ptr_size=None,
              ld_size=None):
     if mappings is None:
         # we cant detect the os_name and cpu_bits without _memory_mappings
         assert os_name is not None and cpu_bits is not None
     elif not isinstance(mappings, list):
         raise TypeError("list of IMemoryMapping expected")
     elif len(mappings) == 0:
         raise TypeError("list with at least one IMemoryMapping expected")
     elif not isinstance(mappings[0], interfaces.IMemoryMapping):
         raise TypeError("IMemoryMapping list expected")
     self.__os_name = os_name or self._detect_os(mappings)
     self.__cpu_bits = cpu_bits or self._detect_cpu(mappings,
                                                    self.__os_name)
     self.__word_size = word_size or self._detect_word_size()
     self.__ptr_size = ptr_size or self._detect_ptr_size()
     self.__ld_size = ld_size or self._detect_ld_size()  # long double
     # win  32 bits, 4,4,8
     # linux 32 bits, 4,4,12
     # linux 64 bits, 8,8,16
     self.__ctypes_proxy = types.build_ctypes_proxy(self.__word_size,
                                                    self.__ptr_size,
                                                    self.__ld_size)
     pass
 def test_load_ctypes_default(self):
     """Test if the default proxy works"""
     ctypes = types.build_ctypes_proxy(4, 4, 8)
     self.assertTrue(ctypes.proxy)
     # test
     ctypes = types.load_ctypes_default()
     self.assertTrue(ctypes.proxy)
     for name, value in make_types(ctypes).items():
         globals()[name] = value
     # default ctypes should be similar to host ctypes.
     self.assertEqual(
         ctypes.sizeof(arra1),
         4 *
         ctypes.sizeof(
             ctypes.get_real_ctypes_member('c_long')))
     self.assertEqual(
         ctypes.sizeof(stp),
         ctypes.sizeof(
             ctypes.get_real_ctypes_member('c_void_p')))
     self.assertEqual(
         ctypes.sizeof(arra1),
         4 *
         ctypes.sizeof(
             ctypes.c_long))
     self.assertEqual(ctypes.sizeof(stp), ctypes.sizeof(ctypes.c_void_p))
     return
Example #17
0
    def test_pointer_type_cache(self):
        """test the comportment of _pointer_type_cache"""
        # between reset(), we keep the reference to the ctypes modules
        # and we don't the pointer cache, that we only share with the default
        # ctypes proxy instance
        ctypes = types.load_ctypes_default()

        class X(ctypes.Structure):
            pass

        self.assertNotIn(X, ctypes._pointer_type_cache.keys())
        ctypes.POINTER(X)
        self.assertIn(X, ctypes._pointer_type_cache.keys())

        # we keep the cache
        self.assertIn(X, ctypes._pointer_type_cache.keys())

        c4 = types.build_ctypes_proxy(4, 4, 8)
        c8 = types.build_ctypes_proxy(8, 8, 16)
        cd = types.load_ctypes_default()
        if c4 != cd:
            newarch = c4
        elif c8 != cd:
            newarch = c4
        else:
            raise RuntimeError("unmanaged case")

        # cd and ctypes share a cache
        self.assertIn(X, cd._pointer_type_cache.keys())
        # and cd.POINTER is actually ctypes.POINTER
        self.assertEqual(cd.POINTER, ctypes.POINTER)
        self.assertEqual(cd._pointer_type_cache, ctypes._pointer_type_cache)

        # but not other proxies
        self.assertNotIn(X, newarch._pointer_type_cache.keys())

        class Y(newarch.Structure):
            pass
        self.assertNotIn(Y, cd._pointer_type_cache.keys())
        self.assertNotIn(Y, ctypes._pointer_type_cache.keys())
        self.assertNotIn(Y, newarch._pointer_type_cache.keys())
        newarch.POINTER(Y)
        self.assertNotIn(Y, cd._pointer_type_cache.keys())
        self.assertNotIn(Y, ctypes._pointer_type_cache.keys())
        self.assertIn(Y, newarch._pointer_type_cache.keys())

        pass
Example #18
0
    def test_unpackWord(self):
        # 64b
        my_utils = utils.Utils(types.build_ctypes_proxy(8, 8, 16))

        one = b'\x01' + 7 * b'\x00'
        x = my_utils.unpackWord(one)
        self.assertEquals(x, 1)
        # 32b
        my_utils = utils.Utils(types.build_ctypes_proxy(4, 4, 8))
        one32 = b'\x01' + 3 * b'\x00'
        x = my_utils.unpackWord(one32)
        self.assertEquals(x, 1)
        pass
        # endianness
        two32 = 3 * b'\x00' + '\x02'
        x = my_utils.unpackWord(two32, '>')
        self.assertEquals(x, 2)
        pass
Example #19
0
    def test_unpackWord(self):
        # 64b
        my_utils = utils.Utils(types.build_ctypes_proxy(8, 8, 16))

        one = b'\x01' + 7 * b'\x00'
        x = my_utils.unpackWord(one)
        self.assertEquals(x, 1)
        # 32b
        my_utils = utils.Utils(types.build_ctypes_proxy(4, 4, 8))
        one32 = b'\x01' + 3 * b'\x00'
        x = my_utils.unpackWord(one32)
        self.assertEquals(x, 1)
        pass
        # endianness
        two32 = 3 * b'\x00' + '\x02'
        x = my_utils.unpackWord(two32, '>')
        self.assertEquals(x, 2)
        pass
Example #20
0
    def test_unpackWord(self):
        # 64b
        my_utils = utils.Utils(types.build_ctypes_proxy(8, 8, 16))

        one = b"\x01" + 7 * b"\x00"
        x = my_utils.unpackWord(one)
        self.assertEquals(x, 1)
        # 32b
        my_utils = utils.Utils(types.build_ctypes_proxy(4, 4, 8))
        one32 = b"\x01" + 3 * b"\x00"
        x = my_utils.unpackWord(one32)
        self.assertEquals(x, 1)
        pass
        # endianness
        two32 = 3 * b"\x00" + "\x02"
        x = my_utils.unpackWord(two32, ">")
        self.assertEquals(x, 2)
        pass
Example #21
0
    def test_bytes2array(self):
        """bytes to ctypes array"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b"\xAA" + 4 * b"\xBB" + 4 * b"\xCC" + 4 * b"\xDD" + 4 * b"\xEE" + 4 * b"\xFF"
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEquals(array[0], 0xAAAAAAAA)
        self.assertEquals(len(array), 6)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b"\xAA" + 4 * b"\xBB" + 4 * b"\xCC" + 4 * b"\xDD" + 4 * b"\xEE" + 4 * b"\xFF"
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEquals(array[0], 0xBBBBBBBBAAAAAAAA)
        self.assertEquals(len(array), 3)
        pass
    def test_set_ctypes(self):
        """Test reloading of previous defined arch-ctypes."""
        x32 = types.build_ctypes_proxy(4, 4, 8)
        x64 = types.build_ctypes_proxy(8, 8, 16)
        win = types.build_ctypes_proxy(8, 8, 8)
        ctypes = types.load_ctypes_default()

        ctypes = x32
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes, x32)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 4)
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(double), 8)

        ctypes = x64
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes, x64)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 8)
        self.assertEqual(ctypes.sizeof(stp), 8)
        self.assertEqual(ctypes.sizeof(double), 16)

        ctypes = win
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes, win)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 8)
        self.assertEqual(ctypes.sizeof(stp), 8)
        self.assertEqual(ctypes.sizeof(double), 8)

        ctypes = x32
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes, x32)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 4)
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(double), 8)

        return
Example #23
0
    def test_bytes2array(self):
        """bytes to ctypes array"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b'\xAA' + 4 * b'\xBB' + 4 * b'\xCC' + \
            4 * b'\xDD' + 4 * b'\xEE' + 4 * b'\xFF'
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEquals(array[0], 0xAAAAAAAA)
        self.assertEquals(len(array), 6)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b'\xAA' + 4 * b'\xBB' + 4 * b'\xCC' + \
            4 * b'\xDD' + 4 * b'\xEE' + 4 * b'\xFF'
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEquals(array[0], 0xBBBBBBBBAAAAAAAA)
        self.assertEquals(len(array), 3)
        pass
Example #24
0
    def test_bytes2array(self):
        """bytes to ctypes array"""
        my_ctypes = types.build_ctypes_proxy(4, 4, 8)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b'\xAA' + 4 * b'\xBB' + 4 * b'\xCC' + \
            4 * b'\xDD' + 4 * b'\xEE' + 4 * b'\xFF'
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEqual(array[0], 0xAAAAAAAA)
        self.assertEqual(len(array), 6)

        my_ctypes = types.build_ctypes_proxy(8, 8, 16)
        my_utils = utils.Utils(my_ctypes)

        bytes = 4 * b'\xAA' + 4 * b'\xBB' + 4 * b'\xCC' + \
            4 * b'\xDD' + 4 * b'\xEE' + 4 * b'\xFF'
        array = my_utils.bytes2array(bytes, my_ctypes.c_ulong)
        self.assertEqual(array[0], 0xBBBBBBBBAAAAAAAA)
        self.assertEqual(len(array), 3)
        pass
Example #25
0
    def test_reload_ctypes(self):
        """Tests loading of specific arch ctypes."""
        ctypes = types.build_ctypes_proxy(4, 4, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEquals(ctypes.sizeof(arra1), 4 * 4)
        self.assertEquals(ctypes.sizeof(stp), 4)
        self.assertEquals(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(4, 8, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEquals(ctypes.sizeof(arra1), 4 * 4)
        self.assertEquals(ctypes.sizeof(stp), 8)
        self.assertEquals(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(8, 4, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEquals(ctypes.sizeof(arra1), 4 * 8)
        self.assertEquals(ctypes.sizeof(stp), 4)
        self.assertEquals(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(8, 4, 16)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEquals(ctypes.sizeof(arra1), 4 * 8)
        self.assertEquals(ctypes.sizeof(stp), 4)
        self.assertEquals(ctypes.sizeof(double), 16)

        # other arch
        self.assertRaises(NotImplementedError, types.build_ctypes_proxy, 16, 8,
                          16)
        return
 def setUp(self):
     """Have to reload that at every test. classmethod will not work"""
     # use the host ctypes with modif
     ctypes = types.build_ctypes_proxy(8, 8, 16)
     self.assertTrue(ctypes.proxy)
     self._ctypes = ctypes
     for name, value in make_types(ctypes).items():
         globals()[name] = value
     #
     self.tests = [St, St2, SubSt2, btype, longt, voidp, stp, stpvoid, arra1,
                   arra2, arra3, charp, string, fptr, arra4, double, arra5,
                   arra6, Union, ptrUnion]
Example #27
0
    def test_reload_ctypes(self):
        """Tests loading of specific arch ctypes."""
        ctypes = types.build_ctypes_proxy(4, 4, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 4)
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(4, 8, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 4)
        self.assertEqual(ctypes.sizeof(stp), 8)
        self.assertEqual(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(8, 4, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 8)
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(double), 8)

        # other arch
        ctypes = types.build_ctypes_proxy(8, 4, 16)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertTrue(ctypes.proxy)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 8)
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(double), 16)

        # other arch
        self.assertRaises(NotImplementedError, types.build_ctypes_proxy, 16, 8, 16)
        return
    def test_reset_ctypes(self):
        """Test if reset gives the original types"""
        ctypes = types.build_ctypes_proxy(4, 4, 8)
        for name, value in make_types(ctypes).items():
            globals()[name] = value
        self.assertEqual(ctypes.sizeof(stp), 4)
        self.assertEqual(ctypes.sizeof(arra1), 4 * 4)

        import ctypes
        # no CString.
        self.assertRaises(AttributeError, make_types, ctypes)
        self.assertIn('ctypes', '%s' % (ctypes))
        self.assertFalse(hasattr(ctypes, 'proxy'))
        return
Example #29
0
    def test_get_subtype(self):
        my_ctypes = types.load_ctypes_default()
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _fields_ = [('p', my_ctypes.POINTER(my_ctypes.c_long))]
        PX = my_ctypes.POINTER(X)
        self.assertEquals(my_utils.get_subtype(PX), X)

        my_ctypes = types.build_ctypes_proxy(4, 4, 8)  # different arch

        class Y(my_ctypes.Structure):
            _fields_ = [('p', my_ctypes.POINTER(my_ctypes.c_long))]
        PY = my_ctypes.POINTER(Y)
        self.assertEquals(my_utils.get_subtype(PY), Y)
Example #30
0
    def test_get_subtype(self):
        my_ctypes = types.load_ctypes_default()
        my_utils = utils.Utils(my_ctypes)

        class X(my_ctypes.Structure):
            _fields_ = [('p', my_ctypes.POINTER(my_ctypes.c_long))]

        PX = my_ctypes.POINTER(X)
        self.assertEquals(my_utils.get_subtype(PX), X)

        my_ctypes = types.build_ctypes_proxy(4, 4, 8)  # different arch

        class Y(my_ctypes.Structure):
            _fields_ = [('p', my_ctypes.POINTER(my_ctypes.c_long))]

        PY = my_ctypes.POINTER(Y)
        self.assertEquals(my_utils.get_subtype(PY), Y)
Example #31
0
 def __init__(self, mappings, os_name=None, cpu_bits=None, word_size=None, ptr_size=None, ld_size=None):
     if mappings is None:
         # we cant detect the os_name and cpu_bits without _memory_mappings
         assert os_name is not None and cpu_bits is not None
     elif not isinstance(mappings, list):
         raise TypeError("list of IMemoryMapping expected")
     elif len(mappings) == 0:
         raise TypeError("list with at least one IMemoryMapping expected")
     elif not isinstance(mappings[0], interfaces.IMemoryMapping):
         raise TypeError("IMemoryMapping list expected")
     self.__os_name = os_name or self._detect_os(mappings)
     self.__cpu_bits = cpu_bits or self._detect_cpu(mappings, self.__os_name)
     self.__word_size = word_size or self._detect_word_size()
     self.__ptr_size = ptr_size or self._detect_ptr_size()
     self.__ld_size = ld_size or self._detect_ld_size()  # long double
     # win  32 bits, 4,4,8
     # linux 32 bits, 4,4,12
     # linux 64 bits, 8,8,16
     self.__ctypes_proxy = types.build_ctypes_proxy(self.__word_size, self.__ptr_size, self.__ld_size)
     pass