|
| 1 | +//! Test stack canaries. |
| 2 | +
|
| 3 | +#![no_std] |
| 4 | +#![no_main] |
| 5 | +#![allow(internal_features)] |
| 6 | +#![allow(unused_imports)] |
| 7 | +#![feature(lang_items)] |
| 8 | +#![feature(core_intrinsics)] |
| 9 | + |
| 10 | +extern crate alloc; |
| 11 | +extern crate compiler_builtins; |
| 12 | + |
| 13 | +use atomic_dbg::dbg; |
| 14 | +use core::arch::asm; |
| 15 | +use origin::{program, thread}; |
| 16 | + |
| 17 | +#[panic_handler] |
| 18 | +fn panic(panic: &core::panic::PanicInfo<'_>) -> ! { |
| 19 | + dbg!(panic); |
| 20 | + core::intrinsics::abort(); |
| 21 | +} |
| 22 | + |
| 23 | +#[lang = "eh_personality"] |
| 24 | +extern "C" fn eh_personality() {} |
| 25 | + |
| 26 | +#[global_allocator] |
| 27 | +static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc; |
| 28 | + |
| 29 | +extern "C" { |
| 30 | + static mut __stack_chk_guard: usize; |
| 31 | +} |
| 32 | + |
| 33 | +/// Read the canary field from its well-known location in TLS, if there is one, |
| 34 | +/// or read `__stack_chk_guard` otherweise. |
| 35 | +fn tls_guard() -> usize { |
| 36 | + let ret; |
| 37 | + |
| 38 | + #[cfg(target_arch = "x86_64")] |
| 39 | + unsafe { |
| 40 | + asm!("mov {}, fs:40", out(reg) ret); |
| 41 | + } |
| 42 | + |
| 43 | + #[cfg(target_arch = "x86")] |
| 44 | + unsafe { |
| 45 | + asm!("mov {}, gs:20", out(reg) ret); |
| 46 | + } |
| 47 | + |
| 48 | + #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 49 | + unsafe { |
| 50 | + ret = __stack_chk_guard; |
| 51 | + } |
| 52 | + |
| 53 | + ret |
| 54 | +} |
| 55 | + |
| 56 | +#[no_mangle] |
| 57 | +unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 { |
| 58 | + assert_ne!(__stack_chk_guard, 0); |
| 59 | + assert_eq!(__stack_chk_guard, tls_guard()); |
| 60 | + |
| 61 | + let thread = thread::create( |
| 62 | + |_args| { |
| 63 | + assert_ne!(__stack_chk_guard, 0); |
| 64 | + assert_eq!(__stack_chk_guard, tls_guard()); |
| 65 | + None |
| 66 | + }, |
| 67 | + &[], |
| 68 | + thread::default_stack_size(), |
| 69 | + thread::default_guard_size(), |
| 70 | + ) |
| 71 | + .unwrap(); |
| 72 | + |
| 73 | + thread::join(thread); |
| 74 | + |
| 75 | + assert_ne!(__stack_chk_guard, 0); |
| 76 | + assert_eq!(__stack_chk_guard, tls_guard()); |
| 77 | + |
| 78 | + program::exit(203); |
| 79 | +} |
0 commit comments