Skip to content

Commit

Permalink
better error when calling pthread shims on unsupported unixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 12, 2023
1 parent e0fef82 commit 195776b
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/shims/unix/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_mutexattr_init` is not supported on {}",
this.tcx.sess.target.os
);
}

let default_kind = this.eval_libc_i32("PTHREAD_MUTEX_DEFAULT");
mutexattr_set_kind(this, attr_op, default_kind)?;

Expand Down Expand Up @@ -359,6 +366,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_mutex_init` is not supported on {}",
this.tcx.sess.target.os
);
}

let attr = this.read_pointer(attr_op)?;
let kind = if this.ptr_is_null(attr)? {
this.eval_libc_i32("PTHREAD_MUTEX_DEFAULT")
Expand Down Expand Up @@ -513,6 +527,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_rdlock` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;
let active_thread = this.get_active_thread();

Expand All @@ -531,6 +552,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_tryrdlock` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;
let active_thread = this.get_active_thread();

Expand All @@ -548,6 +576,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_wrlock` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;
let active_thread = this.get_active_thread();

Expand Down Expand Up @@ -578,6 +613,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_trywrlock` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;
let active_thread = this.get_active_thread();

Expand All @@ -595,6 +637,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_unlock` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;
let active_thread = this.get_active_thread();

Expand All @@ -614,6 +663,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_rwlock_destroy` is not supported on {}",
this.tcx.sess.target.os
);
}

let id = rwlock_get_id(this, rwlock_op)?;

if this.rwlock_is_locked(id) {
Expand All @@ -638,6 +694,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_condattr_init` is not supported on {}",
this.tcx.sess.target.os
);
}

// The default value of the clock attribute shall refer to the system
// clock.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_condattr_setclock.html
Expand Down Expand Up @@ -704,6 +767,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
) -> InterpResult<'tcx, i32> {
let this = self.eval_context_mut();

if !matches!(&*this.tcx.sess.target.os, "linux" | "macos") {
throw_unsup_format!(
"`pthread_cond_init` is not supported on {}",
this.tcx.sess.target.os
);
}

let attr = this.read_pointer(attr_op)?;
let clock_id = if this.ptr_is_null(attr)? {
this.eval_libc_i32("CLOCK_REALTIME")
Expand Down

0 comments on commit 195776b

Please sign in to comment.