aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/com/code_intelligence/jazzer/agent/Agent.kt
blob: 33d0226310b641208a83faf2b02f7d366044c168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2021 Code Intelligence GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@file:JvmName("Agent")

package com.code_intelligence.jazzer.agent

import com.code_intelligence.jazzer.instrumentor.CoverageRecorder
import com.code_intelligence.jazzer.instrumentor.InstrumentationType
import com.code_intelligence.jazzer.instrumentor.loadHooks
import com.code_intelligence.jazzer.runtime.ManifestUtils
import com.code_intelligence.jazzer.utils.ClassNameGlobber
import java.io.File
import java.lang.instrument.Instrumentation
import java.nio.file.Paths
import java.util.jar.JarFile
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.exists
import kotlin.io.path.isDirectory

val KNOWN_ARGUMENTS = listOf(
    "instrumentation_includes",
    "instrumentation_excludes",
    "custom_hook_includes",
    "custom_hook_excludes",
    "trace",
    "custom_hooks",
    "id_sync_file",
    "dump_classes_dir",
)

private object AgentJarFinder {
    private val agentJarPath = AgentJarFinder::class.java.protectionDomain?.codeSource?.location?.toURI()
    val agentJarFile = agentJarPath?.let { JarFile(File(it)) }
}

private val argumentDelimiter = if (System.getProperty("os.name").startsWith("Windows")) ";" else ":"

@OptIn(ExperimentalPathApi::class)
fun premain(agentArgs: String?, instrumentation: Instrumentation) {
    // Add the agent jar (i.e., the jar out of which we are currently executing) to the search path of the bootstrap
    // class loader to ensure that instrumented classes can find the CoverageMap class regardless of which ClassLoader
    // they are using.
    if (AgentJarFinder.agentJarFile != null) {
        instrumentation.appendToBootstrapClassLoaderSearch(AgentJarFinder.agentJarFile)
    } else {
        println("WARN: Failed to add agent JAR to bootstrap class loader search path")
    }
    val argumentMap = (agentArgs ?: "")
        .split(',')
        .mapNotNull {
            val splitArg = it.split('=', limit = 2)
            when {
                splitArg.size != 2 -> {
                    if (splitArg[0].isNotEmpty())
                        println("WARN: Ignoring argument ${splitArg[0]} without value")
                    null
                }
                splitArg[0] !in KNOWN_ARGUMENTS -> {
                    println("WARN: Ignoring unknown argument ${splitArg[0]}")
                    null
                }
                else -> splitArg[0] to splitArg[1].split(argumentDelimiter)
            }
        }.toMap()
    val manifestCustomHookNames = ManifestUtils.combineManifestValues(ManifestUtils.HOOK_CLASSES).flatMap {
        it.split(':')
    }
    val customHookNames = manifestCustomHookNames + (argumentMap["custom_hooks"] ?: emptyList())
    val classNameGlobber = ClassNameGlobber(
        argumentMap["instrumentation_includes"] ?: emptyList(),
        (argumentMap["instrumentation_excludes"] ?: emptyList()) + customHookNames
    )
    CoverageRecorder.classNameGlobber = classNameGlobber
    val dependencyClassNameGlobber = ClassNameGlobber(
        argumentMap["custom_hook_includes"] ?: emptyList(),
        (argumentMap["custom_hook_excludes"] ?: emptyList()) + customHookNames
    )
    val instrumentationTypes = (argumentMap["trace"] ?: listOf("all")).flatMap {
        when (it) {
            "cmp" -> setOf(InstrumentationType.CMP)
            "cov" -> setOf(InstrumentationType.COV)
            "div" -> setOf(InstrumentationType.DIV)
            "gep" -> setOf(InstrumentationType.GEP)
            "indir" -> setOf(InstrumentationType.INDIR)
            "native" -> setOf(InstrumentationType.NATIVE)
            // Disable GEP instrumentation by default as it appears to negatively affect fuzzing
            // performance. Our current GEP instrumentation only reports constant indices, but even
            // when we instead reported non-constant indices, they tended to completely fill up the
            // table of recent compares and value profile map.
            "all" -> InstrumentationType.values().toSet() - InstrumentationType.GEP
            else -> {
                println("WARN: Skipping unknown instrumentation type $it")
                emptySet()
            }
        }
    }.toSet()
    val idSyncFile = argumentMap["id_sync_file"]?.let {
        Paths.get(it.single()).also { path ->
            println("INFO: Synchronizing coverage IDs in ${path.toAbsolutePath()}")
        }
    }
    val dumpClassesDir = argumentMap["dump_classes_dir"]?.let {
        Paths.get(it.single()).toAbsolutePath().also { path ->
            if (path.exists() && path.isDirectory()) {
                println("INFO: Dumping instrumented classes into $path")
            } else {
                println("ERROR: Cannot dump instrumented classes into $path; does not exist or not a directory")
            }
        }
    }
    val runtimeInstrumentor = RuntimeInstrumentor(
        instrumentation,
        classNameGlobber,
        dependencyClassNameGlobber,
        instrumentationTypes,
        idSyncFile,
        dumpClassesDir,
    )
    instrumentation.apply {
        addTransformer(runtimeInstrumentor)
    }

    val relevantClassesLoadedBeforeCustomHooks = instrumentation.allLoadedClasses
        .map { it.name }
        .filter { classNameGlobber.includes(it) || dependencyClassNameGlobber.includes(it) }
        .toSet()
    val customHooks = customHookNames.toSet().flatMap { hookClassName ->
        try {
            loadHooks(Class.forName(hookClassName)).also {
                println("INFO: Loaded ${it.size} hooks from $hookClassName")
            }
        } catch (_: ClassNotFoundException) {
            println("WARN: Failed to load hooks from $hookClassName")
            emptySet()
        }
    }
    val relevantClassesLoadedAfterCustomHooks = instrumentation.allLoadedClasses
        .map { it.name }
        .filter { classNameGlobber.includes(it) || dependencyClassNameGlobber.includes(it) }
        .toSet()
    val nonHookClassesLoadedByHooks = relevantClassesLoadedAfterCustomHooks - relevantClassesLoadedBeforeCustomHooks
    if (nonHookClassesLoadedByHooks.isNotEmpty()) {
        println("WARN: Hooks were not applied to the following classes as they are dependencies of hooks:")
        println("WARN: ${nonHookClassesLoadedByHooks.joinToString()}")
    }

    runtimeInstrumentor.registerCustomHooks(customHooks)
}