Skip to content

Commit 5ba267f

Browse files
authored
Improve isolation of embedded JFFI dependency (#7789)
1 parent 46be01c commit 5ba267f

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/DatadogClassLoader.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,28 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
105105
}
106106
}
107107
return cl.loadInstrumentationClass(name, agentCodeSource);
108+
} else if (name.startsWith("com.kenai.jffi")) {
109+
// prefer our embedded JFFI to other versions exposed by the parent class-loader
110+
return loadLocalClass(name, resolve);
108111
} else {
109112
return super.loadClass(name, resolve);
110113
}
111114
}
112115

116+
/** Same as {@link #loadClass(String, boolean)} but it doesn't delegate to the parent. */
117+
private Class<?> loadLocalClass(String name, boolean resolve) throws ClassNotFoundException {
118+
synchronized (getClassLoadingLock(name)) {
119+
Class<?> c = findLoadedClass(name);
120+
if (null == c) {
121+
c = findClass(name);
122+
}
123+
if (resolve) {
124+
resolveClass(c);
125+
}
126+
return c;
127+
}
128+
}
129+
113130
@Override
114131
protected Class<?> findClass(String name) throws ClassNotFoundException {
115132
byte[] buf = loadClassBytes(name);

dd-java-agent/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ ext.generalShadowJarConfig = {
5353
relocate 'net.jpountz', 'datadog.jpountz'
5454
// rewrite dependencies calling Logger.getLogger
5555
relocate 'java.util.logging.Logger', 'datadog.trace.bootstrap.PatchLogger'
56+
// patch JFFI loading mechanism to maintain isolation
57+
exclude '**/com/kenai/jffi/Init.class'
58+
relocate('com.kenai.jffi.Init', 'com.kenai.jffi.PatchInit')
5659

5760
final String projectName = "${project.name}"
5861

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.kenai.jffi;
2+
3+
import com.kenai.jffi.internal.StubLoader;
4+
5+
/** Replacement Init class that loads StubLoader from the same (isolating) class-loader. */
6+
final class PatchInit {
7+
private static volatile boolean loaded;
8+
9+
private PatchInit() {}
10+
11+
static void load() {
12+
if (loaded) {
13+
return;
14+
}
15+
try {
16+
if (StubLoader.isLoaded()) {
17+
loaded = true;
18+
} else {
19+
throw StubLoader.getFailureCause();
20+
}
21+
} catch (UnsatisfiedLinkError e) {
22+
throw e;
23+
} catch (Throwable e) {
24+
throw (UnsatisfiedLinkError) new UnsatisfiedLinkError(e.getLocalizedMessage()).initCause(e);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)