Avoid creating slices with null pointers.

This commit is contained in:
Dan Gohman
2019-01-03 12:04:19 -08:00
parent 57e183f5f8
commit 701b1998e9
4 changed files with 8 additions and 5 deletions

View File

@@ -56,6 +56,7 @@ impl MemoryStyle {
if maximum <= tunables.static_memory_bound {
// A heap with a declared maximum can be immovable, so make
// it static.
assert!(tunables.static_memory_bound >= memory.minimum);
return (
MemoryStyle::Static {
bound: tunables.static_memory_bound,

View File

@@ -70,7 +70,7 @@ impl CodeMemory {
self.position = 0;
for m in &mut self.mmaps[self.published..] {
if !m.as_ptr().is_null() {
if m.len() != 0 {
unsafe {
region::protect(m.as_mut_ptr(), m.len(), region::Protection::ReadExecute)
}

View File

@@ -121,7 +121,6 @@ impl LinearMemory {
if new_bytes > self.mmap.len() - self.offset_guard_size {
// If we have no maximum, this is a "dynamic" heap, and it's allowed to move.
assert!(self.maximum.is_none());
let guard_bytes = self.offset_guard_size;
let request_bytes = new_bytes.checked_add(guard_bytes)?;

View File

@@ -24,8 +24,11 @@ pub struct Mmap {
impl Mmap {
/// Construct a new empty instance of `Mmap`.
pub fn new() -> Self {
// Rust's slices require non-null pointers, even when empty. `Vec`
// contains code to create a non-null dangling pointer value when
// constructed empty, so we reuse that here.
Self {
ptr: ptr::null_mut(),
ptr: Vec::new().as_mut_ptr(),
len: 0,
}
}
@@ -119,7 +122,7 @@ impl Mmap {
impl Drop for Mmap {
#[cfg(not(target_os = "windows"))]
fn drop(&mut self) {
if !self.ptr.is_null() {
if self.len != 0 {
let r = unsafe { libc::munmap(self.ptr as *mut libc::c_void, self.len) };
assert_eq!(r, 0, "munmap failed: {}", errno::errno());
}
@@ -127,7 +130,7 @@ impl Drop for Mmap {
#[cfg(target_os = "windows")]
fn drop(&mut self) {
if !self.ptr.is_null() {
if self.len != 0 {
use winapi::um::memoryapi::VirtualFree;
use winapi::um::winnt::MEM_RELEASE;
let r = unsafe { VirtualFree(self.ptr as *mut libc::c_void, self.len, MEM_RELEASE) };