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

Paul Sandoz psandoz at openjdk.java.net
Wed Apr 15 18:24:08 UTC 2020


On Wed, 15 Apr 2020 15:23:51 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> * Fix access modes not preserved in AbstractMemorySegmentImpl.ofBuffer
>> * Add check to ensure that Foreign::withSize and Foreign::asMallocSegment are called with unchecked address
>
> 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();

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

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


More information about the panama-dev mailing list