Skip to content

Commit ff3db52

Browse files
committedJun 16, 2022
8288534: Out of bound errors for memory segment access mentions wrong values
Reviewed-by: psandoz
1 parent 729164f commit ff3db52

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed
 

‎src/java.base/share/classes/java/lang/foreign/AbstractLayout.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929
import java.util.Optional;
3030
import jdk.internal.foreign.Utils;
31+
import jdk.internal.vm.annotation.ForceInline;
3132
import jdk.internal.vm.annotation.Stable;
3233

3334
abstract non-sealed class AbstractLayout implements MemoryLayout {
@@ -86,6 +87,7 @@ public final long bitAlignment() {
8687
}
8788

8889
@Override
90+
@ForceInline
8991
public long byteSize() {
9092
if (cachedSize == 0) {
9193
cachedSize = Utils.bitsToBytesOrThrow(bitSize(),

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.nio.LongBuffer;
4242
import java.nio.ShortBuffer;
4343
import java.util.*;
44+
import java.util.function.BiFunction;
4445
import java.util.function.Consumer;
4546
import java.util.function.Function;
4647
import java.util.function.IntFunction;
@@ -51,6 +52,7 @@
5152
import jdk.internal.access.foreign.UnmapperProxy;
5253
import jdk.internal.misc.ScopedMemoryAccess;
5354
import jdk.internal.util.ArraysSupport;
55+
import jdk.internal.util.Preconditions;
5456
import jdk.internal.vm.annotation.ForceInline;
5557

5658
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
@@ -64,7 +66,7 @@
6466
* are defined for each memory segment kind, see {@link NativeMemorySegmentImpl}, {@link HeapMemorySegmentImpl} and
6567
* {@link MappedMemorySegmentImpl}.
6668
*/
67-
public abstract non-sealed class AbstractMemorySegmentImpl implements MemorySegment, SegmentAllocator, Scoped {
69+
public abstract non-sealed class AbstractMemorySegmentImpl implements MemorySegment, SegmentAllocator, Scoped, BiFunction<String, List<Number>, RuntimeException> {
6870

6971
private static final ScopedMemoryAccess SCOPED_MEMORY_ACCESS = ScopedMemoryAccess.getScopedMemoryAccess();
7072

@@ -394,13 +396,20 @@ private int checkArraySize(String typeName, int elemSize) {
394396
@ForceInline
395397
void checkBounds(long offset, long length) {
396398
if (length > 0) {
397-
Objects.checkIndex(offset, this.length - length + 1);
399+
Preconditions.checkIndex(offset, this.length - length + 1, this);
398400
} else if (length < 0 || offset < 0 ||
399401
offset > this.length - length) {
400402
throw outOfBoundException(offset, length);
401403
}
402404
}
403405

406+
@Override
407+
public RuntimeException apply(String s, List<Number> numbers) {
408+
long offset = numbers.get(0).longValue();
409+
long length = byteSize() - numbers.get(1).longValue() + 1;
410+
return outOfBoundException(offset, length);
411+
}
412+
404413
@Override
405414
@ForceInline
406415
public MemorySessionImpl sessionImpl() {
@@ -413,7 +422,7 @@ public MemorySession session() {
413422
}
414423

415424
private IndexOutOfBoundsException outOfBoundException(long offset, long length) {
416-
return new IndexOutOfBoundsException(String.format("Out of bound access on segment %s; new offset = %d; new length = %d",
425+
return new IndexOutOfBoundsException(String.format("Out of bound access on segment %s; offset = %d; length = %d",
417426
this, offset, length));
418427
}
419428

‎test/jdk/java/foreign/TestSegments.java

+12
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ public void testSmallSegmentMin() {
154154
memorySegment.get(JAVA_INT, offset);
155155
}
156156

157+
@Test
158+
public void testSegmentOOBMessage() {
159+
try {
160+
var segment = MemorySegment.allocateNative(10, MemorySession.global());
161+
segment.getAtIndex(ValueLayout.JAVA_INT, 2);
162+
} catch (IndexOutOfBoundsException ex) {
163+
assertTrue(ex.getMessage().contains("Out of bound access"));
164+
assertTrue(ex.getMessage().contains("offset = 8"));
165+
assertTrue(ex.getMessage().contains("length = 4"));
166+
}
167+
}
168+
157169
@Test(dataProvider = "segmentFactories")
158170
public void testAccessModesOfFactories(Supplier<MemorySegment> segmentSupplier) {
159171
MemorySegment segment = segmentSupplier.get();

0 commit comments

Comments
 (0)
Please sign in to comment.