Skip to content

Commit e828a93

Browse files
David Holmesiklamcoleenp
committed
8261395: C1 crash "cannot make java calls from the native compiler"
Co-authored-by: Ioi Lam <iklam@openjdk.org> Co-authored-by: Coleen Phillimore <coleenp@openjdk.org> Reviewed-by: iklam, hseigel, coleenp
1 parent 3c47cab commit e828a93

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
@@ -163,60 +163,31 @@ static inline bool is_class_loader(const Symbol* class_name,
163163
// private: called to verify that k is a static member of this nest.
164164
// We know that k is an instance class in the same package and hence the
165165
// same classloader.
166-
bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
166+
bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
167167
assert(!is_hidden(), "unexpected hidden class");
168168
if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
169169
if (log_is_enabled(Trace, class, nestmates)) {
170-
ResourceMark rm(THREAD);
170+
ResourceMark rm(current);
171171
log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
172172
k->external_name(), this->external_name());
173173
}
174174
return false;
175175
}
176176

177177
if (log_is_enabled(Trace, class, nestmates)) {
178-
ResourceMark rm(THREAD);
178+
ResourceMark rm(current);
179179
log_trace(class, nestmates)("Checking nest membership of %s in %s",
180180
k->external_name(), this->external_name());
181181
}
182182

183-
// Check for a resolved cp entry , else fall back to a name check.
184-
// We don't want to resolve any class other than the one being checked.
183+
// Check for the named class in _nest_members.
184+
// We don't resolve, or load, any classes.
185185
for (int i = 0; i < _nest_members->length(); i++) {
186186
int cp_index = _nest_members->at(i);
187-
if (_constants->tag_at(cp_index).is_klass()) {
188-
Klass* k2 = _constants->klass_at(cp_index, THREAD);
189-
assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass()),
190-
"Exceptions should not be possible here");
191-
if (k2 == k) {
192-
log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
193-
return true;
194-
}
195-
}
196-
else {
197-
Symbol* name = _constants->klass_name_at(cp_index);
198-
if (name == k->name()) {
199-
log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
200-
201-
// Names match so check actual klass. This may trigger class loading if
202-
// it doesn't match though that should be impossible as it means one classloader
203-
// has defined two different classes with the same name! A compiler thread won't be
204-
// able to perform that loading but we can't exclude the compiler threads from
205-
// executing this logic. But it should actually be impossible to trigger loading here.
206-
Klass* k2 = _constants->klass_at(cp_index, THREAD);
207-
assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass()),
208-
"Exceptions should not be possible here");
209-
if (k2 == k) {
210-
log_trace(class, nestmates)("- class is listed as a nest member");
211-
return true;
212-
}
213-
else {
214-
// same name but different klass!
215-
log_trace(class, nestmates)(" - klass comparison failed!");
216-
// can't have two names the same, so we're done
217-
return false;
218-
}
219-
}
187+
Symbol* name = _constants->klass_name_at(cp_index);
188+
if (name == k->name()) {
189+
log_trace(class, nestmates)("- named class found at nest_members[%d] => cp[%d]", i, cp_index);
190+
return true;
220191
}
221192
}
222193
log_trace(class, nestmates)("- class is NOT a nest member!");
@@ -287,7 +258,8 @@ InstanceKlass* InstanceKlass::nest_host(TRAPS) {
287258
// need to resolve and save our nest-host class.
288259
if (_nest_host_index != 0) { // we have a real nest_host
289260
// Before trying to resolve check if we're in a suitable context
290-
if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
261+
bool can_resolve = THREAD->can_call_java();
262+
if (!can_resolve && !_constants->tag_at(_nest_host_index).is_klass()) {
291263
log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
292264
this->external_name());
293265
return NULL; // sentinel to say "try again from a different context"
@@ -325,26 +297,15 @@ InstanceKlass* InstanceKlass::nest_host(TRAPS) {
325297
// not an instance class.
326298
if (k->is_instance_klass()) {
327299
nest_host_k = InstanceKlass::cast(k);
328-
bool is_member = nest_host_k->has_nest_member(this, THREAD);
329-
// exception is rare, perhaps impossible
330-
if (!HAS_PENDING_EXCEPTION) {
331-
if (is_member) {
332-
_nest_host = nest_host_k; // save resolved nest-host value
333-
334-
log_trace(class, nestmates)("Resolved nest-host of %s to %s",
335-
this->external_name(), k->external_name());
336-
return nest_host_k;
337-
} else {
338-
error = "current type is not listed as a nest member";
339-
}
300+
bool is_member = nest_host_k->has_nest_member(THREAD->as_Java_thread(), this);
301+
if (is_member) {
302+
_nest_host = nest_host_k; // save resolved nest-host value
303+
304+
log_trace(class, nestmates)("Resolved nest-host of %s to %s",
305+
this->external_name(), k->external_name());
306+
return nest_host_k;
340307
} else {
341-
if (PENDING_EXCEPTION->is_a(vmClasses::VirtualMachineError_klass())) {
342-
return NULL; // propagate VMEs
343-
}
344-
stringStream ss;
345-
ss.print("exception on member check: ");
346-
java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
347-
error = ss.as_string();
308+
error = "current type is not listed as a nest member";
348309
}
349310
} else {
350311
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
@@ -479,8 +479,9 @@ class InstanceKlass: public Klass {
479479
void set_permitted_subclasses(Array<u2>* s) { _permitted_subclasses = s; }
480480

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

485486
public:
486487
// 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 jdk.test.lib.helpers.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)