1 /* 2 * Copyright (c) 2016, 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 25 import java.lang.invoke.*; 26 27 /** 28 * @test 29 * @bug 8148752 30 * @summary Test correct casting of MH arguments during inlining. 31 * @run main LongReferenceCastingTest 32 */ 33 public class LongReferenceCastingTest { 34 static final String MY_STRING = "myString"; 35 static final MethodHandle MH; 36 37 static { 38 try { 39 MethodHandles.Lookup lookup = MethodHandles.lookup(); 40 MethodType mt = MethodType.methodType(String.class, long.class, Object.class, String.class); 41 MH = lookup.findVirtual(LongReferenceCastingTest.class, "myMethod", mt); 42 } catch (Exception e) { 43 throw new Error(e); 44 } 45 } 46 47 public String myMethod(long l, Object o, String s) { 48 // The long argument occupies two stack slots, causing C2 to treat it as 49 // two arguments and casting the fist one two long and the second one to Object. 50 // As a result, Object o is casted to String and the o.toString() call is 51 // inlined as String::toString(). We fail at runtime because 'o' is not a String. 52 return o.toString(); 53 } 54 55 public String toString() { 56 return MY_STRING; 57 } 58 59 public static void main(String[] args) throws Exception { 60 LongReferenceCastingTest test = new LongReferenceCastingTest(); 61 try { 62 for (int i = 0; i < 20_000; ++i) { 63 if (!test.invoke().equals(MY_STRING)) { 64 throw new RuntimeException("Invalid string"); 65 } 66 } 67 } catch (Throwable t) { 68 throw new RuntimeException("Test failed", t); 69 } 70 } 71 72 public String invoke() throws Throwable { 73 return (String) MH.invokeExact(this, 0L, (Object)this, MY_STRING); 74 } 75 }