| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include "gtest/gtest.h" |
| 6 |
|
| 7 |
#include "mozmemory.h" |
| 8 |
#include "mozilla/Assertions.h" |
| 9 |
#include "mozilla/mozalloc.h" |
| 10 |
#include "PHC.h" |
| 11 |
|
| 12 |
using namespace mozilla; |
| 13 |
|
| 14 |
bool PHCInfoEq(phc::AddrInfo& aInfo, phc::AddrInfo::Kind aKind, void* aBaseAddr, |
| 15 |
size_t aUsableSize, bool aHasAllocStack, bool aHasFreeStack) { |
| 16 |
return aInfo.mKind == aKind && aInfo.mBaseAddr == aBaseAddr && |
| 17 |
aInfo.mUsableSize == aUsableSize && |
| 18 |
// Proper stack traces will have at least 3 elements. |
| 19 |
(aHasAllocStack ? (aInfo.mAllocStack->mLength > 2) |
| 20 |
: (aInfo.mAllocStack.isNothing())) && |
| 21 |
(aHasFreeStack ? (aInfo.mFreeStack->mLength > 2) |
| 22 |
: (aInfo.mFreeStack.isNothing())); |
| 23 |
} |
| 24 |
|
| 25 |
bool JeInfoEq(jemalloc_ptr_info_t& aInfo, PtrInfoTag aTag, void* aAddr, |
| 26 |
size_t aSize, arena_id_t arenaId) { |
| 27 |
return aInfo.tag == aTag && aInfo.addr == aAddr && aInfo.size == aSize |
| 28 |
#ifdef MOZ_DEBUG |
| 29 |
&& aInfo.arenaId == arenaId |
| 30 |
#endif |
| 31 |
; |
| 32 |
} |
| 33 |
|
| 34 |
uint8_t* GetPHCAllocation(size_t aSize, size_t aAlignment = 1) { |
| 35 |
// A crude but effective way to get a PHC allocation. |
| 36 |
for (int i = 0; i < 2000000; i++) { |
| 37 |
void* p = (aAlignment == 1) ? moz_xmalloc(aSize) |
| 38 |
: moz_xmemalign(aAlignment, aSize); |
| 39 |
if (mozilla::phc::IsPHCAllocation(p, nullptr)) { |
| 40 |
return (uint8_t*)p; |
| 41 |
} |
| 42 |
free(p); |
| 43 |
} |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
|
| 47 |
#if defined(XP_DARWIN) && defined(__aarch64__) |
| 48 |
static const size_t kPageSize = 16384; |
| 49 |
#else |
| 50 |
static const size_t kPageSize = 4096; |
| 51 |
#endif |
| 52 |
|
| 53 |
TEST(PHC, TestPHCAllocations) |
| 54 |
{ |
| 55 |
mozilla::phc::SetPHCState(phc::PHCState::Enabled); |
| 56 |
|
| 57 |
// First, check that allocations of various sizes all get put at the end of |
| 58 |
// their page as expected. Also, check their sizes are as expected. |
| 59 |
|
| 60 |
#define ASSERT_POS(n1, n2) \ |
| 61 |
p = (uint8_t*)moz_xrealloc(p, (n1)); \ |
| 62 |
ASSERT_EQ((reinterpret_cast<uintptr_t>(p) & (kPageSize - 1)), \ |
| 63 |
kPageSize - (n2)); \ |
| 64 |
ASSERT_EQ(moz_malloc_usable_size(p), (n2)); |
| 65 |
|
| 66 |
uint8_t* p = GetPHCAllocation(1); |
| 67 |
if (!p) { |
| 68 |
MOZ_CRASH("failed to get a PHC allocation"); |
| 69 |
} |
| 70 |
|
| 71 |
// On Win64 the smallest possible allocation is 16 bytes. On other platforms |
| 72 |
// it is 8 bytes. |
| 73 |
#if defined(XP_WIN) && defined(HAVE_64BIT_BUILD) |
| 74 |
ASSERT_POS(8U, 16U); |
| 75 |
#else |
| 76 |
ASSERT_POS(8U, 8U); |
| 77 |
#endif |
| 78 |
for (unsigned i = 16; i <= kPageSize; i *= 2) { |
| 79 |
ASSERT_POS(i, i); |
| 80 |
} |
| 81 |
|
| 82 |
free(p); |
| 83 |
|
| 84 |
#undef ASSERT_POS |
| 85 |
|
| 86 |
// Second, do similar checking with allocations of various alignments. Also |
| 87 |
// check that their sizes (which are different to allocations with normal |
| 88 |
// alignment) are the same as the sizes of equivalent non-PHC allocations. |
| 89 |
|
| 90 |
#define ASSERT_ALIGN(a1, a2) \ |
| 91 |
p = (uint8_t*)GetPHCAllocation(8, (a1)); \ |
| 92 |
ASSERT_EQ((reinterpret_cast<uintptr_t>(p) & (kPageSize - 1)), \ |
| 93 |
kPageSize - (a2)); \ |
| 94 |
ASSERT_EQ(moz_malloc_usable_size(p), (a2)); \ |
| 95 |
free(p); \ |
| 96 |
p = (uint8_t*)moz_xmemalign((a1), 8); \ |
| 97 |
ASSERT_EQ(moz_malloc_usable_size(p), (a2)); \ |
| 98 |
free(p); |
| 99 |
|
| 100 |
// On Win64 the smallest possible allocation is 16 bytes. On other platforms |
| 101 |
// it is 8 bytes. |
| 102 |
#if defined(XP_WIN) && defined(HAVE_64BIT_BUILD) |
| 103 |
ASSERT_ALIGN(8U, 16U); |
| 104 |
#else |
| 105 |
ASSERT_ALIGN(8U, 8U); |
| 106 |
#endif |
| 107 |
for (unsigned i = 16; i <= kPageSize; i *= 2) { |
| 108 |
ASSERT_ALIGN(i, i); |
| 109 |
} |
| 110 |
|
| 111 |
#undef ASSERT_ALIGN |
| 112 |
} |
| 113 |
|
| 114 |
static void TestInUseAllocation(uint8_t* aPtr, size_t aSize) { |
| 115 |
phc::AddrInfo phcInfo; |
| 116 |
jemalloc_ptr_info_t jeInfo; |
| 117 |
|
| 118 |
// Test an in-use PHC allocation: first byte within it. |
| 119 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr, &phcInfo)); |
| 120 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::InUsePage, aPtr, aSize, |
| 121 |
true, false)); |
| 122 |
ASSERT_EQ(moz_malloc_usable_size(aPtr), aSize); |
| 123 |
jemalloc_ptr_info(aPtr, &jeInfo); |
| 124 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagLiveAlloc, aPtr, aSize, 0)); |
| 125 |
|
| 126 |
// Test an in-use PHC allocation: last byte within it. |
| 127 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize - 1, &phcInfo)); |
| 128 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::InUsePage, aPtr, aSize, |
| 129 |
true, false)); |
| 130 |
ASSERT_EQ(moz_malloc_usable_size(aPtr + aSize - 1), aSize); |
| 131 |
jemalloc_ptr_info(aPtr + aSize - 1, &jeInfo); |
| 132 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagLiveAlloc, aPtr, aSize, 0)); |
| 133 |
|
| 134 |
// Test an in-use PHC allocation: last byte before it. |
| 135 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr - 1, &phcInfo)); |
| 136 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::InUsePage, aPtr, aSize, |
| 137 |
true, false)); |
| 138 |
ASSERT_EQ(moz_malloc_usable_size(aPtr - 1), 0ul); |
| 139 |
jemalloc_ptr_info(aPtr - 1, &jeInfo); |
| 140 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 141 |
|
| 142 |
// Test an in-use PHC allocation: first byte on its allocation page. |
| 143 |
ASSERT_TRUE( |
| 144 |
mozilla::phc::IsPHCAllocation(aPtr + aSize - kPageSize, &phcInfo)); |
| 145 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::InUsePage, aPtr, aSize, |
| 146 |
true, false)); |
| 147 |
jemalloc_ptr_info(aPtr + aSize - kPageSize, &jeInfo); |
| 148 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 149 |
|
| 150 |
// Test an in-use PHC allocation: first byte in the following guard page. |
| 151 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize, &phcInfo)); |
| 152 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 153 |
true, false)); |
| 154 |
jemalloc_ptr_info(aPtr + aSize, &jeInfo); |
| 155 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 156 |
|
| 157 |
// Test an in-use PHC allocation: last byte in the lower half of the |
| 158 |
// following guard page. |
| 159 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize + (kPageSize / 2 - 1), |
| 160 |
&phcInfo)); |
| 161 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 162 |
true, false)); |
| 163 |
jemalloc_ptr_info(aPtr + aSize + (kPageSize / 2 - 1), &jeInfo); |
| 164 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 165 |
|
| 166 |
// Test an in-use PHC allocation: last byte in the preceding guard page. |
| 167 |
ASSERT_TRUE( |
| 168 |
mozilla::phc::IsPHCAllocation(aPtr + aSize - 1 - kPageSize, &phcInfo)); |
| 169 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 170 |
true, false)); |
| 171 |
jemalloc_ptr_info(aPtr + aSize - 1 - kPageSize, &jeInfo); |
| 172 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 173 |
|
| 174 |
// Test an in-use PHC allocation: first byte in the upper half of the |
| 175 |
// preceding guard page. |
| 176 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation( |
| 177 |
aPtr + aSize - 1 - kPageSize - (kPageSize / 2 - 1), &phcInfo)); |
| 178 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 179 |
true, false)); |
| 180 |
jemalloc_ptr_info(aPtr + aSize - 1 - kPageSize - (kPageSize / 2 - 1), |
| 181 |
&jeInfo); |
| 182 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 183 |
} |
| 184 |
|
| 185 |
static void TestFreedAllocation(uint8_t* aPtr, size_t aSize) { |
| 186 |
phc::AddrInfo phcInfo; |
| 187 |
jemalloc_ptr_info_t jeInfo; |
| 188 |
|
| 189 |
// Test a freed PHC allocation: first byte within it. |
| 190 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr, &phcInfo)); |
| 191 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::FreedPage, aPtr, aSize, |
| 192 |
true, true)); |
| 193 |
jemalloc_ptr_info(aPtr, &jeInfo); |
| 194 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagFreedAlloc, aPtr, aSize, 0)); |
| 195 |
|
| 196 |
// Test a freed PHC allocation: last byte within it. |
| 197 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize - 1, &phcInfo)); |
| 198 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::FreedPage, aPtr, aSize, |
| 199 |
true, true)); |
| 200 |
jemalloc_ptr_info(aPtr + aSize - 1, &jeInfo); |
| 201 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagFreedAlloc, aPtr, aSize, 0)); |
| 202 |
|
| 203 |
// Test a freed PHC allocation: last byte before it. |
| 204 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr - 1, &phcInfo)); |
| 205 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::FreedPage, aPtr, aSize, |
| 206 |
true, true)); |
| 207 |
jemalloc_ptr_info(aPtr - 1, &jeInfo); |
| 208 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 209 |
|
| 210 |
// Test a freed PHC allocation: first byte on its allocation page. |
| 211 |
ASSERT_TRUE( |
| 212 |
mozilla::phc::IsPHCAllocation(aPtr + aSize - kPageSize, &phcInfo)); |
| 213 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::FreedPage, aPtr, aSize, |
| 214 |
true, true)); |
| 215 |
jemalloc_ptr_info(aPtr + aSize - kPageSize, &jeInfo); |
| 216 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 217 |
|
| 218 |
// Test a freed PHC allocation: first byte in the following guard page. |
| 219 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize, &phcInfo)); |
| 220 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 221 |
true, true)); |
| 222 |
jemalloc_ptr_info(aPtr + aSize, &jeInfo); |
| 223 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 224 |
|
| 225 |
// Test a freed PHC allocation: last byte in the lower half of the following |
| 226 |
// guard page. |
| 227 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(aPtr + aSize + (kPageSize / 2 - 1), |
| 228 |
&phcInfo)); |
| 229 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 230 |
true, true)); |
| 231 |
jemalloc_ptr_info(aPtr + aSize + (kPageSize / 2 - 1), &jeInfo); |
| 232 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 233 |
|
| 234 |
// Test a freed PHC allocation: last byte in the preceding guard page. |
| 235 |
ASSERT_TRUE( |
| 236 |
mozilla::phc::IsPHCAllocation(aPtr + aSize - 1 - kPageSize, &phcInfo)); |
| 237 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 238 |
true, true)); |
| 239 |
jemalloc_ptr_info(aPtr + aSize - 1 - kPageSize, &jeInfo); |
| 240 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 241 |
|
| 242 |
// Test a freed PHC allocation: first byte in the upper half of the preceding |
| 243 |
// guard page. |
| 244 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation( |
| 245 |
aPtr + aSize - 1 - kPageSize - (kPageSize / 2 - 1), &phcInfo)); |
| 246 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::GuardPage, aPtr, aSize, |
| 247 |
true, true)); |
| 248 |
jemalloc_ptr_info(aPtr + aSize - 1 - kPageSize - (kPageSize / 2 - 1), |
| 249 |
&jeInfo); |
| 250 |
ASSERT_TRUE(JeInfoEq(jeInfo, TagUnknown, nullptr, 0, 0)); |
| 251 |
} |
| 252 |
|
| 253 |
TEST(PHC, TestPHCInfo) |
| 254 |
{ |
| 255 |
mozilla::phc::SetPHCState(phc::PHCState::Enabled); |
| 256 |
|
| 257 |
int stackVar; |
| 258 |
phc::AddrInfo phcInfo; |
| 259 |
|
| 260 |
// Test a default AddrInfo. |
| 261 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::Unknown, nullptr, 0ul, |
| 262 |
false, false)); |
| 263 |
|
| 264 |
// Test some non-PHC allocation addresses. |
| 265 |
ASSERT_FALSE(mozilla::phc::IsPHCAllocation(nullptr, &phcInfo)); |
| 266 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::Unknown, nullptr, 0, |
| 267 |
false, false)); |
| 268 |
ASSERT_FALSE(mozilla::phc::IsPHCAllocation(&stackVar, &phcInfo)); |
| 269 |
ASSERT_TRUE(PHCInfoEq(phcInfo, phc::AddrInfo::Kind::Unknown, nullptr, 0, |
| 270 |
false, false)); |
| 271 |
|
| 272 |
uint8_t* p = GetPHCAllocation(32); |
| 273 |
if (!p) { |
| 274 |
MOZ_CRASH("failed to get a PHC allocation"); |
| 275 |
} |
| 276 |
|
| 277 |
TestInUseAllocation(p, 32); |
| 278 |
|
| 279 |
free(p); |
| 280 |
|
| 281 |
TestFreedAllocation(p, 32); |
| 282 |
|
| 283 |
// There are no tests for `mKind == NeverAllocatedPage` because it's not |
| 284 |
// possible to reliably get ahold of such a page. |
| 285 |
} |
| 286 |
|
| 287 |
TEST(PHC, TestPHCDisablingThread) |
| 288 |
{ |
| 289 |
mozilla::phc::SetPHCState(phc::PHCState::Enabled); |
| 290 |
|
| 291 |
uint8_t* p = GetPHCAllocation(32); |
| 292 |
uint8_t* q = GetPHCAllocation(32); |
| 293 |
if (!p || !q) { |
| 294 |
MOZ_CRASH("failed to get a PHC allocation"); |
| 295 |
} |
| 296 |
|
| 297 |
ASSERT_TRUE(mozilla::phc::IsPHCEnabledOnCurrentThread()); |
| 298 |
mozilla::phc::DisablePHCOnCurrentThread(); |
| 299 |
ASSERT_FALSE(mozilla::phc::IsPHCEnabledOnCurrentThread()); |
| 300 |
|
| 301 |
// Test realloc() on a PHC allocation while PHC is disabled on the thread. |
| 302 |
uint8_t* p2 = (uint8_t*)realloc(p, 128); |
| 303 |
// The small realloc is fulfilled within the same page, but it does move. |
| 304 |
ASSERT_TRUE(p2 == p - 96); |
| 305 |
ASSERT_TRUE(mozilla::phc::IsPHCAllocation(p2, nullptr)); |
| 306 |
uint8_t* p3 = (uint8_t*)realloc(p2, 2 * kPageSize); |
| 307 |
// The big realloc is not in-place, and the result is not a PHC allocation. |
| 308 |
ASSERT_TRUE(p3 != p2); |
| 309 |
ASSERT_FALSE(mozilla::phc::IsPHCAllocation(p3, nullptr)); |
| 310 |
free(p3); |
| 311 |
|
| 312 |
// Test free() on a PHC allocation while PHC is disabled on the thread. |
| 313 |
free(q); |
| 314 |
|
| 315 |
// These must not be PHC allocations. |
| 316 |
uint8_t* r = GetPHCAllocation(32); // This will fail. |
| 317 |
ASSERT_FALSE(!!r); |
| 318 |
|
| 319 |
mozilla::phc::ReenablePHCOnCurrentThread(); |
| 320 |
ASSERT_TRUE(mozilla::phc::IsPHCEnabledOnCurrentThread()); |
| 321 |
|
| 322 |
// If it really was reenabled we should be able to get PHC allocations |
| 323 |
// again. |
| 324 |
uint8_t* s = GetPHCAllocation(32); // This should succeed. |
| 325 |
ASSERT_TRUE(!!s); |
| 326 |
free(s); |
| 327 |
} |
| 328 |
|
| 329 |
TEST(PHC, TestPHCDisablingGlobal) |
| 330 |
{ |
| 331 |
mozilla::phc::SetPHCState(phc::PHCState::Enabled); |
| 332 |
|
| 333 |
uint8_t* p1 = GetPHCAllocation(32); |
| 334 |
uint8_t* p2 = GetPHCAllocation(32); |
| 335 |
uint8_t* q = GetPHCAllocation(32); |
| 336 |
if (!p1 || !p2 || !q) { |
| 337 |
MOZ_CRASH("failed to get a PHC allocation"); |
| 338 |
} |
| 339 |
|
| 340 |
mozilla::phc::SetPHCState(phc::PHCState::OnlyFree); |
| 341 |
|
| 342 |
// Test realloc() on a PHC allocation while PHC is disabled on the thread. |
| 343 |
uint8_t* p3 = (uint8_t*)realloc(p1, 128); |
| 344 |
// The small realloc is evicted from PHC because in "OnlyFree" state PHC |
| 345 |
// tries to reduce its memory impact. |
| 346 |
ASSERT_TRUE(p3 != p1); |
| 347 |
ASSERT_FALSE(mozilla::phc::IsPHCAllocation(p3, nullptr)); |
| 348 |
free(p3); |
| 349 |
uint8_t* p4 = (uint8_t*)realloc(p2, 2 * kPageSize); |
| 350 |
// The big realloc is not in-place, and the result is not a PHC |
| 351 |
// allocation, regardless of PHC's state. |
| 352 |
ASSERT_TRUE(p4 != p2); |
| 353 |
ASSERT_FALSE(mozilla::phc::IsPHCAllocation(p4, nullptr)); |
| 354 |
free(p4); |
| 355 |
|
| 356 |
// Test free() on a PHC allocation while PHC is disabled on the thread. |
| 357 |
free(q); |
| 358 |
|
| 359 |
// These must not be PHC allocations. |
| 360 |
uint8_t* r = GetPHCAllocation(32); // This will fail. |
| 361 |
ASSERT_FALSE(!!r); |
| 362 |
|
| 363 |
mozilla::phc::SetPHCState(phc::PHCState::Enabled); |
| 364 |
|
| 365 |
// If it really was reenabled we should be able to get PHC allocations |
| 366 |
// again. |
| 367 |
uint8_t* s = GetPHCAllocation(32); // This should succeed. |
| 368 |
ASSERT_TRUE(!!s); |
| 369 |
free(s); |
| 370 |
} |
| 371 |
|
| 372 |
// This test is disabled for now, see Bug 1845017 and Bug 1845655. |
| 373 |
// TEST(PHC, TestPHCExhaustion) |
| 374 |
// { |
| 375 |
void DisabledPHCExhaustionTest() { |
| 376 |
// PHC hardcodes the amount of allocations to track. |
| 377 |
#if defined(XP_DARWIN) && defined(__aarch64__) |
| 378 |
const unsigned NUM_ALLOCATIONS = 1024; |
| 379 |
#else |
| 380 |
const unsigned NUM_ALLOCATIONS = 4096; |
| 381 |
#endif |
| 382 |
uint8_t* allocations[NUM_ALLOCATIONS]; |
| 383 |
const unsigned REQUIRED_ALLOCATIONS = NUM_ALLOCATIONS - 50; |
| 384 |
|
| 385 |
unsigned last_allocation; |
| 386 |
for (unsigned i = 0; i < NUM_ALLOCATIONS; i++) { |
| 387 |
allocations[i] = GetPHCAllocation(128); |
| 388 |
last_allocation = i; |
| 389 |
if (i < REQUIRED_ALLOCATIONS) { |
| 390 |
// Assert that the first REQUIRED_ALLOCATIONS work. We require only |
| 391 |
// REQUIRED_ALLOCATIONS rather than NUM_ALLOCATIONS because sometimes |
| 392 |
// some PHC slots are used before the test begins. |
| 393 |
ASSERT_TRUE(allocations[i]); |
| 394 |
} else if (!allocations[i]) { |
| 395 |
// Break the loop if an allocation fails to move to the next phase. |
| 396 |
last_allocation--; |
| 397 |
break; |
| 398 |
} |
| 399 |
TestInUseAllocation(allocations[i], 128); |
| 400 |
} |
| 401 |
|
| 402 |
// We should now fail to get an allocation. |
| 403 |
ASSERT_FALSE(GetPHCAllocation(128)); |
| 404 |
|
| 405 |
for (unsigned i = 0; i <= last_allocation; i++) { |
| 406 |
free(allocations[i]); |
| 407 |
TestFreedAllocation(allocations[i], 128); |
| 408 |
} |
| 409 |
|
| 410 |
// And now that we've released those allocations we should be able to get |
| 411 |
// new allocations again. |
| 412 |
uint8_t* r = GetPHCAllocation(128); |
| 413 |
ASSERT_TRUE(!!r); |
| 414 |
free(r); |
| 415 |
} |