Skip to content

Commit

Permalink
Fixed heap.base_address
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Dec 14, 2021
1 parent fbbcc3a commit e709aeb
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions gef.py
Expand Up @@ -279,35 +279,35 @@ def wrapper(*args, **kwargs):

def p8(x: int, s: bool = False) -> bytes:
"""Pack one byte respecting the current architecture endianness."""
return struct.pack("{}B".format(endian_str()), x) if not s else struct.pack("{}b".format(endian_str()), x)
return struct.pack("{}B".format(gef.arch.endianness), x) if not s else struct.pack("{}b".format(gef.arch.endianness), x)

def p16(x: int, s: bool = False) -> bytes:
"""Pack one word respecting the current architecture endianness."""
return struct.pack("{}H".format(endian_str()), x) if not s else struct.pack("{}h".format(endian_str()), x)
return struct.pack("{}H".format(gef.arch.endianness), x) if not s else struct.pack("{}h".format(gef.arch.endianness), x)

def p32(x: int, s: bool = False) -> bytes:
"""Pack one dword respecting the current architecture endianness."""
return struct.pack("{}I".format(endian_str()), x) if not s else struct.pack("{}i".format(endian_str()), x)
return struct.pack("{}I".format(gef.arch.endianness), x) if not s else struct.pack("{}i".format(gef.arch.endianness), x)

def p64(x: int, s: bool = False) -> bytes:
"""Pack one qword respecting the current architecture endianness."""
return struct.pack("{}Q".format(endian_str()), x) if not s else struct.pack("{}q".format(endian_str()), x)
return struct.pack("{}Q".format(gef.arch.endianness), x) if not s else struct.pack("{}q".format(gef.arch.endianness), x)

def u8(x: bytes, s: bool = False) -> int:
"""Unpack one byte respecting the current architecture endianness."""
return struct.unpack("{}B".format(endian_str()), x)[0] if not s else struct.unpack("{}b".format(endian_str()), x)[0]
return struct.unpack("{}B".format(gef.arch.endianness), x)[0] if not s else struct.unpack("{}b".format(gef.arch.endianness), x)[0]

def u16(x: bytes, s: bool = False) -> int:
"""Unpack one word respecting the current architecture endianness."""
return struct.unpack("{}H".format(endian_str()), x)[0] if not s else struct.unpack("{}h".format(endian_str()), x)[0]
return struct.unpack("{}H".format(gef.arch.endianness), x)[0] if not s else struct.unpack("{}h".format(gef.arch.endianness), x)[0]

def u32(x: bytes, s: bool = False) -> int:
"""Unpack one dword respecting the current architecture endianness."""
return struct.unpack("{}I".format(endian_str()), x)[0] if not s else struct.unpack("{}i".format(endian_str()), x)[0]
return struct.unpack("{}I".format(gef.arch.endianness), x)[0] if not s else struct.unpack("{}i".format(gef.arch.endianness), x)[0]

def u64(x: bytes, s: bool = False) -> int:
"""Unpack one qword respecting the current architecture endianness."""
return struct.unpack("{}Q".format(endian_str()), x)[0] if not s else struct.unpack("{}q".format(endian_str()), x)[0]
return struct.unpack("{}Q".format(gef.arch.endianness), x)[0] if not s else struct.unpack("{}q".format(gef.arch.endianness), x)[0]


def is_ascii_string(address):
Expand Down Expand Up @@ -761,7 +761,7 @@ def __init__(self, elf="", minimalist=False):
self.e_magic, self.e_class, self.e_endianness, self.e_eiversion = struct.unpack(">IBBB", self.read(7))

# adjust endianness in bin reading
endian = endian_str()
endian = gef.arch.endianness

# off 0x7
self.e_osabi, self.e_abiversion = struct.unpack("{}BB".format(endian), self.read(2))
Expand Down Expand Up @@ -845,7 +845,7 @@ def __init__(self, elf, off):
if not elf:
return None
elf.seek(off)
endian = endian_str()
endian = gef.arch.endianness
if elf.e_class == Elf.ELF_64_BITS:
self.p_type, self.p_flags, self.p_offset = struct.unpack("{}IIQ".format(endian), elf.read(16))
self.p_vaddr, self.p_paddr = struct.unpack("{}QQ".format(endian), elf.read(16))
Expand Down Expand Up @@ -926,7 +926,7 @@ def __init__(self, elf, off):
if elf is None:
return None
elf.seek(off)
endian = endian_str()
endian = gef.arch.endianness
if elf.e_class == Elf.ELF_64_BITS:
self.sh_name, self.sh_type, self.sh_flags = struct.unpack("{}IIQ".format(endian), elf.read(16))
self.sh_addr, self.sh_offset = struct.unpack("{}QQ".format(endian), elf.read(16))
Expand Down Expand Up @@ -1240,14 +1240,6 @@ def bin(self, i):
bw = int(self.bins[idx + 1])
return fd, bw

# def get_next(self):
# addr_next = int(self.next)
# arena_main = GlibcArena(self.__name)
# if addr_next == arena_main.__addr:
# return None
# return GlibcArena("*{:#x} ".format(addr_next))

@deprecated("use `==` operator instead")
def is_main_arena(self):
return int(self) == int(gef.heap.main_arena)

Expand Down Expand Up @@ -4001,7 +3993,6 @@ def endian_str():
def get_gef_setting(name):
return gef.config


@deprecated("Use `gef.config[key] = value`")
def set_gef_setting(name, value):
gef.config[name] = value
Expand Down Expand Up @@ -4724,12 +4715,12 @@ def __init__(self):

@property
def format_matrix(self):
# `endian_str()` is a runtime property, should not be defined as a class property
# `gef.arch.endianness` is a runtime property, should not be defined as a class property
return {
8: (endian_str() + "B", "char", "db"),
16: (endian_str() + "H", "short", "dw"),
32: (endian_str() + "I", "int", "dd"),
64: (endian_str() + "Q", "long long", "dq"),
8: (gef.arch.endianness + "B", "char", "db"),
16: (gef.arch.endianness + "H", "short", "dw"),
32: (gef.arch.endianness + "I", "int", "dd"),
64: (gef.arch.endianness + "Q", "long long", "dq"),
}

@only_if_gdb_running
Expand Down Expand Up @@ -7475,7 +7466,7 @@ def do_invoke(self, argv, *args, **kwargs):
err("invalid registers for architecture: {}".format(", ".join(invalid_regs)))

memsize = gef.arch.ptrsize
endian = endian_str()
endian = gef.arch.endianness
charset = string.printable
widest = max(map(len, gef.arch.all_registers))
special_line = ""
Expand Down Expand Up @@ -9086,7 +9077,7 @@ def do_invoke(self, argv, *args, **kwargs):
return

def _hexdump(self, start_addr, length, arrange_as, offset=0):
endianness = endian_str()
endianness = gef.arch.endianness

base_address_color = gef.config["theme.dereference_base_address"]
show_ascii = gef.config["hexdump.always_show_ascii"]
Expand Down Expand Up @@ -9209,7 +9200,7 @@ def do_invoke(self, argv, *args, **kwargs):
addr = align_address(parse_address(args.location))
size, fcode = self.SUPPORTED_SIZES[self.format]

d = endian_str()
d = gef.arch.endianness
for value in args.values:
value = parse_address(value) & ((1 << size * 8) - 1)
vstr = struct.pack(d + fcode, value)
Expand Down Expand Up @@ -11343,7 +11334,15 @@ def arenas(self):
@property
def base_address(self):
if not self.__heap_base:
self.__heap_base = parse_address("mp_->sbrk_base")
base = 0
try:
base = parse_address("mp_->sbrk_base")
except gdb.error:
# missing symbol, try again
base = 0
if not base:
base = get_section_base_address("[heap]")
self.__heap_base = base
return self.__heap_base

@property
Expand Down

0 comments on commit e709aeb

Please sign in to comment.