From b09c7c4c993e137f65342aaf247a90f4d2807c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Wed, 2 Dec 2026 14:46:11 +0100 Subject: [PATCH] GH-1544: Reject reference count overflow in BufferLedger.retain Check that the increment fits before the compare-and-set, so a retain that would wrap the count negative is rejected and leaves it unchanged. Record retains in the debug history only once they take effect, and include the increment and the current count in the error messages. Only assert that setInt on a released buffer throws when bounds checking is enabled, since ArrowBuf does not check the reference count otherwise. Closes #1544. --- .../org/apache/arrow/memory/BufferLedger.java | 19 ++++++++++--- .../arrow/memory/TestBaseAllocator.java | 28 ++++++++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) 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()); } }