[foreign-abi] Test for wrong memory address value

Paul Sandoz paul.sandoz at oracle.com
Fri Dec 20 17:19:09 UTC 2019


This is an addition to the change proposed in [1] adding tests accessing a memory address value with the wrong type.

I think I am correct in assuming the foreign-abi branch is downstream from the foreign-memaccess branch.

Paul.

[1] https://mail.openjdk.java.net/pipermail/panama-dev/2019-December/006899.html <https://mail.openjdk.java.net/pipermail/panama-dev/2019-December/006899.html>

diff -r ae09f6996e74 test/jdk/java/foreign/TestTypeAccess.java
--- a/test/jdk/java/foreign/TestTypeAccess.java Fri Dec 20 09:13:20 2019 -0800
+++ b/test/jdk/java/foreign/TestTypeAccess.java Fri Dec 20 09:13:26 2019 -0800
@@ -27,17 +27,22 @@
  * @run testng TestTypeAccess
  */
 
+import jdk.incubator.foreign.MemoryAddress;
+import jdk.incubator.foreign.MemoryHandles;
 import jdk.incubator.foreign.MemorySegment;
 import jdk.incubator.foreign.MemoryLayouts;
 import org.testng.annotations.*;
 
 import java.lang.invoke.VarHandle;
 import java.lang.invoke.WrongMethodTypeException;
+import java.nio.ByteOrder;
 
 public class TestTypeAccess {
 
     static final VarHandle INT_HANDLE = MemoryLayouts.JAVA_INT.varHandle(int.class);
 
+    static final VarHandle ADDR_HANDLE = MemoryHandles.varHandle(MemoryAddress .class, ByteOrder.nativeOrder());
+
     @Test(expectedExceptions=ClassCastException.class)
     public void testMemoryAddressCoordinateAsString() {
         try (MemorySegment s = MemorySegment.allocateNative(8)) {
@@ -51,4 +56,33 @@
             int v = (int)INT_HANDLE.get(1);
         }
     }
+
+    @Test(expectedExceptions=ClassCastException.class)
+    public void testMemoryAddressValueGetAsString() {
+        try (MemorySegment s = MemorySegment.allocateNative(8)) {
+            String address = (String)ADDR_HANDLE.get(s.baseAddress());
+        }
+    }
+
+    @Test(expectedExceptions=ClassCastException.class)
+    public void testMemoryAddressValueSetAsString() {
+        try (MemorySegment s = MemorySegment.allocateNative(8)) {
+            ADDR_HANDLE.set(s.baseAddress(), "string");
+        }
+    }
+
+    @Test(expectedExceptions=ClassCastException.class)
+    public void testMemoryAddressValueGetAsPrimitive() {
+        try (MemorySegment s = MemorySegment.allocateNative(8)) {
+            int address = (int)ADDR_HANDLE.get(s.baseAddress());
+        }
+    }
+
+    @Test(expectedExceptions=ClassCastException.class)
+    public void testMemoryAddressValueSetAsPrimitive() {
+        try (MemorySegment s = MemorySegment.allocateNative(8)) {
+            ADDR_HANDLE.set(s.baseAddress(), 1);
+        }
+    }
+
 }



More information about the panama-dev mailing list