Skip to content

Commit 3d65927

Browse files
committed
Auto merge of #3170 - RalfJung:libc-tests, r=RalfJung
a bit of libc test reorganization
2 parents 0b6b44f + 8edb0ad commit 3d65927

File tree

5 files changed

+76
-77
lines changed

5 files changed

+76
-77
lines changed

src/tools/miri/ci.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ case $HOST_TARGET in
108108
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
109109
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
110110
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
111-
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy libc-getrandom libc-reallocarray atomic env/var
111+
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthread-threadname libc-getentropy libc-getrandom libc-misc atomic env align
112112
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
113113
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
114114
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm

src/tools/miri/tests/pass-dep/shims/libc-misc.rs

+24
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn test_thread_local_errno() {
172172
}
173173

174174
/// Tests whether clock support exists at all
175+
#[cfg(not(target_os = "freebsd"))]
175176
fn test_clocks() {
176177
let mut tp = std::mem::MaybeUninit::<libc::timespec>::uninit();
177178
let is_error = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, tp.as_mut_ptr()) };
@@ -237,6 +238,7 @@ fn test_isatty() {
237238
}
238239
}
239240

241+
#[cfg(not(target_os = "freebsd"))]
240242
fn test_posix_mkstemp() {
241243
use std::ffi::CString;
242244
use std::ffi::OsStr;
@@ -388,8 +390,23 @@ fn test_dlsym() {
388390
assert_eq!(errno, libc::EBADF);
389391
}
390392

393+
#[cfg(not(target_os = "macos"))]
394+
fn test_reallocarray() {
395+
unsafe {
396+
let mut p = libc::reallocarray(std::ptr::null_mut(), 4096, 2);
397+
assert!(!p.is_null());
398+
libc::free(p);
399+
p = libc::malloc(16);
400+
let r = libc::reallocarray(p, 2, 32);
401+
assert!(!r.is_null());
402+
libc::free(r);
403+
}
404+
}
405+
391406
fn main() {
392407
test_posix_gettimeofday();
408+
409+
#[cfg(not(target_os = "freebsd"))] // FIXME we should support this on FreeBSD as well
393410
test_posix_mkstemp();
394411

395412
test_posix_realpath_alloc();
@@ -399,12 +416,19 @@ fn main() {
399416
test_thread_local_errno();
400417

401418
test_isatty();
419+
420+
#[cfg(not(target_os = "freebsd"))] // FIXME we should support this on FreeBSD as well
402421
test_clocks();
422+
403423
test_dlsym();
404424

405425
test_memcpy();
406426
test_strcpy();
407427

428+
#[cfg(not(target_os = "macos"))] // reallocarray does not exist on macOS
429+
test_reallocarray();
430+
431+
// These are Linux-specific
408432
#[cfg(target_os = "linux")]
409433
{
410434
test_posix_fadvise();

src/tools/miri/tests/pass-dep/shims/libc-reallocarray.rs

-16
This file was deleted.

src/tools/miri/tests/pass-dep/shims/pthreads.rs renamed to src/tools/miri/tests/pass-dep/shims/pthread-sync.rs

-60
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
//@ignore-target-windows: No libc on Windows
2-
use std::ffi::CStr;
3-
#[cfg(not(target_os = "freebsd"))]
4-
use std::ffi::CString;
5-
use std::thread;
62

73
fn main() {
8-
test_named_thread_truncation();
9-
10-
#[cfg(not(target_os = "freebsd"))]
114
test_mutex_libc_init_recursive();
12-
#[cfg(not(target_os = "freebsd"))]
135
test_mutex_libc_init_normal();
14-
#[cfg(not(target_os = "freebsd"))]
156
test_mutex_libc_init_errorcheck();
16-
#[cfg(not(target_os = "freebsd"))]
177
test_rwlock_libc_static_initializer();
188

199
#[cfg(target_os = "linux")]
2010
test_mutex_libc_static_initializer_recursive();
2111
}
2212

23-
#[cfg(not(target_os = "freebsd"))]
2413
fn test_mutex_libc_init_recursive() {
2514
unsafe {
2615
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -45,7 +34,6 @@ fn test_mutex_libc_init_recursive() {
4534
}
4635
}
4736

48-
#[cfg(not(target_os = "freebsd"))]
4937
fn test_mutex_libc_init_normal() {
5038
unsafe {
5139
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -68,7 +56,6 @@ fn test_mutex_libc_init_normal() {
6856
}
6957
}
7058

71-
#[cfg(not(target_os = "freebsd"))]
7259
fn test_mutex_libc_init_errorcheck() {
7360
unsafe {
7461
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
@@ -114,7 +101,6 @@ fn test_mutex_libc_static_initializer_recursive() {
114101
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
115102
// need to go a layer deeper and test the behavior of the libc functions, because
116103
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117-
#[cfg(not(target_os = "freebsd"))]
118104
fn test_rwlock_libc_static_initializer() {
119105
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
120106
unsafe {
@@ -139,49 +125,3 @@ fn test_rwlock_libc_static_initializer() {
139125
assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
140126
}
141127
}
142-
143-
fn test_named_thread_truncation() {
144-
let long_name = std::iter::once("test_named_thread_truncation")
145-
.chain(std::iter::repeat(" yada").take(100))
146-
.collect::<String>();
147-
148-
fn set_thread_name(name: &CStr) -> i32 {
149-
#[cfg(target_os = "linux")]
150-
return unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) };
151-
#[cfg(target_os = "freebsd")]
152-
unsafe {
153-
// pthread_set_name_np does not return anything
154-
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast());
155-
return 0;
156-
};
157-
#[cfg(target_os = "macos")]
158-
return unsafe { libc::pthread_setname_np(name.as_ptr().cast()) };
159-
}
160-
161-
let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
162-
// Rust remembers the full thread name itself.
163-
assert_eq!(thread::current().name(), Some(long_name.as_str()));
164-
165-
// But the system is limited -- make sure we successfully set a truncation.
166-
let mut buf = vec![0u8; long_name.len() + 1];
167-
#[cfg(not(target_os = "freebsd"))]
168-
unsafe {
169-
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
170-
};
171-
#[cfg(target_os = "freebsd")]
172-
unsafe {
173-
libc::pthread_get_name_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
174-
};
175-
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
176-
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
177-
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
178-
179-
// Also test directly calling pthread_setname to check its return value.
180-
assert_eq!(set_thread_name(&cstr), 0);
181-
// But with a too long name it should fail (except on FreeBSD where the
182-
// function has no return, hence cannot indicate failure).
183-
#[cfg(not(target_os = "freebsd"))]
184-
assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0);
185-
});
186-
result.unwrap().join().unwrap();
187-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//@ignore-target-windows: No libc on Windows
2+
use std::ffi::CStr;
3+
#[cfg(not(target_os = "freebsd"))]
4+
use std::ffi::CString;
5+
use std::thread;
6+
7+
fn main() {
8+
let long_name = std::iter::once("test_named_thread_truncation")
9+
.chain(std::iter::repeat(" yada").take(100))
10+
.collect::<String>();
11+
12+
fn set_thread_name(name: &CStr) -> i32 {
13+
#[cfg(target_os = "linux")]
14+
return unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) };
15+
#[cfg(target_os = "freebsd")]
16+
unsafe {
17+
// pthread_set_name_np does not return anything
18+
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast());
19+
return 0;
20+
};
21+
#[cfg(target_os = "macos")]
22+
return unsafe { libc::pthread_setname_np(name.as_ptr().cast()) };
23+
}
24+
25+
let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
26+
// Rust remembers the full thread name itself.
27+
assert_eq!(thread::current().name(), Some(long_name.as_str()));
28+
29+
// But the system is limited -- make sure we successfully set a truncation.
30+
let mut buf = vec![0u8; long_name.len() + 1];
31+
#[cfg(not(target_os = "freebsd"))]
32+
unsafe {
33+
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
34+
};
35+
#[cfg(target_os = "freebsd")]
36+
unsafe {
37+
libc::pthread_get_name_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
38+
};
39+
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
40+
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
41+
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
42+
43+
// Also test directly calling pthread_setname to check its return value.
44+
assert_eq!(set_thread_name(&cstr), 0);
45+
// But with a too long name it should fail (except on FreeBSD where the
46+
// function has no return, hence cannot indicate failure).
47+
#[cfg(not(target_os = "freebsd"))]
48+
assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0);
49+
});
50+
result.unwrap().join().unwrap();
51+
}

0 commit comments

Comments
 (0)