From 248f2b6d9392069e1285fd243f567e379c0c1f72 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Fri, 27 Feb 2026 20:38:44 +0800 Subject: [PATCH 1/2] add test --- .../spark/unsafe/PlatformUtilSuite.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java b/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java index 469b8fe8a143e..4e4169b1f30e2 100644 --- a/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java +++ b/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java @@ -164,4 +164,83 @@ public void cleanerCreateMethodIsDefined() { // path to be hit in normal usage. Assertions.assertTrue(Platform.cleanerCreateMethodIsDefined()); } + + @Test + public void reallocateMemoryGrow() { + long address = Platform.allocateMemory(1024); + try { + for (int i = 0; i < 1024; i++) { + Platform.putByte(null, address + i, (byte) i); + } + + address = Platform.reallocateMemory(address, 1024, 2048); + for (int i = 0; i < 1024; i++) { + Assertions.assertEquals((byte) i, Platform.getByte(null, address + i)); + } + for (int i = 1024; i < 2048; i++) { + Platform.putByte(null, address + i, (byte) i); + } + for (int i = 1024; i < 2048; i++) { + Assertions.assertEquals((byte) i, Platform.getByte(null, address + i)); + } + } finally { + Platform.freeMemory(address); + } + } + + @Test + public void reallocateMemoryShrinkDoesNotOverflow() { + final long OLD_SIZE = 1024L; + final long NEW_SIZE = 512L; + final long SENTINEL_SIZE = OLD_SIZE - NEW_SIZE; + final byte SENTINEL_VALUE = (byte) 0xAB; + final byte SOURCE_VALUE = (byte) 0xCD; + final int PAIRS = 1000; + + long[] sources = new long[PAIRS]; + long[] sentinels = new long[PAIRS]; + + try { + // Allocate all pairs + for (int i = 0; i < PAIRS; i++) { + sources[i] = Platform.allocateMemory(OLD_SIZE); + sentinels[i] = Platform.allocateMemory(SENTINEL_SIZE); + + Platform.setMemory(sources[i], SOURCE_VALUE, OLD_SIZE); + Platform.setMemory(sentinels[i], SENTINEL_VALUE, SENTINEL_SIZE); + } + + // Reallocate source blocks + for (int i = 0; i < PAIRS; i++) { + long oldAddr = sources[i]; + long newAddr = Platform.reallocateMemory(oldAddr, OLD_SIZE, NEW_SIZE); + sources[i] = newAddr; + } + + // Verify dest content + for (int i = 0; i < PAIRS; i++) { + for (long j = 0; j < NEW_SIZE; j++) { + Assertions.assertEquals(SOURCE_VALUE, Platform.getByte(null, sources[i] + j), + "dest block content corrupted at pair " + i); + } + } + + // Verify sentinel content + for (int i = 0; i < PAIRS; i++) { + for (long j = 0; j < SENTINEL_SIZE; j++) { + Assertions.assertEquals(SENTINEL_VALUE, Platform.getByte(null, sentinels[i] + j), + "sentinel corrupted – overflow write detected at pair " + i); + } + } + + } finally { + // Free all memory + for (long addr : sources) { + if (addr != 0) Platform.freeMemory(addr); + } + for (long addr : sentinels) { + if (addr != 0) Platform.freeMemory(addr); + } + } + } } From 3d23bf75575adbf0dafae86041e580e92843d7a9 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Fri, 27 Feb 2026 20:57:45 +0800 Subject: [PATCH 2/2] rename --- .../spark/unsafe/PlatformUtilSuite.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java b/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java index 4e4169b1f30e2..756598a8390f2 100644 --- a/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java +++ b/common/unsafe/src/test/java/org/apache/spark/unsafe/PlatformUtilSuite.java @@ -190,45 +190,45 @@ public void reallocateMemoryGrow() { @Test public void reallocateMemoryShrinkDoesNotOverflow() { - final long OLD_SIZE = 1024L; - final long NEW_SIZE = 512L; - final long SENTINEL_SIZE = OLD_SIZE - NEW_SIZE; - final byte SENTINEL_VALUE = (byte) 0xAB; - final byte SOURCE_VALUE = (byte) 0xCD; - final int PAIRS = 1000; + long oldSize = 1024L; + long newSize = 512L; + long sentinelSize = oldSize - newSize; + byte sentinelValue = (byte) 0xAB; + byte sourceValue = (byte) 0xCD; + int pairs = 1000; - long[] sources = new long[PAIRS]; - long[] sentinels = new long[PAIRS]; + long[] sources = new long[pairs]; + long[] sentinels = new long[pairs]; try { // Allocate all pairs - for (int i = 0; i < PAIRS; i++) { - sources[i] = Platform.allocateMemory(OLD_SIZE); - sentinels[i] = Platform.allocateMemory(SENTINEL_SIZE); + for (int i = 0; i < pairs; i++) { + sources[i] = Platform.allocateMemory(oldSize); + sentinels[i] = Platform.allocateMemory(sentinelSize); - Platform.setMemory(sources[i], SOURCE_VALUE, OLD_SIZE); - Platform.setMemory(sentinels[i], SENTINEL_VALUE, SENTINEL_SIZE); + Platform.setMemory(sources[i], sourceValue, oldSize); + Platform.setMemory(sentinels[i], sentinelValue, sentinelSize); } // Reallocate source blocks - for (int i = 0; i < PAIRS; i++) { + for (int i = 0; i < pairs; i++) { long oldAddr = sources[i]; - long newAddr = Platform.reallocateMemory(oldAddr, OLD_SIZE, NEW_SIZE); + long newAddr = Platform.reallocateMemory(oldAddr, oldSize, newSize); sources[i] = newAddr; } // Verify dest content - for (int i = 0; i < PAIRS; i++) { - for (long j = 0; j < NEW_SIZE; j++) { - Assertions.assertEquals(SOURCE_VALUE, Platform.getByte(null, sources[i] + j), + for (int i = 0; i < pairs; i++) { + for (long j = 0; j < newSize; j++) { + Assertions.assertEquals(sourceValue, Platform.getByte(null, sources[i] + j), "dest block content corrupted at pair " + i); } } // Verify sentinel content - for (int i = 0; i < PAIRS; i++) { - for (long j = 0; j < SENTINEL_SIZE; j++) { - Assertions.assertEquals(SENTINEL_VALUE, Platform.getByte(null, sentinels[i] + j), + for (int i = 0; i < pairs; i++) { + for (long j = 0; j < sentinelSize; j++) { + Assertions.assertEquals(sentinelValue, Platform.getByte(null, sentinels[i] + j), "sentinel corrupted – overflow write detected at pair " + i); } }