Line  | Count  | Source  | 
1  |  | // Copyright 2024 Google LLC  | 
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  |  | #include <cstdlib>  | 
16  |  | #include <filesystem>  | 
17  |  | #include <fstream>  | 
18  |  | #include <string>  | 
19  |  |  | 
20  |  | #include "file_compiler.h"  | 
21  |  | #include <shaderc/shaderc.hpp>  | 
22  |  |  | 
23  |  | namespace fs = std::filesystem;  | 
24  |  |  | 
25  | 6.41k  | std::string CreateTemporaryGLSLFile(const std::string &shader_content) { | 
26  | 6.41k  |   std::string temp_dir = fs::temp_directory_path().string();  | 
27  | 6.41k  |   std::string temp_file =  | 
28  | 6.41k  |       temp_dir + "/temp_shader_" + std::to_string(rand()) + ".glsl";  | 
29  | 6.41k  |   std::ofstream ofs(temp_file);  | 
30  | 6.41k  |   ofs << shader_content;  | 
31  | 6.41k  |   ofs.close();  | 
32  | 6.41k  |   return temp_file;  | 
33  | 6.41k  | }  | 
34  |  |  | 
35  | 6.41k  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 
36  |  |   // Create temporary glsl file with valid structure and fuzzed data  | 
37  |  |   // std::string shader_content = GenerateShaderContent(data, size);  | 
38  | 6.41k  |   std::string payload(reinterpret_cast<const char *>(data), size);  | 
39  | 6.41k  |   std::string temp_shader_file = CreateTemporaryGLSLFile(payload);  | 
40  |  |  | 
41  |  |   // Initialize FileCompiler and options  | 
42  | 6.41k  |   glslc::FileCompiler file_compiler;  | 
43  | 6.41k  |   file_compiler.options().SetSuppressWarnings();  | 
44  | 6.41k  |   file_compiler.AddIncludeDirectory(fs::temp_directory_path().string());  | 
45  | 6.41k  |   file_compiler.SetOutputFileName("-"); | 
46  |  |  | 
47  | 6.41k  |   file_compiler.SetSpirvBinaryOutputFormat(  | 
48  | 6.41k  |       glslc::FileCompiler::SpirvBinaryEmissionFormat::WGSL);  | 
49  |  |  | 
50  | 6.41k  |   glslc::InputFileSpec input_spec = {temp_shader_file, | 
51  | 6.41k  |                                      shaderc_glsl_fragment_shader,  | 
52  | 6.41k  |                                      shaderc_source_language_glsl, "main"};  | 
53  |  |  | 
54  |  |   // Fuzz common CompileShaderFile  | 
55  | 6.41k  |   file_compiler.CompileShaderFile(input_spec);  | 
56  |  |  | 
57  |  |   // Fuzz Preprocessing  | 
58  | 6.41k  |   file_compiler.SetPreprocessingOnlyFlag();  | 
59  | 6.41k  |   file_compiler.ValidateOptions(1);  | 
60  | 6.41k  |   file_compiler.CompileShaderFile(input_spec);  | 
61  |  |  | 
62  |  |   // Fuzz CompileShaderFile with DisassemblyFlag  | 
63  | 6.41k  |   file_compiler.SetDisassemblyFlag();  | 
64  | 6.41k  |   file_compiler.CompileShaderFile(input_spec);  | 
65  |  |  | 
66  |  |   // Cleanup the temporary file  | 
67  | 6.41k  |   fs::remove(temp_shader_file);  | 
68  |  |  | 
69  | 6.41k  |   return 0;  | 
70  | 6.41k  | }  |