From a853a04969842d644305745ea2da539b4f4822b2 Mon Sep 17 00:00:00 2001 From: Andrea Marziali Date: Mon, 13 Jan 2025 13:39:50 +0100 Subject: [PATCH] muzzle --- .../instrumentation/mule-4/build.gradle | 5 +---- .../mule4/JpmsMuleInstrumentation.java | 17 +++++++++++++++- .../mule4/JpmsAdvisingHelper.java | 14 +++++++++++-- .../mule4/JpmsClearanceAdvice.java | 20 ------------------- 4 files changed, 29 insertions(+), 27 deletions(-) delete mode 100644 dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java diff --git a/dd-java-agent/instrumentation/mule-4/build.gradle b/dd-java-agent/instrumentation/mule-4/build.gradle index ead93c8cc679..e68295922eeb 100644 --- a/dd-java-agent/instrumentation/mule-4/build.gradle +++ b/dd-java-agent/instrumentation/mule-4/build.gradle @@ -130,10 +130,7 @@ tasks.named("compileLatestDepForkedTestJava").configure { dependencies { compileOnly group: 'org.mule.runtime', name: 'mule-core', version: muleVersion compileOnly group: 'org.mule.runtime', name: 'mule-tracer-customization-impl', version: muleVersion - main_java11CompileOnly project(':internal-api') - main_java11CompileOnly project(':dd-java-agent:agent-tooling') - main_java11CompileOnly project(':dd-java-agent:agent-bootstrap') - main_java11CompileOnly sourceSets.main.output + compileOnly sourceSets.main_java11.output testImplementation project(':dd-java-agent:instrumentation:aws-common') testImplementation project(':dd-java-agent:instrumentation:reactor-core-3.1') testImplementation project(':dd-java-agent:instrumentation:reactive-streams') diff --git a/dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/JpmsMuleInstrumentation.java b/dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/JpmsMuleInstrumentation.java index 4e2391dc5108..d65123e05c32 100644 --- a/dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/JpmsMuleInstrumentation.java +++ b/dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/JpmsMuleInstrumentation.java @@ -6,6 +6,9 @@ import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.InstrumenterModule; import datadog.trace.api.Platform; +import net.bytebuddy.asm.Advice; +import net.bytebuddy.implementation.bytecode.assign.Assigner; +import org.mule.runtime.tracer.api.EventTracer; @AutoService(InstrumenterModule.class) public class JpmsMuleInstrumentation extends InstrumenterModule.Tracing @@ -38,6 +41,18 @@ public String[] helperClassNames() { @Override public void methodAdvice(MethodTransformer transformer) { // it does not work with typeInitializer() - transformer.applyAdvice(isConstructor(), packageName + ".JpmsClearanceAdvice"); + transformer.applyAdvice(isConstructor(), packageName + "$JpmsClearanceAdvice"); + } + + public static class JpmsClearanceAdvice { + @Advice.OnMethodExit(suppress = Throwable.class) + public static void openOnReturn(@Advice.This(typing = Assigner.Typing.DYNAMIC) Object self) { + JpmsAdvisingHelper.allowAccessOnModuleClass(self.getClass()); + } + + private static void muzzleCheck(final EventTracer tracer) { + // introduced in 4.5.0 + tracer.endCurrentSpan(null); + } } } diff --git a/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java b/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java index aee99b4fae5c..9c71274f97a6 100644 --- a/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java +++ b/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsAdvisingHelper.java @@ -5,8 +5,18 @@ public class JpmsAdvisingHelper { private static final WeakHashMap ALREADY_PROCESSED_CACHE = new WeakHashMap<>(); - public static boolean isModuleAlreadyProcessed(final Module module) { - return Boolean.TRUE.equals(ALREADY_PROCESSED_CACHE.putIfAbsent(module, Boolean.TRUE)); + public static void allowAccessOnModuleClass(final Class cls) { + final Module module = cls.getModule(); + if (module == null + || Boolean.TRUE.equals(ALREADY_PROCESSED_CACHE.putIfAbsent(module, Boolean.TRUE))) { + return; + } + for (String pn : module.getPackages()) { + try { + module.addExports(pn, module.getClassLoader().getUnnamedModule()); + } catch (Throwable ignored) { + } + } } private JpmsAdvisingHelper() {} diff --git a/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java b/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java deleted file mode 100644 index 684b9ccff3b4..000000000000 --- a/dd-java-agent/instrumentation/mule-4/src/main/java11/datadog/trace/instrumentation/mule4/JpmsClearanceAdvice.java +++ /dev/null @@ -1,20 +0,0 @@ -package datadog.trace.instrumentation.mule4; - -import net.bytebuddy.asm.Advice; -import net.bytebuddy.implementation.bytecode.assign.Assigner; - -public class JpmsClearanceAdvice { - @Advice.OnMethodExit(suppress = Throwable.class) - public static void openOnReturn(@Advice.This(typing = Assigner.Typing.DYNAMIC) Object self) { - final Module module = self.getClass().getModule(); - if (module == null || JpmsAdvisingHelper.isModuleAlreadyProcessed(module)) { - return; - } - for (String pn : module.getPackages()) { - try { - module.addExports(pn, module.getClassLoader().getUnnamedModule()); - } catch (Throwable ignored) { - } - } - } -}