xref: /aosp_15_r20/external/jazzer-api/src/main/java/com/code_intelligence/jazzer/driver/FuzzTargetHolder.java (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
1 /*
2  * Copyright 2023 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 
17 package com.code_intelligence.jazzer.driver;
18 
19 import com.code_intelligence.jazzer.api.FuzzedDataProvider;
20 import java.lang.reflect.Method;
21 import java.util.Optional;
22 import java.util.concurrent.Callable;
23 
24 public class FuzzTargetHolder {
autofuzzFuzzTarget(Callable<Object> newInstance)25   public static FuzzTarget autofuzzFuzzTarget(Callable<Object> newInstance) {
26     try {
27       Method fuzzerTestOneInput = com.code_intelligence.jazzer.autofuzz.FuzzTarget.class.getMethod(
28           "fuzzerTestOneInput", FuzzedDataProvider.class);
29       return new FuzzTargetHolder.FuzzTarget(fuzzerTestOneInput, newInstance, Optional.empty());
30     } catch (NoSuchMethodException e) {
31       throw new IllegalStateException(e);
32     }
33   }
34 
35   public static final FuzzTarget AUTOFUZZ_FUZZ_TARGET = autofuzzFuzzTarget(() -> {
36     com.code_intelligence.jazzer.autofuzz.FuzzTarget.fuzzerInitialize(
37         Opt.targetArgs.toArray(new String[0]));
38     return null;
39   });
40 
41   /**
42    * The fuzz target that {@link FuzzTargetRunner} should fuzz.
43    */
44   public static FuzzTarget fuzzTarget;
45 
46   public static class FuzzTarget {
47     public final Method method;
48     public final Callable<Object> newInstance;
49     public final Optional<Method> tearDown;
50 
FuzzTarget(Method method, Callable<Object> newInstance, Optional<Method> tearDown)51     public FuzzTarget(Method method, Callable<Object> newInstance, Optional<Method> tearDown) {
52       this.method = method;
53       this.newInstance = newInstance;
54       this.tearDown = tearDown;
55     }
56 
usesFuzzedDataProvider()57     public boolean usesFuzzedDataProvider() {
58       return this.method.getParameterCount() == 1
59           && this.method.getParameterTypes()[0] == FuzzedDataProvider.class;
60     }
61   }
62 }
63