xref: /aosp_15_r20/external/jazzer-api/src/main/java/com/code_intelligence/jazzer/agent/AgentUtils.java (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
1 /*
2  * Copyright 2023 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.code_intelligence.jazzer.agent;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.file.Files;
23 import java.nio.file.StandardCopyOption;
24 import java.util.jar.JarFile;
25 
26 final class AgentUtils {
27   private static final String BOOTSTRAP_JAR =
28       "/com/code_intelligence/jazzer/runtime/jazzer_bootstrap.jar";
29 
extractBootstrapJar()30   public static JarFile extractBootstrapJar() {
31     try (InputStream bootstrapJarStream = AgentUtils.class.getResourceAsStream(BOOTSTRAP_JAR)) {
32       if (bootstrapJarStream == null) {
33         throw new IllegalStateException("Failed to find Jazzer agent bootstrap jar in resources");
34       }
35       File bootstrapJar = Files.createTempFile("jazzer-agent-", ".jar").toFile();
36       bootstrapJar.deleteOnExit();
37       Files.copy(bootstrapJarStream, bootstrapJar.toPath(), StandardCopyOption.REPLACE_EXISTING);
38       return new JarFile(bootstrapJar);
39     } catch (IOException e) {
40       throw new IllegalStateException("Failed to extract Jazzer agent bootstrap jar", e);
41     }
42   }
43 
AgentUtils()44   private AgentUtils() {}
45 }
46