-
Notifications
You must be signed in to change notification settings - Fork 333
Introduce a generic JavaModuleOpener for JPMS #11220
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
amarziali
wants to merge
7
commits into
master
Choose a base branch
from
andrea.marziali/javamodule
base: master
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c9de5a
Introduce a generic JavaModuleOpener for JPMS
amarziali 21f9c40
Move to ForConfiguredTypes
amarziali 9e1153e
Also open target classloader unnamed module
amarziali 8ce6f83
Add graalvm config
amarziali 9640136
Make sure the instrumentation compiles with 1.8 compatibility
amarziali 8541151
change test
amarziali 1174aaa
add final modifier
amarziali 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
40 changes: 40 additions & 0 deletions
40
...otstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/module/JpmsHelper.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,40 @@ | ||
| package datadog.trace.bootstrap.instrumentation.java.module; | ||
|
|
||
| import static java.util.Collections.unmodifiableSet; | ||
|
|
||
| import datadog.trace.api.GenericClassValue; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @NotThreadSafe | ||
| public final class JpmsHelper { | ||
| private JpmsHelper() {} | ||
|
|
||
| private static final Set<String> TRIGGERS = new HashSet<>(); | ||
|
|
||
| private static final ClassValue<AtomicBoolean> CLSVALUE = | ||
| GenericClassValue.constructing(AtomicBoolean.class); | ||
|
|
||
| public static final Logger LOGGER = LoggerFactory.getLogger(JpmsHelper.class); | ||
|
|
||
| public static void addAllTriggers(Iterable<String> classes) { | ||
| if (classes == null) { | ||
| return; | ||
| } | ||
| for (String cls : classes) { | ||
| TRIGGERS.add(cls); | ||
| } | ||
| } | ||
|
|
||
| public static Set<String> getAllTriggers() { | ||
| return unmodifiableSet(TRIGGERS); | ||
| } | ||
|
|
||
| public static boolean shouldBeOpened(Class<?> cls) { | ||
| return CLSVALUE.get(cls).compareAndSet(false, true); | ||
| } | ||
| } |
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
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
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
12 changes: 12 additions & 0 deletions
12
...agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/JavaModuleOpenProvider.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,12 @@ | ||
| package datadog.trace.agent.tooling; | ||
|
|
||
| /** | ||
| * Allows an {@link InstrumenterModule} to possibly open a java module to the unnamed module. | ||
| * | ||
| * <p>This is typically used when reflective operations need to be done and the agent cannot assume | ||
| * that the host application has permitted them. | ||
| */ | ||
| public interface JavaModuleOpenProvider { | ||
| /** Classes whose constructors trigger the one-time module open when first instantiated. */ | ||
| Iterable<String> triggerClasses(); | ||
| } |
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
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
76 changes: 76 additions & 0 deletions
76
...ain/java/datadog/trace/instrumentation/java/lang/module/JpmsClearanceInstrumentation.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,76 @@ | ||
| package datadog.trace.instrumentation.java.lang.module; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.environment.JavaVirtualMachine; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.bootstrap.instrumentation.java.module.JpmsHelper; | ||
| import java.util.Collection; | ||
| import java.util.Set; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.implementation.bytecode.assign.Assigner; | ||
|
|
||
| /** | ||
| * Generic instrumenter module that will advice the constructor of each known classes in order to | ||
| * open once their module. This is marked for bootstrap even if it's not for sure, but we cannot | ||
| * know in advance (depends to the instrumented types and today we are instrumenting InetAddress | ||
| * that's in the bootstrap). | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public class JpmsClearanceInstrumentation extends InstrumenterModule | ||
| implements Instrumenter.ForConfiguredTypes, | ||
| Instrumenter.ForBootstrap, | ||
| Instrumenter.HasMethodAdvice { | ||
| public JpmsClearanceInstrumentation() { | ||
| super("java-module"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled() { | ||
| return super.isEnabled() && JavaVirtualMachine.isJavaVersionAtLeast(9); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isApplicable(Set<TargetSystem> enabledSystems) { | ||
| return true; // not directly linked ot a target system | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice(isConstructor(), getClass().getName() + "$OpenModuleAdvice"); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> configuredMatchingTypes() { | ||
| return JpmsHelper.getAllTriggers(); | ||
| } | ||
|
|
||
| public static class OpenModuleAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void onExit(@Advice.This(typing = Assigner.Typing.DYNAMIC) Object self) { | ||
| final Class<?> cls = self.getClass(); | ||
| if (JpmsHelper.shouldBeOpened(cls)) { | ||
| final Module module = cls.getModule(); | ||
| if (module != null) { | ||
| try { | ||
| // This call needs imperatively to be done from the same module we're adding exports | ||
| // because the jdk is checking that the caller belongs to the same module. | ||
| // The code of this advice is getting inlined into the constructor of the class | ||
| // belonging | ||
| // to that package so it will work. Moving the same to a helper won't. | ||
| module.addOpens(cls.getPackageName(), JpmsHelper.class.getModule()); | ||
|
amarziali marked this conversation as resolved.
|
||
| final ClassLoader loader = cls.getClassLoader(); | ||
| if (loader != null) { | ||
| module.addOpens(cls.getPackageName(), loader.getUnnamedModule()); | ||
| } | ||
| } catch (Throwable t) { | ||
| JpmsHelper.LOGGER.debug( | ||
| "Unable to open package {} to the unnamed module", cls.getPackageName(), t); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
24 changes: 6 additions & 18 deletions
24
...rc/main/java/datadog/trace/instrumentation/httpclient/JpmsInetAddressInstrumentation.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 |
|---|---|---|
| @@ -1,33 +1,21 @@ | ||
| package datadog.trace.instrumentation.httpclient; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
| import static java.util.Collections.singleton; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.environment.JavaVirtualMachine; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.agent.tooling.JavaModuleOpenProvider; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public class JpmsInetAddressInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.HasMethodAdvice, Instrumenter.ForSingleType { | ||
| public class JpmsInetAddressInstrumentation extends InstrumenterModule | ||
| implements JavaModuleOpenProvider { | ||
|
|
||
| public JpmsInetAddressInstrumentation() { | ||
| super("java-net"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled() { | ||
| return super.isEnabled() && JavaVirtualMachine.isJavaVersionAtLeast(9); | ||
| } | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "java.net.InetAddress"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| // it does not work with typeInitializer() | ||
| transformer.applyAdvice(isConstructor(), packageName + ".JpmsInetAddressClearanceAdvice"); | ||
| public Iterable<String> triggerClasses() { | ||
| return singleton("java.net.InetAddress"); | ||
| } | ||
| } |
19 changes: 0 additions & 19 deletions
19
.../main/java11/datadog/trace/instrumentation/httpclient/JpmsInetAddressClearanceAdvice.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.