Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
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 | #ifdef JSGC_GENERATIONAL |
michael@0 | 9 | |
michael@0 | 10 | #include "js/RootingAPI.h" |
michael@0 | 11 | #include "jsapi-tests/tests.h" |
michael@0 | 12 | |
michael@0 | 13 | BEGIN_TEST(testGCHeapPostBarriers) |
michael@0 | 14 | { |
michael@0 | 15 | /* Sanity check - objects start in the nursery and then become tenured. */ |
michael@0 | 16 | JS_GC(cx->runtime()); |
michael@0 | 17 | JS::RootedObject obj(cx, NurseryObject()); |
michael@0 | 18 | CHECK(js::gc::IsInsideNursery(obj.get())); |
michael@0 | 19 | JS_GC(cx->runtime()); |
michael@0 | 20 | CHECK(!js::gc::IsInsideNursery(obj.get())); |
michael@0 | 21 | JS::RootedObject tenuredObject(cx, obj); |
michael@0 | 22 | |
michael@0 | 23 | /* Currently JSObject and JSFunction objects are nursery allocated. */ |
michael@0 | 24 | CHECK(TestHeapPostBarriers(NurseryObject())); |
michael@0 | 25 | CHECK(TestHeapPostBarriers(NurseryFunction())); |
michael@0 | 26 | |
michael@0 | 27 | return true; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | template <typename T> |
michael@0 | 31 | bool |
michael@0 | 32 | TestHeapPostBarriers(T initialObj) |
michael@0 | 33 | { |
michael@0 | 34 | CHECK(initialObj != nullptr); |
michael@0 | 35 | CHECK(js::gc::IsInsideNursery(initialObj)); |
michael@0 | 36 | |
michael@0 | 37 | /* Construct Heap<> wrapper. */ |
michael@0 | 38 | JS::Heap<T> *heapData = new JS::Heap<T>(); |
michael@0 | 39 | CHECK(heapData); |
michael@0 | 40 | CHECK(heapData->get() == nullptr); |
michael@0 | 41 | heapData->set(initialObj); |
michael@0 | 42 | |
michael@0 | 43 | /* Store the pointer as an integer so that the hazard analysis will miss it. */ |
michael@0 | 44 | uintptr_t initialObjAsInt = uintptr_t(initialObj); |
michael@0 | 45 | |
michael@0 | 46 | /* Perform minor GC and check heap wrapper is udated with new pointer. */ |
michael@0 | 47 | js::MinorGC(cx, JS::gcreason::API); |
michael@0 | 48 | CHECK(uintptr_t(heapData->get()) != initialObjAsInt); |
michael@0 | 49 | CHECK(!js::gc::IsInsideNursery(heapData->get())); |
michael@0 | 50 | |
michael@0 | 51 | /* Check object is definitely still alive. */ |
michael@0 | 52 | JS::Rooted<T> obj(cx, heapData->get()); |
michael@0 | 53 | JS::RootedValue value(cx); |
michael@0 | 54 | CHECK(JS_GetProperty(cx, obj, "x", &value)); |
michael@0 | 55 | CHECK(value.isInt32()); |
michael@0 | 56 | CHECK(value.toInt32() == 42); |
michael@0 | 57 | |
michael@0 | 58 | delete heapData; |
michael@0 | 59 | return true; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | JSObject *NurseryObject() |
michael@0 | 63 | { |
michael@0 | 64 | JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); |
michael@0 | 65 | if (!obj) |
michael@0 | 66 | return nullptr; |
michael@0 | 67 | JS_DefineProperty(cx, obj, "x", 42, 0); |
michael@0 | 68 | return obj; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | JSFunction *NurseryFunction() |
michael@0 | 72 | { |
michael@0 | 73 | /* |
michael@0 | 74 | * We don't actually use the function as a function, so here we cheat and |
michael@0 | 75 | * cast a JSObject. |
michael@0 | 76 | */ |
michael@0 | 77 | return static_cast<JSFunction *>(NurseryObject()); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | END_TEST(testGCHeapPostBarriers) |
michael@0 | 81 | |
michael@0 | 82 | #endif |