Skip to content

Commit

Permalink
8236634: Memory Access API tests fail on 32-bit
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore, shade
  • Loading branch information
nick-arm committed Jan 13, 2020
1 parent 34b9c84 commit 2afe1c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
14 changes: 10 additions & 4 deletions test/jdk/java/foreign/TestArrays.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -38,6 +38,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.testng.SkipException;
import org.testng.annotations.*;
import static org.testng.Assert.*;

Expand Down Expand Up @@ -76,8 +77,8 @@ public class TestArrays {
static VarHandle shortHandle = shorts.varHandle(short.class, PathElement.sequenceElement());
static VarHandle intHandle = ints.varHandle(int.class, PathElement.sequenceElement());
static VarHandle floatHandle = floats.varHandle(float.class, PathElement.sequenceElement());
static VarHandle longHandle = doubles.varHandle(long.class, PathElement.sequenceElement());
static VarHandle doubleHandle = longs.varHandle(double.class, PathElement.sequenceElement());
static VarHandle longHandle = longs.varHandle(long.class, PathElement.sequenceElement());
static VarHandle doubleHandle = doubles.varHandle(double.class, PathElement.sequenceElement());

static void initBytes(MemoryAddress base, SequenceLayout seq, BiConsumer<MemoryAddress, Long> handleSetter) {
for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
Expand All @@ -103,8 +104,13 @@ public void testArrays(Consumer<MemoryAddress> init, SequenceLayout layout) {
}
}

@Test(expectedExceptions = UnsupportedOperationException.class)
@Test(expectedExceptions = { UnsupportedOperationException.class,
OutOfMemoryError.class })
public void testTooBigForArray() {
if (System.getProperty("sun.arch.data.model").equals("32")) {
throw new SkipException("32-bit Unsafe does not support this allocation size");
}

MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).toByteArray();
}

Expand Down
26 changes: 22 additions & 4 deletions test/jdk/java/foreign/TestByteBuffer.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -70,6 +70,7 @@
import java.util.stream.Stream;

import jdk.internal.foreign.MemoryAddressImpl;
import org.testng.SkipException;
import org.testng.annotations.*;
import sun.nio.ch.DirectBuffer;

Expand Down Expand Up @@ -119,8 +120,8 @@ public class TestByteBuffer {
static VarHandle shortHandle = shorts.varHandle(short.class, PathElement.sequenceElement());
static VarHandle intHandle = ints.varHandle(int.class, PathElement.sequenceElement());
static VarHandle floatHandle = floats.varHandle(float.class, PathElement.sequenceElement());
static VarHandle longHandle = doubles.varHandle(long.class, PathElement.sequenceElement());
static VarHandle doubleHandle = longs.varHandle(double.class, PathElement.sequenceElement());
static VarHandle longHandle = longs.varHandle(long.class, PathElement.sequenceElement());
static VarHandle doubleHandle = doubles.varHandle(double.class, PathElement.sequenceElement());


static void initTuples(MemoryAddress base) {
Expand Down Expand Up @@ -247,6 +248,13 @@ static void withMappedBuffer(FileChannel channel, FileChannel.MapMode mode, long
}
}

static void checkByteArrayAlignment(MemoryLayout layout) {
if (layout.bitSize() > 32
&& System.getProperty("sun.arch.data.model").equals("32")) {
throw new SkipException("avoid unaligned access on 32-bit system");
}
}

@Test(dataProvider = "bufferOps")
public void testScopedBuffer(Function<ByteBuffer, Buffer> bufferFactory, Map<Method, Object[]> members) {
Buffer bb;
Expand Down Expand Up @@ -338,6 +346,7 @@ public void testResizeOffheap(Consumer<MemoryAddress> checker, Consumer<MemoryAd

@Test(dataProvider="resizeOps")
public void testResizeHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());
int capacity = (int)seq.byteSize();
MemoryAddress base = MemorySegment.ofArray(new byte[capacity]).baseAddress();
initializer.accept(base);
Expand All @@ -346,6 +355,7 @@ public void testResizeHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddre

@Test(dataProvider="resizeOps")
public void testResizeBuffer(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());
int capacity = (int)seq.byteSize();
MemoryAddress base = MemorySegment.ofByteBuffer(ByteBuffer.wrap(new byte[capacity])).baseAddress();
initializer.accept(base);
Expand All @@ -354,6 +364,7 @@ public void testResizeBuffer(Consumer<MemoryAddress> checker, Consumer<MemoryAdd

@Test(dataProvider="resizeOps")
public void testResizeRoundtripHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());
int capacity = (int)seq.byteSize();
byte[] arr = new byte[capacity];
MemorySegment segment = MemorySegment.ofArray(arr);
Expand Down Expand Up @@ -382,13 +393,19 @@ public void testBufferOnClosedScope() {
leaked.asByteBuffer();
}

@Test(expectedExceptions = UnsupportedOperationException.class)
@Test(expectedExceptions = { UnsupportedOperationException.class,
OutOfMemoryError.class })
public void testTooBigForByteBuffer() {
if (System.getProperty("sun.arch.data.model").equals("32")) {
throw new SkipException("32-bit Unsafe does not support this allocation size");
}

MemorySegment.allocateNative((long) Integer.MAX_VALUE * 2).asByteBuffer();
}

@Test(dataProvider="resizeOps")
public void testCopyHeapToNative(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());
int bytes = (int)seq.byteSize();
try (MemorySegment nativeArray = MemorySegment.allocateNative(bytes);
MemorySegment heapArray = MemorySegment.ofArray(new byte[bytes])) {
Expand All @@ -400,6 +417,7 @@ public void testCopyHeapToNative(Consumer<MemoryAddress> checker, Consumer<Memor

@Test(dataProvider="resizeOps")
public void testCopyNativeToHeap(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());
int bytes = (int)seq.byteSize();
try (MemorySegment nativeArray = MemorySegment.allocateNative(seq);
MemorySegment heapArray = MemorySegment.ofArray(new byte[bytes])) {
Expand Down
3 changes: 2 additions & 1 deletion test/jdk/java/foreign/TestMemoryAlignment.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,6 +23,7 @@

/*
* @test
* @requires vm.bits != "32"
* @run testng TestMemoryAlignment
*/

Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/foreign/TestSegments.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -58,7 +58,8 @@ public void testBadAllocateLayout(MemoryLayout layout) {
MemorySegment.allocateNative(layout);
}

@Test(expectedExceptions = OutOfMemoryError.class)
@Test(expectedExceptions = { OutOfMemoryError.class,
IllegalArgumentException.class })
public void testAllocateTooBig() {
MemorySegment.allocateNative(Long.MAX_VALUE);
}
Expand Down
33 changes: 17 additions & 16 deletions test/jdk/java/foreign/libNativeAccess.c
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -16,96 +16,97 @@
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "jni.h"
#include <stdio.h>
#include <stdint.h>

JNIEXPORT jbyte JNICALL
Java_TestNative_getByteRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jbyte *arr = (jbyte*)addr;
jbyte *arr = (jbyte*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jbyte JNICALL
Java_TestNative_getByteBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getByteRaw(env, cls, addr, index);
}

JNIEXPORT jchar JNICALL
Java_TestNative_getCharRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jchar *arr = (jchar*)addr;
jchar *arr = (jchar*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jchar JNICALL
Java_TestNative_getCharBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getCharRaw(env, cls, addr, index);
}

JNIEXPORT jshort JNICALL
Java_TestNative_getShortRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jshort *arr = (jshort*)addr;
jshort *arr = (jshort*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jshort JNICALL
Java_TestNative_getShortBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getShortRaw(env, cls, addr, index);
}

JNIEXPORT jint JNICALL
Java_TestNative_getIntRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jint *arr = (jint*)addr;
jint *arr = (jint*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jint JNICALL
Java_TestNative_getIntBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getIntRaw(env, cls, addr, index);
}

JNIEXPORT jfloat JNICALL
Java_TestNative_getFloatRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jfloat *arr = (jfloat*)addr;
jfloat *arr = (jfloat*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jfloat JNICALL
Java_TestNative_getFloatBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getFloatRaw(env, cls, addr, index);
}

JNIEXPORT jlong JNICALL
Java_TestNative_getLongRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jlong *arr = (jlong*)addr;
jlong *arr = (jlong*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jlong JNICALL
Java_TestNative_getLongBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getLongRaw(env, cls, addr, index);
}

JNIEXPORT jdouble JNICALL
Java_TestNative_getDoubleRaw(JNIEnv *env, jclass cls, jlong addr, jint index) {
jdouble *arr = (jdouble*)addr;
jdouble *arr = (jdouble*)(uintptr_t)addr;
return arr[index];
}

JNIEXPORT jdouble JNICALL
Java_TestNative_getDoubleBuffer(JNIEnv *env, jclass cls, jobject buf, jint index) {
jlong addr = (jlong)(*env)->GetDirectBufferAddress(env, buf);
jlong addr = (jlong)(uintptr_t)(*env)->GetDirectBufferAddress(env, buf);
return Java_TestNative_getDoubleRaw(env, cls, addr, index);
}

Expand Down

0 comments on commit 2afe1c6

Please sign in to comment.