Skip to content

Commit 24ffa6b

Browse files
committed
Generate bindings w/ array_pointers_in_arguments
Ensures cs_regs_access arguments are more clearly pointers to fixed-size arrays.
1 parent 05dc25f commit 24ffa6b

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

capstone-rs/src/capstone.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use alloc::string::String;
22
use core::convert::From;
33
use core::marker::PhantomData;
4+
use core::mem::MaybeUninit;
45

56
use libc::{c_int, c_void};
67

@@ -14,9 +15,6 @@ use crate::instruction::{Insn, InsnDetail, InsnGroupId, InsnId, Instructions, Re
1415

1516
use {crate::ffi::str_from_cstr_ptr, alloc::string::ToString, libc::c_uint};
1617

17-
/// taken from the [python bindings](https://github.com/capstone-engine/capstone/blob/5fb8a423d4455cade99b12912142fd3a0c10d957/bindings/python/capstone/__init__.py#L929)
18-
const MAX_NUM_REGISTERS: usize = 64;
19-
2018
/// An instance of the capstone disassembler
2119
///
2220
/// Create with an instance with [`.new()`](Self::new) and disassemble bytes with [`.disasm_all()`](Self::disasm_all).
@@ -405,16 +403,16 @@ impl Capstone {
405403
let mut regs_read_count: u8 = 0;
406404
let mut regs_write_count: u8 = 0;
407405

408-
let mut regs_write = [0u16; MAX_NUM_REGISTERS];
409-
let mut regs_read = [0u16; MAX_NUM_REGISTERS];
406+
let mut regs_write: MaybeUninit<cs_regs> = MaybeUninit::uninit();
407+
let mut regs_read: MaybeUninit<cs_regs> = MaybeUninit::uninit();
410408

411409
let err = unsafe {
412410
cs_regs_access(
413411
self.csh(),
414412
&insn.insn as *const cs_insn,
415-
&mut regs_read as *mut _,
413+
regs_read.as_mut_ptr(),
416414
&mut regs_read_count as *mut _,
417-
&mut regs_write as *mut _,
415+
regs_write.as_mut_ptr(),
418416
&mut regs_write_count as *mut _,
419417
)
420418
};
@@ -423,18 +421,23 @@ impl Capstone {
423421
return Err(err.into());
424422
}
425423

426-
read.extend(
427-
regs_read
428-
.iter()
429-
.take(regs_read_count as usize)
430-
.map(|x| RegId(*x)),
431-
);
432-
write.extend(
433-
regs_write
434-
.iter()
435-
.take(regs_write_count as usize)
436-
.map(|x| RegId(*x)),
437-
);
424+
// SAFETY: count indicates how many elements are initialized;
425+
let regs_read_slice: &[RegId] = unsafe {
426+
std::slice::from_raw_parts(
427+
regs_read.as_mut_ptr() as *mut RegId,
428+
regs_read_count as usize,
429+
)
430+
};
431+
read.extend_from_slice(regs_read_slice);
432+
433+
// SAFETY: count indicates how many elements are initialized
434+
let regs_write_slice: &[RegId] = unsafe {
435+
std::slice::from_raw_parts(
436+
regs_write.as_mut_ptr() as *mut RegId,
437+
regs_write_count as usize,
438+
)
439+
};
440+
write.extend_from_slice(regs_write_slice);
438441

439442
Ok(())
440443
} else {

capstone-sys/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [UNRELEASED] - YYYY-MM-DD
8+
### Changed
9+
- change `cs_regs_access()` `regs_read`/`regs_write` args to take `*mut cs_regs` (instead of `*mut u16`)
10+
- makes it more clear that args should be fixed size arrays
11+
712
## [0.17.0] - 2025-02-04
813
### Fixed
914
- Segfault when running on s390x (AKA SystemZ)
@@ -123,6 +128,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
123128
### Removed
124129
- Dependency
125130

131+
[UNRELEASED]: https://github.com/capstone-rust/capstone-rs/compare/capstone-sys-v0.17.0...master
126132
[0.17.0]: https://github.com/capstone-rust/capstone-rs/compare/capstone-sys-v0.16.0...capstone-sys-v0.17.0
127133
[0.16.0]: https://github.com/capstone-rust/capstone-rs/compare/capstone-sys-v0.15.0...capstone-sys-v0.16.0
128134
[0.15.0]: https://github.com/capstone-rust/capstone-rs/compare/capstone-sys-v0.14.0...capstone-sys-v0.15.0

capstone-sys/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ fn write_bindgen_bindings(
261261
.constified_enum_module("cs_err|cs_group_type|cs_opt_value")
262262
.bitfield_enum("cs_mode|cs_ac_type")
263263
.rustified_enum(".*")
264+
.array_pointers_in_arguments(true)
264265
.no_copy("cs_insn");
265266

266267
// Whitelist cs_.* functions and types

capstone-sys/pre_generated/capstone.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ pub type cs_vsnprintf_t = ::core::option::Option<
271271
str_: *mut libc::c_char,
272272
size: usize,
273273
format: *const libc::c_char,
274-
ap: *mut __va_list_tag,
274+
ap: *mut va_list,
275275
) -> libc::c_int,
276276
>;
277277
#[doc = " User-defined dynamic memory related functions: malloc/calloc/realloc/free/vsnprintf()\n By default, Capstone uses system's malloc(), calloc(), realloc(), free() & vsnprintf()."]
@@ -15581,9 +15581,9 @@ extern "C" {
1558115581
pub fn cs_regs_access(
1558215582
handle: csh,
1558315583
insn: *const cs_insn,
15584-
regs_read: *mut u16,
15584+
regs_read: *mut cs_regs,
1558515585
regs_read_count: *mut u8,
15586-
regs_write: *mut u16,
15586+
regs_write: *mut cs_regs,
1558715587
regs_write_count: *mut u8,
1558815588
) -> cs_err::Type;
1558915589
}

0 commit comments

Comments
 (0)