Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.
/ jdk15u-dev Public archive

Commit 0903e10

Browse files
author
Vladimir Kempik
committed
8261395: C1 crash "cannot make java calls from the native compiler"
Reviewed-by: yan Backport-of: e828a939a8155a3b4ab26811a405bb4e4b2b99e8
1 parent 717506c commit 0903e10

File tree

4 files changed

+187
-60
lines changed

4 files changed

+187
-60
lines changed

src/hotspot/share/oops/instanceKlass.cpp

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -158,60 +158,31 @@ static inline bool is_class_loader(const Symbol* class_name,
158158
// private: called to verify that k is a static member of this nest.
159159
// We know that k is an instance class in the same package and hence the
160160
// same classloader.
161-
bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
161+
bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
162162
assert(!is_hidden(), "unexpected hidden class");
163163
if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
164164
if (log_is_enabled(Trace, class, nestmates)) {
165-
ResourceMark rm(THREAD);
165+
ResourceMark rm(current);
166166
log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
167167
k->external_name(), this->external_name());
168168
}
169169
return false;
170170
}
171171

172172
if (log_is_enabled(Trace, class, nestmates)) {
173-
ResourceMark rm(THREAD);
173+
ResourceMark rm(current);
174174
log_trace(class, nestmates)("Checking nest membership of %s in %s",
175175
k->external_name(), this->external_name());
176176
}
177177

178-
// Check for a resolved cp entry , else fall back to a name check.
179-
// We don't want to resolve any class other than the one being checked.
178+
// Check for the named class in _nest_members.
179+
// We don't resolve, or load, any classes.
180180
for (int i = 0; i < _nest_members->length(); i++) {
181181
int cp_index = _nest_members->at(i);
182-
if (_constants->tag_at(cp_index).is_klass()) {
183-
Klass* k2 = _constants->klass_at(cp_index, THREAD);
184-
assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
185-
"Exceptions should not be possible here");
186-
if (k2 == k) {
187-
log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
188-
return true;
189-
}
190-
}
191-
else {
192-
Symbol* name = _constants->klass_name_at(cp_index);
193-
if (name == k->name()) {
194-
log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
195-
196-
// Names match so check actual klass. This may trigger class loading if
197-
// it doesn't match though that should be impossible as it means one classloader
198-
// has defined two different classes with the same name! A compiler thread won't be
199-
// able to perform that loading but we can't exclude the compiler threads from
200-
// executing this logic. But it should actually be impossible to trigger loading here.
201-
Klass* k2 = _constants->klass_at(cp_index, THREAD);
202-
assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
203-
"Exceptions should not be possible here");
204-
if (k2 == k) {
205-
log_trace(class, nestmates)("- class is listed as a nest member");
206-
return true;
207-
}
208-
else {
209-
// same name but different klass!
210-
log_trace(class, nestmates)(" - klass comparison failed!");
211-
// can't have two names the same, so we're done
212-
return false;
213-
}
214-
}
182+
Symbol* name = _constants->klass_name_at(cp_index);
183+
if (name == k->name()) {
184+
log_trace(class, nestmates)("- named class found at nest_members[%d] => cp[%d]", i, cp_index);
185+
return true;
215186
}
216187
}
217188
log_trace(class, nestmates)("- class is NOT a nest member!");
@@ -293,7 +264,8 @@ InstanceKlass* InstanceKlass::nest_host(TRAPS) {
293264
// need to resolve and save our nest-host class.
294265
if (_nest_host_index != 0) { // we have a real nest_host
295266
// Before trying to resolve check if we're in a suitable context
296-
if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
267+
bool can_resolve = THREAD->can_call_java();
268+
if (!can_resolve && !_constants->tag_at(_nest_host_index).is_klass()) {
297269
log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
298270
this->external_name());
299271
return NULL; // sentinel to say "try again from a different context"
@@ -331,26 +303,15 @@ InstanceKlass* InstanceKlass::nest_host(TRAPS) {
331303
// not an instance class.
332304
if (k->is_instance_klass()) {
333305
nest_host_k = InstanceKlass::cast(k);
334-
bool is_member = nest_host_k->has_nest_member(this, THREAD);
335-
// exception is rare, perhaps impossible
336-
if (!HAS_PENDING_EXCEPTION) {
337-
if (is_member) {
338-
_nest_host = nest_host_k; // save resolved nest-host value
339-
340-
log_trace(class, nestmates)("Resolved nest-host of %s to %s",
341-
this->external_name(), k->external_name());
342-
return nest_host_k;
343-
} else {
344-
error = "current type is not listed as a nest member";
345-
}
306+
bool is_member = nest_host_k->has_nest_member((JavaThread*) THREAD, this);
307+
if (is_member) {
308+
_nest_host = nest_host_k; // save resolved nest-host value
309+
310+
log_trace(class, nestmates)("Resolved nest-host of %s to %s",
311+
this->external_name(), k->external_name());
312+
return nest_host_k;
346313
} else {
347-
if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
348-
return NULL; // propagate VMEs
349-
}
350-
stringStream ss;
351-
ss.print("exception on member check: ");
352-
java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
353-
error = ss.as_string();
314+
error = "current type is not listed as a nest member";
354315
}
355316
} else {
356317
error = "host is not an instance class";

src/hotspot/share/oops/instanceKlass.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,9 @@ class InstanceKlass: public Klass {
480480
void set_permitted_subclasses(Array<u2>* s) { _permitted_subclasses = s; }
481481

482482
private:
483-
// Called to verify that k is a member of this nest - does not look at k's nest-host
484-
bool has_nest_member(InstanceKlass* k, TRAPS) const;
483+
// Called to verify that k is a member of this nest - does not look at k's nest-host,
484+
// nor does it resolve any CP entries or load any classes.
485+
bool has_nest_member(JavaThread* current, InstanceKlass* k) const;
485486

486487
public:
487488
// Used to construct informative IllegalAccessError messages at a higher level,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2021, 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+
// Host and Host$Member will be loaded by a custom loader with different
25+
// protection domains.
26+
27+
public class Host {
28+
29+
private static int forNestmatesOnly = 1;
30+
31+
public static class Member {
32+
// We need our static initializer to ensure our CP reference
33+
// to Host is resolved by the main thread.
34+
static final Class<?> hostClass = Host.class;
35+
36+
int id;
37+
38+
// Executing, or JIT compiling, this method will result in
39+
// a nestmate access check.
40+
public Member() {
41+
id = forNestmatesOnly++;
42+
}
43+
}
44+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2021, 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 8261395
27+
* @summary Test the code paths when a nest-host and nest-member class are in
28+
* different protection domains and the compiler thread needs to
29+
* perform a nestmate access check.
30+
* @comment We use WB to force-compile a constructor to recreate the original
31+
* failure scenario, so only run when we have "normal" compiler flags.
32+
* @requires vm.compMode=="Xmixed" &
33+
* (vm.opt.TieredStopAtLevel == null | vm.opt.TieredStopAtLevel == 4)
34+
* @library /test/lib /
35+
* @build sun.hotspot.WhiteBox
36+
* @run driver ClassFileInstaller sun.hotspot.WhiteBox
37+
* @compile Host.java
38+
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
39+
* -Xlog:class+nestmates=trace,protectiondomain=trace
40+
* -Djava.security.manager=allow
41+
* TestDifferentProtectionDomains
42+
*/
43+
44+
import java.lang.reflect.Constructor;
45+
import java.io.IOException;
46+
import java.nio.file.Files;
47+
import java.nio.file.Paths;
48+
import java.nio.file.Path;
49+
import java.security.ProtectionDomain;
50+
51+
import compiler.whitebox.CompilerWhiteBoxTest;
52+
import sun.hotspot.WhiteBox;
53+
54+
public class TestDifferentProtectionDomains {
55+
56+
static final String TARGET = "Host";
57+
58+
// We need a custom classloader so that we can
59+
// use a different protection domain for our target classes.
60+
61+
static class CustomLoader extends ClassLoader {
62+
63+
CustomLoader(ClassLoader parent) {
64+
super(parent);
65+
}
66+
67+
@Override
68+
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
69+
synchronized (getClassLoadingLock(name)) {
70+
// First, check if the class has already been loaded
71+
Class<?> clz = findLoadedClass(name);
72+
if (clz != null) {
73+
return clz;
74+
}
75+
76+
// Check for target class
77+
if (name.startsWith(TARGET)) {
78+
try {
79+
String clzFile = name.replaceAll("\\.", "/") + ".class";
80+
byte[] buff = getResourceAsStream(clzFile).readAllBytes();
81+
ProtectionDomain differentPD = new ProtectionDomain(null, null);
82+
return defineClass(name, buff, 0, buff.length, differentPD);
83+
} catch (Throwable t) {
84+
throw new RuntimeException("Unexpected", t);
85+
}
86+
}
87+
}
88+
return super.loadClass(name, resolve);
89+
}
90+
}
91+
92+
public static void main(String[] args) throws Throwable {
93+
94+
CustomLoader cl = new CustomLoader(TestDifferentProtectionDomains.class.getClassLoader());
95+
Class<?> host = cl.loadClass("Host");
96+
Class<?> member = cl.loadClass("Host$Member");
97+
98+
if (host.getProtectionDomain() == member.getProtectionDomain()) {
99+
throw new Error("ProtectionDomain instances were not different!");
100+
}
101+
102+
Constructor cons = member.getDeclaredConstructor(new Class<?>[] {});
103+
WhiteBox wb = WhiteBox.getWhiteBox();
104+
105+
// The code path for the original failure is now only followed when
106+
// there is a security manager set, so we set one. We do this here
107+
// as any earlier causes security exceptions running the test and we
108+
// don't want to have to set up a policy file etc.
109+
System.setSecurityManager(new SecurityManager());
110+
111+
// Force the constructor to compile, which then triggers the nestmate
112+
// access check in the compiler thread, which leads to the original bug.
113+
wb.enqueueMethodForCompilation(cons, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
114+
while (!wb.isMethodCompiled(cons)) {
115+
Thread.sleep(100);
116+
}
117+
118+
// Just for good measure call the compiled constructor.
119+
Object m = member.newInstance();
120+
}
121+
}

0 commit comments

Comments
 (0)