@@ -244,18 +244,16 @@ See the [Clang ControlFlowIntegrity documentation][clang-cfi] for more details.
244
244
245
245
# # Example 1: Redirecting control flow using an indirect branch/call to an invalid destination
246
246
247
- ` ` ` rust,ignore (making doc tests pass cross-platform is hard)
248
- use std::arch::naked_asm;
249
- use std::mem;
250
-
247
+ ` ` ` rust
251
248
fn add_one(x: i32) -> i32 {
252
249
x + 1
253
250
}
254
251
255
252
# [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) {
257
255
// x + 2 preceded by a landing pad/nop block
258
- naked_asm! (
256
+ std::arch:: naked_asm! (
259
257
"
260
258
nop
261
259
nop
@@ -281,16 +279,18 @@ fn main() {
281
279
282
280
println! (" The answer is: {}" , answer);
283
281
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
+ # }
294
294
}
295
295
` ` `
296
296
Fig. 1. Redirecting control flow using an indirect branch/call to an invalid
0 commit comments