diff --git a/Cargo.lock b/Cargo.lock index 2551a2e8ee..f8acaf0921 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -633,7 +633,6 @@ dependencies = [ "cranelift-frontend", "cranelift-module", "cranelift-native", - "errno", "libc", "log", "memmap2", diff --git a/cranelift/jit/Cargo.toml b/cranelift/jit/Cargo.toml index 98c8f8e524..5917edbc9d 100644 --- a/cranelift/jit/Cargo.toml +++ b/cranelift/jit/Cargo.toml @@ -17,7 +17,6 @@ cranelift-entity = { path = "../entity", version = "0.73.0" } anyhow = "1.0" region = "2.2.0" libc = { version = "0.2.42" } -errno = "0.2.4" target-lexicon = "0.12" memmap2 = { version = "0.2.1", optional = true } log = { version = "0.4.6", default-features = false } diff --git a/cranelift/jit/src/memory.rs b/cranelift/jit/src/memory.rs index b85d231185..afd1a9e053 100644 --- a/cranelift/jit/src/memory.rs +++ b/cranelift/jit/src/memory.rs @@ -1,6 +1,3 @@ -#[cfg(not(feature = "selinux-fix"))] -use errno; - #[cfg(not(any(feature = "selinux-fix", windows)))] use libc; @@ -9,6 +6,7 @@ use memmap2::MmapMut; use region; use std::convert::TryFrom; +use std::io; use std::mem; use std::ptr; @@ -41,27 +39,22 @@ impl PtrLen { /// Create a new `PtrLen` pointing to at least `size` bytes of memory, /// suitably sized and aligned for memory protection. #[cfg(all(not(target_os = "windows"), feature = "selinux-fix"))] - fn with_size(size: usize) -> Result { + fn with_size(size: usize) -> io::Result { let page_size = region::page::size(); let alloc_size = round_up_to_page_size(size, page_size); - let map = MmapMut::map_anon(alloc_size); - - match map { - Ok(mut map) => { - // The order here is important; we assign the pointer first to get - // around compile time borrow errors. - Ok(Self { - ptr: map.as_mut_ptr(), - map: Some(map), - len: alloc_size, - }) - } - Err(e) => Err(e.to_string()), - } + MmapMut::map_anon(alloc_size).map(|mut mmap| { + // The order here is important; we assign the pointer first to get + // around compile time borrow errors. + Ok(Self { + ptr: mmap.as_mut_ptr(), + map: Some(mmap), + len: alloc_size, + }) + }) } #[cfg(all(not(target_os = "windows"), not(feature = "selinux-fix")))] - fn with_size(size: usize) -> Result { + fn with_size(size: usize) -> io::Result { let mut ptr = ptr::null_mut(); let page_size = region::page::size(); let alloc_size = round_up_to_page_size(size, page_size); @@ -74,13 +67,13 @@ impl PtrLen { len: alloc_size, }) } else { - Err(errno::Errno(err).to_string()) + Err(io::Error::from_raw_os_error(err)) } } } #[cfg(target_os = "windows")] - fn with_size(size: usize) -> Result { + fn with_size(size: usize) -> io::Result { use winapi::um::memoryapi::VirtualAlloc; use winapi::um::winnt::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE}; @@ -101,7 +94,7 @@ impl PtrLen { len: round_up_to_page_size(size, page_size), }) } else { - Err(errno::errno().to_string()) + Err(io::Error::last_os_error()) } } } @@ -149,8 +142,7 @@ impl Memory { self.position = 0; } - /// TODO: Use a proper error type. - pub(crate) fn allocate(&mut self, size: usize, align: u64) -> Result<*mut u8, String> { + pub(crate) fn allocate(&mut self, size: usize, align: u64) -> io::Result<*mut u8> { let align = usize::try_from(align).expect("alignment too big"); if self.position % align != 0 { self.position += align - self.position % align;