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(); .as_ptr();
code.publish(); 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(match call_wasm(func) {
Ok(()) => { Ok(()) => {

View File

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

View File

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