-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix segfault in cs_option() on s390x
capstone-rs crashes on s390x (also knows as SystemZ in the LLVM world) as follows: Thread 2 received signal SIGSEGV, Segmentation fault. 0x000002aa00a6a604 in cs_option (ud=140702726002672, type=CS_OPT_SYNTAX, value=2) at capstone/cs.c:782 782 return arch_configs[handle->arch].arch_option(handle, type, value); (gdb) p/x handle->arch $7 = 0x7ffb handle->arch is clearly corrupted. It is assigned as follows in cs_open(): 0x000002aa00a6a44a <+146>: lgr %r9,%r3 [...] 0x000002aa00a6a468 <+176>: rosbg %r9,%r10,0,31,32 [...] 458 ud->arch = arch; 459 ud->mode = mode; 0x000002aa00a6a47a <+194>: stg %r9,0(%r13) Here the 32-bit arch value and the 32-bit mode value are placed into a 64-bit register and stored with one instruction. The important part is that cs_mode is a 32-bit value passed in %r3, and the code uses instructions with mnemonics ending with "g", which operate on full 64-bit registers [1]. This is allowed because of the s390x ABI [1]: it requires zero-extension of 4-byte int arguments. Bitfield enums are currently generated like this: #[repr(C)] [...] pub struct cs_mode(pub libc::c_uint); which causes the problem above, because the ABI does not require zero-extension of 4-byte struct arguments. Starting from RustTarget::Stable_1_28, bindgen generates #[repr(transparent)] instead of #[repr(C)], which is designed to solve exactly this kind of problems [3]. capstone-rs' MSRV is 1.70, so it should be possible to go even further, but be conservative and use the minimum version necessary to fix the issue. [1] https://publibfp.dhe.ibm.com/epubs/pdf/a227832d.pdf [2] https://github.com/IBM/s390x-abi/releases [3] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
- Loading branch information
Showing
4 changed files
with
340 additions
and
623 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.