test-roots.cc 3.31 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
#include "src/common/globals.h"
#include "src/heap/basic-memory-chunk.h"
7 8 9 10
#include "src/heap/heap-inl.h"
#include "src/objects/cell.h"
#include "src/objects/feedback-cell.h"
#include "src/objects/script.h"
11
#include "src/roots/roots-inl.h"
12 13 14 15 16 17
#include "test/cctest/cctest.h"

namespace v8 {
namespace internal {

namespace {
18
AllocationSpace GetSpaceFromObject(Object object) {
19
  DCHECK(object.IsHeapObject());
20 21 22 23
  BasicMemoryChunk* chunk =
      BasicMemoryChunk::FromHeapObject(HeapObject::cast(object));
  if (chunk->InReadOnlySpace()) return RO_SPACE;
  return chunk->owner()->identity();
24 25 26
}
}  // namespace

27
#define CHECK_IN_RO_SPACE(type, name, CamelName) \
28
  HeapObject name = roots.name();                \
29 30 31 32
  CHECK_EQ(RO_SPACE, GetSpaceFromObject(name));

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

36
  READ_ONLY_ROOT_LIST(CHECK_IN_RO_SPACE)
37 38 39 40 41
}

#undef CHECK_IN_RO_SPACE

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

#define TEST_CAN_BE_READ_ONLY(name) \
62
  if (factory->name().address() == object_address) return false;
63 64 65 66 67 68 69
  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

70 71 72
// 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.
73 74 75 76 77 78 79
#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)));         \
  }
80 81 82 83 84

// 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) {
85
  Factory* factory = CcTest::i_isolate()->factory();
86 87
  Heap* heap = CcTest::i_isolate()->heap();

88
  MUTABLE_ROOT_LIST(CHECK_NOT_IN_RO_SPACE)
89 90 91 92 93 94
}

#undef CHECK_NOT_IN_RO_SPACE

}  // namespace internal
}  // namespace v8