2019-08-14 10:47:11 +02:00
|
|
|
//===-- sanitizer_allocator.cpp -------------------------------------------===//
|
2012-11-12 15:53:47 +00:00
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-11-12 15:53:47 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
2013-11-04 21:33:31 +00:00
|
|
|
// This allocator is used inside run-times.
|
2012-11-12 15:53:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-21 10:32:45 +03:00
|
|
|
|
2013-11-04 21:33:31 +00:00
|
|
|
#include "sanitizer_allocator.h"
|
2016-11-08 22:04:09 +00:00
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
#include "sanitizer_allocator_checks.h"
|
2013-11-04 21:33:31 +00:00
|
|
|
#include "sanitizer_allocator_internal.h"
|
2016-11-08 22:04:09 +00:00
|
|
|
#include "sanitizer_atomic.h"
|
2012-11-12 15:53:47 +00:00
|
|
|
#include "sanitizer_common.h"
|
2022-05-03 12:56:26 +02:00
|
|
|
#include "sanitizer_platform.h"
|
2012-11-12 15:53:47 +00:00
|
|
|
|
2013-11-04 21:33:31 +00:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
// Default allocator names.
|
|
|
|
const char *PrimaryAllocatorName = "SizeClassAllocator";
|
|
|
|
const char *SecondaryAllocatorName = "LargeMmapAllocator";
|
|
|
|
|
2013-11-04 21:33:31 +00:00
|
|
|
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) {
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
internal_allocator_instance->Init(kReleaseToOSIntervalNever);
|
2013-11-04 21:33:31 +00:00
|
|
|
atomic_store(&internal_allocator_initialized, 1, memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return internal_allocator_instance;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:04:09 +00:00
|
|
|
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,
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
alignment);
|
2016-11-08 22:04:09 +00:00
|
|
|
}
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
return internal_allocator()->Allocate(cache, size, alignment);
|
2016-11-08 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *RawInternalRealloc(void *ptr, uptr size,
|
|
|
|
InternalAllocatorCache *cache) {
|
|
|
|
uptr alignment = 8;
|
2013-11-04 21:33:31 +00:00
|
|
|
if (cache == 0) {
|
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
2016-11-08 22:04:09 +00:00
|
|
|
return internal_allocator()->Reallocate(&internal_allocator_cache, ptr,
|
|
|
|
size, alignment);
|
2013-11-04 21:33:31 +00:00
|
|
|
}
|
2016-11-08 22:04:09 +00:00
|
|
|
return internal_allocator()->Reallocate(cache, ptr, size, alignment);
|
2013-11-04 21:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {
|
2015-10-21 10:32:45 +03:00
|
|
|
if (!cache) {
|
2013-11-04 21:33:31 +00:00
|
|
|
SpinMutexLock l(&internal_allocator_cache_mu);
|
|
|
|
return internal_allocator()->Deallocate(&internal_allocator_cache, ptr);
|
|
|
|
}
|
|
|
|
internal_allocator()->Deallocate(cache, ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
|
|
|
|
SetAllocatorOutOfMemory();
|
|
|
|
Report("FATAL: %s: internal allocator is out of memory trying to allocate "
|
|
|
|
"0x%zx bytes\n", SanitizerToolName, requested_size);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:04:09 +00:00
|
|
|
void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
|
2021-07-20 10:44:37 -07:00
|
|
|
void *p = RawInternalAlloc(size, cache, alignment);
|
2018-10-31 12:14:23 +01:00
|
|
|
if (UNLIKELY(!p))
|
2021-07-20 10:44:37 -07:00
|
|
|
ReportInternalAllocatorOutOfMemory(size);
|
|
|
|
return p;
|
2012-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 22:04:09 +00:00
|
|
|
void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
|
2021-07-20 10:44:37 -07:00
|
|
|
void *p = RawInternalRealloc(addr, size, cache);
|
2018-10-31 12:14:23 +01:00
|
|
|
if (UNLIKELY(!p))
|
2021-07-20 10:44:37 -07:00
|
|
|
ReportInternalAllocatorOutOfMemory(size);
|
|
|
|
return p;
|
2016-11-08 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
void *InternalReallocArray(void *addr, uptr count, uptr size,
|
|
|
|
InternalAllocatorCache *cache) {
|
|
|
|
if (UNLIKELY(CheckForCallocOverflow(count, size))) {
|
|
|
|
Report(
|
|
|
|
"FATAL: %s: reallocarray parameters overflow: count * size (%zd * %zd) "
|
|
|
|
"cannot be represented in type size_t\n",
|
|
|
|
SanitizerToolName, count, size);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
return InternalRealloc(addr, count * size, cache);
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:04:09 +00:00
|
|
|
void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
|
2018-10-31 12:14:23 +01:00
|
|
|
if (UNLIKELY(CheckForCallocOverflow(count, size))) {
|
|
|
|
Report("FATAL: %s: calloc parameters overflow: count * size (%zd * %zd) "
|
|
|
|
"cannot be represented in type size_t\n", SanitizerToolName, count,
|
|
|
|
size);
|
|
|
|
Die();
|
|
|
|
}
|
2016-11-08 22:04:09 +00:00
|
|
|
void *p = InternalAlloc(count * size, cache);
|
2018-10-31 12:14:23 +01:00
|
|
|
if (LIKELY(p))
|
|
|
|
internal_memset(p, 0, count * size);
|
2016-11-08 22:04:09 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:33:31 +00:00
|
|
|
void InternalFree(void *addr, InternalAllocatorCache *cache) {
|
2021-07-20 10:44:37 -07:00
|
|
|
RawInternalFree(addr, cache);
|
2012-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 12:56:26 +02:00
|
|
|
void InternalAllocatorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
|
|
|
|
internal_allocator_cache_mu.Lock();
|
|
|
|
internal_allocator()->ForceLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalAllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
|
|
|
|
internal_allocator()->ForceUnlock();
|
|
|
|
internal_allocator_cache_mu.Unlock();
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:53:47 +00:00
|
|
|
// LowLevelAllocator
|
2018-10-31 12:14:23 +01:00
|
|
|
constexpr uptr kLowLevelAllocatorDefaultAlignment = 8;
|
2023-11-15 12:45:58 +01:00
|
|
|
constexpr uptr kMinNumPagesRounded = 16;
|
|
|
|
constexpr uptr kMinRoundedSize = 65536;
|
2018-10-31 12:14:23 +01:00
|
|
|
static uptr low_level_alloc_min_alignment = kLowLevelAllocatorDefaultAlignment;
|
2012-11-12 15:53:47 +00:00
|
|
|
static LowLevelAllocateCallback low_level_alloc_callback;
|
|
|
|
|
2023-11-15 12:45:58 +01:00
|
|
|
static LowLevelAllocator Alloc;
|
|
|
|
LowLevelAllocator &GetGlobalLowLevelAllocator() { return Alloc; }
|
|
|
|
|
2012-11-12 15:53:47 +00:00
|
|
|
void *LowLevelAllocator::Allocate(uptr size) {
|
|
|
|
// Align allocation size.
|
2018-10-31 12:14:23 +01:00
|
|
|
size = RoundUpTo(size, low_level_alloc_min_alignment);
|
2012-11-12 15:53:47 +00:00
|
|
|
if (allocated_end_ - allocated_current_ < (sptr)size) {
|
2023-11-15 12:45:58 +01:00
|
|
|
uptr size_to_allocate = RoundUpTo(
|
|
|
|
size, Min(GetPageSizeCached() * kMinNumPagesRounded, kMinRoundedSize));
|
2023-04-26 09:42:29 +02:00
|
|
|
allocated_current_ = (char *)MmapOrDie(size_to_allocate, __func__);
|
2012-11-12 15:53:47 +00:00
|
|
|
allocated_end_ = allocated_current_ + size_to_allocate;
|
|
|
|
if (low_level_alloc_callback) {
|
2023-04-26 09:42:29 +02:00
|
|
|
low_level_alloc_callback((uptr)allocated_current_, size_to_allocate);
|
2012-11-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(allocated_end_ - allocated_current_ >= (sptr)size);
|
|
|
|
void *res = allocated_current_;
|
|
|
|
allocated_current_ += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
void SetLowLevelAllocateMinAlignment(uptr alignment) {
|
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
|
|
|
low_level_alloc_min_alignment = Max(alignment, low_level_alloc_min_alignment);
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:53:47 +00:00
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback) {
|
|
|
|
low_level_alloc_callback = callback;
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
// Allocator's OOM and other errors handling support.
|
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
static atomic_uint8_t allocator_out_of_memory = {0};
|
|
|
|
static atomic_uint8_t allocator_may_return_null = {0};
|
2016-11-08 22:04:09 +00:00
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
bool IsAllocatorOutOfMemory() {
|
|
|
|
return atomic_load_relaxed(&allocator_out_of_memory);
|
|
|
|
}
|
2016-11-08 22:04:09 +00:00
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
void SetAllocatorOutOfMemory() {
|
|
|
|
atomic_store_relaxed(&allocator_out_of_memory, 1);
|
2013-11-04 21:33:31 +00:00
|
|
|
}
|
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
void PrintHintAllocatorCannotReturnNull() {
|
|
|
|
Report("HINT: if you don't care about these errors you may set "
|
|
|
|
"allocator_may_return_null=1\n");
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* 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
2017-10-19 13:23:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 12:56:26 +02:00
|
|
|
static atomic_uint8_t rss_limit_exceeded;
|
|
|
|
|
|
|
|
bool IsRssLimitExceeded() {
|
|
|
|
return atomic_load(&rss_limit_exceeded, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetRssLimitExceeded(bool limit_exceeded) {
|
|
|
|
atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2015-10-21 10:32:45 +03:00
|
|
|
} // namespace __sanitizer
|