Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ public static void agentmain(String agentArguments, Instrumentation instrumentat
}

private static AgentBuilder createAgentBuilder() throws Exception {
final Junction<TypeDescription> socketType = ElementMatchers.isSubTypeOf(SocketChannel.class)
.or(ElementMatchers.isSubTypeOf(Socket.class));
final Junction<TypeDescription> pathType = ElementMatchers.isSubTypeOf(Files.class);
final Junction<TypeDescription> fileChannelType = ElementMatchers.isSubTypeOf(FileChannel.class);
final Junction<TypeDescription> fileSystemProviderType = ElementMatchers.isSubTypeOf(FileSystemProvider.class);
final Junction<TypeDescription> socketType = ElementMatchers.nameStartsWith(Socket.class.getPackageName() + ".")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timlueg @cwperks I am afraid this slippery road: the JDK evolves, the assumptions invalidate fast. Where the decision which package to take into consideration are coming from?

Please, correct me if I am missing something, but clearly we are excluding sun.security.ssl.SSLSocketImpl (JDK) and any user class that implement SocketChannel or Socket by package filters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree the PR suggestion is not an ideal solution. I selected the JDK packages where I found the relevant Subclasses.
SSLSocketImpl is excluded. But SSLSocketImpl.connect() delegates to java.net.Socket.connect() which is matched. But it is better to still include the package explicitly.

Right, user classes that extend e.g. Socket would no longer be matched unless they delegate like above.

(My understanding is that these Agent matcher(s) cover the most common cases. Because I can imagine a arbitrary user class could for example open a connection without using a Socket subclass.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(My understanding is that these Agent matcher(s) cover the most common cases. Because I can imagine a arbitrary user class could for example open a connection without using a Socket subclass.)

Thanks @timlueg , I think we should be covering any possible scenario (API set is very limited), otherwise the agent is not really serving the purpose

.or(ElementMatchers.nameStartsWith("sun.nio.ch."))
.or(ElementMatchers.nameStartsWith("sun.security.ssl."))
.and(ElementMatchers.isSubTypeOf(SocketChannel.class).or(ElementMatchers.isSubTypeOf(Socket.class)));
final Junction<TypeDescription> pathType = ElementMatchers.named(Files.class.getName());
final Junction<TypeDescription> fileChannelType = ElementMatchers.nameStartsWith(FileChannel.class.getPackageName() + ".")
.or(ElementMatchers.nameStartsWith("sun.nio.ch."))
Comment thread
timlueg marked this conversation as resolved.
.and(ElementMatchers.isSubTypeOf(FileChannel.class));
final Junction<TypeDescription> fileSystemProviderType = ElementMatchers.nameStartsWith("sun.nio.fs.")
.or(ElementMatchers.nameStartsWith("jdk.nio.zipfs."))
.or(ElementMatchers.nameStartsWith("jdk.internal.jrtfs."))
.and(ElementMatchers.isSubTypeOf(FileSystemProvider.class));

final AgentBuilder.Transformer socketTransformer = (b, typeDescription, classLoader, module, pd) -> b.visit(
Advice.to(SocketChannelInterceptor.class)
Expand All @@ -102,13 +109,13 @@ private static AgentBuilder createAgentBuilder() throws Exception {
.transform(socketTransformer)
.type(pathType.or(fileChannelType).or(fileSystemProviderType))
.transform(fileTransformer)
.type(ElementMatchers.is(java.lang.System.class))
.type(ElementMatchers.named(java.lang.System.class.getName()))
.transform(
(b, typeDescription, classLoader, module, pd) -> b.visit(
Advice.to(SystemExitInterceptor.class).on(ElementMatchers.named("exit"))
)
)
.type(ElementMatchers.is(java.lang.Runtime.class))
.type(ElementMatchers.named(java.lang.Runtime.class.getName()))
.transform(
(b, typeDescription, classLoader, module, pd) -> b.visit(
Advice.to(RuntimeHaltInterceptor.class).on(ElementMatchers.named("halt"))
Expand All @@ -117,7 +124,7 @@ private static AgentBuilder createAgentBuilder() throws Exception {

// Only apply the transformation when running on JDK-24 or above
if (Runtime.version().feature() >= 24) {
builder = builder.type(ElementMatchers.is(Subject.class)).transform(subjectTransformer);
builder = builder.type(ElementMatchers.named(Subject.class.getName())).transform(subjectTransformer);
}

return builder;
Expand Down
Loading