Skip to content

Commit 1bfc69e

Browse files
committed
address comments
1 parent 3beb3fc commit 1bfc69e

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/shims/unix/sync.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,16 @@ fn condattr_get_clock_id<'tcx>(
286286
.to_i32()
287287
}
288288

289-
fn translate_clock_id<'tcx>(
290-
ecx: &MiriInterpCx<'tcx>,
291-
raw_id: i32,
292-
) -> InterpResult<'tcx, ClockType> {
289+
fn translate_clock_id<'tcx>(ecx: &MiriInterpCx<'tcx>, raw_id: i32) -> InterpResult<'tcx, ClockId> {
293290
// To ensure compatibility with PTHREAD_COND_INITIALIZER on all platforms,
294291
// we can't just compare with CLOCK_REALTIME: on Solarish, PTHREAD_COND_INITIALIZER
295292
// makes the clock 0 but CLOCK_REALTIME is 3.
296293
Ok(if raw_id == ecx.eval_libc_i32("CLOCK_REALTIME") || raw_id == 0 {
297-
ClockType::Realtime
294+
ClockId::Realtime
298295
} else if raw_id == ecx.eval_libc_i32("CLOCK_MONOTONIC") {
299-
ClockType::Monotonic
296+
ClockId::Monotonic
300297
} else {
301-
throw_unsup_format!("unsupported type of clock id: {raw_id}");
298+
throw_unsup_format!("unsupported clock id: {raw_id}");
302299
})
303300
}
304301

@@ -363,7 +360,7 @@ fn cond_clock_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> u64 {
363360
let id = ecx.read_scalar(&id_field).unwrap().to_i32().unwrap();
364361
let id = translate_clock_id(ecx, id).expect("static initializer should be valid");
365362
assert!(
366-
matches!(id, ClockType::Realtime),
363+
matches!(id, ClockId::Realtime),
367364
"PTHREAD_COND_INITIALIZER is incompatible with our pthread_cond layout: clock is not CLOCK_REALTIME"
368365
);
369366
}
@@ -372,7 +369,7 @@ fn cond_clock_offset<'tcx>(ecx: &MiriInterpCx<'tcx>) -> u64 {
372369
}
373370

374371
#[derive(Debug, Clone, Copy)]
375-
enum ClockType {
372+
enum ClockId {
376373
Realtime,
377374
Monotonic,
378375
}
@@ -383,8 +380,8 @@ struct AdditionalCondData {
383380
/// The address of the cond.
384381
address: u64,
385382

386-
/// The clock type of the cond.
387-
clock_type: ClockType,
383+
/// The clock id of the cond.
384+
clock_id: ClockId,
388385
}
389386

390387
fn cond_get_id<'tcx>(
@@ -399,8 +396,8 @@ fn cond_get_id<'tcx>(
399396
} else {
400397
cond_get_clock_id(ecx, cond_ptr)?
401398
};
402-
let clock_type = translate_clock_id(ecx, raw_id)?;
403-
Ok(Some(Box::new(AdditionalCondData { address, clock_type })))
399+
let clock_id = translate_clock_id(ecx, raw_id)?;
400+
Ok(Some(Box::new(AdditionalCondData { address, clock_id })))
404401
})?;
405402

406403
// Check that the mutex has not been moved since last use.
@@ -835,14 +832,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
835832
} else {
836833
condattr_get_clock_id(this, attr_op)?
837834
};
838-
let clock_type = translate_clock_id(this, clock_id)?;
835+
let clock_id = translate_clock_id(this, clock_id)?;
839836

840837
let cond = this.deref_pointer(cond_op)?;
841838
let address = cond.ptr().addr().bytes();
842839
this.condvar_create(
843840
&cond,
844841
cond_id_offset(this)?,
845-
Some(Box::new(AdditionalCondData { address, clock_type })),
842+
Some(Box::new(AdditionalCondData { address, clock_id })),
846843
)?;
847844

848845
Ok(())
@@ -898,10 +895,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
898895
let mutex_id = mutex_get_id(this, mutex_op)?;
899896

900897
// Extract the timeout.
901-
let clock_type = this
898+
let clock_id = this
902899
.condvar_get_data::<AdditionalCondData>(id)
903900
.expect("additional data should always be present for pthreads")
904-
.clock_type;
901+
.clock_id;
905902
let duration = match this
906903
.read_timespec(&this.deref_pointer_as(abstime_op, this.libc_ty_layout("timespec"))?)?
907904
{
@@ -912,13 +909,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
912909
return Ok(());
913910
}
914911
};
915-
let timeout_clock = if matches!(clock_type, ClockType::Realtime) {
916-
this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?;
917-
TimeoutClock::RealTime
918-
} else if let ClockType::Monotonic = clock_type {
919-
TimeoutClock::Monotonic
920-
} else {
921-
throw_unsup_format!("unsupported clock id: {clock_type:?}");
912+
let timeout_clock = match clock_id {
913+
ClockId::Realtime => {
914+
this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?;
915+
TimeoutClock::RealTime
916+
}
917+
ClockId::Monotonic => TimeoutClock::Monotonic,
922918
};
923919

924920
this.condvar_wait(
@@ -934,16 +930,16 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
934930
}
935931

936932
fn pthread_cond_destroy(&mut self, cond_op: &OpTy<'tcx>) -> InterpResult<'tcx, ()> {
933+
//NOTE: Destroying an uninit pthread_cond is UB. Make sure it's not uninit,
934+
// by accessing at least once all of its fields that we use.
935+
937936
let this = self.eval_context_mut();
938937

939938
let id = cond_get_id(this, cond_op)?;
940939
if this.condvar_is_awaited(id) {
941940
throw_ub_format!("destroying an awaited conditional variable");
942941
}
943942

944-
// Destroying an uninit pthread_cond is UB, so check to make sure it's not uninit.
945-
cond_get_id(this, cond_op)?;
946-
947943
// This might lead to false positives, see comment in pthread_mutexattr_destroy
948944
this.write_uninit(&this.deref_pointer_as(cond_op, this.libc_ty_layout("pthread_cond_t"))?)?;
949945
// FIXME: delete interpreter state associated with this condvar.

0 commit comments

Comments
 (0)