|
| 1 | +/* |
| 2 | + * Copyright 2025 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.code_intelligence.jazzer.sanitizers; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +import com.code_intelligence.jazzer.api.MethodHook; |
| 21 | +import com.code_intelligence.jazzer.api.MethodHooks; |
| 22 | +import java.lang.invoke.MethodType; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Stream; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +/** |
| 32 | + * Verifies that for every declared @MethodHook in built-in sanitizers, a corresponding target |
| 33 | + * method (or constructor) with the configured descriptor exists. This guards against typos and |
| 34 | + * wrong descriptors. |
| 35 | + */ |
| 36 | +public class HookBindingSanityTest { |
| 37 | + |
| 38 | + final Set<String> SKIPPED_CLASSES = |
| 39 | + Set.of( |
| 40 | + // Only Java 8 |
| 41 | + "java.util.regex.Pattern$Single", |
| 42 | + "java.util.regex.Pattern$SingleI", |
| 43 | + "java.util.regex.Pattern$SingleS", |
| 44 | + "java.util.regex.Pattern$SingleU"); |
| 45 | + |
| 46 | + final Set<String> SKIPPED_METHODS = |
| 47 | + Set.of( |
| 48 | + // Only Java 8 |
| 49 | + "java.util.regex.Pattern.caseInsensitiveRangeFor", |
| 50 | + "java.util.regex.Pattern.rangeFor", |
| 51 | + "java.util.regex.Pattern.union", |
| 52 | + "sun.misc.Unsafe.getByte", |
| 53 | + "sun.misc.Unsafe.getChar", |
| 54 | + "sun.misc.Unsafe.getDouble", |
| 55 | + "sun.misc.Unsafe.getFloat", |
| 56 | + "sun.misc.Unsafe.getInt", |
| 57 | + "sun.misc.Unsafe.getLong", |
| 58 | + "sun.misc.Unsafe.getShort", |
| 59 | + "sun.misc.Unsafe.putByte", |
| 60 | + "sun.misc.Unsafe.putChar", |
| 61 | + "sun.misc.Unsafe.putDouble", |
| 62 | + "sun.misc.Unsafe.putFloat", |
| 63 | + "sun.misc.Unsafe.putInt", |
| 64 | + "sun.misc.Unsafe.putLong", |
| 65 | + "sun.misc.Unsafe.putShort"); |
| 66 | + |
| 67 | + @ParameterizedTest |
| 68 | + @MethodSource("getMethodHooks") |
| 69 | + public void allHooksResolveIfClassPresent(MethodHook hook) { |
| 70 | + String targetClassName = hook.targetClassName(); |
| 71 | + assertNotNull(targetClassName, "Hook has no target class"); |
| 72 | + if (SKIPPED_CLASSES.contains(targetClassName)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + ClassLoader loader = HookBindingSanityTest.class.getClassLoader(); |
| 76 | + Class<?> targetClass = |
| 77 | + assertDoesNotThrow( |
| 78 | + () -> Class.forName(targetClassName, false, loader), |
| 79 | + () -> "Expected class to exist for hook: " + targetClassName); |
| 80 | + String methodName = hook.targetMethod(); |
| 81 | + String methodDesc = hook.targetMethodDescriptor(); |
| 82 | + |
| 83 | + if (SKIPPED_METHODS.contains(String.format("%s.%s", targetClassName, methodName))) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if ("<init>".equals(methodName)) { |
| 88 | + if (methodDesc.isEmpty()) { |
| 89 | + // Any constructor is acceptable. |
| 90 | + assertNotEquals( |
| 91 | + 0, |
| 92 | + targetClass.getDeclaredConstructors().length, |
| 93 | + String.format("no constructor for class %s found", targetClassName)); |
| 94 | + } else { |
| 95 | + // Match specific constructor by descriptor |
| 96 | + MethodType mt = MethodType.fromMethodDescriptorString(methodDesc, loader); |
| 97 | + Class<?>[] descriptorParams = mt.parameterArray(); |
| 98 | + assertTrue( |
| 99 | + Arrays.stream(targetClass.getDeclaredConstructors()) |
| 100 | + .anyMatch(c -> Arrays.equals(c.getParameterTypes(), descriptorParams)), |
| 101 | + String.format("no matching constructor for class %s found", targetClassName)); |
| 102 | + } |
| 103 | + } else { |
| 104 | + if (methodDesc.isEmpty()) { |
| 105 | + // Require at least one declared method with that name |
| 106 | + assertTrue( |
| 107 | + Arrays.stream(targetClass.getDeclaredMethods()) |
| 108 | + .anyMatch(md -> md.getName().equals(methodName)), |
| 109 | + String.format("method name %s not found in class %s", methodName, targetClassName)); |
| 110 | + } else { |
| 111 | + MethodType mt = MethodType.fromMethodDescriptorString(methodDesc, loader); |
| 112 | + Class<?> descriptorReturnType = mt.returnType(); |
| 113 | + Class<?>[] descriptorParams = mt.parameterArray(); |
| 114 | + assertTrue( |
| 115 | + Arrays.stream(targetClass.getDeclaredMethods()) |
| 116 | + .anyMatch( |
| 117 | + md -> |
| 118 | + md.getName().equals(methodName) |
| 119 | + && md.getReturnType().equals(descriptorReturnType) |
| 120 | + && Arrays.equals(md.getParameterTypes(), descriptorParams)), |
| 121 | + String.format( |
| 122 | + "method %s with descriptor %s not found in class %s", |
| 123 | + methodName, methodDesc, targetClassName)); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + static Class<?> getHookClass(String className) { |
| 129 | + try { |
| 130 | + return Class.forName(className, false, HookBindingSanityTest.class.getClassLoader()); |
| 131 | + } catch (ClassNotFoundException e) { |
| 132 | + throw new RuntimeException("Could not find hook class " + className, e); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + static MethodHook[] getMethodHooks() throws ClassNotFoundException, NoSuchMethodException { |
| 137 | + return Constants.SANITIZER_HOOK_NAMES.stream() |
| 138 | + .map(HookBindingSanityTest::getHookClass) |
| 139 | + .flatMap(clazz -> Stream.of(clazz.getMethods())) |
| 140 | + .flatMap( |
| 141 | + m -> |
| 142 | + Stream.concat( |
| 143 | + Stream.of(m.getAnnotation(MethodHook.class)), |
| 144 | + Optional.ofNullable(m.getAnnotation(MethodHooks.class)) |
| 145 | + .map(MethodHooks::value) |
| 146 | + .map(Stream::of) |
| 147 | + .orElseGet(Stream::empty))) |
| 148 | + .filter(Objects::nonNull) |
| 149 | + .toArray(MethodHook[]::new); |
| 150 | + } |
| 151 | +} |
0 commit comments