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 * @compile -XDallowWithFieldOperator TestJNICalls.java 35 * @run driver ClassFileInstaller sun.hotspot.WhiteBox jdk.test.lib.Platform 36 * @run main/othervm/timeout=120 -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions 37 * -XX:+UnlockExperimentalVMOptions -XX:+WhiteBoxAPI -XX:+EnableValhalla 38 * compiler.valhalla.valuetypes.ValueTypeTest 39 * compiler.valhalla.valuetypes.TestJNICalls 40 */ 41 public class TestJNICalls extends ValueTypeTest { 42 // Extra VM parameters for some test scenarios. See ValueTypeTest.getVMParameters() 43 @Override 44 public String[] getExtraVMParameters(int scenario) { 45 return null; 46 } 47 48 public static void main(String[] args) throws Throwable { 49 TestJNICalls test = new TestJNICalls(); 50 test.run(args, MyValue1.class); 51 } 52 53 static { 54 System.loadLibrary("TestJNICalls"); 55 } 56 57 public native Object testMethod1(MyValue1 o); 58 public native long testMethod2(MyValue1 o); 59 60 // Pass a value type to a native method that calls back into Java code and returns a value 61 @Test 62 @Warmup(10000) // Make sure native method is compiled 63 public MyValue1 test1(MyValue1 vt, boolean callback) { 64 if (!callback) { 65 return (MyValue1)testMethod1(vt); 66 } else { 67 return vt; 68 } 69 } 70 71 @DontCompile 72 public void test1_verifier(boolean warmup) { 73 MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL); 74 MyValue1 result = test1(vt, false); 75 Asserts.assertEQ(result.hash(), vt.hash()); 76 result = test1(vt, true); 77 Asserts.assertEQ(result.hash(), vt.hash()); 78 } 79 80 // Pass a value type to a native method that calls the hash method and returns the result 81 @Test 82 @Warmup(10000) // Make sure native method is compiled 83 public long test2(MyValue1 vt) { 84 return testMethod2(vt); 85 } 86 87 @DontCompile 88 public void test2_verifier(boolean warmup) { 89 MyValue1 vt = MyValue1.createWithFieldsInline(rI, rL); 90 long result = test2(vt); 91 Asserts.assertEQ(result, vt.hash()); 92 } 93 94 static value class MyValueWithNative { 95 public final int x; 96 97 private MyValueWithNative(int x) { 98 this.x = x; 99 } 100 101 public native int testMethod3(); 102 } 103 104 // Call a native method with a value type receiver 105 @Test 106 @Warmup(10000) // Make sure native method is compiled 107 public int test3(MyValueWithNative vt) { 108 return vt.testMethod3(); 109 } 110 111 @DontCompile 112 public void test3_verifier(boolean warmup) { 113 MyValueWithNative vt = new MyValueWithNative(rI); 114 int result = test3(vt); 115 Asserts.assertEQ(result, rI); 116 } 117 }