8257186: Size of heap segments is not computed correctlyFix overflow in size computation for heap segments

Reviewed-by: jvernee, chegar
This commit is contained in:
Maurizio Cimadamore 2020-12-07 17:25:23 +00:00
parent b4b9828cb0
commit bbc44f57c4
2 changed files with 29 additions and 8 deletions

View File

@ -98,7 +98,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(byte[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_BYTE_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_BYTE_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfByte(Unsafe.ARRAY_BYTE_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -122,7 +122,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(char[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_CHAR_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_CHAR_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfChar(Unsafe.ARRAY_CHAR_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -146,7 +146,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(short[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_SHORT_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_SHORT_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfShort(Unsafe.ARRAY_SHORT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -170,7 +170,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(int[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_INT_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_INT_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfInt(Unsafe.ARRAY_INT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -194,7 +194,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(long[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_LONG_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_LONG_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfLong(Unsafe.ARRAY_LONG_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -218,7 +218,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(float[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_FLOAT_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_FLOAT_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfFloat(Unsafe.ARRAY_FLOAT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}
@ -242,7 +242,7 @@ public abstract class HeapMemorySegmentImpl<H> extends AbstractMemorySegmentImpl
public static MemorySegment fromArray(double[] arr) {
Objects.requireNonNull(arr);
int byteSize = arr.length * Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
long byteSize = (long)arr.length * Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null);
return new OfDouble(Unsafe.ARRAY_DOUBLE_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope);
}

View File

@ -23,7 +23,7 @@
/*
* @test
* @run testng/othervm -XX:MaxDirectMemorySize=1M TestSegments
* @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M TestSegments
*/
import jdk.incubator.foreign.MappedMemorySegments;
@ -47,6 +47,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.LongFunction;
import java.util.function.Supplier;
import java.util.stream.Stream;
@ -277,6 +279,13 @@ public class TestSegments {
segment.hasAccessModes((1 << AccessActions.values().length) + 1);
}
@Test(dataProvider = "heapFactories")
public void testBigHeapSegments(IntFunction<MemorySegment> heapSegmentFactory, int factor) {
int bigSize = (Integer.MAX_VALUE / factor) + 1;
MemorySegment segment = heapSegmentFactory.apply(bigSize);
assertTrue(segment.byteSize() > 0);
}
@DataProvider(name = "badSizeAndAlignments")
public Object[][] sizesAndAlignments() {
return new Object[][] {
@ -445,4 +454,16 @@ public class TestSegments {
abstract void run(MemorySegment segment);
}
@DataProvider(name = "heapFactories")
public Object[][] heapFactories() {
return new Object[][] {
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new char[size]), 2 },
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new short[size]), 2 },
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new int[size]), 4 },
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new float[size]), 4 },
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new long[size]), 8 },
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new double[size]), 8 }
};
}
}