8302795: Shared archive failed on old version class with jsr bytecode

Reviewed-by: minqi, matsaave
This commit is contained in:
Calvin Cheung 2023-03-14 17:15:19 +00:00
parent 4e631fa43f
commit 830fd41346
4 changed files with 158 additions and 1 deletions
src/hotspot/share/oops
test/hotspot/jtreg/runtime/cds/appcds

@ -2412,7 +2412,21 @@ void InstanceKlass::metaspace_pointers_do(MetaspaceClosure* it) {
#if INCLUDE_JVMTI
it->push(&_previous_versions);
#endif
it->push(&_methods);
#if INCLUDE_CDS
// For "old" classes with methods containing the jsr bytecode, the _methods array will
// be rewritten during runtime (see Rewriter::rewrite_jsrs()). So setting the _methods to
// be writable. The length check on the _methods is necessary because classes which
// don't have any methods share the Universe::_the_empty_method_array which is in the RO region.
if (_methods != nullptr && _methods->length() > 0 &&
!can_be_verified_at_dumptime() && methods_contain_jsr_bytecode()) {
// To handle jsr bytecode, new Method* maybe stored into _methods
it->push(&_methods, MetaspaceClosure::_writable);
} else {
#endif
it->push(&_methods);
#if INCLUDE_CDS
}
#endif
it->push(&_default_methods);
it->push(&_local_interfaces);
it->push(&_transitive_interfaces);
@ -2620,6 +2634,21 @@ bool InstanceKlass::can_be_verified_at_dumptime() const {
}
return true;
}
bool InstanceKlass::methods_contain_jsr_bytecode() const {
Thread* thread = Thread::current();
for (int i = 0; i < _methods->length(); i++) {
methodHandle m(thread, _methods->at(i));
BytecodeStream bcs(m);
while (!bcs.is_last_bytecode()) {
Bytecodes::Code opcode = bcs.next();
if (opcode == Bytecodes::_jsr || opcode == Bytecodes::_jsr_w) {
return true;
}
}
}
return false;
}
#endif // INCLUDE_CDS
#if INCLUDE_JVMTI

@ -1145,6 +1145,7 @@ public:
void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
void init_shared_package_entry();
bool can_be_verified_at_dumptime() const;
bool methods_contain_jsr_bytecode() const;
#endif
jint compute_modifier_flags() const;

@ -0,0 +1,70 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
* @test
* @bug 8302795
* @summary CDS support of old classes with major version < JDK_6 (50) for static archive.
* Test old class with jsr bytecode.
* @requires vm.cds
* @library /test/lib
* @compile test-classes/OldClassWithjsrApp.jasm
* @run driver OldClassWithjsr
*/
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.process.OutputAnalyzer;
public class OldClassWithjsr {
public static void main(String[] args) throws Exception {
String mainClass = "OldClassWithjsrApp";
String namePrefix = "oldclasswithjsr";
String appClasses[] = TestCommon.list(mainClass);
JarBuilder.build(namePrefix, appClasses);
String appJar = TestCommon.getTestJar(namePrefix + ".jar");
boolean dynamicMode = CDSTestUtils.DYNAMIC_DUMP;
// create archive with class list
OutputAnalyzer output = TestCommon.dump(appJar, appClasses, "-Xlog:class+load,cds=debug,verification=trace");
TestCommon.checkExecReturn(output, 0,
dynamicMode ? true : false,
"Skipping " + mainClass + ": Old class has been linked");
// run with archive
TestCommon.run(
"-cp", appJar,
"-Xlog:class+load,cds=debug,verification=trace",
mainClass, "1")
.assertNormalExit(out -> {
out.shouldContain("Verifying class " + mainClass + " with old format");
if (!dynamicMode) {
out.shouldContain(mainClass + " source: shared objects file");
} else {
// Old classes were already linked before dynamic dump happened,
// so they couldn't be archived.
out.shouldMatch(".class.load.*" + mainClass + " source:.*" + namePrefix + ".jar");
}
});
}
}

@ -0,0 +1,57 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
super public class OldClassWithjsrApp
version 49:0
{
public Method "<init>":"()V"
stack 1 locals 1
{
// null
aload_0;
invokespecial Method java/lang/Object."<init>":"()V";
return;
}
public static Method main:"([Ljava/lang/String;)V"
stack 6 locals 2
{
aload_0;
iconst_0;
aaload;
invokestatic Method java/lang/Integer.parseInt:"(Ljava/lang/String;)I";
istore_1;
L12: return;
L13: iload_1;
ifeq L12;
jsr L23;
goto L24;
L23: return;
L24: aconst_null;
goto L13;
return;
}
}