v8/test/unittests/heap/conservative-stack-visitor-unittest.cc
Nikolaos Papaspyrou 9554743a0b [heap] Refactor the stack object
The stack object is primarily used for conservative stack scanning, both
by the V8 and C++ garbage collectors. This CL introduces the notion of a
"stack context", which comprises of the current stack marker (the lowest
address on the stack that may contain interesting pointers) and the
values of the saved registers. It simplifies the way in which iteration
through the stack is invoked: the context must have previously been
saved and iteration always uses the stack marker.

Bug: v8:13257
Bug: v8:13493
Change-Id: Ia99ef702eb6ac67a3bcd006f0edf5e57d9975ab2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4017512
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Commit-Queue: Nikolaos Papaspyrou <nikolaos@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84303}
2022-11-16 16:21:50 +00:00

238 lines
7.5 KiB
C++

// 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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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());
SaveStackContextScope stack_context_scope(&heap()->stack());
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