diff --git a/dist-build/emscripten-symbols.def b/dist-build/emscripten-symbols.def index 8ac770c3..9558a886 100644 --- a/dist-build/emscripten-symbols.def +++ b/dist-build/emscripten-symbols.def @@ -487,6 +487,7 @@ _randombytes_set_implementation 0 0 _randombytes_stir 1 1 _randombytes_uniform 1 1 _sodium_add 0 0 +_sodium_alloc_overhead 0 0 _sodium_allocarray 0 0 _sodium_bin2hex 1 1 _sodium_compare 0 0 diff --git a/src/libsodium/include/sodium/utils.h b/src/libsodium/include/sodium/utils.h index 0a7aadb4..4586cc2a 100644 --- a/src/libsodium/include/sodium/utils.h +++ b/src/libsodium/include/sodium/utils.h @@ -120,6 +120,9 @@ int sodium_mprotect_readonly(void *ptr); SODIUM_EXPORT int sodium_mprotect_readwrite(void *ptr); +SODIUM_EXPORT +size_t sodium_alloc_overhead(size_t size); + /* -------- */ int _sodium_alloc_init(void); diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index bff6e359..8ef2e630 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -702,3 +702,13 @@ sodium_mprotect_readwrite(void *ptr) { return _sodium_mprotect(ptr, _mprotect_readwrite); } + +size_t +sodium_alloc_overhead(size_t size) +{ +#ifndef HAVE_ALIGNED_MALLOC + return unprotected_size == (size_t) 0U; +#else + return _page_round((sizeof canary) + size) + page_size * (size_t) 3U; +#endif +} diff --git a/test/default/sodium_utils3.c b/test/default/sodium_utils3.c index 3f2b600e..4bd7eb5a 100644 --- a/test/default/sodium_utils3.c +++ b/test/default/sodium_utils3.c @@ -37,6 +37,9 @@ main(void) void * buf; size_t size; + assert(sodium_alloc_overhead((size_t) 0U) != + sodium_alloc_overhead((size_t) 65537U)); + #ifdef SIGSEGV signal(SIGSEGV, segv_handler); #endif