diff options
| author | Ibraheem Ahmed <ibraheem@ibraheem.ca> | 2025-07-10 16:37:54 -0400 |
|---|---|---|
| committer | Ibraheem Ahmed <ibraheem@ibraheem.ca> | 2025-07-15 17:24:47 -0400 |
| commit | d060859376994b32dad84e5bb7f507fbb1dcb89a (patch) | |
| tree | 97acec009ac297624a8881d6e63f9f9f4851a468 | |
| parent | 85c89494d9efdc90f2ac08446c00b0b78287b7a2 (diff) | |
| download | rust-d060859376994b32dad84e5bb7f507fbb1dcb89a.tar.gz rust-d060859376994b32dad84e5bb7f507fbb1dcb89a.zip | |
add test for global constructors
| -rw-r--r-- | src/tools/miri/tests/pass/shims/ctor.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/tools/miri/tests/pass/shims/ctor.rs b/src/tools/miri/tests/pass/shims/ctor.rs new file mode 100644 index 00000000000..c832e3f82a3 --- /dev/null +++ b/src/tools/miri/tests/pass/shims/ctor.rs @@ -0,0 +1,43 @@ +use std::sync::atomic::{AtomicUsize, Ordering}; + +static COUNT: AtomicUsize = AtomicUsize::new(0); + +unsafe extern "C" fn ctor() { + COUNT.fetch_add(1, Ordering::Relaxed); +} + +macro_rules! ctor { + ($ident:ident = $ctor:ident) => { + #[cfg_attr( + all(any( + target_os = "linux", + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "haiku", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "solaris", + target_os = "none", + target_family = "wasm", + )), + link_section = ".init_array" + )] + #[cfg_attr(windows, link_section = ".CRT$XCU")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios"), + link_section = "__DATA,__mod_init_func" + )] + #[used] + static $ident: unsafe extern "C" fn() = $ctor; + }; +} + +ctor! { CTOR1 = ctor } +ctor! { CTOR2 = ctor } +ctor! { CTOR3 = ctor } + +fn main() { + assert_eq!(COUNT.load(Ordering::Relaxed), 3, "ctors did not run"); +} |
