diff options
| author | Folkert de Vries <folkert@folkertdev.nl> | 2025-03-29 17:50:20 +0100 |
|---|---|---|
| committer | Folkert de Vries <folkert@folkertdev.nl> | 2025-09-03 13:10:48 +0200 |
| commit | b301e3aed4975d6431cdc963a85b33ca7157757c (patch) | |
| tree | 88848ce14342e8e29f3df20fae1fc12937d352fd /src | |
| parent | 94722cabf4983abcbe5088c1b8e81517ba2a7126 (diff) | |
| download | rust-b301e3aed4975d6431cdc963a85b33ca7157757c.tar.gz rust-b301e3aed4975d6431cdc963a85b33ca7157757c.zip | |
unstable book: in a sanitizer example, check the code
this uses some # directives to make sure the code works on x86_64, and does not produce errors on other platforms
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/unstable-book/src/compiler-flags/sanitizer.md | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/doc/unstable-book/src/compiler-flags/sanitizer.md b/src/doc/unstable-book/src/compiler-flags/sanitizer.md index 2f9d4d22e5a..493256de99d 100644 --- a/src/doc/unstable-book/src/compiler-flags/sanitizer.md +++ b/src/doc/unstable-book/src/compiler-flags/sanitizer.md @@ -244,18 +244,16 @@ See the [Clang ControlFlowIntegrity documentation][clang-cfi] for more details. ## Example 1: Redirecting control flow using an indirect branch/call to an invalid destination -```rust,ignore (making doc tests pass cross-platform is hard) -use std::arch::naked_asm; -use std::mem; - +```rust fn add_one(x: i32) -> i32 { x + 1 } #[unsafe(naked)] -pub extern "C" fn add_two(x: i32) { +# #[cfg(all(target_os = "linux", target_arch = "x86_64"))] +pub extern "sysv64" fn add_two(x: i32) { // x + 2 preceded by a landing pad/nop block - naked_asm!( + std::arch::naked_asm!( " nop nop @@ -281,16 +279,18 @@ fn main() { println!("The answer is: {}", answer); - println!("With CFI enabled, you should not see the next answer"); - let f: fn(i32) -> i32 = unsafe { - // Offset 0 is a valid branch/call destination (i.e., the function entry - // point), but offsets 1-8 within the landing pad/nop block are invalid - // branch/call destinations (i.e., within the body of the function). - mem::transmute::<*const u8, fn(i32) -> i32>((add_two as *const u8).offset(5)) - }; - let next_answer = do_twice(f, 5); - - println!("The next answer is: {}", next_answer); +# #[cfg(all(target_os = "linux", target_arch = "x86_64"))] { + println!("With CFI enabled, you should not see the next answer"); + let f: fn(i32) -> i32 = unsafe { + // Offset 0 is a valid branch/call destination (i.e., the function entry + // point), but offsets 1-8 within the landing pad/nop block are invalid + // branch/call destinations (i.e., within the body of the function). + std::mem::transmute::<*const u8, fn(i32) -> i32>((add_two as *const u8).offset(5)) + }; + let next_answer = do_twice(f, 5); + + println!("The next answer is: {}", next_answer); +# } } ``` Fig. 1. Redirecting control flow using an indirect branch/call to an invalid |
