Skip to content

Add REPL set_offset and search_offset commands - #7

Open
YSaxon wants to merge 1 commit into
masterfrom
codex/add-set_offset-and-search_offset-functions-14lmab
Open

Add REPL set_offset and search_offset commands#7
YSaxon wants to merge 1 commit into
masterfrom
codex/add-set_offset-and-search_offset-functions-14lmab

Conversation

@YSaxon

@YSaxon YSaxon commented Mar 11, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Provide a way to directly set a library offset and to locate an in-memory bytestring for computing a library offset to complement calculate_offset.
  • Make the search robust across platforms by enumerating readable mapped regions for a library and verifying uniqueness of matches.
  • Give users a quick way to inspect context via hexdumps to verify search results before storing offsets.

Description

  • Added REPL commands set_offset <library> <offset> and search_offset [<variable>] <library> <bytestring> <address> with parsers parseSetOffset and parseSearchOffset in src/main.c.
  • Implemented helpers parse_search_bytes, append_region, find_bytes_in_regions, and dump_match_context and small MemoryRegion/MemoryMatch types to support searching and context dumping.
  • Added platform-specific region enumeration used by search_offset: Linux via /proc/self/maps, Windows via GetModuleInformation + VirtualQuery, and macOS via dyld image lookup + mach_vm_region.
  • Hooked the new commands into the REPL help text and dispatch; forward-declared printVariableWithArgInfo and included required platform headers.
  • When a unique match is found, the code computes address_in_memory - address_provided and stores it via storeOffsetForLibLoadedAtAddress; if a variable name is supplied, the offset is also stored in that variable.

Testing

  • Ran build configuration and compile with cmake -S . -B build and cmake --build build -j4, which completed successfully.
  • Ran the full test suite with ctest --test-dir build --output-on-failure, and all tests passed (156/156 passed).

Codex Task

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 51f684dcaf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/main.c
Comment on lines +296 to +298
void* lib_handle = getOrLoadLibrary(library_name);
void* offset = getAddressFromAddressStringOrNameOfCoercableVariable(offset_str);
storeOffsetForLibLoadedAtAddress(lib_handle, offset);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fail set_offset when getOrLoadLibrary returns NULL

parseSetOffset stores an offset even if getOrLoadLibrary fails, so a bad or non-loadable library name silently writes liboffset_(nil) and reports success; this can corrupt later offset-based lookups that also use a null handle instead of surfacing a load error. Add a null-handle check before calling storeOffsetForLibLoadedAtAddress.

Useful? React with 👍 / 👎.

Comment thread src/main.c
while (*clean_path && isspace((unsigned char)*clean_path)) {
clean_path++;
}
if (path_ends_with(clean_path, library_path)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Canonicalize library path before region-path suffix checks

The Linux/macOS region matcher requires mapped paths to end with resolve_library_path output, but that resolver can return non-canonical symlink paths (for example libfoo.so) while mapped image paths are canonicalized targets (for example libfoo.so.1), causing search_offset to report no readable regions even when the library is loaded. Canonicalizing the resolved path (or matching by inode/basename) avoids this false negative.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant