Skip to content

Commit bfe030d

Browse files
committed
Auto merge of #1362 - vakaras:add-sync-primitives-cr1, r=RalfJung
Add sync primitives This is a follow up PR for #1284 that adds support for the missing synchronization primitives. Sorry for flooding with PRs, but my internship is coming to an end and I need to get things out. Fixes #1419
2 parents 726373f + 34ddd77 commit bfe030d

17 files changed

+1505
-370
lines changed

src/eval.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ pub fn eval_main<'tcx>(tcx: TyCtxt<'tcx>, main_id: DefId, config: MiriConfig) ->
210210
SchedulingAction::ExecuteStep => {
211211
assert!(ecx.step()?, "a terminated thread was scheduled for execution");
212212
}
213+
SchedulingAction::ExecuteTimeoutCallback => {
214+
assert!(ecx.machine.communicate,
215+
"scheduler callbacks require disabled isolation, but the code \
216+
that created the callback did not check it");
217+
ecx.run_timeout_callback()?;
218+
}
213219
SchedulingAction::ExecuteDtors => {
214220
// This will either enable the thread again (so we go back
215221
// to `ExecuteStep`), or determine that this thread is done

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod operator;
3131
mod range_map;
3232
mod shims;
3333
mod stacked_borrows;
34+
mod sync;
3435
mod thread;
3536

3637
// Make all those symbols available in the same place as our own.
@@ -45,7 +46,7 @@ pub use crate::shims::fs::{DirHandler, EvalContextExt as FileEvalContextExt, Fil
4546
pub use crate::shims::intrinsics::EvalContextExt as IntrinsicsEvalContextExt;
4647
pub use crate::shims::os_str::EvalContextExt as OsStrEvalContextExt;
4748
pub use crate::shims::panic::{CatchUnwindData, EvalContextExt as PanicEvalContextExt};
48-
pub use crate::shims::sync::{EvalContextExt as SyncEvalContextExt};
49+
pub use crate::shims::sync::{EvalContextExt as SyncShimsEvalContextExt};
4950
pub use crate::shims::thread::EvalContextExt as ThreadShimsEvalContextExt;
5051
pub use crate::shims::time::EvalContextExt as TimeEvalContextExt;
5152
pub use crate::shims::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
@@ -70,6 +71,9 @@ pub use crate::stacked_borrows::{
7071
pub use crate::thread::{
7172
EvalContextExt as ThreadsEvalContextExt, SchedulingAction, ThreadId, ThreadManager, ThreadState,
7273
};
74+
pub use crate::sync::{
75+
EvalContextExt as SyncEvalContextExt, CondvarId, MutexId, RwLockId
76+
};
7377

7478
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
7579
/// set per default, for maximal validation power.

src/shims/foreign_items/posix.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,55 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
330330
let result = this.pthread_rwlock_destroy(rwlock)?;
331331
this.write_scalar(Scalar::from_i32(result), dest)?;
332332
}
333+
"pthread_condattr_init" => {
334+
let &[attr] = check_arg_count(args)?;
335+
let result = this.pthread_condattr_init(attr)?;
336+
this.write_scalar(Scalar::from_i32(result), dest)?;
337+
}
338+
"pthread_condattr_setclock" => {
339+
let &[attr, clock_id] = check_arg_count(args)?;
340+
let result = this.pthread_condattr_setclock(attr, clock_id)?;
341+
this.write_scalar(Scalar::from_i32(result), dest)?;
342+
}
343+
"pthread_condattr_getclock" => {
344+
let &[attr, clock_id] = check_arg_count(args)?;
345+
let result = this.pthread_condattr_getclock(attr, clock_id)?;
346+
this.write_scalar(Scalar::from_i32(result), dest)?;
347+
}
348+
"pthread_condattr_destroy" => {
349+
let &[attr] = check_arg_count(args)?;
350+
let result = this.pthread_condattr_destroy(attr)?;
351+
this.write_scalar(Scalar::from_i32(result), dest)?;
352+
}
353+
"pthread_cond_init" => {
354+
let &[cond, attr] = check_arg_count(args)?;
355+
let result = this.pthread_cond_init(cond, attr)?;
356+
this.write_scalar(Scalar::from_i32(result), dest)?;
357+
}
358+
"pthread_cond_signal" => {
359+
let &[cond] = check_arg_count(args)?;
360+
let result = this.pthread_cond_signal(cond)?;
361+
this.write_scalar(Scalar::from_i32(result), dest)?;
362+
}
363+
"pthread_cond_broadcast" => {
364+
let &[cond] = check_arg_count(args)?;
365+
let result = this.pthread_cond_broadcast(cond)?;
366+
this.write_scalar(Scalar::from_i32(result), dest)?;
367+
}
368+
"pthread_cond_wait" => {
369+
let &[cond, mutex] = check_arg_count(args)?;
370+
let result = this.pthread_cond_wait(cond, mutex)?;
371+
this.write_scalar(Scalar::from_i32(result), dest)?;
372+
}
373+
"pthread_cond_timedwait" => {
374+
let &[cond, mutex, abstime] = check_arg_count(args)?;
375+
this.pthread_cond_timedwait(cond, mutex, abstime, dest)?;
376+
}
377+
"pthread_cond_destroy" => {
378+
let &[cond] = check_arg_count(args)?;
379+
let result = this.pthread_cond_destroy(cond)?;
380+
this.write_scalar(Scalar::from_i32(result), dest)?;
381+
}
333382

334383
// Threading
335384
"pthread_create" => {
@@ -391,16 +440,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
391440

392441
| "pthread_attr_init"
393442
| "pthread_attr_destroy"
394-
| "pthread_condattr_init"
395-
| "pthread_condattr_destroy"
396-
| "pthread_cond_destroy"
397443
if this.frame().instance.to_string().starts_with("std::sys::unix::") => {
398444
let &[_] = check_arg_count(args)?;
399445
this.write_null(dest)?;
400446
}
401-
| "pthread_cond_init"
402447
| "pthread_attr_setstacksize"
403-
| "pthread_condattr_setclock"
404448
if this.frame().instance.to_string().starts_with("std::sys::unix::") => {
405449
let &[_, _] = check_arg_count(args)?;
406450
this.write_null(dest)?;

0 commit comments

Comments
 (0)