mirror of
https://github.com/girlbossceo/hardened_malloc-rs.git
synced 2025-04-29 14:19:25 -04:00
test linker fix
This commit is contained in:
parent
391d6d9e60
commit
bff0910b52
2 changed files with 100 additions and 69 deletions
77
src/bindings.rs
Normal file
77
src/bindings.rs
Normal file
|
@ -0,0 +1,77 @@
|
|||
#![no_std]
|
||||
|
||||
use core::ffi::{c_int, c_void};
|
||||
|
||||
extern crate libc;
|
||||
|
||||
extern "C" {
|
||||
/*
|
||||
TODO: implement this
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#define H_MALLOC_USABLE_SIZE_CONST const
|
||||
#else
|
||||
#define H_MALLOC_USABLE_SIZE_CONST
|
||||
#endif
|
||||
|
||||
for:
|
||||
// glibc extensions
|
||||
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
|
||||
*/
|
||||
|
||||
/* C standard */
|
||||
|
||||
pub fn h_malloc(size: usize) -> *mut c_void;
|
||||
pub fn h_calloc(nmemb: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
|
||||
pub fn h_aligned_malloc(alignment: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_free(ptr: *mut c_void);
|
||||
|
||||
/* POSIX */
|
||||
|
||||
pub fn h_posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
|
||||
|
||||
/* glibc extensions */
|
||||
|
||||
pub fn h_malloc_usable_size(ptr: *const c_void) -> usize;
|
||||
pub fn h_mallopt(param: c_int, value: c_int) -> c_int;
|
||||
pub fn h_malloc_trim(pad: usize) -> c_int;
|
||||
pub fn h_malloc_stats(void: c_void) -> c_void;
|
||||
|
||||
/* obsolete glibc extensions */
|
||||
|
||||
pub fn h_memalign(alignment: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_pvalloc(size: usize) -> *mut c_void;
|
||||
pub fn h_cfree(ptr: *mut c_void) -> c_void;
|
||||
pub fn h_malloc_get_state(void: c_void) -> c_void;
|
||||
pub fn h_malloc_set_state(ptr: *mut c_void) -> c_int;
|
||||
|
||||
/*TODO: implement this see the top:
|
||||
#if defined(__GLIBC__) || defined(__ANDROID__)
|
||||
struct mallinfo h_mallinfo(void);
|
||||
#endif
|
||||
#ifndef __ANDROID__
|
||||
int h_malloc_info(int options, FILE *fp);
|
||||
#endif
|
||||
*/
|
||||
|
||||
/* hardened_malloc extensions */
|
||||
|
||||
/// return an upper bound on object size for any pointer based on malloc metadata
|
||||
pub fn h_malloc_object_size(ptr: *const c_void) -> usize;
|
||||
|
||||
/// similar to malloc_object_size, but avoids locking so the results are much more limited
|
||||
pub fn h_malloc_object_size_fast(ptr: *const c_void) -> usize;
|
||||
|
||||
/// The free function with an extra parameter for passing the size requested at
|
||||
/// allocation time.
|
||||
///
|
||||
/// This offers the same functionality as C++14 sized deallocation and can be
|
||||
/// used to implement it.
|
||||
///
|
||||
/// A performance-oriented allocator would use this as a performance
|
||||
/// enhancement with undefined behavior on a mismatch. Instead, this hardened
|
||||
/// allocator implementation uses it to improve security by checking that the
|
||||
/// passed size matches the allocated size.
|
||||
pub fn h_free_sized(ptr: *mut c_void, expected_size: usize) -> c_void;
|
||||
}
|
92
src/lib.rs
92
src/lib.rs
|
@ -1,77 +1,31 @@
|
|||
#![no_std]
|
||||
|
||||
use core::ffi::{c_int, c_void};
|
||||
mod bindings;
|
||||
pub use bindings::*;
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
use core::ffi::c_void;
|
||||
use ffi::*;
|
||||
|
||||
extern crate libc;
|
||||
pub struct HardenedMalloc;
|
||||
|
||||
extern "C" {
|
||||
/*
|
||||
TODO: implement this
|
||||
unsafe impl GlobalAlloc for HardenedMalloc {
|
||||
#[inline]
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
h_malloc(layout.size()) as *mut u8
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#define H_MALLOC_USABLE_SIZE_CONST const
|
||||
#else
|
||||
#define H_MALLOC_USABLE_SIZE_CONST
|
||||
#endif
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
h_calloc(layout.size(), 1) as *mut u8
|
||||
}
|
||||
|
||||
for:
|
||||
// glibc extensions
|
||||
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
|
||||
*/
|
||||
#[inline]
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||
h_free(ptr as *mut c_void);
|
||||
}
|
||||
|
||||
/* C standard */
|
||||
|
||||
pub fn h_malloc(size: usize) -> *mut c_void;
|
||||
pub fn h_calloc(nmemb: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
|
||||
pub fn h_aligned_malloc(alignment: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_free(ptr: *mut c_void);
|
||||
|
||||
/* POSIX */
|
||||
|
||||
pub fn h_posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
|
||||
|
||||
/* glibc extensions */
|
||||
|
||||
pub fn h_malloc_usable_size(ptr: *const c_void) -> usize;
|
||||
pub fn h_mallopt(param: c_int, value: c_int) -> c_int;
|
||||
pub fn h_malloc_trim(pad: usize) -> c_int;
|
||||
pub fn h_malloc_stats(void: c_void) -> c_void;
|
||||
|
||||
/* obsolete glibc extensions */
|
||||
|
||||
pub fn h_memalign(alignment: usize, size: usize) -> *mut c_void;
|
||||
pub fn h_pvalloc(size: usize) -> *mut c_void;
|
||||
pub fn h_cfree(ptr: *mut c_void) -> c_void;
|
||||
pub fn h_malloc_get_state(void: c_void) -> c_void;
|
||||
pub fn h_malloc_set_state(ptr: *mut c_void) -> c_int;
|
||||
|
||||
/*TODO: implement this see the top:
|
||||
#if defined(__GLIBC__) || defined(__ANDROID__)
|
||||
struct mallinfo h_mallinfo(void);
|
||||
#endif
|
||||
#ifndef __ANDROID__
|
||||
int h_malloc_info(int options, FILE *fp);
|
||||
#endif
|
||||
*/
|
||||
|
||||
/* hardened_malloc extensions */
|
||||
|
||||
/// return an upper bound on object size for any pointer based on malloc metadata
|
||||
pub fn h_malloc_object_size(ptr: *const c_void) -> usize;
|
||||
|
||||
/// similar to malloc_object_size, but avoids locking so the results are much more limited
|
||||
pub fn h_malloc_object_size_fast(ptr: *const c_void) -> usize;
|
||||
|
||||
/// The free function with an extra parameter for passing the size requested at
|
||||
/// allocation time.
|
||||
///
|
||||
/// This offers the same functionality as C++14 sized deallocation and can be
|
||||
/// used to implement it.
|
||||
///
|
||||
/// A performance-oriented allocator would use this as a performance
|
||||
/// enhancement with undefined behavior on a mismatch. Instead, this hardened
|
||||
/// allocator implementation uses it to improve security by checking that the
|
||||
/// passed size matches the allocated size.
|
||||
pub fn h_free_sized(ptr: *mut c_void, expected_size: usize) -> c_void;
|
||||
#[inline]
|
||||
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
|
||||
h_realloc(ptr as *mut c_void, size) as *mut u8
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue