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_typedef(self):
type_ = typedef_type('SHORT', self.prog.type('short'))
self.assertEqual(+Object(self.prog, type_, value=5),
Object(self.prog, 'int', value=5))
# Typedef should be preserved if the type wasn't promoted.
type_ = typedef_type('self.int', self.prog.type('int'))
self.assertEqual(+Object(self.prog, type_, value=5),
Object(self.prog, type_, value=5))
# Qualified type argument.
self.assertEqual(t, typedef_type(
'INT', int_type('int', 4, True)))
# Different name.
self.assertNotEqual(t, typedef_type(
'integer', int_type('int', 4, True)))
# Different type.
self.assertNotEqual(t, typedef_type(
'integer', int_type('unsigned int', 4, False)))
self.assertNotEqual(t, typedef_type(
'INT', int_type('int', 4, True, Qualifiers.CONST)))
self.assertEqual(repr(t), "typedef_type(name='INT', type=int_type(name='int', size=4, is_signed=True))")
self.assertEqual(sizeof(t), 4)
t = typedef_type('VOID', void_type())
self.assertFalse(t.is_complete())
self.assertRaises(TypeError, typedef_type, None,
int_type('int', 4, True))
self.assertRaises(TypeError, typedef_type, 'INT', 4)
self.assertEqual(
typedef_type('size_t',
int_type('unsigned long', 8, False)).primitive,
PrimitiveType.C_SIZE_T)
self.assertEqual(
typedef_type('ptrdiff_t', int_type('long', 8, True)).primitive,
PrimitiveType.C_PTRDIFF_T)
point_type = struct_type('point', 8, (
(int_type('int', 4, True), 'x', 0),
(int_type('int', 4, True), 'y', 32),
))
line_segment_type = struct_type('line_segment', 16, (
(point_type, 'a'),
(point_type, 'b', 64),
))
option_type = union_type('option', 4, (
(int_type('int', 4, True), 'i'),
(float_type('float', 4), 'f'),
))
color_type = enum_type('color', int_type('unsigned int', 4, False),
(('RED', 0), ('GREEN', 1), ('BLUE', 2)))
pid_type = typedef_type('pid_t', int_type('int', 4, True))
def mock_type_index(word_size, types):
def mock_type_index_find(kind, name, filename):
if filename:
return None
for type in types:
if type.kind == kind:
try:
type_name = type.name
except AttributeError:
try:
type_name = type.tag
except AttributeError:
continue
if type_name == name:
def test_typedef(self):
type_ = typedef_type('INT', self.prog.type('int'))
self.assertCommonRealType(type_, type_, type_)
self.assertCommonRealType('int', type_, type_, commutative=False)
self.assertCommonRealType(type_, 'int', 'int', commutative=False)
type_ = typedef_type('LONG', self.prog.type('long'))
self.assertCommonRealType(type_, 'int', type_)
def test_void_typedef(self):
dies = [
DwarfDie(
DW_TAG.typedef,
[
DwarfAttrib(DW_AT.name, DW_FORM.string, 'VOID'),
],
),
]
self.assertFromDwarf(dies, typedef_type('VOID', void_type()))
dies[0].attribs.pop(0)
self.assertRaisesRegex(Exception,
'DW_TAG_typedef has missing or invalid DW_AT_name',
self.type_from_dwarf, dies)
))
point_type = struct_type('point', 8, (
(int_type('int', 4, True), 'x', 0),
(int_type('int', 4, True), 'y', 32),
))
line_segment_type = struct_type('line_segment', 16, (
(point_type, 'a'),
(point_type, 'b', 64),
))
option_type = union_type('option', 4, (
(int_type('int', 4, True), 'i'),
(float_type('float', 4), 'f'),
))
color_type = enum_type('color', int_type('unsigned int', 4, False),
(('RED', 0), ('GREEN', 1), ('BLUE', 2)))
pid_type = typedef_type('pid_t', int_type('int', 4, True))
MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN,
PlatformFlags.IS_LITTLE_ENDIAN)
MOCK_PLATFORM = Platform(Architecture.UNKNOWN,
PlatformFlags.IS_64_BIT |
PlatformFlags.IS_LITTLE_ENDIAN)
class MockMemorySegment(NamedTuple):
buf: bytes
virt_addr: Optional[int] = None
phys_addr: Optional[int] = None
def mock_memory_read(data, address, count, offset, physical):
),
DwarfDie(
DW_TAG.array_type,
[DwarfAttrib(DW_AT.type, DW_FORM.ref4, 3)],
[
DwarfDie(
DW_TAG.subrange_type,
[DwarfAttrib(DW_AT.count, DW_FORM.data1, 0)]
),
],
),
int_die,
]
type_ = array_type(
3, typedef_type('ZARRAY', array_type(0, int_type('int', 4, True))))
self.assertFromDwarf(dies, type_)
# GCC actually squashes arrays of typedef arrays into one array type,
# but let's handle it like GCC < 9.0 anyways.
del dies[2].children[0]
self.assertFromDwarf(dies, type_)
def test_size_t_and_ptrdiff_t(self):
# 64-bit architecture with 4-byte long/unsigned long.
prog = mock_program(types=[
int_type('long', 4, True),
int_type('unsigned long', 4, False),
])
self.assertEqual(prog.type('size_t'),
typedef_type('size_t', prog.type('unsigned long long')))
self.assertEqual(prog.type('ptrdiff_t'),
typedef_type('ptrdiff_t', prog.type('long long')))
# 32-bit architecture with 8-byte long/unsigned long.
prog = mock_program(MOCK_32BIT_PLATFORM, types=[
int_type('long', 8, True),
int_type('unsigned long', 8, False),
])
self.assertEqual(prog.type('size_t'),
typedef_type('size_t', prog.type('unsigned int')))
self.assertEqual(prog.type('ptrdiff_t'),
typedef_type('ptrdiff_t', prog.type('int')))
# Nonsense sizes.
prog = mock_program(types=[
int_type('int', 1, True),
def test_typedef(self):
type_ = typedef_type('INT', int_type('int', 4, True))
self.assertEqual(str(Object(self.prog, type_, value=99)), '(INT)99')
type_ = typedef_type('INT', int_type('int', 4, True), Qualifiers.CONST)
self.assertEqual(str(Object(self.prog, type_, value=99)), '(const INT)99')
type_ = typedef_type(
'CINT', int_type('int', 4, True, Qualifiers.CONST))
self.assertEqual(str(Object(self.prog, type_, value=99)), '(CINT)99')