Skip to content

Commit 2331bc7

Browse files
committed
8329013: StackOverflowError when starting Apache Tomcat with signed jar
Backport-of: 925d82931c09dc11ea5a3bc410ea5cfd67ee14aa
1 parent 68d0925 commit 2331bc7

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

src/java.base/share/classes/jdk/internal/event/EventHelper.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
2727

2828
import jdk.internal.access.JavaUtilJarAccess;
2929
import jdk.internal.access.SharedSecrets;
30+
import jdk.internal.misc.ThreadTracker;
3031

3132
import java.lang.invoke.MethodHandles;
3233
import java.lang.invoke.VarHandle;
@@ -133,6 +134,18 @@ private static String getDurationString(Instant start) {
133134
}
134135
}
135136

137+
private static class ThreadTrackHolder {
138+
static final ThreadTracker TRACKER = new ThreadTracker();
139+
}
140+
141+
private static Object tryBeginLookup() {
142+
return ThreadTrackHolder.TRACKER.tryBegin();
143+
}
144+
145+
private static void endLookup(Object key) {
146+
ThreadTrackHolder.TRACKER.end(key);
147+
}
148+
136149
/**
137150
* Helper to determine if security events are being logged
138151
* at a preconfigured logging level. The configuration value
@@ -141,14 +154,20 @@ private static String getDurationString(Instant start) {
141154
* @return boolean indicating whether an event should be logged
142155
*/
143156
public static boolean isLoggingSecurity() {
144-
// Avoid a bootstrap issue where the commitEvent attempts to
145-
// trigger early loading of System Logger but where
146-
// the verification process still has JarFiles locked
147-
if (securityLogger == null && !JUJA.isInitializing()) {
148-
LOGGER_HANDLE.compareAndSet( null, System.getLogger(SECURITY_LOGGER_NAME));
149-
loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);
157+
Object key;
158+
// Avoid bootstrap issues where
159+
// * commitEvent triggers early loading of System Logger but where
160+
// the verification process still has JarFiles locked
161+
// * the loading of the logging libraries involves recursive
162+
// calls to security libraries triggering recursion
163+
if (securityLogger == null && !JUJA.isInitializing() && (key = tryBeginLookup()) != null) {
164+
try {
165+
LOGGER_HANDLE.compareAndSet(null, System.getLogger(SECURITY_LOGGER_NAME));
166+
loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);
167+
} finally {
168+
endLookup(key);
169+
}
150170
}
151171
return loggingSecurity;
152172
}
153-
154173
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2024, 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+
import java.util.logging.*;
25+
26+
import jdk.internal.event.EventHelper;
27+
28+
/*
29+
* @test
30+
* @bug 8329013
31+
* @summary StackOverflowError when starting Apache Tomcat with signed jar
32+
* @modules java.base/jdk.internal.event:+open
33+
* @run main/othervm -Xmx32m -Djava.util.logging.manager=RecursiveEventHelper RecursiveEventHelper
34+
*/
35+
public class RecursiveEventHelper extends LogManager {
36+
// an extra check to ensure the custom manager is in use
37+
static volatile boolean customMethodCalled;
38+
39+
public static void main(String[] args) throws Exception {
40+
String classname = System.getProperty("java.util.logging.manager");
41+
if (!classname.equals("RecursiveEventHelper")) {
42+
throw new RuntimeException("java.util.logging.manager not set");
43+
}
44+
45+
// this call will trigger initialization of logging framework
46+
// which will call into our custom LogManager and use the
47+
// custom getProperty method below. EventHelper.isLoggingSecurity()
48+
// is also on the code path of original report and triggers
49+
// similar recursion.
50+
System.getLogger("testLogger");
51+
if (!customMethodCalled) {
52+
throw new RuntimeException("Method not called");
53+
}
54+
}
55+
56+
@Override
57+
public String getProperty(String p) {
58+
// this call mimics issue reported in initial bug report where
59+
// opening of a signed jar during System logger initialization triggered
60+
// a recursive call (via EventHelper.isLoggingSecurity) back into
61+
// logger API
62+
EventHelper.isLoggingSecurity();
63+
customMethodCalled = true;
64+
return super.getProperty(p);
65+
}
66+
}

0 commit comments

Comments
 (0)