|
| 1 | +/* |
| 2 | + * Copyright 2026 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.example; |
| 18 | + |
| 19 | +import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow; |
| 20 | +import java.util.ArrayList; |
| 21 | + |
| 22 | +/** |
| 23 | + * Regression test for https://github.com/CodeIntelligence/jazzer/issues/878. |
| 24 | + * |
| 25 | + * <p>When generating a coverage report at shutdown, any use of hooked method would trigger custom |
| 26 | + * hook dispatch. If the hook class is no longer loadable at that point, the JVM throws |
| 27 | + * NoClassDefFoundError. |
| 28 | + * |
| 29 | + * <p>This test verifies that hooks are disabled during coverage report generation by checking |
| 30 | + * whether the hook's system property marker was set after the last fuzzer iteration. The shutdown |
| 31 | + * sequence calls coverage report generation BEFORE fuzzerTearDown, so if hooks fire during report |
| 32 | + * generation, the property will be set when fuzzerTearDown runs. |
| 33 | + */ |
| 34 | +public class CoverageWithHooksFuzzer { |
| 35 | + public static void fuzzerTestOneInput(byte[] data) { |
| 36 | + // Use ArrayList so the hook fires during fuzzing. |
| 37 | + ArrayList<Byte> list = new ArrayList<>(); |
| 38 | + for (byte b : data) { |
| 39 | + list.add(b); |
| 40 | + } |
| 41 | + // Verify the hook actually fired during this iteration. |
| 42 | + if (!"true".equals(System.getProperty("jazzer.test.hook.called"))) { |
| 43 | + throw new IllegalStateException("Hook did not fire during fuzzing"); |
| 44 | + } |
| 45 | + // Clear the property after all ArrayList usage in this iteration. |
| 46 | + // If hooks fire during coverage report generation (after the last iteration), |
| 47 | + // the property will be set again. |
| 48 | + System.clearProperty("jazzer.test.hook.called"); |
| 49 | + if (list.size() > 3) { |
| 50 | + throw new FuzzerSecurityIssueLow("found enough bytes"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void fuzzerTearDown() { |
| 55 | + // fuzzerTearDown is called AFTER coverage report generation in the shutdown sequence. |
| 56 | + // If hooks were active during coverage report generation, use of hooked classes |
| 57 | + // would have triggered our hook, setting the property. |
| 58 | + if ("true".equals(System.getProperty("jazzer.test.hook.called"))) { |
| 59 | + throw new IllegalStateException("Hook was called during coverage report generation"); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments