Skip to content

fix(jingle_sleigh)!: delay slots, sleigh context state, and image thread safety - #304

Merged
toolCHAINZ merged 1 commit into
mainfrom
fix/sleigh-context-state
Nov 19, 2026
Merged

toolCHAINZ merged 1 commit into
mainfrom
fix/sleigh-context-state

Conversation

@toolCHAINZ

Copy link
Copy Markdown
Owner

Summary

Fixes three issues in how jingle_sleigh wraps SLEIGH: delay slots, context state, and thread safety.

Delay slots

get_one_instruction took its length from instructionLength(), which covers only the instruction's own bytes. For an instruction with a delay slot, oneInstruction() also folds the delay slot's p-code into the instruction. The result was that the synthetic Fallthrough pointed into the delay slot, and read() decoded the delay slot again as a separate instruction, so it ran twice. The length now comes from oneInstruction()'s return value. Example: b 0x14; addiu; nop on MIPS now reports len=8 with its fallthrough at 0x8.

Context state

SLEIGH's context database is shared, mutable state. Any decode that uses globalset writes context for other addresses (for example, ARM blx switches its target to Thumb), so a decode's result depended on what had been decoded before it, on any thread. Changes:

  • instruction_at always decodes against default context. Resetting the database only happens when a decode has actually written context, so architectures that never do (e.g. x86) pay nothing.
  • read / read_until_branch see only the context written by earlier instructions in the same iteration. If another call decodes in between, the iterator resets the context and re-decodes its own earlier instructions. The iterator doesn't hold the lock between steps, so calling instruction_at inside a read loop can't deadlock.
  • set_image now starts from default context.
  • set_base_address now clears SLEIGH's per-address parse cache. Before, after a rebase, a cached decode of the old bytes could be returned.
  • register_context no longer aborts the process when called after context has been written. SLEIGH threw a C++ exception there that nothing caught.
  • ContextFFI now owns its load image. Previously each setImage leaked one.

Thread safety

LoadedSleighContext had a blanket unsafe impl Send + Sync, with no Send/Sync requirement on the image. Rust code (read_bytes, get_sections) also calls into the image without taking the lock, while SLEIGH calls into it on another thread. Images must now be Send + Sync, both blanket unsafe impls are gone, and the only one left is a narrow unsafe impl Send on the FFI handle, with a safety comment.

Breaking changes

  • Images passed to initialize_with_image, set_image and LoadedSleighContext::load must be Send + Sync. All images in this repo already are.
  • Instruction::length now includes any delay slot bytes.

Testing

  • Added tests for: delay-slot folding (MIPS), context not leaking into instruction_at (ARM), an iterator applying its own context, an iterator surviving decodes interleaved from elsewhere, register_context after context writes, a rebase clearing the parse cache, and LoadedSleighContext being Send + Sync.
  • Run against the code before this change, all of these fail or abort except two: the iterator-applies-its-own-context test, a sanity check on behavior that was already correct, and the Send + Sync check, which wasn't run against the old code.
  • The new tests need a Ghidra installation in GHIDRA_INSTALL_DIR that includes compiled ARM and MIPS .sla files.
  • cargo test --workspace and cargo clippy --workspace --all-features --all-targets (with -Dwarnings) both pass.

🤖 Generated with Claude Code

…ead safety

- Report instruction length from oneInstruction, which covers delay slots
  folded into the instruction's p-code. Previously the delay slot was
  decoded a second time and the synthetic fallthrough pointed into it.
- Decode instruction_at against default context, independent of what was
  decoded before. Iterators see only the context committed by their own
  earlier instructions, replaying them if another caller decoded in between.
- Reset context on set_image, invalidate sleigh's parse cache on
  set_base_address, and allow register_context after context commits
  (previously an uncaught C++ exception).
- Own the load image in ContextFFI instead of leaking one per setImage.
- Require Send + Sync images and drop the blanket unsafe Send/Sync impls.

BREAKING CHANGE: images passed to initialize_with_image, set_image and
LoadedSleighContext::load must be Send + Sync. Instruction::length now
includes any delay slot bytes.
@toolCHAINZ
toolCHAINZ merged commit 7868fe1 into main Nov 19, 2026
8 checks passed
@toolCHAINZ
toolCHAINZ deleted the fix/sleigh-context-state branch November 19, 2026 21:03
@toolCHAINZ toolCHAINZ mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant