changeset 13840:1ddd971c9761

GNFI fixes, part2
author Matthias Grimmer <grimmer@ssw.jku.at>
date Thu, 30 Jan 2014 16:41:59 +0100
parents 5fb138b6a92f
children 72c48280c1d0
files graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/NativeFunctionInterface.java graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/ArrayPointerTest.java graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/LibCallTest.java graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/StdLibCallTest.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java
diffstat 5 files changed, 85 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/NativeFunctionInterface.java	Thu Jan 30 15:50:46 2014 +0100
+++ b/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/NativeFunctionInterface.java	Thu Jan 30 16:41:59 2014 +0100
@@ -37,8 +37,8 @@
     NativeLibraryHandle getLibraryHandle(String libPath);
 
     /**
-     * Resolves the <code>NativeFunctionHandle</code> of a native function that can be called. Use a
-     * {@code NativeFunctionHandle} to invoke the native target function.
+     * Resolves the {@Code NativeFunctionHandle} of a native function that can be called. Use
+     * a {@code NativeFunctionHandle} to invoke the native target function.
      * 
      * @param libraryHandle the handle to a resolved library
      * @param functionName the name of the function to be resolved
@@ -49,8 +49,8 @@
     NativeFunctionHandle getFunctionHandle(NativeLibraryHandle libraryHandle, String functionName, Class returnType, Class[] argumentTypes);
 
     /**
-     * Resolves the <code>NativeFunctionHandle</code> of a native function that can be called. Use a
-     * {@code NativeFunctionHandle} to invoke the native target function.
+     * Resolves the {@Code NativeFunctionHandle} of a native function that can be called. Use
+     * a {@code NativeFunctionHandle} to invoke the native target function.
      * 
      * @param functionPointer the function pointer
      * @param returnType the type of the return value
@@ -60,7 +60,7 @@
     NativeFunctionHandle getFunctionHandle(NativeFunctionPointer functionPointer, Class returnType, Class[] argumentTypes);
 
     /**
-     * Resolves the function pointer <code>NativeFunctionPointer</code> of a native function. A
+     * Resolves the function pointer {@Code NativeFunctionPointer} of a native function. A
      * {@code NativeFunctionPointer} wraps the raw pointer value.
      * 
      * @param libraryHandles the handles to a various resolved library, the first library containing
@@ -71,8 +71,8 @@
     NativeFunctionPointer getFunctionPointer(NativeLibraryHandle[] libraryHandles, String functionName);
 
     /**
-     * Resolves the <code>NativeFunctionHandle</code> of a native function that can be called. Use a
-     * {@code NativeFunctionHandle} to invoke the native target function.
+     * Resolves the {@Code NativeFunctionHandle} of a native function that can be called. Use
+     * a {@code NativeFunctionHandle} to invoke the native target function.
      * 
      * @param libraryHandles the handles to a various resolved library, the first library containing
      *            the method wins
@@ -84,8 +84,8 @@
     NativeFunctionHandle getFunctionHandle(NativeLibraryHandle[] libraryHandles, String functionName, Class returnType, Class[] argumentTypes);
 
     /**
-     * Resolves the <code>NativeFunctionHandle</code> of a native function that can be called. Use a
-     * {@code NativeFunctionHandle} to invoke the native target function.
+     * Resolves the {@Code NativeFunctionHandle} of a native function that can be called. Use
+     * a {@code NativeFunctionHandle} to invoke the native target function.
      * 
      * @param functionName the name of the function to be resolved
      * @param returnType the type of the return value
@@ -95,11 +95,11 @@
     NativeFunctionHandle getFunctionHandle(String functionName, Class returnType, Class[] argumentTypes);
 
     /**
-     * Creates <code>NativeFunctionPointer</code> from raw value. A {@code NativeFunctionPointer}
+     * Creates {@Code NativeFunctionPointer} from raw value. A {@code NativeFunctionPointer}
      * wraps the raw pointer value.
      * 
      * @param rawValue Raw pointer value
-     * @return <code>NativeFunctionPointer</code> of the raw pointer
+     * @return {@Code NativeFunctionPointer} of the raw pointer
      */
     NativeFunctionPointer getNativeFunctionPointerFromRawValue(long rawValue);
 }
--- a/graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/ArrayPointerTest.java	Thu Jan 30 15:50:46 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-/*
- * Copyright (c) 2013, 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
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 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
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.graal.ffi.amd64.test;
-
-import org.junit.*;
-
-import com.oracle.graal.api.code.*;
-
-public class ArrayPointerTest extends LibCallTest {
-
-    @Test
-    public void arrayPointerTest01() {
-        byte[] str = new byte[]{'A', 'B', 'C', '\0'};
-        NativeFunctionHandle handle = ffi.getFunctionHandle("puts", int.class, new Class[]{byte[].class});
-        Object[] args = new Object[]{str};
-        int result = (int) handle.call(args);
-        Assert.assertTrue(0 < result);
-    }
-}
--- a/graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/LibCallTest.java	Thu Jan 30 15:50:46 2014 +0100
+++ b/graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/LibCallTest.java	Thu Jan 30 16:41:59 2014 +0100
@@ -24,8 +24,6 @@
 
 import java.lang.reflect.*;
 
-import org.junit.*;
-
 import sun.misc.*;
 
 import com.oracle.graal.api.code.*;
@@ -49,19 +47,6 @@
         }
     }
 
-    @Test
-    public void test() {
-        Long str = unsafe.allocateMemory(4);
-        unsafe.putByte(str, (byte) 'A');
-        unsafe.putByte(str + 1, (byte) 'B');
-        unsafe.putByte(str + 2, (byte) 'C');
-        unsafe.putByte(str + 3, (byte) '\0');
-        NativeFunctionHandle handle = ffi.getFunctionHandle("puts", int.class, new Class[]{long.class});
-        Object[] args = new Object[]{str};
-        int result = (int) handle.call(args);
-        Assert.assertTrue(0 < result);
-    }
-
     protected long getDouble(double val) {
         Long d = unsafe.allocateMemory(8);
         unsafe.putDouble(d, val);
--- a/graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/StdLibCallTest.java	Thu Jan 30 15:50:46 2014 +0100
+++ b/graal/com.oracle.graal.ffi.amd64.test/test/com/oracle/graal/ffi/amd64/test/StdLibCallTest.java	Thu Jan 30 16:41:59 2014 +0100
@@ -28,29 +28,43 @@
 
 public class StdLibCallTest extends LibCallTest {
 
+    private static void copyString(long pointer, String s) {
+        for (int i = 0; i < s.length(); i++) {
+            unsafe.putByte(pointer + i, (byte) s.charAt(i));
+        }
+        unsafe.putByte(pointer + s.length(), (byte) '\0');
+    }
+
+    private static void checkString(long pointer, String s) {
+        for (int i = 0; i < s.length(); i++) {
+            Assert.assertEquals(unsafe.getByte(pointer + i) & 0xFF, (byte) s.charAt(i));
+        }
+        Assert.assertEquals(unsafe.getByte(pointer + s.length()) & 0xFF, (byte) '\0');
+    }
+
+    private static long getBuffer(int length) {
+        return unsafe.allocateMemory(length);
+    }
+
     @Test
     public void mallocTest() {
-        Object[] args = new Object[]{6};
+        String string = "GRAAL";
+        int stringLength = string.length() + 1;
+
+        Object[] args = new Object[]{1};
+        args[0] = stringLength;
         NativeFunctionHandle mallocHandle = ffi.getFunctionHandle("malloc", long.class, new Class[]{int.class});
 
         long p = (long) mallocHandle.call(args);
-        unsafe.putByte(p, (byte) 'G');
-        unsafe.putByte(p + 1, (byte) 'R');
-        unsafe.putByte(p + 2, (byte) 'A');
-        unsafe.putByte(p + 3, (byte) 'A');
-        unsafe.putByte(p + 4, (byte) 'L');
-        unsafe.putByte(p + 5, (byte) '\0');
+        copyString(p, string);
 
-        NativeFunctionHandle putsHandle = ffi.getFunctionHandle("puts", int.class, new Class[]{long.class});
-        Object[] args2 = new Object[]{p};
+        long buffer = getBuffer(stringLength);
+        NativeFunctionHandle putsHandle = ffi.getFunctionHandle("snprintf", int.class, new Class[]{long.class, int.class, long.class});
+        Object[] args2 = new Object[]{buffer, stringLength, p};
         int result = (int) putsHandle.call(args2);
         Assert.assertTrue(0 < result);
-        Assert.assertEquals(unsafe.getByte(p) & 0xFF, 'G');
-        Assert.assertEquals(unsafe.getByte(p + 1) & 0xFF, 'R');
-        Assert.assertEquals(unsafe.getByte(p + 2) & 0xFF, 'A');
-        Assert.assertEquals(unsafe.getByte(p + 3) & 0xFF, 'A');
-        Assert.assertEquals(unsafe.getByte(p + 4) & 0xFF, 'L');
-        Assert.assertEquals(unsafe.getByte(p + 5) & 0xFF, '\0');
+        checkString(p, string);
+        checkString(buffer, string);
 
         NativeFunctionHandle freeHandle = ffi.getFunctionHandle("free", void.class, new Class[]{long.class});
         freeHandle.call(args2);
@@ -59,35 +73,19 @@
     @Test
     public void printfSimpleTest() {
         long str = unsafe.allocateMemory(8);
-        unsafe.putByte(str, (byte) 'A');
-        unsafe.putByte(str + 1, (byte) 'B');
-        unsafe.putByte(str + 2, (byte) ' ');
-        unsafe.putByte(str + 3, (byte) '%');
-        unsafe.putByte(str + 4, (byte) 'f');
-        unsafe.putByte(str + 5, (byte) '%');
-        unsafe.putByte(str + 6, (byte) 'f');
-        unsafe.putByte(str + 7, (byte) '\0');
+        copyString(str, "AB %f%f");
+
+        String referenceString = "AB 1.0000001.000000";
+        int referenceStringLenght = referenceString.length() + 1;
 
-        Object[] args = new Object[]{str, Double.doubleToRawLongBits(1.0), Double.doubleToRawLongBits(1.0)};
-        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("printf", int.class, new Class[]{long.class, double.class, double.class});
+        long buffer = getBuffer(referenceStringLenght);
+
+        Object[] args = new Object[]{buffer, referenceStringLenght, str, Double.doubleToRawLongBits(1.0), Double.doubleToRawLongBits(1.0)};
+        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("snprintf", int.class, new Class[]{long.class, int.class, long.class, double.class, double.class});
 
         int result = (int) printfHandle.call(args);
-        Assert.assertTrue(0 < result);
-    }
 
-    @Test
-    public void putsSimpleTest() {
-        long str = unsafe.allocateMemory(6);
-        unsafe.putByte(str, (byte) 'A');
-        unsafe.putByte(str + 1, (byte) 'B');
-        unsafe.putByte(str + 2, (byte) 'B');
-        unsafe.putByte(str + 3, (byte) 'B');
-        unsafe.putByte(str + 4, (byte) '\0');
-
-        Object[] args = new Object[]{str};
-        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("puts", int.class, new Class[]{long.class});
-
-        int result = (int) printfHandle.call(args);
+        checkString(buffer, referenceString);
         Assert.assertTrue(0 < result);
     }
 
@@ -102,11 +100,17 @@
         }
         unsafe.putByte(str + 24, (byte) '\0');
 
-        Object[] args = new Object[]{str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11]};
-        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("printf", int.class, new Class[]{long.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
-                        int.class, int.class, int.class, int.class});
+        String referenceString = "01234567891011";
+        int referenceStringLenght = referenceString.length() + 1;
+
+        long buffer = getBuffer(referenceStringLenght);
+
+        Object[] args = new Object[]{buffer, referenceStringLenght, str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11]};
+        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("snprintf", int.class, new Class[]{long.class, int.class, long.class, int.class, int.class, int.class, int.class, int.class,
+                        int.class, int.class, int.class, int.class, int.class, int.class, int.class});
 
         int result = (int) printfHandle.call(args);
+        checkString(buffer, referenceString);
         Assert.assertTrue(0 < result);
     }
 
@@ -127,14 +131,20 @@
         }
         unsafe.putByte(str + 48, (byte) '\0');
 
-        Object[] args = new Object[]{str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11], dval[0], dval[1], dval[2], dval[3], dval[4], dval[5],
-                        dval[6], dval[7], dval[8], dval[9], dval[10], dval[11]};
+        String referenceString = "0123456789101112.50000013.50000014.50000015.50000016.50000017.50000018.50000019.50000020.500000" + "21.50000022.50000023.500000";
+        int referenceStringLenght = referenceString.length() + 1;
+
+        long buffer = getBuffer(referenceStringLenght);
 
-        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("printf", int.class, new Class[]{long.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
-                        int.class, int.class, int.class, int.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class,
-                        double.class, double.class, double.class});
+        Object[] args = new Object[]{buffer, referenceStringLenght, str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11], dval[0], dval[1], dval[2],
+                        dval[3], dval[4], dval[5], dval[6], dval[7], dval[8], dval[9], dval[10], dval[11]};
+
+        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("snprintf", int.class, new Class[]{long.class, int.class, long.class, int.class, int.class, int.class, int.class, int.class,
+                        int.class, int.class, int.class, int.class, int.class, int.class, int.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class,
+                        double.class, double.class, double.class, double.class, double.class});
 
         int result = (int) printfHandle.call(args);
+        checkString(buffer, referenceString);
         Assert.assertTrue(0 < result);
     }
 
@@ -161,15 +171,22 @@
         }
         unsafe.putByte(str + 72, (byte) '\0');
 
-        Object[] args = new Object[]{str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11], dval[0], dval[1], dval[2], dval[3], dval[4], dval[5],
-                        dval[6], dval[7], dval[8], dval[9], dval[10], dval[11], cval[0], cval[1], cval[2], cval[3], cval[4], cval[5], cval[6], cval[7], cval[8], cval[9], cval[10], cval[11]};
+        String referenceString = "0123456789101112.50000013.50000014.50000015.50000016.50000017.50000018.50000019.50000020.50000021.50000022.50000023.500000abcdefghijkl";
+        int referenceStringLenght = referenceString.length() + 1;
+
+        long buffer = getBuffer(referenceStringLenght);
 
-        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("printf", int.class, new Class[]{long.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class, int.class,
-                        int.class, int.class, int.class, int.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class,
-                        double.class, double.class, double.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class,
-                        char.class});
+        Object[] args = new Object[]{buffer, referenceStringLenght, str, val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11], dval[0], dval[1], dval[2],
+                        dval[3], dval[4], dval[5], dval[6], dval[7], dval[8], dval[9], dval[10], dval[11], cval[0], cval[1], cval[2], cval[3], cval[4], cval[5], cval[6], cval[7], cval[8], cval[9],
+                        cval[10], cval[11]};
+
+        NativeFunctionHandle printfHandle = ffi.getFunctionHandle("snprintf", int.class, new Class[]{long.class, int.class, long.class, int.class, int.class, int.class, int.class, int.class,
+                        int.class, int.class, int.class, int.class, int.class, int.class, int.class, double.class, double.class, double.class, double.class, double.class, double.class, double.class,
+                        double.class, double.class, double.class, double.class, double.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class, char.class,
+                        char.class, char.class, char.class, char.class});
 
         int result = (int) printfHandle.call(args);
+        checkString(buffer, referenceString);
         Assert.assertTrue(0 < result);
     }
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java	Thu Jan 30 15:50:46 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotVMConfig.java	Thu Jan 30 16:41:59 2014 +0100
@@ -840,9 +840,9 @@
     @HotSpotVMField(name = "JavaThread::_vm_result", type = "oop", get = HotSpotVMField.Type.OFFSET) @Stable public int threadObjectResultOffset;
     @HotSpotVMField(name = "JavaThread::_graal_counters[0]", type = "jlong", get = HotSpotVMField.Type.OFFSET, optional = true) @Stable public int graalCountersThreadOffset;
 
-    @HotSpotVMConstant(name = "libraryLoadAddress") @Stable public long libraryLoadAddress;
-    @HotSpotVMConstant(name = "functionLookupAddress") @Stable public long functionLookupAddress;
-    @HotSpotVMConstant(name = "rtldDefault") @Stable public long rtldDefault;
+    @Stable public long libraryLoadAddress;
+    @Stable public long functionLookupAddress;
+    @Stable public long rtldDefault;
 
     /**
      * This field is used to pass exception objects into and out of the runtime system during