rs: add DisasmBuffer api to reuse the cs_insn allocation further#186
Open
rs: add DisasmBuffer api to reuse the cs_insn allocation further#186
Conversation
jiegec
commented
Jul 24, 2025
capstone-rs/src/capstone.rs
Outdated
| &'cs self, | ||
| code: &'buf [u8], | ||
| addr: u64, | ||
| buffer: DisasmBuffer<'cs>, |
Contributor
Author
There was a problem hiding this comment.
A possible alternative to this is to add another lifetime and use mutable reference to access DisasmBuffer: &mut DisasmBuffer. So into_buffer is no longer needed (but relying on dropping the iterator upon reuse). But the user must hold DisasmBuffer in a variable then. I am not sure which is better.
Member
There was a problem hiding this comment.
I think we need to take a &mut DisasmBuffer here to ensure there is exclusive access and to allow reuse:
We want to make sure the following is prevented:
let buffer = ...;
let mut insn_iter1 = cs.disasm_iter_with_buffer(code1, addr1, &mut buffer);
// unsound since we are mutating buffer from different iterators
let mut insn_iter2 = cs.disasm_iter_with_buffer(code2, addr1, &mut buffer);
let insn1 = insn_iter1.next().unwrap();
let insn2 = insn_iter2.next().unwrap();
let insn3 = insn_iter1.next().unwrap();While still allowing:
let buffer = ...;
for code in [code1, code2, code3] {
let mut insn_iter = cs.disasm_iter_with_buffer(code2, addr1, &mut buffer);
let insn = insn_iter.next().unwrap();
}88b7813 to
91ab670
Compare
Closes #185 partially.
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.
Add
DisasmBufferstruct to wrap cs_insn allocation. It calls cs_free when dropped.Add
disasm_iter_with_buffer,create_bufferandinto_bufferfunctions to ease buffer reuse.The other
disasm*functions are not removed yet.