xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_mman.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
168d75effSDimitry Andric //===-- tsan_mman.cpp -----------------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of ThreadSanitizer (TSan), a race detector.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric #include "sanitizer_common/sanitizer_allocator_checks.h"
1368d75effSDimitry Andric #include "sanitizer_common/sanitizer_allocator_interface.h"
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_allocator_report.h"
1568d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1668d75effSDimitry Andric #include "sanitizer_common/sanitizer_errno.h"
1768d75effSDimitry Andric #include "sanitizer_common/sanitizer_placement_new.h"
1868d75effSDimitry Andric #include "tsan_mman.h"
1968d75effSDimitry Andric #include "tsan_rtl.h"
2068d75effSDimitry Andric #include "tsan_report.h"
2168d75effSDimitry Andric #include "tsan_flags.h"
2268d75effSDimitry Andric 
2368d75effSDimitry Andric // May be overriden by front-end.
2468d75effSDimitry Andric SANITIZER_WEAK_DEFAULT_IMPL
2568d75effSDimitry Andric void __sanitizer_malloc_hook(void *ptr, uptr size) {
2668d75effSDimitry Andric   (void)ptr;
2768d75effSDimitry Andric   (void)size;
2868d75effSDimitry Andric }
2968d75effSDimitry Andric 
3068d75effSDimitry Andric SANITIZER_WEAK_DEFAULT_IMPL
3168d75effSDimitry Andric void __sanitizer_free_hook(void *ptr) {
3268d75effSDimitry Andric   (void)ptr;
3368d75effSDimitry Andric }
3468d75effSDimitry Andric 
3568d75effSDimitry Andric namespace __tsan {
3668d75effSDimitry Andric 
3768d75effSDimitry Andric struct MapUnmapCallback {
3868d75effSDimitry Andric   void OnMap(uptr p, uptr size) const { }
3968d75effSDimitry Andric   void OnUnmap(uptr p, uptr size) const {
4068d75effSDimitry Andric     // We are about to unmap a chunk of user memory.
4168d75effSDimitry Andric     // Mark the corresponding shadow memory as not needed.
4268d75effSDimitry Andric     DontNeedShadowFor(p, size);
4368d75effSDimitry Andric     // Mark the corresponding meta shadow memory as not needed.
4468d75effSDimitry Andric     // Note the block does not contain any meta info at this point
4568d75effSDimitry Andric     // (this happens after free).
4668d75effSDimitry Andric     const uptr kMetaRatio = kMetaShadowCell / kMetaShadowSize;
4768d75effSDimitry Andric     const uptr kPageSize = GetPageSizeCached() * kMetaRatio;
4868d75effSDimitry Andric     // Block came from LargeMmapAllocator, so must be large.
4968d75effSDimitry Andric     // We rely on this in the calculations below.
5068d75effSDimitry Andric     CHECK_GE(size, 2 * kPageSize);
5168d75effSDimitry Andric     uptr diff = RoundUp(p, kPageSize) - p;
5268d75effSDimitry Andric     if (diff != 0) {
5368d75effSDimitry Andric       p += diff;
5468d75effSDimitry Andric       size -= diff;
5568d75effSDimitry Andric     }
5668d75effSDimitry Andric     diff = p + size - RoundDown(p + size, kPageSize);
5768d75effSDimitry Andric     if (diff != 0)
5868d75effSDimitry Andric       size -= diff;
5968d75effSDimitry Andric     uptr p_meta = (uptr)MemToMeta(p);
6068d75effSDimitry Andric     ReleaseMemoryPagesToOS(p_meta, p_meta + size / kMetaRatio);
6168d75effSDimitry Andric   }
6268d75effSDimitry Andric };
6368d75effSDimitry Andric 
6468d75effSDimitry Andric static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
6568d75effSDimitry Andric Allocator *allocator() {
6668d75effSDimitry Andric   return reinterpret_cast<Allocator*>(&allocator_placeholder);
6768d75effSDimitry Andric }
6868d75effSDimitry Andric 
6968d75effSDimitry Andric struct GlobalProc {
7068d75effSDimitry Andric   Mutex mtx;
7168d75effSDimitry Andric   Processor *proc;
72*4824e7fdSDimitry Andric   // This mutex represents the internal allocator combined for
73*4824e7fdSDimitry Andric   // the purposes of deadlock detection. The internal allocator
74*4824e7fdSDimitry Andric   // uses multiple mutexes, moreover they are locked only occasionally
75*4824e7fdSDimitry Andric   // and they are spin mutexes which don't support deadlock detection.
76*4824e7fdSDimitry Andric   // So we use this fake mutex to serve as a substitute for these mutexes.
77*4824e7fdSDimitry Andric   CheckedMutex internal_alloc_mtx;
7868d75effSDimitry Andric 
79*4824e7fdSDimitry Andric   GlobalProc()
80*4824e7fdSDimitry Andric       : mtx(MutexTypeGlobalProc),
81*4824e7fdSDimitry Andric         proc(ProcCreate()),
82*4824e7fdSDimitry Andric         internal_alloc_mtx(MutexTypeInternalAlloc) {}
8368d75effSDimitry Andric };
8468d75effSDimitry Andric 
8568d75effSDimitry Andric static char global_proc_placeholder[sizeof(GlobalProc)] ALIGNED(64);
8668d75effSDimitry Andric GlobalProc *global_proc() {
8768d75effSDimitry Andric   return reinterpret_cast<GlobalProc*>(&global_proc_placeholder);
8868d75effSDimitry Andric }
8968d75effSDimitry Andric 
90*4824e7fdSDimitry Andric static void InternalAllocAccess() {
91*4824e7fdSDimitry Andric   global_proc()->internal_alloc_mtx.Lock();
92*4824e7fdSDimitry Andric   global_proc()->internal_alloc_mtx.Unlock();
93*4824e7fdSDimitry Andric }
94*4824e7fdSDimitry Andric 
9568d75effSDimitry Andric ScopedGlobalProcessor::ScopedGlobalProcessor() {
9668d75effSDimitry Andric   GlobalProc *gp = global_proc();
9768d75effSDimitry Andric   ThreadState *thr = cur_thread();
9868d75effSDimitry Andric   if (thr->proc())
9968d75effSDimitry Andric     return;
10068d75effSDimitry Andric   // If we don't have a proc, use the global one.
10168d75effSDimitry Andric   // There are currently only two known case where this path is triggered:
10268d75effSDimitry Andric   //   __interceptor_free
10368d75effSDimitry Andric   //   __nptl_deallocate_tsd
10468d75effSDimitry Andric   //   start_thread
10568d75effSDimitry Andric   //   clone
10668d75effSDimitry Andric   // and:
10768d75effSDimitry Andric   //   ResetRange
10868d75effSDimitry Andric   //   __interceptor_munmap
10968d75effSDimitry Andric   //   __deallocate_stack
11068d75effSDimitry Andric   //   start_thread
11168d75effSDimitry Andric   //   clone
11268d75effSDimitry Andric   // Ideally, we destroy thread state (and unwire proc) when a thread actually
11368d75effSDimitry Andric   // exits (i.e. when we join/wait it). Then we would not need the global proc
11468d75effSDimitry Andric   gp->mtx.Lock();
11568d75effSDimitry Andric   ProcWire(gp->proc, thr);
11668d75effSDimitry Andric }
11768d75effSDimitry Andric 
11868d75effSDimitry Andric ScopedGlobalProcessor::~ScopedGlobalProcessor() {
11968d75effSDimitry Andric   GlobalProc *gp = global_proc();
12068d75effSDimitry Andric   ThreadState *thr = cur_thread();
12168d75effSDimitry Andric   if (thr->proc() != gp->proc)
12268d75effSDimitry Andric     return;
12368d75effSDimitry Andric   ProcUnwire(gp->proc, thr);
12468d75effSDimitry Andric   gp->mtx.Unlock();
12568d75effSDimitry Andric }
12668d75effSDimitry Andric 
127*4824e7fdSDimitry Andric void AllocatorLock() NO_THREAD_SAFETY_ANALYSIS {
128*4824e7fdSDimitry Andric   global_proc()->mtx.Lock();
129*4824e7fdSDimitry Andric   global_proc()->internal_alloc_mtx.Lock();
130*4824e7fdSDimitry Andric   InternalAllocatorLock();
131*4824e7fdSDimitry Andric }
132*4824e7fdSDimitry Andric 
133*4824e7fdSDimitry Andric void AllocatorUnlock() NO_THREAD_SAFETY_ANALYSIS {
134*4824e7fdSDimitry Andric   InternalAllocatorUnlock();
135*4824e7fdSDimitry Andric   global_proc()->internal_alloc_mtx.Unlock();
136*4824e7fdSDimitry Andric   global_proc()->mtx.Unlock();
137*4824e7fdSDimitry Andric }
138*4824e7fdSDimitry Andric 
139480093f4SDimitry Andric static constexpr uptr kMaxAllowedMallocSize = 1ull << 40;
140480093f4SDimitry Andric static uptr max_user_defined_malloc_size;
141480093f4SDimitry Andric 
14268d75effSDimitry Andric void InitializeAllocator() {
14368d75effSDimitry Andric   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
14468d75effSDimitry Andric   allocator()->Init(common_flags()->allocator_release_to_os_interval_ms);
145480093f4SDimitry Andric   max_user_defined_malloc_size = common_flags()->max_allocation_size_mb
146480093f4SDimitry Andric                                      ? common_flags()->max_allocation_size_mb
147480093f4SDimitry Andric                                            << 20
148480093f4SDimitry Andric                                      : kMaxAllowedMallocSize;
14968d75effSDimitry Andric }
15068d75effSDimitry Andric 
15168d75effSDimitry Andric void InitializeAllocatorLate() {
15268d75effSDimitry Andric   new(global_proc()) GlobalProc();
15368d75effSDimitry Andric }
15468d75effSDimitry Andric 
15568d75effSDimitry Andric void AllocatorProcStart(Processor *proc) {
15668d75effSDimitry Andric   allocator()->InitCache(&proc->alloc_cache);
15768d75effSDimitry Andric   internal_allocator()->InitCache(&proc->internal_alloc_cache);
15868d75effSDimitry Andric }
15968d75effSDimitry Andric 
16068d75effSDimitry Andric void AllocatorProcFinish(Processor *proc) {
16168d75effSDimitry Andric   allocator()->DestroyCache(&proc->alloc_cache);
16268d75effSDimitry Andric   internal_allocator()->DestroyCache(&proc->internal_alloc_cache);
16368d75effSDimitry Andric }
16468d75effSDimitry Andric 
16568d75effSDimitry Andric void AllocatorPrintStats() {
16668d75effSDimitry Andric   allocator()->PrintStats();
16768d75effSDimitry Andric }
16868d75effSDimitry Andric 
16968d75effSDimitry Andric static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
17068d75effSDimitry Andric   if (atomic_load_relaxed(&thr->in_signal_handler) == 0 ||
171fe6060f1SDimitry Andric       !ShouldReport(thr, ReportTypeSignalUnsafe))
17268d75effSDimitry Andric     return;
17368d75effSDimitry Andric   VarSizeStackTrace stack;
17468d75effSDimitry Andric   ObtainCurrentStack(thr, pc, &stack);
17568d75effSDimitry Andric   if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack))
17668d75effSDimitry Andric     return;
177349cc55cSDimitry Andric   ThreadRegistryLock l(&ctx->thread_registry);
17868d75effSDimitry Andric   ScopedReport rep(ReportTypeSignalUnsafe);
17968d75effSDimitry Andric   rep.AddStack(stack, true);
18068d75effSDimitry Andric   OutputReport(thr, rep);
18168d75effSDimitry Andric }
18268d75effSDimitry Andric 
18368d75effSDimitry Andric 
18468d75effSDimitry Andric void *user_alloc_internal(ThreadState *thr, uptr pc, uptr sz, uptr align,
18568d75effSDimitry Andric                           bool signal) {
186480093f4SDimitry Andric   if (sz >= kMaxAllowedMallocSize || align >= kMaxAllowedMallocSize ||
187480093f4SDimitry Andric       sz > max_user_defined_malloc_size) {
18868d75effSDimitry Andric     if (AllocatorMayReturnNull())
18968d75effSDimitry Andric       return nullptr;
190480093f4SDimitry Andric     uptr malloc_limit =
191480093f4SDimitry Andric         Min(kMaxAllowedMallocSize, max_user_defined_malloc_size);
19268d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
193480093f4SDimitry Andric     ReportAllocationSizeTooBig(sz, malloc_limit, &stack);
19468d75effSDimitry Andric   }
19568d75effSDimitry Andric   void *p = allocator()->Allocate(&thr->proc()->alloc_cache, sz, align);
19668d75effSDimitry Andric   if (UNLIKELY(!p)) {
19768d75effSDimitry Andric     SetAllocatorOutOfMemory();
19868d75effSDimitry Andric     if (AllocatorMayReturnNull())
19968d75effSDimitry Andric       return nullptr;
20068d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
20168d75effSDimitry Andric     ReportOutOfMemory(sz, &stack);
20268d75effSDimitry Andric   }
20368d75effSDimitry Andric   if (ctx && ctx->initialized)
20468d75effSDimitry Andric     OnUserAlloc(thr, pc, (uptr)p, sz, true);
20568d75effSDimitry Andric   if (signal)
20668d75effSDimitry Andric     SignalUnsafeCall(thr, pc);
20768d75effSDimitry Andric   return p;
20868d75effSDimitry Andric }
20968d75effSDimitry Andric 
21068d75effSDimitry Andric void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
21168d75effSDimitry Andric   ScopedGlobalProcessor sgp;
21268d75effSDimitry Andric   if (ctx && ctx->initialized)
21368d75effSDimitry Andric     OnUserFree(thr, pc, (uptr)p, true);
21468d75effSDimitry Andric   allocator()->Deallocate(&thr->proc()->alloc_cache, p);
21568d75effSDimitry Andric   if (signal)
21668d75effSDimitry Andric     SignalUnsafeCall(thr, pc);
21768d75effSDimitry Andric }
21868d75effSDimitry Andric 
21968d75effSDimitry Andric void *user_alloc(ThreadState *thr, uptr pc, uptr sz) {
22068d75effSDimitry Andric   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, kDefaultAlignment));
22168d75effSDimitry Andric }
22268d75effSDimitry Andric 
22368d75effSDimitry Andric void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
22468d75effSDimitry Andric   if (UNLIKELY(CheckForCallocOverflow(size, n))) {
22568d75effSDimitry Andric     if (AllocatorMayReturnNull())
22668d75effSDimitry Andric       return SetErrnoOnNull(nullptr);
22768d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
22868d75effSDimitry Andric     ReportCallocOverflow(n, size, &stack);
22968d75effSDimitry Andric   }
23068d75effSDimitry Andric   void *p = user_alloc_internal(thr, pc, n * size);
23168d75effSDimitry Andric   if (p)
23268d75effSDimitry Andric     internal_memset(p, 0, n * size);
23368d75effSDimitry Andric   return SetErrnoOnNull(p);
23468d75effSDimitry Andric }
23568d75effSDimitry Andric 
23668d75effSDimitry Andric void *user_reallocarray(ThreadState *thr, uptr pc, void *p, uptr size, uptr n) {
23768d75effSDimitry Andric   if (UNLIKELY(CheckForCallocOverflow(size, n))) {
23868d75effSDimitry Andric     if (AllocatorMayReturnNull())
23968d75effSDimitry Andric       return SetErrnoOnNull(nullptr);
24068d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
24168d75effSDimitry Andric     ReportReallocArrayOverflow(size, n, &stack);
24268d75effSDimitry Andric   }
24368d75effSDimitry Andric   return user_realloc(thr, pc, p, size * n);
24468d75effSDimitry Andric }
24568d75effSDimitry Andric 
24668d75effSDimitry Andric void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
247349cc55cSDimitry Andric   DPrintf("#%d: alloc(%zu) = 0x%zx\n", thr->tid, sz, p);
24868d75effSDimitry Andric   ctx->metamap.AllocBlock(thr, pc, p, sz);
249349cc55cSDimitry Andric   if (write && thr->ignore_reads_and_writes == 0 && thr->is_inited)
25068d75effSDimitry Andric     MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
25168d75effSDimitry Andric   else
25268d75effSDimitry Andric     MemoryResetRange(thr, pc, (uptr)p, sz);
25368d75effSDimitry Andric }
25468d75effSDimitry Andric 
25568d75effSDimitry Andric void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
25668d75effSDimitry Andric   CHECK_NE(p, (void*)0);
25768d75effSDimitry Andric   uptr sz = ctx->metamap.FreeBlock(thr->proc(), p);
258349cc55cSDimitry Andric   DPrintf("#%d: free(0x%zx, %zu)\n", thr->tid, p, sz);
259349cc55cSDimitry Andric   if (write && thr->ignore_reads_and_writes == 0 && thr->is_inited)
26068d75effSDimitry Andric     MemoryRangeFreed(thr, pc, (uptr)p, sz);
26168d75effSDimitry Andric }
26268d75effSDimitry Andric 
26368d75effSDimitry Andric void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
26468d75effSDimitry Andric   // FIXME: Handle "shrinking" more efficiently,
26568d75effSDimitry Andric   // it seems that some software actually does this.
26668d75effSDimitry Andric   if (!p)
26768d75effSDimitry Andric     return SetErrnoOnNull(user_alloc_internal(thr, pc, sz));
26868d75effSDimitry Andric   if (!sz) {
26968d75effSDimitry Andric     user_free(thr, pc, p);
27068d75effSDimitry Andric     return nullptr;
27168d75effSDimitry Andric   }
27268d75effSDimitry Andric   void *new_p = user_alloc_internal(thr, pc, sz);
27368d75effSDimitry Andric   if (new_p) {
27468d75effSDimitry Andric     uptr old_sz = user_alloc_usable_size(p);
27568d75effSDimitry Andric     internal_memcpy(new_p, p, min(old_sz, sz));
27668d75effSDimitry Andric     user_free(thr, pc, p);
27768d75effSDimitry Andric   }
27868d75effSDimitry Andric   return SetErrnoOnNull(new_p);
27968d75effSDimitry Andric }
28068d75effSDimitry Andric 
28168d75effSDimitry Andric void *user_memalign(ThreadState *thr, uptr pc, uptr align, uptr sz) {
28268d75effSDimitry Andric   if (UNLIKELY(!IsPowerOfTwo(align))) {
28368d75effSDimitry Andric     errno = errno_EINVAL;
28468d75effSDimitry Andric     if (AllocatorMayReturnNull())
28568d75effSDimitry Andric       return nullptr;
28668d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
28768d75effSDimitry Andric     ReportInvalidAllocationAlignment(align, &stack);
28868d75effSDimitry Andric   }
28968d75effSDimitry Andric   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align));
29068d75effSDimitry Andric }
29168d75effSDimitry Andric 
29268d75effSDimitry Andric int user_posix_memalign(ThreadState *thr, uptr pc, void **memptr, uptr align,
29368d75effSDimitry Andric                         uptr sz) {
29468d75effSDimitry Andric   if (UNLIKELY(!CheckPosixMemalignAlignment(align))) {
29568d75effSDimitry Andric     if (AllocatorMayReturnNull())
29668d75effSDimitry Andric       return errno_EINVAL;
29768d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
29868d75effSDimitry Andric     ReportInvalidPosixMemalignAlignment(align, &stack);
29968d75effSDimitry Andric   }
30068d75effSDimitry Andric   void *ptr = user_alloc_internal(thr, pc, sz, align);
30168d75effSDimitry Andric   if (UNLIKELY(!ptr))
30268d75effSDimitry Andric     // OOM error is already taken care of by user_alloc_internal.
30368d75effSDimitry Andric     return errno_ENOMEM;
30468d75effSDimitry Andric   CHECK(IsAligned((uptr)ptr, align));
30568d75effSDimitry Andric   *memptr = ptr;
30668d75effSDimitry Andric   return 0;
30768d75effSDimitry Andric }
30868d75effSDimitry Andric 
30968d75effSDimitry Andric void *user_aligned_alloc(ThreadState *thr, uptr pc, uptr align, uptr sz) {
31068d75effSDimitry Andric   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(align, sz))) {
31168d75effSDimitry Andric     errno = errno_EINVAL;
31268d75effSDimitry Andric     if (AllocatorMayReturnNull())
31368d75effSDimitry Andric       return nullptr;
31468d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
31568d75effSDimitry Andric     ReportInvalidAlignedAllocAlignment(sz, align, &stack);
31668d75effSDimitry Andric   }
31768d75effSDimitry Andric   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align));
31868d75effSDimitry Andric }
31968d75effSDimitry Andric 
32068d75effSDimitry Andric void *user_valloc(ThreadState *thr, uptr pc, uptr sz) {
32168d75effSDimitry Andric   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, GetPageSizeCached()));
32268d75effSDimitry Andric }
32368d75effSDimitry Andric 
32468d75effSDimitry Andric void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz) {
32568d75effSDimitry Andric   uptr PageSize = GetPageSizeCached();
32668d75effSDimitry Andric   if (UNLIKELY(CheckForPvallocOverflow(sz, PageSize))) {
32768d75effSDimitry Andric     errno = errno_ENOMEM;
32868d75effSDimitry Andric     if (AllocatorMayReturnNull())
32968d75effSDimitry Andric       return nullptr;
33068d75effSDimitry Andric     GET_STACK_TRACE_FATAL(thr, pc);
33168d75effSDimitry Andric     ReportPvallocOverflow(sz, &stack);
33268d75effSDimitry Andric   }
33368d75effSDimitry Andric   // pvalloc(0) should allocate one page.
33468d75effSDimitry Andric   sz = sz ? RoundUpTo(sz, PageSize) : PageSize;
33568d75effSDimitry Andric   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, PageSize));
33668d75effSDimitry Andric }
33768d75effSDimitry Andric 
33868d75effSDimitry Andric uptr user_alloc_usable_size(const void *p) {
33968d75effSDimitry Andric   if (p == 0)
34068d75effSDimitry Andric     return 0;
34168d75effSDimitry Andric   MBlock *b = ctx->metamap.GetBlock((uptr)p);
34268d75effSDimitry Andric   if (!b)
34368d75effSDimitry Andric     return 0;  // Not a valid pointer.
34468d75effSDimitry Andric   if (b->siz == 0)
34568d75effSDimitry Andric     return 1;  // Zero-sized allocations are actually 1 byte.
34668d75effSDimitry Andric   return b->siz;
34768d75effSDimitry Andric }
34868d75effSDimitry Andric 
34968d75effSDimitry Andric void invoke_malloc_hook(void *ptr, uptr size) {
35068d75effSDimitry Andric   ThreadState *thr = cur_thread();
35168d75effSDimitry Andric   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
35268d75effSDimitry Andric     return;
35368d75effSDimitry Andric   __sanitizer_malloc_hook(ptr, size);
35468d75effSDimitry Andric   RunMallocHooks(ptr, size);
35568d75effSDimitry Andric }
35668d75effSDimitry Andric 
35768d75effSDimitry Andric void invoke_free_hook(void *ptr) {
35868d75effSDimitry Andric   ThreadState *thr = cur_thread();
35968d75effSDimitry Andric   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
36068d75effSDimitry Andric     return;
36168d75effSDimitry Andric   __sanitizer_free_hook(ptr);
36268d75effSDimitry Andric   RunFreeHooks(ptr);
36368d75effSDimitry Andric }
36468d75effSDimitry Andric 
365349cc55cSDimitry Andric void *Alloc(uptr sz) {
36668d75effSDimitry Andric   ThreadState *thr = cur_thread();
36768d75effSDimitry Andric   if (thr->nomalloc) {
36868d75effSDimitry Andric     thr->nomalloc = 0;  // CHECK calls internal_malloc().
36968d75effSDimitry Andric     CHECK(0);
37068d75effSDimitry Andric   }
371*4824e7fdSDimitry Andric   InternalAllocAccess();
37268d75effSDimitry Andric   return InternalAlloc(sz, &thr->proc()->internal_alloc_cache);
37368d75effSDimitry Andric }
37468d75effSDimitry Andric 
375349cc55cSDimitry Andric void FreeImpl(void *p) {
37668d75effSDimitry Andric   ThreadState *thr = cur_thread();
37768d75effSDimitry Andric   if (thr->nomalloc) {
37868d75effSDimitry Andric     thr->nomalloc = 0;  // CHECK calls internal_malloc().
37968d75effSDimitry Andric     CHECK(0);
38068d75effSDimitry Andric   }
381*4824e7fdSDimitry Andric   InternalAllocAccess();
38268d75effSDimitry Andric   InternalFree(p, &thr->proc()->internal_alloc_cache);
38368d75effSDimitry Andric }
38468d75effSDimitry Andric 
38568d75effSDimitry Andric }  // namespace __tsan
38668d75effSDimitry Andric 
38768d75effSDimitry Andric using namespace __tsan;
38868d75effSDimitry Andric 
38968d75effSDimitry Andric extern "C" {
39068d75effSDimitry Andric uptr __sanitizer_get_current_allocated_bytes() {
39168d75effSDimitry Andric   uptr stats[AllocatorStatCount];
39268d75effSDimitry Andric   allocator()->GetStats(stats);
39368d75effSDimitry Andric   return stats[AllocatorStatAllocated];
39468d75effSDimitry Andric }
39568d75effSDimitry Andric 
39668d75effSDimitry Andric uptr __sanitizer_get_heap_size() {
39768d75effSDimitry Andric   uptr stats[AllocatorStatCount];
39868d75effSDimitry Andric   allocator()->GetStats(stats);
39968d75effSDimitry Andric   return stats[AllocatorStatMapped];
40068d75effSDimitry Andric }
40168d75effSDimitry Andric 
40268d75effSDimitry Andric uptr __sanitizer_get_free_bytes() {
40368d75effSDimitry Andric   return 1;
40468d75effSDimitry Andric }
40568d75effSDimitry Andric 
40668d75effSDimitry Andric uptr __sanitizer_get_unmapped_bytes() {
40768d75effSDimitry Andric   return 1;
40868d75effSDimitry Andric }
40968d75effSDimitry Andric 
41068d75effSDimitry Andric uptr __sanitizer_get_estimated_allocated_size(uptr size) {
41168d75effSDimitry Andric   return size;
41268d75effSDimitry Andric }
41368d75effSDimitry Andric 
41468d75effSDimitry Andric int __sanitizer_get_ownership(const void *p) {
41568d75effSDimitry Andric   return allocator()->GetBlockBegin(p) != 0;
41668d75effSDimitry Andric }
41768d75effSDimitry Andric 
41868d75effSDimitry Andric uptr __sanitizer_get_allocated_size(const void *p) {
41968d75effSDimitry Andric   return user_alloc_usable_size(p);
42068d75effSDimitry Andric }
42168d75effSDimitry Andric 
42268d75effSDimitry Andric void __tsan_on_thread_idle() {
42368d75effSDimitry Andric   ThreadState *thr = cur_thread();
42468d75effSDimitry Andric   thr->clock.ResetCached(&thr->proc()->clock_cache);
42568d75effSDimitry Andric   thr->last_sleep_clock.ResetCached(&thr->proc()->clock_cache);
42668d75effSDimitry Andric   allocator()->SwallowCache(&thr->proc()->alloc_cache);
42768d75effSDimitry Andric   internal_allocator()->SwallowCache(&thr->proc()->internal_alloc_cache);
42868d75effSDimitry Andric   ctx->metamap.OnProcIdle(thr->proc());
42968d75effSDimitry Andric }
43068d75effSDimitry Andric }  // extern "C"
431