rev 58768 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: alanb, cjplummer, coleenp, dholmes, dlong, forax, jlahoda, psandoz, plevart, vromero
Contributed-by: mandy.chung@oracle.com, lois.foltan@oracle.com, david.holmes@oracle.com, harold.seigel@oracle.com, serguei.spitsyn@oracle.com, alex.buckley@oracle.com, jamsheed.c.m@oracle.com, jan.lahoda@oracle.com, amy.lu@oracle.com
rev 58769 : [mq]: type-descriptor-name

   1 /*
   2  * Copyright (c) 2019, 2020, 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.org.objectweb.asm
  27  *          jdk.compiler
  28  * @library /test/lib
  29  * @build jdk.test.lib.Utils
  30  *        jdk.test.lib.compiler.CompilerUtils
  31  * @run testng/othervm --enable-preview BasicTest
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.lang.invoke.MethodHandles.Lookup;
  37 
  38 import static java.lang.invoke.MethodHandles.lookup;
  39 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
  40 
  41 import java.lang.reflect.Array;
  42 import java.lang.reflect.Method;
  43 import java.nio.charset.StandardCharsets;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.util.Arrays;
  48 import java.util.List;
  49 import java.util.stream.Stream;
  50 
  51 import jdk.internal.org.objectweb.asm.ClassWriter;
  52 import jdk.internal.org.objectweb.asm.Type;
  53 import jdk.test.lib.compiler.CompilerUtils;
  54 import jdk.test.lib.Utils;
  55 
  56 import org.testng.annotations.BeforeTest;
  57 import org.testng.annotations.DataProvider;
  58 import org.testng.annotations.Test;
  59 
  60 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  61 import static org.testng.Assert.*;
  62 
  63 interface HiddenTest {
  64     void test();
  65 }
  66 
  67 public class BasicTest {
  68 
  69     private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "src");
  70     private static final Path CLASSES_DIR = Paths.get("classes");
  71     private static final Path CLASSES_10_DIR = Paths.get("classes_10");
  72 
  73     private static byte[] hiddenClassBytes;
  74 
  75     @BeforeTest
  76     static void setup() throws IOException {
  77         compileSources(SRC_DIR, CLASSES_DIR,
  78                 "--enable-preview", "-source", String.valueOf(Runtime.version().feature()));
  79         hiddenClassBytes = Files.readAllBytes(CLASSES_DIR.resolve("HiddenClass.class"));
  80 
  81         // compile with --release 10 with no NestHost and NestMembers attribute
  82         compileSources(SRC_DIR.resolve("Outer.java"), CLASSES_10_DIR, "--release", "10");
  83         compileSources(SRC_DIR.resolve("EnclosingClass.java"), CLASSES_10_DIR, "--release", "10");
  84     }
  85 
  86     static void compileSources(Path sourceFile, Path dest, String... options) throws IOException {
  87         Stream<String> ops = Stream.of("-cp", Utils.TEST_CLASSES + File.pathSeparator + CLASSES_DIR);
  88         if (options != null && options.length > 0) {
  89             ops = Stream.concat(ops, Arrays.stream(options));
  90         }
  91         if (!CompilerUtils.compile(sourceFile, dest, ops.toArray(String[]::new))) {
  92             throw new RuntimeException("Compilation of the test failed: " + sourceFile);
  93         }
  94     }
  95 
  96     static Class<?> defineHiddenClass(String name) throws Exception {
  97         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve(name + ".class"));
  98         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
  99         assertHiddenClass(hc);
 100         singletonNest(hc);
 101         return hc;
 102     }
 103 
 104     // basic test on a hidden class
 105     @Test
 106     public void hiddenClass() throws Throwable {
 107         HiddenTest t = (HiddenTest)defineHiddenClass("HiddenClass").newInstance();
 108         t.test();
 109 
 110         // sanity check
 111         Class<?> c = t.getClass();
 112         Class<?>[] intfs = c.getInterfaces();
 113         assertTrue(c.isHidden());
 114         assertFalse(c.isPrimitive());
 115         assertTrue(intfs.length == 1);
 116         assertTrue(intfs[0] == HiddenTest.class);
 117         assertTrue(c.getCanonicalName() == null);
 118         assertTrue(c.getName().startsWith("HiddenClass/"));




 119 
 120         // test array of hidden class
 121         testHiddenArray(c);
 122 
 123         // test setAccessible
 124         checkSetAccessible(c, "realTest");
 125         checkSetAccessible(c, "test");
 126     }
 127 
 128     // primitive class is not a hidden class
 129     @Test
 130     public void primitiveClass() {
 131         assertFalse(int.class.isHidden());
 132         assertFalse(String.class.isHidden());
 133     }
 134 
 135     private void testHiddenArray(Class<?> type) throws Exception {
 136         // array of hidden class
 137         Object array = Array.newInstance(type, 2);
 138         Class<?> arrayType = array.getClass();
 139         assertTrue(arrayType.isArray());
 140         assertTrue(Array.getLength(array) == 2);
 141         assertFalse(arrayType.isHidden());
 142         assertTrue(arrayType.getName().startsWith("[LHiddenClass/"), "unexpected name: " + arrayType.getName());




 143 
 144         assertTrue(arrayType.getComponentType().isHidden());
 145         assertTrue(arrayType.getComponentType() == type);
 146         Object t = type.newInstance();
 147         Array.set(array, 0, t);
 148         Object o = Array.get(array, 0);
 149         assertTrue(o == t);
 150     }
 151 
 152     private void checkSetAccessible(Class<?> c, String name, Class<?>... ptypes) throws Exception {
 153         Method m = c.getDeclaredMethod(name, ptypes);
 154         assertTrue(m.trySetAccessible());
 155         m.setAccessible(true);
 156     }
 157 
 158     // Define a hidden class that uses lambda
 159     // This verifies LambdaMetaFactory supports the caller which is a hidden class
 160     @Test
 161     public void testLambda() throws Throwable {
 162         HiddenTest t = (HiddenTest)defineHiddenClass("Lambda").newInstance();
 163         try {
 164             t.test();
 165         } catch (Error e) {
 166             if (!e.getMessage().equals("thrown by " + t.getClass().getName())) {
 167                 throw e;
 168             }
 169         }
 170     }
 171 
 172     // Verify the nest host and nest members of a hidden class and hidden nestmate class
 173     @Test
 174     public void testHiddenNestHost() throws Throwable {
 175         byte[] hc1 = hiddenClassBytes;
 176         Lookup lookup1 = lookup().defineHiddenClass(hc1, false);
 177         Class<?> host = lookup1.lookupClass();
 178 
 179         byte[] hc2 = Files.readAllBytes(CLASSES_DIR.resolve("Lambda.class"));
 180         Lookup lookup2 = lookup1.defineHiddenClass(hc2, false, NESTMATE);
 181         Class<?> member = lookup2.lookupClass();
 182 
 183         // test nest membership and reflection API
 184         assertTrue(host.isNestmateOf(member));
 185         assertTrue(host.getNestHost() == host);
 186         // getNestHost and getNestMembers return the same value when calling
 187         // on a nest member and the nest host
 188         assertTrue(member.getNestHost() == host.getNestHost());
 189         assertTrue(Arrays.equals(member.getNestMembers(), host.getNestMembers()));
 190         // getNestMembers includes the nest host that can be a hidden class but
 191         // only includes static nest members
 192         assertTrue(host.getNestMembers().length == 1);
 193         assertTrue(host.getNestMembers()[0] == host);
 194     }
 195 
 196     @DataProvider(name = "hiddenClasses")
 197     private Object[][] hiddenClasses() {
 198         return new Object[][] {
 199                 new Object[] { "HiddenInterface", false },
 200                 new Object[] { "AbstractClass", false },
 201                 // a hidden annotation is useless because it cannot be referenced by any class
 202                 new Object[] { "HiddenAnnotation", false },
 203                 // class file with bad NestHost, NestMembers and InnerClasses or EnclosingMethod attribute
 204                 // define them as nestmate to verify Class::getNestHost and getNestMembers
 205                 new Object[] { "Outer", true },
 206                 new Object[] { "Outer$Inner", true },
 207                 new Object[] { "EnclosingClass", true },
 208                 new Object[] { "EnclosingClass$1", true },
 209         };
 210     }
 211 
 212     /*
 213      * Test that class file bytes that can be defined as a normal class
 214      * can be successfully created as a hidden class even it might not
 215      * make sense as a hidden class.  For example, a hidden annotation
 216      * is not useful as it cannot be referenced and an outer/inner class
 217      * when defined as a hidden effectively becomes a final top-level class.
 218      */
 219     @Test(dataProvider = "hiddenClasses")
 220     public void defineHiddenClass(String name, boolean nestmate) throws Exception {
 221         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve(name + ".class"));
 222         Class<?> hc;
 223         Class<?> host;
 224         if (nestmate) {
 225             hc = lookup().defineHiddenClass(bytes, false, NESTMATE).lookupClass();
 226             host = lookup().lookupClass().getNestHost();
 227         } else {
 228             hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 229             host = hc;
 230         }
 231         assertTrue(hc.getNestHost() == host);
 232         assertTrue(hc.getNestMembers().length == 1);
 233         assertTrue(hc.getNestMembers()[0] == host);
 234     }
 235 
 236     @DataProvider(name = "emptyClasses")
 237     private Object[][] emptyClasses() {
 238         return new Object[][] {
 239                 new Object[] { "EmptyHiddenSynthetic", ACC_SYNTHETIC },
 240                 new Object[] { "EmptyHiddenEnum", ACC_ENUM },
 241                 new Object[] { "EmptyHiddenAbstractClass", ACC_ABSTRACT },
 242                 new Object[] { "EmptyHiddenInterface", ACC_ABSTRACT|ACC_INTERFACE },
 243                 new Object[] { "EmptyHiddenAnnotation", ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE },
 244         };
 245     }
 246 
 247     /*
 248      * Test if an empty class with valid access flags can be created as a hidden class
 249      * as long as it does not violate the restriction of a hidden class.
 250      *
 251      * A meaningful enum type defines constants of that enum type.  So
 252      * enum class containing constants of its type should not be a hidden
 253      * class.
 254      */
 255     @Test(dataProvider = "emptyClasses")
 256     public void emptyHiddenClass(String name, int accessFlags) throws Exception {
 257         byte[] bytes = (accessFlags == ACC_ENUM) ? classBytes(name, Enum.class, accessFlags)
 258                                                  : classBytes(name, accessFlags);
 259         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 260         switch (accessFlags) {
 261             case ACC_SYNTHETIC:
 262                 assertTrue(hc.isSynthetic());
 263                 assertFalse(hc.isEnum());
 264                 assertFalse(hc.isAnnotation());
 265                 assertFalse(hc.isInterface());
 266                 break;
 267             case ACC_ENUM:
 268                 assertFalse(hc.isSynthetic());
 269                 assertTrue(hc.isEnum());
 270                 assertFalse(hc.isAnnotation());
 271                 assertFalse(hc.isInterface());
 272                 break;
 273             case ACC_ABSTRACT:
 274                 assertFalse(hc.isSynthetic());
 275                 assertFalse(hc.isEnum());
 276                 assertFalse(hc.isAnnotation());
 277                 assertFalse(hc.isInterface());
 278                 break;
 279             case ACC_ABSTRACT|ACC_INTERFACE:
 280                 assertFalse(hc.isSynthetic());
 281                 assertFalse(hc.isEnum());
 282                 assertFalse(hc.isAnnotation());
 283                 assertTrue(hc.isInterface());
 284                 break;
 285             case ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE:
 286                 assertFalse(hc.isSynthetic());
 287                 assertFalse(hc.isEnum());
 288                 assertTrue(hc.isAnnotation());
 289                 assertTrue(hc.isInterface());
 290                 break;
 291             default:
 292                 throw new IllegalArgumentException("unexpected access flag: " + accessFlags);
 293         }
 294         assertTrue(hc.isHidden());
 295         assertTrue(hc.getModifiers() == (ACC_PUBLIC|accessFlags));
 296         assertFalse(hc.isLocalClass());
 297         assertFalse(hc.isMemberClass());
 298         assertFalse(hc.isAnonymousClass());
 299         assertFalse(hc.isArray());
 300     }
 301 
 302     // These class files can't be defined as hidden classes
 303     @DataProvider(name = "cantBeHiddenClasses")
 304     private Object[][] cantBeHiddenClasses() {
 305         return new Object[][] {
 306                 // a hidden class can't be a field's declaring type
 307                 // enum class with static final HiddenEnum[] $VALUES:
 308                 new Object[] { "HiddenEnum" },
 309                 // supertype of this class is a hidden class
 310                 new Object[] { "HiddenSuper" },
 311                 // a record class whose equals(HiddenRecord, Object) method
 312                 // refers to a hidden class in the parameter type and fails
 313                 // verification.  Perhaps this method signature should be reconsidered. 
 314                 new Object[] { "HiddenRecord" },
 315         };
 316     }
 317 
 318     /*
 319      * These class files
 320      */
 321     @Test(dataProvider = "cantBeHiddenClasses", expectedExceptions = NoClassDefFoundError.class)
 322     public void failToDeriveAsHiddenClass(String name) throws Exception {
 323         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve(name + ".class"));
 324         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 325     }
 326 
 327     /*
 328      * A hidden class can be successfully created but fails to be reflected
 329      * if it refers to its own type in the descriptor.
 330      * e.g. Class::getMethods resolves the declaring type of fields,
 331      * parameter types and return type.
 332      */
 333     @Test
 334     public void hiddenCantReflect() throws Throwable {
 335         HiddenTest t = (HiddenTest)defineHiddenClass("HiddenCantReflect").newInstance();
 336         t.test();
 337 
 338         Class<?> c = t.getClass();
 339         Class<?>[] intfs = c.getInterfaces();
 340         assertTrue(intfs.length == 1);
 341         assertTrue(intfs[0] == HiddenTest.class);
 342 
 343         try {
 344             // this would cause loading of class HiddenCantReflect and NCDFE due
 345             // to error during verification
 346             c.getDeclaredMethods();
 347         } catch (NoClassDefFoundError e) {
 348             Throwable x = e.getCause();
 349             if (x == null || !(x instanceof ClassNotFoundException && x.getMessage().contains("HiddenCantReflect"))) {
 350                 throw e;
 351             }
 352         }
 353     }
 354 
 355     @Test(expectedExceptions = {IllegalArgumentException.class})
 356     public void cantDefineModule() throws Throwable {
 357         Path src = Paths.get("module-info.java");
 358         Path dir = CLASSES_DIR.resolve("m");
 359         Files.write(src, List.of("module m {}"), StandardCharsets.UTF_8);
 360         compileSources(src, dir);
 361 
 362         byte[] bytes = Files.readAllBytes(dir.resolve("module-info.class"));
 363         lookup().defineHiddenClass(bytes, false);
 364     }
 365 
 366     @Test(expectedExceptions = {IllegalArgumentException.class})
 367     public void cantDefineClassInAnotherPackage() throws Throwable {
 368         Path src = Paths.get("ClassInAnotherPackage.java");
 369         Files.write(src, List.of("package p;", "public class ClassInAnotherPackage {}"), StandardCharsets.UTF_8);
 370         compileSources(src, CLASSES_DIR);
 371 
 372         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve("p").resolve("ClassInAnotherPackage.class"));
 373         lookup().defineHiddenClass(bytes, false);
 374     }
 375 
 376     @Test(expectedExceptions = {IllegalAccessException.class})
 377     public void lessPrivilegedLookup() throws Throwable {
 378         Lookup lookup = lookup().dropLookupMode(Lookup.PRIVATE);
 379         lookup.defineHiddenClass(hiddenClassBytes, false);
 380     }
 381 
 382     @DataProvider(name = "nestedTypesOrAnonymousClass")
 383     private Object[][] nestedTypesOrAnonymousClass() {
 384         return new Object[][] {
 385                 // class file with bad InnerClasses or EnclosingMethod attribute
 386                 new Object[] { "Outer", null },
 387                 new Object[] { "Outer$Inner", "Outer" },
 388                 new Object[] { "EnclosingClass", null },
 389                 new Object[] { "EnclosingClass$1", "EnclosingClass" },
 390         };
 391     }
 392 
 393     @Test(dataProvider = "nestedTypesOrAnonymousClass")
 394     public void hasInnerClassesOrEnclosingMethodAttribute(String className, String badDeclaringClassName) throws Throwable {
 395         byte[] bytes = Files.readAllBytes(CLASSES_10_DIR.resolve(className + ".class"));
 396         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 397         hiddenClassWithBadAttribute(hc, badDeclaringClassName);
 398     }
 399 
 400     // define a hidden class with static nest membership
 401     @Test
 402     public void hasStaticNestHost() throws Exception {
 403         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve("Outer$Inner.class"));
 404         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 405         hiddenClassWithBadAttribute(hc, "Outer");
 406     }
 407 
 408     @Test
 409     public void hasStaticNestMembers() throws Throwable {
 410         byte[] bytes = Files.readAllBytes(CLASSES_DIR.resolve("Outer.class"));
 411         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
 412         assertHiddenClass(hc);
 413         assertTrue(hc.getNestHost() == hc);
 414         Class<?>[] members = hc.getNestMembers();
 415         assertTrue(members.length == 1 && members[0] == hc);
 416     }
 417 
 418     // a hidden class with bad InnerClasses or EnclosingMethod attribute
 419     private void hiddenClassWithBadAttribute(Class<?> hc, String badDeclaringClassName) {
 420         assertTrue(hc.isHidden());
 421         assertTrue(hc.getCanonicalName() == null);
 422         assertTrue(hc.getName().contains("/"));
 423 
 424         if (badDeclaringClassName == null) {
 425             // the following reflection API assumes a good name in InnerClasses
 426             // or EnclosingMethod attribute can successfully be resolved.
 427             assertTrue(hc.getSimpleName().length() > 0);
 428             assertFalse(hc.isAnonymousClass());
 429             assertFalse(hc.isLocalClass());
 430             assertFalse(hc.isMemberClass());
 431         } else {
 432             declaringClassNotFound(hc, badDeclaringClassName);
 433         }
 434 
 435         // validation of nest membership
 436         assertTrue(hc.getNestHost() == hc);
 437         // validate the static nest membership
 438         Class<?>[] members = hc.getNestMembers();
 439         assertTrue(members.length == 1 && members[0] == hc);
 440     }
 441 
 442     // Class::getSimpleName, Class::isMemberClass
 443     private void declaringClassNotFound(Class<?> c, String cn) {
 444         try {
 445             // fail to find declaring/enclosing class
 446             c.isMemberClass();
 447             assertTrue(false);
 448         } catch (NoClassDefFoundError e) {
 449             if (!e.getMessage().equals(cn)) {
 450                 throw e;
 451             }
 452         }
 453         try {
 454             // fail to find declaring/enclosing class
 455             c.getSimpleName();
 456             assertTrue(false);
 457         } catch (NoClassDefFoundError e) {
 458             if (!e.getMessage().equals(cn)) {
 459                 throw e;
 460             }
 461         }
 462     }
 463 
 464     private static void singletonNest(Class<?> hc) {
 465         assertTrue(hc.getNestHost() == hc);
 466         assertTrue(hc.getNestMembers().length == 1);
 467         assertTrue(hc.getNestMembers()[0] == hc);
 468     }
 469 
 470     private static void assertHiddenClass(Class<?> hc) {
 471         assertTrue(hc.isHidden());
 472         assertTrue(hc.getCanonicalName() == null);
 473         assertTrue(hc.getName().contains("/"));
 474         assertFalse(hc.isAnonymousClass());
 475         assertFalse(hc.isLocalClass());
 476         assertFalse(hc.isMemberClass());
 477         assertFalse(hc.getSimpleName().isEmpty()); // sanity check
 478     }
 479 
 480     private static byte[] classBytes(String classname, int accessFlags) {
 481         return classBytes(classname, Object.class, accessFlags);
 482     }
 483 
 484     private static byte[] classBytes(String classname, Class<?> supertType, int accessFlags) {
 485         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
 486         cw.visit(V14, ACC_PUBLIC|accessFlags, classname, null, Type.getInternalName(supertType), null);
 487         cw.visitEnd();
 488 
 489         return cw.toByteArray();
 490     }
 491 }
--- EOF ---