Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pointer_to_function_returning_pointer_to_const(self):
i = int_type('int', 4, True)
self.assertTypeName(pointer_type(8, function_type(pointer_type(8, int_type('int', 4, True, Qualifiers.CONST)), ((i,),), False)),
'const int *(*)(int)', True)
[
DwarfDie(
DW_TAG.formal_parameter,
[DwarfAttrib(DW_AT.type, DW_FORM.ref4, 1)],
),
]
),
DwarfDie(
DW_TAG.array_type,
[DwarfAttrib(DW_AT.type, DW_FORM.ref4, 2)],
),
int_die,
]
self.assertFromDwarf(
dies,
function_type(void_type(),
((array_type(None, int_type('int', 4, True)),),),
False))
def test_pointer_to_function_returning_pointer(self):
i = int_type('int', 4, True)
self.assertTypeName(pointer_type(8, function_type(pointer_type(8, i), ((i,),), False)),
'int *(*)(int)', True)
self.assertTypeName(pointer_type(8, function_type(pointer_type(8, i), ((pointer_type(8, i),),), False)),
'int *(*)(int *)', True)
# Unnamed parameter.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True),),)))
# Different number of parameters.
self.assertNotEqual(t, function_type(
void_type(),
((int_type('int', 4, True), 'n'),
(pointer_type(8, void_type()), 'p'))))
# One is variadic.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'n'),), True))
self.assertEqual(repr(t), "function_type(type=void_type(), parameters=((int_type(name='int', size=4, is_signed=True), 'n'),), is_variadic=False)")
self.assertRaises(TypeError, sizeof, t)
self.assertFalse(function_type(void_type(), (), False).is_variadic)
self.assertTrue(function_type(void_type(), (), True).is_variadic)
self.assertRaisesRegex(TypeError, 'must be Type', function_type, None,
())
self.assertRaisesRegex(TypeError, 'must be sequence', function_type,
void_type(), None)
self.assertRaisesRegex(TypeError, 'must be.*sequence', function_type,
void_type(), (4,))
self.assertRaisesRegex(ValueError, 'must be.*sequence', function_type,
void_type, ((),))
self.assertRaisesRegex(TypeError, 'must be string or None',
function_type, void_type(),
((int_type('int', 4, True), 4),))
self.assertRaisesRegex(TypeError, 'must be Type', function_type,
void_type(), ((None, 'n'),))
def test_function(self):
obj = Object(self.prog,
function_type(void_type(), (), False), address=0)
self.assertIs(obj.prog_, self.prog)
self.assertEqual(obj.type_, function_type(void_type(), (), False))
self.assertEqual(obj.address_, 0)
self.assertEqual(obj.byteorder_, 'little')
self.assertEqual(obj.bit_offset_, 0)
self.assertIsNone(obj.bit_field_size_)
self.assertRaisesRegex(TypeError,
'cannot read object with function type',
obj.value_)
self.assertRaisesRegex(TypeError,
'cannot read object with function type',
obj.read_)
self.assertRaises(TypeError, sizeof, obj)
def test_function(self):
t = function_type(void_type(), ((int_type('int', 4, True), 'n'),))
self.assertEqual(t.kind, TypeKind.FUNCTION)
self.assertIsNone(t.primitive)
self.assertEqual(t.type, void_type())
self.assertEqual(t.parameters, (
(int_type('int', 4, True), 'n'),))
self.assertFalse(t.is_variadic)
self.assertTrue(t.is_complete())
self.assertEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'n'),)))
# Different return type.
self.assertNotEqual(t, function_type(
int_type('int', 4, True),
((int_type('int', 4, True), 'n'),)))
# Different parameter name.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'x'),)))
# Unnamed parameter.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True),),)))
# Different number of parameters.
self.assertNotEqual(t, function_type(
void_type(),
((int_type('int', 4, True), 'n'),
(pointer_type(8, void_type()), 'p'))))
# One is variadic.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'n'),), True))
def test_pointer_to_variadic_function(self):
i = int_type('int', 4, True)
self.assertTypeName(pointer_type(8, function_type(i, ((i,),), True)),
'int (*)(int, ...)', True)
def test_function(self):
t = function_type(void_type(), ((int_type('int', 4, True), 'n'),))
self.assertEqual(t.kind, TypeKind.FUNCTION)
self.assertIsNone(t.primitive)
self.assertEqual(t.type, void_type())
self.assertEqual(t.parameters, (
(int_type('int', 4, True), 'n'),))
self.assertFalse(t.is_variadic)
self.assertTrue(t.is_complete())
self.assertEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'n'),)))
# Different return type.
self.assertNotEqual(t, function_type(
int_type('int', 4, True),
((int_type('int', 4, True), 'n'),)))
# Different parameter name.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True), 'x'),)))
# Unnamed parameter.
self.assertNotEqual(t, function_type(
void_type(), ((int_type('int', 4, True),),)))
# Different number of parameters.
self.assertNotEqual(t, function_type(
void_type(),
((int_type('int', 4, True), 'n'),
(pointer_type(8, void_type()), 'p'))))
def test_function_typedef(self):
self.assertPrettyPrint(typedef_type('fn', function_type(int_type('int', 4, True), (), False)),
'typedef int fn(void)')
def test_function(self):
mock_obj = MockObject('func', function_type(void_type(), (), False),
address=0xffff0000)
prog = mock_program(objects=[mock_obj])
self.assertEqual(prog['func'],
Object(prog, function_type(void_type(), (), False),
address=0xffff0000))
self.assertEqual(prog.object('func', FindObjectFlags.FUNCTION),
prog['func'])
self.assertTrue('func' in prog)