test-roots.cc 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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.

#include "src/heap/heap.h"
#include "src/roots-inl.h"
#include "test/cctest/cctest.h"

namespace v8 {
namespace internal {

namespace {
AllocationSpace GetSpaceFromObject(Object* object) {
  DCHECK(object->IsHeapObject());
  return MemoryChunk::FromHeapObject(HeapObject::cast(object))
      ->owner()
      ->identity();
}
}  // namespace

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

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

30
  READ_ONLY_ROOT_LIST(CHECK_IN_RO_SPACE)
31 32 33 34 35
}

#undef CHECK_IN_RO_SPACE

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

#define TEST_CAN_BE_READ_ONLY(name) \
54
  if (factory->name().address() == object_address) return false;
55 56 57 58 59 60 61
  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

62 63 64
// 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.
65
#define CHECK_NOT_IN_RO_SPACE(type, name, CamelName)                       \
66 67 68 69 70
  Handle<Object> name = factory->name();                                   \
  CHECK_EQ(*name, heap->name());                                           \
  if (name->IsHeapObject() && IsInitiallyMutable(factory, name.address())) \
    CHECK_NE(RO_SPACE,                                                     \
             GetSpaceFromObject(reinterpret_cast<HeapObject*>(*name)));
71 72 73 74 75

// 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) {
76
  Factory* factory = CcTest::i_isolate()->factory();
77 78
  Heap* heap = CcTest::i_isolate()->heap();

79
  MUTABLE_ROOT_LIST(CHECK_NOT_IN_RO_SPACE)
80 81 82 83 84 85
}

#undef CHECK_NOT_IN_RO_SPACE

}  // namespace internal
}  // namespace v8