test-roots.cc 3.15 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
#include "src/heap/heap-inl.h"
#include "src/objects/cell.h"
#include "src/objects/feedback-cell.h"
#include "src/objects/script.h"
9
#include "src/roots/roots-inl.h"
10 11 12 13 14 15
#include "test/cctest/cctest.h"

namespace v8 {
namespace internal {

namespace {
16
AllocationSpace GetSpaceFromObject(Object object) {
17
  DCHECK(object.IsHeapObject());
18
  return MemoryChunk::FromHeapObject(HeapObject::cast(object))
19
      ->owner_identity();
20 21 22
}
}  // namespace

23
#define CHECK_IN_RO_SPACE(type, name, CamelName) \
24
  HeapObject name = roots.name();                \
25 26 27 28
  CHECK_EQ(RO_SPACE, GetSpaceFromObject(name));

// The following tests check that all the roots accessible via ReadOnlyRoots are
// in RO_SPACE.
29
TEST(TestReadOnlyRoots) {
30 31
  ReadOnlyRoots roots(CcTest::i_isolate());

32
  READ_ONLY_ROOT_LIST(CHECK_IN_RO_SPACE)
33 34 35 36 37
}

#undef CHECK_IN_RO_SPACE

namespace {
38
bool IsInitiallyMutable(Factory* factory, Address object_address) {
39 40
// Entries in this list are in STRONG_MUTABLE_MOVABLE_ROOT_LIST, but may
// initially point to objects that are in RO_SPACE.
41
#define INITIALLY_READ_ONLY_ROOT_LIST(V)  \
42 43
  V(api_private_symbol_table)             \
  V(api_symbol_table)                     \
44
  V(builtins_constants_table)             \
45
  V(current_microtask)                    \
46 47
  V(detached_contexts)                    \
  V(feedback_vectors_for_profiling_tools) \
48
  V(shared_wasm_memories)                 \
49
  V(materialized_objects)                 \
50
  V(public_symbol_table)                  \
51 52 53
  V(retained_maps)                        \
  V(retaining_path_targets)               \
  V(serialized_global_proxy_sizes)        \
54 55
  V(serialized_objects)                   \
  V(weak_refs_keep_during_job)
56 57

#define TEST_CAN_BE_READ_ONLY(name) \
58
  if (factory->name().address() == object_address) return false;
59 60 61 62 63 64 65
  INITIALLY_READ_ONLY_ROOT_LIST(TEST_CAN_BE_READ_ONLY)
#undef TEST_CAN_BE_READ_ONLY
#undef INITIALLY_READ_ONLY_ROOT_LIST
  return true;
}
}  // namespace

66 67 68
// The CHECK_EQ line is there just to ensure that the root is publicly
// accessible from Heap, but ultimately the factory is used as it provides
// handles that have the address in the root table.
69 70 71 72 73 74 75
#define CHECK_NOT_IN_RO_SPACE(type, name, CamelName)                         \
  Handle<Object> name = factory->name();                                     \
  CHECK_EQ(*name, heap->name());                                             \
  if (name->IsHeapObject() && IsInitiallyMutable(factory, name.address()) && \
      !name->IsUndefined(CcTest::i_isolate())) {                             \
    CHECK_NE(RO_SPACE, GetSpaceFromObject(HeapObject::cast(*name)));         \
  }
76 77 78 79 80

// The following tests check that all the roots accessible via public Heap
// accessors are not in RO_SPACE with the exception of the objects listed in
// INITIALLY_READ_ONLY_ROOT_LIST.
TEST(TestHeapRootsNotReadOnly) {
81
  Factory* factory = CcTest::i_isolate()->factory();
82 83
  Heap* heap = CcTest::i_isolate()->heap();

84
  MUTABLE_ROOT_LIST(CHECK_NOT_IN_RO_SPACE)
85 86 87 88 89 90
}

#undef CHECK_NOT_IN_RO_SPACE

}  // namespace internal
}  // namespace v8