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; 724824e7fdSDimitry Andric // This mutex represents the internal allocator combined for 734824e7fdSDimitry Andric // the purposes of deadlock detection. The internal allocator 744824e7fdSDimitry Andric // uses multiple mutexes, moreover they are locked only occasionally 754824e7fdSDimitry Andric // and they are spin mutexes which don't support deadlock detection. 764824e7fdSDimitry Andric // So we use this fake mutex to serve as a substitute for these mutexes. 774824e7fdSDimitry Andric CheckedMutex internal_alloc_mtx; 7868d75effSDimitry Andric 794824e7fdSDimitry Andric GlobalProc() 804824e7fdSDimitry Andric : mtx(MutexTypeGlobalProc), 814824e7fdSDimitry Andric proc(ProcCreate()), 824824e7fdSDimitry 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 904824e7fdSDimitry Andric static void InternalAllocAccess() { 914824e7fdSDimitry Andric global_proc()->internal_alloc_mtx.Lock(); 924824e7fdSDimitry Andric global_proc()->internal_alloc_mtx.Unlock(); 934824e7fdSDimitry Andric } 944824e7fdSDimitry 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*04eeddc0SDimitry Andric void AllocatorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { 1284824e7fdSDimitry Andric global_proc()->internal_alloc_mtx.Lock(); 1294824e7fdSDimitry Andric InternalAllocatorLock(); 1304824e7fdSDimitry Andric } 1314824e7fdSDimitry Andric 132*04eeddc0SDimitry Andric void AllocatorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { 1334824e7fdSDimitry Andric InternalAllocatorUnlock(); 1344824e7fdSDimitry Andric global_proc()->internal_alloc_mtx.Unlock(); 1350eae32dcSDimitry Andric } 1360eae32dcSDimitry Andric 137*04eeddc0SDimitry Andric void GlobalProcessorLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { 1380eae32dcSDimitry Andric global_proc()->mtx.Lock(); 1390eae32dcSDimitry Andric } 1400eae32dcSDimitry Andric 141*04eeddc0SDimitry Andric void GlobalProcessorUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS { 1424824e7fdSDimitry Andric global_proc()->mtx.Unlock(); 1434824e7fdSDimitry Andric } 1444824e7fdSDimitry Andric 145480093f4SDimitry Andric static constexpr uptr kMaxAllowedMallocSize = 1ull << 40; 146480093f4SDimitry Andric static uptr max_user_defined_malloc_size; 147480093f4SDimitry Andric 14868d75effSDimitry Andric void InitializeAllocator() { 14968d75effSDimitry Andric SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null); 15068d75effSDimitry Andric allocator()->Init(common_flags()->allocator_release_to_os_interval_ms); 151480093f4SDimitry Andric max_user_defined_malloc_size = common_flags()->max_allocation_size_mb 152480093f4SDimitry Andric ? common_flags()->max_allocation_size_mb 153480093f4SDimitry Andric << 20 154480093f4SDimitry Andric : kMaxAllowedMallocSize; 15568d75effSDimitry Andric } 15668d75effSDimitry Andric 15768d75effSDimitry Andric void InitializeAllocatorLate() { 15868d75effSDimitry Andric new(global_proc()) GlobalProc(); 15968d75effSDimitry Andric } 16068d75effSDimitry Andric 16168d75effSDimitry Andric void AllocatorProcStart(Processor *proc) { 16268d75effSDimitry Andric allocator()->InitCache(&proc->alloc_cache); 16368d75effSDimitry Andric internal_allocator()->InitCache(&proc->internal_alloc_cache); 16468d75effSDimitry Andric } 16568d75effSDimitry Andric 16668d75effSDimitry Andric void AllocatorProcFinish(Processor *proc) { 16768d75effSDimitry Andric allocator()->DestroyCache(&proc->alloc_cache); 16868d75effSDimitry Andric internal_allocator()->DestroyCache(&proc->internal_alloc_cache); 16968d75effSDimitry Andric } 17068d75effSDimitry Andric 17168d75effSDimitry Andric void AllocatorPrintStats() { 17268d75effSDimitry Andric allocator()->PrintStats(); 17368d75effSDimitry Andric } 17468d75effSDimitry Andric 17568d75effSDimitry Andric static void SignalUnsafeCall(ThreadState *thr, uptr pc) { 17668d75effSDimitry Andric if (atomic_load_relaxed(&thr->in_signal_handler) == 0 || 177fe6060f1SDimitry Andric !ShouldReport(thr, ReportTypeSignalUnsafe)) 17868d75effSDimitry Andric return; 17968d75effSDimitry Andric VarSizeStackTrace stack; 18068d75effSDimitry Andric ObtainCurrentStack(thr, pc, &stack); 18168d75effSDimitry Andric if (IsFiredSuppression(ctx, ReportTypeSignalUnsafe, stack)) 18268d75effSDimitry Andric return; 183349cc55cSDimitry Andric ThreadRegistryLock l(&ctx->thread_registry); 18468d75effSDimitry Andric ScopedReport rep(ReportTypeSignalUnsafe); 18568d75effSDimitry Andric rep.AddStack(stack, true); 18668d75effSDimitry Andric OutputReport(thr, rep); 18768d75effSDimitry Andric } 18868d75effSDimitry Andric 18968d75effSDimitry Andric 19068d75effSDimitry Andric void *user_alloc_internal(ThreadState *thr, uptr pc, uptr sz, uptr align, 19168d75effSDimitry Andric bool signal) { 192480093f4SDimitry Andric if (sz >= kMaxAllowedMallocSize || align >= kMaxAllowedMallocSize || 193480093f4SDimitry Andric sz > max_user_defined_malloc_size) { 19468d75effSDimitry Andric if (AllocatorMayReturnNull()) 19568d75effSDimitry Andric return nullptr; 196480093f4SDimitry Andric uptr malloc_limit = 197480093f4SDimitry Andric Min(kMaxAllowedMallocSize, max_user_defined_malloc_size); 19868d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 199480093f4SDimitry Andric ReportAllocationSizeTooBig(sz, malloc_limit, &stack); 20068d75effSDimitry Andric } 2010eae32dcSDimitry Andric if (UNLIKELY(IsRssLimitExceeded())) { 2020eae32dcSDimitry Andric if (AllocatorMayReturnNull()) 2030eae32dcSDimitry Andric return nullptr; 2040eae32dcSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 2050eae32dcSDimitry Andric ReportRssLimitExceeded(&stack); 2060eae32dcSDimitry Andric } 20768d75effSDimitry Andric void *p = allocator()->Allocate(&thr->proc()->alloc_cache, sz, align); 20868d75effSDimitry Andric if (UNLIKELY(!p)) { 20968d75effSDimitry Andric SetAllocatorOutOfMemory(); 21068d75effSDimitry Andric if (AllocatorMayReturnNull()) 21168d75effSDimitry Andric return nullptr; 21268d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 21368d75effSDimitry Andric ReportOutOfMemory(sz, &stack); 21468d75effSDimitry Andric } 21568d75effSDimitry Andric if (ctx && ctx->initialized) 21668d75effSDimitry Andric OnUserAlloc(thr, pc, (uptr)p, sz, true); 21768d75effSDimitry Andric if (signal) 21868d75effSDimitry Andric SignalUnsafeCall(thr, pc); 21968d75effSDimitry Andric return p; 22068d75effSDimitry Andric } 22168d75effSDimitry Andric 22268d75effSDimitry Andric void user_free(ThreadState *thr, uptr pc, void *p, bool signal) { 22368d75effSDimitry Andric ScopedGlobalProcessor sgp; 22468d75effSDimitry Andric if (ctx && ctx->initialized) 22568d75effSDimitry Andric OnUserFree(thr, pc, (uptr)p, true); 22668d75effSDimitry Andric allocator()->Deallocate(&thr->proc()->alloc_cache, p); 22768d75effSDimitry Andric if (signal) 22868d75effSDimitry Andric SignalUnsafeCall(thr, pc); 22968d75effSDimitry Andric } 23068d75effSDimitry Andric 23168d75effSDimitry Andric void *user_alloc(ThreadState *thr, uptr pc, uptr sz) { 23268d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, kDefaultAlignment)); 23368d75effSDimitry Andric } 23468d75effSDimitry Andric 23568d75effSDimitry Andric void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) { 23668d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(size, n))) { 23768d75effSDimitry Andric if (AllocatorMayReturnNull()) 23868d75effSDimitry Andric return SetErrnoOnNull(nullptr); 23968d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 24068d75effSDimitry Andric ReportCallocOverflow(n, size, &stack); 24168d75effSDimitry Andric } 24268d75effSDimitry Andric void *p = user_alloc_internal(thr, pc, n * size); 24368d75effSDimitry Andric if (p) 24468d75effSDimitry Andric internal_memset(p, 0, n * size); 24568d75effSDimitry Andric return SetErrnoOnNull(p); 24668d75effSDimitry Andric } 24768d75effSDimitry Andric 24868d75effSDimitry Andric void *user_reallocarray(ThreadState *thr, uptr pc, void *p, uptr size, uptr n) { 24968d75effSDimitry Andric if (UNLIKELY(CheckForCallocOverflow(size, n))) { 25068d75effSDimitry Andric if (AllocatorMayReturnNull()) 25168d75effSDimitry Andric return SetErrnoOnNull(nullptr); 25268d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 25368d75effSDimitry Andric ReportReallocArrayOverflow(size, n, &stack); 25468d75effSDimitry Andric } 25568d75effSDimitry Andric return user_realloc(thr, pc, p, size * n); 25668d75effSDimitry Andric } 25768d75effSDimitry Andric 25868d75effSDimitry Andric void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) { 259349cc55cSDimitry Andric DPrintf("#%d: alloc(%zu) = 0x%zx\n", thr->tid, sz, p); 2600eae32dcSDimitry Andric // Note: this can run before thread initialization/after finalization. 2610eae32dcSDimitry Andric // As a result this is not necessarily synchronized with DoReset, 2620eae32dcSDimitry Andric // which iterates over and resets all sync objects, 2630eae32dcSDimitry Andric // but it is fine to create new MBlocks in this context. 26468d75effSDimitry Andric ctx->metamap.AllocBlock(thr, pc, p, sz); 2650eae32dcSDimitry Andric // If this runs before thread initialization/after finalization 2660eae32dcSDimitry Andric // and we don't have trace initialized, we can't imitate writes. 2670eae32dcSDimitry Andric // In such case just reset the shadow range, it is fine since 2680eae32dcSDimitry Andric // it affects only a small fraction of special objects. 2690eae32dcSDimitry Andric if (write && thr->ignore_reads_and_writes == 0 && 2700eae32dcSDimitry Andric atomic_load_relaxed(&thr->trace_pos)) 27168d75effSDimitry Andric MemoryRangeImitateWrite(thr, pc, (uptr)p, sz); 27268d75effSDimitry Andric else 27368d75effSDimitry Andric MemoryResetRange(thr, pc, (uptr)p, sz); 27468d75effSDimitry Andric } 27568d75effSDimitry Andric 27668d75effSDimitry Andric void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) { 27768d75effSDimitry Andric CHECK_NE(p, (void*)0); 2780eae32dcSDimitry Andric if (!thr->slot) { 2790eae32dcSDimitry Andric // Very early/late in thread lifetime, or during fork. 2800eae32dcSDimitry Andric UNUSED uptr sz = ctx->metamap.FreeBlock(thr->proc(), p, false); 2810eae32dcSDimitry Andric DPrintf("#%d: free(0x%zx, %zu) (no slot)\n", thr->tid, p, sz); 2820eae32dcSDimitry Andric return; 2830eae32dcSDimitry Andric } 2840eae32dcSDimitry Andric SlotLocker locker(thr); 2850eae32dcSDimitry Andric uptr sz = ctx->metamap.FreeBlock(thr->proc(), p, true); 286349cc55cSDimitry Andric DPrintf("#%d: free(0x%zx, %zu)\n", thr->tid, p, sz); 2870eae32dcSDimitry Andric if (write && thr->ignore_reads_and_writes == 0) 28868d75effSDimitry Andric MemoryRangeFreed(thr, pc, (uptr)p, sz); 28968d75effSDimitry Andric } 29068d75effSDimitry Andric 29168d75effSDimitry Andric void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) { 29268d75effSDimitry Andric // FIXME: Handle "shrinking" more efficiently, 29368d75effSDimitry Andric // it seems that some software actually does this. 29468d75effSDimitry Andric if (!p) 29568d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz)); 29668d75effSDimitry Andric if (!sz) { 29768d75effSDimitry Andric user_free(thr, pc, p); 29868d75effSDimitry Andric return nullptr; 29968d75effSDimitry Andric } 30068d75effSDimitry Andric void *new_p = user_alloc_internal(thr, pc, sz); 30168d75effSDimitry Andric if (new_p) { 30268d75effSDimitry Andric uptr old_sz = user_alloc_usable_size(p); 30368d75effSDimitry Andric internal_memcpy(new_p, p, min(old_sz, sz)); 30468d75effSDimitry Andric user_free(thr, pc, p); 30568d75effSDimitry Andric } 30668d75effSDimitry Andric return SetErrnoOnNull(new_p); 30768d75effSDimitry Andric } 30868d75effSDimitry Andric 30968d75effSDimitry Andric void *user_memalign(ThreadState *thr, uptr pc, uptr align, uptr sz) { 31068d75effSDimitry Andric if (UNLIKELY(!IsPowerOfTwo(align))) { 31168d75effSDimitry Andric errno = errno_EINVAL; 31268d75effSDimitry Andric if (AllocatorMayReturnNull()) 31368d75effSDimitry Andric return nullptr; 31468d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 31568d75effSDimitry Andric ReportInvalidAllocationAlignment(align, &stack); 31668d75effSDimitry Andric } 31768d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align)); 31868d75effSDimitry Andric } 31968d75effSDimitry Andric 32068d75effSDimitry Andric int user_posix_memalign(ThreadState *thr, uptr pc, void **memptr, uptr align, 32168d75effSDimitry Andric uptr sz) { 32268d75effSDimitry Andric if (UNLIKELY(!CheckPosixMemalignAlignment(align))) { 32368d75effSDimitry Andric if (AllocatorMayReturnNull()) 32468d75effSDimitry Andric return errno_EINVAL; 32568d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 32668d75effSDimitry Andric ReportInvalidPosixMemalignAlignment(align, &stack); 32768d75effSDimitry Andric } 32868d75effSDimitry Andric void *ptr = user_alloc_internal(thr, pc, sz, align); 32968d75effSDimitry Andric if (UNLIKELY(!ptr)) 33068d75effSDimitry Andric // OOM error is already taken care of by user_alloc_internal. 33168d75effSDimitry Andric return errno_ENOMEM; 33268d75effSDimitry Andric CHECK(IsAligned((uptr)ptr, align)); 33368d75effSDimitry Andric *memptr = ptr; 33468d75effSDimitry Andric return 0; 33568d75effSDimitry Andric } 33668d75effSDimitry Andric 33768d75effSDimitry Andric void *user_aligned_alloc(ThreadState *thr, uptr pc, uptr align, uptr sz) { 33868d75effSDimitry Andric if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(align, sz))) { 33968d75effSDimitry Andric errno = errno_EINVAL; 34068d75effSDimitry Andric if (AllocatorMayReturnNull()) 34168d75effSDimitry Andric return nullptr; 34268d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 34368d75effSDimitry Andric ReportInvalidAlignedAllocAlignment(sz, align, &stack); 34468d75effSDimitry Andric } 34568d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, align)); 34668d75effSDimitry Andric } 34768d75effSDimitry Andric 34868d75effSDimitry Andric void *user_valloc(ThreadState *thr, uptr pc, uptr sz) { 34968d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, GetPageSizeCached())); 35068d75effSDimitry Andric } 35168d75effSDimitry Andric 35268d75effSDimitry Andric void *user_pvalloc(ThreadState *thr, uptr pc, uptr sz) { 35368d75effSDimitry Andric uptr PageSize = GetPageSizeCached(); 35468d75effSDimitry Andric if (UNLIKELY(CheckForPvallocOverflow(sz, PageSize))) { 35568d75effSDimitry Andric errno = errno_ENOMEM; 35668d75effSDimitry Andric if (AllocatorMayReturnNull()) 35768d75effSDimitry Andric return nullptr; 35868d75effSDimitry Andric GET_STACK_TRACE_FATAL(thr, pc); 35968d75effSDimitry Andric ReportPvallocOverflow(sz, &stack); 36068d75effSDimitry Andric } 36168d75effSDimitry Andric // pvalloc(0) should allocate one page. 36268d75effSDimitry Andric sz = sz ? RoundUpTo(sz, PageSize) : PageSize; 36368d75effSDimitry Andric return SetErrnoOnNull(user_alloc_internal(thr, pc, sz, PageSize)); 36468d75effSDimitry Andric } 36568d75effSDimitry Andric 36668d75effSDimitry Andric uptr user_alloc_usable_size(const void *p) { 3670eae32dcSDimitry Andric if (p == 0 || !IsAppMem((uptr)p)) 36868d75effSDimitry Andric return 0; 36968d75effSDimitry Andric MBlock *b = ctx->metamap.GetBlock((uptr)p); 37068d75effSDimitry Andric if (!b) 37168d75effSDimitry Andric return 0; // Not a valid pointer. 37268d75effSDimitry Andric if (b->siz == 0) 37368d75effSDimitry Andric return 1; // Zero-sized allocations are actually 1 byte. 37468d75effSDimitry Andric return b->siz; 37568d75effSDimitry Andric } 37668d75effSDimitry Andric 37768d75effSDimitry Andric void invoke_malloc_hook(void *ptr, uptr size) { 37868d75effSDimitry Andric ThreadState *thr = cur_thread(); 37968d75effSDimitry Andric if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors) 38068d75effSDimitry Andric return; 38168d75effSDimitry Andric __sanitizer_malloc_hook(ptr, size); 38268d75effSDimitry Andric RunMallocHooks(ptr, size); 38368d75effSDimitry Andric } 38468d75effSDimitry Andric 38568d75effSDimitry Andric void invoke_free_hook(void *ptr) { 38668d75effSDimitry Andric ThreadState *thr = cur_thread(); 38768d75effSDimitry Andric if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors) 38868d75effSDimitry Andric return; 38968d75effSDimitry Andric __sanitizer_free_hook(ptr); 39068d75effSDimitry Andric RunFreeHooks(ptr); 39168d75effSDimitry Andric } 39268d75effSDimitry Andric 393349cc55cSDimitry Andric void *Alloc(uptr sz) { 39468d75effSDimitry Andric ThreadState *thr = cur_thread(); 39568d75effSDimitry Andric if (thr->nomalloc) { 39668d75effSDimitry Andric thr->nomalloc = 0; // CHECK calls internal_malloc(). 39768d75effSDimitry Andric CHECK(0); 39868d75effSDimitry Andric } 3994824e7fdSDimitry Andric InternalAllocAccess(); 40068d75effSDimitry Andric return InternalAlloc(sz, &thr->proc()->internal_alloc_cache); 40168d75effSDimitry Andric } 40268d75effSDimitry Andric 403349cc55cSDimitry Andric void FreeImpl(void *p) { 40468d75effSDimitry Andric ThreadState *thr = cur_thread(); 40568d75effSDimitry Andric if (thr->nomalloc) { 40668d75effSDimitry Andric thr->nomalloc = 0; // CHECK calls internal_malloc(). 40768d75effSDimitry Andric CHECK(0); 40868d75effSDimitry Andric } 4094824e7fdSDimitry Andric InternalAllocAccess(); 41068d75effSDimitry Andric InternalFree(p, &thr->proc()->internal_alloc_cache); 41168d75effSDimitry Andric } 41268d75effSDimitry Andric 41368d75effSDimitry Andric } // namespace __tsan 41468d75effSDimitry Andric 41568d75effSDimitry Andric using namespace __tsan; 41668d75effSDimitry Andric 41768d75effSDimitry Andric extern "C" { 41868d75effSDimitry Andric uptr __sanitizer_get_current_allocated_bytes() { 41968d75effSDimitry Andric uptr stats[AllocatorStatCount]; 42068d75effSDimitry Andric allocator()->GetStats(stats); 42168d75effSDimitry Andric return stats[AllocatorStatAllocated]; 42268d75effSDimitry Andric } 42368d75effSDimitry Andric 42468d75effSDimitry Andric uptr __sanitizer_get_heap_size() { 42568d75effSDimitry Andric uptr stats[AllocatorStatCount]; 42668d75effSDimitry Andric allocator()->GetStats(stats); 42768d75effSDimitry Andric return stats[AllocatorStatMapped]; 42868d75effSDimitry Andric } 42968d75effSDimitry Andric 43068d75effSDimitry Andric uptr __sanitizer_get_free_bytes() { 43168d75effSDimitry Andric return 1; 43268d75effSDimitry Andric } 43368d75effSDimitry Andric 43468d75effSDimitry Andric uptr __sanitizer_get_unmapped_bytes() { 43568d75effSDimitry Andric return 1; 43668d75effSDimitry Andric } 43768d75effSDimitry Andric 43868d75effSDimitry Andric uptr __sanitizer_get_estimated_allocated_size(uptr size) { 43968d75effSDimitry Andric return size; 44068d75effSDimitry Andric } 44168d75effSDimitry Andric 44268d75effSDimitry Andric int __sanitizer_get_ownership(const void *p) { 44368d75effSDimitry Andric return allocator()->GetBlockBegin(p) != 0; 44468d75effSDimitry Andric } 44568d75effSDimitry Andric 44668d75effSDimitry Andric uptr __sanitizer_get_allocated_size(const void *p) { 44768d75effSDimitry Andric return user_alloc_usable_size(p); 44868d75effSDimitry Andric } 44968d75effSDimitry Andric 45068d75effSDimitry Andric void __tsan_on_thread_idle() { 45168d75effSDimitry Andric ThreadState *thr = cur_thread(); 45268d75effSDimitry Andric allocator()->SwallowCache(&thr->proc()->alloc_cache); 45368d75effSDimitry Andric internal_allocator()->SwallowCache(&thr->proc()->internal_alloc_cache); 45468d75effSDimitry Andric ctx->metamap.OnProcIdle(thr->proc()); 45568d75effSDimitry Andric } 45668d75effSDimitry Andric } // extern "C" 457