From 5246e52be3bf4427791000355cbef86626b43eca Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 29 Jan 2021 16:20:19 +0100 Subject: Initial commit --- .../jazzer/instrumentor/HookInstrumentor.kt | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt (limited to 'agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt') diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt new file mode 100644 index 00000000..4ebe962e --- /dev/null +++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt @@ -0,0 +1,49 @@ +// 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.instrumentor + +import org.objectweb.asm.ClassReader +import org.objectweb.asm.ClassVisitor +import org.objectweb.asm.ClassWriter +import org.objectweb.asm.MethodVisitor +import org.objectweb.asm.Opcodes + +internal class HookInstrumentor(private val hooks: Iterable, private val java6Mode: Boolean) : Instrumentor { + + private lateinit var random: DeterministicRandom + + override fun instrument(bytecode: ByteArray): ByteArray { + val reader = ClassReader(bytecode) + val writer = ClassWriter(reader, ClassWriter.COMPUTE_MAXS) + random = DeterministicRandom("hook", reader.className) + val interceptor = object : ClassVisitor(Opcodes.ASM9, writer) { + override fun visitMethod( + access: Int, + name: String?, + descriptor: String?, + signature: String?, + exceptions: Array?, + ): MethodVisitor? { + val mv = cv.visitMethod(access, name, descriptor, signature, exceptions) ?: return null + return if (shouldInstrument(access)) + makeHookMethodVisitor(access, descriptor, mv, hooks, java6Mode, random) + else + mv + } + } + reader.accept(interceptor, ClassReader.EXPAND_FRAMES) + return writer.toByteArray() + } +} -- cgit v1.2.3 From 0bcfd380fae4e121e3275fe05c9b8101ffca3fff Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 12 Feb 2021 09:56:53 +0100 Subject: Update dependencies (#5) * Update dependencies * Fail if changed Maven deps are not repinned * Extract ASM API version into Instrumentor --- .../java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt') diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt index 4ebe962e..6db76605 100644 --- a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt +++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt @@ -18,7 +18,6 @@ import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassVisitor import org.objectweb.asm.ClassWriter import org.objectweb.asm.MethodVisitor -import org.objectweb.asm.Opcodes internal class HookInstrumentor(private val hooks: Iterable, private val java6Mode: Boolean) : Instrumentor { @@ -28,7 +27,7 @@ internal class HookInstrumentor(private val hooks: Iterable, private val j val reader = ClassReader(bytecode) val writer = ClassWriter(reader, ClassWriter.COMPUTE_MAXS) random = DeterministicRandom("hook", reader.className) - val interceptor = object : ClassVisitor(Opcodes.ASM9, writer) { + val interceptor = object : ClassVisitor(Instrumentor.ASM_API_VERSION, writer) { override fun visitMethod( access: Int, name: String?, -- cgit v1.2.3 From 04fd630ec184fa1da44881dbb0ffafa67c15d4cf Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 26 Mar 2021 11:33:30 +0100 Subject: Shade external dependencies Fuzz targets may use the ASM libraries or JaCoCo themselves, which can lead to dependency version conflicts. To counter this, we shade all our external dependencies into the com.code_intelligence.jazzer.third_party.* package using bazel_jar_jar when we build the instrumentor library. The seemingly simpler approach of applying jar shading directly to the jazzer_agent_deploy.jar does not work as jarjar is unable to handle some of the Kotlin runtime files in the resulting jar. --- .../com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt') diff --git a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt index 6db76605..ac5f1780 100644 --- a/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt +++ b/agent/src/main/java/com/code_intelligence/jazzer/instrumentor/HookInstrumentor.kt @@ -14,10 +14,10 @@ package com.code_intelligence.jazzer.instrumentor -import org.objectweb.asm.ClassReader -import org.objectweb.asm.ClassVisitor -import org.objectweb.asm.ClassWriter -import org.objectweb.asm.MethodVisitor +import com.code_intelligence.jazzer.third_party.objectweb.asm.ClassReader +import com.code_intelligence.jazzer.third_party.objectweb.asm.ClassVisitor +import com.code_intelligence.jazzer.third_party.objectweb.asm.ClassWriter +import com.code_intelligence.jazzer.third_party.objectweb.asm.MethodVisitor internal class HookInstrumentor(private val hooks: Iterable, private val java6Mode: Boolean) : Instrumentor { -- cgit v1.2.3