aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/com
diff options
context:
space:
mode:
authorNorbert Schneider <norbert.schneider@code-intelligence.com>2022-03-18 15:26:08 +0100
committerNorbert Schneider <mail@bertschneider.de>2022-03-18 16:46:22 +0100
commit8a30d4301e01f44756ad0d8456f4bd74e2b72b4c (patch)
treebffa369ea77f1c62df3c9eda71a132b69b08922b /agent/src/main/java/com
parent297b9203d91c2543f4180386fd6c1d4200def0ba (diff)
downloadjazzer-api-8a30d4301e01f44756ad0d8456f4bd74e2b72b4c.tar.gz
Extract native library loading to dedicated class
Native library loading has a few pitfalls. This PR extracts the code out of RuntimeInstrumentor into its own class and describes the situation in context of the Java agent.
Diffstat (limited to 'agent/src/main/java/com')
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt17
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/agent/RuntimeInstrumentor.kt8
2 files changed, 16 insertions, 9 deletions
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt b/agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt
index dd9bccb7..3ce50e03 100644
--- a/agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt
+++ b/agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt
@@ -35,7 +35,7 @@ import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
-val KNOWN_ARGUMENTS = listOf(
+private val KNOWN_ARGUMENTS = listOf(
"instrumentation_includes",
"instrumentation_excludes",
"custom_hook_includes",
@@ -46,6 +46,19 @@ val KNOWN_ARGUMENTS = listOf(
"dump_classes_dir",
)
+// To be accessible by the agent classes the native library has to be loaded by the same class loader.
+// premain is executed in the context of the system class loader. At the beginning of premain the agent jar is added to
+// the bootstrap class loader and all subsequently required agent classes are loaded by it. Hence, it's not possible to
+// load the native library directly in premain by the system class loader, instead it's delegated to NativeLibraryLoader
+// loaded by the bootstrap class loader.
+internal object NativeLibraryLoader {
+ fun load() {
+ // Calls JNI_OnLoad_jazzer_initialize in the driver, which ensures that dynamically
+ // linked JNI methods are resolved against it.
+ System.loadLibrary("jazzer_initialize")
+ }
+}
+
private object AgentJarFinder {
val agentJarFile = jarUriForClass(AgentJarFinder::class.java)?.let { JarFile(File(it)) }
}
@@ -67,6 +80,8 @@ fun premain(agentArgs: String?, instrumentation: Instrumentation) {
} else {
println("WARN: Failed to add agent JAR to bootstrap class loader search path")
}
+ NativeLibraryLoader.load()
+
val argumentMap = (agentArgs ?: "")
.split(',')
.mapNotNull {
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/agent/RuntimeInstrumentor.kt b/agent/src/main/java/com/code_intelligence/jazzer/agent/RuntimeInstrumentor.kt
index c069b9e8..79c04c4a 100644
--- a/agent/src/main/java/com/code_intelligence/jazzer/agent/RuntimeInstrumentor.kt
+++ b/agent/src/main/java/com/code_intelligence/jazzer/agent/RuntimeInstrumentor.kt
@@ -178,12 +178,4 @@ internal class RuntimeInstrumentor(
instrumentedBytecode
}
}
-
- companion object {
- init {
- // Calls JNI_OnLoad_jazzer_initialize in the driver, which ensures that dynamically
- // linked JNI methods are resolved against it.
- System.loadLibrary("jazzer_initialize")
- }
- }
}