160c66db4SNico Weber //===-- msan_allocator.cpp -------------------------- ---------------------===//
260c66db4SNico Weber //
360c66db4SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
460c66db4SNico Weber // See https://llvm.org/LICENSE.txt for license information.
560c66db4SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
660c66db4SNico Weber //
760c66db4SNico Weber //===----------------------------------------------------------------------===//
860c66db4SNico Weber //
960c66db4SNico Weber // This file is a part of MemorySanitizer.
1060c66db4SNico Weber //
1160c66db4SNico Weber // MemorySanitizer allocator.
1260c66db4SNico Weber //===----------------------------------------------------------------------===//
1360c66db4SNico Weber
1460c66db4SNico Weber #include "sanitizer_common/sanitizer_allocator.h"
1560c66db4SNico Weber #include "sanitizer_common/sanitizer_allocator_checks.h"
1660c66db4SNico Weber #include "sanitizer_common/sanitizer_allocator_interface.h"
1760c66db4SNico Weber #include "sanitizer_common/sanitizer_allocator_report.h"
1860c66db4SNico Weber #include "sanitizer_common/sanitizer_errno.h"
1960c66db4SNico Weber #include "msan.h"
2060c66db4SNico Weber #include "msan_allocator.h"
2160c66db4SNico Weber #include "msan_origin.h"
2260c66db4SNico Weber #include "msan_thread.h"
2360c66db4SNico Weber #include "msan_poisoning.h"
2460c66db4SNico Weber
2560c66db4SNico Weber namespace __msan {
2660c66db4SNico Weber
2760c66db4SNico Weber struct Metadata {
2860c66db4SNico Weber uptr requested_size;
2960c66db4SNico Weber };
3060c66db4SNico Weber
3160c66db4SNico Weber struct MsanMapUnmapCallback {
OnMap__msan::MsanMapUnmapCallback3260c66db4SNico Weber void OnMap(uptr p, uptr size) const {}
OnUnmap__msan::MsanMapUnmapCallback3360c66db4SNico Weber void OnUnmap(uptr p, uptr size) const {
3460c66db4SNico Weber __msan_unpoison((void *)p, size);
3560c66db4SNico Weber
3660c66db4SNico Weber // We are about to unmap a chunk of user memory.
3760c66db4SNico Weber // Mark the corresponding shadow memory as not needed.
3860c66db4SNico Weber uptr shadow_p = MEM_TO_SHADOW(p);
3960c66db4SNico Weber ReleaseMemoryPagesToOS(shadow_p, shadow_p + size);
4060c66db4SNico Weber if (__msan_get_track_origins()) {
4160c66db4SNico Weber uptr origin_p = MEM_TO_ORIGIN(p);
4260c66db4SNico Weber ReleaseMemoryPagesToOS(origin_p, origin_p + size);
4360c66db4SNico Weber }
4460c66db4SNico Weber }
4560c66db4SNico Weber };
4660c66db4SNico Weber
4760c66db4SNico Weber #if defined(__mips64)
4860c66db4SNico Weber static const uptr kMaxAllowedMallocSize = 2UL << 30;
4960c66db4SNico Weber
5060c66db4SNico Weber struct AP32 {
5160c66db4SNico Weber static const uptr kSpaceBeg = 0;
5260c66db4SNico Weber static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
5360c66db4SNico Weber static const uptr kMetadataSize = sizeof(Metadata);
5460c66db4SNico Weber typedef __sanitizer::CompactSizeClassMap SizeClassMap;
5560c66db4SNico Weber static const uptr kRegionSizeLog = 20;
5660c66db4SNico Weber using AddressSpaceView = LocalAddressSpaceView;
5760c66db4SNico Weber typedef MsanMapUnmapCallback MapUnmapCallback;
5860c66db4SNico Weber static const uptr kFlags = 0;
5960c66db4SNico Weber };
6060c66db4SNico Weber typedef SizeClassAllocator32<AP32> PrimaryAllocator;
6160c66db4SNico Weber #elif defined(__x86_64__)
6260c66db4SNico Weber #if SANITIZER_NETBSD || \
6360c66db4SNico Weber (SANITIZER_LINUX && !defined(MSAN_LINUX_X86_64_OLD_MAPPING))
6460c66db4SNico Weber static const uptr kAllocatorSpace = 0x700000000000ULL;
6560c66db4SNico Weber #else
6660c66db4SNico Weber static const uptr kAllocatorSpace = 0x600000000000ULL;
6760c66db4SNico Weber #endif
6860c66db4SNico Weber static const uptr kMaxAllowedMallocSize = 8UL << 30;
6960c66db4SNico Weber
7060c66db4SNico Weber struct AP64 { // Allocator64 parameters. Deliberately using a short name.
7160c66db4SNico Weber static const uptr kSpaceBeg = kAllocatorSpace;
7260c66db4SNico Weber static const uptr kSpaceSize = 0x40000000000; // 4T.
7360c66db4SNico Weber static const uptr kMetadataSize = sizeof(Metadata);
7460c66db4SNico Weber typedef DefaultSizeClassMap SizeClassMap;
7560c66db4SNico Weber typedef MsanMapUnmapCallback MapUnmapCallback;
7660c66db4SNico Weber static const uptr kFlags = 0;
7760c66db4SNico Weber using AddressSpaceView = LocalAddressSpaceView;
7860c66db4SNico Weber };
7960c66db4SNico Weber
8060c66db4SNico Weber typedef SizeClassAllocator64<AP64> PrimaryAllocator;
8160c66db4SNico Weber
8260c66db4SNico Weber #elif defined(__powerpc64__)
8360c66db4SNico Weber static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
8460c66db4SNico Weber
8560c66db4SNico Weber struct AP64 { // Allocator64 parameters. Deliberately using a short name.
8660c66db4SNico Weber static const uptr kSpaceBeg = 0x300000000000;
8760c66db4SNico Weber static const uptr kSpaceSize = 0x020000000000; // 2T.
8860c66db4SNico Weber static const uptr kMetadataSize = sizeof(Metadata);
8960c66db4SNico Weber typedef DefaultSizeClassMap SizeClassMap;
9060c66db4SNico Weber typedef MsanMapUnmapCallback MapUnmapCallback;
9160c66db4SNico Weber static const uptr kFlags = 0;
9260c66db4SNico Weber using AddressSpaceView = LocalAddressSpaceView;
9360c66db4SNico Weber };
9460c66db4SNico Weber
9560c66db4SNico Weber typedef SizeClassAllocator64<AP64> PrimaryAllocator;
96921009e6SIlya Leoshkevich #elif defined(__s390x__)
97921009e6SIlya Leoshkevich static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
98921009e6SIlya Leoshkevich
99921009e6SIlya Leoshkevich struct AP64 { // Allocator64 parameters. Deliberately using a short name.
100921009e6SIlya Leoshkevich static const uptr kSpaceBeg = 0x440000000000;
101921009e6SIlya Leoshkevich static const uptr kSpaceSize = 0x020000000000; // 2T.
102921009e6SIlya Leoshkevich static const uptr kMetadataSize = sizeof(Metadata);
103921009e6SIlya Leoshkevich typedef DefaultSizeClassMap SizeClassMap;
104921009e6SIlya Leoshkevich typedef MsanMapUnmapCallback MapUnmapCallback;
105921009e6SIlya Leoshkevich static const uptr kFlags = 0;
106921009e6SIlya Leoshkevich using AddressSpaceView = LocalAddressSpaceView;
107921009e6SIlya Leoshkevich };
108921009e6SIlya Leoshkevich
109921009e6SIlya Leoshkevich typedef SizeClassAllocator64<AP64> PrimaryAllocator;
11060c66db4SNico Weber #elif defined(__aarch64__)
11160c66db4SNico Weber static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
11260c66db4SNico Weber
11360c66db4SNico Weber struct AP32 {
11460c66db4SNico Weber static const uptr kSpaceBeg = 0;
11560c66db4SNico Weber static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
11660c66db4SNico Weber static const uptr kMetadataSize = sizeof(Metadata);
11760c66db4SNico Weber typedef __sanitizer::CompactSizeClassMap SizeClassMap;
11860c66db4SNico Weber static const uptr kRegionSizeLog = 20;
11960c66db4SNico Weber using AddressSpaceView = LocalAddressSpaceView;
12060c66db4SNico Weber typedef MsanMapUnmapCallback MapUnmapCallback;
12160c66db4SNico Weber static const uptr kFlags = 0;
12260c66db4SNico Weber };
12360c66db4SNico Weber typedef SizeClassAllocator32<AP32> PrimaryAllocator;
12460c66db4SNico Weber #endif
12560c66db4SNico Weber typedef CombinedAllocator<PrimaryAllocator> Allocator;
12660c66db4SNico Weber typedef Allocator::AllocatorCache AllocatorCache;
12760c66db4SNico Weber
12860c66db4SNico Weber static Allocator allocator;
12960c66db4SNico Weber static AllocatorCache fallback_allocator_cache;
13060c66db4SNico Weber static StaticSpinMutex fallback_mutex;
13160c66db4SNico Weber
1327904bd94SMatt Morehouse static uptr max_malloc_size;
1337904bd94SMatt Morehouse
MsanAllocatorInit()13460c66db4SNico Weber void MsanAllocatorInit() {
13560c66db4SNico Weber SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
13660c66db4SNico Weber allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
1377904bd94SMatt Morehouse if (common_flags()->max_allocation_size_mb)
1387904bd94SMatt Morehouse max_malloc_size = Min(common_flags()->max_allocation_size_mb << 20,
1397904bd94SMatt Morehouse kMaxAllowedMallocSize);
1407904bd94SMatt Morehouse else
1417904bd94SMatt Morehouse max_malloc_size = kMaxAllowedMallocSize;
14260c66db4SNico Weber }
14360c66db4SNico Weber
GetAllocatorCache(MsanThreadLocalMallocStorage * ms)14460c66db4SNico Weber AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
14560c66db4SNico Weber CHECK(ms);
14660c66db4SNico Weber CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
14760c66db4SNico Weber return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
14860c66db4SNico Weber }
14960c66db4SNico Weber
CommitBack()15060c66db4SNico Weber void MsanThreadLocalMallocStorage::CommitBack() {
15160c66db4SNico Weber allocator.SwallowCache(GetAllocatorCache(this));
15260c66db4SNico Weber }
15360c66db4SNico Weber
MsanAllocate(StackTrace * stack,uptr size,uptr alignment,bool zeroise)15460c66db4SNico Weber static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment,
15560c66db4SNico Weber bool zeroise) {
1567904bd94SMatt Morehouse if (size > max_malloc_size) {
15760c66db4SNico Weber if (AllocatorMayReturnNull()) {
15860c66db4SNico Weber Report("WARNING: MemorySanitizer failed to allocate 0x%zx bytes\n", size);
15960c66db4SNico Weber return nullptr;
16060c66db4SNico Weber }
1617904bd94SMatt Morehouse ReportAllocationSizeTooBig(size, max_malloc_size, stack);
16260c66db4SNico Weber }
16363180012SVitaly Buka if (UNLIKELY(IsRssLimitExceeded())) {
16463180012SVitaly Buka if (AllocatorMayReturnNull())
16563180012SVitaly Buka return nullptr;
16663180012SVitaly Buka ReportRssLimitExceeded(stack);
16763180012SVitaly Buka }
16860c66db4SNico Weber MsanThread *t = GetCurrentThread();
16960c66db4SNico Weber void *allocated;
17060c66db4SNico Weber if (t) {
17160c66db4SNico Weber AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
17260c66db4SNico Weber allocated = allocator.Allocate(cache, size, alignment);
17360c66db4SNico Weber } else {
17460c66db4SNico Weber SpinMutexLock l(&fallback_mutex);
17560c66db4SNico Weber AllocatorCache *cache = &fallback_allocator_cache;
17660c66db4SNico Weber allocated = allocator.Allocate(cache, size, alignment);
17760c66db4SNico Weber }
17860c66db4SNico Weber if (UNLIKELY(!allocated)) {
17960c66db4SNico Weber SetAllocatorOutOfMemory();
18060c66db4SNico Weber if (AllocatorMayReturnNull())
18160c66db4SNico Weber return nullptr;
18260c66db4SNico Weber ReportOutOfMemory(size, stack);
18360c66db4SNico Weber }
18460c66db4SNico Weber Metadata *meta =
18560c66db4SNico Weber reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
18660c66db4SNico Weber meta->requested_size = size;
18760c66db4SNico Weber if (zeroise) {
18860c66db4SNico Weber __msan_clear_and_unpoison(allocated, size);
18960c66db4SNico Weber } else if (flags()->poison_in_malloc) {
19060c66db4SNico Weber __msan_poison(allocated, size);
19160c66db4SNico Weber if (__msan_get_track_origins()) {
19260c66db4SNico Weber stack->tag = StackTrace::TAG_ALLOC;
19360c66db4SNico Weber Origin o = Origin::CreateHeapOrigin(stack);
19460c66db4SNico Weber __msan_set_origin(allocated, size, o.raw_id());
19560c66db4SNico Weber }
19660c66db4SNico Weber }
197b84673b3SVitaly Buka UnpoisonParam(2);
198b84673b3SVitaly Buka RunMallocHooks(allocated, size);
19960c66db4SNico Weber return allocated;
20060c66db4SNico Weber }
20160c66db4SNico Weber
MsanDeallocate(StackTrace * stack,void * p)20260c66db4SNico Weber void MsanDeallocate(StackTrace *stack, void *p) {
20360c66db4SNico Weber CHECK(p);
204b84673b3SVitaly Buka UnpoisonParam(1);
205b84673b3SVitaly Buka RunFreeHooks(p);
206*c36fbe05SVitaly Buka
20760c66db4SNico Weber Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
20860c66db4SNico Weber uptr size = meta->requested_size;
20960c66db4SNico Weber meta->requested_size = 0;
21060c66db4SNico Weber // This memory will not be reused by anyone else, so we are free to keep it
21160c66db4SNico Weber // poisoned.
21260c66db4SNico Weber if (flags()->poison_in_free) {
21360c66db4SNico Weber __msan_poison(p, size);
21460c66db4SNico Weber if (__msan_get_track_origins()) {
21560c66db4SNico Weber stack->tag = StackTrace::TAG_DEALLOC;
21660c66db4SNico Weber Origin o = Origin::CreateHeapOrigin(stack);
21760c66db4SNico Weber __msan_set_origin(p, size, o.raw_id());
21860c66db4SNico Weber }
21960c66db4SNico Weber }
22060c66db4SNico Weber MsanThread *t = GetCurrentThread();
22160c66db4SNico Weber if (t) {
22260c66db4SNico Weber AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
22360c66db4SNico Weber allocator.Deallocate(cache, p);
22460c66db4SNico Weber } else {
22560c66db4SNico Weber SpinMutexLock l(&fallback_mutex);
22660c66db4SNico Weber AllocatorCache *cache = &fallback_allocator_cache;
22760c66db4SNico Weber allocator.Deallocate(cache, p);
22860c66db4SNico Weber }
22960c66db4SNico Weber }
23060c66db4SNico Weber
MsanReallocate(StackTrace * stack,void * old_p,uptr new_size,uptr alignment)231c027272aSJianzhou Zhao static void *MsanReallocate(StackTrace *stack, void *old_p, uptr new_size,
23260c66db4SNico Weber uptr alignment) {
23360c66db4SNico Weber Metadata *meta = reinterpret_cast<Metadata*>(allocator.GetMetaData(old_p));
23460c66db4SNico Weber uptr old_size = meta->requested_size;
23560c66db4SNico Weber uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
23660c66db4SNico Weber if (new_size <= actually_allocated_size) {
23760c66db4SNico Weber // We are not reallocating here.
23860c66db4SNico Weber meta->requested_size = new_size;
23960c66db4SNico Weber if (new_size > old_size) {
24060c66db4SNico Weber if (flags()->poison_in_malloc) {
24160c66db4SNico Weber stack->tag = StackTrace::TAG_ALLOC;
24260c66db4SNico Weber PoisonMemory((char *)old_p + old_size, new_size - old_size, stack);
24360c66db4SNico Weber }
24460c66db4SNico Weber }
24560c66db4SNico Weber return old_p;
24660c66db4SNico Weber }
24760c66db4SNico Weber uptr memcpy_size = Min(new_size, old_size);
24860c66db4SNico Weber void *new_p = MsanAllocate(stack, new_size, alignment, false /*zeroise*/);
24960c66db4SNico Weber if (new_p) {
25060c66db4SNico Weber CopyMemory(new_p, old_p, memcpy_size, stack);
25160c66db4SNico Weber MsanDeallocate(stack, old_p);
25260c66db4SNico Weber }
25360c66db4SNico Weber return new_p;
25460c66db4SNico Weber }
25560c66db4SNico Weber
MsanCalloc(StackTrace * stack,uptr nmemb,uptr size)256c027272aSJianzhou Zhao static void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size) {
25760c66db4SNico Weber if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
25860c66db4SNico Weber if (AllocatorMayReturnNull())
25960c66db4SNico Weber return nullptr;
26060c66db4SNico Weber ReportCallocOverflow(nmemb, size, stack);
26160c66db4SNico Weber }
26260c66db4SNico Weber return MsanAllocate(stack, nmemb * size, sizeof(u64), true);
26360c66db4SNico Weber }
26460c66db4SNico Weber
AllocationSize(const void * p)26560c66db4SNico Weber static uptr AllocationSize(const void *p) {
26660c66db4SNico Weber if (!p) return 0;
26760c66db4SNico Weber const void *beg = allocator.GetBlockBegin(p);
26860c66db4SNico Weber if (beg != p) return 0;
26960c66db4SNico Weber Metadata *b = (Metadata *)allocator.GetMetaData(p);
27060c66db4SNico Weber return b->requested_size;
27160c66db4SNico Weber }
27260c66db4SNico Weber
msan_malloc(uptr size,StackTrace * stack)27360c66db4SNico Weber void *msan_malloc(uptr size, StackTrace *stack) {
27460c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
27560c66db4SNico Weber }
27660c66db4SNico Weber
msan_calloc(uptr nmemb,uptr size,StackTrace * stack)27760c66db4SNico Weber void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack) {
27860c66db4SNico Weber return SetErrnoOnNull(MsanCalloc(stack, nmemb, size));
27960c66db4SNico Weber }
28060c66db4SNico Weber
msan_realloc(void * ptr,uptr size,StackTrace * stack)28160c66db4SNico Weber void *msan_realloc(void *ptr, uptr size, StackTrace *stack) {
28260c66db4SNico Weber if (!ptr)
28360c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, sizeof(u64), false));
28460c66db4SNico Weber if (size == 0) {
28560c66db4SNico Weber MsanDeallocate(stack, ptr);
28660c66db4SNico Weber return nullptr;
28760c66db4SNico Weber }
28860c66db4SNico Weber return SetErrnoOnNull(MsanReallocate(stack, ptr, size, sizeof(u64)));
28960c66db4SNico Weber }
29060c66db4SNico Weber
msan_reallocarray(void * ptr,uptr nmemb,uptr size,StackTrace * stack)29160c66db4SNico Weber void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack) {
29260c66db4SNico Weber if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
29360c66db4SNico Weber errno = errno_ENOMEM;
29460c66db4SNico Weber if (AllocatorMayReturnNull())
29560c66db4SNico Weber return nullptr;
29660c66db4SNico Weber ReportReallocArrayOverflow(nmemb, size, stack);
29760c66db4SNico Weber }
29860c66db4SNico Weber return msan_realloc(ptr, nmemb * size, stack);
29960c66db4SNico Weber }
30060c66db4SNico Weber
msan_valloc(uptr size,StackTrace * stack)30160c66db4SNico Weber void *msan_valloc(uptr size, StackTrace *stack) {
30260c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, GetPageSizeCached(), false));
30360c66db4SNico Weber }
30460c66db4SNico Weber
msan_pvalloc(uptr size,StackTrace * stack)30560c66db4SNico Weber void *msan_pvalloc(uptr size, StackTrace *stack) {
30660c66db4SNico Weber uptr PageSize = GetPageSizeCached();
30760c66db4SNico Weber if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
30860c66db4SNico Weber errno = errno_ENOMEM;
30960c66db4SNico Weber if (AllocatorMayReturnNull())
31060c66db4SNico Weber return nullptr;
31160c66db4SNico Weber ReportPvallocOverflow(size, stack);
31260c66db4SNico Weber }
31360c66db4SNico Weber // pvalloc(0) should allocate one page.
31460c66db4SNico Weber size = size ? RoundUpTo(size, PageSize) : PageSize;
31560c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, PageSize, false));
31660c66db4SNico Weber }
31760c66db4SNico Weber
msan_aligned_alloc(uptr alignment,uptr size,StackTrace * stack)31860c66db4SNico Weber void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) {
31960c66db4SNico Weber if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
32060c66db4SNico Weber errno = errno_EINVAL;
32160c66db4SNico Weber if (AllocatorMayReturnNull())
32260c66db4SNico Weber return nullptr;
32360c66db4SNico Weber ReportInvalidAlignedAllocAlignment(size, alignment, stack);
32460c66db4SNico Weber }
32560c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
32660c66db4SNico Weber }
32760c66db4SNico Weber
msan_memalign(uptr alignment,uptr size,StackTrace * stack)32860c66db4SNico Weber void *msan_memalign(uptr alignment, uptr size, StackTrace *stack) {
32960c66db4SNico Weber if (UNLIKELY(!IsPowerOfTwo(alignment))) {
33060c66db4SNico Weber errno = errno_EINVAL;
33160c66db4SNico Weber if (AllocatorMayReturnNull())
33260c66db4SNico Weber return nullptr;
33360c66db4SNico Weber ReportInvalidAllocationAlignment(alignment, stack);
33460c66db4SNico Weber }
33560c66db4SNico Weber return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false));
33660c66db4SNico Weber }
33760c66db4SNico Weber
msan_posix_memalign(void ** memptr,uptr alignment,uptr size,StackTrace * stack)33860c66db4SNico Weber int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
33960c66db4SNico Weber StackTrace *stack) {
34060c66db4SNico Weber if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
34160c66db4SNico Weber if (AllocatorMayReturnNull())
34260c66db4SNico Weber return errno_EINVAL;
34360c66db4SNico Weber ReportInvalidPosixMemalignAlignment(alignment, stack);
34460c66db4SNico Weber }
34560c66db4SNico Weber void *ptr = MsanAllocate(stack, size, alignment, false);
34660c66db4SNico Weber if (UNLIKELY(!ptr))
34760c66db4SNico Weber // OOM error is already taken care of by MsanAllocate.
34860c66db4SNico Weber return errno_ENOMEM;
34960c66db4SNico Weber CHECK(IsAligned((uptr)ptr, alignment));
35060c66db4SNico Weber *memptr = ptr;
35160c66db4SNico Weber return 0;
35260c66db4SNico Weber }
35360c66db4SNico Weber
35460c66db4SNico Weber } // namespace __msan
35560c66db4SNico Weber
35660c66db4SNico Weber using namespace __msan;
35760c66db4SNico Weber
__sanitizer_get_current_allocated_bytes()35860c66db4SNico Weber uptr __sanitizer_get_current_allocated_bytes() {
35960c66db4SNico Weber uptr stats[AllocatorStatCount];
36060c66db4SNico Weber allocator.GetStats(stats);
36160c66db4SNico Weber return stats[AllocatorStatAllocated];
36260c66db4SNico Weber }
36360c66db4SNico Weber
__sanitizer_get_heap_size()36460c66db4SNico Weber uptr __sanitizer_get_heap_size() {
36560c66db4SNico Weber uptr stats[AllocatorStatCount];
36660c66db4SNico Weber allocator.GetStats(stats);
36760c66db4SNico Weber return stats[AllocatorStatMapped];
36860c66db4SNico Weber }
36960c66db4SNico Weber
__sanitizer_get_free_bytes()37060c66db4SNico Weber uptr __sanitizer_get_free_bytes() { return 1; }
37160c66db4SNico Weber
__sanitizer_get_unmapped_bytes()37260c66db4SNico Weber uptr __sanitizer_get_unmapped_bytes() { return 1; }
37360c66db4SNico Weber
__sanitizer_get_estimated_allocated_size(uptr size)37460c66db4SNico Weber uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
37560c66db4SNico Weber
__sanitizer_get_ownership(const void * p)37660c66db4SNico Weber int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
37760c66db4SNico Weber
__sanitizer_get_allocated_size(const void * p)37860c66db4SNico Weber uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }
379