Skip to content

Commit 830fd41

Browse files
committed
8302795: Shared archive failed on old version class with jsr bytecode
Reviewed-by: minqi, matsaave
1 parent 4e631fa commit 830fd41

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

src/hotspot/share/oops/instanceKlass.cpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -2412,7 +2412,21 @@ void InstanceKlass::metaspace_pointers_do(MetaspaceClosure* it) {
24122412
#if INCLUDE_JVMTI
24132413
it->push(&_previous_versions);
24142414
#endif
2415-
it->push(&_methods);
2415+
#if INCLUDE_CDS
2416+
// For "old" classes with methods containing the jsr bytecode, the _methods array will
2417+
// be rewritten during runtime (see Rewriter::rewrite_jsrs()). So setting the _methods to
2418+
// be writable. The length check on the _methods is necessary because classes which
2419+
// don't have any methods share the Universe::_the_empty_method_array which is in the RO region.
2420+
if (_methods != nullptr && _methods->length() > 0 &&
2421+
!can_be_verified_at_dumptime() && methods_contain_jsr_bytecode()) {
2422+
// To handle jsr bytecode, new Method* maybe stored into _methods
2423+
it->push(&_methods, MetaspaceClosure::_writable);
2424+
} else {
2425+
#endif
2426+
it->push(&_methods);
2427+
#if INCLUDE_CDS
2428+
}
2429+
#endif
24162430
it->push(&_default_methods);
24172431
it->push(&_local_interfaces);
24182432
it->push(&_transitive_interfaces);
@@ -2620,6 +2634,21 @@ bool InstanceKlass::can_be_verified_at_dumptime() const {
26202634
}
26212635
return true;
26222636
}
2637+
2638+
bool InstanceKlass::methods_contain_jsr_bytecode() const {
2639+
Thread* thread = Thread::current();
2640+
for (int i = 0; i < _methods->length(); i++) {
2641+
methodHandle m(thread, _methods->at(i));
2642+
BytecodeStream bcs(m);
2643+
while (!bcs.is_last_bytecode()) {
2644+
Bytecodes::Code opcode = bcs.next();
2645+
if (opcode == Bytecodes::_jsr || opcode == Bytecodes::_jsr_w) {
2646+
return true;
2647+
}
2648+
}
2649+
}
2650+
return false;
2651+
}
26232652
#endif // INCLUDE_CDS
26242653

26252654
#if INCLUDE_JVMTI

src/hotspot/share/oops/instanceKlass.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ class InstanceKlass: public Klass {
11451145
void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
11461146
void init_shared_package_entry();
11471147
bool can_be_verified_at_dumptime() const;
1148+
bool methods_contain_jsr_bytecode() const;
11481149
#endif
11491150

11501151
jint compute_modifier_flags() const;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2023, 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 8302795
27+
* @summary CDS support of old classes with major version < JDK_6 (50) for static archive.
28+
* Test old class with jsr bytecode.
29+
* @requires vm.cds
30+
* @library /test/lib
31+
* @compile test-classes/OldClassWithjsrApp.jasm
32+
* @run driver OldClassWithjsr
33+
*/
34+
35+
import jdk.test.lib.cds.CDSTestUtils;
36+
import jdk.test.lib.process.OutputAnalyzer;
37+
38+
public class OldClassWithjsr {
39+
public static void main(String[] args) throws Exception {
40+
String mainClass = "OldClassWithjsrApp";
41+
String namePrefix = "oldclasswithjsr";
42+
String appClasses[] = TestCommon.list(mainClass);
43+
JarBuilder.build(namePrefix, appClasses);
44+
String appJar = TestCommon.getTestJar(namePrefix + ".jar");
45+
46+
boolean dynamicMode = CDSTestUtils.DYNAMIC_DUMP;
47+
48+
// create archive with class list
49+
OutputAnalyzer output = TestCommon.dump(appJar, appClasses, "-Xlog:class+load,cds=debug,verification=trace");
50+
TestCommon.checkExecReturn(output, 0,
51+
dynamicMode ? true : false,
52+
"Skipping " + mainClass + ": Old class has been linked");
53+
54+
// run with archive
55+
TestCommon.run(
56+
"-cp", appJar,
57+
"-Xlog:class+load,cds=debug,verification=trace",
58+
mainClass, "1")
59+
.assertNormalExit(out -> {
60+
out.shouldContain("Verifying class " + mainClass + " with old format");
61+
if (!dynamicMode) {
62+
out.shouldContain(mainClass + " source: shared objects file");
63+
} else {
64+
// Old classes were already linked before dynamic dump happened,
65+
// so they couldn't be archived.
66+
out.shouldMatch(".class.load.*" + mainClass + " source:.*" + namePrefix + ".jar");
67+
}
68+
});
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2023, 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+
super public class OldClassWithjsrApp
26+
version 49:0
27+
{
28+
29+
30+
public Method "<init>":"()V"
31+
stack 1 locals 1
32+
{
33+
// null
34+
aload_0;
35+
invokespecial Method java/lang/Object."<init>":"()V";
36+
return;
37+
}
38+
39+
public static Method main:"([Ljava/lang/String;)V"
40+
stack 6 locals 2
41+
{
42+
aload_0;
43+
iconst_0;
44+
aaload;
45+
invokestatic Method java/lang/Integer.parseInt:"(Ljava/lang/String;)I";
46+
istore_1;
47+
L12: return;
48+
L13: iload_1;
49+
ifeq L12;
50+
jsr L23;
51+
goto L24;
52+
L23: return;
53+
L24: aconst_null;
54+
goto L13;
55+
return;
56+
}
57+
}

0 commit comments

Comments
 (0)