Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.SourceCompiler;

public class CapturedSnapshotTest extends CapturingTestBase {
private static final Logger log = LoggerFactory.getLogger(CapturedSnapshotTest.class);

private static final ProbeId PROBE_ID1 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f6", 0);
private static final ProbeId PROBE_ID2 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f7", 0);
private static final ProbeId PROBE_ID3 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f8", 0);
Expand Down Expand Up @@ -2845,11 +2849,20 @@ private TestSnapshotListener setupInstrumentTheWorldTransformer(
return listener;
}

// TODO: JEP 500 - avoid mutating final fields
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Is it possible to log such usage ?

Copy link
Contributor

@sarahchen6 sarahchen6 Jan 27, 2026

Choose a reason for hiding this comment

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

Hm good idea. I've already started trying to get rid of mutations as a whole (#10440), but we could log when final fields are detected in the meantime.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know if there's a cleaner way to approach the logging 😅 . Otherwise, the next steps after this PR are to rework the three cases of final field mutation

private void setCorrelationSingleton(Object instance) {
Class<?> singletonClass = CorrelationAccess.class.getDeclaredClasses()[0];
try {
Field instanceField = singletonClass.getDeclaredField("INSTANCE");
instanceField.setAccessible(true);
boolean isFinal = Modifier.isFinal(instanceField.getModifiers());
if (isFinal) {
log.warn(
"JEP 500: Final field 'INSTANCE' in class '{}' is being mutated. This will soon be disallowed. Field type: {}. Field declaring class: {}.",
singletonClass.getName(),
instanceField.getType().getName(),
instanceField.getDeclaringClass().getName());
}
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(instanceField, instanceField.getModifiers() & ~Modifier.FINAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
import datadog.trace.api.gateway.RequestContext;
import datadog.trace.api.gateway.RequestContextSlot;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.reflect.Field;
import java.util.regex.Pattern;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

/** See https://github.com/akka/akka-http/issues/4304 */
/**
* See <a href="https://github.com/akka/akka-http/issues/4304">Duplicated 100 responses if there is
* an exception thrown by the unmarshaller</a>
*/
@AutoService(InstrumenterModule.class)
public class Bug4304Instrumentation extends InstrumenterModule.AppSec
implements Instrumenter.ForTypeHierarchy,
Expand Down Expand Up @@ -91,6 +95,11 @@ public void methodAdvice(MethodTransformer transformer) {
}

static class GraphStageLogicAdvice {
// Field::set() is forbidden because it may be used to mutate final fields, disallowed by
// https://openjdk.org/jeps/500.
// However, in this case the method is called on a non-final field, so it is safe. See
// https://github.com/akka/akka-http/blob/8fb19fce3548c3bfa1e8ebcb1115be29f342df69/akka-http-core/src/main/scala/akka/http/impl/engine/server/HttpServerBluePrint.scala#L588
@SuppressForbidden
@Advice.OnMethodExit(suppress = Throwable.class)
static void after(@Advice.This GraphStageLogic thiz)
throws NoSuchFieldException, IllegalAccessException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.bytebuddy.asm.Advice;
import org.slf4j.LoggerFactory;
import sbt.testing.TaskDef;
import weaver.framework.SuiteEvent;

Expand Down Expand Up @@ -42,12 +45,27 @@ public void methodAdvice(MethodTransformer transformer) {
}

public static class SbtTaskCreationAdvice {
// TODO: JEP 500 - avoid mutating final fields
@SuppressForbidden
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onTaskCreation(
@Advice.This Object sbtTask, @Advice.FieldValue("taskDef") TaskDef taskDef) {
try {
Field queueField = sbtTask.getClass().getDeclaredField("queue");
queueField.setAccessible(true);
boolean isFinal = Modifier.isFinal(queueField.getModifiers());
if (isFinal) {
try {
LoggerFactory.getLogger("datadog.trace.instrumentation.weaver.WeaverInstrumentation")
.warn(
"JEP 500: Final field 'queue' in class '{}' is being mutated. This will soon be disallowed. Field type: {}. Field declaring class: {}.",
sbtTask.getClass().getName(),
queueField.getType().getName(),
queueField.getDeclaringClass().getName());
} catch (Throwable ignored) {
// Ignore logging errors to avoid failing instrumentation
}
}
Object queue = queueField.get(sbtTask);
if (queue instanceof ConcurrentLinkedQueue) {
// disney's implementation (0.8.4+) uses a ConcurrentLinkedQueue for the field
Expand Down
17 changes: 15 additions & 2 deletions gradle/forbiddenApiFilters/main.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# loads and instantiates a class which may be inefficient depending on context
java.lang.Class#forName(java.lang.String)

# String methods which uses regexes for matching
# String methods which use regexes for matching
java.lang.String#split(java.lang.String)
java.lang.String#split(java.lang.String,int)
java.lang.String#replaceAll(java.lang.String,java.lang.String)
Expand Down Expand Up @@ -34,6 +34,19 @@ java.lang.System#err
java.lang.System#getenv()
java.lang.System#getenv(java.lang.String)

#Use jdk LongAdder
# use jdk LongAdder
@defaultMessage use LongAdder instead of the legacy jctools FixedSizeStripedLongCounter
org.jctools.counters.FixedSizeStripedLongCounter

# avoid methods that mutate final fields, as this will soon be disallowed. For more details: https://openjdk.org/jeps/500.
@defaultMessage Avoid mutating final fields (e.g. with methods such as Field::set and MethodHandles.Lookup::unreflectSetter). If the field is not final, override with @SuppressForbidden.
java.lang.reflect.Field#set(java.lang.Object,java.lang.Object)
java.lang.reflect.Field#setBoolean(java.lang.Object,boolean)
java.lang.reflect.Field#setByte(java.lang.Object,byte)
java.lang.reflect.Field#setChar(java.lang.Object,char)
java.lang.reflect.Field#setShort(java.lang.Object,short)
java.lang.reflect.Field#setInt(java.lang.Object,int)
java.lang.reflect.Field#setLong(java.lang.Object,long)
java.lang.reflect.Field#setFloat(java.lang.Object,float)
java.lang.reflect.Field#setDouble(java.lang.Object,double)
java.lang.invoke.MethodHandles.Lookup#unreflectSetter(java.lang.reflect.Field)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import datadog.trace.api.iast.sink.XContentTypeModule;
import datadog.trace.api.iast.sink.XPathInjectionModule;
import datadog.trace.api.iast.sink.XssModule;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -124,6 +125,10 @@ private static <M extends IastModule> M get(final Field field) {
}
}

// Field::set() is forbidden because it may be used to mutate final fields, disallowed by
// https://openjdk.org/jeps/500.
// However, in this case the method is called on a non-final field, so it is safe.
@SuppressForbidden
private static void set(final Field field, final IastModule module) {
try {
field.set(null, module);
Expand Down
12 changes: 12 additions & 0 deletions internal-api/src/main/java/datadog/trace/util/UnsafeUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.util;

import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.slf4j.Logger;
Expand Down Expand Up @@ -57,13 +58,24 @@ public static <T> T tryShallowClone(T original) {
}
}

// TODO: JEP 500 - avoid mutating final fields
@SuppressForbidden
private static void cloneFields(Class<?> clazz, Object original, Object clone) throws Exception {
for (Field field : clazz.getDeclaredFields()) {
if ((field.getModifiers() & Modifier.STATIC) != 0) {
continue;
}
field.setAccessible(true);
Object fieldValue = field.get(original);
boolean isFinal = Modifier.isFinal(field.getModifiers());
if (isFinal) {
log.warn(
"JEP 500: Final field '{}' in cloned object class '{}' is being mutated. This will soon be disallowed. Field type: {}. Field declaring class: {}.",
field.getName(),
clone.getClass().getName(),
field.getType().getName(),
field.getDeclaringClass().getName());
}
field.set(clone, fieldValue);
}
}
Expand Down