Bug 1400754 - stylo: crash on Win64 Asan build. r=manishearth, dmajor.

* adds a hashglobe::alloc::realloc, as that was previously not implemented,
  copying and simplifying from liballoc_system.

* routes malloc and realloc calls through hashglobe::alloc::, instead of
  doing it via direct 'extern "C"' calls.
This commit is contained in:
Julian Seward 2017-10-02 20:53:23 +02:00
parent e13f5a656a
commit 419642a2bd
3 changed files with 22 additions and 12 deletions

View file

@ -6,16 +6,12 @@ extern crate hashglobe;
extern crate smallvec;
use hashglobe::FailedAllocationError;
#[cfg(feature = "known_system_malloc")]
use hashglobe::alloc;
use smallvec::Array;
use smallvec::SmallVec;
use std::vec::Vec;
#[cfg(feature = "known_system_malloc")]
extern "C" {
fn realloc(ptr: *mut u8, bytes: usize) -> *mut u8;
fn malloc(bytes: usize) -> *mut u8;
}
pub trait FallibleVec<T> {
/// Append |val| to the end of |vec|. Returns Ok(()) on success,
/// Err(reason) if it fails, with |reason| describing the failure.
@ -67,9 +63,9 @@ fn try_double_vec<T>(vec: &mut Vec<T>) -> Result<(), FailedAllocationError> {
let new_ptr = unsafe {
if old_cap == 0 {
malloc(new_size_bytes)
alloc::alloc(new_size_bytes, 0)
} else {
realloc(old_ptr as *mut u8, new_size_bytes)
alloc::realloc(old_ptr as *mut u8, new_size_bytes)
}
};
@ -146,12 +142,12 @@ where
// There's an old block to free, and, presumably, old contents to
// copy. realloc takes care of both aspects.
unsafe {
new_ptr = realloc(old_ptr as *mut u8, new_size_bytes);
new_ptr = alloc::realloc(old_ptr as *mut u8, new_size_bytes);
}
} else {
// There's no old block to free. There may be old contents to copy.
unsafe {
new_ptr = malloc(new_size_bytes);
new_ptr = alloc::alloc(new_size_bytes, 0);
if !new_ptr.is_null() && old_size_bytes > 0 {
copy_nonoverlapping(old_ptr as *const u8,
new_ptr as *mut u8, old_size_bytes);

View file

@ -22,7 +22,7 @@ const MIN_ALIGN: usize = 8;
target_arch = "sparc64")))]
const MIN_ALIGN: usize = 16;
pub use self::platform::{alloc, dealloc};
pub use self::platform::{alloc, dealloc, realloc};
#[cfg(any(unix, target_os = "redox"))]
mod platform {
@ -47,6 +47,11 @@ mod platform {
libc::free(ptr as *mut libc::c_void)
}
#[inline]
pub unsafe fn realloc(ptr: *mut u8, new_size: usize) -> *mut u8 {
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
}
#[cfg(any(target_os = "android", target_os = "redox"))]
#[inline]
unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
@ -98,6 +103,7 @@ mod platform {
extern "system" {
fn GetProcessHeap() -> HANDLE;
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
fn GetLastError() -> DWORD;
}
@ -149,4 +155,12 @@ mod platform {
GetLastError());
}
}
#[inline]
pub unsafe fn realloc(ptr: *mut u8, new_size: usize) -> *mut u8 {
HeapReAlloc(GetProcessHeap(),
0,
ptr as LPVOID,
new_size) as *mut u8
}
}

View file

@ -10,7 +10,7 @@
extern crate heapsize;
mod alloc;
pub mod alloc;
pub mod hash_map;
pub mod hash_set;
pub mod protected;