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

View file

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