[cppgc] Question about stack scanning

214 views
Skip to first unread message

Cheng

unread,
Aug 7, 2024, 8:11:33 PM8/7/24
to v8-users
Cppgc does stack scanning so objects pointed by raw pointers on stack are not garbage collected:

Object* ptr = MakeGarbageCollected<Object>(); // retained.

But what if I put the pointer in a variant?

std::variant<Object*, std::monostate> ptr = MakeGarbageCollected<Object>(); // retain?

Or even in a vector?

std::vector<Object*> ptrs = { MakeGarbageCollected<Object>() }; // retain?

Will the object still be retained by the container of pointer on stack?

Cheng

unread,
Aug 8, 2024, 4:43:43 AM8/8/24
to v8-users
To answer my own question, I wrote some tests:

It seems that pointers stored in containers can be retained.
(It would be great if someone can verify this though.)

Omer Katz

unread,
Aug 8, 2024, 7:00:44 AM8/8/24
to v8-users
For some reasons my previous replies didn't get to the mailing list.

The question is whether the pointer itself is on the stack or not.
IIRC std::variant uses inline storage to store values, so a pointer that you keep in a std::variant would be on stack and would be found by stack scanning.
std::vector, on the other hand, allocates an off-stack backing store that it allocates (and reallocates as the vector grows), so pointers kept in a std::vector would not be found by stack scanning.

If your tests pass, it's because the GC is finding your pointer somewhere else on the stack (e.g. left over from calling set_needle), but not in the vector.

Cheng

unread,
Aug 8, 2024, 8:59:11 AM8/8/24
to v8-users
Thanks for the explanation!

I was actually quite confused why pointer in vector was also retained, I'll fix my tests.

Message has been deleted

Cheng

unread,
Aug 8, 2024, 7:53:15 PM8/8/24
to v8-users
For future reference, here is the test that verifies pointers in std::variant can be retained but pointers in std::vector can not:
Message has been deleted

Omer Katz

unread,
Aug 9, 2024, 7:55:29 AM8/9/24
to v8-users
It will be hard to guarantee that a pointer is never found anywhere on the stack.
That's why we generally only tests that objects are retained with stack scanning, not that they are reclaimed.
I would recommend dropping the test rather than trying to fix it.

Omer Katz

unread,
Aug 9, 2024, 7:55:31 AM8/9/24
to v8-users
Thanks, but note that your last test is not guaranteed to always succeed.
You're checking that some value is not found on stack, and trying to prevent leftover stale values on stack to make the test pass.
Most times this will likely work, but you can't 100% guarantee that some value will not be present on the stack, and if it does your test will flakily fail.
This could happen if e.g. a pointer somehow leaks to stack unexpectedly (stale value, some compiler optimization, etc...) or just a random false positive (i.e. the address of your object randomly appearing on stack regardless of any actual pointers to it).

Cheng

unread,
Aug 9, 2024, 7:44:16 PM8/9/24
to v8-users
Ah that explains a lot of things, thanks for walking me through this!
Reply all
Reply to author
Forward
0 new messages