xref: /aosp_15_r20/external/jazzer-api/src/main/java/com/code_intelligence/jazzer/utils/ClassNameGlobber.kt (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
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.utils
16 
17 private val BASE_INCLUDED_CLASS_NAME_GLOBS = listOf(
18     "**", // everything
19 )
20 
21 // We use both a strong indicator for running as a Bazel test together with an indicator for a
22 // Bazel coverage run to rule out false positives.
23 private val IS_BAZEL_COVERAGE_RUN = System.getenv("TEST_UNDECLARED_OUTPUTS_DIR") != null &&
24     System.getenv("COVERAGE_DIR") != null
25 
26 private val ADDITIONAL_EXCLUDED_NAME_GLOBS_FOR_BAZEL_COVERAGE = listOf(
27     "com.google.testing.coverage.**",
28     "org.jacoco.**",
29 )
30 
31 private val BASE_EXCLUDED_CLASS_NAME_GLOBS = listOf(
32     // JDK internals
33     "\\[**", // array types
34     "java.**",
35     "javax.**",
36     "jdk.**",
37     "sun.**",
38     "com.sun.**", // package for Proxy objects
39     // Azul JDK internals
40     "com.azul.tooling.**",
41     // Kotlin internals
42     "kotlin.**",
43     // Jazzer internals
44     "com.code_intelligence.jazzer.**",
45     "jaz.Ter", // safe companion of the honeypot class used by sanitizers
46     "jaz.Zer", // honeypot class used by sanitizers
47     // Test and instrumentation tools
48     "org.junit.**", // dependency of @FuzzTest
49     "org.mockito.**", // can cause instrumentation cycles
50     "net.bytebuddy.**", // ignore Byte Buddy, though it's probably shaded
51     "org.jetbrains.**", // ignore JetBrains products (coverage agent)
52 ) + if (IS_BAZEL_COVERAGE_RUN) ADDITIONAL_EXCLUDED_NAME_GLOBS_FOR_BAZEL_COVERAGE else listOf()
53 
54 class ClassNameGlobber(includes: List<String>, excludes: List<String>) {
55     // If no include globs are provided, start with all classes.
<lambda>null56     private val includeMatchers = includes.ifEmpty { BASE_INCLUDED_CLASS_NAME_GLOBS }
57         .map(::SimpleGlobMatcher)
58 
59     // If no include globs are provided, additionally exclude stdlib classes as well as our own classes.
60     private val excludeMatchers = (if (includes.isEmpty()) BASE_EXCLUDED_CLASS_NAME_GLOBS + excludes else excludes)
61         .map(::SimpleGlobMatcher)
62 
includesnull63     fun includes(className: String): Boolean {
64         return includeMatchers.any { it.matches(className) } && excludeMatchers.none { it.matches(className) }
65     }
66 }
67