diff --git a/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/CapturedContextInstrumentor.java b/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/CapturedContextInstrumentor.java index dc7d48203b8..3c17eb278ab 100644 --- a/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/CapturedContextInstrumentor.java +++ b/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/CapturedContextInstrumentor.java @@ -1,6 +1,5 @@ package com.datadog.debugger.instrumentation; -import static com.datadog.debugger.instrumentation.ASMHelper.extractSuperClass; import static com.datadog.debugger.instrumentation.ASMHelper.getStatic; import static com.datadog.debugger.instrumentation.ASMHelper.invokeConstructor; import static com.datadog.debugger.instrumentation.ASMHelper.invokeStatic; @@ -1136,51 +1135,13 @@ private static List extractStaticFields( } } } - if (!Config.get().isDynamicInstrumentationInstrumentTheWorld()) { - // Collects inherited static fields only if the ITW mode is not enabled - // because it can lead to LinkageError: attempted duplicate class definition - // for example, when a probe is located in method overridden in enum element - addInheritedStaticFields(classNode, classLoader, limits, results, fieldCount); - } + // Collecting inherited static fields is problematic because it some cases can lead to + // LinkageError: attempted duplicate class definition + // as we force to load a class to get the static fields in a different order than the JVM + // for example, when a probe is located in method overridden in enum element return results; } - private static void addInheritedStaticFields( - ClassNode classNode, - ClassLoader classLoader, - Limits limits, - List results, - int fieldCount) { - String superClassName = extractSuperClass(classNode); - while (!superClassName.equals(Object.class.getTypeName())) { - Class clazz; - try { - clazz = Class.forName(superClassName, false, classLoader); - } catch (ClassNotFoundException ex) { - break; - } - try { - for (Field field : clazz.getDeclaredFields()) { - if (isStaticField(field) && !isFinalField(field)) { - String desc = Type.getDescriptor(field.getType()); - FieldNode fieldNode = - new FieldNode(field.getModifiers(), field.getName(), desc, null, field); - results.add(fieldNode); - LOGGER.debug("Adding static inherited field {}", fieldNode.name); - fieldCount++; - if (fieldCount > limits.maxFieldCount) { - return; - } - } - } - } catch (ClassCircularityError ex) { - break; - } - clazz = clazz.getSuperclass(); - superClassName = clazz.getTypeName(); - } - } - private int declareContextVar(InsnList insnList) { int var = newVar(CAPTURED_CONTEXT_TYPE); getStatic(insnList, CAPTURED_CONTEXT_TYPE, "EMPTY_CAPTURING_CONTEXT"); diff --git a/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java b/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java index bed200dfb13..75f07473d4e 100644 --- a/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java +++ b/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java @@ -939,27 +939,6 @@ public void fieldExtractorNotAccessible() throws IOException, URISyntaxException "Field is not accessible: module java.base does not opens/exports to the current module"); } - @Test - @EnabledForJreRange(min = JRE.JAVA_17) - public void staticFieldExtractorNotAccessible() throws IOException, URISyntaxException { - final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot30"; - LogProbe logProbe = - createMethodProbe(PROBE_ID, CLASS_NAME + "$MyHttpURLConnection", "process", "()"); - TestSnapshotListener listener = installProbes(logProbe); - Class testClass = compileAndLoadClass(CLASS_NAME); - int result = Reflect.onClass(testClass).call("main", "static").get(); - assertEquals(42, result); - Snapshot snapshot = assertOneSnapshot(listener); - assertCaptureStaticFieldsNotCaptured( - snapshot.getCaptures().getReturn(), - "followRedirects", - "Field is not accessible: module java.base does not opens/exports to the current module"); - assertCaptureStaticFieldsNotCaptured( - snapshot.getCaptures().getReturn(), - "factory", - "Field is not accessible: module java.base does not opens/exports to the current module"); - } - @Test public void uncaughtException() throws IOException, URISyntaxException { final String CLASS_NAME = "CapturedSnapshot05"; @@ -1611,12 +1590,9 @@ public void staticInheritedFields() throws IOException, URISyntaxException { Snapshot snapshot = assertOneSnapshot(listener); Map staticFields = snapshot.getCaptures().getReturn().getStaticFields(); - assertEquals(7, staticFields.size()); + // inherited static fields are not collected + assertEquals(2, staticFields.size()); assertEquals("barfoo", MoshiSnapshotTestHelper.getValue(staticFields.get("strValue"))); - assertEquals("48", MoshiSnapshotTestHelper.getValue(staticFields.get("intValue"))); - assertEquals("6.28", MoshiSnapshotTestHelper.getValue(staticFields.get("doubleValue"))); - assertEquals("[1, 2, 3, 4]", MoshiSnapshotTestHelper.getValue(staticFields.get("longValues"))); - assertEquals("[foo, bar]", MoshiSnapshotTestHelper.getValue(staticFields.get("strValues"))); } @Test