Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions field-inject/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ plugins {
}

dependencies {
implementation(libs.asm)
implementation(project(":class-inject"))

testImplementation(sourceSets["glue"].output)
testImplementation(project(":utils"))
testImplementation(project(":class-inject"))
testImplementation(libs.asm.commons)
testImplementation(libs.asm.util)

jmh("com.blogspot.mydailyjava:weak-lock-free:0.18")
jmh(project(":testing"))
jmh(project(":class-inject"))
jmh(libs.asm.commons)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@
import datadog.instrument.glue.ObjectStoreGlue;
import datadog.instrument.testing.TestAgent;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
Expand Down Expand Up @@ -75,7 +72,7 @@ static synchronized String install(String variant) throws Exception {
ClassInjector.injectBootClass(
dispatchClass.replace('/', '.'), dispatcher(variant, dispatchClass));
}
inst.addTransformer(new SelectDispatch(dispatchClass), true);
inst.addTransformer(new FieldInjector.DispatchTransformer(dispatchClass), true);
inst.retransformClasses(ObjectStore.class);
installed = variant;
return dispatchClass;
Expand Down Expand Up @@ -318,51 +315,4 @@ public void visitEnd() {}
writer.visitEnd();
return writer.toByteArray();
}

private static final class SelectDispatch implements ClassFileTransformer {
private final String dispatchClass;

SelectDispatch(String dispatchClass) {
this.dispatchClass = dispatchClass;
}

@Override
@Nullable
public byte[] transform(
@Nullable ClassLoader loader,
@Nullable String className,
@Nullable Class<?> redefined,
@Nullable ProtectionDomain domain,
byte[] bytecode) {
if (redefined == null || !"datadog/instrument/fieldinject/ObjectStore".equals(className)) {
return null;
}
ClassReader reader = new ClassReader(bytecode);
ClassWriter writer = new ClassWriter(reader, 0);
reader.accept(
new ClassVisitor(ASM9, writer) {
@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
// Only instance operations use dispatch; static cleanup stays on the global store.
if ((access & ACC_STATIC) != 0) {
return mv;
}
return new MethodVisitor(ASM9, mv) {
@Override
public void visitMethodInsn(
int opcode, String owner, String name, String descriptor, boolean isInterface) {
if (opcode == INVOKESTATIC && LOCAL_GLOBAL_STORE.equals(owner)) {
owner = dispatchClass;
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
};
}
},
0);
return writer.toByteArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2026-Present Datadog, Inc.
*/

package datadog.instrument.fieldinject;

import static datadog.instrument.glue.ObjectStoreGlue.*;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import static org.objectweb.asm.Opcodes.ASM9;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;

import datadog.instrument.classinject.ClassInjector;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.security.ProtectionDomain;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;

/** Enables field injection for {@link ObjectStore}. */
public final class FieldInjector {

private static final Object enableLock = new Object();
private static boolean enabled;

private FieldInjector() {}

/**
* Enables field injection via {@link Instrumentation}.
*
* @param inst the instrumentation instance
*/
public static void enableFieldInjection(Instrumentation inst) {
synchronized (enableLock) {
if (enabled) {
return;
}

String dispatchClass;
try {
ClassInjector.enableClassInjection(inst);
ClassInjector.injectBootClasses(objectStoreBytecode());
Class.forName(DISPATCH_ID, false, null)
.getMethod("initialize", Lookup.class)
.invoke(null, MethodHandles.lookup().in(GlobalObjectStore.class));
dispatchClass = DISPATCH_ID.replace('.', '/');
} catch (Throwable ignore) {
dispatchClass = "datadog/instrument/fieldinject/ObjectStoreDispatch";
}

// redirect object-store requests to the available dispatcher
DispatchTransformer transformer = new DispatchTransformer(dispatchClass);
try {
inst.addTransformer(transformer, true);
inst.retransformClasses(ObjectStore.class);
enabled = true;
} catch (Throwable ignore) {
inst.removeTransformer(transformer);
}
}
}

private static Map<String, byte[]> objectStoreBytecode() {
Map<String, byte[]> bytecode = new LinkedHashMap<>();
addGlue(bytecode, KEY_ID, KEY);
addGlue(bytecode, DISPATCH_ID, DISPATCH);
return bytecode;
}

private static void addGlue(Map<String, byte[]> bytecode, String name, String glue) {
bytecode.put(name, glue.getBytes(ISO_8859_1));
}

static final class DispatchTransformer implements ClassFileTransformer {
private static final String OBJECT_STORE = "datadog/instrument/fieldinject/ObjectStore";

private final String dispatchClass;

DispatchTransformer(String dispatchClass) {
this.dispatchClass = dispatchClass;
}

@Override
@Nullable
public byte[] transform(
@Nullable ClassLoader loader,
@Nullable String className,
@Nullable Class<?> classBeingRedefined,
@Nullable ProtectionDomain protectionDomain,
byte[] bytecode) {
if (classBeingRedefined == null || !OBJECT_STORE.equals(className)) {
return null;
}
ClassReader cr = new ClassReader(bytecode);
ClassWriter cw = new ClassWriter(cr, 0);
cr.accept(new DispatchPatch(cw, dispatchClass), 0);
return cw.toByteArray();
}
}

static final class DispatchPatch extends ClassVisitor {
private static final String GLOBAL_OBJECT_STORE =
"datadog/instrument/fieldinject/GlobalObjectStore";

private final String dispatchClass;

DispatchPatch(ClassVisitor cv, String dispatchClass) {
super(ASM9, cv);
this.dispatchClass = dispatchClass;
}

@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
// Static cleanup stays with the global store; only instance accessors use dispatch.
if ((access & ACC_STATIC) != 0) {
return mv;
}
return new MethodVisitor(ASM9, mv) {
@Override
public void visitMethodInsn(
int opcode, String owner, String name, String descriptor, boolean isInterface) {
if (opcode == INVOKESTATIC && GLOBAL_OBJECT_STORE.equals(owner)) {
owner = dispatchClass;
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
};
}
}
}
Loading