Skip to content

Commit

Permalink
added test to demonstrate stack overflow "bomb" in logstash layout
Browse files Browse the repository at this point in the history
  • Loading branch information
labakomchev committed Apr 13, 2019
1 parent b2f8351 commit f933ba0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vlkan.log4j2.logstash.layout;

import org.apache.commons.lang3.RandomUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ContextDataFactory;
Expand All @@ -24,6 +25,10 @@ static List<LogEvent> createLiteLogEvents(int logEventCount) {
}
return logEvents;
}
static LogEvent createHugeMeessagLogEvent(int messageSize) {

return createFullLogEvent("hugeId",new String(RandomUtils.nextBytes(messageSize)));
}

private static LogEvent createLiteLogEvent(String id) {
SimpleMessage message = new SimpleMessage("Msg" + id);
Expand All @@ -44,13 +49,13 @@ private static LogEvent createLiteLogEvent(String id) {
static List<LogEvent> createFullLogEvents(int logEventCount) {
List<LogEvent> logEvents = new ArrayList<>(logEventCount);
for (int logEventIndex = 0; logEventIndex < logEventCount; logEventIndex++) {
LogEvent logEvent = LogEventFixture.createFullLogEvent(String.valueOf(logEventIndex));
LogEvent logEvent = LogEventFixture.createFullLogEvent(String.valueOf(logEventIndex),"Msg" + logEventIndex);
logEvents.add(logEvent);
}
return logEvents;
}

private static LogEvent createFullLogEvent(String id) {
private static LogEvent createFullLogEvent(String id,String message) {

// Create exception.
Exception sourceHelper = new Exception();
Expand All @@ -68,7 +73,6 @@ private static LogEvent createFullLogEvent(String id) {
int threadId = id.hashCode();
String threadName = "MyThreadName" + id;
int threadPriority = threadId % 10;
SimpleMessage message = new SimpleMessage("Msg" + id);
Level level = Level.DEBUG;
String loggerFqcn = "f.q.c.n" + id;
String loggerName = "a.B" + id;
Expand All @@ -80,7 +84,7 @@ private static LogEvent createFullLogEvent(String id) {
.setLoggerName(loggerName)
.setLoggerFqcn(loggerFqcn)
.setLevel(level)
.setMessage(message)
.setMessage(new SimpleMessage(message))
.setThrown(ioException)
.setContextData(contextData)
.setContextStack(contextStack)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.vlkan.log4j2.logstash.layout;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.nio.BufferOverflowException;

public class LogStashLayoutGeneratorStateTest {
private static final Configuration CONFIGURATION = new DefaultConfiguration();

@Test
public void testGeneratorStateCorruptedAfterUnhandledException() {
LogstashLayout layout = LogstashLayout
.newBuilder()
.setConfiguration(CONFIGURATION)
.setEventTemplateUri("classpath:LogstashTestLayout.json")
.setStackTraceEnabled(true)
.setLocationInfoEnabled(true)
.setEmptyPropertyExclusionEnabled(true)
.build();
LogEvent hugeMessageLogEvent = LogEventFixture.createHugeMeessagLogEvent(512 * 1024);
LogEvent fullEvent = LogEventFixture.createFullLogEvents(1).get(0);
long counter = 0;
encodeSafe(layout, fullEvent);
Assertions.assertThatThrownBy(()->layout.toByteArray(hugeMessageLogEvent)).hasCauseInstanceOf(BufferOverflowException.class);
long startTime = System.currentTimeMillis();
while(true){
encodeSafe(layout, fullEvent);
long l = ++counter;
if(System.currentTimeMillis()-startTime>=1000){
System.out.println(l);
startTime=System.currentTimeMillis();
}
}

}

private void encodeSafe(LogstashLayout layout, LogEvent fullEvent) {
try {
layout.toByteArray(fullEvent);
}
catch (Exception e){
//do nothing
}
}
}

0 comments on commit f933ba0

Please sign in to comment.