Skip to content

Add REPL set_offset and search_offset commands - #6

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

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

Conversation

@YSaxon

@YSaxon YSaxon commented Mar 11, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Provide REPL primitives to set library offsets directly and to discover offsets by searching a library's readable memory, complementing the existing calculate_offset workflow.
  • Make it easier to compute and store offsets for libraries across platforms by searching process memory for a known byte sequence and validating uniqueness before storing the offset.

Description

  • Added parseSetOffset to implement set_offset <library> <offset> which stores an offset via the existing storeOffsetForLibLoadedAtAddress mechanism.
  • Added parseSearchOffset to implement search_offset [<variable>] <library> <bytestring> <address> which parses bytestring (plain text or 0x.. hex), locates readable memory ranges for the target library, searches for the pattern, hex-dumps context around the first (and second) matches, verifies uniqueness, computes address_in_memory - address_provided, stores the offset and optionally stores it in the given variable.
  • Implemented helpers in src/main.c: parseByteString, MemoryRangeList management, collectReadableLibraryRanges with platform-specific memory discovery (/proc/self/maps for Linux, VirtualQuery for Windows, Mach VM region walking for macOS), findSubsequence, and dumpMatchContext.
  • Updated REPL help text and the REPL command dispatcher to register set_offset and search_offset commands.

Testing

  • Built the project with cmake -S . -B build && cmake --build build -j4, which completed successfully (compile succeeded, warnings only).
  • Ran unit and integration tests via ctest --test-dir build --output-on-failure and all tests passed (156/156 tests 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: d263c66866

ℹ️ 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 +173 to +174
if ((info.protection & VM_PROT_READ) != 0) {
appendMemoryRange(ranges, (uintptr_t)addr, (uintptr_t)(addr + size));

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 Restrict macOS range collection to the requested library

In the __APPLE__ branch, every readable VM region is appended without checking whether it belongs to the requested library, so search_offset can match bytes from unrelated images and either compute a wrong offset or fail as "multiple times" even when the target library has a unique match. This affects macOS runs whenever the pattern exists outside the requested module.

Useful? React with 👍 / 👎.

Comment thread src/main.c
return;
}

ptrdiff_t offset = (uintptr_t)firstMatch - (uintptr_t)providedAddress;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize ARM Thumb addresses before offset subtraction

parseSearchOffset subtracts providedAddress directly, but unlike parseCalculateOffset it never clears the Thumb bit on ARM. When the user supplies a function/symbol pointer in Thumb mode (LSB set), the stored offset becomes off by one byte, which breaks later address reconstruction for relative calls on ARM targets.

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