-
Notifications
You must be signed in to change notification settings - Fork 342
Fix StackOverflowError in caused by missing re-entrancy guard #11838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ValentinZakharov
wants to merge
1
commit into
master
Choose a base branch
from
vzakharov/fix-request-dispatcher-reentrancy-soe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...vlet/javax-servlet/javax-servlet-common/src/test/java/RequestDispatcherRecursionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Proxy; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import javax.servlet.RequestDispatcher; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletRequestWrapper; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Regression test for the SAP Hybris/Spartacus StackOverflow reported in v1.63.0. | ||
| * | ||
| * <p>Root cause: {@code RequestDispatcherAdvice.start()} calls {@code injectContext()} which calls | ||
| * {@code request.setAttribute()} for each propagation header. In some request wrappers (Hybris | ||
| * internals, SAP Commerce), {@code setAttribute()} internally triggers another {@code | ||
| * RequestDispatcher.forward()} call. Without a re-entrancy guard the advice recurses until {@code | ||
| * StackOverflowError}, visible in production as many "Failed to handle exception in instrumentation | ||
| * for ApplicationDispatcher" log entries. | ||
| * | ||
| * <p>The fix is a {@code CallDepthThreadLocalMap} guard in {@code RequestDispatcherAdvice.start()} | ||
| * analogous to the one already present in {@code AsyncContextInstrumentation}. | ||
| */ | ||
| class RequestDispatcherRecursionTest extends AbstractInstrumentationTest { | ||
|
|
||
| /** Minimal {@code RequestDispatcher} stub — instrumented by ByteBuddy via interface match. */ | ||
| static class TestDispatcher implements RequestDispatcher { | ||
| @Override | ||
| public void forward(ServletRequest req, ServletResponse resp) | ||
| throws ServletException, IOException {} | ||
|
|
||
| @Override | ||
| public void include(ServletRequest req, ServletResponse resp) | ||
| throws ServletException, IOException {} | ||
| } | ||
|
|
||
| /** | ||
| * {@code RequestDispatcher} with a non-trivial body simulating {@code ApplicationDispatcher} | ||
| * (Tomcat / SAP Commerce), which traverses a filter and valve chain adding significant call | ||
| * depth. | ||
| */ | ||
| static class RealisticDispatcher implements RequestDispatcher { | ||
| @Override | ||
| public void forward(ServletRequest req, ServletResponse resp) | ||
| throws ServletException, IOException { | ||
| simulateFilterChain(200); | ||
| } | ||
|
|
||
| @Override | ||
| public void include(ServletRequest req, ServletResponse resp) | ||
| throws ServletException, IOException {} | ||
|
|
||
| private static void simulateFilterChain(int depth) { | ||
| if (depth > 0) simulateFilterChain(depth - 1); | ||
| } | ||
| } | ||
|
|
||
| /** Creates a proxy stub for {@code iface} where all methods return null / 0 / false. */ | ||
| @SuppressWarnings("unchecked") | ||
| static <T> T nullStub(Class<T> iface) { | ||
| return (T) | ||
| Proxy.newProxyInstance( | ||
| iface.getClassLoader(), | ||
| new Class<?>[] {iface}, | ||
| (proxy, method, args) -> { | ||
| Class<?> ret = method.getReturnType(); | ||
| if (ret == boolean.class) return false; | ||
| if (ret == int.class || ret == long.class) return 0; | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void forward_doesNotRecurse_whenSetAttributeTriggersRedispatch() throws Exception { | ||
| TestDispatcher dispatcher = new TestDispatcher(); | ||
| HttpServletResponse response = nullStub(HttpServletResponse.class); | ||
|
|
||
| AtomicInteger currentDepth = new AtomicInteger(0); | ||
| AtomicInteger maxDepth = new AtomicInteger(0); | ||
|
|
||
| // Hybris-style wrapper: setAttribute() re-triggers a dispatch. Capped at depth 3 | ||
| // to bound total calls (≈ N^3, N ≈ 5 headers) and avoid OOM without the fix. | ||
| final int MAX_SETATTRIBUTE_DEPTH = 3; | ||
| HttpServletRequest recursiveRequest = | ||
| new HttpServletRequestWrapper(nullStub(HttpServletRequest.class)) { | ||
| @Override | ||
| public void setAttribute(String name, Object value) { | ||
| int depth = currentDepth.incrementAndGet(); | ||
| maxDepth.updateAndGet(d -> Math.max(d, depth)); | ||
| try { | ||
| super.setAttribute(name, value); | ||
| if (depth < MAX_SETATTRIBUTE_DEPTH) { | ||
| dispatcher.forward(this, response); | ||
| } | ||
| } catch (Exception | StackOverflowError ignored) { | ||
| } finally { | ||
| currentDepth.decrementAndGet(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // runUnderTrace provides an active span; without one the advice exits before injectContext(). | ||
| runUnderTrace( | ||
| "test", | ||
| () -> { | ||
| dispatcher.forward(recursiveRequest, response); | ||
| return null; | ||
| }); | ||
|
|
||
| assertTrue( | ||
| maxDepth.get() >= 1, | ||
| "advice did not call setAttribute() — check that TestDispatcher is instrumented" | ||
| + " by ByteBuddy and that runUnderTrace creates an active span"); | ||
|
|
||
| assertEquals( | ||
| 1, | ||
| maxDepth.get(), | ||
| "setAttribute() was called recursively to depth " | ||
| + maxDepth.get() | ||
| + ". RequestDispatcherAdvice.start() needs a CallDepthThreadLocalMap guard " | ||
| + "(see AsyncContextInstrumentation for the pattern)."); | ||
| } | ||
|
|
||
| /** | ||
| * Regression test for the production SOE scenario with a realistic dispatcher body. | ||
| * | ||
| * <p>Without the fix, the recursive {@code setAttribute()} pattern in Hybris would fill the call | ||
| * stack, and the extra frames from {@code simulateFilterChain()} would tip it into a {@code | ||
| * StackOverflowError} from the method <em>body</em> — captured by {@code @Advice.Thrown} → {@code | ||
| * DECORATE.onError()} → {@code span.setTag("error.stack")}. | ||
| * | ||
| * <p>With the {@code CallDepthThreadLocalMap} fix, re-entrant {@code forward()} calls return | ||
| * immediately from the enter advice (depth > 0), so the stack never saturates and no SOE is | ||
| * thrown. The test verifies exactly this: {@code capturedSoe} must be {@code null}. | ||
| */ | ||
| @Test | ||
| void forward_noSoe_withRealisticDispatcherBodyAndSetAttributeRecursion() throws Exception { | ||
| AtomicReference<StackOverflowError> capturedSoe = new AtomicReference<>(); | ||
| AtomicBoolean soeSeen = new AtomicBoolean(false); // stops re-dispatching once SOE is caught | ||
| AtomicBoolean setAttributeInvoked = new AtomicBoolean(false); // guards against false-positive | ||
|
|
||
| RealisticDispatcher dispatcher = new RealisticDispatcher(); | ||
| HttpServletResponse response = nullStub(HttpServletResponse.class); | ||
|
|
||
| // Hybris-style wrapper: setAttribute() triggers a new forward(); no depth cap. | ||
| HttpServletRequest recursiveRequest = | ||
| new HttpServletRequestWrapper(nullStub(HttpServletRequest.class)) { | ||
| @Override | ||
| public void setAttribute(String name, Object value) { | ||
| super.setAttribute(name, value); | ||
| setAttributeInvoked.set(true); | ||
| if (soeSeen.get()) return; // already captured — stop recursing | ||
| try { | ||
| dispatcher.forward(this, response); | ||
| } catch (StackOverflowError soe) { | ||
| capturedSoe.compareAndSet(null, soe); | ||
| soeSeen.set(true); | ||
| throw soe; | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| try { | ||
| runUnderTrace( | ||
| "test", | ||
| () -> { | ||
| dispatcher.forward(recursiveRequest, response); | ||
| return null; | ||
| }); | ||
| } catch (StackOverflowError ignored) { | ||
| // SOE may reach this level if it is not fully absorbed at inner levels. | ||
| } | ||
|
|
||
| assertTrue( | ||
| setAttributeInvoked.get(), | ||
| "advice did not call setAttribute() — check that RealisticDispatcher is instrumented" | ||
| + " by ByteBuddy and that runUnderTrace creates an active span"); | ||
|
|
||
| assertNull( | ||
| capturedSoe.get(), | ||
| "StackOverflowError was thrown — fix regression: CallDepthThreadLocalMap guard " | ||
| + "was removed from RequestDispatcherAdvice.start()"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the normal servlet dispatch path
requestContextis non-null because the servlet advice storesDD_CONTEXT_ATTRIBUTEon the request, so resetting the call-depth guard beforerequest.setAttribute(DD_CONTEXT_ATTRIBUTE, requestContext)reopens the same recursion window on exit. With wrappers that redispatch fromsetAttribute()(the production scenario this fixes), the restore call now starts another fully instrumentedforward()while the dispatcher span is still active, which can repeat from each nested exit and still overflow; keep the guard set until after all advice-drivensetAttributecalls have completed.Useful? React with 👍 / 👎.