hardened_malloc_rs/bindings.rs
1use core::ffi::{c_int, c_void};
2
3// ideally we would use c_size_t but it's unstable
4
5#[allow(dead_code)]
6extern "C" {
7 /* C standard */
8 pub fn malloc(size: usize) -> *mut c_void;
9 pub fn calloc(nmemb: usize, size: usize) -> *mut c_void;
10 pub fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void;
11 pub fn aligned_alloc(alignment: usize, size: usize) -> *mut c_void;
12 pub fn free(ptr: *mut c_void);
13
14 /* POSIX */
15 pub fn posix_memalign(memptr: *mut *mut c_void, alignment: usize, size: usize) -> c_int;
16
17 /* glibc extensions */
18 #[cfg(target_os = "android")]
19 pub fn malloc_usable_size(ptr: *const c_void) -> usize;
20 #[cfg(not(target_os = "android"))]
21 pub fn malloc_usable_size(ptr: *mut c_void) -> usize;
22 pub fn mallopt(param: c_int, value: c_int) -> c_int;
23 pub fn malloc_trim(pad: usize) -> c_int;
24 pub fn malloc_stats() -> c_void;
25
26 /* TODO: add support for these
27 #if defined(__GLIBC__) || defined(__ANDROID__)
28 struct mallinfo h_mallinfo(void);
29 #endif
30 #ifndef __ANDROID__
31 int h_malloc_info(int options, FILE *fp);
32 #endif
33 */
34
35 /* obsolete glbc extensions */
36 pub fn memalign(alignment: usize, size: usize) -> *mut c_void;
37 #[cfg(not(target_os = "android"))]
38 pub fn valloc(size: usize) -> *mut c_void;
39 #[cfg(not(target_os = "android"))]
40 pub fn pvalloc(size: usize) -> *mut c_void;
41 pub fn cfree(ptr: *mut c_void) -> c_void;
42 pub fn malloc_get_state() -> *mut c_void;
43 pub fn malloc_set_state(state: *mut c_void) -> c_int;
44
45 /* TODO: add support for these android extensions
46 // Android extensions
47 #ifdef __ANDROID__
48 size_t h_mallinfo_narenas(void);
49 size_t h_mallinfo_nbins(void);
50 struct mallinfo h_mallinfo_arena_info(size_t arena);
51 struct mallinfo h_mallinfo_bin_info(size_t arena, size_t bin);
52 int h_malloc_iterate(uintptr_t base, size_t size, void (*callback)(uintptr_t ptr, size_t size, void *arg),
53 void *arg);
54 void h_malloc_disable(void);
55 void h_malloc_enable(void);
56 void h_malloc_disable_memory_tagging(void);
57 #endif */
58
59 /* hardened_malloc extensions */
60 /// return an upper bound on object size for any pointer based on malloc
61 /// metadata
62 pub fn malloc_object_size(ptr: *const c_void) -> usize;
63
64 /// similar to malloc_object_size, but avoiding locking so the results are
65 /// much more limited
66 pub fn malloc_object_size_fast(ptr: *const c_void) -> usize;
67
68 /// The free function with an extra parameter for passing the size requested
69 /// at allocation time.
70 ///
71 /// This offers the same functionality as C++14 sized deallocation and can
72 /// be used to implement it.
73 ///
74 /// A performance-oriented allocator would use this as a performance
75 /// enhancement with undefined behavior on a mismatch. Instead, this
76 /// hardened allocator implementation uses it to improve security by
77 /// checking that the passed size matches the allocated size.
78 pub fn free_sized(ptr: *mut c_void, expected_size: usize) -> c_void;
79}