Repository navigation
fix(jingle_sleigh)!: delay slots, sleigh context state, and image thread safety - #304
Merged
Merged
Conversation
…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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes three issues in how
jingle_sleighwraps SLEIGH: delay slots, context state, and thread safety.Delay slots
get_one_instructiontook its length frominstructionLength(), 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 syntheticFallthroughpointed into the delay slot, andread()decoded the delay slot again as a separate instruction, so it ran twice. The length now comes fromoneInstruction()'s return value. Example:b 0x14; addiu; nopon MIPS now reportslen=8with its fallthrough at0x8.Context state
SLEIGH's context database is shared, mutable state. Any decode that uses
globalsetwrites context for other addresses (for example, ARMblxswitches its target to Thumb), so a decode's result depended on what had been decoded before it, on any thread. Changes:instruction_atalways 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_branchsee 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 callinginstruction_atinside a read loop can't deadlock.set_imagenow starts from default context.set_base_addressnow clears SLEIGH's per-address parse cache. Before, after a rebase, a cached decode of the old bytes could be returned.register_contextno longer aborts the process when called after context has been written. SLEIGH threw a C++ exception there that nothing caught.ContextFFInow owns its load image. Previously eachsetImageleaked one.Thread safety
LoadedSleighContexthad a blanketunsafe impl Send + Sync, with noSend/Syncrequirement 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 beSend + Sync, both blanketunsafe impls are gone, and the only one left is a narrowunsafe impl Sendon the FFI handle, with a safety comment.Breaking changes
initialize_with_image,set_imageandLoadedSleighContext::loadmust beSend + Sync. All images in this repo already are.Instruction::lengthnow includes any delay slot bytes.Testing
instruction_at(ARM), an iterator applying its own context, an iterator surviving decodes interleaved from elsewhere,register_contextafter context writes, a rebase clearing the parse cache, andLoadedSleighContextbeingSend + Sync.Send + Synccheck, which wasn't run against the old code.GHIDRA_INSTALL_DIRthat includes compiled ARM and MIPS.slafiles.cargo test --workspaceandcargo clippy --workspace --all-features --all-targets(with-Dwarnings) both pass.🤖 Generated with Claude Code