-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix segfault in cs_option() on s390x #166
Merged
tmfink
merged 2 commits into
capstone-rust:master
from
iii-i:fix-segfault-in-cs-option-on-s390x
Jan 31, 2025
Merged
Fix segfault in cs_option() on s390x #166
tmfink
merged 2 commits into
capstone-rust:master
from
iii-i:fix-segfault-in-cs-option-on-s390x
Jan 31, 2025
Conversation
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
d751f07
to
0658209
Compare
|
philmd
approved these changes
Jan 14, 2025
0658209
to
ae8f3bc
Compare
|
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
ae8f3bc
to
7c89f33
Compare
|
Thanks for the PR! Your comments are very descriptive. |
tmfink
requested changes
Jan 24, 2025
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.
capstone-rs crashes on s390x (also knows as SystemZ in the LLVM world)
as follows:
handle->arch is clearly corrupted. It is assigned as follows in
cs_open():
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:
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