diff --git a/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java b/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java index e090cb1a9e..310a6b1cdc 100644 --- a/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java +++ b/memory/memory-core/src/main/java/org/apache/arrow/memory/BufferLedger.java @@ -171,9 +171,6 @@ public void retain() { @Override public void retain(int increment) { Preconditions.checkArgument(increment > 0, "retain(%s) argument is not positive", increment); - if (historicalLog != null) { - historicalLog.recordEvent("retain(%d)", increment); - } int originalReferenceCount; do { originalReferenceCount = BUF_REF_CNT_UPDATER.get(this); @@ -181,9 +178,23 @@ public void retain(int increment) { // that was already released, which lets the next retain() succeed and lets // ArrowBuf's reference count check pass for memory that has already been freed. Preconditions.checkArgument( - originalReferenceCount > 0, "retain() on a ledger with no remaining references"); + originalReferenceCount > 0, + "retain(%s) on a ledger with reference count %s", + increment, + originalReferenceCount); + // A count that wraps negative can never be released back to 0, so the memory would leak. + Preconditions.checkArgument( + increment <= Integer.MAX_VALUE - originalReferenceCount, + "retain(%s) would overflow reference count %s", + increment, + originalReferenceCount); } while (!BUF_REF_CNT_UPDATER.compareAndSet( this, originalReferenceCount, originalReferenceCount + increment)); + // Record only retains that took effect, so that a rejected retain on a released ledger + // does not show up in the history as if it had revived the ledger. + if (historicalLog != null) { + historicalLog.recordEvent("retain(%d)", increment); + } } /** diff --git a/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java b/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java index 6662cb3ae6..35955b71fe 100644 --- a/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java +++ b/memory/memory-core/src/test/java/org/apache/arrow/memory/TestBaseAllocator.java @@ -289,7 +289,33 @@ public void testRetainOnReleasedBufferIsRejectedRepeatedly() throws Exception { assertEquals(0, arrowBuf.refCnt()); assertThrows(IllegalArgumentException.class, () -> referenceManager.retain(2)); assertEquals(0, arrowBuf.refCnt()); - assertThrows(IllegalStateException.class, () -> arrowBuf.setInt(0, 1)); + // ArrowBuf only checks the reference count on access when bounds checking is enabled. + // Without it, setInt would write into the freed memory instead of throwing. + if (BoundsChecking.BOUNDS_CHECKING_ENABLED) { + assertThrows(IllegalStateException.class, () -> arrowBuf.setInt(0, 1)); + } + } + } + + @Test + public void testRetainRejectsReferenceCountOverflow() throws Exception { + try (final RootAllocator rootAllocator = new RootAllocator(MAX_ALLOCATION)) { + final ArrowBuf arrowBuf = rootAllocator.buffer(512); + final ReferenceManager referenceManager = arrowBuf.getReferenceManager(); + + // A retain that would wrap the count negative has to be rejected without changing it. + assertThrows( + IllegalArgumentException.class, () -> referenceManager.retain(Integer.MAX_VALUE)); + assertEquals(1, arrowBuf.refCnt()); + + referenceManager.retain(Integer.MAX_VALUE - 1); + assertEquals(Integer.MAX_VALUE, arrowBuf.refCnt()); + assertThrows(IllegalArgumentException.class, referenceManager::retain); + assertEquals(Integer.MAX_VALUE, arrowBuf.refCnt()); + + // The count is still intact, so releasing every reference frees the buffer. + assertTrue(referenceManager.release(Integer.MAX_VALUE)); + assertEquals(0, arrowBuf.refCnt()); } }