Skip to content

Commit 307b406

Browse files
committed
Make base offset method and invalid offset long
1 parent 45263d7 commit 307b406

30 files changed

+44
-34
lines changed

src/java.base/share/classes/java/lang/invoke/VarHandles.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
196196

197197
Class<?> componentType = arrayClass.getComponentType();
198198

199-
int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
199+
int aoffset = (int) UNSAFE.arrayBaseOffset(arrayClass);
200200
int ascale = UNSAFE.arrayIndexScale(arrayClass);
201201
int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
202202

src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6382,7 +6382,7 @@ public final void compute() {
63826382
= U.objectFieldOffset(ConcurrentHashMap.class, "cellsBusy");
63836383
private static final long CELLVALUE
63846384
= U.objectFieldOffset(CounterCell.class, "value");
6385-
private static final int ABASE = U.arrayBaseOffset(Node[].class);
6385+
private static final long ABASE = U.arrayBaseOffset(Node[].class);
63866386
private static final int ASHIFT;
63876387

63886388
static {

src/java.base/share/classes/jdk/internal/misc/Unsafe.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,11 @@ private void checkWritebackEnabled() {
10431043
* This constant differs from all results that will ever be returned from
10441044
* {@link #staticFieldOffset}, {@link #objectFieldOffset},
10451045
* or {@link #arrayBaseOffset}.
1046+
* <p>
1047+
* The static type is @code long} to emphasize that long arithmetics should
1048+
* always be used for offset calculations to avoid overflows.
10461049
*/
1047-
public static final int INVALID_FIELD_OFFSET = -1;
1050+
public static final long INVALID_FIELD_OFFSET = -1;
10481051

10491052
/**
10501053
* Reports the location of a given field in the storage allocation of its
@@ -1172,11 +1175,15 @@ public void ensureClassInitialized(Class<?> c) {
11721175
* for the same class, you may use that scale factor, together with this
11731176
* base offset, to form new offsets to access elements of arrays of the
11741177
* given class.
1178+
* <p>
1179+
* The return value is in the range of a {@code int}. The return type is
1180+
* {@code long} to emphasize that long arithmetics should always be used
1181+
* for offset calculations to avoid overflows.
11751182
*
11761183
* @see #getInt(Object, long)
11771184
* @see #putInt(Object, long, int)
11781185
*/
1179-
public int arrayBaseOffset(Class<?> arrayClass) {
1186+
public long arrayBaseOffset(Class<?> arrayClass) {
11801187
if (arrayClass == null) {
11811188
throw new NullPointerException();
11821189
}
@@ -1227,6 +1234,9 @@ public int arrayBaseOffset(Class<?> arrayClass) {
12271234
* will generally not work properly with accessors like {@link
12281235
* #getByte(Object, long)}, so the scale factor for such classes is reported
12291236
* as zero.
1237+
* <p>
1238+
* The computation of the actual memory offset should always use {@code
1239+
* long} arithmetics to avoid overflows.
12301240
*
12311241
* @see #arrayBaseOffset
12321242
* @see #getInt(Object, long)
@@ -3840,7 +3850,7 @@ private void putShortParts(Object o, long offset, byte i0, byte i1) {
38403850
private native Object staticFieldBase0(Field f);
38413851
private native boolean shouldBeInitialized0(Class<?> c);
38423852
private native void ensureClassInitialized0(Class<?> c);
3843-
private native int arrayBaseOffset0(Class<?> arrayClass);
3853+
private native int arrayBaseOffset0(Class<?> arrayClass); // public version returns long to promote correct arithmetics
38443854
private native int arrayIndexScale0(Class<?> arrayClass);
38453855
private native int getLoadAverage0(double[] loadavg, int nelems);
38463856

src/jdk.unsupported/share/classes/sun/misc/Unsafe.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ public void freeMemory(long address) {
863863
* @deprecated Not needed when using {@link VarHandle} or {@link java.lang.foreign}.
864864
*/
865865
@Deprecated(since="23", forRemoval=true)
866-
public static final int INVALID_FIELD_OFFSET = jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
866+
public static final int INVALID_FIELD_OFFSET = (int) jdk.internal.misc.Unsafe.INVALID_FIELD_OFFSET;
867867

868868
/**
869869
* Reports the location of a given field in the storage allocation of its
@@ -994,7 +994,7 @@ public Object staticFieldBase(Field f) {
994994
@ForceInline
995995
public int arrayBaseOffset(Class<?> arrayClass) {
996996
beforeMemoryAccess();
997-
return theInternalUnsafe.arrayBaseOffset(arrayClass);
997+
return (int) theInternalUnsafe.arrayBaseOffset(arrayClass);
998998
}
999999

10001000
/** The value of {@code arrayBaseOffset(boolean[].class)}.

test/hotspot/jtreg/compiler/c2/Test6968348.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
public class Test6968348 {
4040
static Unsafe unsafe = Unsafe.getUnsafe();
4141
static final long[] buffer = new long[4096];
42-
static int array_long_base_offset;
42+
static long array_long_base_offset;
4343

4444
public static void main(String[] args) throws Exception {
4545
array_long_base_offset = unsafe.arrayBaseOffset(long[].class);

test/hotspot/jtreg/compiler/c2/cr8004867/TestIntUnsafeCAS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class TestIntUnsafeCAS {
5050
private static final int UNALIGN_OFF = 5;
5151

5252
private static final Unsafe unsafe = Unsafe.getUnsafe();
53-
private static final int BASE;
53+
private static final long BASE;
5454
static {
5555
try {
5656
BASE = unsafe.arrayBaseOffset(int[].class);

test/hotspot/jtreg/compiler/c2/cr8004867/TestIntUnsafeOrdered.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class TestIntUnsafeOrdered {
5050
private static final int UNALIGN_OFF = 5;
5151

5252
private static final Unsafe unsafe = Unsafe.getUnsafe();
53-
private static final int BASE;
53+
private static final long BASE;
5454
static {
5555
try {
5656
BASE = unsafe.arrayBaseOffset(int[].class);

test/hotspot/jtreg/compiler/c2/cr8004867/TestIntUnsafeVolatile.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class TestIntUnsafeVolatile {
5050
private static final int UNALIGN_OFF = 5;
5151

5252
private static final Unsafe unsafe = Unsafe.getUnsafe();
53-
private static final int BASE;
53+
private static final long BASE;
5454
static {
5555
try {
5656
BASE = unsafe.arrayBaseOffset(int[].class);

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestBoolean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestBoolean {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestByte.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestByte {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestChar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestChar {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestDouble.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestDouble {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestFloat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestFloat {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestInt.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestInt {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestLong.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestLong {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestObject.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestObject {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/JdkInternalMiscUnsafeAccessTestShort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JdkInternalMiscUnsafeAccessTestShort {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestBoolean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestBoolean {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestByte.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestByte {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestChar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestChar {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestDouble.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestDouble {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestFloat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestFloat {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestInt.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestInt {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestLong.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestLong {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestObject.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestObject {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/SunMiscUnsafeAccessTestShort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SunMiscUnsafeAccessTestShort {
6060

6161
static final long STATIC_V_OFFSET;
6262

63-
static int ARRAY_OFFSET;
63+
static long ARRAY_OFFSET;
6464

6565
static int ARRAY_SHIFT;
6666

test/hotspot/jtreg/compiler/unsafe/X-UnsafeAccessTest.java.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class $Qualifier$UnsafeAccessTest$Type$ {
6464

6565
static final long STATIC_V_OFFSET;
6666

67-
static int ARRAY_OFFSET;
67+
static long ARRAY_OFFSET;
6868

6969
static int ARRAY_SHIFT;
7070

test/micro/org/openjdk/bench/java/lang/foreign/BulkOps.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class BulkOps {
6969

7070
final int[] ints = new int[ELEM_SIZE];
7171
final MemorySegment bytesSegment = MemorySegment.ofArray(ints);
72-
final int UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
72+
final long UNSAFE_INT_OFFSET = unsafe.arrayBaseOffset(int[].class);
7373

7474
// large(ish) segments/buffers with same content, 0, for mismatch, non-multiple-of-8 sized
7575
static final int SIZE_WITH_TAIL = (1024 * 1024) + 7;

test/micro/org/openjdk/bench/java/lang/foreign/LoopOverNonConstantHeap.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public class LoopOverNonConstantHeap extends JavaLayouts {
5757
static final int ELEM_SIZE = 1_000_000;
5858
static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
5959
static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
60-
static final int UNSAFE_BYTE_BASE = unsafe.arrayBaseOffset(byte[].class);
61-
static final int UNSAFE_INT_BASE = unsafe.arrayBaseOffset(int[].class);
60+
static final long UNSAFE_BYTE_BASE = unsafe.arrayBaseOffset(byte[].class);
61+
static final long UNSAFE_INT_BASE = unsafe.arrayBaseOffset(int[].class);
6262

6363
MemorySegment segment, alignedSegment;
6464
byte[] base;

test/micro/org/openjdk/bench/java/lang/foreign/xor/GetArrayUnsafeXorOpImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public class GetArrayUnsafeXorOpImpl implements XorOp {
4343

4444
static final Unsafe UNSAFE = Utils.unsafe;
45-
static final int BYTE_ARR_OFFSET = Utils.unsafe.arrayBaseOffset(byte[].class);
45+
static final long BYTE_ARR_OFFSET = Utils.unsafe.arrayBaseOffset(byte[].class);
4646

4747
static {
4848
System.loadLibrary("jnitest");

0 commit comments

Comments
 (0)