1 /*
  2  * Copyright (c) 2023, 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  * @modules java.base/jdk.internal.javac
 27  * @modules java.base/jdk.internal.reflect
 28  * @run testng TestRestricted
 29  */
 30 
 31 import jdk.internal.javac.Restricted;
 32 import jdk.internal.reflect.CallerSensitive;
 33 import org.testng.annotations.Test;
 34 
 35 import java.io.IOException;
 36 import java.io.UncheckedIOException;
 37 import java.lang.foreign.AddressLayout;
 38 import java.lang.foreign.Arena;
 39 import java.lang.foreign.FunctionDescriptor;
 40 import java.lang.foreign.Linker;
 41 import java.lang.foreign.MemoryLayout;
 42 import java.lang.foreign.MemorySegment;
 43 import java.lang.foreign.SymbolLookup;
 44 import java.lang.invoke.MethodHandle;
 45 import java.lang.invoke.MethodType;
 46 import java.lang.module.ModuleReader;
 47 import java.lang.module.ModuleReference;
 48 import java.lang.reflect.Method;
 49 import java.lang.reflect.Modifier;
 50 import java.nio.file.Path;
 51 import java.util.Arrays;
 52 import java.util.HashSet;
 53 import java.util.List;
 54 import java.util.Set;
 55 import java.util.StringJoiner;
 56 import java.util.function.Consumer;
 57 import java.util.stream.Collectors;
 58 import java.util.stream.Stream;
 59 
 60 import static org.testng.Assert.assertTrue;
 61 import static org.testng.Assert.fail;
 62 
 63 /**
 64  * This test checks all methods in java.base to make sure that methods annotated with {@link Restricted} are
 65  * expected restricted methods. Conversely, the test also checks that there is no expected restricted method
 66  * that is not marked with the annotation. For each restricted method, we also check that they are
 67  * marked with the {@link CallerSensitive} annotation.
 68  */
 69 public class TestRestricted {
 70 
 71     record RestrictedMethod(Class<?> owner, String name, MethodType type) {
 72         static RestrictedMethod from(Method method) {
 73             return of(method.getDeclaringClass(), method.getName(), method.getReturnType(), method.getParameterTypes());
 74         }
 75 
 76         static RestrictedMethod of(Class<?> owner, String name, Class<?> returnType, Class<?>... paramTypes) {
 77             return new RestrictedMethod(owner, name, MethodType.methodType(returnType, paramTypes));
 78         }
 79     };
 80 
 81     static final Set<RestrictedMethod> RESTRICTED_METHODS = Set.of(
 82             RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, String.class, Arena.class),
 83             RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, Path.class, Arena.class),
 84             RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, FunctionDescriptor.class, Linker.Option[].class),
 85             RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, MemorySegment.class, FunctionDescriptor.class, Linker.Option[].class),
 86             RestrictedMethod.of(Linker.class, "upcallStub", MemorySegment.class, MethodHandle.class, FunctionDescriptor.class, Arena.class, Linker.Option[].class),
 87             RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class),
 88             RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, Arena.class, Consumer.class),
 89             RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class, Arena.class, Consumer.class),
 90             RestrictedMethod.of(AddressLayout.class, "withTargetLayout", AddressLayout.class, MemoryLayout.class),
 91             RestrictedMethod.of(ModuleLayer.Controller.class, "enableNativeAccess", ModuleLayer.Controller.class, Module.class),
 92             RestrictedMethod.of(System.class, "load", void.class, String.class),
 93             RestrictedMethod.of(System.class, "loadLibrary", void.class, String.class),
 94             RestrictedMethod.of(Runtime.class, "load", void.class, String.class),
 95             RestrictedMethod.of(Runtime.class, "loadLibrary", void.class, String.class),
 96             // custom scheduler prototype 1
 97             RestrictedMethod.of(Thread.Builder.OfVirtual.class, "unstarted", Thread.class, Runnable.class, Thread.class, Object.class),
 98             // custom scheduler prototype 2
 99             RestrictedMethod.of(Thread.Builder.OfVirtual.class, "scheduler", Thread.Builder.OfVirtual.class, Thread.VirtualThreadScheduler.class),
100             RestrictedMethod.of(Thread.VirtualThreadScheduler.class, "current", Thread.VirtualThreadScheduler.class)
101     );
102 
103     @Test
104     public void testRestricted() {
105         Set<RestrictedMethod> restrictedMethods = new HashSet<>(RESTRICTED_METHODS);
106         restrictedMethods(Object.class.getModule()).forEach(m -> checkRestrictedMethod(m, restrictedMethods));
107         if (!restrictedMethods.isEmpty()) {
108             fail("@Restricted annotation not found for methods: " + restrictedMethods);
109         }
110     }
111 
112     void checkRestrictedMethod(Method meth, Set<RestrictedMethod> restrictedMethods) {
113         String sig = meth.getDeclaringClass().getName() + "::" + shortSig(meth);
114         boolean expectRestricted = restrictedMethods.remove(RestrictedMethod.from(meth));
115         assertTrue(expectRestricted, "unexpected @Restricted annotation found on method " + sig);
116         assertTrue(meth.isAnnotationPresent(CallerSensitive.class), "@CallerSensitive annotation not found on restricted method " + sig);
117     }
118 
119     /**
120      * Returns a stream of all restricted methods on public classes in packages
121      * exported by a named module. This logic is borrowed from CallerSensitiveTest.
122      */
123     static Stream<Method> restrictedMethods(Module module) {
124         assert module.isNamed();
125         ModuleReference mref = module.getLayer().configuration()
126                 .findModule(module.getName())
127                 .orElseThrow(() -> new RuntimeException())
128                 .reference();
129         // find all ".class" resources in the module
130         // transform the resource name to a class name
131         // load every class in the exported packages
132         // return the restricted methods of the public classes
133         try (ModuleReader reader = mref.open()) {
134             return reader.list()
135                     .filter(rn -> rn.endsWith(".class"))
136                     .map(rn -> rn.substring(0, rn.length() - 6)
137                             .replace('/', '.'))
138                     .filter(cn -> module.isExported(packageName(cn)))
139                     .map(cn -> Class.forName(module, cn))
140                     .filter(refc -> refc != null
141                             && Modifier.isPublic(refc.getModifiers()))
142                     .map(refc -> restrictedMethods(refc))
143                     .flatMap(List::stream);
144         } catch (IOException ioe) {
145             throw new UncheckedIOException(ioe);
146         }
147     }
148 
149     static String packageName(String cn) {
150         int last = cn.lastIndexOf('.');
151         if (last > 0) {
152             return cn.substring(0, last);
153         } else {
154             return "";
155         }
156     }
157 
158     static String shortSig(Method m) {
159         StringJoiner sj = new StringJoiner(",", m.getName() + "(", ")");
160         for (Class<?> parameterType : m.getParameterTypes()) {
161             sj.add(parameterType.getTypeName());
162         }
163         return sj.toString();
164     }
165 
166     /**
167      * Returns a list of restricted methods directly declared by the given
168      * class.
169      */
170     static List<Method> restrictedMethods(Class<?> refc) {
171         return Arrays.stream(refc.getDeclaredMethods())
172                 .filter(m -> m.isAnnotationPresent(Restricted.class))
173                 .collect(Collectors.toList());
174     }
175 }