[hwasan] Provide __sanitizer_* aliases to allocator functions.

Summary:
Export __sanitizer_malloc, etc as aliases to malloc, etc.
This way users can wrap sanitizer malloc, even in fully static binaries.

Both jemalloc and tcmalloc provide similar aliases (je_* and tc_*).

Reviewers: vitalybuka, kcc

Subscribers: llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D50570

llvm-svn: 339614
This commit is contained in:
Evgeniy Stepanov 2018-08-13 21:07:27 +00:00
parent 4a6f190e19
commit d8cc7f9f07
7 changed files with 70 additions and 8 deletions

View File

@ -82,7 +82,6 @@ extern "C" {
Currently available with ASan only.
*/
void __sanitizer_purge_allocator(void);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -32,6 +32,21 @@ extern "C" {
void __hwasan_enable_allocator_tagging(void);
void __hwasan_disable_allocator_tagging(void);
int __sanitizer_posix_memalign(void **memptr, size_t alignment, size_t size);
void * __sanitizer_memalign(size_t alignment, size_t size);
void * __sanitizer_aligned_alloc(size_t alignment, size_t size);
void * __sanitizer___libc_memalign(size_t alignment, size_t size);
void * __sanitizer_valloc(size_t size);
void * __sanitizer_pvalloc(size_t size);
void __sanitizer_free(void *ptr);
void __sanitizer_cfree(void *ptr);
size_t __sanitizer_malloc_usable_size(const void *ptr);
struct mallinfo __sanitizer_mallinfo();
int __sanitizer_mallopt(int cmd, int value);
void __sanitizer_malloc_stats(void);
void * __sanitizer_calloc(size_t nmemb, size_t size);
void * __sanitizer_realloc(void *ptr, size_t size);
void * __sanitizer_malloc(size_t size);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -128,6 +128,25 @@ static void *AllocateFromLocalPool(uptr size_in_bytes) {
CHECK_UNPOISONED((x), \
common_flags()->strict_string_checks ? (len) + 1 : (n) )
#define SANITIZER_ALIAS(RET, FN, ARGS...) \
extern "C" SANITIZER_INTERFACE_ATTRIBUTE RET __sanitizer_##FN(ARGS) \
ALIAS(WRAPPER_NAME(FN));
SANITIZER_ALIAS(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size);
SANITIZER_ALIAS(void *, memalign, SIZE_T alignment, SIZE_T size);
SANITIZER_ALIAS(void *, aligned_alloc, SIZE_T alignment, SIZE_T size);
SANITIZER_ALIAS(void *, __libc_memalign, SIZE_T alignment, SIZE_T size);
SANITIZER_ALIAS(void *, valloc, SIZE_T size);
SANITIZER_ALIAS(void *, pvalloc, SIZE_T size);
SANITIZER_ALIAS(void, free, void *ptr);
SANITIZER_ALIAS(void, cfree, void *ptr);
SANITIZER_ALIAS(uptr, malloc_usable_size, const void *ptr);
SANITIZER_ALIAS(void, mallinfo, __sanitizer_struct_mallinfo *sret);
SANITIZER_ALIAS(int, mallopt, int cmd, int value);
SANITIZER_ALIAS(void, malloc_stats, void);
SANITIZER_ALIAS(void *, calloc, SIZE_T nmemb, SIZE_T size);
SANITIZER_ALIAS(void *, realloc, void *ptr, SIZE_T size);
SANITIZER_ALIAS(void *, malloc, SIZE_T size);
INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
GET_MALLOC_STACK_TRACE;
@ -200,11 +219,11 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
// temporary! The following is equivalent on all supported platforms but
// aarch64 (which uses a different register for sret value). We have a test
// to confirm that.
INTERCEPTOR(void, mallinfo, __sanitizer_mallinfo *sret) {
INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
#ifdef __aarch64__
uptr r8;
asm volatile("mov %0,x8" : "=r" (r8));
sret = reinterpret_cast<__sanitizer_mallinfo*>(r8);
sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8);
#endif
REAL(memset)(sret, 0, sizeof(*sret));
}

View File

@ -249,11 +249,11 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
// temporary! The following is equivalent on all supported platforms but
// aarch64 (which uses a different register for sret value). We have a test
// to confirm that.
INTERCEPTOR(void, mallinfo, __sanitizer_mallinfo *sret) {
INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
#ifdef __aarch64__
uptr r8;
asm volatile("mov %0,x8" : "=r" (r8));
sret = reinterpret_cast<__sanitizer_mallinfo*>(r8);
sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8);
#endif
REAL(memset)(sret, 0, sizeof(*sret));
__msan_unpoison(sret, sizeof(*sret));

View File

@ -1206,7 +1206,7 @@ CHECK_SIZE_AND_OFFSET(ifaddrs, ifa_data);
#endif
#if SANITIZER_LINUX
COMPILER_CHECK(sizeof(__sanitizer_mallinfo) == sizeof(struct mallinfo));
COMPILER_CHECK(sizeof(__sanitizer_struct_mallinfo) == sizeof(struct mallinfo));
#endif
#if !SANITIZER_ANDROID

View File

@ -187,13 +187,13 @@ namespace __sanitizer {
#endif // SANITIZER_LINUX || SANITIZER_FREEBSD
#if SANITIZER_ANDROID
struct __sanitizer_mallinfo {
struct __sanitizer_struct_mallinfo {
uptr v[10];
};
#endif
#if SANITIZER_LINUX && !SANITIZER_ANDROID
struct __sanitizer_mallinfo {
struct __sanitizer_struct_mallinfo {
int v[10];
};

View File

@ -0,0 +1,29 @@
// Test allocator aliases.
//
// RUN: %clangxx_hwasan -O0 %s -o %t && %run %t
#include <sanitizer/hwasan_interface.h>
int main() {
void *volatile sink;
sink = (void *)&__sanitizer_posix_memalign;
sink = (void *)&__sanitizer_memalign;
sink = (void *)&__sanitizer_aligned_alloc;
sink = (void *)&__sanitizer___libc_memalign;
sink = (void *)&__sanitizer_valloc;
sink = (void *)&__sanitizer_pvalloc;
sink = (void *)&__sanitizer_free;
sink = (void *)&__sanitizer_cfree;
sink = (void *)&__sanitizer_malloc_usable_size;
sink = (void *)&__sanitizer_mallinfo;
sink = (void *)&__sanitizer_mallopt;
sink = (void *)&__sanitizer_malloc_stats;
sink = (void *)&__sanitizer_calloc;
sink = (void *)&__sanitizer_realloc;
sink = (void *)&__sanitizer_malloc;
// sanity check
void *p = __sanitizer_malloc(100);
p = __sanitizer_realloc(p, 200);
__sanitizer_free(p);
}