Skip to content

Commit 90f5101

Browse files
authored
Rollup merge of #139113 - folkertdev:sanitizer-unstable-book-check-block, r=rcvalle
unstable book: in a sanitizer example, check the code Use some `#` directives to make sure the code checks on x86_64, and does not produce errors on other platforms. This example still used an older version of `#[naked]`, and because the snippet was ignored that was missed before. I'm not sure when this gets built on CI exactly, so it might be worthwhile to try and build it for a non-x86_64 architecture to make sure that works. I'm not sure how to verify locally that e.g. on aarch64 this code works without errors/warnings. try-job: aarch64-apple try-job: x86_64-msvc-2
2 parents 349fbba + b301e3a commit 90f5101

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/doc/unstable-book/src/compiler-flags/sanitizer.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,16 @@ See the [Clang ControlFlowIntegrity documentation][clang-cfi] for more details.
244244
245245
## Example 1: Redirecting control flow using an indirect branch/call to an invalid destination
246246
247-
```rust,ignore (making doc tests pass cross-platform is hard)
248-
use std::arch::naked_asm;
249-
use std::mem;
250-
247+
```rust
251248
fn add_one(x: i32) -> i32 {
252249
x + 1
253250
}
254251

255252
#[unsafe(naked)]
256-
pub extern "C" fn add_two(x: i32) {
253+
# #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
254+
pub extern "sysv64" fn add_two(x: i32) {
257255
// x + 2 preceded by a landing pad/nop block
258-
naked_asm!(
256+
std::arch::naked_asm!(
259257
"
260258
nop
261259
nop
@@ -281,16 +279,18 @@ fn main() {
281279

282280
println!("The answer is: {}", answer);
283281

284-
println!("With CFI enabled, you should not see the next answer");
285-
let f: fn(i32) -> i32 = unsafe {
286-
// Offset 0 is a valid branch/call destination (i.e., the function entry
287-
// point), but offsets 1-8 within the landing pad/nop block are invalid
288-
// branch/call destinations (i.e., within the body of the function).
289-
mem::transmute::<*const u8, fn(i32) -> i32>((add_two as *const u8).offset(5))
290-
};
291-
let next_answer = do_twice(f, 5);
292-
293-
println!("The next answer is: {}", next_answer);
282+
# #[cfg(all(target_os = "linux", target_arch = "x86_64"))] {
283+
println!("With CFI enabled, you should not see the next answer");
284+
let f: fn(i32) -> i32 = unsafe {
285+
// Offset 0 is a valid branch/call destination (i.e., the function entry
286+
// point), but offsets 1-8 within the landing pad/nop block are invalid
287+
// branch/call destinations (i.e., within the body of the function).
288+
std::mem::transmute::<*const u8, fn(i32) -> i32>((add_two as *const u8).offset(5))
289+
};
290+
let next_answer = do_twice(f, 5);
291+
292+
println!("The next answer is: {}", next_answer);
293+
# }
294294
}
295295
```
296296
Fig. 1. Redirecting control flow using an indirect branch/call to an invalid

0 commit comments

Comments
 (0)