Using FFI to call JVMTI functions
David Holmes
david.holmes at oracle.com
Wed Nov 27 01:00:07 UTC 2024
On 26/11/2024 10:48 pm, Jorn Vernee wrote:
> Hello,
>
> It's correct that FFM upcalls only support callers coming from the
> 'native' thread state. Looking at the code of
> IterateOverInstancesOfClass, it holds the heap lock for the duration of
> the iteration process, so I don't see how we could execute arbitrary
> Java code during this (through an FFM upcall).
>
> So, to answer your question: no, you can not use an FFM upcall stub as a
> callback function for IterateOverInstancesOfClass, or the more modern
> IterateThroughHeap [1]. In both of these cases, the callback function
> can not execute Java code.
>
> Jorn
>
> [1]: https://docs.oracle.com/en/java/javase/23/docs/specs/
> jvmti.html#IterateThroughHeap
We should probably update:
https://docs.oracle.com/en/java/javase/23/docs/specs/jvmti.html#heapCallbacks
to include FFI upcalls as another thing you cannot do during a heap
callback.
David
-----
> On 26-11-2024 11:59, ght ggg wrote:
>> Hello:
>>
>> I'm trying to use JVMTI's IterateOverInstancesOfClass function to get
>> all instances of a specified class. The code can be found here:
>> https://github.com/GGGGGHT/ffitest/blob/main/IterateInstance.java
>>
>> When I call the IterateOverInstancesOfClass method, the JVM crashes.
>> The crash log is available here: https://github.com/GGGGGHT/ffitest/
>> blob/main/hs_err_pid20038.log
>>
>> After examining the code in the UpcallLinker::on_entry method, I found
>> that this method needs to run on a JavaThread, but the current thread
>> is a VMThread, which leads to the JVM crash. I'm not entirely sure if
>> my analysis is correct. If it is correct, does this mean that using
>> FFI to call functions requiring UPCALL is not feasible? Or are there
>> alternative approaches to achieve my desired functionality?
>>
>> I look forward to your response.
>> Thanks!
>> GGGGGHT
More information about the panama-dev
mailing list