FFM API: questions about reinterpret and MemorySegment
Pedro Lamarão
pedro.lamarao at prodist.com.br
Thu Oct 3 14:07:17 UTC 2024
Em qui., 3 de out. de 2024 às 05:46, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> escreveu:
> On 02/10/2024 23:58, Anastasiya Lisitskaya wrote:
>
> Hi,
>
> It is very helpful!
>
> So, if I want to use data from the heap without extra copying to off-heap
> (native MemorySegment), should using String be avoided? It seems there is
> no way to use a String without copying, as we can't guarantee a trailing
> null terminator.
>
> I'm afraid that's the case. The Java String API does not concern with
> string terminators because, in Java, all strings have a size. In C that's
> not the case - so in general you need to append a terminator, and that will
> involve some degree of copying.
>
>
> One thing still concerns me: is processing an unterminated string
> unpredictable? Only one test from my suite fails (returning this extra
> symbol or crashing).
>
> Processing an unterminated string leads to undefined behavior.
> Effectively, your program is scanning _past_ the contents of your string,
> in search for a zero. Because of the way some system calls work (e.g.
> malloc) it is likely that a zero will be found more or less where expected.
> But that behavior is OS/platform dependent and absolutely cannot be relied
> upon.
>
If you have control of the native code and are able to change its API, this
is a great opportunity to add a size or length companion parameter to the
char* parameter, avoiding this problem entirely.
This change is justified for memory safety (explicit bounds checking) and
performance (compute / discover the bounds only once).
--
Pedro Lamarão
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20241003/79ab880a/attachment-0001.htm>
More information about the panama-dev
mailing list