Skip to content

Commit e4d0ae9

Browse files
committed
add non-portable linux pthread initializers to layout sanity check
1 parent 5fc0865 commit e4d0ae9

File tree

1 file changed

+23
-10
lines changed
  • src/tools/miri/src/shims/unix

1 file changed

+23
-10
lines changed

src/tools/miri/src/shims/unix/sync.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,28 @@ fn mutex_id_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> InterpResult<'tcx, u64> {
9696
// recursive or error checking mutexes. We should also add thme in this sanity check.
9797
static SANITY: AtomicBool = AtomicBool::new(false);
9898
if !SANITY.swap(true, Ordering::Relaxed) {
99-
let static_initializer = ecx.eval_path(&["libc", "PTHREAD_MUTEX_INITIALIZER"]);
100-
let id_field = static_initializer
101-
.offset(Size::from_bytes(offset), ecx.machine.layouts.u32, ecx)
102-
.unwrap();
103-
let id = ecx.read_scalar(&id_field).unwrap().to_u32().unwrap();
104-
assert_eq!(
105-
id, 0,
106-
"PTHREAD_MUTEX_INITIALIZER is incompatible with our pthread_mutex layout: id is not 0"
107-
);
99+
let check_static_initializer = |name| {
100+
let static_initializer = ecx.eval_path(&["libc", name]);
101+
let id_field = static_initializer
102+
.offset(Size::from_bytes(offset), ecx.machine.layouts.u32, ecx)
103+
.unwrap();
104+
let id = ecx.read_scalar(&id_field).unwrap().to_u32().unwrap();
105+
assert_eq!(id, 0, "{name} is incompatible with our pthread_mutex layout: id is not 0");
106+
};
107+
108+
check_static_initializer("PTHREAD_MUTEX_INITIALIZER");
109+
// Check non-standard initializers.
110+
match &*ecx.tcx.sess.target.os {
111+
"linux" => {
112+
check_static_initializer("PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP");
113+
check_static_initializer("PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP");
114+
check_static_initializer("PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP");
115+
}
116+
"illumos" | "solaris" | "macos" => {
117+
// No non-standard initializers.
118+
}
119+
os => throw_unsup_format!("`pthread_mutex` is not supported on {os}"),
120+
}
108121
}
109122

110123
Ok(offset)
@@ -167,7 +180,7 @@ fn kind_from_static_initializer<'tcx>(
167180
mutex.offset(Size::from_bytes(offset), ecx.machine.layouts.i32, ecx)?;
168181
ecx.read_scalar(&kind_place)?.to_i32()?
169182
}
170-
| "illumos" | "solaris" | "macos" => ecx.eval_libc_i32("PTHREAD_MUTEX_DEFAULT"),
183+
"illumos" | "solaris" | "macos" => ecx.eval_libc_i32("PTHREAD_MUTEX_DEFAULT"),
171184
os => throw_unsup_format!("`pthread_mutex` is not supported on {os}"),
172185
};
173186

0 commit comments

Comments
 (0)