110d565efSmrg //===-- tsan_mman.cc ------------------------------------------------------===//
210d565efSmrg //
310d565efSmrg // This file is distributed under the University of Illinois Open Source
410d565efSmrg // License. See LICENSE.TXT for details.
510d565efSmrg //
610d565efSmrg //===----------------------------------------------------------------------===//
710d565efSmrg //
810d565efSmrg // This file is a part of ThreadSanitizer (TSan), a race detector.
910d565efSmrg //
1010d565efSmrg //===----------------------------------------------------------------------===//
11c7a68eb7Smrg #include "sanitizer_common/sanitizer_allocator_checks.h"
1210d565efSmrg #include "sanitizer_common/sanitizer_allocator_interface.h"
13*0fc04c29Smrg #include "sanitizer_common/sanitizer_allocator_report.h"
1410d565efSmrg #include "sanitizer_common/sanitizer_common.h"
15c7a68eb7Smrg #include "sanitizer_common/sanitizer_errno.h"
1610d565efSmrg #include "sanitizer_common/sanitizer_placement_new.h"
1710d565efSmrg #include "tsan_mman.h"
1810d565efSmrg #include "tsan_rtl.h"
1910d565efSmrg #include "tsan_report.h"
2010d565efSmrg #include "tsan_flags.h"
2110d565efSmrg 
2210d565efSmrg // May be overriden by front-end.
2310d565efSmrg SANITIZER_WEAK_DEFAULT_IMPL
__sanitizer_malloc_hook(void * ptr,uptr size)2410d565efSmrg void __sanitizer_malloc_hook(void *ptr, uptr size) {
2510d565efSmrg   (void)ptr;
2610d565efSmrg   (void)size;
2710d565efSmrg }
2810d565efSmrg 
2910d565efSmrg SANITIZER_WEAK_DEFAULT_IMPL
__sanitizer_free_hook(void * ptr)3010d565efSmrg void __sanitizer_free_hook(void *ptr) {
3110d565efSmrg   (void)ptr;
3210d565efSmrg }
3310d565efSmrg 
3410d565efSmrg namespace __tsan {
3510d565efSmrg 
3610d565efSmrg struct MapUnmapCallback {
OnMap__tsan::MapUnmapCallback3710d565efSmrg   void OnMap(uptr p, uptr size) const { }
OnUnmap__tsan::MapUnmapCallback3810d565efSmrg   void OnUnmap(uptr p, uptr size) const {
3910d565efSmrg     // We are about to unmap a chunk of user memory.
4010d565efSmrg     // Mark the corresponding shadow memory as not needed.
4110d565efSmrg     DontNeedShadowFor(p, size);
4210d565efSmrg     // Mark the corresponding meta shadow memory as not needed.
4310d565efSmrg     // Note the block does not contain any meta info at this point
4410d565efSmrg     // (this happens after free).
4510d565efSmrg     const uptr kMetaRatio = kMetaShadowCell / kMetaShadowSize;
4610d565efSmrg     const uptr kPageSize = GetPageSizeCached() * kMetaRatio;
4710d565efSmrg     // Block came from LargeMmapAllocator, so must be large.
4810d565efSmrg     // We rely on this in the calculations below.
4910d565efSmrg     CHECK_GE(size, 2 * kPageSize);
5010d565efSmrg     uptr diff = RoundUp(p, kPageSize) - p;
5110d565efSmrg     if (diff != 0) {
5210d565efSmrg       p += diff;
5310d565efSmrg       size -= diff;
5410d565efSmrg     }
5510d565efSmrg     diff = p + size - RoundDown(p + size, kPageSize);
5610d565efSmrg     if (diff != 0)
5710d565efSmrg       size -= diff;
58c7a68eb7Smrg     uptr p_meta = (uptr)MemToMeta(p);
59c7a68eb7Smrg     ReleaseMemoryPagesToOS(p_meta, p_meta + size / kMetaRatio);
6010d565efSmrg   }
6110d565efSmrg };
6210d565efSmrg 
6310d565efSmrg static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
allocator()6410d565efSmrg Allocator *allocator() {
6510d565efSmrg   return reinterpret_cast<Allocator*>(&allocator_placeholder);
6610d565efSmrg }
6710d565efSmrg 
6810d565efSmrg struct GlobalProc {
6910d565efSmrg   Mutex mtx;
7010d565efSmrg   Processor *proc;
7110d565efSmrg 
GlobalProc__tsan::GlobalProc7210d565efSmrg   GlobalProc()
7310d565efSmrg       : mtx(MutexTypeGlobalProc, StatMtxGlobalProc)
7410d565efSmrg       , proc(ProcCreate()) {
7510d565efSmrg   }
7610d565efSmrg };
7710d565efSmrg 
7810d565efSmrg static char global_proc_placeholder[sizeof(GlobalProc)] ALIGNED(64);
global_proc()7910d565efSmrg GlobalProc *global_proc() {
8010d565efSmrg   return reinterpret_cast<GlobalProc*>(&global_proc_placeholder);
8110d565efSmrg }
8210d565efSmrg 
ScopedGlobalProcessor()8310d565efSmrg ScopedGlobalProcessor::ScopedGlobalProcessor() {
8410d565efSmrg   GlobalProc *gp = global_proc();
8510d565efSmrg   ThreadState *thr = cur_thread();
8610d565efSmrg   if (thr->proc())
8710d565efSmrg     return;
8810d565efSmrg   // If we don't have a proc, use the global one.
8910d565efSmrg   // There are currently only two known case where this path is triggered:
9010d565efSmrg   //   __interceptor_free
9110d565efSmrg   //   __nptl_deallocate_tsd
9210d565efSmrg   //   start_thread
9310d565efSmrg   //   clone
9410d565efSmrg   // and:
9510d565efSmrg   //   ResetRange
9610d565efSmrg   //   __interceptor_munmap
9710d565efSmrg   //   __deallocate_stack
9810d565efSmrg   //   start_thread
9910d565efSmrg   //   clone
10010d565efSmrg   // Ideally, we destroy thread state (and unwire proc) when a thread actually
10110d565efSmrg   // exits (i.e. when we join/wait it). Then we would not need the global proc
10210d565efSmrg   gp->mtx.Lock();
10310d565efSmrg   ProcWire(gp->proc, thr);
10410d565efSmrg }
10510d565efSmrg 
~ScopedGlobalProcessor()10610d565efSmrg ScopedGlobalProcessor::~ScopedGlobalProcessor() {
10710d565efSmrg   GlobalProc *gp = global_proc();
10810d565efSmrg   ThreadState *thr = cur_thread();
10910d565efSmrg   if (thr->proc() != gp->proc)
11010d565efSmrg     return;
11110d565efSmrg   ProcUnwire(gp->proc, thr);
11210d565efSmrg   gp->mtx.Unlock();
11310d565efSmrg }
11410d565efSmrg 
InitializeAllocator()11510d565efSmrg void InitializeAllocator() {
116c7a68eb7Smrg   SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
117c7a68eb7Smrg   allocator()->Init(common_flags()->allocator_release_to_os_interval_ms);
11810d565efSmrg }
11910d565efSmrg 
InitializeAllocatorLate()12010d565efSmrg void InitializeAllocatorLate() {
12110d565efSmrg   new(global_proc()) GlobalProc();
12210d565efSmrg }
12310d565efSmrg 
AllocatorProcStart(Processor * proc)12410d565efSmrg void AllocatorProcStart(Processor *proc) {
12510d565efSmrg   allocator()->InitCache(&proc->alloc_cache);
12610d565efSmrg   internal_allocator()->InitCache(&proc->internal_alloc_cache);
12710d565efSmrg }
12810d565efSmrg 
AllocatorProcFinish(Processor * proc)12910d565efSmrg void AllocatorProcFinish(Processor *proc) {
13010d565efSmrg   allocator()->DestroyCache(&proc->alloc_cache);
13110d565efSmrg   internal_allocator()->DestroyCache(&proc->internal_alloc_cache);
13210d565efSmrg }
13310d565efSmrg 
AllocatorPrintStats()13410d565efSmrg void AllocatorPrintStats() {
13510d565efSmrg   allocator()->PrintStats();
13610d565efSmrg }
13710d565efSmrg 
SignalUnsafeCall(ThreadState * thr,uptr pc)13810d565efSmrg static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
13910d565efSmrg   if (atomic_load_relaxed(&thr->in_signal_handler) == 0 ||
14010d565efSmrg       !flags()->report_signal_unsafe)
14110d565efSmrg     return;
14210d565efSmrg   VarSizeStackTrace stack;
14310d565efSmrg   ObtainCurrentStack(thr, pc, &stack);
14410d565efSmrg   if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack))
14510d565efSmrg     return;
14610d565efSmrg   ThreadRegistryLock l(ctx->thread_registry);
14710d565efSmrg   ScopedReport rep(ReportTypeSignalUnsafe);
14810d565efSmrg   rep.AddStack(stack, true);
14910d565efSmrg   OutputReport(thr, rep);
15010d565efSmrg }
15110d565efSmrg 
152*0fc04c29Smrg static constexpr uptr kMaxAllowedMallocSize = 1ull << 40;
153*0fc04c29Smrg 
user_alloc_internal(ThreadState * thr,uptr pc,uptr sz,uptr align,bool signal)154c7a68eb7Smrg void *user_alloc_internal(ThreadState *thr, uptr pc, uptr sz, uptr align,
155c7a68eb7Smrg                           bool signal) {
156*0fc04c29Smrg   if (sz >= kMaxAllowedMallocSize || align >= kMaxAllowedMallocSize) {
157*0fc04c29Smrg     if (AllocatorMayReturnNull())
158*0fc04c29Smrg       return nullptr;
159*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
160*0fc04c29Smrg     ReportAllocationSizeTooBig(sz, kMaxAllowedMallocSize, &stack);
161*0fc04c29Smrg   }
16210d565efSmrg   void *p = allocator()->Allocate(&thr->proc()->alloc_cache, sz, align);
163*0fc04c29Smrg   if (UNLIKELY(!p)) {
164*0fc04c29Smrg     SetAllocatorOutOfMemory();
165*0fc04c29Smrg     if (AllocatorMayReturnNull())
166*0fc04c29Smrg       return nullptr;
167*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
168*0fc04c29Smrg     ReportOutOfMemory(sz, &stack);
169*0fc04c29Smrg   }
17010d565efSmrg   if (ctx && ctx->initialized)
17110d565efSmrg     OnUserAlloc(thr, pc, (uptr)p, sz, true);
17210d565efSmrg   if (signal)
17310d565efSmrg     SignalUnsafeCall(thr, pc);
17410d565efSmrg   return p;
17510d565efSmrg }
17610d565efSmrg 
user_free(ThreadState * thr,uptr pc,void * p,bool signal)17710d565efSmrg void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
17810d565efSmrg   ScopedGlobalProcessor sgp;
17910d565efSmrg   if (ctx && ctx->initialized)
18010d565efSmrg     OnUserFree(thr, pc, (uptr)p, true);
18110d565efSmrg   allocator()->Deallocate(&thr->proc()->alloc_cache, p);
18210d565efSmrg   if (signal)
18310d565efSmrg     SignalUnsafeCall(thr, pc);
18410d565efSmrg }
18510d565efSmrg 
user_alloc(ThreadState * thr,uptr pc,uptr sz)186c7a68eb7Smrg void *user_alloc(ThreadState *thr, uptr pc, uptr sz) {
187c7a68eb7Smrg   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, kDefaultAlignment));
188c7a68eb7Smrg }
189c7a68eb7Smrg 
user_calloc(ThreadState * thr,uptr pc,uptr size,uptr n)190c7a68eb7Smrg void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
191*0fc04c29Smrg   if (UNLIKELY(CheckForCallocOverflow(size, n))) {
192*0fc04c29Smrg     if (AllocatorMayReturnNull())
193*0fc04c29Smrg       return SetErrnoOnNull(nullptr);
194*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
195*0fc04c29Smrg     ReportCallocOverflow(n, size, &stack);
196*0fc04c29Smrg   }
197c7a68eb7Smrg   void *p = user_alloc_internal(thr, pc, n * size);
198c7a68eb7Smrg   if (p)
199c7a68eb7Smrg     internal_memset(p, 0, n * size);
200c7a68eb7Smrg   return SetErrnoOnNull(p);
201c7a68eb7Smrg }
202c7a68eb7Smrg 
OnUserAlloc(ThreadState * thr,uptr pc,uptr p,uptr sz,bool write)20310d565efSmrg void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
20410d565efSmrg   DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
20510d565efSmrg   ctx->metamap.AllocBlock(thr, pc, p, sz);
20610d565efSmrg   if (write && thr->ignore_reads_and_writes == 0)
20710d565efSmrg     MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
20810d565efSmrg   else
20910d565efSmrg     MemoryResetRange(thr, pc, (uptr)p, sz);
21010d565efSmrg }
21110d565efSmrg 
OnUserFree(ThreadState * thr,uptr pc,uptr p,bool write)21210d565efSmrg void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
21310d565efSmrg   CHECK_NE(p, (void*)0);
21410d565efSmrg   uptr sz = ctx->metamap.FreeBlock(thr->proc(), p);
21510d565efSmrg   DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
21610d565efSmrg   if (write && thr->ignore_reads_and_writes == 0)
21710d565efSmrg     MemoryRangeFreed(thr, pc, (uptr)p, sz);
21810d565efSmrg }
21910d565efSmrg 
user_realloc(ThreadState * thr,uptr pc,void * p,uptr sz)22010d565efSmrg void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
22110d565efSmrg   // FIXME: Handle "shrinking" more efficiently,
22210d565efSmrg   // it seems that some software actually does this.
223c7a68eb7Smrg   if (!p)
224c7a68eb7Smrg     return SetErrnoOnNull(user_alloc_internal(thr, pc, sz));
225c7a68eb7Smrg   if (!sz) {
226c7a68eb7Smrg     user_free(thr, pc, p);
227c7a68eb7Smrg     return nullptr;
228c7a68eb7Smrg   }
229c7a68eb7Smrg   void *new_p = user_alloc_internal(thr, pc, sz);
230c7a68eb7Smrg   if (new_p) {
231c7a68eb7Smrg     uptr old_sz = user_alloc_usable_size(p);
232c7a68eb7Smrg     internal_memcpy(new_p, p, min(old_sz, sz));
23310d565efSmrg     user_free(thr, pc, p);
23410d565efSmrg   }
235c7a68eb7Smrg   return SetErrnoOnNull(new_p);
236c7a68eb7Smrg }
237c7a68eb7Smrg 
user_memalign(ThreadState * thr,uptr pc,uptr align,uptr sz)238c7a68eb7Smrg void *user_memalign(ThreadState *thr, uptr pc, uptr align, uptr sz) {
239c7a68eb7Smrg   if (UNLIKELY(!IsPowerOfTwo(align))) {
240c7a68eb7Smrg     errno = errno_EINVAL;
241*0fc04c29Smrg     if (AllocatorMayReturnNull())
242*0fc04c29Smrg       return nullptr;
243*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
244*0fc04c29Smrg     ReportInvalidAllocationAlignment(align, &stack);
245c7a68eb7Smrg   }
246c7a68eb7Smrg   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align));
247c7a68eb7Smrg }
248c7a68eb7Smrg 
user_posix_memalign(ThreadState * thr,uptr pc,void ** memptr,uptr align,uptr sz)249c7a68eb7Smrg int user_posix_memalign(ThreadState *thr, uptr pc, void **memptr, uptr align,
250c7a68eb7Smrg                         uptr sz) {
251c7a68eb7Smrg   if (UNLIKELY(!CheckPosixMemalignAlignment(align))) {
252*0fc04c29Smrg     if (AllocatorMayReturnNull())
253c7a68eb7Smrg       return errno_EINVAL;
254*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
255*0fc04c29Smrg     ReportInvalidPosixMemalignAlignment(align, &stack);
256c7a68eb7Smrg   }
257c7a68eb7Smrg   void *ptr = user_alloc_internal(thr, pc, sz, align);
258c7a68eb7Smrg   if (UNLIKELY(!ptr))
259*0fc04c29Smrg     // OOM error is already taken care of by user_alloc_internal.
260c7a68eb7Smrg     return errno_ENOMEM;
261c7a68eb7Smrg   CHECK(IsAligned((uptr)ptr, align));
262c7a68eb7Smrg   *memptr = ptr;
263c7a68eb7Smrg   return 0;
264c7a68eb7Smrg }
265c7a68eb7Smrg 
user_aligned_alloc(ThreadState * thr,uptr pc,uptr align,uptr sz)266c7a68eb7Smrg void *user_aligned_alloc(ThreadState *thr, uptr pc, uptr align, uptr sz) {
267c7a68eb7Smrg   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(align, sz))) {
268c7a68eb7Smrg     errno = errno_EINVAL;
269*0fc04c29Smrg     if (AllocatorMayReturnNull())
270*0fc04c29Smrg       return nullptr;
271*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
272*0fc04c29Smrg     ReportInvalidAlignedAllocAlignment(sz, align, &stack);
273c7a68eb7Smrg   }
274c7a68eb7Smrg   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align));
275c7a68eb7Smrg }
276c7a68eb7Smrg 
user_valloc(ThreadState * thr,uptr pc,uptr sz)277c7a68eb7Smrg void *user_valloc(ThreadState *thr, uptr pc, uptr sz) {
278c7a68eb7Smrg   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, GetPageSizeCached()));
279c7a68eb7Smrg }
280c7a68eb7Smrg 
user_pvalloc(ThreadState * thr,uptr pc,uptr sz)281c7a68eb7Smrg void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz) {
282c7a68eb7Smrg   uptr PageSize = GetPageSizeCached();
283c7a68eb7Smrg   if (UNLIKELY(CheckForPvallocOverflow(sz, PageSize))) {
284c7a68eb7Smrg     errno = errno_ENOMEM;
285*0fc04c29Smrg     if (AllocatorMayReturnNull())
286*0fc04c29Smrg       return nullptr;
287*0fc04c29Smrg     GET_STACK_TRACE_FATAL(thr, pc);
288*0fc04c29Smrg     ReportPvallocOverflow(sz, &stack);
289c7a68eb7Smrg   }
290c7a68eb7Smrg   // pvalloc(0) should allocate one page.
291c7a68eb7Smrg   sz = sz ? RoundUpTo(sz, PageSize) : PageSize;
292c7a68eb7Smrg   return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, PageSize));
29310d565efSmrg }
29410d565efSmrg 
user_alloc_usable_size(const void * p)29510d565efSmrg uptr user_alloc_usable_size(const void *p) {
29610d565efSmrg   if (p == 0)
29710d565efSmrg     return 0;
29810d565efSmrg   MBlock *b = ctx->metamap.GetBlock((uptr)p);
29910d565efSmrg   if (!b)
30010d565efSmrg     return 0;  // Not a valid pointer.
30110d565efSmrg   if (b->siz == 0)
30210d565efSmrg     return 1;  // Zero-sized allocations are actually 1 byte.
30310d565efSmrg   return b->siz;
30410d565efSmrg }
30510d565efSmrg 
invoke_malloc_hook(void * ptr,uptr size)30610d565efSmrg void invoke_malloc_hook(void *ptr, uptr size) {
30710d565efSmrg   ThreadState *thr = cur_thread();
30810d565efSmrg   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
30910d565efSmrg     return;
31010d565efSmrg   __sanitizer_malloc_hook(ptr, size);
31110d565efSmrg   RunMallocHooks(ptr, size);
31210d565efSmrg }
31310d565efSmrg 
invoke_free_hook(void * ptr)31410d565efSmrg void invoke_free_hook(void *ptr) {
31510d565efSmrg   ThreadState *thr = cur_thread();
31610d565efSmrg   if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
31710d565efSmrg     return;
31810d565efSmrg   __sanitizer_free_hook(ptr);
31910d565efSmrg   RunFreeHooks(ptr);
32010d565efSmrg }
32110d565efSmrg 
internal_alloc(MBlockType typ,uptr sz)32210d565efSmrg void *internal_alloc(MBlockType typ, uptr sz) {
32310d565efSmrg   ThreadState *thr = cur_thread();
32410d565efSmrg   if (thr->nomalloc) {
32510d565efSmrg     thr->nomalloc = 0;  // CHECK calls internal_malloc().
32610d565efSmrg     CHECK(0);
32710d565efSmrg   }
32810d565efSmrg   return InternalAlloc(sz, &thr->proc()->internal_alloc_cache);
32910d565efSmrg }
33010d565efSmrg 
internal_free(void * p)33110d565efSmrg void internal_free(void *p) {
33210d565efSmrg   ThreadState *thr = cur_thread();
33310d565efSmrg   if (thr->nomalloc) {
33410d565efSmrg     thr->nomalloc = 0;  // CHECK calls internal_malloc().
33510d565efSmrg     CHECK(0);
33610d565efSmrg   }
33710d565efSmrg   InternalFree(p, &thr->proc()->internal_alloc_cache);
33810d565efSmrg }
33910d565efSmrg 
34010d565efSmrg }  // namespace __tsan
34110d565efSmrg 
34210d565efSmrg using namespace __tsan;
34310d565efSmrg 
34410d565efSmrg extern "C" {
__sanitizer_get_current_allocated_bytes()34510d565efSmrg uptr __sanitizer_get_current_allocated_bytes() {
34610d565efSmrg   uptr stats[AllocatorStatCount];
34710d565efSmrg   allocator()->GetStats(stats);
34810d565efSmrg   return stats[AllocatorStatAllocated];
34910d565efSmrg }
35010d565efSmrg 
__sanitizer_get_heap_size()35110d565efSmrg uptr __sanitizer_get_heap_size() {
35210d565efSmrg   uptr stats[AllocatorStatCount];
35310d565efSmrg   allocator()->GetStats(stats);
35410d565efSmrg   return stats[AllocatorStatMapped];
35510d565efSmrg }
35610d565efSmrg 
__sanitizer_get_free_bytes()35710d565efSmrg uptr __sanitizer_get_free_bytes() {
35810d565efSmrg   return 1;
35910d565efSmrg }
36010d565efSmrg 
__sanitizer_get_unmapped_bytes()36110d565efSmrg uptr __sanitizer_get_unmapped_bytes() {
36210d565efSmrg   return 1;
36310d565efSmrg }
36410d565efSmrg 
__sanitizer_get_estimated_allocated_size(uptr size)36510d565efSmrg uptr __sanitizer_get_estimated_allocated_size(uptr size) {
36610d565efSmrg   return size;
36710d565efSmrg }
36810d565efSmrg 
__sanitizer_get_ownership(const void * p)36910d565efSmrg int __sanitizer_get_ownership(const void *p) {
37010d565efSmrg   return allocator()->GetBlockBegin(p) != 0;
37110d565efSmrg }
37210d565efSmrg 
__sanitizer_get_allocated_size(const void * p)37310d565efSmrg uptr __sanitizer_get_allocated_size(const void *p) {
37410d565efSmrg   return user_alloc_usable_size(p);
37510d565efSmrg }
37610d565efSmrg 
__tsan_on_thread_idle()37710d565efSmrg void __tsan_on_thread_idle() {
37810d565efSmrg   ThreadState *thr = cur_thread();
379c7a68eb7Smrg   thr->clock.ResetCached(&thr->proc()->clock_cache);
380c7a68eb7Smrg   thr->last_sleep_clock.ResetCached(&thr->proc()->clock_cache);
38110d565efSmrg   allocator()->SwallowCache(&thr->proc()->alloc_cache);
38210d565efSmrg   internal_allocator()->SwallowCache(&thr->proc()->internal_alloc_cache);
38310d565efSmrg   ctx->metamap.OnProcIdle(thr->proc());
38410d565efSmrg }
38510d565efSmrg }  // extern "C"
386