xref: /aosp_15_r20/external/jazzer-api/src/main/java/com/code_intelligence/jazzer/utils/ManifestUtils.kt (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)

<lambda>null1 // 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 import java.util.jar.Manifest
18 
19 object ManifestUtils {
20 
21     private const val FUZZ_TARGET_CLASS = "Jazzer-Fuzz-Target-Class"
22     const val HOOK_CLASSES = "Jazzer-Hook-Classes"
23 
24     fun combineManifestValues(attribute: String): List<String> {
25         val manifests = ManifestUtils::class.java.classLoader.getResources("META-INF/MANIFEST.MF")
26         return manifests.asSequence().mapNotNull { url ->
27             url.openStream().use { inputStream ->
28                 val manifest = Manifest(inputStream)
29                 manifest.mainAttributes.getValue(attribute)
30             }
31         }.toList()
32     }
33 
34     /**
35      * Returns the value of the `Fuzz-Target-Class` manifest attribute if there is a unique one among all manifest
36      * files in the classpath.
37      */
38     @JvmStatic
39     fun detectFuzzTargetClass(): String? {
40         val fuzzTargets = combineManifestValues(FUZZ_TARGET_CLASS)
41         return when (fuzzTargets.size) {
42             0 -> null
43             1 -> fuzzTargets.first()
44             else -> {
45                 Log.warn("More than one Jazzer-Fuzz-Target-Class manifest entry detected on the classpath.")
46                 null
47             }
48         }
49     }
50 }
51