1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.valhalla.valuetypes;
  25 
  26 import jdk.test.lib.Asserts;
  27 
  28 import java.lang.reflect.Method;
  29 
  30 /*
  31  * @test
  32  * @summary Test calling native methods with value type arguments from compiled code.
  33  * @library /testlibrary /test/lib /compiler/whitebox /
  34  * @requires os.simpleArch == "x64"
  35  * @compile TestJNICalls.java
  36  * @run driver ClassFileInstaller sun.hotspot.WhiteBox jdk.test.lib.Platform
  37  * @run main/othervm/timeout=120 -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
  38  *                               -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI
  39  *                               compiler.valhalla.valuetypes.ValueTypeTest
  40  *                               compiler.valhalla.valuetypes.TestJNICalls
  41  */
  42 public class TestJNICalls extends ValueTypeTest {
  43     // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters()
  44     @Override
  45     public String[] getExtraVMParameters(int scenario) {
  46         return null;
  47     }
  48 
  49     public static void main(String[] args) throws Throwable {
  50         TestJNICalls test = new TestJNICalls();
  51         test.run(args, MyValue1.class);
  52     }
  53 
  54     static {
  55         System.loadLibrary("TestJNICalls");
  56     }
  57 
  58     public native Object testMethod1(MyValue1 o);
  59     public native long testMethod2(MyValue1 o);
  60 
  61     // Pass a value type to a native method that calls back into Java code and returns a value
  62     @Test
  63     @Warmup(10000) // Make sure native method is compiled
  64     public MyValue1 test1(MyValue1 vt, boolean callback) {
  65         if (!callback) {
  66           return (MyValue1)testMethod1(vt);
  67         } else {
  68           return vt;
  69         }
  70     }
  71 
  72     @DontCompile
  73     public void test1_verifier(boolean warmup) {
  74         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
  75         MyValue1 result = test1(vt, false);
  76         Asserts.assertEQ(result.hash(), vt.hash());
  77         result = test1(vt, true);
  78         Asserts.assertEQ(result.hash(), vt.hash());
  79     }
  80 
  81     // Pass a value type to a native method that calls the hash method and returns the result
  82     @Test
  83     @Warmup(10000) // Make sure native method is compiled
  84     public long test2(MyValue1 vt) {
  85         return testMethod2(vt);
  86     }
  87 
  88     @DontCompile
  89     public void test2_verifier(boolean warmup) {
  90         MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL);
  91         long result = test2(vt);
  92         Asserts.assertEQ(result, vt.hash());
  93     }
  94 
  95     static inline class MyValueWithNative {
  96         public final int x;
  97 
  98         private MyValueWithNative(int x) {
  99             this.x = x;
 100         }
 101 
 102         public native int testMethod3();
 103     }
 104 
 105     // Call a native method with a value type receiver
 106     @Test
 107     @Warmup(10000) // Make sure native method is compiled
 108     public int test3(MyValueWithNative vt) {
 109         return vt.testMethod3();
 110     }
 111 
 112     @DontCompile
 113     public void test3_verifier(boolean warmup) {
 114         MyValueWithNative vt = new MyValueWithNative(rI);
 115         int result = test3(vt);
 116         Asserts.assertEQ(result, rI);
 117     }
 118 }