js/src/jsapi-tests/testGCFinalizeCallback.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "jsapi-tests/tests.h"
michael@0 6
michael@0 7 static const unsigned BufferSize = 20;
michael@0 8 static unsigned FinalizeCalls = 0;
michael@0 9 static JSFinalizeStatus StatusBuffer[BufferSize];
michael@0 10 static bool IsCompartmentGCBuffer[BufferSize];
michael@0 11
michael@0 12 BEGIN_TEST(testGCFinalizeCallback)
michael@0 13 {
michael@0 14 JS_SetGCParameter(rt, JSGC_MODE, JSGC_MODE_INCREMENTAL);
michael@0 15 JS_SetFinalizeCallback(rt, FinalizeCallback);
michael@0 16
michael@0 17 /* Full GC, non-incremental. */
michael@0 18 FinalizeCalls = 0;
michael@0 19 JS_GC(rt);
michael@0 20 CHECK(rt->gcIsFull);
michael@0 21 CHECK(checkSingleGroup());
michael@0 22 CHECK(checkFinalizeStatus());
michael@0 23 CHECK(checkFinalizeIsCompartmentGC(false));
michael@0 24
michael@0 25 /* Full GC, incremental. */
michael@0 26 FinalizeCalls = 0;
michael@0 27 JS::PrepareForFullGC(rt);
michael@0 28 JS::IncrementalGC(rt, JS::gcreason::API, 1000000);
michael@0 29 CHECK(rt->gcIncrementalState == js::gc::NO_INCREMENTAL);
michael@0 30 CHECK(rt->gcIsFull);
michael@0 31 CHECK(checkMultipleGroups());
michael@0 32 CHECK(checkFinalizeStatus());
michael@0 33 CHECK(checkFinalizeIsCompartmentGC(false));
michael@0 34
michael@0 35 JS::RootedObject global1(cx, createGlobal());
michael@0 36 JS::RootedObject global2(cx, createGlobal());
michael@0 37 JS::RootedObject global3(cx, createGlobal());
michael@0 38 CHECK(global1);
michael@0 39 CHECK(global2);
michael@0 40 CHECK(global3);
michael@0 41
michael@0 42 /* Compartment GC, non-incremental, single compartment. */
michael@0 43 FinalizeCalls = 0;
michael@0 44 JS::PrepareZoneForGC(global1->zone());
michael@0 45 JS::GCForReason(rt, JS::gcreason::API);
michael@0 46 CHECK(!rt->gcIsFull);
michael@0 47 CHECK(checkSingleGroup());
michael@0 48 CHECK(checkFinalizeStatus());
michael@0 49 CHECK(checkFinalizeIsCompartmentGC(true));
michael@0 50
michael@0 51 /* Compartment GC, non-incremental, multiple compartments. */
michael@0 52 FinalizeCalls = 0;
michael@0 53 JS::PrepareZoneForGC(global1->zone());
michael@0 54 JS::PrepareZoneForGC(global2->zone());
michael@0 55 JS::PrepareZoneForGC(global3->zone());
michael@0 56 JS::GCForReason(rt, JS::gcreason::API);
michael@0 57 CHECK(!rt->gcIsFull);
michael@0 58 CHECK(checkSingleGroup());
michael@0 59 CHECK(checkFinalizeStatus());
michael@0 60 CHECK(checkFinalizeIsCompartmentGC(true));
michael@0 61
michael@0 62 /* Compartment GC, incremental, single compartment. */
michael@0 63 FinalizeCalls = 0;
michael@0 64 JS::PrepareZoneForGC(global1->zone());
michael@0 65 JS::IncrementalGC(rt, JS::gcreason::API, 1000000);
michael@0 66 CHECK(rt->gcIncrementalState == js::gc::NO_INCREMENTAL);
michael@0 67 CHECK(!rt->gcIsFull);
michael@0 68 CHECK(checkSingleGroup());
michael@0 69 CHECK(checkFinalizeStatus());
michael@0 70 CHECK(checkFinalizeIsCompartmentGC(true));
michael@0 71
michael@0 72 /* Compartment GC, incremental, multiple compartments. */
michael@0 73 FinalizeCalls = 0;
michael@0 74 JS::PrepareZoneForGC(global1->zone());
michael@0 75 JS::PrepareZoneForGC(global2->zone());
michael@0 76 JS::PrepareZoneForGC(global3->zone());
michael@0 77 JS::IncrementalGC(rt, JS::gcreason::API, 1000000);
michael@0 78 CHECK(rt->gcIncrementalState == js::gc::NO_INCREMENTAL);
michael@0 79 CHECK(!rt->gcIsFull);
michael@0 80 CHECK(checkMultipleGroups());
michael@0 81 CHECK(checkFinalizeStatus());
michael@0 82 CHECK(checkFinalizeIsCompartmentGC(true));
michael@0 83
michael@0 84 #ifdef JS_GC_ZEAL
michael@0 85
michael@0 86 /* Full GC with reset due to new compartment, becoming compartment GC. */
michael@0 87
michael@0 88 FinalizeCalls = 0;
michael@0 89 JS_SetGCZeal(cx, 9, 1000000);
michael@0 90 JS::PrepareForFullGC(rt);
michael@0 91 js::GCDebugSlice(rt, true, 1);
michael@0 92 CHECK(rt->gcIncrementalState == js::gc::MARK);
michael@0 93 CHECK(rt->gcIsFull);
michael@0 94
michael@0 95 JS::RootedObject global4(cx, createGlobal());
michael@0 96 js::GCDebugSlice(rt, true, 1);
michael@0 97 CHECK(rt->gcIncrementalState == js::gc::NO_INCREMENTAL);
michael@0 98 CHECK(!rt->gcIsFull);
michael@0 99 CHECK(checkMultipleGroups());
michael@0 100 CHECK(checkFinalizeStatus());
michael@0 101
michael@0 102 for (unsigned i = 0; i < FinalizeCalls - 1; ++i)
michael@0 103 CHECK(!IsCompartmentGCBuffer[i]);
michael@0 104 CHECK(IsCompartmentGCBuffer[FinalizeCalls - 1]);
michael@0 105
michael@0 106 JS_SetGCZeal(cx, 0, 0);
michael@0 107
michael@0 108 #endif
michael@0 109
michael@0 110 /*
michael@0 111 * Make some use of the globals here to ensure the compiler doesn't optimize
michael@0 112 * them away in release builds, causing the compartments to be collected and
michael@0 113 * the test to fail.
michael@0 114 */
michael@0 115 CHECK(JS_IsGlobalObject(global1));
michael@0 116 CHECK(JS_IsGlobalObject(global2));
michael@0 117 CHECK(JS_IsGlobalObject(global3));
michael@0 118
michael@0 119 JS_SetFinalizeCallback(rt, nullptr);
michael@0 120 return true;
michael@0 121 }
michael@0 122
michael@0 123 bool checkSingleGroup()
michael@0 124 {
michael@0 125 CHECK(FinalizeCalls < BufferSize);
michael@0 126 CHECK(FinalizeCalls == 3);
michael@0 127 return true;
michael@0 128 }
michael@0 129
michael@0 130 bool checkMultipleGroups()
michael@0 131 {
michael@0 132 CHECK(FinalizeCalls < BufferSize);
michael@0 133 CHECK(FinalizeCalls % 2 == 1);
michael@0 134 CHECK((FinalizeCalls - 1) / 2 > 1);
michael@0 135 return true;
michael@0 136 }
michael@0 137
michael@0 138 bool checkFinalizeStatus()
michael@0 139 {
michael@0 140 /*
michael@0 141 * The finalize callback should be called twice for each compartment group
michael@0 142 * finalized, with status JSFINALIZE_GROUP_START and JSFINALIZE_GROUP_END,
michael@0 143 * and then once more with JSFINALIZE_COLLECTION_END.
michael@0 144 */
michael@0 145
michael@0 146 for (unsigned i = 0; i < FinalizeCalls - 1; i += 2) {
michael@0 147 CHECK(StatusBuffer[i] == JSFINALIZE_GROUP_START);
michael@0 148 CHECK(StatusBuffer[i + 1] == JSFINALIZE_GROUP_END);
michael@0 149 }
michael@0 150
michael@0 151 CHECK(StatusBuffer[FinalizeCalls - 1] == JSFINALIZE_COLLECTION_END);
michael@0 152
michael@0 153 return true;
michael@0 154 }
michael@0 155
michael@0 156 bool checkFinalizeIsCompartmentGC(bool isCompartmentGC)
michael@0 157 {
michael@0 158 for (unsigned i = 0; i < FinalizeCalls; ++i)
michael@0 159 CHECK(IsCompartmentGCBuffer[i] == isCompartmentGC);
michael@0 160
michael@0 161 return true;
michael@0 162 }
michael@0 163
michael@0 164 static void
michael@0 165 FinalizeCallback(JSFreeOp *fop, JSFinalizeStatus status, bool isCompartmentGC)
michael@0 166 {
michael@0 167 if (FinalizeCalls < BufferSize) {
michael@0 168 StatusBuffer[FinalizeCalls] = status;
michael@0 169 IsCompartmentGCBuffer[FinalizeCalls] = isCompartmentGC;
michael@0 170 }
michael@0 171 ++FinalizeCalls;
michael@0 172 }
michael@0 173 END_TEST(testGCFinalizeCallback)

mercurial