Skip to content

Commit 1c5c24f

Browse files
committed
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
1 parent 19cab6b commit 1c5c24f

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -244,43 +244,42 @@ 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)
247+
```rust
248248
#![feature(naked_functions)]
249249

250-
use std::arch::asm;
250+
use std::arch::naked_asm;
251251
use std::mem;
252252

253253
fn add_one(x: i32) -> i32 {
254254
x + 1
255255
}
256256

257257
#[naked]
258+
# #[cfg(target_arch = "x86_64")]
258259
pub extern "C" fn add_two(x: i32) {
259260
// x + 2 preceded by a landing pad/nop block
260261
unsafe {
261-
asm!(
262-
"
263-
nop
264-
nop
265-
nop
266-
nop
267-
nop
268-
nop
269-
nop
270-
nop
271-
nop
272-
lea eax, [rdi+2]
273-
ret
274-
",
275-
options(noreturn)
276-
);
262+
naked_asm!(
263+
"nop",
264+
"nop",
265+
"nop",
266+
"nop",
267+
"nop",
268+
"nop",
269+
"nop",
270+
"nop",
271+
"nop",
272+
"lea eax, [rdi+2]",
273+
"ret",
274+
)
277275
}
278276
}
279277

280278
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
281279
f(arg) + f(arg)
282280
}
283281

282+
# #[cfg(target_arch = "x86_64")]
284283
fn main() {
285284
let answer = do_twice(add_one, 5);
286285

@@ -297,6 +296,7 @@ fn main() {
297296

298297
println!("The next answer is: {}", next_answer);
299298
}
299+
# #[cfg(not(target_arch = "x86_64"))] fn main() {}
300300
```
301301
Fig. 1. Redirecting control flow using an indirect branch/call to an invalid
302302
destination (i.e., within the body of the function).

0 commit comments

Comments
 (0)