Skip to content

Commit 0dab920

Browse files
committed
8343984: Fix Unsafe address overflow
Reviewed-by: pminborg, alanb
1 parent 168b18e commit 0dab920

File tree

11 files changed

+34
-34
lines changed

11 files changed

+34
-34
lines changed

src/java.base/share/classes/java/lang/StringLatin1.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -830,22 +830,22 @@ static Stream<String> lines(byte[] value) {
830830
static void putCharsAt(byte[] val, int index, int c1, int c2, int c3, int c4) {
831831
assert index >= 0 && index + 3 < length(val) : "Trusted caller missed bounds check";
832832
// Don't use the putChar method, Its instrinsic will cause C2 unable to combining values into larger stores.
833-
long address = Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
834-
UNSAFE.putByte(val, address , (byte)(c1));
835-
UNSAFE.putByte(val, address + 1, (byte)(c2));
836-
UNSAFE.putByte(val, address + 2, (byte)(c3));
837-
UNSAFE.putByte(val, address + 3, (byte)(c4));
833+
long offset = (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
834+
UNSAFE.putByte(val, offset , (byte)(c1));
835+
UNSAFE.putByte(val, offset + 1, (byte)(c2));
836+
UNSAFE.putByte(val, offset + 2, (byte)(c3));
837+
UNSAFE.putByte(val, offset + 3, (byte)(c4));
838838
}
839839

840840
static void putCharsAt(byte[] val, int index, int c1, int c2, int c3, int c4, int c5) {
841841
assert index >= 0 && index + 4 < length(val) : "Trusted caller missed bounds check";
842842
// Don't use the putChar method, Its instrinsic will cause C2 unable to combining values into larger stores.
843-
long address = Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
844-
UNSAFE.putByte(val, address , (byte)(c1));
845-
UNSAFE.putByte(val, address + 1, (byte)(c2));
846-
UNSAFE.putByte(val, address + 2, (byte)(c3));
847-
UNSAFE.putByte(val, address + 3, (byte)(c4));
848-
UNSAFE.putByte(val, address + 4, (byte)(c5));
843+
long offset = (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
844+
UNSAFE.putByte(val, offset , (byte)(c1));
845+
UNSAFE.putByte(val, offset + 1, (byte)(c2));
846+
UNSAFE.putByte(val, offset + 2, (byte)(c3));
847+
UNSAFE.putByte(val, offset + 3, (byte)(c4));
848+
UNSAFE.putByte(val, offset + 4, (byte)(c5));
849849
}
850850

851851
public static void putChar(byte[] val, int index, int c) {

src/java.base/share/classes/java/util/zip/ZipUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static final int get16(byte[] b, int off) {
174174
Preconditions.checkIndex(off, b.length, Preconditions.AIOOBE_FORMATTER);
175175
Preconditions.checkIndex(off + 1, b.length, Preconditions.AIOOBE_FORMATTER);
176176
return Short.toUnsignedInt(
177-
UNSAFE.getShortUnaligned(b, off + Unsafe.ARRAY_BYTE_BASE_OFFSET, false));
177+
UNSAFE.getShortUnaligned(b, off + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET, false));
178178
}
179179

180180
/**
@@ -185,7 +185,7 @@ public static final long get32(byte[] b, int off) {
185185
Preconditions.checkIndex(off, b.length, Preconditions.AIOOBE_FORMATTER);
186186
Preconditions.checkIndex(off + 3, b.length, Preconditions.AIOOBE_FORMATTER);
187187
return Integer.toUnsignedLong(
188-
UNSAFE.getIntUnaligned(b, off + Unsafe.ARRAY_BYTE_BASE_OFFSET, false));
188+
UNSAFE.getIntUnaligned(b, off + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET, false));
189189
}
190190

191191
/**
@@ -195,7 +195,7 @@ public static final long get32(byte[] b, int off) {
195195
public static final long get64S(byte[] b, int off) {
196196
Preconditions.checkIndex(off, b.length, Preconditions.AIOOBE_FORMATTER);
197197
Preconditions.checkIndex(off + 7, b.length, Preconditions.AIOOBE_FORMATTER);
198-
return UNSAFE.getLongUnaligned(b, off + Unsafe.ARRAY_BYTE_BASE_OFFSET, false);
198+
return UNSAFE.getLongUnaligned(b, off + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET, false);
199199
}
200200

201201
/**
@@ -206,7 +206,7 @@ public static final long get64S(byte[] b, int off) {
206206
public static final int get32S(byte[] b, int off) {
207207
Preconditions.checkIndex(off, b.length, Preconditions.AIOOBE_FORMATTER);
208208
Preconditions.checkIndex(off + 3, b.length, Preconditions.AIOOBE_FORMATTER);
209-
return UNSAFE.getIntUnaligned(b, off + Unsafe.ARRAY_BYTE_BASE_OFFSET, false);
209+
return UNSAFE.getIntUnaligned(b, off + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET, false);
210210
}
211211

212212
/*

src/java.base/unix/classes/sun/nio/fs/UnixUserDefinedFileAttributeView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -194,7 +194,7 @@ public int read(String name, ByteBuffer dst) throws IOException {
194194
int n = read(name, address, rem);
195195

196196
// copy from buffer into backing array
197-
int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
197+
long off = dst.arrayOffset() + pos + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET;
198198
unsafe.copyMemory(null, address, dst.array(), off, n);
199199
dst.position(pos + n);
200200

@@ -257,7 +257,7 @@ public int write(String name, ByteBuffer src) throws IOException {
257257

258258
if (src.hasArray()) {
259259
// copy from backing array into buffer
260-
int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
260+
long off = src.arrayOffset() + pos + (long) Unsafe.ARRAY_BYTE_BASE_OFFSET;
261261
unsafe.copyMemory(src.array(), off, null, address, rem);
262262
} else {
263263
// backing array not accessible so transfer via temporary array

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1005,56 +1005,56 @@ protected void writePrimitiveArray(TypeArray array) throws IOException {
10051005

10061006
private void writeBooleanArray(TypeArray array, int length) throws IOException {
10071007
for (int index = 0; index < length; index++) {
1008-
long offset = BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE;
1008+
long offset = (long) BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE;
10091009
out.writeBoolean(array.getHandle().getJBooleanAt(offset));
10101010
}
10111011
}
10121012

10131013
private void writeByteArray(TypeArray array, int length) throws IOException {
10141014
for (int index = 0; index < length; index++) {
1015-
long offset = BYTE_BASE_OFFSET + index * BYTE_SIZE;
1015+
long offset = (long) BYTE_BASE_OFFSET + index * BYTE_SIZE;
10161016
out.writeByte(array.getHandle().getJByteAt(offset));
10171017
}
10181018
}
10191019

10201020
private void writeShortArray(TypeArray array, int length) throws IOException {
10211021
for (int index = 0; index < length; index++) {
1022-
long offset = SHORT_BASE_OFFSET + index * SHORT_SIZE;
1022+
long offset = (long) SHORT_BASE_OFFSET + index * SHORT_SIZE;
10231023
out.writeShort(array.getHandle().getJShortAt(offset));
10241024
}
10251025
}
10261026

10271027
private void writeIntArray(TypeArray array, int length) throws IOException {
10281028
for (int index = 0; index < length; index++) {
1029-
long offset = INT_BASE_OFFSET + index * INT_SIZE;
1029+
long offset = (long) INT_BASE_OFFSET + index * INT_SIZE;
10301030
out.writeInt(array.getHandle().getJIntAt(offset));
10311031
}
10321032
}
10331033

10341034
private void writeLongArray(TypeArray array, int length) throws IOException {
10351035
for (int index = 0; index < length; index++) {
1036-
long offset = LONG_BASE_OFFSET + index * LONG_SIZE;
1036+
long offset = (long) LONG_BASE_OFFSET + index * LONG_SIZE;
10371037
out.writeLong(array.getHandle().getJLongAt(offset));
10381038
}
10391039
}
10401040

10411041
private void writeCharArray(TypeArray array, int length) throws IOException {
10421042
for (int index = 0; index < length; index++) {
1043-
long offset = CHAR_BASE_OFFSET + index * CHAR_SIZE;
1043+
long offset = (long) CHAR_BASE_OFFSET + index * CHAR_SIZE;
10441044
out.writeChar(array.getHandle().getJCharAt(offset));
10451045
}
10461046
}
10471047

10481048
private void writeFloatArray(TypeArray array, int length) throws IOException {
10491049
for (int index = 0; index < length; index++) {
1050-
long offset = FLOAT_BASE_OFFSET + index * FLOAT_SIZE;
1050+
long offset = (long) FLOAT_BASE_OFFSET + index * FLOAT_SIZE;
10511051
out.writeFloat(array.getHandle().getJFloatAt(offset));
10521052
}
10531053
}
10541054

10551055
private void writeDoubleArray(TypeArray array, int length) throws IOException {
10561056
for (int index = 0; index < length; index++) {
1057-
long offset = DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE;
1057+
long offset = (long) DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE;
10581058
out.writeDouble(array.getHandle().getJDoubleAt(offset));
10591059
}
10601060
}

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/ByteVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4101,7 +4101,7 @@ static long booleanArrayAddress(boolean[] a, int index) {
41014101

41024102
@ForceInline
41034103
static long byteArrayAddress(byte[] a, int index) {
4104-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
4104+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
41054105
}
41064106

41074107
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/DoubleVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3621,7 +3621,7 @@ static long arrayAddress(double[] a, int index) {
36213621

36223622
@ForceInline
36233623
static long byteArrayAddress(byte[] a, int index) {
3624-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
3624+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
36253625
}
36263626

36273627
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/FloatVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ static long arrayAddress(float[] a, int index) {
35713571

35723572
@ForceInline
35733573
static long byteArrayAddress(byte[] a, int index) {
3574-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
3574+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
35753575
}
35763576

35773577
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/IntVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3739,7 +3739,7 @@ static long arrayAddress(int[] a, int index) {
37393739

37403740
@ForceInline
37413741
static long byteArrayAddress(byte[] a, int index) {
3742-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
3742+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
37433743
}
37443744

37453745
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/LongVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3674,7 +3674,7 @@ static long arrayAddress(long[] a, int index) {
36743674

36753675
@ForceInline
36763676
static long byteArrayAddress(byte[] a, int index) {
3677-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
3677+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
36783678
}
36793679

36803680
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/ShortVector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,7 +4092,7 @@ static long charArrayAddress(char[] a, int index) {
40924092

40934093
@ForceInline
40944094
static long byteArrayAddress(byte[] a, int index) {
4095-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
4095+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
40964096
}
40974097

40984098
// ================================================

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5310,7 +5310,7 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
53105310

53115311
@ForceInline
53125312
static long byteArrayAddress(byte[] a, int index) {
5313-
return Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
5313+
return (long) Unsafe.ARRAY_BYTE_BASE_OFFSET + index;
53145314
}
53155315

53165316
// ================================================

0 commit comments

Comments
 (0)