-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
685 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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 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
...ntelemetry/javaagent/instrumentation/internal/classloader/DefineClassInstrumentation.java
This file contains 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 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper; | ||
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler.DefineClassContext; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.nio.ByteBuffer; | ||
import java.security.ProtectionDomain; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class DefineClassInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("java.lang.ClassLoader"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("defineClass") | ||
.and( | ||
takesArguments( | ||
String.class, byte[].class, int.class, int.class, ProtectionDomain.class)), | ||
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("defineClass") | ||
.and(takesArguments(String.class, ByteBuffer.class, ProtectionDomain.class)), | ||
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice2"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class DefineClassAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static DefineClassContext onEnter( | ||
@Advice.This ClassLoader classLoader, | ||
@Advice.Argument(0) String className, | ||
@Advice.Argument(1) byte[] classBytes, | ||
@Advice.Argument(2) int offset, | ||
@Advice.Argument(3) int length) { | ||
return DefineClassHelper.beforeDefineClass( | ||
classLoader, className, classBytes, offset, length); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Enter DefineClassContext context) { | ||
DefineClassHelper.afterDefineClass(context); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class DefineClassAdvice2 { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static DefineClassContext onEnter( | ||
@Advice.This ClassLoader classLoader, | ||
@Advice.Argument(0) String className, | ||
@Advice.Argument(1) ByteBuffer classBytes) { | ||
return DefineClassHelper.beforeDefineClass(classLoader, className, classBytes); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Enter DefineClassContext context) { | ||
DefineClassHelper.afterDefineClass(context); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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
68 changes: 68 additions & 0 deletions
68
...agent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/DefineClassHelper.java
This file contains 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,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class DefineClassHelper { | ||
|
||
/** Helper class for {@code ClassLoader.defineClass} callbacks. */ | ||
public interface Handler { | ||
DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length); | ||
|
||
void afterDefineClass(DefineClassContext context); | ||
|
||
/** Context returned from {@code beforeDefineClass} and passed to {@code afterDefineClass}. */ | ||
interface DefineClassContext { | ||
void exit(); | ||
} | ||
} | ||
|
||
private static volatile Handler handler; | ||
|
||
public static Handler.DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length) { | ||
return handler.beforeDefineClass(classLoader, className, classBytes, offset, length); | ||
} | ||
|
||
public static Handler.DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, ByteBuffer byteBuffer) { | ||
// see how ClassLoader handles ByteBuffer | ||
// https:/openjdk/jdk11u/blob/487c3344fee3502b4843e7e11acceb77ad16100c/src/java.base/share/classes/java/lang/ClassLoader.java#L1095 | ||
int length = byteBuffer.remaining(); | ||
if (byteBuffer.hasArray()) { | ||
return beforeDefineClass( | ||
classLoader, | ||
className, | ||
byteBuffer.array(), | ||
byteBuffer.position() + byteBuffer.arrayOffset(), | ||
length); | ||
} else { | ||
byte[] classBytes = new byte[length]; | ||
byteBuffer.duplicate().get(classBytes); | ||
return beforeDefineClass(classLoader, className, classBytes, 0, length); | ||
} | ||
} | ||
|
||
public static void afterDefineClass(Handler.DefineClassContext context) { | ||
handler.afterDefineClass(context); | ||
} | ||
|
||
/** | ||
* Sets the {@link Handler} with callbacks to execute when {@code ClassLoader.defineClass} is | ||
* called. | ||
*/ | ||
public static void internalSetHandler(Handler handler) { | ||
if (DefineClassHelper.handler != null) { | ||
// Only possible by misuse of this API, just ignore. | ||
return; | ||
} | ||
DefineClassHelper.handler = handler; | ||
} | ||
|
||
private DefineClassHelper() {} | ||
} |
This file contains 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.