Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
folkertdev committed Mar 29, 2025
commit b00b6a8cdf3eeef354872a9cd6d0207949be1c22
36 changes: 18 additions & 18 deletions src/doc/unstable-book/src/compiler-flags/sanitizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,36 @@ 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)
```rust
#![feature(naked_functions)]

use std::arch::asm;
use std::arch::naked_asm;
use std::mem;

fn add_one(x: i32) -> i32 {
x + 1
}

# #[cfg(not(target_arch = "x86_64"))] pub extern "C" fn add_two(_: i32) {}

#[naked]
# #[cfg(target_arch = "x86_64")]
pub extern "C" fn add_two(x: i32) {
// x + 2 preceded by a landing pad/nop block
unsafe {
asm!(
"
nop
nop
nop
nop
nop
nop
nop
nop
nop
lea eax, [rdi+2]
ret
",
options(noreturn)
);
naked_asm!(
"nop",
"nop",
"nop",
"nop",
"nop",
"nop",
"nop",
"nop",
"nop",
"lea eax, [rdi+2]",
"ret",
)
}
}

Expand Down
Loading