• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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.code_intelligence.jazzer.runtime;
16 
17 import java.util.ArrayList;
18 
19 final public class JazzerInternal {
20   private static final ArrayList<Runnable> ON_FUZZ_TARGET_READY_CALLBACKS = new ArrayList<>();
21 
22   public static Throwable lastFinding;
23 
24   // Accessed from api.Jazzer via reflection.
reportFindingFromHook(Throwable finding)25   public static void reportFindingFromHook(Throwable finding) {
26     lastFinding = finding;
27     // Throw an Error that is hard to catch (short of outright ignoring it) in order to quickly
28     // terminate the execution of the fuzz target. The finding will be reported as soon as the fuzz
29     // target returns even if this Error is swallowed.
30     throw new HardToCatchError();
31   }
32 
registerOnFuzzTargetReadyCallback(Runnable callback)33   public static void registerOnFuzzTargetReadyCallback(Runnable callback) {
34     ON_FUZZ_TARGET_READY_CALLBACKS.add(callback);
35   }
36 
onFuzzTargetReady(String fuzzTargetClass)37   public static void onFuzzTargetReady(String fuzzTargetClass) {
38     ON_FUZZ_TARGET_READY_CALLBACKS.forEach(Runnable::run);
39     ON_FUZZ_TARGET_READY_CALLBACKS.clear();
40   }
41 }
42