Skip to content

Commit

Permalink
to rebase
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jul 19, 2022
1 parent f8dabd6 commit ea5e25c
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 36 deletions.
10 changes: 5 additions & 5 deletions library/std/src/io/error/repr_bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Repr {
let p = Box::into_raw(b).cast::<u8>();
// Should only be possible if an allocator handed out a pointer with
// wrong alignment.
debug_assert_eq!(p.addr() & TAG_MASK, 0);
assert_eq!(p.addr() & TAG_MASK, 0);
// Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
// end of file), and both the start and end of the expression must be
// valid without address space wraparound due to `Box`'s semantics.
Expand All @@ -167,7 +167,7 @@ impl Repr {
let res = Self(unsafe { NonNull::new_unchecked(tagged) }, PhantomData);
// quickly smoke-check we encoded the right thing (This generally will
// only run in libstd's tests, unless the user uses -Zbuild-std)
debug_assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
res
}

Expand All @@ -178,7 +178,7 @@ impl Repr {
let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
// quickly smoke-check we encoded the right thing (This generally will
// only run in libstd's tests, unless the user uses -Zbuild-std)
debug_assert!(
assert!(
matches!(res.data(), ErrorData::Os(c) if c == code),
"repr(os) encoding failed for {code}"
);
Expand All @@ -192,7 +192,7 @@ impl Repr {
let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
// quickly smoke-check we encoded the right thing (This generally will
// only run in libstd's tests, unless the user uses -Zbuild-std)
debug_assert!(
assert!(
matches!(res.data(), ErrorData::Simple(k) if k == kind),
"repr(simple) encoding failed {:?}",
kind,
Expand Down Expand Up @@ -256,7 +256,7 @@ where
TAG_SIMPLE => {
let kind_bits = (bits >> 32) as u32;
let kind = kind_from_prim(kind_bits).unwrap_or_else(|| {
debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
// This means the `ptr` passed in was not valid, which violates
// the unsafe contract of `decode_repr`.
//
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/os/windows/io/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl IntoRawSocket for OwnedSocket {
impl FromRawSocket for OwnedSocket {
#[inline]
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
Self { socket }
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sync/mpsc/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<T> Packet<T> {
}
} else {
wait_token.wait();
debug_assert!(self.state.load(Ordering::SeqCst) != EMPTY);
assert!(self.state.load(Ordering::SeqCst) != EMPTY, "packet state not empity");
}
} else {
// drop the signal token, since we never blocked
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<T> OnceLock<T> {
}
self.initialize(f)?;

debug_assert!(self.is_initialized());
assert!(self.is_initialized(), "bad initialization");

// SAFETY: The inner value has been initialized
Ok(unsafe { self.get_unchecked() })
Expand Down Expand Up @@ -369,15 +369,15 @@ impl<T> OnceLock<T> {
///
/// The value must be initialized
unsafe fn get_unchecked(&self) -> &T {
debug_assert!(self.is_initialized());
assert!(self.is_initialized(), "value not initialized");
(&*self.value.get()).assume_init_ref()
}

/// # Safety
///
/// The value must be initialized
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
debug_assert!(self.is_initialized());
assert!(self.is_initialized(), "value not initialized");
(&mut *self.value.get()).assume_init_mut()
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/itron/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod waiter_queue {
// Zeroness of `Waiter::task` indicates whether the `Waiter` is
// linked to a queue or not. This invariant is important for
// the correctness.
debug_assert_ne!(task, 0);
assert_ne!(task, 0);

Self { task, priority, prev: None, next: None }
}
Expand All @@ -182,8 +182,8 @@ mod waiter_queue {
unsafe {
let waiter = waiter_ptr.as_mut();

debug_assert!(waiter.prev.is_none());
debug_assert!(waiter.next.is_none());
assert!(waiter.prev.is_none());
assert!(waiter.next.is_none());

if let Some(head) = &mut self.head {
// Find the insertion position and insert `waiter`
Expand Down
17 changes: 10 additions & 7 deletions library/std/src/sys/itron/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<T> SpinMutex<T> {
let _guard;
if unsafe { abi::sns_dsp() } == 0 {
let er = unsafe { abi::dis_dsp() };
debug_assert!(er >= 0);
assert!(er >= 0, "unexpected error during abi::dis_dsp(): {:?}", ret);

// Wait until the current processor acquires a lock.
while self.locked.swap(true, Ordering::Acquire) {}
Expand Down Expand Up @@ -95,11 +95,11 @@ impl<T> SpinIdOnceCell<T> {
/// Assign the content without checking if it's already initialized or
/// being initialized.
pub unsafe fn set_unchecked(&self, (id, extra): (abi::ID, T)) {
debug_assert!(self.get().is_none());
assert!(self.get().is_none());

// Assumption: A positive `abi::ID` fits in `usize`.
debug_assert!(id >= 0);
debug_assert!(usize::try_from(id).is_ok());
assert!(id >= 0, "negative `abi::ID` received: {:?}", id);
assert!(usize::try_from(id).is_ok(), "fails to conver `abi::ID` to `usize`: {:?}", id);
let id = id as usize;

unsafe { *self.extra.get() = MaybeUninit::new(extra) };
Expand All @@ -124,7 +124,7 @@ impl<T> SpinIdOnceCell<T> {

self.initialize(f)?;

debug_assert!(self.get().is_some());
assert!(self.get().is_some());

// Safety: The inner value has been initialized
Ok(unsafe { self.get_unchecked() })
Expand All @@ -139,8 +139,11 @@ impl<T> SpinIdOnceCell<T> {
let (initialized_id, initialized_extra) = f()?;

// Assumption: A positive `abi::ID` fits in `usize`.
debug_assert!(initialized_id >= 0);
debug_assert!(usize::try_from(initialized_id).is_ok());
assert!(initialized_id >= 0, "negative `abi::ID`");
assert!(
usize::try_from(initialized_id).is_ok(),
"fails to conver `abi::ID` to `usize`: {:?}"
);
let initialized_id = initialized_id as usize;

// Store the initialized contents. Use the release ordering to
Expand Down
18 changes: 15 additions & 3 deletions library/std/src/sys/itron/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,21 @@ impl Thread {
// Get the current task ID. Panicking here would cause a resource leak,
// so just abort on failure.
let current_task = task::current_task_id_aborting();
debug_assert!(usize::try_from(current_task).is_ok());
debug_assert_ne!(current_task as usize, LIFECYCLE_INIT);
debug_assert_ne!(current_task as usize, LIFECYCLE_DETACHED);
assert!(
usize::try_from(current_task).is_ok(),
"fails to convert `current_task_id` to `usize`: {:?}",
current_task
);
assert_ne!(
current_task as usize, LIFECYCLE_INIT,
"`current_task` is not in a valid state: {:?}",
current_task
);
assert_ne!(
current_task as usize, LIFECYCLE_DETACHED,
"`current_task` is not in a valid state: {:?}",
current_task
);

let current_task = current_task as usize;

Expand Down
8 changes: 6 additions & 2 deletions library/std/src/sys/unix/locks/futex_rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ impl RwLock {

// It's impossible for a reader to be waiting on a read-locked RwLock,
// except if there is also a writer waiting.
debug_assert!(!has_readers_waiting(state) || has_writers_waiting(state));
assert!(
!has_readers_waiting(state) || has_writers_waiting(state),
"invalid read state: {:?}",
state
);

// Wake up a writer if we were the last reader and there's a writer waiting.
if is_unlocked(state) && has_writers_waiting(state) {
Expand Down Expand Up @@ -161,7 +165,7 @@ impl RwLock {
pub unsafe fn write_unlock(&self) {
let state = self.state.fetch_sub(WRITE_LOCKED, Release) - WRITE_LOCKED;

debug_assert!(is_unlocked(state));
assert!(is_unlocked(state), "trying to write while the state is invalid: {:?}", state);

if has_writers_waiting(state) || has_readers_waiting(state) {
self.wake_writer_or_readers(state);
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/locks/pthread_rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl RwLock {
}
#[inline]
pub unsafe fn write_unlock(&self) {
debug_assert_eq!(self.num_readers.load(Ordering::Relaxed), 0);
assert_eq!(self.num_readers.load(Ordering::Relaxed), 0);
assert!(*self.write_locked.get(), "trying to unlock a thread already unlocked");
*self.write_locked.get() = false;
self.raw_unlock();
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Command {
pub fn set_arg_0(&mut self, arg: &OsStr) {
// Set a new arg0
let arg = os2c(arg, &mut self.saw_nul);
debug_assert!(self.argv.0.len() > 1);
assert!(self.argv.0.len() > 1, "invalid number of argument: {:?}", self.argv.0);
self.argv.0[0] = arg.as_ptr();
self.args[0] = arg;
}
Expand Down
14 changes: 9 additions & 5 deletions library/std/src/sys/unix/thread_parker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ const NOTIFIED: usize = 2;

unsafe fn lock(lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_mutex_lock(lock);
debug_assert_eq!(r, 0);
assert_eq!(r, 0, "unexpected error during pthread_mutex_lock: {:?}", r);
}

unsafe fn unlock(lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_mutex_unlock(lock);
debug_assert_eq!(r, 0);
assert_eq!(r, 0, "unexpected error during pthread_mutex_ulock: {:?}", r);
}

unsafe fn notify_one(cond: *mut libc::pthread_cond_t) {
let r = libc::pthread_cond_signal(cond);
debug_assert_eq!(r, 0);
assert_eq!(r, 0, "unexpected error during pthread_cond_signal: {:?}", r);
}

unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t) {
let r = libc::pthread_cond_wait(cond, lock);
debug_assert_eq!(r, 0);
assert_eq!(r, 0, "unexpected error during pthread_cond_wait: {:?}", r);
}

const TIMESPEC_MAX: libc::timespec =
Expand Down Expand Up @@ -83,7 +83,11 @@ unsafe fn wait_timeout(
let timeout =
now.checked_add_duration(&dur).and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
debug_assert!(r == libc::ETIMEDOUT || r == 0);
assert!(
r == libc::ETIMEDOUT || r == 0,
"unexpected error during pthread_cond_timedwait: {:?}",
ret
);
}

pub struct Parker {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/wasi/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Thread {

pub fn yield_now() {
let ret = unsafe { wasi::sched_yield() };
debug_assert_eq!(ret, Ok(()));
assert_eq!(ret, Ok(()), "unexpected error wasi::sched_yield: {:?}", ret);
}

pub fn set_name(_name: &CStr) {
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/wasm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod lock {
// 1, // expected value
// -1, // timeout
// );
// debug_assert!(r == 0 || r == 1);
// assert!(r == 0 || r == 1);
// }
//
// Unfortunately though in doing so we would cause issues for the
Expand Down Expand Up @@ -144,7 +144,7 @@ mod lock {
impl Drop for DropLock {
fn drop(&mut self) {
let r = LOCKED.swap(0, SeqCst);
debug_assert_eq!(r, 1);
assert_eq!(r, 1, "unexpected error during swap: {:?}", r);

// Note that due to the above logic we don't actually need to wake
// anyone up, but if we did it'd likely look something like this:
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/wasm/atomics/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Thread {
let amt = cmp::min(i64::MAX as u128, nanos);
let mut x = 0;
let val = unsafe { wasm32::memory_atomic_wait32(&mut x, 0, amt as i64) };
debug_assert_eq!(val, 2);
assert_eq!(val, 2, "unexpected value during wasm32::memory_atomic_wait32: {:?}", val);
nanos -= amt;
}
}
Expand Down

0 comments on commit ea5e25c

Please sign in to comment.