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

Maurizio Cimadamore mcimadamore at openjdk.java.net
Wed Apr 15 20:24:52 UTC 2020


On Wed, 15 Apr 2020 18:42:56 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> 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.

Good point, I will fix this. It's interesting how all these issues were latent in the code and having a more direct
implementation for the various segments made them pop out more :-)

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

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


More information about the panama-dev mailing list