5d3805fca3
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins, store max (log2 (align), 0) into uchar field instead of align into uptr field. (ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins, store uchar 0 field instead of uptr 0 field. (instrument_nonnull_return): Use _v1 suffixed nonnull return builtin, instead of passing one address of struct with 2 locations pass two addresses of structs with 1 location each. * sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed. (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1, BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1, BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins. * c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword from expected output regexps. * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-3.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-5.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-6.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-8.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-9.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-10.c: Likewise. * g++.dg/ubsan/float-cast-overflow-bf.C: Likewise. * gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise. * g++.dg/asan/default-options-1.C (__asan_default_options): Add used attribute. * g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2 in the environment. * All source files: Merge from upstream 315899. * asan/Makefile.am (nodist_saninclude_HEADERS): Add include/sanitizer/tsan_interface.h. * asan/libtool-version: Bump the libasan SONAME. * lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc. (lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc. * sanitizer_common/Makefile.am (sanitizer_common_files): Add sancov_flags.cc, sanitizer_allocator_checks.cc, sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc, sanitizer_file.cc, sanitizer_mac_libcdep.cc and sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc and sanitizer_coverage_mapping_libcdep.cc. * tsan/Makefile.am (tsan_files): Add tsan_external.cc. * ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1. (ubsan_files): Add ubsan_init_standalone.cc and ubsan_signals_standalone.cc. * ubsan/libtool-version: Bump the libubsan SONAME. * asan/Makefile.in: Regenerate. * lsan/Makefile.in: Regenerate. * sanitizer_common/Makefile.in: Regenerate. * tsan/Makefile.in: Regenerate. * ubsan/Makefile.in: Regenerate. From-SVN: r253887
252 lines
7.8 KiB
C++
252 lines
7.8 KiB
C++
//===-- sanitizer_allocator.cc --------------------------------------------===//
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
// run-time libraries.
|
|
// This allocator is used inside run-times.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "sanitizer_allocator.h"
|
|
|
|
#include "sanitizer_allocator_checks.h"
|
|
#include "sanitizer_allocator_internal.h"
|
|
#include "sanitizer_atomic.h"
|
|
#include "sanitizer_common.h"
|
|
|
|
namespace __sanitizer {
|
|
|
|
// ThreadSanitizer for Go uses libc malloc/free.
|
|
#if SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
|
# if SANITIZER_LINUX && !SANITIZER_ANDROID
|
|
extern "C" void *__libc_malloc(uptr size);
|
|
# if !SANITIZER_GO
|
|
extern "C" void *__libc_memalign(uptr alignment, uptr size);
|
|
# endif
|
|
extern "C" void *__libc_realloc(void *ptr, uptr size);
|
|
extern "C" void __libc_free(void *ptr);
|
|
# else
|
|
# include <stdlib.h>
|
|
# define __libc_malloc malloc
|
|
# if !SANITIZER_GO
|
|
static void *__libc_memalign(uptr alignment, uptr size) {
|
|
void *p;
|
|
uptr error = posix_memalign(&p, alignment, size);
|
|
if (error) return nullptr;
|
|
return p;
|
|
}
|
|
# endif
|
|
# define __libc_realloc realloc
|
|
# define __libc_free free
|
|
# endif
|
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
|
|
uptr alignment) {
|
|
(void)cache;
|
|
#if !SANITIZER_GO
|
|
if (alignment == 0)
|
|
return __libc_malloc(size);
|
|
else
|
|
return __libc_memalign(alignment, size);
|
|
#else
|
|
// Windows does not provide __libc_memalign/posix_memalign. It provides
|
|
// __aligned_malloc, but the allocated blocks can't be passed to free,
|
|
// they need to be passed to __aligned_free. InternalAlloc interface does
|
|
// not account for such requirement. Alignemnt does not seem to be used
|
|
// anywhere in runtime, so just call __libc_malloc for now.
|
|
DCHECK_EQ(alignment, 0);
|
|
return __libc_malloc(size);
|
|
#endif
|
|
}
|
|
|
|
static void *RawInternalRealloc(void *ptr, uptr size,
|
|
InternalAllocatorCache *cache) {
|
|
(void)cache;
|
|
return __libc_realloc(ptr, size);
|
|
}
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
|
(void)cache;
|
|
__libc_free(ptr);
|
|
}
|
|
|
|
InternalAllocator *internal_allocator() {
|
|
return 0;
|
|
}
|
|
|
|
#else // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
|
|
|
static ALIGNED(64) char internal_alloc_placeholder[sizeof(InternalAllocator)];
|
|
static atomic_uint8_t internal_allocator_initialized;
|
|
static StaticSpinMutex internal_alloc_init_mu;
|
|
|
|
static InternalAllocatorCache internal_allocator_cache;
|
|
static StaticSpinMutex internal_allocator_cache_mu;
|
|
|
|
InternalAllocator *internal_allocator() {
|
|
InternalAllocator *internal_allocator_instance =
|
|
reinterpret_cast<InternalAllocator *>(&internal_alloc_placeholder);
|
|
if (atomic_load(&internal_allocator_initialized, memory_order_acquire) == 0) {
|
|
SpinMutexLock l(&internal_alloc_init_mu);
|
|
if (atomic_load(&internal_allocator_initialized, memory_order_relaxed) ==
|
|
0) {
|
|
internal_allocator_instance->Init(kReleaseToOSIntervalNever);
|
|
atomic_store(&internal_allocator_initialized, 1, memory_order_release);
|
|
}
|
|
}
|
|
return internal_allocator_instance;
|
|
}
|
|
|
|
static void *RawInternalAlloc(uptr size, InternalAllocatorCache *cache,
|
|
uptr alignment) {
|
|
if (alignment == 0) alignment = 8;
|
|
if (cache == 0) {
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
return internal_allocator()->Allocate(&internal_allocator_cache, size,
|
|
alignment);
|
|
}
|
|
return internal_allocator()->Allocate(cache, size, alignment);
|
|
}
|
|
|
|
static void *RawInternalRealloc(void *ptr, uptr size,
|
|
InternalAllocatorCache *cache) {
|
|
uptr alignment = 8;
|
|
if (cache == 0) {
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
|
|
size, alignment);
|
|
}
|
|
return internal_allocator()->Reallocate(cache, ptr, size, alignment);
|
|
}
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
|
if (!cache) {
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
|
|
}
|
|
internal_allocator()->Deallocate(cache, ptr);
|
|
}
|
|
|
|
#endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)
|
|
|
|
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;
|
|
|
|
void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
|
|
if (size + sizeof(u64) < size)
|
|
return nullptr;
|
|
void *p = RawInternalAlloc(size + sizeof(u64), cache, alignment);
|
|
if (!p)
|
|
return nullptr;
|
|
((u64*)p)[0] = kBlockMagic;
|
|
return (char*)p + sizeof(u64);
|
|
}
|
|
|
|
void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
|
|
if (!addr)
|
|
return InternalAlloc(size, cache);
|
|
if (size + sizeof(u64) < size)
|
|
return nullptr;
|
|
addr = (char*)addr - sizeof(u64);
|
|
size = size + sizeof(u64);
|
|
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
|
void *p = RawInternalRealloc(addr, size, cache);
|
|
if (!p)
|
|
return nullptr;
|
|
return (char*)p + sizeof(u64);
|
|
}
|
|
|
|
void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
|
|
if (UNLIKELY(CheckForCallocOverflow(count, size)))
|
|
return InternalAllocator::FailureHandler::OnBadRequest();
|
|
void *p = InternalAlloc(count * size, cache);
|
|
if (p) internal_memset(p, 0, count * size);
|
|
return p;
|
|
}
|
|
|
|
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
|
if (!addr)
|
|
return;
|
|
addr = (char*)addr - sizeof(u64);
|
|
CHECK_EQ(kBlockMagic, ((u64*)addr)[0]);
|
|
((u64*)addr)[0] = 0;
|
|
RawInternalFree(addr, cache);
|
|
}
|
|
|
|
// LowLevelAllocator
|
|
static LowLevelAllocateCallback low_level_alloc_callback;
|
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
|
// Align allocation size.
|
|
size = RoundUpTo(size, 8);
|
|
if (allocated_end_ - allocated_current_ < (sptr)size) {
|
|
uptr size_to_allocate = Max(size, GetPageSizeCached());
|
|
allocated_current_ =
|
|
(char*)MmapOrDie(size_to_allocate, __func__);
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
|
if (low_level_alloc_callback) {
|
|
low_level_alloc_callback((uptr)allocated_current_,
|
|
size_to_allocate);
|
|
}
|
|
}
|
|
CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
|
|
void *res = allocated_current_;
|
|
allocated_current_ += size;
|
|
return res;
|
|
}
|
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
|
|
low_level_alloc_callback = callback;
|
|
}
|
|
|
|
static atomic_uint8_t allocator_out_of_memory = {0};
|
|
static atomic_uint8_t allocator_may_return_null = {0};
|
|
|
|
bool IsAllocatorOutOfMemory() {
|
|
return atomic_load_relaxed(&allocator_out_of_memory);
|
|
}
|
|
|
|
// Prints error message and kills the program.
|
|
void NORETURN ReportAllocatorCannotReturnNull() {
|
|
Report("%s's allocator is terminating the process instead of returning 0\n",
|
|
SanitizerToolName);
|
|
Report("If you don't like this behavior set allocator_may_return_null=1\n");
|
|
CHECK(0);
|
|
Die();
|
|
}
|
|
|
|
bool AllocatorMayReturnNull() {
|
|
return atomic_load(&allocator_may_return_null, memory_order_relaxed);
|
|
}
|
|
|
|
void SetAllocatorMayReturnNull(bool may_return_null) {
|
|
atomic_store(&allocator_may_return_null, may_return_null,
|
|
memory_order_relaxed);
|
|
}
|
|
|
|
void *ReturnNullOrDieOnFailure::OnBadRequest() {
|
|
if (AllocatorMayReturnNull())
|
|
return nullptr;
|
|
ReportAllocatorCannotReturnNull();
|
|
}
|
|
|
|
void *ReturnNullOrDieOnFailure::OnOOM() {
|
|
atomic_store_relaxed(&allocator_out_of_memory, 1);
|
|
if (AllocatorMayReturnNull())
|
|
return nullptr;
|
|
ReportAllocatorCannotReturnNull();
|
|
}
|
|
|
|
void NORETURN *DieOnFailure::OnBadRequest() {
|
|
ReportAllocatorCannotReturnNull();
|
|
}
|
|
|
|
void NORETURN *DieOnFailure::OnOOM() {
|
|
atomic_store_relaxed(&allocator_out_of_memory, 1);
|
|
ReportAllocatorCannotReturnNull();
|
|
}
|
|
|
|
} // namespace __sanitizer
|