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