|
| 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.Collections; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import java.util.stream.Stream; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.MethodSource; |
| 32 | + |
| 33 | +/** |
| 34 | + * Verifies that for every declared @MethodHook in built-in sanitizers, a corresponding target |
| 35 | + * method (or constructor) with the configured descriptor exists. This guards against typos and |
| 36 | + * wrong descriptors. |
| 37 | + */ |
| 38 | +public class HookBindingSanityTest { |
| 39 | + |
| 40 | + final Set<String> SKIPPED_CLASSES_CURRENT_JDK = |
| 41 | + Collections.unmodifiableSet( |
| 42 | + Stream.of( |
| 43 | + // Available for JDK >= 9 |
| 44 | + "java.util.regex.Pattern$Single", |
| 45 | + "java.util.regex.Pattern$SingleI", |
| 46 | + "java.util.regex.Pattern$SingleS", |
| 47 | + "java.util.regex.Pattern$SingleU") |
| 48 | + .collect(Collectors.toSet())); |
| 49 | + |
| 50 | + final Set<String> SKIPPED_METHODS_CURRENT_JDK = |
| 51 | + Collections.unmodifiableSet( |
| 52 | + Stream.of( |
| 53 | + // Available for JDK >= 9 |
| 54 | + "java.util.regex.Pattern.caseInsensitiveRangeFor", |
| 55 | + "java.util.regex.Pattern.rangeFor", |
| 56 | + "java.util.regex.Pattern.union", |
| 57 | + "sun.misc.Unsafe.getByte", |
| 58 | + "sun.misc.Unsafe.getChar", |
| 59 | + "sun.misc.Unsafe.getDouble", |
| 60 | + "sun.misc.Unsafe.getFloat", |
| 61 | + "sun.misc.Unsafe.getInt", |
| 62 | + "sun.misc.Unsafe.getLong", |
| 63 | + "sun.misc.Unsafe.getShort", |
| 64 | + "sun.misc.Unsafe.putByte", |
| 65 | + "sun.misc.Unsafe.putChar", |
| 66 | + "sun.misc.Unsafe.putDouble", |
| 67 | + "sun.misc.Unsafe.putFloat", |
| 68 | + "sun.misc.Unsafe.putInt", |
| 69 | + "sun.misc.Unsafe.putLong", |
| 70 | + "sun.misc.Unsafe.putShort") |
| 71 | + .collect(Collectors.toSet())); |
| 72 | + |
| 73 | + final Set<String> SKIPPED_CLASSES_JDK_8 = |
| 74 | + Collections.unmodifiableSet( |
| 75 | + Stream.of( |
| 76 | + "jakarta.el.ExpressionFactory", |
| 77 | + "java.util.regex.Pattern$CharPredicate", |
| 78 | + "javax.xml.xpath.XPath") |
| 79 | + .collect(Collectors.toSet())); |
| 80 | + |
| 81 | + final Set<String> SKIPPED_METHODS_JDK_8 = |
| 82 | + Collections.unmodifiableSet( |
| 83 | + Stream.of( |
| 84 | + "java.lang.Class.forName", |
| 85 | + "java.lang.ClassLoader.loadClass", |
| 86 | + "java.nio.file.Files.mismatch", |
| 87 | + "java.nio.file.Files.readString", |
| 88 | + "java.nio.file.Files.writeString", |
| 89 | + "java.util.regex.Pattern.CIRange", |
| 90 | + "java.util.regex.Pattern.CIRangeU", |
| 91 | + "java.util.regex.Pattern.Range", |
| 92 | + "java.util.regex.Pattern.Single", |
| 93 | + "java.util.regex.Pattern.SingleI", |
| 94 | + "java.util.regex.Pattern.SingleS", |
| 95 | + "java.util.regex.Pattern.SingleU") |
| 96 | + .collect(Collectors.toSet())); |
| 97 | + |
| 98 | + final boolean isJDK8 = System.getProperty("java.version").startsWith("1.8"); |
| 99 | + final Set<String> SKIPPED_CLASSES = isJDK8 ? SKIPPED_CLASSES_JDK_8 : SKIPPED_CLASSES_CURRENT_JDK; |
| 100 | + final Set<String> SKIPPED_METHODS = isJDK8 ? SKIPPED_METHODS_JDK_8 : SKIPPED_METHODS_CURRENT_JDK; |
| 101 | + |
| 102 | + @ParameterizedTest |
| 103 | + @MethodSource("getMethodHooks") |
| 104 | + public void methodHookResolves(MethodHook hook) { |
| 105 | + String targetClassName = hook.targetClassName(); |
| 106 | + assertNotNull(targetClassName, "Hook has no target class"); |
| 107 | + if (SKIPPED_CLASSES.contains(targetClassName)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + ClassLoader loader = HookBindingSanityTest.class.getClassLoader(); |
| 111 | + Class<?> targetClass = |
| 112 | + assertDoesNotThrow( |
| 113 | + () -> Class.forName(targetClassName, false, loader), |
| 114 | + () -> "class to hook not found: " + targetClassName); |
| 115 | + String methodName = hook.targetMethod(); |
| 116 | + String methodDesc = hook.targetMethodDescriptor(); |
| 117 | + |
| 118 | + if (SKIPPED_METHODS.contains(String.format("%s.%s", targetClassName, methodName))) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if ("<init>".equals(methodName)) { |
| 123 | + if (methodDesc.isEmpty()) { |
| 124 | + // Any constructor is acceptable. |
| 125 | + assertNotEquals( |
| 126 | + 0, |
| 127 | + targetClass.getDeclaredConstructors().length, |
| 128 | + String.format("no constructor for class %s found", targetClassName)); |
| 129 | + } else { |
| 130 | + // Match specific constructor by descriptor |
| 131 | + MethodType mt = MethodType.fromMethodDescriptorString(methodDesc, loader); |
| 132 | + Class<?>[] descriptorParams = mt.parameterArray(); |
| 133 | + assertTrue( |
| 134 | + Arrays.stream(targetClass.getDeclaredConstructors()) |
| 135 | + .anyMatch(c -> Arrays.equals(c.getParameterTypes(), descriptorParams)), |
| 136 | + String.format("no matching constructor for class %s found", targetClassName)); |
| 137 | + } |
| 138 | + } else { |
| 139 | + if (methodDesc.isEmpty()) { |
| 140 | + // Require at least one declared method with that name |
| 141 | + assertTrue( |
| 142 | + Arrays.stream(targetClass.getDeclaredMethods()) |
| 143 | + .anyMatch(md -> md.getName().equals(methodName)), |
| 144 | + String.format("method name %s not found in class %s", methodName, targetClassName)); |
| 145 | + } else { |
| 146 | + MethodType mt = MethodType.fromMethodDescriptorString(methodDesc, loader); |
| 147 | + Class<?> descriptorReturnType = mt.returnType(); |
| 148 | + Class<?>[] descriptorParams = mt.parameterArray(); |
| 149 | + assertTrue( |
| 150 | + Arrays.stream(targetClass.getDeclaredMethods()) |
| 151 | + .anyMatch( |
| 152 | + md -> |
| 153 | + md.getName().equals(methodName) |
| 154 | + && md.getReturnType().equals(descriptorReturnType) |
| 155 | + && Arrays.equals(md.getParameterTypes(), descriptorParams)), |
| 156 | + String.format( |
| 157 | + "method %s with descriptor %s not found in class %s", |
| 158 | + methodName, methodDesc, targetClassName)); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + static Class<?> getHookClass(String className) { |
| 164 | + try { |
| 165 | + return Class.forName(className, false, HookBindingSanityTest.class.getClassLoader()); |
| 166 | + } catch (ClassNotFoundException e) { |
| 167 | + throw new RuntimeException("Could not find hook class " + className, e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + static MethodHook[] getMethodHooks() throws ClassNotFoundException, NoSuchMethodException { |
| 172 | + return Constants.SANITIZER_HOOK_NAMES.stream() |
| 173 | + .map(HookBindingSanityTest::getHookClass) |
| 174 | + .flatMap(clazz -> Stream.of(clazz.getMethods())) |
| 175 | + .flatMap( |
| 176 | + m -> |
| 177 | + Stream.concat( |
| 178 | + Stream.of(m.getAnnotation(MethodHook.class)), |
| 179 | + Optional.ofNullable(m.getAnnotation(MethodHooks.class)) |
| 180 | + .map(MethodHooks::value) |
| 181 | + .map(Stream::of) |
| 182 | + .orElseGet(Stream::empty))) |
| 183 | + .filter(Objects::nonNull) |
| 184 | + .toArray(MethodHook[]::new); |
| 185 | + } |
| 186 | +} |
0 commit comments