1 //===-- wrappers_c_test.cpp -------------------------------------*- 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 #include "scudo/interface.h" 10 #include "tests/scudo_unit_test.h" 11 12 #include <errno.h> 13 #include <limits.h> 14 #include <malloc.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 18 extern "C" { 19 void malloc_enable(void); 20 void malloc_disable(void); 21 int malloc_iterate(uintptr_t base, size_t size, 22 void (*callback)(uintptr_t base, size_t size, void *arg), 23 void *arg); 24 void *valloc(size_t size); 25 void *pvalloc(size_t size); 26 } 27 28 // Note that every C allocation function in the test binary will be fulfilled 29 // by Scudo (this includes the gtest APIs, etc.), which is a test by itself. 30 // But this might also lead to unexpected side-effects, since the allocation and 31 // deallocation operations in the TEST functions will coexist with others (see 32 // the EXPECT_DEATH comment below). 33 34 // We have to use a small quarantine to make sure that our double-free tests 35 // trigger. Otherwise EXPECT_DEATH ends up reallocating the chunk that was just 36 // freed (this depends on the size obviously) and the following free succeeds. 37 38 static const size_t Size = 100U; 39 40 TEST(ScudoWrappersCTest, Malloc) { 41 void *P = malloc(Size); 42 EXPECT_NE(P, nullptr); 43 EXPECT_LE(Size, malloc_usable_size(P)); 44 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) % FIRST_32_SECOND_64(8U, 16U), 0U); 45 EXPECT_DEATH( 46 free(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(P) | 1U)), ""); 47 free(P); 48 EXPECT_DEATH(free(P), ""); 49 50 P = malloc(0U); 51 EXPECT_NE(P, nullptr); 52 free(P); 53 54 errno = 0; 55 EXPECT_EQ(malloc(SIZE_MAX), nullptr); 56 EXPECT_EQ(errno, ENOMEM); 57 } 58 59 TEST(ScudoWrappersCTest, Calloc) { 60 void *P = calloc(1U, Size); 61 EXPECT_NE(P, nullptr); 62 EXPECT_LE(Size, malloc_usable_size(P)); 63 for (size_t I = 0; I < Size; I++) 64 EXPECT_EQ((reinterpret_cast<uint8_t *>(P))[I], 0U); 65 free(P); 66 67 P = calloc(1U, 0U); 68 EXPECT_NE(P, nullptr); 69 free(P); 70 P = calloc(0U, 1U); 71 EXPECT_NE(P, nullptr); 72 free(P); 73 74 errno = 0; 75 EXPECT_EQ(calloc(SIZE_MAX, 1U), nullptr); 76 EXPECT_EQ(errno, ENOMEM); 77 errno = 0; 78 EXPECT_EQ(calloc(static_cast<size_t>(LONG_MAX) + 1U, 2U), nullptr); 79 if (SCUDO_ANDROID) 80 EXPECT_EQ(errno, ENOMEM); 81 errno = 0; 82 EXPECT_EQ(calloc(SIZE_MAX, SIZE_MAX), nullptr); 83 EXPECT_EQ(errno, ENOMEM); 84 } 85 86 TEST(ScudoWrappersCTest, Memalign) { 87 void *P; 88 for (size_t I = FIRST_32_SECOND_64(2U, 3U); I <= 18U; I++) { 89 const size_t Alignment = 1U << I; 90 91 P = memalign(Alignment, Size); 92 EXPECT_NE(P, nullptr); 93 EXPECT_LE(Size, malloc_usable_size(P)); 94 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) % Alignment, 0U); 95 free(P); 96 97 P = nullptr; 98 EXPECT_EQ(posix_memalign(&P, Alignment, Size), 0); 99 EXPECT_NE(P, nullptr); 100 EXPECT_LE(Size, malloc_usable_size(P)); 101 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) % Alignment, 0U); 102 free(P); 103 } 104 105 EXPECT_EQ(memalign(4096U, SIZE_MAX), nullptr); 106 EXPECT_EQ(posix_memalign(&P, 15U, Size), EINVAL); 107 EXPECT_EQ(posix_memalign(&P, 4096U, SIZE_MAX), ENOMEM); 108 109 // Android's memalign accepts non power-of-2 alignments, and 0. 110 if (SCUDO_ANDROID) { 111 for (size_t Alignment = 0U; Alignment <= 128U; Alignment++) { 112 P = memalign(Alignment, 1024U); 113 EXPECT_NE(P, nullptr); 114 free(P); 115 } 116 } 117 } 118 119 TEST(ScudoWrappersCTest, AlignedAlloc) { 120 const size_t Alignment = 4096U; 121 void *P = aligned_alloc(Alignment, Alignment * 4U); 122 EXPECT_NE(P, nullptr); 123 EXPECT_LE(Alignment * 4U, malloc_usable_size(P)); 124 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) % Alignment, 0U); 125 free(P); 126 127 errno = 0; 128 P = aligned_alloc(Alignment, Size); 129 EXPECT_EQ(P, nullptr); 130 EXPECT_EQ(errno, EINVAL); 131 } 132 133 TEST(ScudoWrappersCTest, Realloc) { 134 // realloc(nullptr, N) is malloc(N) 135 void *P = realloc(nullptr, 0U); 136 EXPECT_NE(P, nullptr); 137 free(P); 138 139 P = malloc(Size); 140 EXPECT_NE(P, nullptr); 141 // realloc(P, 0U) is free(P) and returns nullptr 142 EXPECT_EQ(realloc(P, 0U), nullptr); 143 144 P = malloc(Size); 145 EXPECT_NE(P, nullptr); 146 EXPECT_LE(Size, malloc_usable_size(P)); 147 memset(P, 0x42, Size); 148 149 P = realloc(P, Size * 2U); 150 EXPECT_NE(P, nullptr); 151 EXPECT_LE(Size * 2U, malloc_usable_size(P)); 152 for (size_t I = 0; I < Size; I++) 153 EXPECT_EQ(0x42, (reinterpret_cast<uint8_t *>(P))[I]); 154 155 P = realloc(P, Size / 2U); 156 EXPECT_NE(P, nullptr); 157 EXPECT_LE(Size / 2U, malloc_usable_size(P)); 158 for (size_t I = 0; I < Size / 2U; I++) 159 EXPECT_EQ(0x42, (reinterpret_cast<uint8_t *>(P))[I]); 160 free(P); 161 162 EXPECT_DEATH(P = realloc(P, Size), ""); 163 164 errno = 0; 165 EXPECT_EQ(realloc(nullptr, SIZE_MAX), nullptr); 166 EXPECT_EQ(errno, ENOMEM); 167 P = malloc(Size); 168 EXPECT_NE(P, nullptr); 169 errno = 0; 170 EXPECT_EQ(realloc(P, SIZE_MAX), nullptr); 171 EXPECT_EQ(errno, ENOMEM); 172 free(P); 173 174 // Android allows realloc of memalign pointers. 175 if (SCUDO_ANDROID) { 176 const size_t Alignment = 1024U; 177 P = memalign(Alignment, Size); 178 EXPECT_NE(P, nullptr); 179 EXPECT_LE(Size, malloc_usable_size(P)); 180 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) % Alignment, 0U); 181 memset(P, 0x42, Size); 182 183 P = realloc(P, Size * 2U); 184 EXPECT_NE(P, nullptr); 185 EXPECT_LE(Size * 2U, malloc_usable_size(P)); 186 for (size_t I = 0; I < Size; I++) 187 EXPECT_EQ(0x42, (reinterpret_cast<uint8_t *>(P))[I]); 188 free(P); 189 } 190 } 191 192 #if !SCUDO_FUCHSIA 193 TEST(ScudoWrappersCTest, MallOpt) { 194 errno = 0; 195 EXPECT_EQ(mallopt(-1000, 1), 0); 196 // mallopt doesn't set errno. 197 EXPECT_EQ(errno, 0); 198 199 EXPECT_EQ(mallopt(M_PURGE, 0), 1); 200 201 EXPECT_EQ(mallopt(M_DECAY_TIME, 1), 1); 202 EXPECT_EQ(mallopt(M_DECAY_TIME, 0), 1); 203 EXPECT_EQ(mallopt(M_DECAY_TIME, 1), 1); 204 EXPECT_EQ(mallopt(M_DECAY_TIME, 0), 1); 205 206 if (SCUDO_ANDROID) { 207 EXPECT_EQ(mallopt(M_CACHE_COUNT_MAX, 100), 1); 208 EXPECT_EQ(mallopt(M_CACHE_SIZE_MAX, 1024 * 1024 * 2), 1); 209 EXPECT_EQ(mallopt(M_TSDS_COUNT_MAX, 10), 1); 210 } 211 } 212 #endif 213 214 TEST(ScudoWrappersCTest, OtherAlloc) { 215 #if !SCUDO_FUCHSIA 216 const size_t PageSize = sysconf(_SC_PAGESIZE); 217 218 void *P = pvalloc(Size); 219 EXPECT_NE(P, nullptr); 220 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) & (PageSize - 1), 0U); 221 EXPECT_LE(PageSize, malloc_usable_size(P)); 222 free(P); 223 224 EXPECT_EQ(pvalloc(SIZE_MAX), nullptr); 225 226 P = pvalloc(Size); 227 EXPECT_NE(P, nullptr); 228 EXPECT_EQ(reinterpret_cast<uintptr_t>(P) & (PageSize - 1), 0U); 229 free(P); 230 #endif 231 232 EXPECT_EQ(valloc(SIZE_MAX), nullptr); 233 } 234 235 #if !SCUDO_FUCHSIA 236 TEST(ScudoWrappersCTest, MallInfo) { 237 const size_t BypassQuarantineSize = 1024U; 238 239 struct mallinfo MI = mallinfo(); 240 size_t Allocated = MI.uordblks; 241 void *P = malloc(BypassQuarantineSize); 242 EXPECT_NE(P, nullptr); 243 MI = mallinfo(); 244 EXPECT_GE(static_cast<size_t>(MI.uordblks), Allocated + BypassQuarantineSize); 245 EXPECT_GT(static_cast<size_t>(MI.hblkhd), 0U); 246 size_t Free = MI.fordblks; 247 free(P); 248 MI = mallinfo(); 249 EXPECT_GE(static_cast<size_t>(MI.fordblks), Free + BypassQuarantineSize); 250 } 251 #endif 252 253 static uintptr_t BoundaryP; 254 static size_t Count; 255 256 static void callback(uintptr_t Base, size_t Size, void *Arg) { 257 if (Base == BoundaryP) 258 Count++; 259 } 260 261 // Verify that a block located on an iteration boundary is not mis-accounted. 262 // To achieve this, we allocate a chunk for which the backing block will be 263 // aligned on a page, then run the malloc_iterate on both the pages that the 264 // block is a boundary for. It must only be seen once by the callback function. 265 TEST(ScudoWrappersCTest, MallocIterateBoundary) { 266 const size_t PageSize = sysconf(_SC_PAGESIZE); 267 const size_t BlockDelta = FIRST_32_SECOND_64(8U, 16U); 268 const size_t SpecialSize = PageSize - BlockDelta; 269 270 // We aren't guaranteed that any size class is exactly a page wide. So we need 271 // to keep making allocations until we succeed. 272 // 273 // With a 16-byte block alignment and 4096-byte page size, each allocation has 274 // a probability of (1 - (16/4096)) of failing to meet the alignment 275 // requirements, and the probability of failing 65536 times is 276 // (1 - (16/4096))^65536 < 10^-112. So if we still haven't succeeded after 277 // 65536 tries, give up. 278 uintptr_t Block; 279 void *P = nullptr; 280 for (unsigned I = 0; I != 65536; ++I) { 281 void *PrevP = P; 282 P = malloc(SpecialSize); 283 EXPECT_NE(P, nullptr); 284 *reinterpret_cast<void **>(P) = PrevP; 285 BoundaryP = reinterpret_cast<uintptr_t>(P); 286 Block = BoundaryP - BlockDelta; 287 if ((Block & (PageSize - 1)) == 0U) 288 break; 289 } 290 EXPECT_EQ((Block & (PageSize - 1)), 0U); 291 292 Count = 0U; 293 malloc_disable(); 294 malloc_iterate(Block - PageSize, PageSize, callback, nullptr); 295 malloc_iterate(Block, PageSize, callback, nullptr); 296 malloc_enable(); 297 EXPECT_EQ(Count, 1U); 298 299 while (P) { 300 void *NextP = *reinterpret_cast<void **>(P); 301 free(P); 302 P = NextP; 303 } 304 } 305 306 // We expect heap operations within a disable/enable scope to deadlock. 307 TEST(ScudoWrappersCTest, MallocDisableDeadlock) { 308 EXPECT_DEATH( 309 { 310 void *P = malloc(Size); 311 EXPECT_NE(P, nullptr); 312 free(P); 313 malloc_disable(); 314 alarm(1); 315 P = malloc(Size); 316 malloc_enable(); 317 }, 318 ""); 319 } 320 321 // Fuchsia doesn't have fork or malloc_info. 322 #if !SCUDO_FUCHSIA 323 324 TEST(ScudoWrappersCTest, MallocInfo) { 325 // Use volatile so that the allocations don't get optimized away. 326 void *volatile P1 = malloc(1234); 327 void *volatile P2 = malloc(4321); 328 329 char Buffer[16384]; 330 FILE *F = fmemopen(Buffer, sizeof(Buffer), "w+"); 331 EXPECT_NE(F, nullptr); 332 errno = 0; 333 EXPECT_EQ(malloc_info(0, F), 0); 334 EXPECT_EQ(errno, 0); 335 fclose(F); 336 EXPECT_EQ(strncmp(Buffer, "<malloc version=\"scudo-", 23), 0); 337 EXPECT_NE(nullptr, strstr(Buffer, "<alloc size=\"1234\" count=\"")); 338 EXPECT_NE(nullptr, strstr(Buffer, "<alloc size=\"4321\" count=\"")); 339 340 free(P1); 341 free(P2); 342 } 343 344 TEST(ScudoWrappersCTest, Fork) { 345 void *P; 346 pid_t Pid = fork(); 347 EXPECT_GE(Pid, 0); 348 if (Pid == 0) { 349 P = malloc(Size); 350 EXPECT_NE(P, nullptr); 351 memset(P, 0x42, Size); 352 free(P); 353 _exit(0); 354 } 355 waitpid(Pid, nullptr, 0); 356 P = malloc(Size); 357 EXPECT_NE(P, nullptr); 358 memset(P, 0x42, Size); 359 free(P); 360 361 // fork should stall if the allocator has been disabled. 362 EXPECT_DEATH( 363 { 364 malloc_disable(); 365 alarm(1); 366 Pid = fork(); 367 EXPECT_GE(Pid, 0); 368 }, 369 ""); 370 } 371 372 static pthread_mutex_t Mutex; 373 static pthread_cond_t Conditional = PTHREAD_COND_INITIALIZER; 374 static bool Ready; 375 376 static void *enableMalloc(void *Unused) { 377 // Initialize the allocator for this thread. 378 void *P = malloc(Size); 379 EXPECT_NE(P, nullptr); 380 memset(P, 0x42, Size); 381 free(P); 382 383 // Signal the main thread we are ready. 384 pthread_mutex_lock(&Mutex); 385 Ready = true; 386 pthread_cond_signal(&Conditional); 387 pthread_mutex_unlock(&Mutex); 388 389 // Wait for the malloc_disable & fork, then enable the allocator again. 390 sleep(1); 391 malloc_enable(); 392 393 return nullptr; 394 } 395 396 TEST(ScudoWrappersCTest, DisableForkEnable) { 397 pthread_t ThreadId; 398 Ready = false; 399 EXPECT_EQ(pthread_create(&ThreadId, nullptr, &enableMalloc, nullptr), 0); 400 401 // Wait for the thread to be warmed up. 402 pthread_mutex_lock(&Mutex); 403 while (!Ready) 404 pthread_cond_wait(&Conditional, &Mutex); 405 pthread_mutex_unlock(&Mutex); 406 407 // Disable the allocator and fork. fork should succeed after malloc_enable. 408 malloc_disable(); 409 pid_t Pid = fork(); 410 EXPECT_GE(Pid, 0); 411 if (Pid == 0) { 412 void *P = malloc(Size); 413 EXPECT_NE(P, nullptr); 414 memset(P, 0x42, Size); 415 free(P); 416 _exit(0); 417 } 418 waitpid(Pid, nullptr, 0); 419 EXPECT_EQ(pthread_join(ThreadId, 0), 0); 420 } 421 422 #endif // SCUDO_FUCHSIA 423