1 /*
  2  * Copyright (c) 2021, 2024, 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 8266925
 27  * @summary hidden class members can't be statically invocable
 28  * @modules java.base/jdk.internal.misc
 29  * @build java.base/*
 30  * @run testng StaticInvocableTest
 31  */
 32 
 33 import java.lang.classfile.ClassFile;
 34 import java.lang.constant.ClassDesc;
 35 import java.lang.constant.MethodTypeDesc;
 36 import java.lang.invoke.MethodHandle;
 37 import java.lang.invoke.MethodHandles.Lookup;
 38 import java.lang.invoke.MethodType;
 39 import java.lang.invoke.LookupHelper;
 40 import java.lang.reflect.AccessFlag;
 41 import org.testng.annotations.Test;
 42 
 43 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 44 import static java.lang.classfile.ClassFile.ACC_STATIC;
 45 import static java.lang.constant.ConstantDescs.CD_Object;
 46 import static java.lang.constant.ConstantDescs.CD_int;
 47 import static java.lang.constant.ConstantDescs.INIT_NAME;
 48 import static java.lang.constant.ConstantDescs.MTD_void;
 49 
 50 public class StaticInvocableTest {
 51     public static void main(String[] args) throws Throwable {
 52         StaticInvocableTest test = new StaticInvocableTest();
 53         test.testJavaLang();
 54         test.testJavaUtil();
 55         test.testJdkInternalMisc();
 56         test.testJavaLangInvoke();
 57         test.testProhibitedJavaPkg();
 58         System.out.println("TEST PASSED");
 59     }
 60 
 61     // Test hidden classes from different packages
 62     // (see j.l.i.InvokerBytecodeGenerator::isStaticallyInvocable).
 63     @Test public void testJavaLang()        throws Throwable { test("java/lang");         }
 64     @Test public void testJavaUtil()        throws Throwable { test("java/util");         }
 65     @Test public void testJdkInternalMisc() throws Throwable { test("jdk/internal/misc"); }
 66     @Test public void testJavaLangInvoke()  throws Throwable { test("java/lang/invoke");  }
 67     @Test public void testProhibitedJavaPkg() throws Throwable {
 68        try {
 69            test("java/prohibited");
 70        } catch (IllegalArgumentException e) {
 71            return;
 72        }
 73        throw new RuntimeException("Expected SecurityException");
 74      }
 75 
 76     private static void test(String pkg) throws Throwable {
 77         byte[] bytes = dumpClass(pkg);
 78         Lookup lookup;
 79         if (pkg.equals("java/prohibited")) {
 80             StaticInvocableTest sampleclass = new StaticInvocableTest();
 81             lookup = LookupHelper.newLookup(sampleclass.getClass());
 82         } else if (pkg.equals("java/lang")) {
 83             lookup = LookupHelper.newLookup(Object.class);
 84         } else if (pkg.equals("java/util")) {
 85             lookup = LookupHelper.newLookup(java.util.ArrayList.class);
 86         } else if (pkg.equals("jdk/internal/misc")) {
 87             lookup = LookupHelper.newLookup(jdk.internal.misc.Signal.class);
 88         } else if (pkg.equals("java/lang/invoke")) {
 89             lookup = LookupHelper.newLookup(java.lang.invoke.CallSite.class);
 90         } else {
 91             throw new RuntimeException("Unexpected pkg: " + pkg);
 92         }
 93 
 94         // Define hidden class
 95         Lookup l = lookup.defineHiddenClass(bytes, true);
 96 
 97         MethodType t = MethodType.methodType(Object.class, int.class);
 98         MethodHandle target = l.findStatic(l.lookupClass(), "get", t);
 99 
100         // Wrap target into LF (convert) to get "target" referenced from LF
101         MethodHandle wrappedMH = target.asType(MethodType.methodType(Object.class, Integer.class));
102 
103         // Invoke enough times to provoke LF compilation to bytecode.
104         for (int i = 0; i<100; i++) {
105             Object r = wrappedMH.invokeExact((Integer)1);
106         }
107     }
108 
109     /*
110      * Constructs bytecode for the following class:
111      * public class pkg.MyClass {
112      *     MyClass() {}
113      *     public Object get(int i) { return null; }
114      * }
115      */
116     public static byte[] dumpClass(String pkg) {
117         return ClassFile.of().build(ClassDesc.of(pkg.replace('/', '.'), "MyClass"), clb -> {
118             clb.withSuperclass(CD_Object);
119             clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
120             clb.withMethodBody(INIT_NAME, MTD_void, 0, cob -> {
121                 cob.aload(0);
122                 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
123                 cob.return_();
124             });
125             clb.withMethodBody("get", MethodTypeDesc.of(CD_Object, CD_int),
126                     ACC_PUBLIC | ACC_STATIC, cob -> {
127                 cob.aconst_null();
128                 cob.areturn();
129             });
130         });
131     }
132 }