Skip to content

Commit d060859

Browse files
committed
add test for global constructors
1 parent 85c8949 commit d060859

File tree

1 file changed

+43
-0
lines changed
  • src/tools/miri/tests/pass/shims

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::sync::atomic::{AtomicUsize, Ordering};
2+
3+
static COUNT: AtomicUsize = AtomicUsize::new(0);
4+
5+
unsafe extern "C" fn ctor() {
6+
COUNT.fetch_add(1, Ordering::Relaxed);
7+
}
8+
9+
macro_rules! ctor {
10+
($ident:ident = $ctor:ident) => {
11+
#[cfg_attr(
12+
all(any(
13+
target_os = "linux",
14+
target_os = "android",
15+
target_os = "dragonfly",
16+
target_os = "freebsd",
17+
target_os = "haiku",
18+
target_os = "illumos",
19+
target_os = "netbsd",
20+
target_os = "openbsd",
21+
target_os = "solaris",
22+
target_os = "none",
23+
target_family = "wasm",
24+
)),
25+
link_section = ".init_array"
26+
)]
27+
#[cfg_attr(windows, link_section = ".CRT$XCU")]
28+
#[cfg_attr(
29+
any(target_os = "macos", target_os = "ios"),
30+
link_section = "__DATA,__mod_init_func"
31+
)]
32+
#[used]
33+
static $ident: unsafe extern "C" fn() = $ctor;
34+
};
35+
}
36+
37+
ctor! { CTOR1 = ctor }
38+
ctor! { CTOR2 = ctor }
39+
ctor! { CTOR3 = ctor }
40+
41+
fn main() {
42+
assert_eq!(COUNT.load(Ordering::Relaxed), 3, "ctors did not run");
43+
}

0 commit comments

Comments
 (0)