1 /* 2 * Copyright (c) 2017, 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 package runtime.valhalla.valuetypes.verifier; 24 25 import java.lang.invoke.*; 26 import jdk.experimental.value.*; 27 28 import static jdk.test.lib.Asserts.*; 29 30 /* 31 * @test VunboxErrorIndex 32 * @summary Exercise the verifier for vunbox bytecode with a non-value type CP index. 33 * @library /test/lib 34 * @build runtime.valhalla.valuetypes.verifier.ValueCapableClass 35 * @run main/othervm -Xint -verify -XX:+EnableMVT runtime.valhalla.valuetypes.verifier.VunboxErrorIndex 36 */ 37 public class VunboxErrorIndex { 38 39 public static void main(String[] args) { 40 testCorrectBoxing(); 41 testIncorrectBoxing(); 42 } 43 44 public static void testCorrectBoxing() { 45 Class<?> vcc = ValueCapableClass.class; 46 Class<?> dvt = ValueType.forClass(vcc).valueClass(); 47 48 MethodHandle newDvt = newDvtMh(dvt); 49 MethodHandle box = boxMh(dvt, vcc); 50 MethodHandle unbox = unboxMh(vcc, dvt); 51 52 ValueCapableClass newObject = ValueCapableClass.create(3, (short)7, (short)11); 53 ValueCapableClass newBoxed = null; 54 55 try { 56 ValueCapableClass boxed = (ValueCapableClass) MethodHandles.filterReturnValue(newDvt, box).invokeExact(); 57 // Assuming MVT1.0, where source has no way of holding an actual value at the language level, so: box(unbox(Object)) 58 newBoxed = (ValueCapableClass) MethodHandles.filterReturnValue(unbox, box).invokeExact(newObject); 59 } 60 catch (Throwable t) { fail("Invokation Exception", t); } 61 62 assertTrue(newObject.getX() == newBoxed.getX()); 63 assertTrue(newObject.getY() == newBoxed.getY()); 64 assertTrue(newObject.getZ() == newBoxed.getZ()); 65 } 66 67 public static void testIncorrectBoxing() { 68 Class<?> vcc = ValueCapableClass.class; 69 Class<?> dvt = ValueType.forClass(vcc).valueClass(); 70 71 MethodHandle newDvt = newDvtMh(dvt); 72 MethodHandle box = boxMh(dvt, String.class); // Illegal box type 73 try { 74 MethodHandles.filterReturnValue(newDvt, box).invoke(); 75 fail("Expected ClassCastException"); 76 } 77 catch (ClassCastException cce) {} 78 catch (Throwable t) { fail("Invokation Exception", t); } 79 80 // With -verify, this test will yield a VerifyError 81 // since the constant pool entry at the index is not a 82 // symbolic reference to a direct value class type. 83 try { 84 MethodHandle unbox = unboxMh(vcc, String.class); // Illegal unbox type 85 unbox.invoke(ValueCapableClass.create()); 86 fail("Expected VerifyError"); 87 } 88 catch (Throwable t) { 89 if (!t.getMessage().contains("java.lang.VerifyError")) { 90 fail("Invokation Exception", t); 91 } 92 } 93 } 94 95 /* 96 Create new DVT via loading a value array element. Why this workaround: 97 1) to avoid "ValueType.defaultValueConstant()" which may or may not be implemented with vunbox 98 */ 99 public static MethodHandle newDvtMh(Class<?> dvt) { 100 return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "newDvt", MethodType.methodType(dvt), CODE->{ 101 CODE.iconst_1().anewarray(dvt).iconst_0().vaload().vreturn(); 102 }); 103 } 104 105 public static MethodHandle boxMh(Class<?> inClass, Class<?> outClass) { 106 return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "box", MethodType.methodType(outClass, inClass), CODE->{ 107 CODE.vload(0).vbox(outClass).areturn(); 108 }); 109 } 110 111 public static MethodHandle unboxMh(Class<?> inClass, Class<?> outClass) { 112 return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "box", MethodType.methodType(outClass, inClass), CODE->{ 113 CODE.aload(0).vunbox(outClass).vreturn(); 114 }); 115 } 116 117 }