Skip to content

Commit b957e71

Browse files
authored
Add testcases for stack canaries and thread::detach. (#99)
1 parent 0d4b978 commit b957e71

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Test simple `detach` cases.
2+
3+
#![no_std]
4+
#![no_main]
5+
#![allow(internal_features)]
6+
#![feature(lang_items)]
7+
#![feature(core_intrinsics)]
8+
9+
extern crate alloc;
10+
extern crate compiler_builtins;
11+
12+
use atomic_dbg::dbg;
13+
use origin::{program, thread};
14+
15+
#[panic_handler]
16+
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
17+
dbg!(panic);
18+
core::intrinsics::abort();
19+
}
20+
21+
#[lang = "eh_personality"]
22+
extern "C" fn eh_personality() {}
23+
24+
#[global_allocator]
25+
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;
26+
27+
#[no_mangle]
28+
unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 {
29+
let long_thread = thread::create(
30+
|_args| None,
31+
&[],
32+
thread::default_stack_size(),
33+
thread::default_guard_size(),
34+
)
35+
.unwrap();
36+
37+
let short_thread = thread::create(
38+
|_args| None,
39+
&[],
40+
thread::default_stack_size(),
41+
thread::default_guard_size(),
42+
)
43+
.unwrap();
44+
thread::detach(short_thread);
45+
46+
let _thread = thread::create(
47+
|_args| {
48+
thread::detach(thread::current());
49+
None
50+
},
51+
&[],
52+
thread::default_stack_size(),
53+
thread::default_guard_size(),
54+
)
55+
.unwrap();
56+
57+
thread::detach(long_thread);
58+
59+
program::exit(202);
60+
}

tests/test_crates.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ fn test_tls_crt_static_relocation_static() {
5959
fn test_thread_id() {
6060
test_crate("origin-start", &["--bin=thread-id"], &[], "", "", Some(201));
6161
}
62+
63+
#[test]
64+
fn test_detach() {
65+
test_crate("origin-start", &["--bin=detach"], &[], "", "", Some(202));
66+
}
67+
68+
#[test]
69+
fn test_canary() {
70+
test_crate("origin-start", &["--bin=canary"], &[], "", "", Some(203));
71+
}

0 commit comments

Comments
 (0)