DoesCrossCompartmentBoundaries.cpp (2395B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 // Test that heap snapshots cross compartment boundaries when expected. 7 8 #include "DevTools.h" 9 10 DEF_TEST(DoesCrossCompartmentBoundaries, { 11 // Create a new global to get a new compartment. 12 JS::RealmOptions options; 13 // dummy 14 options.behaviors().setReduceTimerPrecisionCallerType( 15 JS::RTPCallerTypeToken{0}); 16 JS::Rooted<JSObject*> newGlobal( 17 cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, 18 JS::FireOnNewGlobalHook, options)); 19 ASSERT_TRUE(newGlobal); 20 JS::Compartment* newCompartment = nullptr; 21 { 22 JSAutoRealm ar(cx, newGlobal); 23 ASSERT_TRUE(JS::InitRealmStandardClasses(cx)); 24 newCompartment = js::GetContextCompartment(cx); 25 } 26 ASSERT_TRUE(newCompartment); 27 ASSERT_NE(newCompartment, compartment); 28 29 // Our set of target compartments is both the old and new compartments. 30 JS::CompartmentSet targetCompartments; 31 ASSERT_TRUE(targetCompartments.put(compartment)); 32 ASSERT_TRUE(targetCompartments.put(newCompartment)); 33 34 FakeNode nodeA; 35 FakeNode nodeB; 36 FakeNode nodeC; 37 FakeNode nodeD; 38 39 nodeA.compartment = compartment; 40 nodeB.compartment = nullptr; 41 nodeC.compartment = newCompartment; 42 nodeD.compartment = nullptr; 43 44 AddEdge(nodeA, nodeB); 45 AddEdge(nodeA, nodeC); 46 AddEdge(nodeB, nodeD); 47 48 ::testing::NiceMock<MockWriter> writer; 49 50 // Should serialize nodeA, because it is in one of our target compartments. 51 ExpectWriteNode(writer, nodeA); 52 53 // Should serialize nodeB, because it doesn't belong to a compartment and is 54 // therefore assumed to be shared. 55 ExpectWriteNode(writer, nodeB); 56 57 // Should also serialize nodeC, which is in our target compartments, but a 58 // different compartment than A. 59 ExpectWriteNode(writer, nodeC); 60 61 // Should serialize nodeD because it's reachable via B and both nodes B and D 62 // don't belong to a specific compartment. 63 ExpectWriteNode(writer, nodeD); 64 65 JS::AutoCheckCannotGC noGC(cx); 66 67 ASSERT_TRUE(WriteHeapGraph(cx, JS::ubi::Node(&nodeA), writer, 68 /* wantNames = */ false, &targetCompartments, 69 noGC)); 70 });