-
Notifications
You must be signed in to change notification settings - Fork 20
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
Making co-existing jni-sys wrappers as safe as possible #26
Comments
It is possible to enforce that everybody uses the same version through some means, though I forget the details. I think you can declare that you link against something external in a way that makes cargo force you to have exactly one version. Or maybe I'm only talking about something we thought about adding =) but I'm pretty sure it exists. I'll ask around. |
This is what I am talking about: https://doc.rust-lang.org/cargo/reference/resolver.html#links |
I'm not entirely sure we should be trying to workaround upstream OpenJDK bugs in We can only workaround a race condition in I'm curious what your use case for I think it's quite usual to acquire a VM pointer in some platform specific way unless you explicitly created the VM (in which case there wouldn't be a race) One standard mechanism for acquiring a VM pointer that you didn't create is via a My use case for JNI is generally on Android and in that case we currently have a standard Since we have a few things we'd like to update with that |
I couldn't remember where we had this discussion before, but here's an issue I filed a while ago to track the idea of creating a replacement for the ndk-context crate for sharing a JVM pointer: jni-rs/jni-rs#421 - seems potentially relevant here. |
Since this issue is currently also about the wider question of how to enable safe interop with multiple jni wrapper crates I also just wanted to link to this issue I opened for the There are probably a number of risks related to different jni crates trying to independently manage thread detachment and getting into situations where one crate will detach a thread from the JVM because its world view says that's OK, but it doesn't realize there was another crate still relying on that attachment. |
While building duchess we found a data race in the OpenJDK implementation of
JNI_GetCreatedJavaVMs
. When called concurrently withJNI_CreateJavaVM
,JNI_GetCreatedJavaVMs
might return pointers to partially-initialized JVMs (OpenJDK bug), which then causes segmentation faults.The naive fix for duchess and any other library that aims to provide a safe Rust API around
jni-sys
is to use a mutex to synchronize calls toJNI_CreateJavaVM
andJNI_GetCreatedJavaVMs
. However, if each library has its own mutex, the overall Rust API is still technically unsound because one application might depend on multiple such libraries (or different versions of the same library), each making calls toJNI_CreateJavaVM
andJNI_GetCreatedJavaVMs
that are effectively unsynchronized.We were discussing how to make co-existing JNI libraries (e.g. jni, j4rs, duchess) as safe as possible. Adding something to
jni-sys
seems a good approach, becausejni-sys
is used by all such libraries. Two options are:jni-sys
.Mutex<()>
tojni-sys
, leaving to the safe JNI libraries the duty of properly syncronizing the JNI calls that are (due to bugs or by design) thread-unsafe.Option A requires more code in
jni-sys
; option B requires less code injni-sys
but more in the safe JNI libraries. Both options assume that all safe JNI libraries will use the samejni-sys
version. I don't know if there is a way to enforce that.The text was updated successfully, but these errors were encountered: