Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,30 @@ 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);
// Check before writing. An unconditional add leaves the count positive on a ledger
// 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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Loading