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.code_intelligence.jazzer.junit; 16 17 import static com.code_intelligence.jazzer.junit.Utils.getClassPathBasedInstrumentationFilter; 18 import static com.code_intelligence.jazzer.junit.Utils.getLegacyInstrumentationFilter; 19 20 import java.io.File; 21 import java.util.concurrent.atomic.AtomicBoolean; 22 import org.junit.jupiter.api.extension.ExtensionContext; 23 24 class AgentConfigurator { 25 private static final AtomicBoolean hasBeenConfigured = new AtomicBoolean(); 26 forRegressionTest(ExtensionContext extensionContext)27 static void forRegressionTest(ExtensionContext extensionContext) { 28 if (!hasBeenConfigured.compareAndSet(false, true)) { 29 return; 30 } 31 32 applyCommonConfiguration(); 33 34 // Add logic to the hook instrumentation that allows us to enable and disable hooks at runtime. 35 System.setProperty("jazzer.internal.conditional_hooks", "true"); 36 // Apply all hooks, but no coverage or compare instrumentation. 37 System.setProperty("jazzer.instrumentation_excludes", "**"); 38 extensionContext.getConfigurationParameter("jazzer.instrument") 39 .ifPresent(s 40 -> System.setProperty( 41 "jazzer.custom_hook_includes", String.join(File.pathSeparator, s.split(",")))); 42 } 43 forFuzzing(ExtensionContext executionRequest)44 static void forFuzzing(ExtensionContext executionRequest) { 45 if (!hasBeenConfigured.compareAndSet(false, true)) { 46 throw new IllegalStateException("Only a single fuzz test should be executed per fuzzing run"); 47 } 48 49 applyCommonConfiguration(); 50 51 String instrumentationFilter = 52 executionRequest.getConfigurationParameter("jazzer.instrument") 53 .orElseGet( 54 () 55 -> getClassPathBasedInstrumentationFilter(System.getProperty("java.class.path")) 56 .orElseGet(() 57 -> getLegacyInstrumentationFilter( 58 executionRequest.getRequiredTestClass()))); 59 String filter = String.join(File.pathSeparator, instrumentationFilter.split(",")); 60 System.setProperty("jazzer.custom_hook_includes", filter); 61 System.setProperty("jazzer.instrumentation_includes", filter); 62 } 63 applyCommonConfiguration()64 private static void applyCommonConfiguration() { 65 // Do not hook common IDE and JUnit classes and their dependencies. 66 System.setProperty("jazzer.custom_hook_excludes", 67 String.join(File.pathSeparator, "com.google.testing.junit.**", "com.intellij.**", 68 "org.jetbrains.**", "io.github.classgraph.**", "junit.framework.**", "net.bytebuddy.**", 69 "org.apiguardian.**", "org.assertj.core.**", "org.hamcrest.**", "org.junit.**", 70 "org.opentest4j.**", "org.mockito.**", "org.apache.maven.**", "org.gradle.**")); 71 } 72 } 73