• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.example;
16 
17 import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow;
18 import com.code_intelligence.jazzer.api.HookType;
19 import com.code_intelligence.jazzer.api.MethodHook;
20 import java.lang.invoke.MethodHandle;
21 import java.lang.reflect.Field;
22 import java.util.regex.Pattern;
23 
24 public class HookDependenciesFuzzerHooks {
25   private static final Field PATTERN_ROOT;
26 
27   static {
28     Field root;
29     try {
30       root = Pattern.class.getDeclaredField("root");
31     } catch (NoSuchFieldException e) {
32       root = null;
33     }
34     PATTERN_ROOT = root;
35   }
36 
37   @MethodHook(type = HookType.AFTER, targetClassName = "java.util.regex.Matcher",
38       targetMethod = "matches", targetMethodDescriptor = "()Z",
39       additionalClassesToHook = {"java.util.regex.Pattern"})
40   public static void
matcherMatchesHook(MethodHandle method, Object alwaysNull, Object[] alwaysEmpty, int hookId, Boolean returnValue)41   matcherMatchesHook(MethodHandle method, Object alwaysNull, Object[] alwaysEmpty, int hookId,
42       Boolean returnValue) {
43     if (PATTERN_ROOT != null) {
44       throw new FuzzerSecurityIssueLow("Hook applied even though it depends on the class to hook");
45     }
46   }
47 }
48