Skip to content

Commit

Permalink
adding freezero and quick tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Nov 30, 2021
1 parent 2729c8e commit a797b2a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/override/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ extern "C"
}
return ThreadAlloc::get().alloc<ZeroMem::YesZero, CoreDumpMem::NoDump>(sz);
}

SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(freezero)(void* p, size_t size)
{
if (SNMALLOC_UNLIKELY(p == nullptr))
{
return;
}

size_t sz = bits::min(size, ThreadAlloc::get().alloc_size(p));
/* we are not trying to be fast, here but disallowing to potentially
* optimize away the memset call */
void* (*volatile memset_fn)(void*, int, size_t) = memset;
memset_fn(p, 0, sz);

ThreadAlloc::get().dealloc(p);
}
#endif

SNMALLOC_EXPORT
Expand Down
11 changes: 11 additions & 0 deletions src/test/func/malloc/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ int main(int argc, char** argv)
check_result(size + 1, 1, our_malloc_conceal(size + 1), SUCCESS, false);
}

our_freezero(nullptr, 1024);
void* p = our_malloc_conceal(64);
our_freezero(p, 128);
if (((uint8_t *)p)[63] != 0)
{
abort();
}

p = our_malloc_conceal(16);
our_freezero(p, 0);

for (smallsizeclass_t sc = 0; sc < NUM_SMALL_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);
Expand Down

0 comments on commit a797b2a

Please sign in to comment.