Skip to content

Add scope filtering for symbol extraction #8676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import com.datadog.debugger.sink.ProbeStatusSink;
import com.datadog.debugger.sink.SnapshotSink;
import com.datadog.debugger.sink.SymbolSink;
import com.datadog.debugger.symbol.AvroFilter;
import com.datadog.debugger.symbol.ProtoFilter;
import com.datadog.debugger.symbol.ScopeFilter;
import com.datadog.debugger.symbol.SymDBEnablement;
import com.datadog.debugger.symbol.SymbolAggregator;
import com.datadog.debugger.symbol.WireFilter;
import com.datadog.debugger.uploader.BatchUploader;
import com.datadog.debugger.util.ClassNameFiltering;
import com.datadog.debugger.util.DebuggerMetrics;
Expand Down Expand Up @@ -41,7 +45,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -155,9 +161,14 @@ public static void startDynamicInstrumentation() {
if (configurationPoller != null) {
if (config.isSymbolDatabaseEnabled()) {
initClassNameFilter();
List<ScopeFilter> scopeFilters =
Arrays.asList(new AvroFilter(), new ProtoFilter(), new WireFilter());
SymbolAggregator symbolAggregator =
new SymbolAggregator(
classNameFilter, sink.getSymbolSink(), config.getSymbolDatabaseFlushThreshold());
classNameFilter,
scopeFilters,
sink.getSymbolSink(),
config.getSymbolDatabaseFlushThreshold());
symbolAggregator.start();
symDBEnablement =
new SymDBEnablement(instrumentation, config, symbolAggregator, classNameFilter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.datadog.debugger.symbol;

public class AvroFilter implements ScopeFilter {
@Override
public boolean filterOut(Scope scope) {
if (scope == null) {
return false;
}
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics();
if (languageSpecifics != null) {
String superClass = languageSpecifics.getSuperClass();
// Allow Avro data classes that extend SpecificRecordBase.
if ("org.apache.avro.specific.SpecificRecordBase".equals(superClass)) {
return false;
}
}
// Filter out classes that appear to be just schema wrappers.
if (scope.getScopeType() == ScopeType.CLASS
&& scope.getSymbols() != null
&& scope.getSymbols().stream()
.anyMatch(
it ->
it.getSymbolType() == SymbolType.STATIC_FIELD
&& "SCHEMA$".equals(it.getName())
&& it.getType() != null
&& it.getType().contains("org.apache.avro.Schema"))) {
return true;
}
// Otherwise, do not filter.
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.datadog.debugger.symbol;

import java.util.List;

public class ProtoFilter implements ScopeFilter {
@Override
public boolean filterOut(Scope scope) {
if (scope == null) {
return false;
}
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics();
if (languageSpecifics != null) {
List<String> interfaces = languageSpecifics.getInterfaces();
if (interfaces != null) {
if (interfaces.contains("com.google.protobuf.MessageOrBuilder")) {
// MessageOrBuilder is an interface implemented by both message classes and their
// builders.
// Scopes implementing this interface are filtered out because they do not represent
// concrete data structures but rather interfaces for accessing or building messages.
return true;
}
}
String superClass = languageSpecifics.getSuperClass();
if ("com.google.protobuf.AbstractParser".equals(superClass)) {
// AbstractParser is a base class for parsing protobuf messages. Scopes with this super
// class are filtered out because they are utility classes for parsing and do not contain
// actual data fields.
return true;
}
if ("com.google.protobuf.GeneratedMessageV3$Builder".equals(superClass)) {
// GeneratedMessageV3$Builder is a builder class for constructing GeneratedMessageV3
// instances. These scopes are filtered out because they are used for building messages and
// do not represent the final data structure.
return true;
}
}
// If none of the above matched, see if the class has a proto descriptor field. This is the case
// for wrapper
// classes (`OuterClass`) and `Enum` classes. They contain metadata, not data.
if (hasProtoDescriptorField(scope)) {
return true;
}
// Probably no protobuf, pass
return false;
}

private boolean hasProtoDescriptorField(Scope scope) {
return scope.getScopeType() == ScopeType.CLASS
&& scope.getSymbols() != null
&& scope.getSymbols().stream()
.anyMatch(
it ->
it.getSymbolType() == SymbolType.STATIC_FIELD
&& it.getType() != null
&& it.getType().contains("com.google.protobuf.Descriptors"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.datadog.debugger.symbol;

public interface ScopeFilter {
/** returns true if the scope should be excluded */
boolean filterOut(Scope scope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SymbolAggregator {
private static final int CLASSFILE_BUFFER_SIZE = 8192;

private final DebuggerContext.ClassNameFilter classNameFilter;
private final List<ScopeFilter> scopeFilters;
private final SymbolSink sink;
private final int symbolFlushThreshold;
private final Map<String, Scope> jarScopesByName = new HashMap<>();
Expand All @@ -51,8 +52,12 @@ public class SymbolAggregator {
private final Set<String> alreadyScannedJars = ConcurrentHashMap.newKeySet();

public SymbolAggregator(
DebuggerContext.ClassNameFilter classNameFilter, SymbolSink sink, int symbolFlushThreshold) {
DebuggerContext.ClassNameFilter classNameFilter,
List<ScopeFilter> scopeFilters,
SymbolSink sink,
int symbolFlushThreshold) {
this.classNameFilter = classNameFilter;
this.scopeFilters = scopeFilters;
this.sink = sink;
this.symbolFlushThreshold = symbolFlushThreshold;
}
Expand Down Expand Up @@ -119,10 +124,18 @@ public void parseClass(
}
LOGGER.debug("Extracting Symbols from: {}, located in: {}", className, jarName);
Scope jarScope = SymbolExtractor.extract(classfileBuffer, jarName);
jarScope = applyFilters(jarScope);
addJarScope(jarScope, false);
symDBReport.incClassCount(jarName);
}

private Scope applyFilters(Scope jarScope) {
for (ScopeFilter filter : scopeFilters) {
jarScope.getScopes().removeIf(filter::filterOut);
}
return jarScope;
}

private void flushRemainingScopes(SymbolAggregator symbolAggregator) {
synchronized (jarScopeLock) {
if (jarScopesByName.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.datadog.debugger.symbol;

import java.util.List;

public class WireFilter implements ScopeFilter {
@Override
public boolean filterOut(Scope scope) {
// Filter out classes generated by Square Wire: https://square.github.io/wire/
if (scope == null) {
return false;
}
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics();
if (languageSpecifics == null) {
return false;
}
List<String> interfaces = languageSpecifics.getInterfaces();
if (interfaces != null) {
if (interfaces.contains("com.squareup.wire.Message")) {
// Pass-through for Message since it contains data
return false;
}
if (interfaces.stream().anyMatch(it -> it.startsWith("com.squareup.wire"))) {
return true;
}
}
String superClass = languageSpecifics.getSuperClass();
if (superClass != null) {
if (superClass.startsWith("com.squareup.wire")) {
return true;
}
}
// Probably no protobuf, pass
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.datadog.debugger.symbol;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class AvroFilterTest {
@Test
void filterOut() {
AvroFilter avroFilter = new AvroFilter();
assertFalse(avroFilter.filterOut(null));
Scope scope = Scope.builder(ScopeType.CLASS, "", 0, 0).build();
assertFalse(avroFilter.filterOut(scope));
scope = Scope.builder(ScopeType.CLASS, "", 0, 0).name("org.apache.avro.MyClass").build();
assertFalse(avroFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.languageSpecifics(
new LanguageSpecifics.Builder()
.superClass("org.apache.avro.specific.SpecificRecordBase")
.build())
.build();
assertFalse(avroFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.symbols(
asList(
new Symbol(
SymbolType.STATIC_FIELD, "SCHEMA$", 0, "org.apache.avro.Schema", null)))
.build();
assertTrue(avroFilter.filterOut(scope));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.datadog.debugger.symbol;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class ProtoFilterTest {
@Test
Copy link
Member

Choose a reason for hiding this comment

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

Need to add a test that we don't filter out non-proto scopes. This is true for the other tests as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

void filterOut() {
ProtoFilter protoFilter = new ProtoFilter();
assertFalse(protoFilter.filterOut(null));
Scope scope = Scope.builder(ScopeType.CLASS, "", 0, 0).build();
assertFalse(protoFilter.filterOut(scope));
scope = Scope.builder(ScopeType.CLASS, "", 0, 0).name("com.google.protobuf.MyClass").build();
assertFalse(protoFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.languageSpecifics(
new LanguageSpecifics.Builder()
.addInterfaces(asList("com.google.protobuf.MessageOrBuilder"))
.build())
.build();
assertTrue(protoFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.languageSpecifics(
new LanguageSpecifics.Builder()
.superClass("com.google.protobuf.AbstractParser")
.build())
.build();
assertTrue(protoFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.languageSpecifics(
new LanguageSpecifics.Builder()
.superClass("com.google.protobuf.GeneratedMessageV3$Builder")
.build())
.build();
assertTrue(protoFilter.filterOut(scope));
scope =
Scope.builder(ScopeType.CLASS, "", 0, 0)
.symbols(
asList(
new Symbol(
SymbolType.STATIC_FIELD,
"SCHEMA$",
0,
"com.google.protobuf.Descriptors",
null)))
.build();
assertTrue(protoFilter.filterOut(scope));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datadog.debugger.symbol;

import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -63,7 +64,7 @@ public void enableDisableSymDBThroughRC() throws Exception {
new SymDBEnablement(
instr,
config,
new SymbolAggregator(classNameFiltering, symbolSink, 1),
new SymbolAggregator(classNameFiltering, emptyList(), symbolSink, 1),
classNameFiltering);
symDBEnablement.accept(ParsedConfigKey.parse(CONFIG_KEY), UPlOAD_SYMBOL_TRUE, null);
waitForUpload(symDBEnablement);
Expand All @@ -79,7 +80,7 @@ public void removeSymDBConfig() throws Exception {
new SymDBEnablement(
instr,
config,
new SymbolAggregator(classNameFiltering, symbolSink, 1),
new SymbolAggregator(classNameFiltering, emptyList(), symbolSink, 1),
classNameFiltering);
symDBEnablement.accept(ParsedConfigKey.parse(CONFIG_KEY), UPlOAD_SYMBOL_TRUE, null);
waitForUpload(symDBEnablement);
Expand All @@ -96,7 +97,7 @@ public void noIncludesFilterOutDatadogClass() {
new SymDBEnablement(
instr,
config,
new SymbolAggregator(classNameFiltering, symbolSink, 1),
new SymbolAggregator(classNameFiltering, emptyList(), symbolSink, 1),
classNameFiltering);
symDBEnablement.startSymbolExtraction();
ArgumentCaptor<SymbolExtractionTransformer> captor =
Expand All @@ -122,7 +123,7 @@ public void parseLoadedClass() throws ClassNotFoundException, IOException {
.collect(Collectors.toSet()));
ClassNameFiltering classNameFiltering = ClassNameFiltering.allowAll();
SymbolAggregator symbolAggregator =
spy(new SymbolAggregator(classNameFiltering, symbolSink, 1));
spy(new SymbolAggregator(classNameFiltering, emptyList(), symbolSink, 1));
SymDBEnablement symDBEnablement =
new SymDBEnablement(instr, config, symbolAggregator, classNameFiltering);
symDBEnablement.startSymbolExtraction();
Expand Down Expand Up @@ -150,7 +151,7 @@ public void parseLoadedClassFromDirectory()
.collect(Collectors.toSet()));
ClassNameFiltering classNameFiltering = ClassNameFiltering.allowAll();
SymbolAggregator symbolAggregator =
spy(new SymbolAggregator(classNameFiltering, symbolSink, 1));
spy(new SymbolAggregator(classNameFiltering, emptyList(), symbolSink, 1));
SymDBEnablement symDBEnablement =
new SymDBEnablement(instr, config, symbolAggregator, classNameFiltering);
symDBEnablement.startSymbolExtraction();
Expand All @@ -171,7 +172,8 @@ public void noDuplicateSymbolExtraction() {
Collections.singleton("org.springframework."),
Collections.singleton("com.datadog.debugger."),
Collections.emptySet());
SymbolAggregator symbolAggregator = new SymbolAggregator(classNameFiltering, mockSymbolSink, 1);
SymbolAggregator symbolAggregator =
new SymbolAggregator(classNameFiltering, emptyList(), mockSymbolSink, 1);
SymDBEnablement symDBEnablement =
new SymDBEnablement(instr, config, symbolAggregator, classNameFiltering);
doAnswer(
Expand Down
Loading
Loading