Ah that explains a lot of things, thanks for walking me through this!
On Friday, August 9, 2024 at 8:55:31 PM UTC+9 omer...@chromium.org wrote:

> 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).
>
> On Friday, August 9, 2024 at 1:53:15 AM UTC+2 zcb...@gmail.com wrote:
>
>> For future reference, here is the test that verifies pointers in 
>> std::variant can be retained but pointers in std::vector can not:
>>
>> https://github.com/compilets/compilets/blob/abb49a4/cpp/runtime/tests/stack_unittest.cc
>>
>> On Thursday, August 8, 2024 at 9:59:11 PM UTC+9 Cheng wrote:
>>
>>> Thanks for the explanation!
>>>
>>> I was actually quite confused why pointer in vector was also retained, 
>>> I'll fix my tests.
>>>
>>> On Thursday, August 8, 2024 at 8:00:44 PM UTC+9 omer...@chromium.org 
>>> wrote:
>>>
>>>> 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.
>>>> On Thursday, August 8, 2024 at 10:43:43 AM UTC+2 zcb...@gmail.com 
>>>> wrote:
>>>>
>>>>> To answer my own question, I wrote some tests:
>>>>>
>>>>> https://github.com/compilets/compilets/commit/d69722fb36260caa278843311cb043f5fe8a90d7
>>>>>
>>>>> It seems that pointers stored in containers can be retained.
>>>>> (It would be great if someone can verify this though.)
>>>>>
>>>>> On Thursday, August 8, 2024 at 9:11:33 AM UTC+9 Cheng wrote:
>>>>>
>>>>>> 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?
>>>>>>
>>>>>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/286a00cb-873a-4c60-be87-cc3fbeed125fn%40googlegroups.com.

Reply via email to