Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/heap/heap.h"
6 : #include "src/roots-inl.h"
7 : #include "test/cctest/cctest.h"
8 :
9 : namespace v8 {
10 : namespace internal {
11 :
12 : namespace {
13 2835 : AllocationSpace GetSpaceFromObject(Object object) {
14 : DCHECK(object->IsHeapObject());
15 : return MemoryChunk::FromHeapObject(HeapObject::cast(object))
16 : ->owner()
17 2835 : ->identity();
18 : }
19 : } // namespace
20 :
21 : #define CHECK_IN_RO_SPACE(type, name, CamelName) \
22 : HeapObject name = roots.name(); \
23 : CHECK_EQ(RO_SPACE, GetSpaceFromObject(name));
24 :
25 : // The following tests check that all the roots accessible via ReadOnlyRoots are
26 : // in RO_SPACE.
27 28342 : TEST(TestReadOnlyRoots) {
28 : ReadOnlyRoots roots(CcTest::i_isolate());
29 :
30 2630 : READ_ONLY_ROOT_LIST(CHECK_IN_RO_SPACE)
31 5 : }
32 :
33 : #undef CHECK_IN_RO_SPACE
34 :
35 : namespace {
36 280 : bool IsInitiallyMutable(Factory* factory, Address object_address) {
37 : // Entries in this list are in STRONG_MUTABLE_MOVABLE_ROOT_LIST, but may
38 : // initially point to objects that are in RO_SPACE.
39 : #define INITIALLY_READ_ONLY_ROOT_LIST(V) \
40 : V(api_private_symbol_table) \
41 : V(api_symbol_table) \
42 : V(builtins_constants_table) \
43 : V(current_microtask) \
44 : V(detached_contexts) \
45 : V(dirty_js_weak_factories) \
46 : V(feedback_vectors_for_profiling_tools) \
47 : V(materialized_objects) \
48 : V(noscript_shared_function_infos) \
49 : V(public_symbol_table) \
50 : V(retained_maps) \
51 : V(retaining_path_targets) \
52 : V(serialized_global_proxy_sizes) \
53 : V(serialized_objects) \
54 : V(weak_refs_keep_during_job)
55 :
56 : #define TEST_CAN_BE_READ_ONLY(name) \
57 : if (factory->name().address() == object_address) return false;
58 3675 : INITIALLY_READ_ONLY_ROOT_LIST(TEST_CAN_BE_READ_ONLY)
59 : #undef TEST_CAN_BE_READ_ONLY
60 : #undef INITIALLY_READ_ONLY_ROOT_LIST
61 205 : return true;
62 : }
63 : } // namespace
64 :
65 : // The CHECK_EQ line is there just to ensure that the root is publicly
66 : // accessible from Heap, but ultimately the factory is used as it provides
67 : // handles that have the address in the root table.
68 : #define CHECK_NOT_IN_RO_SPACE(type, name, CamelName) \
69 : Handle<Object> name = factory->name(); \
70 : CHECK_EQ(*name, heap->name()); \
71 : if (name->IsHeapObject() && IsInitiallyMutable(factory, name.address())) \
72 : CHECK_NE(RO_SPACE, GetSpaceFromObject(HeapObject::cast(*name)));
73 :
74 : // The following tests check that all the roots accessible via public Heap
75 : // accessors are not in RO_SPACE with the exception of the objects listed in
76 : // INITIALLY_READ_ONLY_ROOT_LIST.
77 28342 : TEST(TestHeapRootsNotReadOnly) {
78 : Factory* factory = CcTest::i_isolate()->factory();
79 5 : Heap* heap = CcTest::i_isolate()->heap();
80 :
81 1830 : MUTABLE_ROOT_LIST(CHECK_NOT_IN_RO_SPACE)
82 5 : }
83 :
84 : #undef CHECK_NOT_IN_RO_SPACE
85 :
86 : } // namespace internal
87 85011 : } // namespace v8
|