diff --git a/include/mimalloc-override.h b/include/mimalloc-override.h index c63b0b91a..893489076 100644 --- a/include/mimalloc-override.h +++ b/include/mimalloc-override.h @@ -49,6 +49,9 @@ not accidentally mix pointers from different allocators). #define pvalloc(n) mi_pvalloc(n) #define reallocarray(p,s,n) mi_reallocarray(p,s,n) #define reallocarr(p,s,n) mi_reallocarr(p,s,n) +#define malloc_conceal(n) mi_malloc_conceal(n) +#define calloc_conceal(n,c) mi_calloc_conceal(n,c) +#define freezero(p,n) mi_freezero(p,n) #define memalign(a,n) mi_memalign(a,n) #define aligned_alloc(a,n) mi_aligned_alloc(a,n) #define posix_memalign(p,a,n) mi_posix_memalign(p,a,n) diff --git a/include/mimalloc.h b/include/mimalloc.h index e7cbec11a..ac9791860 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -358,6 +358,9 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_aligned_alloc(size_t mi_decl_nodiscard mi_decl_export void* mi_reallocarray(void* p, size_t count, size_t size) mi_attr_noexcept mi_attr_alloc_size2(2,3); mi_decl_nodiscard mi_decl_export int mi_reallocarr(void* p, size_t count, size_t size) mi_attr_noexcept; +mi_decl_nodiscard mi_decl_export void* mi_malloc_conceal(size_t size) mi_attr_noexcept; +mi_decl_nodiscard mi_decl_export void* mi_calloc_conceal(size_t count, size_t size) mi_attr_noexcept; +mi_decl_export void mi_freezero(void* p, size_t size) mi_attr_noexcept; mi_decl_nodiscard mi_decl_export void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept; mi_decl_nodiscard mi_decl_export void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept; diff --git a/src/alloc-override.c b/src/alloc-override.c index 6bbe4aac7..3e4d1f896 100644 --- a/src/alloc-override.c +++ b/src/alloc-override.c @@ -248,6 +248,9 @@ void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); } void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); } void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); } +void* malloc_conceal(size_t size) { return mi_malloc_conceal(size); } +void* calloc_conceal(size_t count, size_t size) { return mi_calloc_conceal(count, size); } +void freezero(void* p, size_t size) { mi_freezero(p, size); } #if defined(__GLIBC__) && defined(__linux__) // forward __libc interface (needed for glibc-based Linux distributions) diff --git a/src/alloc.c b/src/alloc.c index ca32cabab..328c1117d 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -119,6 +119,49 @@ extern inline mi_decl_restrict void* mi_malloc(size_t size) mi_attr_noexcept { return mi_heap_malloc(mi_get_default_heap(), size); } +mi_decl_restrict void* mi_malloc_conceal(size_t size) mi_attr_noexcept { + void* p; + p = mi_heap_malloc(mi_get_default_heap(), size); + if (mi_likely(p != NULL)) { +#if defined(MADV_DONTDUMP) + madvise(p, size, MADV_DONTDUMP); +#elif defined(MADV_NOCORE) + madvise(p, size, MADV_NOCORE); +#endif + } + return p; +} + +mi_decl_restrict void* mi_calloc_conceal(size_t count, size_t size) mi_attr_noexcept { + void* p; + p = mi_heap_calloc(mi_get_default_heap(),count,size); + if (mi_likely(p != NULL)) { +#if defined(MADV_DONTDUMP) + madvise(p, size, MADV_DONTDUMP); +#elif defined(MADV_NOCORE) + madvise(p, size, MADV_NOCORE); +#endif + } + return p; +} + +void mi_freezero(void* p, size_t size) mi_attr_noexcept { + mi_free(p); +#if (MI_DEBUG==0) + if (size > 0) { +#if defined(_MSC_VER) + SecureZeroMemory(p, size); +#else + // reusing memset return value and using memory fence + // so memset call is generated regardless of the optimisation level + p = memset(p, 0, size); + __asm__ volatile("" :: "r"(p) : "memory"); +#endif + } +#else + MI_UNUSED(size); +#endif +} void _mi_block_zero_init(const mi_page_t* page, void* p, size_t size) { // note: we need to initialize the whole usable block size to zero, not just the requested size, diff --git a/test/test-api.c b/test/test-api.c index f057799a5..a12e88af3 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -98,6 +98,12 @@ int main(void) { CHECK_BODY("calloc0",{ result = (mi_usable_size(mi_calloc(0,1000)) <= 16); }); + CHECK_BODY("malloc_conceal",{ + char* p = (char *)mi_malloc(24); + p[0] = 1; + mi_freezero(p, 24); + result = p[0] == 0; + }); // --------------------------------------------------- // Extended