1//===-- wrappers_c.inc ------------------------------------------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef SCUDO_PREFIX 10#error "Define SCUDO_PREFIX prior to including this file!" 11#endif 12 13// malloc-type functions have to be aligned to std::max_align_t. This is 14// distinct from (1U << SCUDO_MIN_ALIGNMENT_LOG), since C++ new-type functions 15// do not have to abide by the same requirement. 16#ifndef SCUDO_MALLOC_ALIGNMENT 17#define SCUDO_MALLOC_ALIGNMENT FIRST_32_SECOND_64(8U, 16U) 18#endif 19 20extern "C" { 21 22INTERFACE WEAK void *SCUDO_PREFIX(calloc)(size_t nmemb, size_t size) { 23 scudo::uptr Product; 24 if (UNLIKELY(scudo::checkForCallocOverflow(size, nmemb, &Product))) { 25 if (SCUDO_ALLOCATOR.canReturnNull()) { 26 errno = ENOMEM; 27 return nullptr; 28 } 29 scudo::reportCallocOverflow(nmemb, size); 30 } 31 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 32 Product, scudo::Chunk::Origin::Malloc, SCUDO_MALLOC_ALIGNMENT, true)); 33} 34 35INTERFACE WEAK void SCUDO_PREFIX(free)(void *ptr) { 36 SCUDO_ALLOCATOR.deallocate(ptr, scudo::Chunk::Origin::Malloc); 37} 38 39INTERFACE WEAK struct SCUDO_MALLINFO SCUDO_PREFIX(mallinfo)(void) { 40 struct SCUDO_MALLINFO Info = {}; 41 scudo::StatCounters Stats; 42 SCUDO_ALLOCATOR.getStats(Stats); 43 // Space allocated in mmapped regions (bytes) 44 Info.hblkhd = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatMapped]); 45 // Maximum total allocated space (bytes) 46 Info.usmblks = Info.hblkhd; 47 // Space in freed fastbin blocks (bytes) 48 Info.fsmblks = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatFree]); 49 // Total allocated space (bytes) 50 Info.uordblks = 51 static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatAllocated]); 52 // Total free space (bytes) 53 Info.fordblks = Info.fsmblks; 54 return Info; 55} 56 57INTERFACE WEAK void *SCUDO_PREFIX(malloc)(size_t size) { 58 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 59 size, scudo::Chunk::Origin::Malloc, SCUDO_MALLOC_ALIGNMENT)); 60} 61 62#if SCUDO_ANDROID 63INTERFACE WEAK size_t SCUDO_PREFIX(malloc_usable_size)(const void *ptr) { 64#else 65INTERFACE WEAK size_t SCUDO_PREFIX(malloc_usable_size)(void *ptr) { 66#endif 67 return SCUDO_ALLOCATOR.getUsableSize(ptr); 68} 69 70INTERFACE WEAK void *SCUDO_PREFIX(memalign)(size_t alignment, size_t size) { 71 // Android rounds up the alignment to a power of two if it isn't one. 72 if (SCUDO_ANDROID) { 73 if (UNLIKELY(!alignment)) { 74 alignment = 1U; 75 } else { 76 if (UNLIKELY(!scudo::isPowerOfTwo(alignment))) 77 alignment = scudo::roundUpToPowerOfTwo(alignment); 78 } 79 } else { 80 if (UNLIKELY(!scudo::isPowerOfTwo(alignment))) { 81 if (SCUDO_ALLOCATOR.canReturnNull()) { 82 errno = EINVAL; 83 return nullptr; 84 } 85 scudo::reportAlignmentNotPowerOfTwo(alignment); 86 } 87 } 88 return SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Memalign, 89 alignment); 90} 91 92INTERFACE WEAK int SCUDO_PREFIX(posix_memalign)(void **memptr, size_t alignment, 93 size_t size) { 94 if (UNLIKELY(scudo::checkPosixMemalignAlignment(alignment))) { 95 if (!SCUDO_ALLOCATOR.canReturnNull()) 96 scudo::reportInvalidPosixMemalignAlignment(alignment); 97 return EINVAL; 98 } 99 void *Ptr = 100 SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Memalign, alignment); 101 if (UNLIKELY(!Ptr)) 102 return ENOMEM; 103 *memptr = Ptr; 104 return 0; 105} 106 107INTERFACE WEAK void *SCUDO_PREFIX(pvalloc)(size_t size) { 108 const scudo::uptr PageSize = scudo::getPageSizeCached(); 109 if (UNLIKELY(scudo::checkForPvallocOverflow(size, PageSize))) { 110 if (SCUDO_ALLOCATOR.canReturnNull()) { 111 errno = ENOMEM; 112 return nullptr; 113 } 114 scudo::reportPvallocOverflow(size); 115 } 116 // pvalloc(0) should allocate one page. 117 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 118 size ? scudo::roundUpTo(size, PageSize) : PageSize, 119 scudo::Chunk::Origin::Memalign, PageSize)); 120} 121 122INTERFACE WEAK void *SCUDO_PREFIX(realloc)(void *ptr, size_t size) { 123 if (!ptr) 124 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 125 size, scudo::Chunk::Origin::Malloc, SCUDO_MALLOC_ALIGNMENT)); 126 if (size == 0) { 127 SCUDO_ALLOCATOR.deallocate(ptr, scudo::Chunk::Origin::Malloc); 128 return nullptr; 129 } 130 return scudo::setErrnoOnNull( 131 SCUDO_ALLOCATOR.reallocate(ptr, size, SCUDO_MALLOC_ALIGNMENT)); 132} 133 134INTERFACE WEAK void *SCUDO_PREFIX(valloc)(size_t size) { 135 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 136 size, scudo::Chunk::Origin::Memalign, scudo::getPageSizeCached())); 137} 138 139INTERFACE WEAK int SCUDO_PREFIX(malloc_iterate)( 140 uintptr_t base, size_t size, 141 void (*callback)(uintptr_t base, size_t size, void *arg), void *arg) { 142 SCUDO_ALLOCATOR.iterateOverChunks(base, size, callback, arg); 143 return 0; 144} 145 146INTERFACE WEAK void SCUDO_PREFIX(malloc_enable)() { SCUDO_ALLOCATOR.enable(); } 147 148INTERFACE WEAK void SCUDO_PREFIX(malloc_disable)() { 149 SCUDO_ALLOCATOR.disable(); 150} 151 152void SCUDO_PREFIX(malloc_postinit)() { 153 pthread_atfork(SCUDO_PREFIX(malloc_disable), SCUDO_PREFIX(malloc_enable), 154 SCUDO_PREFIX(malloc_enable)); 155} 156 157INTERFACE WEAK int SCUDO_PREFIX(mallopt)(int param, UNUSED int value) { 158 if (param == M_DECAY_TIME) { 159 // TODO(kostyak): set release_to_os_interval_ms accordingly. 160 return 1; 161 } else if (param == M_PURGE) { 162 SCUDO_ALLOCATOR.releaseToOS(); 163 return 1; 164 } 165 return 0; 166} 167 168INTERFACE WEAK void *SCUDO_PREFIX(aligned_alloc)(size_t alignment, 169 size_t size) { 170 if (UNLIKELY(scudo::checkAlignedAllocAlignmentAndSize(alignment, size))) { 171 if (SCUDO_ALLOCATOR.canReturnNull()) { 172 errno = EINVAL; 173 return nullptr; 174 } 175 scudo::reportInvalidAlignedAllocAlignment(alignment, size); 176 } 177 return scudo::setErrnoOnNull( 178 SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Malloc, alignment)); 179} 180 181INTERFACE WEAK int SCUDO_PREFIX(malloc_info)(UNUSED int options, FILE *stream) { 182 fputs("<malloc version=\"scudo-1\">", stream); 183 fputs("</malloc>", stream); 184 return 0; 185} 186 187} // extern "C" 188