summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/heap/conservative-stack-visitor-unittest.cc
blob: 7b10a01c496529639d8893d8e8217ed107224493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2022 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/conservative-stack-visitor.h"

#include "test/unittests/heap/heap-utils.h"
#include "test/unittests/test-utils.h"

namespace v8 {
namespace internal {

namespace {

class RecordingVisitor final : public RootVisitor {
 public:
  V8_NOINLINE explicit RecordingVisitor(Isolate* isolate) {
    // Allocate the object.
    auto h = isolate->factory()->NewFixedArray(256, AllocationType::kOld);
    the_object_ = h->GetHeapObject();
    base_address_ = the_object_.address();
    tagged_address_ = the_object_.ptr();
    inner_address_ = base_address_ + 42 * kTaggedSize;
#ifdef V8_COMPRESS_POINTERS
    compr_address_ = static_cast<uint32_t>(
        V8HeapCompressionScheme::CompressTagged(base_address_));
    compr_inner_ = static_cast<uint32_t>(
        V8HeapCompressionScheme::CompressTagged(inner_address_));
#else
    compr_address_ = static_cast<uint32_t>(base_address_);
    compr_inner_ = static_cast<uint32_t>(inner_address_);
#endif
  }

  void VisitRootPointers(Root root, const char* description,
                         FullObjectSlot start, FullObjectSlot end) override {
    for (FullObjectSlot current = start; current != end; ++current) {
      if (*current == the_object_) found_ = true;
    }
  }

  void Reset() { found_ = false; }
  bool found() const { return found_; }

  Address base_address() const { return base_address_; }
  Address tagged_address() const { return tagged_address_; }
  Address inner_address() const { return inner_address_; }
  uint32_t compr_address() const { return compr_address_; }
  uint32_t compr_inner() const { return compr_inner_; }

 private:
  // Some heap object that we want to check if it is visited or not.
  HeapObject the_object_;

  // Addresses of this object.
  Address base_address_;    // Uncompressed base address
  Address tagged_address_;  // Tagged uncompressed base address
  Address inner_address_;   // Some inner address
  uint32_t compr_address_;  // Compressed base address
  uint32_t compr_inner_;    // Compressed inner address

  // Has the object been found?
  bool found_ = false;
};

}  // namespace

using ConservativeStackVisitorTest = TestWithHeapInternalsAndContext;

// In the following, we avoid negative tests, i.e., tests checking that objects
// are not visited when there are no pointers to them on the stack. Such tests
// are generally fragile and could fail on some platforms because of unforeseen
// compiler optimizations. In general we cannot ensure in a portable way that
// no pointer remained on the stack (or in some register) after the
// initialization of RecordingVisitor and until the invocation of
// Stack::IteratePointers.

TEST_F(ConservativeStackVisitorTest, DirectBasePointer) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile Address ptr = recorder->base_address();

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(kNullAddress, ptr);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

TEST_F(ConservativeStackVisitorTest, TaggedBasePointer) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile Address ptr = recorder->tagged_address();

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(kNullAddress, ptr);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

TEST_F(ConservativeStackVisitorTest, InnerPointer) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile Address ptr = recorder->inner_address();

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(kNullAddress, ptr);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

#ifdef V8_COMPRESS_POINTERS

TEST_F(ConservativeStackVisitorTest, HalfWord1) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile uint32_t ptr[] = {recorder->compr_address(), 0};

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(static_cast<uint32_t>(0), ptr[0]);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

TEST_F(ConservativeStackVisitorTest, HalfWord2) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile uint32_t ptr[] = {0, recorder->compr_address()};

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(static_cast<uint32_t>(0), ptr[1]);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

TEST_F(ConservativeStackVisitorTest, InnerHalfWord1) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile uint32_t ptr[] = {recorder->compr_inner(), 0};

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(static_cast<uint32_t>(0), ptr[0]);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

TEST_F(ConservativeStackVisitorTest, InnerHalfWord2) {
  auto recorder = std::make_unique<RecordingVisitor>(isolate());

  // Ensure the heap is iterable before CSS.
  IsolateSafepointScope safepoint_scope(heap());
  heap()->MakeHeapIterable();

  {
    volatile uint32_t ptr[] = {0, recorder->compr_inner()};

    ConservativeStackVisitor stack_visitor(isolate(), recorder.get());
    isolate()->heap()->stack().IteratePointers(&stack_visitor);

    // Make sure to keep the pointer alive.
    EXPECT_NE(static_cast<uint32_t>(0), ptr[1]);
  }

  // The object should have been visited.
  EXPECT_TRUE(recorder->found());
}

#endif  // V8_COMPRESS_POINTERS

}  // namespace internal
}  // namespace v8