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 
  24 /*
  25  * @test
  26  * @bug 8186046
  27  * @summary Test bootstrap methods returning the wrong type
  28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
  30  * @run testng CondyWrongType
  31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyWrongType
  32  */
  33 
  34 import org.testng.Assert;
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 import test.java.lang.invoke.lib.InstructionHelper;
  38 
  39 import java.lang.invoke.MethodHandle;
  40 import java.lang.invoke.MethodHandles;
  41 import java.lang.invoke.MethodType;
  42 import java.lang.invoke.WrongMethodTypeException;
  43 import java.math.BigDecimal;
  44 import java.util.ArrayList;
  45 import java.util.List;
  46 import java.util.Map;
  47 
  48 import static java.lang.invoke.MethodType.methodType;
  49 
  50 public class CondyWrongType {
  51 
  52     @DataProvider
  53     public Object[][] primitivesProvider() throws Exception {
  54         Map<String, Class<?>> typeMap = Map.of(
  55                 "B", byte.class,
  56                 "C", char.class,
  57                 "D", double.class,
  58                 "F", float.class,
  59                 "I", int.class,
  60                 "J", long.class,
  61                 "S", short.class,
  62                 "Z", boolean.class
  63                 );
  64 
  65         List<Object[]> cases = new ArrayList<>();
  66         for (String name : typeMap.keySet()) {
  67             MethodHandle zero = MethodHandles.zero(typeMap.get(name));
  68             for (String type : typeMap.keySet()) {
  69                 // Use asType transformation to detect if primitive conversion
  70                 // is supported from the BSM value type to the dynamic constant type
  71                 boolean pass = true;
  72                 try {
  73                     zero.asType(MethodType.methodType(typeMap.get(type)));
  74                 }
  75                 catch (WrongMethodTypeException e) {
  76                     pass = false;
  77                 }
  78                 cases.add(new Object[] { name, type, pass});
  79             }
  80         }
  81 
  82         return cases.stream().toArray(Object[][]::new);
  83     }
  84 
  85     @Test(dataProvider = "primitivesProvider")
  86     public void testPrimitives(String name, String type, boolean pass) {
  87         test(name, type, pass);
  88     }
  89 
  90     @Test
  91     public void testReferences() {
  92         test("String", "Ljava/math/BigDecimal;", false);
  93         test("BigDecimal", "Ljava/lang/String;", false);
  94     }
  95 
  96     @Test
  97     public void testReferenceAndPrimitives() {
  98         test("String", "B", false);
  99         test("String", "C", false);
 100         test("String", "D", false);
 101         test("String", "F", false);
 102         test("String", "I", false);
 103         test("String", "J", false);
 104         test("String", "S", false);
 105         test("String", "Z", false);
 106     }
 107 
 108     static void test(String name, String type, boolean pass) {
 109         MethodHandle mh = caster(name, type);
 110         Throwable caught = null;
 111         try {
 112             mh.invoke();
 113         }
 114         catch (Throwable t) {
 115             caught = t;
 116         }
 117 
 118         if (caught == null) {
 119             if (pass) {
 120                 return;
 121             }
 122             else {
 123                 Assert.fail("Throwable expected");
 124             }
 125         }
 126         else if (pass) {
 127             Assert.fail("Throwable not expected");
 128         }
 129 
 130         Assert.assertTrue(BootstrapMethodError.class.isAssignableFrom(caught.getClass()));
 131         caught = caught.getCause();
 132         Assert.assertNotNull(caught);
 133         Assert.assertTrue(ClassCastException.class.isAssignableFrom(caught.getClass()));
 134     }
 135 
 136     static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
 137         switch (name) {
 138             case "B":
 139                 return (byte) 1;
 140             case "C":
 141                 return 'A';
 142             case "D":
 143                 return 1.0;
 144             case "F":
 145                 return 1.0f;
 146             case "I":
 147                 return 1;
 148             case "J":
 149                 return 1L;
 150             case "S":
 151                 return (short) 1;
 152             case "Z":
 153                 return true;
 154             case "String":
 155                 return "string";
 156             case "BigDecimal":
 157                 return BigDecimal.ONE;
 158             default:
 159                 throw new UnsupportedOperationException();
 160         }
 161     }
 162 
 163     static MethodHandle caster(String name, String type) {
 164         try {
 165             return InstructionHelper.ldcDynamicConstant(
 166                     MethodHandles.lookup(),
 167                     name, type,
 168                     "bsm", methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class).toMethodDescriptorString(),
 169                     S -> { });
 170         } catch (Exception e) {
 171             throw new Error(e);
 172         }
 173     }
 174 }