Question about jmethodId and class unloading

Thomas Stüfe thomas.stuefe at gmail.com
Wed Apr 13 10:21:38 UTC 2022


Hi,

In hotspot, jmethodId is an address to a slot in a table-like structure of
Method*. We ensure that the slot never goes away, so the slot address is
always valid. But we reuse the slot - if a class is unloaded, its method
ids are released by marking their slot with a canary. Later it could get
reused for a different Method*.

I must miss something really obvious, but what prevents an outside caller
from using an invalid or outdated method id via JNI?

If the method id got released, on CallXXXMethod, we stop the VM with a
fatal JNI error (if CheckJNICalls=1). But there is no way to gracefully
continue, right?

And what happens if the slot got reused by the VM, so it now contains a
different Method* ? There is no way catch that, right?

Do both cases just boil down to "the JNI programmer should know when class
unloading did happen, and should not use the jmethodid beyond that point"?

Thanks a lot,

Thomas


More information about the hotspot-runtime-dev mailing list