Skip to content

Commit 5ffd214

Browse files
author
Andrew Kent
committed
Set max stack size in bytebuddy exception handler
1 parent 22aa2d7 commit 5ffd214

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/ExceptionHandlers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import datadog.trace.agent.bootstrap.ExceptionLogger;
44
import net.bytebuddy.implementation.Implementation;
55
import net.bytebuddy.implementation.bytecode.StackManipulation;
6-
import net.bytebuddy.implementation.bytecode.StackSize;
76
import net.bytebuddy.jar.asm.Label;
87
import net.bytebuddy.jar.asm.MethodVisitor;
98
import net.bytebuddy.jar.asm.Opcodes;
@@ -19,7 +18,8 @@ public class ExceptionHandlers {
1918

2019
private static final StackManipulation EXCEPTION_STACK_HANDLER =
2120
new StackManipulation() {
22-
private final Size size = StackSize.SINGLE.toDecreasingSize();
21+
// Pops one Throwable off the stack. Maxes the stack to at least 3.
22+
private final Size size = new StackManipulation.Size(-1, 3);
2323

2424
@Override
2525
public boolean isValid() {

dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/test/ExceptionHandlerTest.groovy

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class ExceptionHandlerTest extends Specification {
3333
.advice(
3434
isMethod().and(named("isInstrumented")),
3535
BadAdvice.getName()))
36+
.transform(
37+
new AgentBuilder.Transformer.ForAdvice()
38+
.with(new AgentBuilder.LocationStrategy.Simple(ClassFileLocator.ForClassLoader.of(BadAdvice.getClassLoader())))
39+
.withExceptionHandler(ExceptionHandlers.defaultExceptionHandler())
40+
.advice(
41+
isMethod().and(named("smallStack").or(named("largeStack"))),
42+
BadAdvice.NoOpAdvice.getName()))
3643
.asDecorator()
3744

3845
ByteBuddyAgent.install()
@@ -80,9 +87,31 @@ class ExceptionHandlerTest extends Specification {
8087
testAppender.list.size() == initLogEvents
8188
}
8289

90+
def "exception handler sets the correct stack size"() {
91+
when:
92+
SomeClass.smallStack()
93+
SomeClass.largeStack()
94+
95+
then:
96+
noExceptionThrown()
97+
}
98+
8399
static class SomeClass {
84100
static boolean isInstrumented() {
85101
return false
86102
}
103+
104+
static void smallStack() {
105+
// a method with a max stack of 0
106+
}
107+
108+
static void largeStack() {
109+
// a method with a max stack of 6
110+
long l = 22l
111+
int i = 3
112+
double d = 32.2d
113+
Object o = new Object()
114+
println "large stack: $l $i $d $o"
115+
}
87116
}
88117
}

dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/test/BadAdvice.java renamed to dd-java-agent/agent-tooling/src/test/java/datadog/trace/agent/test/BadAdvice.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ public static void throwAnException(@Advice.Return(readOnly = false) boolean ret
88
returnVal = true;
99
throw new RuntimeException("Test Exception");
1010
}
11+
12+
public static class NoOpAdvice {
13+
@Advice.OnMethodExit(suppress = Throwable.class)
14+
public static void doNothing() {
15+
System.currentTimeMillis();
16+
}
17+
}
1118
}

dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/test/HelperClass.java renamed to dd-java-agent/agent-tooling/src/test/java/datadog/trace/agent/test/HelperClass.java

File renamed without changes.

dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import spock.lang.Timeout
77

88
import java.util.concurrent.TimeUnit
99

10-
@Timeout(1)
10+
@Timeout(10)
1111
class DDSpanTest extends Specification {
1212
def writer = new ListWriter()
1313
def tracer = new DDTracer(writer)

0 commit comments

Comments
 (0)