| 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 "mozilla/DebugOnly.h" |
| 6 |
|
| 7 |
#include "jsapi-tests/tests.h" |
| 8 |
|
| 9 |
#include "vm/HelperThreads.h" |
| 10 |
|
| 11 |
BEGIN_TEST(testOOM) { |
| 12 |
JS::RootedValue v(cx, JS::Int32Value(9)); |
| 13 |
JS::RootedString jsstr(cx, JS::ToString(cx, v)); |
| 14 |
char16_t ch; |
| 15 |
if (!JS_GetStringCharAt(cx, jsstr, 0, &ch)) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
MOZ_RELEASE_ASSERT(ch == '9'); |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
virtual JSContext* createContext() override { |
| 23 |
JSContext* cx = JS_NewContext(0); |
| 24 |
if (!cx) { |
| 25 |
return nullptr; |
| 26 |
} |
| 27 |
JS_SetGCParameter(cx, JSGC_MAX_BYTES, (uint32_t)-1); |
| 28 |
return cx; |
| 29 |
} |
| 30 |
END_TEST(testOOM) |
| 31 |
|
| 32 |
#ifdef DEBUG // js::oom functions are only available in debug builds. |
| 33 |
|
| 34 |
const uint32_t maxAllocsPerTest = 100; |
| 35 |
|
| 36 |
# define START_OOM_TEST(name) \ |
| 37 |
testName = name; \ |
| 38 |
for (bool always : {false, true}) { \ |
| 39 |
const char* subTest = always ? "fail always" : "fail once"; \ |
| 40 |
printf("Test %s (%s): started: ", testName, subTest); \ |
| 41 |
for (oomAfter = 1; oomAfter < maxAllocsPerTest; ++oomAfter) { \ |
| 42 |
js::oom::simulator.simulateFailureAfter( \ |
| 43 |
js::oom::FailureSimulator::Kind::OOM, oomAfter, \ |
| 44 |
js::THREAD_TYPE_MAIN, always) |
| 45 |
|
| 46 |
# define END_OOM_TEST \ |
| 47 |
if (!js::oom::HadSimulatedOOM()) { \ |
| 48 |
printf("\nTest %s (%s): finished with %" PRIu64 " allocations\n", \ |
| 49 |
testName, subTest, oomAfter - 1); \ |
| 50 |
break; \ |
| 51 |
} \ |
| 52 |
} \ |
| 53 |
} \ |
| 54 |
js::oom::simulator.reset(); \ |
| 55 |
CHECK(oomAfter != maxAllocsPerTest) |
| 56 |
|
| 57 |
# define MARK_STAR printf("*"); |
| 58 |
# define MARK_PLUS printf("+"); |
| 59 |
# define MARK_DOT printf("."); |
| 60 |
|
| 61 |
BEGIN_TEST(testNewContextOOM) { |
| 62 |
uninit(); // Get rid of test harness' original JSContext. |
| 63 |
|
| 64 |
const char* testName; |
| 65 |
uint64_t oomAfter; |
| 66 |
JSContext* cx; |
| 67 |
START_OOM_TEST("new context"); |
| 68 |
cx = JS_NewContext(8L * 1024 * 1024); |
| 69 |
if (cx) { |
| 70 |
MARK_PLUS; |
| 71 |
JS_DestroyContext(cx); |
| 72 |
} else { |
| 73 |
MARK_DOT; |
| 74 |
CHECK(!JSRuntime::hasLiveRuntimes()); |
| 75 |
} |
| 76 |
END_OOM_TEST; |
| 77 |
return true; |
| 78 |
} |
| 79 |
END_TEST(testNewContextOOM) |
| 80 |
|
| 81 |
BEGIN_TEST(testHelperThreadOOM) { |
| 82 |
const char* testName; |
| 83 |
uint64_t oomAfter; |
| 84 |
START_OOM_TEST("helper thread state"); |
| 85 |
|
| 86 |
if (js::CreateHelperThreadsState()) { |
| 87 |
if (js::EnsureHelperThreadsInitialized()) { |
| 88 |
MARK_STAR; |
| 89 |
} else { |
| 90 |
MARK_PLUS; |
| 91 |
} |
| 92 |
} else { |
| 93 |
MARK_DOT; |
| 94 |
} |
| 95 |
|
| 96 |
// Reset the helper threads to ensure they get re-initalised in following |
| 97 |
// iterations |
| 98 |
js::DestroyHelperThreadsState(); |
| 99 |
|
| 100 |
END_OOM_TEST; |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool init() override { |
| 106 |
JSAPIRuntimeTest::uninit(); // Discard the just-created JSContext. |
| 107 |
js::DestroyHelperThreadsState(); // The test creates this state. |
| 108 |
return true; |
| 109 |
} |
| 110 |
void uninit() override { |
| 111 |
// Leave things initialized after this test finishes. |
| 112 |
js::CreateHelperThreadsState(); |
| 113 |
} |
| 114 |
|
| 115 |
END_TEST(testHelperThreadOOM) |
| 116 |
|
| 117 |
#endif |