Skip to content
Permalink
Browse files

8236634: Memory Access API tests fail on 32-bit

Reviewed-by: mcimadamore, shade
  • Loading branch information
nick-arm committed Jan 13, 2020
1 parent 34b9c84 commit 2afe1c6c23ff6abef47bde218054268f6964e953
@@ -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
@@ -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.*;

@@ -76,8 +77,8 @@
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++) {
@@ -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();
}

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

@@ -119,8 +120,8 @@
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) {
@@ -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;
@@ -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);
@@ -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);
@@ -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);
@@ -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])) {
@@ -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])) {
@@ -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
@@ -23,6 +23,7 @@

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

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

0 comments on commit 2afe1c6

Please sign in to comment.