[concurrency-interest] DirectByteBuffers and reachabilityFence

Andrew Haley aph at redhat.com
Mon Dec 7 09:41:26 EST 2015


On 12/07/2015 12:40 PM, Alexandre De Champeaux wrote:
> However, this got me concerned about the java.nio.DirectByteBuffer read and
> write methods:
> If the "this" object is garbage collected when making a call like
> ByteBuffer.allocateDirect(int).someGetOrPutMethod(), the native pointer
> that is passed to sun.misc.Unsafe might be freed, and accessing it will
> cause the read/write to occur in an invalid memory area, which might lead
> to a segfault, or other major issues.
> This would be quite unlikely: JIT compilation needs to occur while keeping
> a safepoint, then a GC must happen, and finally the ReferenceHandler thread
> needs to perform its cleanup fast enough.
> 
> I am particularly concerned by the get(byte[] dst, int offset, int length)
> method, that in turns calls Bits.copyToArray, which purposely splits its
> calls to Unsafe.copyMemory to allow for safepoints to sneak in.
> 
> Am I correct, or does the JVM performs specific protection for instances of
> DirectByteBuffer?

There are many places where we need to insert a reachabilityFence.
This is one such.  Some non-HotSpot JVMs perform their cleanups
very aggressively, so it's more likely there.

Andrew.



More information about the Concurrency-interest mailing list