Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nativecast of a rakudo-allocated value doesn't GC-proof it unless assigned to variable #6080

Open
p6rt opened this issue Feb 16, 2017 · 1 comment

Comments

@p6rt
Copy link

p6rt commented Feb 16, 2017

Migrated from rt.perl.org#130792 (status was 'new')

Searchable as RT130792$

@p6rt
Copy link
Author

p6rt commented Feb 16, 2017

From @skids

Not sure whether this is a "bug" or a "doc bug"... there is clearly some intent in the code to cause nativecasting something to prevent it from being GCd from under the resulting pointer, but the limitations of this protection are not spelled out.

Here is one case where a GC can cause a Pointer to point to garbage​:

$ perl6 -e 'use NativeCall; use nqp; my $anchor; my $c = nativecast(Pointer[uint8], Buf.new(1,2,3,4,5,6)); class f is repr("CStruct") { has uint16 $.a; has uint32 $.b }; my $d = nativecast(f,$c); my $f = $d.a; nqp​::force_gc; $d = nativecast(f,$c); say $f == $d.a'
False

Assigning the Buf to a variable seems to restore the protection (but this may just be due to optimization not yet getting around to realizing $anchor is not used?)

$ perl6 -e 'use NativeCall; use nqp; my $anchor; my $c = nativecast(Pointer[uint8], $anchor = Buf.new(1,2,3,4,5,6)); class f is repr("CStruct") { has uint16 $.a; has uint32 $.b }; my $d = nativecast(f,$c); my $f = $d.a; nqp​::force_gc; $d = nativecast(f,$c); say $f == $d.a'
True

Obviously it is necessary to have some level of protection in order for nativecast pointers to objects allocated by rakudo to be at all useful; how far it is technically possible to accomplish this probably has limits and whatever rules users need to follow should be spelled out in docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant