-
Notifications
You must be signed in to change notification settings - Fork 20
fix(language): use correct scope for language pointer #37
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
Conversation
This breaks the regular constructor. |
Do you mean for the case where the user loaded the language pointer with Otherwise I don't see how this breaks the behavior of the constructor. As we noticed in the discussion related to #29, rather the current behavior is broken: As side note: If you want to manually experiment with this, or include it in the unit tests, you probably have to make sure the parser library (e.g. Footnotes
|
I also brought up the general issue with scopes in the foreign API and then specifically the jtreesitter situation on the JDK mailing list, where a JDK maintainer also mentioned that the current Another interesting point mentioned there is that attaching the Footnotes |
To prevent crashes, I think we should just properly document that the arena must be kept open.
I replaced the
|
Are you opposed to the changes in this pull request of setting the scope to the library scope though (in addition to documentation changes)? Just to provide an extra level of safety in case users accidentally disregard this and close the arena too early.
Hmmm ok, though I think this is not directly related to the problems mentioned here. My concern was not that
It seems the difference for the Rust bindings is that there in the common use case you provide the |
The Registering the cleaner for |
We can't be responsible for low-level user errors if the correct process is documented.
That's true. I'll see if I can change the implementation to receive a function handle. |
Follow-up for #29 and #24 (comment)
Currently the
Language
constructor reinterprets the native language pointer usingLIBRARY_ARENA
; this is incorrect (?) because the language pointer in the parsers (at least those currently generated by the tree-sitter CLI) is not newly allocated memory on the heap, but instead points to static data in the native library.So if the native library is unloaded (explicitly by the user by calling
Arena#close
, or implicitly by the garbage collector?) a JVM crash can occur because the scope of the language pointer isLIBRARY_ARENA
instead of the scope of the native library, so the java.lang.foreign implementation does not notice that the address is not accessible anymore.However, there is still another issue then:
Linker
uses the global scope for results of downcalls. So even thoughfunction
has the correct scope (= scope of the native library),languagePointer
has the wrong scope (= global scope). Setting the correct scope can unfortunately only be done if theArena
, which was used to load the library, is present, see also https://mail.openjdk.org/pipermail/panama-dev/2024-September/020636.html and https://bugs.openjdk.org/browse/JDK-8340641.Therefore this pull request adds a
Language#load
overload withArena
parameter.What do you think? I have marked this as draft for now because I am not completely sure about these changes, but I think it tries as good as possible to prevent JVM crashes when you unload the library while the language is still in use.