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 57// On Android, mallinfo2 is an alias of mallinfo, so don't define both. 58#if !SCUDO_ANDROID 59INTERFACE WEAK struct __scudo_mallinfo2 SCUDO_PREFIX(mallinfo2)(void) { 60 struct __scudo_mallinfo2 Info = {}; 61 scudo::StatCounters Stats; 62 SCUDO_ALLOCATOR.getStats(Stats); 63 // Space allocated in mmapped regions (bytes) 64 Info.hblkhd = Stats[scudo::StatMapped]; 65 // Maximum total allocated space (bytes) 66 Info.usmblks = Info.hblkhd; 67 // Space in freed fastbin blocks (bytes) 68 Info.fsmblks = Stats[scudo::StatFree]; 69 // Total allocated space (bytes) 70 Info.uordblks = Stats[scudo::StatAllocated]; 71 // Total free space (bytes) 72 Info.fordblks = Info.fsmblks; 73 return Info; 74} 75#endif 76 77INTERFACE WEAK void *SCUDO_PREFIX(malloc)(size_t size) { 78 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 79 size, scudo::Chunk::Origin::Malloc, SCUDO_MALLOC_ALIGNMENT)); 80} 81 82#if SCUDO_ANDROID 83INTERFACE WEAK size_t SCUDO_PREFIX(malloc_usable_size)(const void *ptr) { 84#else 85INTERFACE WEAK size_t SCUDO_PREFIX(malloc_usable_size)(void *ptr) { 86#endif 87 return SCUDO_ALLOCATOR.getUsableSize(ptr); 88} 89 90INTERFACE WEAK void *SCUDO_PREFIX(memalign)(size_t alignment, size_t size) { 91 // Android rounds up the alignment to a power of two if it isn't one. 92 if (SCUDO_ANDROID) { 93 if (UNLIKELY(!alignment)) { 94 alignment = 1U; 95 } else { 96 if (UNLIKELY(!scudo::isPowerOfTwo(alignment))) 97 alignment = scudo::roundUpPowerOfTwo(alignment); 98 } 99 } else { 100 if (UNLIKELY(!scudo::isPowerOfTwo(alignment))) { 101 if (SCUDO_ALLOCATOR.canReturnNull()) { 102 errno = EINVAL; 103 return nullptr; 104 } 105 scudo::reportAlignmentNotPowerOfTwo(alignment); 106 } 107 } 108 return SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Memalign, 109 alignment); 110} 111 112INTERFACE WEAK int SCUDO_PREFIX(posix_memalign)(void **memptr, size_t alignment, 113 size_t size) { 114 if (UNLIKELY(scudo::checkPosixMemalignAlignment(alignment))) { 115 if (!SCUDO_ALLOCATOR.canReturnNull()) 116 scudo::reportInvalidPosixMemalignAlignment(alignment); 117 return EINVAL; 118 } 119 void *Ptr = 120 SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Memalign, alignment); 121 if (UNLIKELY(!Ptr)) 122 return ENOMEM; 123 *memptr = Ptr; 124 return 0; 125} 126 127INTERFACE WEAK void *SCUDO_PREFIX(pvalloc)(size_t size) { 128 const scudo::uptr PageSize = scudo::getPageSizeCached(); 129 if (UNLIKELY(scudo::checkForPvallocOverflow(size, PageSize))) { 130 if (SCUDO_ALLOCATOR.canReturnNull()) { 131 errno = ENOMEM; 132 return nullptr; 133 } 134 scudo::reportPvallocOverflow(size); 135 } 136 // pvalloc(0) should allocate one page. 137 return scudo::setErrnoOnNull( 138 SCUDO_ALLOCATOR.allocate(size ? scudo::roundUp(size, PageSize) : PageSize, 139 scudo::Chunk::Origin::Memalign, PageSize)); 140} 141 142INTERFACE WEAK void *SCUDO_PREFIX(realloc)(void *ptr, size_t size) { 143 if (!ptr) 144 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 145 size, scudo::Chunk::Origin::Malloc, SCUDO_MALLOC_ALIGNMENT)); 146 if (size == 0) { 147 SCUDO_ALLOCATOR.deallocate(ptr, scudo::Chunk::Origin::Malloc); 148 return nullptr; 149 } 150 return scudo::setErrnoOnNull( 151 SCUDO_ALLOCATOR.reallocate(ptr, size, SCUDO_MALLOC_ALIGNMENT)); 152} 153 154INTERFACE WEAK void *SCUDO_PREFIX(valloc)(size_t size) { 155 return scudo::setErrnoOnNull(SCUDO_ALLOCATOR.allocate( 156 size, scudo::Chunk::Origin::Memalign, scudo::getPageSizeCached())); 157} 158 159INTERFACE WEAK int SCUDO_PREFIX(malloc_iterate)( 160 uintptr_t base, size_t size, 161 void (*callback)(uintptr_t base, size_t size, void *arg), void *arg) { 162 SCUDO_ALLOCATOR.iterateOverChunks(base, size, callback, arg); 163 return 0; 164} 165 166INTERFACE WEAK void SCUDO_PREFIX(malloc_enable)() { SCUDO_ALLOCATOR.enable(); } 167 168INTERFACE WEAK void SCUDO_PREFIX(malloc_disable)() { 169 SCUDO_ALLOCATOR.disable(); 170} 171 172void SCUDO_PREFIX(malloc_postinit)() { 173 SCUDO_ALLOCATOR.initGwpAsan(); 174 pthread_atfork(SCUDO_PREFIX(malloc_disable), SCUDO_PREFIX(malloc_enable), 175 SCUDO_PREFIX(malloc_enable)); 176} 177 178INTERFACE WEAK int SCUDO_PREFIX(mallopt)(int param, int value) { 179 if (param == M_DECAY_TIME) { 180 if (SCUDO_ANDROID) { 181 if (value == 0) { 182 // Will set the release values to their minimum values. 183 value = INT32_MIN; 184 } else { 185 // Will set the release values to their maximum values. 186 value = INT32_MAX; 187 } 188 } 189 190 SCUDO_ALLOCATOR.setOption(scudo::Option::ReleaseInterval, 191 static_cast<scudo::sptr>(value)); 192 return 1; 193 } else if (param == M_PURGE) { 194 SCUDO_ALLOCATOR.releaseToOS(scudo::ReleaseToOS::Force); 195 return 1; 196 } else if (param == M_PURGE_ALL) { 197 SCUDO_ALLOCATOR.releaseToOS(scudo::ReleaseToOS::ForceAll); 198 return 1; 199 } else { 200 scudo::Option option; 201 switch (param) { 202 case M_MEMTAG_TUNING: 203 option = scudo::Option::MemtagTuning; 204 break; 205 case M_THREAD_DISABLE_MEM_INIT: 206 option = scudo::Option::ThreadDisableMemInit; 207 break; 208 case M_CACHE_COUNT_MAX: 209 option = scudo::Option::MaxCacheEntriesCount; 210 break; 211 case M_CACHE_SIZE_MAX: 212 option = scudo::Option::MaxCacheEntrySize; 213 break; 214 case M_TSDS_COUNT_MAX: 215 option = scudo::Option::MaxTSDsCount; 216 break; 217 default: 218 return 0; 219 } 220 return SCUDO_ALLOCATOR.setOption(option, static_cast<scudo::sptr>(value)); 221 } 222} 223 224INTERFACE WEAK void *SCUDO_PREFIX(aligned_alloc)(size_t alignment, 225 size_t size) { 226 if (UNLIKELY(scudo::checkAlignedAllocAlignmentAndSize(alignment, size))) { 227 if (SCUDO_ALLOCATOR.canReturnNull()) { 228 errno = EINVAL; 229 return nullptr; 230 } 231 scudo::reportInvalidAlignedAllocAlignment(alignment, size); 232 } 233 return scudo::setErrnoOnNull( 234 SCUDO_ALLOCATOR.allocate(size, scudo::Chunk::Origin::Malloc, alignment)); 235} 236 237INTERFACE WEAK int SCUDO_PREFIX(malloc_info)(UNUSED int options, FILE *stream) { 238 const scudo::uptr max_size = 239 decltype(SCUDO_ALLOCATOR)::PrimaryT::SizeClassMap::MaxSize; 240 auto *sizes = static_cast<scudo::uptr *>( 241 SCUDO_PREFIX(calloc)(max_size, sizeof(scudo::uptr))); 242 auto callback = [](uintptr_t, size_t size, void *arg) { 243 auto *sizes = reinterpret_cast<scudo::uptr *>(arg); 244 if (size < max_size) 245 sizes[size]++; 246 }; 247 248 SCUDO_ALLOCATOR.disable(); 249 SCUDO_ALLOCATOR.iterateOverChunks(0, -1ul, callback, sizes); 250 SCUDO_ALLOCATOR.enable(); 251 252 fputs("<malloc version=\"scudo-1\">\n", stream); 253 for (scudo::uptr i = 0; i != max_size; ++i) 254 if (sizes[i]) 255 fprintf(stream, "<alloc size=\"%zu\" count=\"%zu\"/>\n", i, sizes[i]); 256 fputs("</malloc>\n", stream); 257 SCUDO_PREFIX(free)(sizes); 258 return 0; 259} 260 261// Disable memory tagging for the heap. The caller must disable memory tag 262// checks globally (e.g. by clearing TCF0 on aarch64) before calling this 263// function, and may not re-enable them after calling the function. 264INTERFACE WEAK void SCUDO_PREFIX(malloc_disable_memory_tagging)() { 265 SCUDO_ALLOCATOR.disableMemoryTagging(); 266} 267 268// Sets whether scudo records stack traces and other metadata for allocations 269// and deallocations. This function only has an effect if the allocator and 270// hardware support memory tagging. 271INTERFACE WEAK void 272SCUDO_PREFIX(malloc_set_track_allocation_stacks)(int track) { 273 SCUDO_ALLOCATOR.setTrackAllocationStacks(track); 274} 275 276// Sets whether scudo zero-initializes all allocated memory. 277INTERFACE WEAK void SCUDO_PREFIX(malloc_set_zero_contents)(int zero_contents) { 278 SCUDO_ALLOCATOR.setFillContents(zero_contents ? scudo::ZeroFill 279 : scudo::NoFill); 280} 281 282// Sets whether scudo pattern-initializes all allocated memory. 283INTERFACE WEAK void 284SCUDO_PREFIX(malloc_set_pattern_fill_contents)(int pattern_fill_contents) { 285 SCUDO_ALLOCATOR.setFillContents( 286 pattern_fill_contents ? scudo::PatternOrZeroFill : scudo::NoFill); 287} 288 289// Sets whether scudo adds a small amount of slack at the end of large 290// allocations, before the guard page. This can be enabled to work around buggy 291// applications that read a few bytes past the end of their allocation. 292INTERFACE WEAK void 293SCUDO_PREFIX(malloc_set_add_large_allocation_slack)(int add_slack) { 294 SCUDO_ALLOCATOR.setAddLargeAllocationSlack(add_slack); 295} 296 297} // extern "C" 298