Question about global SegmentScope
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Mon Apr 17 08:53:02 UTC 2023
What Jorn says is correct. There is no way for the FFM API to reason
about the lifetime of pointers returned by native functions. Sometimes
these pointers will need to be freed, manually, by the caller, other
times they will be freed by the library, other times (as in this case)
they will point to some constant data that will remain valid as long as
the library is loaded.
It is up to the client to “override” the lifetime of the returned
segment as needed.
The Java 21 API makes this a bit clearer, as segments with unknown
lifetime are modelled as segment that have an /unbounded/ scope (e.g. a
scope that is always alive), but that is /different/ from the scope of
the global arena. This is demonstrated below:
|jshell>
MemorySegment.ofAddress(42).scope().equals(Arena.global().scope()) $2
==> false |
Also, you might find the new javadoc for the Linker useful, as it
contains a section on native functions returning pointers:
https://cr.openjdk.org/~pminborg/panama/21/v2/javadoc/api/java.base/java/lang/foreign/Linker.html#by-ref
Which shows how, where needed, new temporal bounds can be given to
segments returned by a downcall method handle.
Hope this helps.
Maurizio
On 17/04/2023 08:25, Jorn Vernee wrote:
> I think they are asking not about the library symbols, but about the
> pointer returned by OPENSSL_version?
>
> Whether such pointers should be 'freed' depends on the native library.
> Some native libraries might allocate memory and return a pointer to
> the user. It is up to the native library to specify how the memory
> should be freed in that case. Usually if the user is expected to free
> the memory a special function to do that is provided by the library.
>
> In this case, OPENSSL_version seems to be returning a pointer to a
> constant string (string literal), which doesn't have to be freed by
> the user.
>
> Jorn
>
> On 16/04/2023 14:32, Glavo wrote:
>> The zero-length MemorySegment associated with the global scope is a
>> wrapper of the raw pointer.
>> It indicates that the lifetime is unknown, rather than indicating
>> that the lifetime is truly infinite.
>>
>> The true lifetime of these symbols is associated with SymbolLookup.
>> They are also invalid when the scope of the SymbolLookup is invalid.
>>
>> Glavo
>>
>> On Sun, Apr 16, 2023 at 7:52 PM 刘希晨 <benrush0705 at gmail.com> wrote:
>>
>> Thank you for the clarification. I didn' mean the SymbolLookup
>> itself, but the MemorySegment that MethodHandle returns, the
>> MethodHandle is created by , I already tested no matter what
>> kind of SegmentScope SymbolLookup used, the native MemorySegment
>> that MethodHandle returns would always be Global scope.
>>
>> A example would be like below :
>>
>> ```
>> SymbolLookup crypto =
>> SymbolLookup.libraryLookup(Path.of("C:\\Program
>> Files\\OpenSSL-Win64\\libcrypto-3-x64.dll"),
>> SegmentScope.auto()); MemorySegment methodPointer =
>> crypto.find("OPENSSL_version") .orElseThrow(() -> new
>> RuntimeException("Unable to load target native method"));
>> MethodHandle openSSLVersion =
>> Linker.nativeLinker().downcallHandle(methodPointer,
>> FunctionDescriptor.of(ValueLayout.ADDRESS,
>> ValueLayout.JAVA_INT)); MemorySegment version = (MemorySegment)
>> openSSLVersion.invokeExact(0); System.out.println(version);
>> ```
>> the version Segment is global somehow and I don't know how to
>> release it
>>
>> Glavo <zjx001202 at gmail.com> 于2023年4月16日周日 19:38写道:
>>
>> SymbolLookup.libraryLookup takes a SegmentScope as a
>> parameter, which represents the lifecycle of the returned
>> SymbolLookup.
>>
>> It looks like you are using an old JDK? Panama has critical
>> changes every Java update, please use the latest JDK.
>>
>> Glavo
>>
>> On Sun, Apr 16, 2023 at 6:36 PM 刘希晨 <benrush0705 at gmail.com>
>> wrote:
>>
>> Hi I have a question about global segment scope.
>>
>> If I use SymbolLookup.libraryLookup to load a C library
>> and call its function which returns a pointer , in java
>> it's represented by a memory segment, the memorysegment's
>> scope would be global scope. Does this mean that the
>> memory segment object wouldn't be released by anything
>> until the VM exits?
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230417/9378be4b/attachment-0001.htm>
More information about the panama-dev
mailing list