2012-08-30 18:02:48 +08:00
|
|
|
//===-- sanitizer_stackdepot.cc -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_stackdepot.h"
|
|
|
|
#include "sanitizer_common.h"
|
2012-09-17 11:14:15 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
2012-08-30 18:02:48 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-08-31 20:47:44 +08:00
|
|
|
#include "sanitizer_atomic.h"
|
2012-08-30 18:02:48 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2012-08-31 20:47:44 +08:00
|
|
|
const int kTabSize = 1024 * 1024; // Hash table size.
|
2012-09-17 11:14:15 +08:00
|
|
|
const int kPartBits = 8;
|
|
|
|
const int kPartShift = sizeof(u32) * 8 - kPartBits - 1;
|
2012-08-31 20:47:44 +08:00
|
|
|
const int kPartCount = 1 << kPartBits; // Number of subparts in the table.
|
|
|
|
const int kPartSize = kTabSize / kPartCount;
|
|
|
|
const int kMaxId = 1 << kPartShift;
|
|
|
|
|
2012-08-30 18:02:48 +08:00
|
|
|
struct StackDesc {
|
|
|
|
StackDesc *link;
|
|
|
|
u32 id;
|
2012-08-31 20:47:44 +08:00
|
|
|
u32 hash;
|
2012-08-30 18:02:48 +08:00
|
|
|
uptr size;
|
2012-08-31 20:47:44 +08:00
|
|
|
uptr stack[1]; // [size]
|
2012-08-30 18:02:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
2012-08-31 20:47:44 +08:00
|
|
|
StaticSpinMutex mtx; // Protects alloc of new blocks for region allocator.
|
|
|
|
atomic_uintptr_t region_pos; // Region allocator for StackDesc's.
|
|
|
|
atomic_uintptr_t region_end;
|
|
|
|
atomic_uintptr_t tab[kTabSize]; // Hash table of StackDesc's.
|
|
|
|
atomic_uint32_t seq[kPartCount]; // Unique id generators.
|
2012-08-30 18:02:48 +08:00
|
|
|
} depot;
|
|
|
|
|
2012-08-31 20:47:44 +08:00
|
|
|
static u32 hash(const uptr *stack, uptr size) {
|
|
|
|
// murmur2
|
|
|
|
const u32 m = 0x5bd1e995;
|
|
|
|
const u32 seed = 0x9747b28c;
|
|
|
|
const u32 r = 24;
|
|
|
|
u32 h = seed ^ (size * sizeof(uptr));
|
|
|
|
for (uptr i = 0; i < size; i++) {
|
|
|
|
u32 k = stack[i];
|
|
|
|
k *= m;
|
|
|
|
k ^= k >> r;
|
|
|
|
k *= m;
|
|
|
|
h *= m;
|
|
|
|
h ^= k;
|
|
|
|
}
|
|
|
|
h ^= h >> 13;
|
|
|
|
h *= m;
|
|
|
|
h ^= h >> 15;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static StackDesc *tryallocDesc(uptr memsz) {
|
|
|
|
// Optimisic lock-free allocation, essentially try to bump the region ptr.
|
|
|
|
for (;;) {
|
|
|
|
uptr cmp = atomic_load(&depot.region_pos, memory_order_acquire);
|
|
|
|
uptr end = atomic_load(&depot.region_end, memory_order_acquire);
|
|
|
|
if (cmp == 0 || cmp + memsz > end)
|
|
|
|
return 0;
|
|
|
|
if (atomic_compare_exchange_weak(
|
|
|
|
&depot.region_pos, &cmp, cmp + memsz,
|
|
|
|
memory_order_acquire))
|
|
|
|
return (StackDesc*)cmp;
|
|
|
|
}
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static StackDesc *allocDesc(uptr size) {
|
2012-08-31 20:47:44 +08:00
|
|
|
// Frist, try to allocate optimisitically.
|
2012-08-30 18:02:48 +08:00
|
|
|
uptr memsz = sizeof(StackDesc) + (size - 1) * sizeof(uptr);
|
2012-08-31 20:47:44 +08:00
|
|
|
StackDesc *s = tryallocDesc(memsz);
|
|
|
|
if (s)
|
|
|
|
return s;
|
|
|
|
// If failed, lock, retry and alloc new superblock.
|
|
|
|
SpinMutexLock l(&depot.mtx);
|
|
|
|
for (;;) {
|
|
|
|
s = tryallocDesc(memsz);
|
|
|
|
if (s)
|
|
|
|
return s;
|
|
|
|
atomic_store(&depot.region_pos, 0, memory_order_relaxed);
|
|
|
|
uptr allocsz = 64 * 1024;
|
2012-08-30 18:02:48 +08:00
|
|
|
if (allocsz < memsz)
|
|
|
|
allocsz = memsz;
|
2012-08-31 20:47:44 +08:00
|
|
|
uptr mem = (uptr)MmapOrDie(allocsz, "stack depot");
|
|
|
|
atomic_store(&depot.region_end, mem + allocsz, memory_order_release);
|
|
|
|
atomic_store(&depot.region_pos, mem, memory_order_release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 find(StackDesc *s, const uptr *stack, uptr size, u32 hash) {
|
|
|
|
// Searches linked list s for the stack, returns its id.
|
|
|
|
for (; s; s = s->link) {
|
|
|
|
if (s->hash == hash && s->size == size) {
|
|
|
|
uptr i = 0;
|
|
|
|
for (; i < size; i++) {
|
|
|
|
if (stack[i] != s->stack[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == size)
|
|
|
|
return s->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static StackDesc *lock(atomic_uintptr_t *p) {
|
|
|
|
// Uses the pointer lsb as mutex.
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
uptr cmp = atomic_load(p, memory_order_relaxed);
|
|
|
|
if ((cmp & 1) == 0
|
|
|
|
&& atomic_compare_exchange_weak(p, &cmp, cmp | 1,
|
|
|
|
memory_order_acquire))
|
|
|
|
return (StackDesc*)cmp;
|
|
|
|
if (i < 10)
|
|
|
|
proc_yield(10);
|
|
|
|
else
|
|
|
|
internal_sched_yield();
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
2012-08-31 20:47:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unlock(atomic_uintptr_t *p, StackDesc *s) {
|
|
|
|
DCHECK_EQ((uptr)s & 1, 0);
|
|
|
|
atomic_store(p, (uptr)s, memory_order_release);
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
|
|
|
|
2012-08-30 21:02:30 +08:00
|
|
|
u32 StackDepotPut(const uptr *stack, uptr size) {
|
2012-08-30 18:02:48 +08:00
|
|
|
if (stack == 0 || size == 0)
|
|
|
|
return 0;
|
|
|
|
uptr h = hash(stack, size);
|
2012-08-31 20:47:44 +08:00
|
|
|
atomic_uintptr_t *p = &depot.tab[h % kTabSize];
|
|
|
|
uptr v = atomic_load(p, memory_order_consume);
|
|
|
|
StackDesc *s = (StackDesc*)(v & ~1);
|
|
|
|
// First, try to find the existing stack.
|
|
|
|
u32 id = find(s, stack, size, h);
|
|
|
|
if (id)
|
|
|
|
return id;
|
|
|
|
// If failed, lock, retry and insert new.
|
|
|
|
StackDesc *s2 = lock(p);
|
|
|
|
if (s2 != s) {
|
|
|
|
id = find(s2, stack, size, h);
|
|
|
|
if (id) {
|
|
|
|
unlock(p, s2);
|
|
|
|
return id;
|
|
|
|
}
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
2012-08-31 20:47:44 +08:00
|
|
|
uptr part = (h % kTabSize) / kPartSize;
|
|
|
|
id = atomic_fetch_add(&depot.seq[part], 1, memory_order_relaxed) + 1;
|
|
|
|
CHECK_LT(id, kMaxId);
|
|
|
|
id |= part << kPartShift;
|
2012-09-17 11:14:15 +08:00
|
|
|
CHECK_NE(id, 0);
|
|
|
|
CHECK_EQ(id & (1u << 31), 0);
|
2012-08-31 20:47:44 +08:00
|
|
|
s = allocDesc(size);
|
|
|
|
s->id = id;
|
2012-08-30 18:02:48 +08:00
|
|
|
s->hash = h;
|
|
|
|
s->size = size;
|
|
|
|
internal_memcpy(s->stack, stack, size * sizeof(uptr));
|
2012-08-31 20:47:44 +08:00
|
|
|
s->link = s2;
|
|
|
|
unlock(p, s);
|
|
|
|
return id;
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
|
|
|
|
2012-08-30 21:02:30 +08:00
|
|
|
const uptr *StackDepotGet(u32 id, uptr *size) {
|
2012-08-30 18:02:48 +08:00
|
|
|
if (id == 0)
|
|
|
|
return 0;
|
2012-09-17 11:14:15 +08:00
|
|
|
CHECK_EQ(id & (1u << 31), 0);
|
2012-08-31 20:47:44 +08:00
|
|
|
// High kPartBits contain part id, so we need to scan at most kPartSize lists.
|
|
|
|
uptr part = id >> kPartShift;
|
|
|
|
for (int i = 0; i != kPartSize; i++) {
|
|
|
|
uptr idx = part * kPartSize + i;
|
|
|
|
CHECK_LT(idx, kTabSize);
|
|
|
|
atomic_uintptr_t *p = &depot.tab[idx];
|
|
|
|
uptr v = atomic_load(p, memory_order_consume);
|
|
|
|
StackDesc *s = (StackDesc*)(v & ~1);
|
|
|
|
for (; s; s = s->link) {
|
|
|
|
if (s->id == id) {
|
|
|
|
*size = s->size;
|
|
|
|
return s->stack;
|
|
|
|
}
|
2012-08-30 18:02:48 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 20:47:44 +08:00
|
|
|
*size = 0;
|
2012-08-30 18:02:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|