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
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ private void extractSymbolForLoadedClasses(SymDBReport symDBReport) {
try {
jarPath = JarScanner.extractJarPath(clazz, symDBReport);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
LOGGER.debug("Failed to extract jar path for class {}", clazz.getTypeName(), e);
continue;
}
if (jarPath == null) {
continue;
Expand All @@ -152,7 +153,11 @@ private void extractSymbolForLoadedClasses(SymDBReport symDBReport) {
symDBReport.addMissingJar(jarPath.toString());
continue;
}
symbolAggregator.scanJar(symDBReport, jarPath, baos, buffer);
try {
symbolAggregator.scanJar(symDBReport, jarPath, baos, buffer);
} catch (Exception ex) {
LOGGER.debug("Failed to scan jar {}", jarPath, ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ void scanQueuedJars(SymbolAggregator symbolAggregator) {
while (!jarsToScanQueue.isEmpty()) {
String jarPath = jarsToScanQueue.poll();
LOGGER.debug("Scanning queued jar: {}", jarPath);
scanJar(SymDBReport.NO_OP, Paths.get(jarPath), baos, buffer);
try {
scanJar(SymDBReport.NO_OP, Paths.get(jarPath), baos, buffer);
} catch (Exception ex) {
Copy link
Member

Choose a reason for hiding this comment

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

I never know, are the Exceptions you're trying to catch here Exceptions or Errors? 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Errors are no caught here. you need Throwable to catch them, but it is generally unwise

LOGGER.debug("Failed to scan jar {}", jarPath, ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Collections;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -111,11 +113,7 @@ public void noIncludesFilterOutDatadogClass() {

@Test
public void parseLoadedClass() throws ClassNotFoundException, IOException {
final String CLASS_NAME = "com.datadog.debugger.symbol.SymbolExtraction01";
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
URL jarUrl = new URL("jar:file:" + jarFileUrl.getFile() + "!/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarUrl}, null);
Class<?> testClass = urlClassLoader.loadClass(CLASS_NAME);
Class<?> testClass = loadSymbolClassFromJar();
when(instr.getAllLoadedClasses()).thenReturn(new Class[] {testClass});
when(config.getThirdPartyIncludes())
.thenReturn(
Expand All @@ -130,14 +128,50 @@ public void parseLoadedClass() throws ClassNotFoundException, IOException {
verify(instr).addTransformer(any(SymbolExtractionTransformer.class));
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(symbolAggregator, times(2))
.parseClass(any(), captor.capture(), any(), eq(jarFileUrl.getFile()));
.parseClass(
any(),
captor.capture(),
any(),
eq(getClass().getResource("/debugger-symbol.jar").getFile()));
assertEquals(
"com/datadog/debugger/symbol/SymbolExtraction01.class", captor.getAllValues().get(0));
assertEquals(
"BOOT-INF/classes/org/springframework/samples/petclinic/vet/VetController.class",
captor.getAllValues().get(1));
}

@Test
public void processCorruptedJar() throws ClassNotFoundException, MalformedURLException {
Class<?> testClass = loadSymbolClassFromJar();
when(instr.getAllLoadedClasses())
.thenReturn(new Class[] {SymDBEnablementTest.class, testClass});
ClassNameFiltering classNameFiltering = ClassNameFiltering.allowAll();
SymbolAggregator symbolAggregatorMock = mock(SymbolAggregator.class);
doAnswer(
invocation -> {
Path arg = invocation.getArgument(1, Path.class);
if (arg.toString().endsWith("/debugger-symbol.jar")) {
return null;
}
throw new IOException("Corrupted jar");
})
.when(symbolAggregatorMock)
.scanJar(any(), any(), any(), any());
SymDBEnablement symDBEnablement =
new SymDBEnablement(instr, config, symbolAggregatorMock, classNameFiltering);
symDBEnablement.startSymbolExtraction();
verify(symbolAggregatorMock, times(2)).scanJar(any(), any(), any(), any());
}

private Class<?> loadSymbolClassFromJar() throws MalformedURLException, ClassNotFoundException {
final String CLASS_NAME = "com.datadog.debugger.symbol.SymbolExtraction01";
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
URL jarUrl = new URL("jar:file:" + jarFileUrl.getFile() + "!/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] {jarUrl}, null);
Class<?> testClass = urlClassLoader.loadClass(CLASS_NAME);
return testClass;
}

@Test
public void parseLoadedClassFromDirectory()
throws ClassNotFoundException, IOException, URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ void testScanQueuedJars() {
captor.getAllValues().get(2));
}

@Test
void testScanQueuedCorruptedJars() {
SymbolSink symbolSink = mock(SymbolSink.class);
SymbolAggregator symbolAggregator =
spy(new SymbolAggregator(ClassNameFiltering.allowAll(), emptyList(), symbolSink, 1));
// add first a corrupted jar
URL corruptedUrl = getClass().getResource("/com/datadog/debugger/classfiles/CommandLine.class");
CodeSource corruptedCodeSource =
new CodeSource(corruptedUrl, (java.security.cert.Certificate[]) null);
ProtectionDomain corruptedProtectionDomain = new ProtectionDomain(corruptedCodeSource, null);
symbolAggregator.parseClass(null, null, corruptedProtectionDomain);
// add second a clean jar
URL jarFileUrl = getClass().getResource("/debugger-symbol.jar");
CodeSource codeSource = new CodeSource(jarFileUrl, (java.security.cert.Certificate[]) null);
ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, null);
symbolAggregator.parseClass(null, null, protectionDomain);
symbolAggregator.scanQueuedJars(null);
// clean jar should have been processed
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(symbolAggregator, atLeastOnce())
.parseClass(any(), captor.capture(), any(), eq(jarFileUrl.getFile()));
// captor.getAllValues().get(0) is the first argument of the first invocation of parseClass with
// null
assertEquals(
"com/datadog/debugger/symbol/SymbolExtraction01.class", captor.getAllValues().get(1));
assertEquals(
"BOOT-INF/classes/org/springframework/samples/petclinic/vet/VetController.class",
captor.getAllValues().get(2));
}

@Test
@DisabledIf(
value = "datadog.environment.JavaVirtualMachine#isJ9",
Expand Down
Loading