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

AttachGuard detaches too soon when dropped out of order #441

Open
wleslie opened this issue Mar 30, 2023 · 2 comments
Open

AttachGuard detaches too soon when dropped out of order #441

wleslie opened this issue Mar 30, 2023 · 2 comments
Labels

Comments

@wleslie
Copy link

wleslie commented Mar 30, 2023

Repro:

let jvm = JavaVM::new(InitArgsBuilder::new().build().unwrap()).unwrap();
let env1 = jvm.attach_current_thread().unwrap();
let mut env2 = jvm.attach_current_thread().unwrap();
std::mem::drop(env1);
// the thread is no longer attached, even though env2 is alive!
env2.find_class("java/lang/String").unwrap();
// seg fault!

It looks like the implementation assumes that AttachGuard instances will be dropped in last-in-first-out order. Whichever AttachGuard is created first is designated as the one that will detach on drop. But the instance that was created first isn't necessarily the same one that will be dropped last.

@rib
Copy link
Contributor

rib commented Apr 1, 2023

Ah, tricky.

Yeah the implementation seems to try and mirror how the C API lets you redundantly call AttachCurrentThread and it will be a NOP that just return the JNIEnv in that case.

The implementation of attach_current_thread() sees when the attach is redundant and in that case it returns a guard that won't do anything on Drop. (so it's not exactly that there's any detailed consideration of lifo ordering except that the first guard that really does attach the thread will be the only guard that does anything on Drop)

I'm not exactly sure why it was implemented this way but I guess it was the simplest approach that reflected how the C API works.

It looks like there needs to be a per-thread ref-count for these guards to work like this.

With needing to have a thread-local reference counter that also makes me wonder what hazards there could be if there are multiple versions of the jni crate linked into one application - each with a private set of thread-local variables.

Similar to the current implementation I think there would need to be some kind of special case to recognize if the first attach increment for the thread was redundant (another version of the jni crate or some other non-Rust code might have attached the thread) and we'd probably have to just assume something else will be responsible for detaching the thread in this case.

@rib rib added the bug 🐛 label Apr 1, 2023
@rib
Copy link
Contributor

rib commented Apr 1, 2023

For reference also see this recent discussion around the soundness of APIs that return a JNIEnv here: #436 (reply in thread)

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

No branches or pull requests

2 participants