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
220 lines
7.4 KiB
C++
220 lines
7.4 KiB
C++
//===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
//
|
|
// ASan-private header for asan_allocator.cc.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef ASAN_ALLOCATOR_H
|
|
#define ASAN_ALLOCATOR_H
|
|
|
|
#include "asan_flags.h"
|
|
#include "asan_internal.h"
|
|
#include "asan_interceptors.h"
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
|
#include "sanitizer_common/sanitizer_list.h"
|
|
|
|
namespace __asan {
|
|
|
|
enum AllocType {
|
|
FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
|
|
FROM_NEW = 2, // Memory block came from operator new.
|
|
FROM_NEW_BR = 3 // Memory block came from operator new [ ]
|
|
};
|
|
|
|
struct AsanChunk;
|
|
|
|
struct AllocatorOptions {
|
|
u32 quarantine_size_mb;
|
|
u32 thread_local_quarantine_size_kb;
|
|
u16 min_redzone;
|
|
u16 max_redzone;
|
|
u8 may_return_null;
|
|
u8 alloc_dealloc_mismatch;
|
|
s32 release_to_os_interval_ms;
|
|
|
|
void SetFrom(const Flags *f, const CommonFlags *cf);
|
|
void CopyTo(Flags *f, CommonFlags *cf);
|
|
};
|
|
|
|
void InitializeAllocator(const AllocatorOptions &options);
|
|
void ReInitializeAllocator(const AllocatorOptions &options);
|
|
void GetAllocatorOptions(AllocatorOptions *options);
|
|
|
|
class AsanChunkView {
|
|
public:
|
|
explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
|
|
bool IsValid() const; // Checks if AsanChunkView points to a valid
|
|
// allocated or quarantined chunk.
|
|
bool IsAllocated() const; // Checks if the memory is currently allocated.
|
|
bool IsQuarantined() const; // Checks if the memory is currently quarantined.
|
|
uptr Beg() const; // First byte of user memory.
|
|
uptr End() const; // Last byte of user memory.
|
|
uptr UsedSize() const; // Size requested by the user.
|
|
uptr AllocTid() const;
|
|
uptr FreeTid() const;
|
|
bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
|
|
u32 GetAllocStackId() const;
|
|
u32 GetFreeStackId() const;
|
|
StackTrace GetAllocStack() const;
|
|
StackTrace GetFreeStack() const;
|
|
AllocType GetAllocType() const;
|
|
bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const {
|
|
if (addr >= Beg() && (addr + access_size) <= End()) {
|
|
*offset = addr - Beg();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const {
|
|
(void)access_size;
|
|
if (addr < Beg()) {
|
|
*offset = Beg() - addr;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
|
|
if (addr + access_size > End()) {
|
|
*offset = addr - End();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
AsanChunk *const chunk_;
|
|
};
|
|
|
|
AsanChunkView FindHeapChunkByAddress(uptr address);
|
|
AsanChunkView FindHeapChunkByAllocBeg(uptr address);
|
|
|
|
// List of AsanChunks with total size.
|
|
class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
|
|
public:
|
|
explicit AsanChunkFifoList(LinkerInitialized) { }
|
|
AsanChunkFifoList() { clear(); }
|
|
void Push(AsanChunk *n);
|
|
void PushList(AsanChunkFifoList *q);
|
|
AsanChunk *Pop();
|
|
uptr size() { return size_; }
|
|
void clear() {
|
|
IntrusiveList<AsanChunk>::clear();
|
|
size_ = 0;
|
|
}
|
|
private:
|
|
uptr size_;
|
|
};
|
|
|
|
struct AsanMapUnmapCallback {
|
|
void OnMap(uptr p, uptr size) const;
|
|
void OnUnmap(uptr p, uptr size) const;
|
|
};
|
|
|
|
#if SANITIZER_CAN_USE_ALLOCATOR64
|
|
# if SANITIZER_FUCHSIA
|
|
const uptr kAllocatorSpace = ~(uptr)0;
|
|
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
# elif defined(__powerpc64__)
|
|
const uptr kAllocatorSpace = 0xa0000000000ULL;
|
|
const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
# elif defined(__aarch64__) && SANITIZER_ANDROID
|
|
const uptr kAllocatorSpace = 0x3000000000ULL;
|
|
const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
|
|
typedef VeryCompactSizeClassMap SizeClassMap;
|
|
# elif defined(__aarch64__)
|
|
// AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
|
|
// so no need to different values for different VMA.
|
|
const uptr kAllocatorSpace = 0x10000000000ULL;
|
|
const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
# elif SANITIZER_WINDOWS
|
|
const uptr kAllocatorSpace = ~(uptr)0;
|
|
const uptr kAllocatorSize = 0x8000000000ULL; // 500G
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
# else
|
|
const uptr kAllocatorSpace = 0x600000000000ULL;
|
|
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
# endif
|
|
struct AP64 { // Allocator64 parameters. Deliberately using a short name.
|
|
static const uptr kSpaceBeg = kAllocatorSpace;
|
|
static const uptr kSpaceSize = kAllocatorSize;
|
|
static const uptr kMetadataSize = 0;
|
|
typedef __asan::SizeClassMap SizeClassMap;
|
|
typedef AsanMapUnmapCallback MapUnmapCallback;
|
|
static const uptr kFlags = 0;
|
|
};
|
|
|
|
typedef SizeClassAllocator64<AP64> PrimaryAllocator;
|
|
#else // Fallback to SizeClassAllocator32.
|
|
static const uptr kRegionSizeLog = 20;
|
|
static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
|
|
# if SANITIZER_WORDSIZE == 32
|
|
typedef FlatByteMap<kNumRegions> ByteMap;
|
|
# elif SANITIZER_WORDSIZE == 64
|
|
typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
|
|
# endif
|
|
typedef CompactSizeClassMap SizeClassMap;
|
|
struct AP32 {
|
|
static const uptr kSpaceBeg = 0;
|
|
static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
|
|
static const uptr kMetadataSize = 16;
|
|
typedef __asan::SizeClassMap SizeClassMap;
|
|
static const uptr kRegionSizeLog = __asan::kRegionSizeLog;
|
|
typedef __asan::ByteMap ByteMap;
|
|
typedef AsanMapUnmapCallback MapUnmapCallback;
|
|
static const uptr kFlags = 0;
|
|
};
|
|
typedef SizeClassAllocator32<AP32> PrimaryAllocator;
|
|
#endif // SANITIZER_CAN_USE_ALLOCATOR64
|
|
|
|
static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
|
|
typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
|
|
typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
|
|
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
|
|
SecondaryAllocator> AsanAllocator;
|
|
|
|
|
|
struct AsanThreadLocalMallocStorage {
|
|
uptr quarantine_cache[16];
|
|
AllocatorCache allocator_cache;
|
|
void CommitBack();
|
|
private:
|
|
// These objects are allocated via mmap() and are zero-initialized.
|
|
AsanThreadLocalMallocStorage() {}
|
|
};
|
|
|
|
void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
|
|
AllocType alloc_type);
|
|
void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
|
|
void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
|
|
AllocType alloc_type);
|
|
|
|
void *asan_malloc(uptr size, BufferedStackTrace *stack);
|
|
void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
|
|
void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
|
|
void *asan_valloc(uptr size, BufferedStackTrace *stack);
|
|
void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
|
|
|
|
int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
|
|
BufferedStackTrace *stack);
|
|
uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
|
|
|
|
uptr asan_mz_size(const void *ptr);
|
|
void asan_mz_force_lock();
|
|
void asan_mz_force_unlock();
|
|
|
|
void PrintInternalAllocatorStats();
|
|
void AsanSoftRssLimitExceededCallback(bool exceeded);
|
|
|
|
} // namespace __asan
|
|
#endif // ASAN_ALLOCATOR_H
|