Skip to content

Commit 52d1f0c

Browse files
author
Amos Shi
committed
8329013: StackOverflowError when starting Apache Tomcat with signed jar
Reviewed-by: mdoerr Backport-of: 925d82931c09dc11ea5a3bc410ea5cfd67ee14aa
1 parent 9682314 commit 52d1f0c

File tree

3 files changed

+186
-8
lines changed

3 files changed

+186
-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, 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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2021, 2022, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.misc;
26+
27+
import java.util.Set;
28+
import java.util.concurrent.ConcurrentHashMap;
29+
30+
/**
31+
* Tracks threads to help detect reentrancy without using ThreadLocal variables.
32+
* A thread invokes the {@code begin} or {@code tryBegin} methods at the start
33+
* of a block, and the {@code end} method at the end of a block.
34+
*/
35+
public class ThreadTracker {
36+
37+
/**
38+
* A reference to a Thread that is suitable for use as a key in a collection.
39+
* The hashCode/equals methods do not invoke the Thread hashCode/equals method
40+
* as they may run arbitrary code and/or leak references to Thread objects.
41+
*/
42+
private record ThreadRef(Thread thread) {
43+
@Override
44+
public int hashCode() {
45+
return Long.hashCode(thread.getId());
46+
}
47+
@Override
48+
public boolean equals(Object obj) {
49+
return (obj instanceof ThreadRef other)
50+
&& this.thread == other.thread;
51+
}
52+
}
53+
54+
private final Set<ThreadRef> threads = ConcurrentHashMap.newKeySet();
55+
56+
/**
57+
* Adds the current thread to thread set if not already in the set.
58+
* Returns a key to remove the thread or {@code null} if already in the set.
59+
*/
60+
public Object tryBegin() {
61+
var threadRef = new ThreadRef(Thread.currentThread());
62+
return threads.add(threadRef) ? threadRef : null;
63+
}
64+
65+
/**
66+
* Adds the current thread to thread set if not already in the set.
67+
* Returns a key to remove the thread.
68+
*/
69+
public Object begin() {
70+
var threadRef = new ThreadRef(Thread.currentThread());
71+
boolean added = threads.add(threadRef);
72+
assert added;
73+
return threadRef;
74+
}
75+
76+
/**
77+
* Removes the thread identified by the key from the thread set.
78+
*/
79+
public void end(Object key) {
80+
var threadRef = (ThreadRef) key;
81+
assert threadRef.thread() == Thread.currentThread();
82+
boolean removed = threads.remove(threadRef);
83+
assert removed;
84+
}
85+
86+
/**
87+
* Returns true if the given thread is tracked.
88+
*/
89+
public boolean contains(Thread thread) {
90+
var threadRef = new ThreadRef(thread);
91+
return threads.contains(threadRef);
92+
}
93+
}
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)