Skip to content

Commit 7b377ac

Browse files
author
Amos Shi
committed
8329013: StackOverflowError when starting Apache Tomcat with signed jar
Reviewed-by: mbaesken Backport-of: 925d82931c09dc11ea5a3bc410ea5cfd67ee14aa
1 parent 31a780d commit 7b377ac

File tree

3 files changed

+196
-8
lines changed

3 files changed

+196
-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.misc.JavaUtilJarAccess;
2929
import jdk.internal.misc.SharedSecrets;
30+
import jdk.internal.misc.ThreadTracker;
3031

3132
import java.lang.invoke.MethodHandles;
3233
import java.lang.invoke.VarHandle;
@@ -130,6 +131,18 @@ private static String getDurationString(Instant start) {
130131
}
131132
}
132133

134+
private static class ThreadTrackHolder {
135+
static final ThreadTracker TRACKER = new ThreadTracker();
136+
}
137+
138+
private static Object tryBeginLookup() {
139+
return ThreadTrackHolder.TRACKER.tryBegin();
140+
}
141+
142+
private static void endLookup(Object key) {
143+
ThreadTrackHolder.TRACKER.end(key);
144+
}
145+
133146
/**
134147
* Helper to determine if security events are being logged
135148
* at a preconfigured logging level. The configuration value
@@ -138,14 +151,20 @@ private static String getDurationString(Instant start) {
138151
* @return boolean indicating whether an event should be logged
139152
*/
140153
public static boolean isLoggingSecurity() {
141-
// Avoid a bootstrap issue where the commitEvent attempts to
142-
// trigger early loading of System Logger but where
143-
// the verification process still has JarFiles locked
144-
if (securityLogger == null && !JUJA.isInitializing()) {
145-
LOGGER_HANDLE.compareAndSet( null, System.getLogger(SECURITY_LOGGER_NAME));
146-
loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);
154+
Object key;
155+
// Avoid bootstrap issues where
156+
// * commitEvent triggers early loading of System Logger but where
157+
// the verification process still has JarFiles locked
158+
// * the loading of the logging libraries involves recursive
159+
// calls to security libraries triggering recursion
160+
if (securityLogger == null && !JUJA.isInitializing() && (key = tryBeginLookup()) != null) {
161+
try {
162+
LOGGER_HANDLE.compareAndSet(null, System.getLogger(SECURITY_LOGGER_NAME));
163+
loggingSecurity = securityLogger.isLoggable(LOG_LEVEL);
164+
} finally {
165+
endLookup(key);
166+
}
147167
}
148168
return loggingSecurity;
149169
}
150-
151170
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 final class ThreadRef {
43+
private final Thread thread;
44+
45+
ThreadRef(Thread thread) {
46+
this.thread = thread;
47+
}
48+
49+
Thread thread() {
50+
return this.thread;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Long.hashCode(thread.getId());
56+
}
57+
@Override
58+
public boolean equals(Object obj) {
59+
return (obj instanceof ThreadRef)
60+
&& this.thread == ((ThreadRef)obj).thread;
61+
}
62+
}
63+
64+
private final Set<ThreadRef> threads = ConcurrentHashMap.newKeySet();
65+
66+
/**
67+
* Adds the current thread to thread set if not already in the set.
68+
* Returns a key to remove the thread or {@code null} if already in the set.
69+
*/
70+
public Object tryBegin() {
71+
var threadRef = new ThreadRef(Thread.currentThread());
72+
return threads.add(threadRef) ? threadRef : null;
73+
}
74+
75+
/**
76+
* Adds the current thread to thread set if not already in the set.
77+
* Returns a key to remove the thread.
78+
*/
79+
public Object begin() {
80+
var threadRef = new ThreadRef(Thread.currentThread());
81+
boolean added = threads.add(threadRef);
82+
assert added;
83+
return threadRef;
84+
}
85+
86+
/**
87+
* Removes the thread identified by the key from the thread set.
88+
*/
89+
public void end(Object key) {
90+
var threadRef = (ThreadRef) key;
91+
assert threadRef.thread() == Thread.currentThread();
92+
boolean removed = threads.remove(threadRef);
93+
assert removed;
94+
}
95+
96+
/**
97+
* Returns true if the given thread is tracked.
98+
*/
99+
public boolean contains(Thread thread) {
100+
var threadRef = new ThreadRef(thread);
101+
return threads.contains(threadRef);
102+
}
103+
}
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)