-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Catch LinkageError in ThrowableExtendedStackTraceRenderer#loadClass (#4028)
#4049
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
chanani
wants to merge
2
commits into
apache:2.x
Choose a base branch
from
chanani:fix/4028
base: 2.x
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.
Open
Changes from 1 commit
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
135 changes: 135 additions & 0 deletions
135
...t/java/org/apache/logging/log4j/core/pattern/ThrowableExtendedStackTraceRendererTest.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,135 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.logging.log4j.core.pattern; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatNoException; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.logging.log4j.Level; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
| import org.apache.logging.log4j.core.layout.PatternLayout; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junitpioneer.jupiter.Issue; | ||
|
|
||
| /** | ||
| * Tests for {@link ThrowableExtendedStackTraceRenderer}. | ||
| * | ||
| * <p>Verifies that {@link NoClassDefFoundError} during class loading | ||
| * does not break the extended stack trace rendering ({@code %xEx}).</p> | ||
| * | ||
| * @see <a href="https://github.com/apache/logging-log4j2/issues/4028">#4028</a> | ||
| */ | ||
| class ThrowableExtendedStackTraceRendererTest { | ||
|
|
||
| private static final PatternParser PATTERN_PARSER = PatternLayout.createPatternParser(null); | ||
|
|
||
| /** | ||
| * Verifies that the extended stack trace renderer does not propagate {@link NoClassDefFoundError} | ||
| * when a class in the stack trace cannot be found by the class loader. | ||
| */ | ||
| @Test | ||
| @Issue("https://github.com/apache/logging-log4j2/issues/4028") | ||
| void loadClass_should_handle_NoClassDefFoundError() { | ||
|
|
||
| // Create an exception with a stack trace element referencing a non-existent class. | ||
| // When the extended renderer tries to resolve this class for JAR/version info, | ||
| // the class loader will throw NoClassDefFoundError. | ||
| final Exception exception = new Exception("test exception"); | ||
| final StackTraceElement[] originalTrace = exception.getStackTrace(); | ||
| final StackTraceElement[] modifiedTrace = new StackTraceElement[originalTrace.length + 1]; | ||
| modifiedTrace[0] = new StackTraceElement( | ||
| "com.nonexistent.deliberately.missing.ClassName", "someMethod", "ClassName.java", 42); | ||
| System.arraycopy(originalTrace, 0, modifiedTrace, 1, originalTrace.length); | ||
| exception.setStackTrace(modifiedTrace); | ||
|
|
||
| // Render using the extended throwable pattern (%xEx) | ||
| final List<PatternFormatter> patternFormatters = PATTERN_PARSER.parse("%xEx", false, true, true); | ||
| assertThat(patternFormatters).hasSize(1); | ||
| final PatternFormatter patternFormatter = patternFormatters.get(0); | ||
|
|
||
| final LogEvent logEvent = Log4jLogEvent.newBuilder() | ||
| .setThrown(exception) | ||
| .setLevel(Level.ERROR) | ||
| .build(); | ||
| final StringBuilder buffer = new StringBuilder(); | ||
|
|
||
| // The rendering should not throw any exception | ||
| assertThatNoException().isThrownBy(() -> patternFormatter.format(logEvent, buffer)); | ||
|
|
||
| // The output should contain our non-existent class name and the exception message | ||
| final String output = buffer.toString(); | ||
| assertThat(output).contains("com.nonexistent.deliberately.missing.ClassName"); | ||
| assertThat(output).contains("test exception"); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that the extended stack trace renderer gracefully handles a class loader | ||
| * that throws {@link NoClassDefFoundError} during class resolution. | ||
| * | ||
| * <p>This simulates the real-world scenario from | ||
| * <a href="https://github.com/apache/logging-log4j2/issues/4028">#4028</a>, | ||
| * where custom class loaders (e.g., in application servers or frameworks like Frank!Framework) | ||
| * throw {@link NoClassDefFoundError} for classes that have been unloaded.</p> | ||
| */ | ||
| @Test | ||
| @Issue("https://github.com/apache/logging-log4j2/issues/4028") | ||
| void loadClass_should_handle_NoClassDefFoundError_from_custom_classloader() { | ||
|
|
||
| // Create a class loader that always throws NoClassDefFoundError | ||
| final ClassLoader errorThrowingLoader = new ClassLoader(null) { | ||
| @Override | ||
| public Class<?> loadClass(final String name) throws ClassNotFoundException { | ||
| throw new NoClassDefFoundError("Simulated missing class: " + name); | ||
| } | ||
| }; | ||
|
|
||
| final Exception exception = new Exception("test with custom classloader"); | ||
| exception.setStackTrace(new StackTraceElement[] { | ||
| new StackTraceElement("com.example.UnloadedClass", "doWork", "UnloadedClass.java", 10), | ||
| new StackTraceElement("com.example.CallerClass", "invoke", "CallerClass.java", 20) | ||
| }); | ||
|
|
||
| final List<PatternFormatter> patternFormatters = PATTERN_PARSER.parse("%xEx", false, true, true); | ||
| final PatternFormatter patternFormatter = patternFormatters.get(0); | ||
|
|
||
| final LogEvent logEvent = Log4jLogEvent.newBuilder() | ||
| .setThrown(exception) | ||
| .setLevel(Level.ERROR) | ||
| .build(); | ||
| final StringBuilder buffer = new StringBuilder(); | ||
|
|
||
| // Set the error-throwing class loader as the context class loader | ||
| // so the renderer's loadClass method encounters it | ||
| final Thread currentThread = Thread.currentThread(); | ||
| final ClassLoader originalLoader = currentThread.getContextClassLoader(); | ||
| try { | ||
| currentThread.setContextClassLoader(errorThrowingLoader); | ||
|
|
||
| // The rendering should succeed without propagating NoClassDefFoundError | ||
| assertThatNoException().isThrownBy(() -> patternFormatter.format(logEvent, buffer)); | ||
| } finally { | ||
| currentThread.setContextClassLoader(originalLoader); | ||
| } | ||
|
|
||
| // The output should still contain the exception and stack trace | ||
| final String output = buffer.toString(); | ||
| assertThat(output).contains("test with custom classloader"); | ||
| assertThat(output).contains("com.example.UnloadedClass"); | ||
| } | ||
| } |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -181,7 +181,7 @@ private static Class<?> loadClass(final ClassLoader loader, final String classNa | |||||
| if (clazz != null) { | ||||||
| return clazz; | ||||||
| } | ||||||
| } catch (final Exception ignored) { | ||||||
| } catch (final Exception | LinkageError ignored) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| // Do nothing | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
9 changes: 9 additions & 0 deletions
9
src/changelog/.2.x.x/4028_fix_catch_LinkageError_in_ThrowableExtendedStackTraceRenderer.xml
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,9 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
| xmlns="https://logging.apache.org/xml/ns" | ||||||
| xsi:schemaLocation="https://logging.apache.org/xml/ns | ||||||
| https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||||||
| type="fixed"> | ||||||
| <issue id="4028" link="https://github.com/apache/logging-log4j2/issues/4028"/> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| <description format="asciidoc">Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` to prevent `NoClassDefFoundError` from breaking logging</description> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| </entry> | ||||||
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.
Instead of adding a new test file, would you mind extending
ThrowablePatternConverterTest.StackTraceTest, please?