1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testGCHeapPostBarriers.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 +* vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 +*/ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#ifdef JSGC_GENERATIONAL 1.12 + 1.13 +#include "js/RootingAPI.h" 1.14 +#include "jsapi-tests/tests.h" 1.15 + 1.16 +BEGIN_TEST(testGCHeapPostBarriers) 1.17 +{ 1.18 + /* Sanity check - objects start in the nursery and then become tenured. */ 1.19 + JS_GC(cx->runtime()); 1.20 + JS::RootedObject obj(cx, NurseryObject()); 1.21 + CHECK(js::gc::IsInsideNursery(obj.get())); 1.22 + JS_GC(cx->runtime()); 1.23 + CHECK(!js::gc::IsInsideNursery(obj.get())); 1.24 + JS::RootedObject tenuredObject(cx, obj); 1.25 + 1.26 + /* Currently JSObject and JSFunction objects are nursery allocated. */ 1.27 + CHECK(TestHeapPostBarriers(NurseryObject())); 1.28 + CHECK(TestHeapPostBarriers(NurseryFunction())); 1.29 + 1.30 + return true; 1.31 +} 1.32 + 1.33 +template <typename T> 1.34 +bool 1.35 +TestHeapPostBarriers(T initialObj) 1.36 +{ 1.37 + CHECK(initialObj != nullptr); 1.38 + CHECK(js::gc::IsInsideNursery(initialObj)); 1.39 + 1.40 + /* Construct Heap<> wrapper. */ 1.41 + JS::Heap<T> *heapData = new JS::Heap<T>(); 1.42 + CHECK(heapData); 1.43 + CHECK(heapData->get() == nullptr); 1.44 + heapData->set(initialObj); 1.45 + 1.46 + /* Store the pointer as an integer so that the hazard analysis will miss it. */ 1.47 + uintptr_t initialObjAsInt = uintptr_t(initialObj); 1.48 + 1.49 + /* Perform minor GC and check heap wrapper is udated with new pointer. */ 1.50 + js::MinorGC(cx, JS::gcreason::API); 1.51 + CHECK(uintptr_t(heapData->get()) != initialObjAsInt); 1.52 + CHECK(!js::gc::IsInsideNursery(heapData->get())); 1.53 + 1.54 + /* Check object is definitely still alive. */ 1.55 + JS::Rooted<T> obj(cx, heapData->get()); 1.56 + JS::RootedValue value(cx); 1.57 + CHECK(JS_GetProperty(cx, obj, "x", &value)); 1.58 + CHECK(value.isInt32()); 1.59 + CHECK(value.toInt32() == 42); 1.60 + 1.61 + delete heapData; 1.62 + return true; 1.63 +} 1.64 + 1.65 +JSObject *NurseryObject() 1.66 +{ 1.67 + JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.68 + if (!obj) 1.69 + return nullptr; 1.70 + JS_DefineProperty(cx, obj, "x", 42, 0); 1.71 + return obj; 1.72 +} 1.73 + 1.74 +JSFunction *NurseryFunction() 1.75 +{ 1.76 + /* 1.77 + * We don't actually use the function as a function, so here we cheat and 1.78 + * cast a JSObject. 1.79 + */ 1.80 + return static_cast<JSFunction *>(NurseryObject()); 1.81 +} 1.82 + 1.83 +END_TEST(testGCHeapPostBarriers) 1.84 + 1.85 +#endif