3 ms·
Seems like that would take care of roots for the bytecode's themselves, but not for "native" functions[1]. Allocating a new object could call gc[2], and native
by celeritascelery 3y ago
Seems like that would take care of roots for the bytecode's themselves, but not for "native" functions[1]. Allocating a new object could call gc[2], and native functions are using the native callstack. It seems like it would be easy to allocate in a native function and any unrooted references would be invalidated. In fact I see a case like that here[3]. That method creates a reference with `expect_concrete_object_at` and then calls gc with `new_java_lang_class_object`. It avoids UB by not using `arg` after the call that gc's, but there is nothing stopping you from using `arg` again (and having an invalid reference).
[1] https://github.com/andreabergia/rjvm/blob/main/vm/src/native_methods_impl.rs https://github.com/andreabergia/rjvm/blob/main/vm/src/native...
[2] https://github.com/andreabergia/rjvm/blob/be9c54066c64a8287902553dc1bab4b94206086c/vm/src/vm.rs#L230-L236 https://github.com/andreabergia/rjvm/blob/be9c54066c64a82879...
[3] https://github.com/andreabergia/rjvm/blob/be9c54066c64a8287902553dc1bab4b94206086c/vm/src/native_methods_impl.rs#L237-L246 https://github.com/andreabergia/rjvm/blob/be9c54066c64a82879...
- andreabergia 3y agoIndeed you are right, this is definitely a bug and could cause errors. I guess the solution would be to add an explicit API to create a GC root, invoked by native methods (which is a bit complicated by the fact that I use a moving collector). Many years ago I was using SpiderMonkey in a c++ project and I seem to remember there were some APIs for native callbacks to invoke that rooted values. Same problem and similar solution. :-)
- munificent 3y ago> I guess the solution would be to add an explicit API to create a GC root, invoked by native methods (which is a bit complicated by the fact that I use a moving collector). This is why I do in the Wren VM. Any time a native C function has the only reference to a GC-managed object and it's possible for a collection to occur, it calls a function to temporarily add the object to a list of known roots.