Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk19 Public archive

Commit d7b43af

Browse files
committed
8288761: SegmentAllocator:allocate(long bytesSize) not throwing IAEx when bytesSize < 0
Reviewed-by: psandoz
1 parent 834d92d commit d7b43af

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

src/java.base/share/classes/java/lang/foreign/MemorySegment.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,7 @@ static MemorySegment ofAddress(MemoryAddress address, long bytesSize, MemorySess
883883
Reflection.ensureNativeAccess(Reflection.getCallerClass(), MemorySegment.class, "ofAddress");
884884
Objects.requireNonNull(address);
885885
Objects.requireNonNull(session);
886-
if (bytesSize < 0) {
887-
throw new IllegalArgumentException("Invalid size : " + bytesSize);
888-
}
886+
Utils.checkAllocationSizeAndAlign(bytesSize, 1);
889887
return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(address, bytesSize, session);
890888
}
891889

@@ -957,15 +955,7 @@ static MemorySegment allocateNative(long bytesSize, MemorySession session) {
957955
*/
958956
static MemorySegment allocateNative(long bytesSize, long alignmentBytes, MemorySession session) {
959957
Objects.requireNonNull(session);
960-
if (bytesSize < 0) {
961-
throw new IllegalArgumentException("Invalid allocation size : " + bytesSize);
962-
}
963-
964-
if (alignmentBytes <= 0 ||
965-
((alignmentBytes & (alignmentBytes - 1)) != 0L)) {
966-
throw new IllegalArgumentException("Invalid alignment constraint : " + alignmentBytes);
967-
}
968-
958+
Utils.checkAllocationSizeAndAlign(bytesSize, alignmentBytes);
969959
return NativeMemorySegmentImpl.makeNativeSegment(bytesSize, alignmentBytes, session);
970960
}
971961

src/java.base/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ public final MemorySegment fill(byte value){
153153

154154
@Override
155155
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
156-
if (bytesAlignment <= 0 ||
157-
((bytesAlignment & (bytesAlignment - 1)) != 0L)) {
158-
throw new IllegalArgumentException("Invalid alignment constraint : " + bytesAlignment);
159-
}
156+
Utils.checkAllocationSizeAndAlign(bytesSize, bytesAlignment);
160157
return asSlice(0, bytesSize);
161158
}
162159

src/java.base/share/classes/jdk/internal/foreign/ArenaAllocator.java

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private MemorySegment newSegment(long bytesSize, long bytesAlignment) {
7171

7272
@Override
7373
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
74+
Utils.checkAllocationSizeAndAlign(bytesSize, bytesAlignment);
7475
// try to slice from current segment first...
7576
MemorySegment slice = trySlice(bytesSize, bytesAlignment);
7677
if (slice != null) {

src/java.base/share/classes/jdk/internal/foreign/Utils.java

+13
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,17 @@ public static void checkElementAlignment(MemoryLayout layout, String msg) {
162162
throw new IllegalArgumentException(msg);
163163
}
164164
}
165+
166+
public static void checkAllocationSizeAndAlign(long bytesSize, long alignmentBytes) {
167+
// size should be >= 0
168+
if (bytesSize < 0) {
169+
throw new IllegalArgumentException("Invalid allocation size : " + bytesSize);
170+
}
171+
172+
// alignment should be > 0, and power of two
173+
if (alignmentBytes <= 0 ||
174+
((alignmentBytes & (alignmentBytes - 1)) != 0L)) {
175+
throw new IllegalArgumentException("Invalid alignment constraint : " + alignmentBytes);
176+
}
177+
}
165178
}

test/jdk/java/foreign/TestSegmentAllocators.java

+29
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ public void testBadUnboundedArenaSize() {
139139
SegmentAllocator.newNativeArena( -1, MemorySession.global());
140140
}
141141

142+
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
143+
public void testBadAllocationSize(SegmentAllocator allocator) {
144+
allocator.allocate(-1);
145+
}
146+
147+
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
148+
public void testBadAllocationAlignZero(SegmentAllocator allocator) {
149+
allocator.allocate(1, 0);
150+
}
151+
152+
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
153+
public void testBadAllocationAlignNeg(SegmentAllocator allocator) {
154+
allocator.allocate(1, -1);
155+
}
156+
157+
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
158+
public void testBadAllocationAlignNotPowerTwo(SegmentAllocator allocator) {
159+
allocator.allocate(1, 3);
160+
}
161+
142162
@Test(dataProvider = "arrayAllocations")
143163
public <Z> void testArray(AllocationFactory allocationFactory, ValueLayout layout, AllocationFunction<Object, ValueLayout> allocationFunction, ToArrayHelper<Z> arrayHelper) {
144164
Z arr = arrayHelper.array();
@@ -444,4 +464,13 @@ private MemoryAddress[] wrap(long[] ints) {
444464
}
445465
};
446466
}
467+
468+
@DataProvider(name = "allocators")
469+
static Object[][] allocators() {
470+
return new Object[][] {
471+
{ SegmentAllocator.implicitAllocator() },
472+
{ SegmentAllocator.newNativeArena(MemorySession.global()) },
473+
{ SegmentAllocator.prefixAllocator(MemorySegment.allocateNative(10, MemorySession.global())) },
474+
};
475+
}
447476
}

0 commit comments

Comments
 (0)