Skip to content

Commit

Permalink
get_memory_alignment() -> gef.arch.ptrsize
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Dec 14, 2021
1 parent e36152d commit fbbcc3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
31 changes: 17 additions & 14 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ def is_main_arena(self):

def heap_addr(self, allow_unaligned=False):
if self.is_main_arena():
heap_section = HeapBaseFunction.heap_base()
heap_section = gef.heap.base_address
if not heap_section:
err("Heap not initialized")
return None
Expand Down Expand Up @@ -1659,7 +1659,7 @@ def hexdump(source, length=0x10, separator=".", show_raw=False, show_symbol=True
@param base is the start address of the block being hexdump
@return a string with the hexdump"""
result = []
align = get_memory_alignment() * 2 + 2 if is_alive() else 18
align = gef.arch.ptrsize * 2 + 2 if is_alive() else 18

for i in range(0, len(source), length):
chunk = bytearray(source[i : i + length])
Expand Down Expand Up @@ -2358,7 +2358,7 @@ def get_ra(self, insn, frame):
if self.is_ret(insn):
# If it's a pop, we have to peek into the stack, otherwise use lr
if insn.mnemonic == "pop":
ra_addr = gef.arch.sp + (len(insn.operands)-1) * get_memory_alignment()
ra_addr = gef.arch.sp + (len(insn.operands)-1) * self.ptrsize
ra = to_unsigned_long(dereference(ra_addr))
elif insn.mnemonic == "ldr":
return to_unsigned_long(dereference(gef.arch.sp))
Expand Down Expand Up @@ -3835,7 +3835,7 @@ def clear_screen(tty=""):

def format_address(addr):
"""Format the address according to its size."""
memalign_size = get_memory_alignment()
memalign_size = gef.arch.ptrsize
addr = align_address(addr)

if memalign_size == 4:
Expand All @@ -3846,7 +3846,7 @@ def format_address(addr):

def format_address_spaces(addr, left=True):
"""Format the address according to its size, but with spaces instead of zeroes."""
width = get_memory_alignment() * 2 + 2
width = gef.arch.ptrsize * 2 + 2
addr = align_address(addr)

if not left:
Expand All @@ -3857,7 +3857,7 @@ def format_address_spaces(addr, left=True):

def align_address(address):
"""Align the provided address to the process's native length."""
if get_memory_alignment() == 4:
if gef.arch.ptrsize == 4:
return address & 0xFFFFFFFF

return address & 0xFFFFFFFFFFFFFFFF
Expand Down Expand Up @@ -4268,7 +4268,7 @@ def stop(self):
if check_heap_overlap:
# seek all the currently allocated chunks, read their effective size and check for overlap
msg = []
align = get_memory_alignment()
align = gef.arch.ptrsize
for chunk_addr, _ in __heap_allocated_list__:
current_chunk = GlibcChunk(chunk_addr)
current_chunk_size = current_chunk.get_chunk_size()
Expand Down Expand Up @@ -5397,7 +5397,7 @@ def apply_structure_to_address(self, mod_name, struct_name, addr, depth=0):

self.deserialize(_struct, data)

_regsize = get_memory_alignment()
_regsize = gef.arch.ptrsize

for field in _struct._fields_:
_name, _type = field
Expand Down Expand Up @@ -7213,7 +7213,7 @@ def find_tcache():
# one thread), we can't use the tcache symbol, but we can guess the
# correct address because the tcache is consistently the first
# allocation in the main arena.
heap_base = HeapBaseFunction.heap_base()
heap_base = gef.heap.base_address
if heap_base is None:
err("No heap section")
return 0x0
Expand Down Expand Up @@ -9346,7 +9346,7 @@ def dereference_from(addr):
elif addr.section.permission.value & Permission.READ:
if is_ascii_string(addr.value):
s = gef.memory.read_cstring(addr.value)
if len(s) < get_memory_alignment():
if len(s) < gef.arch.ptrsize:
txt = '{:s} ("{:s}"?)'.format(format_address(deref), Color.colorify(s, string_color))
elif len(s) > 50:
txt = Color.colorify('"{:s}[...]"'.format(s[:50]), string_color)
Expand Down Expand Up @@ -9525,7 +9525,7 @@ def do_invoke(self, argv):
color = gef.config["theme.table_heading"]

headers = ["Start", "End", "Offset", "Perm", "Path"]
gef_print(Color.colorify("{:<{w}s}{:<{w}s}{:<{w}s}{:<4s} {:s}".format(*headers, w=get_memory_alignment()*2+3), color))
gef_print(Color.colorify("{:<{w}s}{:<{w}s}{:<{w}s}{:<4s} {:s}".format(*headers, w=gef.arch.ptrsize*2+3), color))

for entry in vmmap:
if not argv:
Expand Down Expand Up @@ -9598,7 +9598,7 @@ class XFilesCommand(GenericCommand):
def do_invoke(self, argv):
color = gef.config["theme.table_heading"]
headers = ["Start", "End", "Name", "File"]
gef_print(Color.colorify("{:<{w}s}{:<{w}s}{:<21s} {:s}".format(*headers, w=get_memory_alignment()*2+3), color))
gef_print(Color.colorify("{:<{w}s}{:<{w}s}{:<21s} {:s}".format(*headers, w=gef.arch.ptrsize*2+3), color))

filter_by_file = argv[0] if argv and argv[0] else None
filter_by_name = argv[1] if len(argv) > 1 and argv[1] else None
Expand Down Expand Up @@ -10568,7 +10568,7 @@ def do_invoke(self, argv):


#
# GEF internal classes
# GEF internal command classes
#
class GefCommand(gdb.Command):
"""GEF main command: view all new commands by typing `gef`."""
Expand Down Expand Up @@ -11237,6 +11237,10 @@ def screen_setup(self):
return


#
# GEF internal classes
#

def __gef_prompt__(current_prompt):
"""GEF custom prompt function."""

Expand All @@ -11245,7 +11249,6 @@ def __gef_prompt__(current_prompt):
if is_alive(): return GEF_PROMPT_ON
return GEF_PROMPT_OFF


class GefMemoryManager:
"""Class that manages memory access for gef."""

Expand Down
10 changes: 5 additions & 5 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ class TestGefFunctionsUnit(GefUnitTestGeneric):
"""Tests GEF internal functions."""

def test_func_get_memory_alignment(self):
res = gdb_test_python_method("get_memory_alignment(in_bits=False)")
res = gdb_test_python_method("gef.arch.ptrsize")
self.assertIn(res.splitlines()[-1], ("4", "8"))
return

Expand All @@ -859,23 +859,23 @@ def test_func_which(self):
return

def test_func_get_filepath(self):
res = gdb_test_python_method("get_filepath()", target=BIN_LS)
res = gdb_test_python_method("gef.session.file", target=BIN_LS)
self.assertNoException(res)
target = TMPDIR / "foo bar"
subprocess.call(["cp", BIN_LS, target])
res = gdb_test_python_method("get_filepath()", target=target)
res = gdb_test_python_method("gef.session.file", target=target)
self.assertNoException(res)
subprocess.call(["rm", target])
return

def test_func_get_pid(self):
res = gdb_test_python_method("get_pid()", target=BIN_LS)
res = gdb_test_python_method("gef.session.pid", target=BIN_LS)
self.assertNoException(res)
self.assertTrue(int(res.splitlines()[-1]))
return

def test_fun_gef_get_auxiliary_values(self):
func = "gef_get_auxiliary_values()"
func = "gef.session.auxiliary_values"
res = gdb_test_python_method(func, target=BIN_LS)
self.assertNoException(res)
# we need at least ("AT_PLATFORM", "AT_EXECFN") right now
Expand Down

0 comments on commit fbbcc3a

Please sign in to comment.