mirror of
https://gcc.gnu.org/git/gcc.git
synced 2024-11-23 19:03:59 +08:00
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
353 lines
9.1 KiB
C++
353 lines
9.1 KiB
C++
//===-- sanitizer_addrhashmap.h ---------------------------------*- C++ -*-===//
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Concurrent uptr->T hashmap.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_ADDRHASHMAP_H
|
|
#define SANITIZER_ADDRHASHMAP_H
|
|
|
|
#include "sanitizer_common.h"
|
|
#include "sanitizer_mutex.h"
|
|
#include "sanitizer_atomic.h"
|
|
#include "sanitizer_allocator_internal.h"
|
|
|
|
namespace __sanitizer {
|
|
|
|
// Concurrent uptr->T hashmap.
|
|
// T must be a POD type, kSize is preferably a prime but can be any number.
|
|
// Usage example:
|
|
//
|
|
// typedef AddrHashMap<uptr, 11> Map;
|
|
// Map m;
|
|
// {
|
|
// Map::Handle h(&m, addr);
|
|
// use h.operator->() to access the data
|
|
// if h.created() then the element was just created, and the current thread
|
|
// has exclusive access to it
|
|
// otherwise the current thread has only read access to the data
|
|
// }
|
|
// {
|
|
// Map::Handle h(&m, addr, true);
|
|
// this will remove the data from the map in Handle dtor
|
|
// the current thread has exclusive access to the data
|
|
// if !h.exists() then the element never existed
|
|
// }
|
|
template<typename T, uptr kSize>
|
|
class AddrHashMap {
|
|
private:
|
|
struct Cell {
|
|
atomic_uintptr_t addr;
|
|
T val;
|
|
};
|
|
|
|
struct AddBucket {
|
|
uptr cap;
|
|
uptr size;
|
|
Cell cells[1]; // variable len
|
|
};
|
|
|
|
static const uptr kBucketSize = 3;
|
|
|
|
struct Bucket {
|
|
RWMutex mtx;
|
|
atomic_uintptr_t add;
|
|
Cell cells[kBucketSize];
|
|
};
|
|
|
|
public:
|
|
AddrHashMap();
|
|
|
|
class Handle {
|
|
public:
|
|
Handle(AddrHashMap<T, kSize> *map, uptr addr);
|
|
Handle(AddrHashMap<T, kSize> *map, uptr addr, bool remove);
|
|
Handle(AddrHashMap<T, kSize> *map, uptr addr, bool remove, bool create);
|
|
|
|
~Handle();
|
|
T *operator->();
|
|
T &operator*();
|
|
const T &operator*() const;
|
|
bool created() const;
|
|
bool exists() const;
|
|
|
|
private:
|
|
friend AddrHashMap<T, kSize>;
|
|
AddrHashMap<T, kSize> *map_;
|
|
Bucket *bucket_;
|
|
Cell *cell_;
|
|
uptr addr_;
|
|
uptr addidx_;
|
|
bool created_;
|
|
bool remove_;
|
|
bool create_;
|
|
};
|
|
|
|
private:
|
|
friend class Handle;
|
|
Bucket *table_;
|
|
|
|
void acquire(Handle *h);
|
|
void release(Handle *h);
|
|
uptr calcHash(uptr addr);
|
|
};
|
|
|
|
template<typename T, uptr kSize>
|
|
AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr) {
|
|
map_ = map;
|
|
addr_ = addr;
|
|
remove_ = false;
|
|
create_ = true;
|
|
map_->acquire(this);
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr,
|
|
bool remove) {
|
|
map_ = map;
|
|
addr_ = addr;
|
|
remove_ = remove;
|
|
create_ = true;
|
|
map_->acquire(this);
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
AddrHashMap<T, kSize>::Handle::Handle(AddrHashMap<T, kSize> *map, uptr addr,
|
|
bool remove, bool create) {
|
|
map_ = map;
|
|
addr_ = addr;
|
|
remove_ = remove;
|
|
create_ = create;
|
|
map_->acquire(this);
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
AddrHashMap<T, kSize>::Handle::~Handle() {
|
|
map_->release(this);
|
|
}
|
|
|
|
template <typename T, uptr kSize>
|
|
T *AddrHashMap<T, kSize>::Handle::operator->() {
|
|
return &cell_->val;
|
|
}
|
|
|
|
template <typename T, uptr kSize>
|
|
const T &AddrHashMap<T, kSize>::Handle::operator*() const {
|
|
return cell_->val;
|
|
}
|
|
|
|
template <typename T, uptr kSize>
|
|
T &AddrHashMap<T, kSize>::Handle::operator*() {
|
|
return cell_->val;
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
bool AddrHashMap<T, kSize>::Handle::created() const {
|
|
return created_;
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
bool AddrHashMap<T, kSize>::Handle::exists() const {
|
|
return cell_ != nullptr;
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
AddrHashMap<T, kSize>::AddrHashMap() {
|
|
table_ = (Bucket*)MmapOrDie(kSize * sizeof(table_[0]), "AddrHashMap");
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
void AddrHashMap<T, kSize>::acquire(Handle *h) {
|
|
uptr addr = h->addr_;
|
|
uptr hash = calcHash(addr);
|
|
Bucket *b = &table_[hash];
|
|
|
|
h->created_ = false;
|
|
h->addidx_ = -1U;
|
|
h->bucket_ = b;
|
|
h->cell_ = nullptr;
|
|
|
|
// If we want to remove the element, we need exclusive access to the bucket,
|
|
// so skip the lock-free phase.
|
|
if (h->remove_)
|
|
goto locked;
|
|
|
|
retry:
|
|
// First try to find an existing element w/o read mutex.
|
|
CHECK(!h->remove_);
|
|
// Check the embed cells.
|
|
for (uptr i = 0; i < kBucketSize; i++) {
|
|
Cell *c = &b->cells[i];
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_acquire);
|
|
if (addr1 == addr) {
|
|
h->cell_ = c;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Check the add cells with read lock.
|
|
if (atomic_load(&b->add, memory_order_relaxed)) {
|
|
b->mtx.ReadLock();
|
|
AddBucket *add = (AddBucket*)atomic_load(&b->add, memory_order_relaxed);
|
|
for (uptr i = 0; i < add->size; i++) {
|
|
Cell *c = &add->cells[i];
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
|
|
if (addr1 == addr) {
|
|
h->addidx_ = i;
|
|
h->cell_ = c;
|
|
return;
|
|
}
|
|
}
|
|
b->mtx.ReadUnlock();
|
|
}
|
|
|
|
locked:
|
|
// Re-check existence under write lock.
|
|
// Embed cells.
|
|
b->mtx.Lock();
|
|
for (uptr i = 0; i < kBucketSize; i++) {
|
|
Cell *c = &b->cells[i];
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
|
|
if (addr1 == addr) {
|
|
if (h->remove_) {
|
|
h->cell_ = c;
|
|
return;
|
|
}
|
|
b->mtx.Unlock();
|
|
goto retry;
|
|
}
|
|
}
|
|
|
|
// Add cells.
|
|
AddBucket *add = (AddBucket*)atomic_load(&b->add, memory_order_relaxed);
|
|
if (add) {
|
|
for (uptr i = 0; i < add->size; i++) {
|
|
Cell *c = &add->cells[i];
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
|
|
if (addr1 == addr) {
|
|
if (h->remove_) {
|
|
h->addidx_ = i;
|
|
h->cell_ = c;
|
|
return;
|
|
}
|
|
b->mtx.Unlock();
|
|
goto retry;
|
|
}
|
|
}
|
|
}
|
|
|
|
// The element does not exist, no need to create it if we want to remove.
|
|
if (h->remove_ || !h->create_) {
|
|
b->mtx.Unlock();
|
|
return;
|
|
}
|
|
|
|
// Now try to create it under the mutex.
|
|
h->created_ = true;
|
|
// See if we have a free embed cell.
|
|
for (uptr i = 0; i < kBucketSize; i++) {
|
|
Cell *c = &b->cells[i];
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
|
|
if (addr1 == 0) {
|
|
h->cell_ = c;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Store in the add cells.
|
|
if (!add) {
|
|
// Allocate a new add array.
|
|
const uptr kInitSize = 64;
|
|
add = (AddBucket*)InternalAlloc(kInitSize);
|
|
internal_memset(add, 0, kInitSize);
|
|
add->cap = (kInitSize - sizeof(*add)) / sizeof(add->cells[0]) + 1;
|
|
add->size = 0;
|
|
atomic_store(&b->add, (uptr)add, memory_order_relaxed);
|
|
}
|
|
if (add->size == add->cap) {
|
|
// Grow existing add array.
|
|
uptr oldsize = sizeof(*add) + (add->cap - 1) * sizeof(add->cells[0]);
|
|
uptr newsize = oldsize * 2;
|
|
AddBucket *add1 = (AddBucket*)InternalAlloc(newsize);
|
|
internal_memset(add1, 0, newsize);
|
|
add1->cap = (newsize - sizeof(*add)) / sizeof(add->cells[0]) + 1;
|
|
add1->size = add->size;
|
|
internal_memcpy(add1->cells, add->cells, add->size * sizeof(add->cells[0]));
|
|
InternalFree(add);
|
|
atomic_store(&b->add, (uptr)add1, memory_order_relaxed);
|
|
add = add1;
|
|
}
|
|
// Store.
|
|
uptr i = add->size++;
|
|
Cell *c = &add->cells[i];
|
|
CHECK_EQ(atomic_load(&c->addr, memory_order_relaxed), 0);
|
|
h->addidx_ = i;
|
|
h->cell_ = c;
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
void AddrHashMap<T, kSize>::release(Handle *h) {
|
|
if (!h->cell_)
|
|
return;
|
|
Bucket *b = h->bucket_;
|
|
Cell *c = h->cell_;
|
|
uptr addr1 = atomic_load(&c->addr, memory_order_relaxed);
|
|
if (h->created_) {
|
|
// Denote completion of insertion.
|
|
CHECK_EQ(addr1, 0);
|
|
// After the following store, the element becomes available
|
|
// for lock-free reads.
|
|
atomic_store(&c->addr, h->addr_, memory_order_release);
|
|
b->mtx.Unlock();
|
|
} else if (h->remove_) {
|
|
// Denote that the cell is empty now.
|
|
CHECK_EQ(addr1, h->addr_);
|
|
atomic_store(&c->addr, 0, memory_order_release);
|
|
// See if we need to compact the bucket.
|
|
AddBucket *add = (AddBucket*)atomic_load(&b->add, memory_order_relaxed);
|
|
if (h->addidx_ == -1U) {
|
|
// Removed from embed array, move an add element into the freed cell.
|
|
if (add && add->size != 0) {
|
|
uptr last = --add->size;
|
|
Cell *c1 = &add->cells[last];
|
|
c->val = c1->val;
|
|
uptr addr1 = atomic_load(&c1->addr, memory_order_relaxed);
|
|
atomic_store(&c->addr, addr1, memory_order_release);
|
|
atomic_store(&c1->addr, 0, memory_order_release);
|
|
}
|
|
} else {
|
|
// Removed from add array, compact it.
|
|
uptr last = --add->size;
|
|
Cell *c1 = &add->cells[last];
|
|
if (c != c1) {
|
|
*c = *c1;
|
|
atomic_store(&c1->addr, 0, memory_order_relaxed);
|
|
}
|
|
}
|
|
if (add && add->size == 0) {
|
|
// FIXME(dvyukov): free add?
|
|
}
|
|
b->mtx.Unlock();
|
|
} else {
|
|
CHECK_EQ(addr1, h->addr_);
|
|
if (h->addidx_ != -1U)
|
|
b->mtx.ReadUnlock();
|
|
}
|
|
}
|
|
|
|
template<typename T, uptr kSize>
|
|
uptr AddrHashMap<T, kSize>::calcHash(uptr addr) {
|
|
addr += addr << 10;
|
|
addr ^= addr >> 6;
|
|
return addr % kSize;
|
|
}
|
|
|
|
} // namespace __sanitizer
|
|
|
|
#endif // SANITIZER_ADDRHASHMAP_H
|