Skip to content

Commit

Permalink
- fixed heap CI failures (defered to #785)
Browse files Browse the repository at this point in the history
- minor type adjustments
  • Loading branch information
hugsy authored and Grazfather committed Jan 16, 2022
1 parent fa28aa2 commit e4a77aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
name: CI Test for GEF

on: [push, pull_request]
on:
push:
branches:
- master
- dev

pull_request:
branches:
- master
- dev

jobs:
build:
Expand Down Expand Up @@ -71,4 +80,4 @@ jobs:
- name: Run linter
run: |
make lint
44 changes: 20 additions & 24 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def highlight_text(text: str) -> str:


def gef_print(x: str = "", *args: Tuple, **kwargs: Dict[str, Any]) -> Optional[int]:
"""Wrapper around print(), using string buffering feature."""
"""Wrapper around `print()`, using string buffering feature."""
x = highlight_text(x)
if gef.ui.stream_buffer and not is_debug():
return gef.ui.stream_buffer.write(x + kwargs.get("end", "\n"))
Expand Down Expand Up @@ -1058,8 +1058,6 @@ def __init__(self, addr: str) -> None:
try:
self.__addr = parse_address(f"&{addr}")
except gdb.error:
warn(f"Could not parse address '&{addr}' when searching malloc_state struct, "
"using '&main_arena' instead")
self.__addr = search_for_main_arena()
# if `search_for_main_arena` throws `gdb.error` on symbol lookup:
# it means the session is not started, so just propagate the exception
Expand Down Expand Up @@ -3203,23 +3201,22 @@ def get_filepath() -> Optional[str]:


def download_file(remote_path: str, use_cache: bool = False, local_name: Optional[str] = None) -> Optional[str]:
"""Download filename `remote_path` inside the mirror tree inside the gef.config["gef.tempdir"].
The tree architecture must be gef.config["gef.tempdir"]/gef/<local_pid>/<remote_filepath>.
"""Download filename `remote_path` inside the mirror tree inside the `gef.config["gef.tempdir"]`.
The tree architecture must be `gef.config["gef.tempdir"]/gef/<local_pid>/<remote_filepath>`.
This allow a "chroot-like" tree format."""

try:
local_root = pathlib.Path(gef.config["gef.tempdir"]) / str(gef.session.pid)
if local_name is None:
local_path = local_root / remote_path.strip(os.sep)
else:
local_path = local_root / local_name.strip(os.sep)
local_root = pathlib.Path(gef.config["gef.tempdir"]) / str(gef.session.pid)
if local_name is None:
local_path = local_root / remote_path.strip(os.sep)
else:
local_path = local_root / local_name.strip(os.sep)

if use_cache and local_path.exists():
return str(local_path.absolute())
if use_cache and local_path.exists():
return str(local_path.absolute())

try:
local_path.parent.mkdir(parents=True, exist_ok=True)
gdb.execute(f"remote get {remote_path} {local_path.absolute()}")

local_path = str(local_path.absolute())
except gdb.error:
# fallback memory view
Expand All @@ -3231,7 +3228,7 @@ def download_file(remote_path: str, use_cache: bool = False, local_name: Optiona

except Exception as e:
err(f"download_file() failed: {e}")
local_name = None
local_path = None

return local_path

Expand Down Expand Up @@ -3298,7 +3295,7 @@ def process_lookup_path(name: str, perm=Permission.ALL) -> Optional[Section]:
return None

for sect in gef.memory.maps:
if name in sect.path and sect.permission.value & perm:
if name in sect.path and sect.permission & perm:
return sect

return None
Expand Down Expand Up @@ -3338,14 +3335,13 @@ def lookup_address(address: int) -> Address:

def xor(data: ByteString, key: str) -> bytearray:
"""Return `data` xor-ed with `key`."""
key = key.lstrip("0x")
key = binascii.unhexlify(key)
return bytearray(x ^ y for x, y in zip(data, itertools.cycle(key)))
key_raw = binascii.unhexlify(key.lstrip("0x"))
return bytearray(x ^ y for x, y in zip(data, itertools.cycle(key_raw)))


def is_hex(pattern: str) -> bool:
"""Return whether provided string is a hexadecimal value."""
if not pattern.startswith("0x") and not pattern.startswith("0X"):
if not pattern.lower().startswith("0x"):
return False
return len(pattern) % 2 == 0 and all(c in string.hexdigits for c in pattern[2:])

Expand Down Expand Up @@ -7261,12 +7257,12 @@ def __init__(self) -> None:

@only_if_gdb_running
def do_invoke(self, argv: List) -> None:
if gef.heap.selected_arena is None:
if gef.heap.main_arena is None:
err("Invalid Glibc arena")
return

arena_addr = f"*{argv[0]}" if len(argv) == 1 else gef.heap.selected_arena
gef_print(titlify(f"Unsorted Bin for arena '{arena_addr:s}'"))
gef_print(titlify(f"Unsorted Bin for arena '{arena_addr!s}'"))
nb_chunk = GlibcHeapBinsCommand.pprint_bin(arena_addr, 0, "unsorted_")
if nb_chunk >= 0:
info(f"Found {nb_chunk:d} chunks in unsorted bin.")
Expand All @@ -7291,7 +7287,7 @@ def do_invoke(self, argv: List) -> None:
return

arena = GlibcArena(f"*{argv[0]}") if len(argv) == 1 else gef.heap.selected_arena
gef_print(titlify(f"Small Bins for arena '{arena:s}'"))
gef_print(titlify(f"Small Bins for arena '{arena!s}'"))
bins = {}
for i in range(1, 63):
nb_chunk = GlibcHeapBinsCommand.pprint_bin(arena, i, "small_")
Expand Down Expand Up @@ -7321,7 +7317,7 @@ def do_invoke(self, argv: List) -> None:
return

arena_addr = f"*{argv[0]}" if len(argv) == 1 else gef.heap.selected_arena
gef_print(titlify(f"Large Bins for arena '{arena_addr:s}'"))
gef_print(titlify(f"Large Bins for arena '{arena_addr!s}'"))
bins = {}
for i in range(63, 126):
nb_chunk = GlibcHeapBinsCommand.pprint_bin(arena_addr, i, "large_")
Expand Down

0 comments on commit e4a77aa

Please sign in to comment.