Skip to content
Permalink
Browse files
8257186: Size of heap segments is not computed correctlyFix overflow …
…in size computation for heap segments

Reviewed-by: jvernee, chegar
  • Loading branch information
mcimadamore committed Dec 7, 2020
1 parent b4b9828 commit bbc44f57c4714a49e72a09f0b9d05765d6b41e9b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
@@ -98,7 +98,7 @@ byte[] base() {

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 @@ char[] base() {

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 @@ short[] base() {

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 @@ int[] base() {

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 @@ long[] base() {

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 @@ float[] base() {

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 @@ OfDouble dup(long offset, long size, int mask, MemoryScope scope) {

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);
}
@@ -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.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 void testBadHasAccessModes() {
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 @@ void run(MemorySegment segment) {

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 }
};
}
}

1 comment on commit bbc44f5

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on bbc44f5 Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.