js/src/jsapi-tests/testOOM.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 #include "mozilla/DebugOnly.h"
     7 #include "jsapi-tests/tests.h"
     9 BEGIN_TEST(testOOM)
    10 {
    11     JS::RootedValue v(cx, JS::Int32Value(9));
    12     JS::RootedString jsstr(cx, JS::ToString(cx, v));
    13     mozilla::DebugOnly<const jschar *> s = JS_GetStringCharsZ(cx, jsstr);
    14     JS_ASSERT(s[0] == '9' && s[1] == '\0');
    15     return true;
    16 }
    18 virtual JSRuntime * createRuntime()
    19 {
    20     JSRuntime *rt = JS_NewRuntime(0, JS_USE_HELPER_THREADS);
    21     if (!rt)
    22         return nullptr;
    23     JS_SetGCParameter(rt, JSGC_MAX_BYTES, (uint32_t)-1);
    24     setNativeStackQuota(rt);
    25     return rt;
    26 }
    27 END_TEST(testOOM)
    29 #ifdef DEBUG  // OOM_maxAllocations is only available in debug builds.
    31 const uint32_t maxAllocsPerTest = 100;
    33 #define START_OOM_TEST(name)                                                  \
    34     testName = name;                                                          \
    35     printf("Test %s: started\n", testName);                                   \
    36     for (oomAfter = 1; oomAfter < maxAllocsPerTest; ++oomAfter) {             \
    37         setOOMAfter(oomAfter)
    39 #define OOM_TEST_FINISHED                                                     \
    40     {                                                                         \
    41         printf("Test %s: finished with %d allocations\n",                     \
    42                testName, oomAfter - 1);                                       \
    43         break;                                                                \
    44     }
    46 #define END_OOM_TEST                                                          \
    47     }                                                                         \
    48     cancelOOMAfter();                                                         \
    49     CHECK(oomAfter != maxAllocsPerTest)
    51 BEGIN_TEST(testNewRuntime)
    52 {
    53     uninit(); // Get rid of test harness' original JSRuntime.
    55     JSRuntime *rt;
    56     START_OOM_TEST("new runtime");
    57     rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS);
    58     if (rt)
    59         OOM_TEST_FINISHED;
    60     END_OOM_TEST;
    61     JS_DestroyRuntime(rt);
    62     return true;
    63 }
    65 const char* testName;
    66 uint32_t oomAfter;
    68 void
    69 setOOMAfter(uint32_t numAllocs)
    70 {
    71     OOM_maxAllocations = OOM_counter + numAllocs;
    72 }
    74 void
    75 cancelOOMAfter()
    76 {
    77     OOM_maxAllocations = UINT32_MAX;
    78 }
    79 END_TEST(testNewRuntime)
    81 #endif

mercurial