diff --git a/cvm/test/hotspot/runtime/classFileParser/SuperclassAccessCheckTest.java b/cvm/test/hotspot/runtime/classFileParser/SuperclassAccessCheckTest.java new file mode 100644 index 00000000000..a347be592f8 --- /dev/null +++ b/cvm/test/hotspot/runtime/classFileParser/SuperclassAccessCheckTest.java @@ -0,0 +1,110 @@ +// This project is a modified version of OpenJDK, licensed under GPL v2. +// Modifications Copyright (C) 2026 ByteDance Inc. +/* + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * @test + * @summary Verify superclass access checks are performed when a class is loaded + * by a different class loader in CVM + * @run main/othervm SuperclassAccessCheckTest + */ + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +public class SuperclassAccessCheckTest { + private static final String SUBCLASS_NAME = SuperclassAccessCheckTest.class.getName() + "$Subclass"; + + // This class is intentionally package-private. Although Subclass has the + // same package name, a copy of it defined by another loader is in a + // different runtime package and must not be allowed to extend Superclass. + static class Superclass { + } + + public static class Subclass extends Superclass { + } + + private static class SplitLoader extends ClassLoader { + SplitLoader(ClassLoader parent) { + super(parent); + } + + @Override + protected Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + if (!name.equals(SUBCLASS_NAME)) { + return super.loadClass(name, resolve); + } + + synchronized (getClassLoadingLock(name)) { + Class loaded = findLoadedClass(name); + if (loaded == null) { + byte[] classBytes = readClassBytes(name); + loaded = defineClass(name, classBytes, 0, classBytes.length); + } + if (resolve) { + resolveClass(loaded); + } + return loaded; + } + } + + private byte[] readClassBytes(String name) throws ClassNotFoundException { + String resourceName = name.replace('.', '/') + ".class"; + InputStream in = getParent().getResourceAsStream(resourceName); + if (in == null) { + throw new ClassNotFoundException(name); + } + + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int count; + while ((count = in.read(buffer)) != -1) { + out.write(buffer, 0, count); + } + return out.toByteArray(); + } catch (IOException ioe) { + throw new ClassNotFoundException(name, ioe); + } finally { + try { + in.close(); + } catch (IOException ignored) { + } + } + } + } + + public static void main(String[] args) throws Exception { + ClassLoader appLoader = SuperclassAccessCheckTest.class.getClassLoader(); + Class.forName(Superclass.class.getName(), true, appLoader); + + ClassLoader splitLoader = new SplitLoader(appLoader); + try { + Class.forName(SUBCLASS_NAME, false, splitLoader); + throw new RuntimeException("Expected IllegalAccessError was not thrown"); + } catch (IllegalAccessError expected) { + String message = expected.getMessage(); + if (message == null || !message.contains("cannot access its superclass")) { + throw new RuntimeException("Unexpected IllegalAccessError message: " + message, + expected); + } + System.out.println("Passed with expected exception: " + expected); + } + } +} diff --git a/src/hotspot/share/classfile/classFileParser.cpp b/src/hotspot/share/classfile/classFileParser.cpp index 2bafa7a7520..2aeeac31964 100644 --- a/src/hotspot/share/classfile/classFileParser.cpp +++ b/src/hotspot/share/classfile/classFileParser.cpp @@ -4368,7 +4368,6 @@ void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, } } -#if !defined(HOTSPOT_TARGET_CLASSLIB) || HOTSPOT_TARGET_CLASSLIB >= 9 Reflection::VerifyClassAccessResults vca_result = Reflection::verify_class_access(this_klass, InstanceKlass::cast(super), false); if (vca_result != Reflection::ACCESS_OK) { @@ -4397,7 +4396,6 @@ void ClassFileParser::check_super_class_access(const InstanceKlass* this_klass, msg); } } -#endif } } diff --git a/src/hotspot/share/classfile/systemDictionary.cpp b/src/hotspot/share/classfile/systemDictionary.cpp index ab9e200745d..8ba207a18cc 100644 --- a/src/hotspot/share/classfile/systemDictionary.cpp +++ b/src/hotspot/share/classfile/systemDictionary.cpp @@ -1489,9 +1489,7 @@ void SystemDictionary::define_instance_class(InstanceKlass* k, Handle class_load Symbol* name_h = k->name(); Dictionary* dictionary = loader_data->dictionary(); unsigned int name_hash = dictionary->compute_hash(name_h); -#if HOTSPOT_TARGET_CLASSLIB == 17 check_constraints(name_hash, k, class_loader, true, CHECK); -#endif // Register class just loaded with class loader (placed in ArrayList) // Note we do this before updating the dictionary, as this can diff --git a/src/hotspot/share/interpreter/linkResolver.cpp b/src/hotspot/share/interpreter/linkResolver.cpp index 2e2259f1ee4..223837ee813 100644 --- a/src/hotspot/share/interpreter/linkResolver.cpp +++ b/src/hotspot/share/interpreter/linkResolver.cpp @@ -278,9 +278,6 @@ void LinkInfo::print() { // Klass resolution void LinkResolver::check_klass_accessibility(Klass* ref_klass, Klass* sel_klass, TRAPS) { -#if defined(HOTSPOT_TARGET_CLASSLIB) && HOTSPOT_TARGET_CLASSLIB == 8 - return; -#endif Klass* base_klass = sel_klass; if (sel_klass->is_objArray_klass()) { base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();