Skip to content

Commit 7d8ee71

Browse files
committed
Auto merge of rust-lang#3889 - RalfJung:android-pthread, r=RalfJung
add Android pthread support Also extend what we test on Solarish / FreeBSD, turns out a few more things are working.
2 parents 75921d2 + 5f3bec4 commit 7d8ee71

File tree

8 files changed

+54
-33
lines changed

8 files changed

+54
-33
lines changed

Diff for: src/tools/miri/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ degree documented below):
216216
- For every other target with OS `linux`, `macos`, or `windows`, Miri should generally work, but we
217217
make no promises and we don't run tests for such targets.
218218
- We have unofficial support (not maintained by the Miri team itself) for some further operating systems.
219+
- `solaris` / `illumos`: maintained by @devnexen. Supports `std::{env, thread, sync}`, but not `std::fs`.
219220
- `freebsd`: **maintainer wanted**. Supports `std::env` and parts of `std::{thread, fs}`, but not `std::sync`.
220221
- `android`: **maintainer wanted**. Support very incomplete, but a basic "hello world" works.
221-
- `solaris` / `illumos`: maintained by @devnexen. Support very incomplete, but a basic "hello world" works.
222222
- `wasm`: **maintainer wanted**. Support very incomplete, not even standard output works, but an empty `main` function works.
223223
- For targets on other operating systems, Miri might fail before even reaching the `main` function.
224224

Diff for: src/tools/miri/ci/ci.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ case $HOST_TARGET in
150150
# Partially supported targets (tier 2)
151151
BASIC="empty_main integer vec string btreemap hello hashmap heap_alloc align" # ensures we have the basics: stdout/stderr, system allocator, randomness (for HashMap initialization)
152152
UNIX="panic/panic panic/unwind concurrency/simple atomic libc-mem libc-misc libc-random env num_cpus" # the things that are very similar across all Unixes, and hence easily supported there
153-
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread libc-time fs
154-
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread libc-time fs
155-
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX threadname pthread sync available-parallelism libc-time tls
156-
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX threadname pthread sync available-parallelism libc-time tls
157-
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX
153+
TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread time fs
154+
TEST_TARGET=i686-unknown-freebsd run_tests_minimal $BASIC $UNIX threadname pthread time fs
155+
TEST_TARGET=x86_64-unknown-illumos run_tests_minimal $BASIC $UNIX thread sync available-parallelism time tls
156+
TEST_TARGET=x86_64-pc-solaris run_tests_minimal $BASIC $UNIX thread sync available-parallelism time tls
157+
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX pthread --skip threadname --skip pthread_cond_timedwait
158158
TEST_TARGET=wasm32-wasip2 run_tests_minimal empty_main wasm heap_alloc libc-mem
159159
TEST_TARGET=wasm32-unknown-unknown run_tests_minimal empty_main wasm
160160
TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std

Diff for: src/tools/miri/src/shims/unix/sync.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::*;
1111
#[inline]
1212
fn mutexattr_kind_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
1313
Ok(match &*ecx.tcx.sess.target.os {
14-
"linux" | "illumos" | "solaris" | "macos" | "freebsd" => 0,
14+
"linux" | "illumos" | "solaris" | "macos" | "freebsd" | "android" => 0,
1515
os => throw_unsup_format!("`pthread_mutexattr` is not supported on {os}"),
1616
})
1717
}
@@ -76,7 +76,7 @@ fn mutex_id_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
7676
// When adding a new OS, make sure we also support all its static initializers in
7777
// `mutex_kind_from_static_initializer`!
7878
let offset = match &*ecx.tcx.sess.target.os {
79-
"linux" | "illumos" | "solaris" | "freebsd" => 0,
79+
"linux" | "illumos" | "solaris" | "freebsd" | "android" => 0,
8080
// macOS stores a signature in the first bytes, so we have to move to offset 4.
8181
"macos" => 4,
8282
os => throw_unsup_format!("`pthread_mutex` is not supported on {os}"),
@@ -105,7 +105,7 @@ fn mutex_id_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
105105
check_static_initializer("PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP");
106106
check_static_initializer("PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP");
107107
}
108-
"illumos" | "solaris" | "macos" | "freebsd" => {
108+
"illumos" | "solaris" | "macos" | "freebsd" | "android" => {
109109
// No non-standard initializers.
110110
}
111111
os => throw_unsup_format!("`pthread_mutex` is not supported on {os}"),
@@ -216,7 +216,7 @@ pub struct AdditionalRwLockData {
216216

217217
fn rwlock_id_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
218218
let offset = match &*ecx.tcx.sess.target.os {
219-
"linux" | "illumos" | "solaris" | "freebsd" => 0,
219+
"linux" | "illumos" | "solaris" | "freebsd" | "android" => 0,
220220
// macOS stores a signature in the first bytes, so we have to move to offset 4.
221221
"macos" => 4,
222222
os => throw_unsup_format!("`pthread_rwlock` is not supported on {os}"),
@@ -269,7 +269,7 @@ fn rwlock_get_id<'tcx>(
269269
#[inline]
270270
fn condattr_clock_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
271271
Ok(match &*ecx.tcx.sess.target.os {
272-
"linux" | "illumos" | "solaris" | "freebsd" => 0,
272+
"linux" | "illumos" | "solaris" | "freebsd" | "android" => 0,
273273
// macOS does not have a clock attribute.
274274
os => throw_unsup_format!("`pthread_condattr` clock field is not supported on {os}"),
275275
})
@@ -321,7 +321,7 @@ fn condattr_set_clock_id<'tcx>(
321321

322322
fn cond_id_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
323323
let offset = match &*ecx.tcx.sess.target.os {
324-
"linux" | "illumos" | "solaris" | "freebsd" => 0,
324+
"linux" | "illumos" | "solaris" | "freebsd" | "android" => 0,
325325
// macOS stores a signature in the first bytes, so we have to move to offset 4.
326326
"macos" => 4,
327327
os => throw_unsup_format!("`pthread_cond` is not supported on {os}"),

Diff for: src/tools/miri/test_dependencies/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ dependencies = [
172172
name = "miri-test-deps"
173173
version = "0.1.0"
174174
dependencies = [
175+
"cfg-if",
175176
"getrandom 0.1.16",
176177
"getrandom 0.2.15",
177178
"libc",

Diff for: src/tools/miri/test_dependencies/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edition = "2021"
1111
# all dependencies (and their transitive ones) listed here can be used in `tests/`.
1212
libc = "0.2"
1313
num_cpus = "1.10.1"
14+
cfg-if = "1"
1415

1516
getrandom_01 = { package = "getrandom", version = "0.1" }
1617
getrandom_02 = { package = "getrandom", version = "0.2", features = ["js"] }

Diff for: src/tools/miri/tests/pass-dep/libc/pthread-threadname.rs

+37-18
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,42 @@ fn main() {
1010
.collect::<String>();
1111

1212
fn set_thread_name(name: &CStr) -> i32 {
13-
#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))]
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()) };
13+
cfg_if::cfg_if! {
14+
if #[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))] {
15+
unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) }
16+
} else if #[cfg(target_os = "freebsd")] {
17+
// pthread_set_name_np does not return anything
18+
unsafe { libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast()) };
19+
0
20+
} else if #[cfg(target_os = "macos")] {
21+
unsafe { libc::pthread_setname_np(name.as_ptr().cast()) }
22+
} else {
23+
compile_error!("set_thread_name not supported for this OS")
24+
}
25+
}
26+
}
27+
28+
fn get_thread_name(name: &mut [u8]) -> i32 {
29+
cfg_if::cfg_if! {
30+
if #[cfg(any(
31+
target_os = "linux",
32+
target_os = "illumos",
33+
target_os = "solaris",
34+
target_os = "macos"
35+
))] {
36+
unsafe {
37+
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
38+
}
39+
} else if #[cfg(target_os = "freebsd")] {
40+
// pthread_get_name_np does not return anything
41+
unsafe {
42+
libc::pthread_get_name_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
43+
};
44+
0
45+
} else {
46+
compile_error!("get_thread_name not supported for this OS")
47+
}
48+
}
2349
}
2450

2551
let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
@@ -28,14 +54,7 @@ fn main() {
2854

2955
// But the system is limited -- make sure we successfully set a truncation.
3056
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-
};
57+
assert_eq!(get_thread_name(&mut buf), 0);
3958
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
4059
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
4160
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));

Diff for: src/tools/miri/tests/pass/shims/time-with-isolation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ fn test_block_for_one_second() {
4141
/// Ensures that we get the same behavior across all targets.
4242
fn test_deterministic() {
4343
let begin = Instant::now();
44-
for _ in 0..100_000 {}
44+
for _ in 0..10_000 {}
4545
let time = begin.elapsed();
46-
println!("The loop took around {}s", time.as_secs());
46+
println!("The loop took around {}ms", time.as_millis());
4747
println!("(It's fine for this number to change when you `--bless` this test.)")
4848
}
4949

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
The loop took around 12s
1+
The loop took around 1250ms
22
(It's fine for this number to change when you `--bless` this test.)

0 commit comments

Comments
 (0)