Skip to content

Commit

Permalink
Revert "#![deny(unsafe_op_in_unsafe_fn)] in sys/hermit"
Browse files Browse the repository at this point in the history
This reverts commit 7cae9e8.
  • Loading branch information
maekawatoshiki committed Aug 21, 2020
1 parent 7cae9e8 commit 3a46cca
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 147 deletions.
25 changes: 8 additions & 17 deletions library/std/src/sys/hermit/alloc.rs
@@ -1,5 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
use crate::sys::hermit::abi;
Expand All @@ -8,33 +6,26 @@ use crate::sys::hermit::abi;
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// SAFETY: The safety contract for `malloc` must be upheld by the caller.
unsafe { abi::malloc(layout.size(), layout.align()) }
abi::malloc(layout.size(), layout.align())
}

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
// SAFETY: The safety contract for `malloc` must be upheld by the caller.
// Also, `addr` must be valid for writes of `layout.size() * size_of::<u8>()` bytes.
unsafe {
let addr = abi::malloc(layout.size(), layout.align());

if !addr.is_null() {
ptr::write_bytes(addr, 0x00, layout.size());
}
let addr = abi::malloc(layout.size(), layout.align());

addr
if !addr.is_null() {
ptr::write_bytes(addr, 0x00, layout.size());
}

addr
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// SAFETY: The safety contract for `free` must be upheld by the caller.
unsafe { abi::free(ptr, layout.size(), layout.align()) }
abi::free(ptr, layout.size(), layout.align())
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
// SAFETY: The safety contract for `realloc` must be upheld by the caller.
unsafe { abi::realloc(ptr, layout.size(), layout.align(), new_size) }
abi::realloc(ptr, layout.size(), layout.align(), new_size)
}
}
16 changes: 5 additions & 11 deletions library/std/src/sys/hermit/args.rs
@@ -1,17 +1,15 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::ffi::OsString;
use crate::marker::PhantomData;
use crate::vec;

/// One-time global initialization.
pub unsafe fn init(argc: isize, argv: *const *const u8) {
unsafe { imp::init(argc, argv) }
imp::init(argc, argv)
}

/// One-time global cleanup.
pub unsafe fn cleanup() {
unsafe { imp::cleanup() }
imp::cleanup()
}

/// Returns the command line arguments
Expand Down Expand Up @@ -67,18 +65,14 @@ mod imp {

pub unsafe fn init(argc: isize, argv: *const *const u8) {
let _guard = LOCK.lock();
unsafe {
ARGC = argc;
ARGV = argv;
}
ARGC = argc;
ARGV = argv;
}

pub unsafe fn cleanup() {
let _guard = LOCK.lock();
ARGC = 0;
unsafe {
ARGV = ptr::null();
}
ARGV = ptr::null();
}

pub fn args() -> Args {
Expand Down
34 changes: 10 additions & 24 deletions library/std/src/sys/hermit/condvar.rs
@@ -1,5 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::ffi::c_void;
use crate::ptr;
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
Expand All @@ -25,43 +23,33 @@ impl Condvar {
}

pub unsafe fn init(&mut self) {
unsafe {
let _ = abi::sem_init(&mut self.sem1 as *mut *const c_void, 0);
let _ = abi::sem_init(&mut self.sem2 as *mut *const c_void, 0);
}
let _ = abi::sem_init(&mut self.sem1 as *mut *const c_void, 0);
let _ = abi::sem_init(&mut self.sem2 as *mut *const c_void, 0);
}

pub unsafe fn notify_one(&self) {
if self.counter.load(SeqCst) > 0 {
self.counter.fetch_sub(1, SeqCst);
unsafe {
abi::sem_post(self.sem1);
abi::sem_timedwait(self.sem2, 0);
}
abi::sem_post(self.sem1);
abi::sem_timedwait(self.sem2, 0);
}
}

pub unsafe fn notify_all(&self) {
let counter = self.counter.swap(0, SeqCst);
for _ in 0..counter {
unsafe {
abi::sem_post(self.sem1);
}
abi::sem_post(self.sem1);
}
for _ in 0..counter {
unsafe {
abi::sem_timedwait(self.sem2, 0);
}
abi::sem_timedwait(self.sem2, 0);
}
}

pub unsafe fn wait(&self, mutex: &Mutex) {
self.counter.fetch_add(1, SeqCst);
mutex.unlock();
unsafe {
abi::sem_timedwait(self.sem1, 0);
abi::sem_post(self.sem2);
}
abi::sem_timedwait(self.sem1, 0);
abi::sem_post(self.sem2);
mutex.lock();
}

Expand All @@ -70,9 +58,7 @@ impl Condvar {
}

pub unsafe fn destroy(&self) {
unsafe {
let _ = abi::sem_destroy(self.sem1);
let _ = abi::sem_destroy(self.sem2);
}
let _ = abi::sem_destroy(self.sem1);
let _ = abi::sem_destroy(self.sem2);
}
}
1 change: 0 additions & 1 deletion library/std/src/sys/hermit/fd.rs
@@ -1,4 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![unstable(reason = "not public", issue = "none", feature = "fd")]

use crate::io::{self, ErrorKind, Read};
Expand Down
22 changes: 7 additions & 15 deletions library/std/src/sys/hermit/mod.rs
Expand Up @@ -13,8 +13,6 @@
//! compiling for wasm. That way it's a compile time error for something that's
//! guaranteed to be a runtime error!

#![deny(unsafe_op_in_unsafe_fn)]

use crate::intrinsics;
use crate::os::raw::c_char;

Expand Down Expand Up @@ -64,12 +62,8 @@ pub enum Void {}
pub unsafe fn strlen(start: *const c_char) -> usize {
let mut str = start;

// SAFETY: The safety contract for `*str != 0` must be upheld by the caller.
// `start` must not be null.
unsafe {
while *str != 0 {
str = str.offset(1);
}
while *str != 0 {
str = str.offset(1);
}

(str as usize) - (start as usize)
Expand Down Expand Up @@ -117,15 +111,13 @@ pub unsafe extern "C" fn runtime_entry(
fn main(argc: isize, argv: *const *const c_char) -> i32;
}

unsafe {
// initialize environment
os::init_environment(env as *const *const i8);
// initialize environment
os::init_environment(env as *const *const i8);

let result = main(argc as isize, argv);
let result = main(argc as isize, argv);

run_dtors();
abi::exit(result);
}
run_dtors();
abi::exit(result);
}

pub fn decode_error_kind(errno: i32) -> ErrorKind {
Expand Down
26 changes: 9 additions & 17 deletions library/std/src/sys/hermit/mutex.rs
@@ -1,5 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::ffi::c_void;
use crate::ptr;
use crate::sys::hermit::abi;
Expand All @@ -18,34 +16,28 @@ impl Mutex {

#[inline]
pub unsafe fn init(&mut self) {
unsafe {
let _ = abi::sem_init(&mut self.inner as *mut *const c_void, 1);
}
let _ = abi::sem_init(&mut self.inner as *mut *const c_void, 1);
}

#[inline]
pub unsafe fn lock(&self) {
unsafe {
let _ = abi::sem_timedwait(self.inner, 0);
}
let _ = abi::sem_timedwait(self.inner, 0);
}

#[inline]
pub unsafe fn unlock(&self) {
unsafe {
let _ = abi::sem_post(self.inner);
}
let _ = abi::sem_post(self.inner);
}

#[inline]
pub unsafe fn try_lock(&self) -> bool {
let result = unsafe { abi::sem_trywait(self.inner) };
let result = abi::sem_trywait(self.inner);
result == 0
}

#[inline]
pub unsafe fn destroy(&self) {
let _ = unsafe { abi::sem_destroy(self.inner) };
let _ = abi::sem_destroy(self.inner);
}
}

Expand All @@ -60,12 +52,12 @@ impl ReentrantMutex {

#[inline]
pub unsafe fn init(&self) {
let _ = unsafe { abi::recmutex_init(&self.inner as *const *const c_void as *mut _) };
let _ = abi::recmutex_init(&self.inner as *const *const c_void as *mut _);
}

#[inline]
pub unsafe fn lock(&self) {
let _ = unsafe { abi::recmutex_lock(self.inner) };
let _ = abi::recmutex_lock(self.inner);
}

#[inline]
Expand All @@ -75,11 +67,11 @@ impl ReentrantMutex {

#[inline]
pub unsafe fn unlock(&self) {
let _ = unsafe { abi::recmutex_unlock(self.inner) };
let _ = abi::recmutex_unlock(self.inner);
}

#[inline]
pub unsafe fn destroy(&self) {
let _ = unsafe { abi::recmutex_destroy(self.inner) };
let _ = abi::recmutex_destroy(self.inner);
}
}
2 changes: 0 additions & 2 deletions library/std/src/sys/hermit/os.rs
@@ -1,5 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::collections::HashMap;
use crate::error::Error as StdError;
use crate::ffi::{CStr, OsStr, OsString};
Expand Down
68 changes: 26 additions & 42 deletions library/std/src/sys/hermit/rwlock.rs
@@ -1,5 +1,3 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::cell::UnsafeCell;
use crate::sys::condvar::Condvar;
use crate::sys::mutex::Mutex;
Expand Down Expand Up @@ -34,76 +32,62 @@ impl RWLock {

#[inline]
pub unsafe fn read(&self) {
unsafe {
self.lock.lock();
while !(*self.state.get()).inc_readers() {
self.cond.wait(&self.lock);
}
self.lock.unlock();
self.lock.lock();
while !(*self.state.get()).inc_readers() {
self.cond.wait(&self.lock);
}
self.lock.unlock();
}

#[inline]
pub unsafe fn try_read(&self) -> bool {
unsafe {
self.lock.lock();
let ok = (*self.state.get()).inc_readers();
self.lock.unlock();
}
self.lock.lock();
let ok = (*self.state.get()).inc_readers();
self.lock.unlock();
return ok;
}

#[inline]
pub unsafe fn write(&self) {
unsafe {
self.lock.lock();
while !(*self.state.get()).inc_writers() {
self.cond.wait(&self.lock);
}
self.lock.lock();
while !(*self.state.get()).inc_writers() {
self.cond.wait(&self.lock);
}
self.lock.unlock();
}

#[inline]
pub unsafe fn try_write(&self) -> bool {
unsafe {
self.lock.lock();
let ok = (*self.state.get()).inc_writers();
self.lock.unlock();
}
self.lock.lock();
let ok = (*self.state.get()).inc_writers();
self.lock.unlock();
return ok;
}

#[inline]
pub unsafe fn read_unlock(&self) {
unsafe {
self.lock.lock();
let notify = (*self.state.get()).dec_readers();
self.lock.unlock();
if notify {
// FIXME: should only wake up one of these some of the time
self.cond.notify_all();
}
self.lock.lock();
let notify = (*self.state.get()).dec_readers();
self.lock.unlock();
if notify {
// FIXME: should only wake up one of these some of the time
self.cond.notify_all();
}
}

#[inline]
pub unsafe fn write_unlock(&self) {
unsafe {
self.lock.lock();
(*self.state.get()).dec_writers();
self.lock.unlock();
// FIXME: should only wake up one of these some of the time
self.cond.notify_all();
}
self.lock.lock();
(*self.state.get()).dec_writers();
self.lock.unlock();
// FIXME: should only wake up one of these some of the time
self.cond.notify_all();
}

#[inline]
pub unsafe fn destroy(&self) {
unsafe {
self.lock.destroy();
self.cond.destroy();
}
self.lock.destroy();
self.cond.destroy();
}
}

Expand Down

0 comments on commit 3a46cca

Please sign in to comment.