xpcom/glue/tests/gtest/TestGCPostBarriers.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 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=8 et :
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 /*
michael@0 9 * Tests that generational garbage collection post-barriers are correctly
michael@0 10 * implemented for nsTArrays that contain JavaScript Values.
michael@0 11 */
michael@0 12
michael@0 13 #include "jsapi.h"
michael@0 14 #include "nsTArray.h"
michael@0 15
michael@0 16 #include "gtest/gtest.h"
michael@0 17
michael@0 18 #include "js/TracingAPI.h"
michael@0 19 #include "js/HeapAPI.h"
michael@0 20
michael@0 21 #include "mozilla/CycleCollectedJSRuntime.h"
michael@0 22
michael@0 23 using namespace JS;
michael@0 24 using namespace mozilla;
michael@0 25
michael@0 26 template <class ArrayT>
michael@0 27 static void
michael@0 28 TraceArray(JSTracer* trc, void* data)
michael@0 29 {
michael@0 30 ArrayT* array = static_cast<ArrayT *>(data);
michael@0 31 for (unsigned i = 0; i < array->Length(); ++i)
michael@0 32 JS_CallHeapObjectTracer(trc, &array->ElementAt(i), "array-element");
michael@0 33 }
michael@0 34
michael@0 35 /*
michael@0 36 * Use arrays with initial size much smaller than the final number of elements
michael@0 37 * to test that moving Heap<T> elements works correctly.
michael@0 38 */
michael@0 39 const size_t ElementCount = 100;
michael@0 40 const size_t InitialElements = ElementCount / 10;
michael@0 41
michael@0 42 template <class ArrayT>
michael@0 43 static void
michael@0 44 RunTest(JSRuntime* rt, JSContext* cx, ArrayT* array)
michael@0 45 {
michael@0 46 JS_GC(rt);
michael@0 47
michael@0 48 ASSERT_TRUE(array != nullptr);
michael@0 49 JS_AddExtraGCRootsTracer(rt, TraceArray<ArrayT>, array);
michael@0 50
michael@0 51 /*
michael@0 52 * Create the array and fill it with new JS objects. With GGC these will be
michael@0 53 * allocated in the nursery.
michael@0 54 */
michael@0 55 RootedValue value(cx);
michael@0 56 const char* property = "foo";
michael@0 57 JS::shadow::Runtime* srt = reinterpret_cast<JS::shadow::Runtime*>(rt);
michael@0 58 for (size_t i = 0; i < ElementCount; ++i) {
michael@0 59 RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
michael@0 60 #ifdef JSGC_GENERATIONAL
michael@0 61 ASSERT_TRUE(js::gc::IsInsideNursery(srt, obj));
michael@0 62 #else
michael@0 63 ASSERT_FALSE(js::gc::IsInsideNursery(srt, obj));
michael@0 64 #endif
michael@0 65 value = Int32Value(i);
michael@0 66 ASSERT_TRUE(JS_SetProperty(cx, obj, property, value));
michael@0 67 array->AppendElement(obj);
michael@0 68 }
michael@0 69
michael@0 70 /*
michael@0 71 * If postbarriers are not working, we will crash here when we try to mark
michael@0 72 * objects that have been moved to the tenured heap.
michael@0 73 */
michael@0 74 JS_GC(rt);
michael@0 75
michael@0 76 /*
michael@0 77 * Sanity check that our array contains what we expect.
michael@0 78 */
michael@0 79 for (size_t i = 0; i < ElementCount; ++i) {
michael@0 80 RootedObject obj(cx, array->ElementAt(i));
michael@0 81 ASSERT_FALSE(js::gc::IsInsideNursery(srt, obj));
michael@0 82 ASSERT_TRUE(JS_GetProperty(cx, obj, property, &value));
michael@0 83 ASSERT_TRUE(value.isInt32());
michael@0 84 ASSERT_EQ(static_cast<int32_t>(i), value.toInt32());
michael@0 85 }
michael@0 86
michael@0 87 JS_RemoveExtraGCRootsTracer(rt, TraceArray<ArrayT>, array);
michael@0 88 }
michael@0 89
michael@0 90 static void
michael@0 91 CreateGlobalAndRunTest(JSRuntime* rt, JSContext* cx)
michael@0 92 {
michael@0 93 static const JSClass GlobalClass = {
michael@0 94 "global", JSCLASS_GLOBAL_FLAGS,
michael@0 95 JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
michael@0 96 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
michael@0 97 nullptr, nullptr, nullptr, nullptr,
michael@0 98 JS_GlobalObjectTraceHook
michael@0 99 };
michael@0 100
michael@0 101 JS::CompartmentOptions options;
michael@0 102 options.setVersion(JSVERSION_LATEST);
michael@0 103 JS::PersistentRootedObject global(cx);
michael@0 104 global = JS_NewGlobalObject(cx, &GlobalClass, nullptr, JS::FireOnNewGlobalHook, options);
michael@0 105 ASSERT_TRUE(global != nullptr);
michael@0 106
michael@0 107 JSCompartment *oldCompartment = JS_EnterCompartment(cx, global);
michael@0 108
michael@0 109 typedef Heap<JSObject*> ElementT;
michael@0 110
michael@0 111 {
michael@0 112 nsTArray<ElementT>* array = new nsTArray<ElementT>(InitialElements);
michael@0 113 RunTest(rt, cx, array);
michael@0 114 delete array;
michael@0 115 }
michael@0 116
michael@0 117 {
michael@0 118 FallibleTArray<ElementT>* array = new FallibleTArray<ElementT>(InitialElements);
michael@0 119 RunTest(rt, cx, array);
michael@0 120 delete array;
michael@0 121 }
michael@0 122
michael@0 123 {
michael@0 124 nsAutoTArray<ElementT, InitialElements> array;
michael@0 125 RunTest(rt, cx, &array);
michael@0 126 }
michael@0 127
michael@0 128 {
michael@0 129 AutoFallibleTArray<ElementT, InitialElements> array;
michael@0 130 RunTest(rt, cx, &array);
michael@0 131 }
michael@0 132
michael@0 133 JS_LeaveCompartment(cx, oldCompartment);
michael@0 134 }
michael@0 135
michael@0 136 TEST(GCPostBarriers, nsTArray) {
michael@0 137 CycleCollectedJSRuntime* ccrt = CycleCollectedJSRuntime::Get();
michael@0 138 ASSERT_TRUE(ccrt != nullptr);
michael@0 139 JSRuntime* rt = ccrt->Runtime();
michael@0 140 ASSERT_TRUE(rt != nullptr);
michael@0 141
michael@0 142 JSContext *cx = JS_NewContext(rt, 8192);
michael@0 143 ASSERT_TRUE(cx != nullptr);
michael@0 144 JS_BeginRequest(cx);
michael@0 145
michael@0 146 CreateGlobalAndRunTest(rt, cx);
michael@0 147
michael@0 148 JS_EndRequest(cx);
michael@0 149 JS_DestroyContext(cx);
michael@0 150 }

mercurial