Reorganize unsafe code.

This commit is contained in:
Dan Gohman
2018-12-08 15:28:33 -05:00
parent 7dcca6be5b
commit 10bb311aea
4 changed files with 20 additions and 23 deletions

View File

@@ -207,7 +207,7 @@ fn call_through_wrapper(
.as_ptr();
code.publish();
let func = unsafe { mem::transmute::<_, fn()>(exec_code_buf) };
let func: fn() = unsafe { mem::transmute(exec_code_buf) };
Ok(match call_wasm(func) {
Ok(()) => {

View File

@@ -110,8 +110,8 @@ impl LinearMemory {
guard_bytes,
region::Protection::None,
)
.expect("unable to make memory inaccessible");
}
.expect("unable to make memory inaccessible");
let copy_len = self.mmap.len() - self.offset_guard_size;
new_mmap.as_mut_slice()[..copy_len].copy_from_slice(&self.mmap.as_slice()[..copy_len]);

View File

@@ -4,7 +4,6 @@
use errno;
use libc;
use region;
use std::mem;
use std::ptr;
use std::slice;
use std::string::String;
@@ -36,23 +35,23 @@ impl Mmap {
pub fn with_size(size: usize) -> Result<Self, String> {
let page_size = region::page::size();
let alloc_size = round_up_to_page_size(size, page_size);
unsafe {
let ptr = libc::mmap(
let ptr = unsafe {
libc::mmap(
ptr::null_mut(),
alloc_size,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_PRIVATE | libc::MAP_ANON,
-1,
0,
);
if mem::transmute::<_, isize>(ptr) != -1isize {
)
};
if ptr as isize == -1isize {
Err(errno::errno().to_string())
} else {
Ok(Self {
ptr: ptr as *mut u8,
len: alloc_size,
})
} else {
Err(errno::errno().to_string())
}
}
}

View File

@@ -266,16 +266,14 @@ impl VMGlobal {
/// Construct a `VMGlobalDefinition` variant of `VMGlobal`.
pub fn definition(global: &Global) -> Self {
let mut result = VMGlobalDefinition { storage: [0; 8] };
unsafe {
match global.initializer {
GlobalInit::I32Const(x) => *result.as_i32() = x,
GlobalInit::I64Const(x) => *result.as_i64() = x,
GlobalInit::F32Const(x) => *result.as_f32_bits() = x,
GlobalInit::F64Const(x) => *result.as_f64_bits() = x,
GlobalInit::I32Const(x) => *unsafe { result.as_i32() } = x,
GlobalInit::I64Const(x) => *unsafe { result.as_i64() } = x,
GlobalInit::F32Const(x) => *unsafe { result.as_f32_bits() } = x,
GlobalInit::F64Const(x) => *unsafe { result.as_f64_bits() } = x,
GlobalInit::GetGlobal(_x) => unimplemented!("globals init with get_global"),
GlobalInit::Import => panic!("attempting to initialize imported global"),
}
}
Self { definition: result }
}