Add memory tracking and sanitization hooks

Intended use is sanitization of BoringSSL allocations.

Change-Id: Ia577f944d19e5b0b77373fedd0970e2c0c97cd21
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/39824
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
This commit is contained in:
Wiktor Garbacz 2020-02-05 18:14:20 +01:00 committed by CQ bot account: commit-bot@chromium.org
parent 5cd0724ecb
commit 9ae40ce9ad

View File

@ -85,6 +85,13 @@ static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
// Windows will emit strong symbols instead. See
// https://bugs.llvm.org/show_bug.cgi?id=37598
#if defined(__GNUC__) || (defined(__clang__) && !defined(_MSC_VER))
#define WEAK_SYMBOL_DECL
#define WEAK_SYMBOL_DEFAULT_IMPL __attribute((weak, noinline))
#else
#define WEAK_SYMBOL_DECL static
#define WEAK_SYMBOL_DEFAULT_IMPL static
#endif
// sdallocx is a sized |free| function. By passing the size (which we happen to
// always know in BoringSSL), the malloc implementation can save work. We cannot
// depend on |sdallocx| being available so we declare a wrapper that falls back
@ -94,16 +101,22 @@ static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
// implementation is statically linked with BoringSSL. So, if |sdallocx| is
// provided in, say, libc.so, we still won't use it because that's dynamically
// linked. This isn't an ideal result, but its helps in some cases.
void sdallocx(void *ptr, size_t size, int flags);
__attribute((weak, noinline))
#else
static
#endif
void sdallocx(void *ptr, size_t size, int flags) {
WEAK_SYMBOL_DECL void sdallocx(void *ptr, size_t size, int flags);
WEAK_SYMBOL_DEFAULT_IMPL void sdallocx(void *ptr, size_t size, int flags) {
free(ptr);
}
// The following two functions are for memory tracking. They are no-ops by
// default but can be overridden at link time if the application needs to
// observe heap operations.
WEAK_SYMBOL_DECL void OPENSSL_track_memory_alloc(void *ptr, size_t size);
WEAK_SYMBOL_DEFAULT_IMPL void OPENSSL_track_memory_alloc(void *ptr,
size_t size) {}
WEAK_SYMBOL_DECL void OPENSSL_track_memory_free(void *ptr, size_t size);
WEAK_SYMBOL_DEFAULT_IMPL void OPENSSL_track_memory_free(void *ptr,
size_t size) {}
void *OPENSSL_malloc(size_t size) {
if (size + OPENSSL_MALLOC_PREFIX < size) {
return NULL;
@ -117,6 +130,7 @@ void *OPENSSL_malloc(size_t size) {
*(size_t *)ptr = size;
__asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
OPENSSL_track_memory_alloc(ptr, size + OPENSSL_MALLOC_PREFIX);
return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
}
@ -129,6 +143,7 @@ void OPENSSL_free(void *orig_ptr) {
__asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
size_t size = *(size_t *)ptr;
OPENSSL_track_memory_free(ptr, size + OPENSSL_MALLOC_PREFIX);
OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
}