<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hello,</p>
<p>as mentioned in the Javadoc, for downcalls and upcalls Linker
uses the global scope for the result respectively the arguments.
This is problematic for pointers to `static` data in C because it
leads to unsafe code.<br>
Specifically, consider this case:</p>
<ol>
<li>Load a library using SymbolLookup</li>
<li>Obtain the address of a function in that library<br>
</li>
<li>Call that function using Linker; the function returns a
pointer to `static` data</li>
<li>Use the function result</li>
</ol>
<p>If I understand it correctly that pointer to the `static` data
actually points within the data of the loaded library (and not
some newly allocated heap memory). But I am not that familiar with
C, so please correct me if I am wrong.</p>
<p>The problem is now if you accidentally unload the library by
closing the Arena which was used for loading it, either explicitly
or by the garbage collector in case of Arena.ofAuto(), while the
function result is still is use.<br>
Because the Linker used the global scope, if you use the `static`
data returned by the function it will crash the JVM because it is
not detected that the original Arena had been closed, and the
library had been unloaded.</p>
<p><br>
</p>
<p>What makes this worse is that if you are just given a
SymbolLookup, or even a MemorySegment obtained from it, you don't
have access to the original Arena used to load the library, so you
cannot even manually fix this unsafety this by using
`MemorySegment#reinterpet` to change the Arena.<br>
</p>
<p><br>
</p>
<p>Maybe it would therefore be safer if Linker for downcalls used
the scope of the given function pointer MemorySegment, instead of
the global scope. What do you think?<br>
For upcall arguments I am not sure if there is a way to fix this
unsafety.</p>
<p><br>
</p>
<p>If the behavior for downcalls cannot be changed, could you then
please at least add a 'restricted'
`MemorySegment#withScope(Scope)` (maybe with additional 'cleanup'
argument) or similar, so that you can manually change the scope of
the downcall result, without needing a reference to the original
Arena (which is currently required for
`MemorySegment#reinterpet`)?<br>
Though personally I would prefer if the default downcall behavior
was changed, because manually having to change the scope of the
downcall result every time is error-prone and easy to forget.</p>
<p><br>
</p>
Kind regards
<p></p>
</body>
</html>