Skip to content

Commit eecbd08

Browse files
Avoid stack frame re-computation + prevent indirect classloading (#1758)
* remove hybrid to avoid classloading * avoid stack frame re-computation with >= java7 Co-authored-by: eyalkoren <[email protected]>
1 parent ae6fc4a commit eecbd08

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/ElasticApmAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ public static void ensureInstrumented(Class<?> classToInstrument, Collection<Cla
720720
.with(TypeValidation.of(logger.isDebugEnabled()))
721721
.with(FailSafeDeclaredMethodsCompiler.INSTANCE);
722722
AgentBuilder agentBuilder = getAgentBuilder(
723-
byteBuddy, config, logger, AgentBuilder.DescriptionStrategy.Default.HYBRID, false, false
723+
byteBuddy, config, logger, AgentBuilder.DescriptionStrategy.Default.POOL_ONLY, false, false
724724
);
725725
for (Class<? extends ElasticApmInstrumentation> instrumentationClass : instrumentationClasses) {
726726
ElasticApmInstrumentation apmInstrumentation = instantiate(instrumentationClass);

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/bytebuddy/PatchBytecodeVersionTo51Transformer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package co.elastic.apm.agent.bci.bytebuddy;
2626

2727
import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
28+
import net.bytebuddy.ClassFileVersion;
2829
import net.bytebuddy.agent.builder.AgentBuilder;
2930
import net.bytebuddy.asm.Advice;
3031
import net.bytebuddy.asm.AsmVisitorWrapper;
@@ -48,12 +49,25 @@
4849
* {@link Advice.WithCustomMapping#bootstrap} which is important for {@linkplain TracerAwareInstrumentation#indyPlugin() indy plugins}.
4950
*/
5051
public class PatchBytecodeVersionTo51Transformer implements AgentBuilder.Transformer {
52+
53+
private static boolean isAtLeastJava7(TypeDescription typeDescription) {
54+
ClassFileVersion classFileVersion = typeDescription.getClassFileVersion();
55+
return classFileVersion != null && classFileVersion.getJavaVersion() >= 7;
56+
}
57+
5158
@Override
5259
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
60+
if (isAtLeastJava7(typeDescription)) {
61+
// we can avoid the expensive (and somewhat dangerous) stack frame re-computation if stack frames are already
62+
// present in the bytecode, which also allows eagerly loading types that might be present in the method
63+
// body, but not yet loaded by the JVM.
64+
return builder;
65+
}
5366
return builder.visit(new AsmVisitorWrapper.AbstractBase() {
5467
@Override
5568
public ClassVisitor wrap(TypeDescription typeDescription, ClassVisitor classVisitor, Implementation.Context context,
5669
TypePool typePool, FieldList<FieldDescription.InDefinedShape> fieldList, MethodList<?> methodList, int writerFlags, int readerFlags) {
70+
5771
return new ClassVisitor(Opcodes.ASM7, classVisitor) {
5872
private boolean patchVersion;
5973

apm-agent-core/src/main/java/co/elastic/apm/agent/util/ExecutorUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,19 @@ private static void logException(Runnable r, @Nullable Throwable t) {
197197
* Implementation adapted form the {@link ExecutorService} Javadoc
198198
*/
199199
public static void shutdownAndWaitTermination(ExecutorService executor) {
200+
shutdownAndWaitTermination(executor, 1, TimeUnit.SECONDS);
201+
}
202+
203+
public static void shutdownAndWaitTermination(ExecutorService executor, long timeout, TimeUnit unit){
200204
// Disable new tasks from being submitted
201205
executor.shutdown();
202206
try {
203207
// Wait a while for existing tasks to terminate
204-
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
208+
if (!executor.awaitTermination(timeout, unit)) {
205209
// Cancel currently executing tasks
206210
executor.shutdownNow();
207211
// Wait a while for tasks to respond to being cancelled
208-
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
212+
if (!executor.awaitTermination(timeout, unit)) {
209213
logger.warn("Thread pool did not terminate in time " + executor);
210214
}
211215
}

0 commit comments

Comments
 (0)