aboutsummaryrefslogtreecommitdiff
path: root/agent
diff options
context:
space:
mode:
authorFabian Meumertzheim <meumertzheim@code-intelligence.com>2021-02-22 07:20:18 +0100
committerFabian Meumertzheim <fabian@meumertzhe.im>2021-03-01 14:53:10 +0100
commitd62c07f3c17c02cba18836d09cbeea4c70298e0f (patch)
tree16a0aa7916ca395c692c72bffe5474e86d644077 /agent
parentbabea19e67337e39d5581290a94c656108eea2f7 (diff)
downloadjazzer-api-d62c07f3c17c02cba18836d09cbeea4c70298e0f.tar.gz
Move agent utils into own package
Diffstat (limited to 'agent')
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/instrumentor/BUILD.bazel1
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtils.kt28
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/instrumentor/Hook.kt1
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/runtime/Utils.kt53
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/utils/BUILD.bazel10
-rw-r--r--agent/src/main/java/com/code_intelligence/jazzer/utils/Utils.kt77
-rw-r--r--agent/src/test/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtilsTest.kt9
7 files changed, 96 insertions, 83 deletions
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/BUILD.bazel b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/BUILD.bazel
index d1b6f608..d49c2d4e 100644
--- a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/BUILD.bazel
+++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/BUILD.bazel
@@ -12,6 +12,7 @@ kt_jvm_library(
deps = [
"//agent/src/main/java/com/code_intelligence/jazzer/generated:JavaNoThrowMethods",
"//agent/src/main/java/com/code_intelligence/jazzer/runtime",
+ "//agent/src/main/java/com/code_intelligence/jazzer/utils",
"@com_github_jetbrains_kotlin//:kotlin-reflect",
"@jacoco_internal",
"@maven//:org_ow2_asm_asm",
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtils.kt b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtils.kt
index 4e34e3a4..80cbe80b 100644
--- a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtils.kt
+++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtils.kt
@@ -14,34 +14,6 @@
package com.code_intelligence.jazzer.instrumentor
-import java.lang.reflect.Method
-
-internal val Class<*>.descriptor: String
- get() = when {
- isPrimitive -> {
- when (this) {
- Boolean::class.javaPrimitiveType -> "Z"
- Byte::class.javaPrimitiveType -> "B"
- Char::class.javaPrimitiveType -> "C"
- Short::class.javaPrimitiveType -> "S"
- Int::class.javaPrimitiveType -> "I"
- Long::class.javaPrimitiveType -> "J"
- Float::class.javaPrimitiveType -> "F"
- Double::class.javaPrimitiveType -> "D"
- java.lang.Void::class.javaPrimitiveType -> "V"
- else -> throw IllegalStateException("Unknown primitive type: $name")
- }
- }
- isArray -> "[${componentType.descriptor}"
- java.lang.Object::class.java.isAssignableFrom(this) -> "L${name.replace('.', '/')};"
- else -> throw IllegalArgumentException("Unknown class type: $name")
- }
-
-internal val Method.descriptor: String
- get() = parameterTypes.joinToString(separator = "", prefix = "(", postfix = ")${returnType.descriptor}") { parameterType ->
- parameterType.descriptor
- }
-
internal fun isPrimitiveType(typeDescriptor: String): Boolean {
return typeDescriptor in arrayOf("B", "C", "D", "F", "I", "J", "S", "V", "Z")
}
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/Hook.kt b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/Hook.kt
index e1efc56f..eb17c356 100644
--- a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/Hook.kt
+++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/Hook.kt
@@ -19,6 +19,7 @@ package com.code_intelligence.jazzer.instrumentor
import com.code_intelligence.jazzer.api.HookType
import com.code_intelligence.jazzer.api.MethodHook
import com.code_intelligence.jazzer.api.MethodHooks
+import com.code_intelligence.jazzer.utils.descriptor
import java.lang.invoke.MethodHandle
import java.lang.reflect.Method
import java.lang.reflect.Modifier
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/runtime/Utils.kt b/agent/src/main/java/com/code_intelligence/jazzer/runtime/Utils.kt
deleted file mode 100644
index 7b05c73a..00000000
--- a/agent/src/main/java/com/code_intelligence/jazzer/runtime/Utils.kt
+++ /dev/null
@@ -1,53 +0,0 @@
-// 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.
-
-package com.code_intelligence.jazzer.runtime
-
-import java.nio.ByteBuffer
-import java.security.MessageDigest
-
-@Suppress("unused")
-internal object Utils {
-
- private fun hash(throwable: Throwable): ByteArray = MessageDigest.getInstance("SHA-256").run {
- // It suffices to hash the stack trace of the deepest cause as the higher-level causes only
- // contain part of the stack trace (plus possibly a different exception type).
- var rootCause = throwable
- while (true) {
- rootCause = rootCause.cause ?: break
- }
- update(rootCause.javaClass.name.toByteArray())
- for (element in rootCause.stackTrace) {
- update(element.toString().toByteArray())
- }
- if (throwable.suppressed.isNotEmpty()) {
- update("suppressed".toByteArray())
- for (suppressed in throwable.suppressed) {
- update(hash(suppressed))
- }
- }
- digest()
- }
-
- /**
- * Computes a hash of the stack trace of [throwable] without messages.
- *
- * The hash can be used to deduplicate stack traces obtained on crashes. By not including the
- * messages, this hash should not depend on the precise crashing input.
- */
- @JvmStatic
- fun computeDedupToken(throwable: Throwable): Long {
- return ByteBuffer.wrap(hash(throwable)).long
- }
-}
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/utils/BUILD.bazel b/agent/src/main/java/com/code_intelligence/jazzer/utils/BUILD.bazel
new file mode 100644
index 00000000..89ec1854
--- /dev/null
+++ b/agent/src/main/java/com/code_intelligence/jazzer/utils/BUILD.bazel
@@ -0,0 +1,10 @@
+load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
+
+kt_jvm_library(
+ name = "utils",
+ srcs = glob([
+ "*.java",
+ "*.kt",
+ ]),
+ visibility = ["//visibility:public"],
+)
diff --git a/agent/src/main/java/com/code_intelligence/jazzer/utils/Utils.kt b/agent/src/main/java/com/code_intelligence/jazzer/utils/Utils.kt
new file mode 100644
index 00000000..db6358ee
--- /dev/null
+++ b/agent/src/main/java/com/code_intelligence/jazzer/utils/Utils.kt
@@ -0,0 +1,77 @@
+// 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("Utils")
+
+package com.code_intelligence.jazzer.utils
+
+import java.lang.reflect.Executable
+import java.lang.reflect.Method
+import java.nio.ByteBuffer
+import java.security.MessageDigest
+
+val Class<*>.descriptor: String
+ get() = when {
+ isPrimitive -> {
+ when (this) {
+ Boolean::class.javaPrimitiveType -> "Z"
+ Byte::class.javaPrimitiveType -> "B"
+ Char::class.javaPrimitiveType -> "C"
+ Short::class.javaPrimitiveType -> "S"
+ Int::class.javaPrimitiveType -> "I"
+ Long::class.javaPrimitiveType -> "J"
+ Float::class.javaPrimitiveType -> "F"
+ Double::class.javaPrimitiveType -> "D"
+ java.lang.Void::class.javaPrimitiveType -> "V"
+ else -> throw IllegalStateException("Unknown primitive type: $name")
+ }
+ }
+ isArray -> "[${componentType.descriptor}"
+ java.lang.Object::class.java.isAssignableFrom(this) -> "L${name.replace('.', '/')};"
+ else -> throw IllegalArgumentException("Unknown class type: $name")
+ }
+
+val Executable.descriptor: String
+ get() = parameterTypes.joinToString(separator = "", prefix = "(", postfix = ")") { parameterType ->
+ parameterType.descriptor
+ } + if (this is Method) returnType.descriptor else "V"
+
+private fun hash(throwable: Throwable): ByteArray = MessageDigest.getInstance("SHA-256").run {
+ // It suffices to hash the stack trace of the deepest cause as the higher-level causes only
+ // contain part of the stack trace (plus possibly a different exception type).
+ var rootCause = throwable
+ while (true) {
+ rootCause = rootCause.cause ?: break
+ }
+ update(rootCause.javaClass.name.toByteArray())
+ for (element in rootCause.stackTrace) {
+ update(element.toString().toByteArray())
+ }
+ if (throwable.suppressed.isNotEmpty()) {
+ update("suppressed".toByteArray())
+ for (suppressed in throwable.suppressed) {
+ update(hash(suppressed))
+ }
+ }
+ digest()
+}
+
+/**
+ * Computes a hash of the stack trace of [throwable] without messages.
+ *
+ * The hash can be used to deduplicate stack traces obtained on crashes. By not including the
+ * messages, this hash should not depend on the precise crashing input.
+ */
+fun computeDedupToken(throwable: Throwable): Long {
+ return ByteBuffer.wrap(hash(throwable)).long
+}
diff --git a/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtilsTest.kt b/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtilsTest.kt
index bd7ddc88..546df13a 100644
--- a/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtilsTest.kt
+++ b/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/DescriptorUtilsTest.kt
@@ -57,9 +57,14 @@ class DescriptorUtilsTest {
listOf("I", "I"),
"Ljava/lang/CharSequence;"
),
+ Triple(
+ String::class.java.getConstructor(),
+ emptyList(),
+ "()V"
+ )
)
- for ((method, parameterDescriptors, returnTypeDescriptor) in testCases) {
- val descriptor = method.descriptor
+ for ((executable, parameterDescriptors, returnTypeDescriptor) in testCases) {
+ val descriptor = executable.descriptor
assertEquals(extractParameterTypeDescriptors(descriptor), parameterDescriptors)
assertEquals(extractReturnTypeDescriptor(descriptor), returnTypeDescriptor)
}