cppgc: Add tests for in-construction during ctor

Adds explicit tests that check that an object is marked as in
construction while running the constructor.

Bug: chromium:1056170
Change-Id: I7f7340832e1bc31cec98784c261ed86deb402e72
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2811238
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73869}
This commit is contained in:
Michael Lippautz 2021-04-08 12:05:08 +02:00 committed by Commit Bot
parent 660e87d2ee
commit 62ff82e44b

View File

@ -156,5 +156,86 @@ TEST_F(GarbageCollectedTestWithHeap, PostConstructionCallbackForMixin) {
EXPECT_EQ(1u, MixinWithPostConstructionCallback::cb_callcount);
}
namespace {
int GetDummyValue() {
static std::mutex mutex;
static int ret = 43;
// Global lock access to avoid reordering.
std::lock_guard<std::mutex> guard(mutex);
return ret;
}
class CheckObjectInConstructionBeforeInitializerList final
: public GarbageCollected<CheckObjectInConstructionBeforeInitializerList> {
public:
CheckObjectInConstructionBeforeInitializerList()
: in_construction_before_initializer_list_(
HeapObjectHeader::FromPayload(this).IsInConstruction()),
unused_int_(GetDummyValue()) {
EXPECT_TRUE(in_construction_before_initializer_list_);
EXPECT_TRUE(HeapObjectHeader::FromPayload(this).IsInConstruction());
}
void Trace(Visitor*) const {}
private:
bool in_construction_before_initializer_list_;
int unused_int_;
};
class CheckMixinInConstructionBeforeInitializerList
: public GarbageCollectedMixin {
public:
explicit CheckMixinInConstructionBeforeInitializerList(void* payload_start)
: in_construction_before_initializer_list_(
HeapObjectHeader::FromPayload(payload_start).IsInConstruction()),
unused_int_(GetDummyValue()) {
EXPECT_TRUE(in_construction_before_initializer_list_);
EXPECT_TRUE(
HeapObjectHeader::FromPayload(payload_start).IsInConstruction());
}
void Trace(Visitor*) const override {}
private:
bool in_construction_before_initializer_list_;
int unused_int_;
};
class UnmanagedMixinForcingVTable {
protected:
virtual void ForceVTable() {}
};
class CheckGCedWithMixinInConstructionBeforeInitializerList
: public GarbageCollected<
CheckGCedWithMixinInConstructionBeforeInitializerList>,
public UnmanagedMixinForcingVTable,
public CheckMixinInConstructionBeforeInitializerList {
public:
CheckGCedWithMixinInConstructionBeforeInitializerList()
: CheckMixinInConstructionBeforeInitializerList(this) {
// Ensure that compiler indeed generated an inner object.
CHECK_NE(
this,
static_cast<void*>(
static_cast<CheckMixinInConstructionBeforeInitializerList*>(this)));
}
};
} // namespace
TEST_F(GarbageCollectedTestWithHeap, GarbageCollectedInConstructionDuringCtor) {
MakeGarbageCollected<CheckObjectInConstructionBeforeInitializerList>(
GetAllocationHandle());
}
TEST_F(GarbageCollectedTestWithHeap,
GarbageCollectedMixinInConstructionDuringCtor) {
MakeGarbageCollected<CheckGCedWithMixinInConstructionBeforeInitializerList>(
GetAllocationHandle());
}
} // namespace internal
} // namespace cppgc