[foreign-memaccess] [Rev 01] RFR: Memory access implementation rewrite - post cleanup

Jorn Vernee jvernee at openjdk.java.net
Wed Apr 15 18:46:28 UTC 2020


On Wed, 15 Apr 2020 18:21:40 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:

>> Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Add test for roundtrip access modes
>
> src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java line 408:
> 
>> 407:         } else if (unmapper == null) {
>> 408:             return new NativeMemorySegmentImpl(bbAddress + pos, size, modes, Thread.currentThread(), bufferScope);
>> 409:         } else {
> 
> I think this and prior code can result in an integrity issue with segment -> buffer -> segment. One can close the
> original segment while retaining access on another thread to the segment obtained from the buffer.
> Consider the following example:
>         CountDownLatch a = new CountDownLatch(1);
>         CountDownLatch b = new CountDownLatch(1);
>         CompletableFuture<?> r;
>         try (MemorySegment s1 = MemorySegment.allocateNative(intArrayLayout)) {
>             r = CompletableFuture.runAsync(() -> {
>                 try {
>                     ByteBuffer bb = s1.asByteBuffer();
> 
>                     MemorySegment s2 = MemorySegment.ofByteBuffer(bb);
>                     a.countDown();
> 
>                     try {
>                         b.await();
>                     } catch (InterruptedException e) {
>                     }
> 
>                     MemoryAddress base = s2.baseAddress();
>                     intElemHandle.set(base, 1L, -42);
>                 } catch (RuntimeException e) {
>                     e.printStackTrace();
>                     throw e;
>                 }
>             });
> 
>             a.await();
>             MemoryAddress base = s1.baseAddress();
>             intElemHandle.set(base, 1L, 42);
>         }
> 
>         b.countDown();
>         r.get();

Yeah. Instead of always using Thread.currentThread(), I think we can use the owner thread of `bufferSegment` if it's
not null. That makes the example fail.

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/111


More information about the panama-dev mailing list