com.code_intelligence.jazzer.instrumentor.Hook.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jazzer Show documentation
Show all versions of jazzer Show documentation
Coverage-guided, in-process fuzzing for the JVM
// 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:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
package com.code_intelligence.jazzer.instrumentor
import com.code_intelligence.jazzer.api.HookType
import com.code_intelligence.jazzer.api.MethodHook
import java.lang.invoke.MethodHandle
import java.lang.reflect.Method
import java.lang.reflect.Modifier
class Hook private constructor(
private val targetClassName: String,
val hookType: HookType,
val targetMethodName: String,
val targetMethodDescriptor: String?,
val additionalClassesToHook: List,
val targetInternalClassName: String,
private val targetReturnTypeDescriptor: String?,
private val targetWrappedReturnTypeDescriptor: String?,
private val hookClassName: String,
val hookInternalClassName: String,
val hookMethodName: String,
val hookMethodDescriptor: String,
) {
override fun toString(): String {
return "$hookType $targetClassName.$targetMethodName: $hookClassName.$hookMethodName $additionalClassesToHook"
}
companion object {
fun createAndVerifyHook(hookMethod: Method, hookData: MethodHook, className: String): Hook {
return createHook(hookMethod, hookData, className).also {
verify(hookMethod, it)
}
}
private fun createHook(hookMethod: Method, annotation: MethodHook, targetClassName: String): Hook {
val targetReturnTypeDescriptor = annotation.targetMethodDescriptor
.takeIf { it.isNotBlank() }?.let { extractReturnTypeDescriptor(it) }
val hookClassName: String = hookMethod.declaringClass.name
return Hook(
targetClassName = targetClassName,
hookType = annotation.type,
targetMethodName = annotation.targetMethod,
targetMethodDescriptor = annotation.targetMethodDescriptor.takeIf { it.isNotBlank() },
additionalClassesToHook = annotation.additionalClassesToHook.asList(),
targetInternalClassName = targetClassName.replace('.', '/'),
targetReturnTypeDescriptor = targetReturnTypeDescriptor,
targetWrappedReturnTypeDescriptor = targetReturnTypeDescriptor?.let { getWrapperTypeDescriptor(it) },
hookClassName = hookClassName,
hookInternalClassName = hookClassName.replace('.', '/'),
hookMethodName = hookMethod.name,
hookMethodDescriptor = hookMethod.descriptor,
)
}
private fun verify(hookMethod: Method, potentialHook: Hook) {
// Verify the hook method's modifiers (public static).
require(Modifier.isPublic(hookMethod.modifiers)) { "$potentialHook: hook method must be public" }
require(Modifier.isStatic(hookMethod.modifiers)) { "$potentialHook: hook method must be static" }
// Verify the hook method's parameter count.
val numParameters = hookMethod.parameters.size
when (potentialHook.hookType) {
HookType.BEFORE, HookType.REPLACE -> require(numParameters == 4) { "$potentialHook: incorrect number of parameters (expected 4)" }
HookType.AFTER -> require(numParameters == 5) { "$potentialHook: incorrect number of parameters (expected 5)" }
}
// Verify the hook method's parameter types.
val parameterTypes = hookMethod.parameterTypes
require(parameterTypes[0] == MethodHandle::class.java) { "$potentialHook: first parameter must have type MethodHandle" }
require(parameterTypes[1] == Object::class.java || parameterTypes[1].name == potentialHook.targetClassName) { "$potentialHook: second parameter must have type Object or ${potentialHook.targetClassName}" }
require(parameterTypes[2] == Array