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_overlap_segment_tail(self):
# Existing segment: |_______|
# New segment: |_______|
prog = Program()
segment1 = unittest.mock.Mock(side_effect=zero_memory_read)
segment2 = unittest.mock.Mock(side_effect=zero_memory_read)
prog.add_memory_segment(0xffff0000, 128, segment1)
prog.add_memory_segment(0xffff0040, 128, segment2)
prog.read(0xffff0000, 192)
segment1.assert_called_once_with(0xffff0000, 64, 0, False)
segment2.assert_called_once_with(0xffff0040, 128, 0, False)
return None
for obj in objects:
if obj.name == name:
if obj.value is not None:
if flags & FindObjectFlags.CONSTANT:
break
elif obj.type.kind == TypeKind.FUNCTION:
if flags & FindObjectFlags.FUNCTION:
break
elif flags & FindObjectFlags.VARIABLE:
break
else:
return None
return Object(prog, obj.type, address=obj.address, value=obj.value)
prog = Program(platform)
if segments is not None:
for segment in segments:
if segment.virt_addr is not None:
prog.add_memory_segment(
segment.virt_addr, len(segment.buf),
functools.partial(mock_memory_read, segment.buf))
if segment.phys_addr is not None:
prog.add_memory_segment(
segment.phys_addr, len(segment.buf),
functools.partial(mock_memory_read, segment.buf), True)
if types is not None:
prog.add_type_finder(mock_find_type)
if objects is not None:
prog.add_object_finder(mock_object_find)
return prog
def test_overlap_same_address_smaller_size(self):
# Existing segment: |_______|
# New segment: |___|
prog = Program()
segment1 = unittest.mock.Mock(side_effect=zero_memory_read)
segment2 = unittest.mock.Mock(side_effect=zero_memory_read)
prog.add_memory_segment(0xffff0000, 128, segment1)
prog.add_memory_segment(0xffff0000, 64, segment2)
prog.read(0xffff0000, 128)
segment1.assert_called_once_with(0xffff0040, 64, 64, False)
segment2.assert_called_once_with(0xffff0000, 64, 0, False)
def setup_target() -> Optional[drgn.Program]:
"""
Create a drgn.Program instance and setup the SDB
context for all the integration tests. If there
is no crash dump to attach to this is going to
be an empty drgn.Program.
"""
prog = drgn.Program()
if not dump_exists():
return prog
prog.set_core_dump(DUMP_PATH)
load_debug_info(prog, [VMLX_PATH, MODS_PATH])
return prog
def test_overlap_segment_head(self):
# Existing segment: |_______|
# New segment: |_______|
prog = Program()
segment1 = unittest.mock.Mock(side_effect=zero_memory_read)
segment2 = unittest.mock.Mock(side_effect=zero_memory_read)
prog.add_memory_segment(0xffff0040, 128, segment1)
prog.add_memory_segment(0xffff0000, 128, segment2)
prog.read(0xffff0000, 192)
segment1.assert_called_once_with(0xffff0080, 64, 64, False)
segment2.assert_called_once_with(0xffff0000, 128, 0, False)
def test_overlap_within_segment(self):
# Existing segment: |_______|
# New segment: |___|
prog = Program()
segment1 = unittest.mock.Mock(side_effect=zero_memory_read)
segment2 = unittest.mock.Mock(side_effect=zero_memory_read)
prog.add_memory_segment(0xffff0000, 128, segment1)
prog.add_memory_segment(0xffff0020, 64, segment2)
prog.read(0xffff0000, 128)
segment1.assert_has_calls([
unittest.mock.call(0xffff0000, 32, 00, False),
unittest.mock.call(0xffff0060, 32, 96, False),
])
segment2.assert_called_once_with(0xffff0020, 64, 0, False)
def test_physical(self):
data = b'hello, world'
prog = Program()
with tempfile.NamedTemporaryFile() as f:
f.write(create_elf_file(ET.CORE, [
ElfSection(
p_type=PT.LOAD,
vaddr=0xffff0000,
paddr=0xa0,
data=data,
),
]))
f.flush()
prog.set_core_dump(f.name)
self.assertEqual(prog.read(0xffff0000, len(data)), data)
self.assertEqual(prog.read(0xa0, len(data), physical=True), data)
def test_not_core_dump(self):
prog = Program()
self.assertRaisesRegex(ValueError, 'not an ELF core file',
prog.set_core_dump, '/dev/null')
with tempfile.NamedTemporaryFile() as f:
f.write(create_elf_file(ET.EXEC, []))
f.flush()
self.assertRaisesRegex(ValueError, 'not an ELF core file',
prog.set_core_dump, f.name)
def test_overlap_same_address_larger_size(self):
# Existing segment: |___|
# New segment: |_______|
prog = Program()
segment1 = unittest.mock.Mock(side_effect=zero_memory_read)
segment2 = unittest.mock.Mock(side_effect=zero_memory_read)
prog.add_memory_segment(0xffff0000, 64, segment1)
prog.add_memory_segment(0xffff0000, 128, segment2)
prog.read(0xffff0000, 128)
segment1.assert_not_called()
segment2.assert_called_once_with(0xffff0000, 128, 0, False)
def for_each_mount(prog_or_ns, src=None, dst=None, fstype=None):
"""
.. c:function:: for_each_mount(struct mnt_namespace *ns, char *src, char *dst, char *fstype)
Iterate over all of the mounts in a given namespace. If given a
:class:`Program` instead, the initial mount namespace is used. returned
mounts can be filtered by source, destination, or filesystem type, all of
which are encoded using :func:`os.fsencode()`.
:return: Iterator of ``struct mount *`` objects.
"""
if isinstance(prog_or_ns, Program):
ns = prog_or_ns['init_task'].nsproxy.mnt_ns
else:
ns = prog_or_ns
if src is not None:
src = os.fsencode(src)
if dst is not None:
dst = os.fsencode(dst)
if fstype:
fstype = os.fsencode(fstype)
for mnt in list_for_each_entry('struct mount', ns.list.address_of_(),
'mnt_list'):
if ((src is None or mount_src(mnt) == src) and
(dst is None or mount_dst(mnt) == dst) and
(fstype is None or mount_fstype(mnt) == fstype)):
yield mnt