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 | 5.73k | std::string CreateTemporaryGLSLFile(const std::string &shader_content) { |
26 | 5.73k | std::string temp_dir = fs::temp_directory_path().string(); |
27 | 5.73k | std::string temp_file = |
28 | 5.73k | temp_dir + "/temp_shader_" + std::to_string(rand()) + ".glsl"; |
29 | 5.73k | std::ofstream ofs(temp_file); |
30 | 5.73k | ofs << shader_content; |
31 | 5.73k | ofs.close(); |
32 | 5.73k | return temp_file; |
33 | 5.73k | } |
34 | | |
35 | 5.73k | 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 | 5.73k | std::string payload(reinterpret_cast<const char *>(data), size); |
39 | 5.73k | std::string temp_shader_file = CreateTemporaryGLSLFile(payload); |
40 | | |
41 | | // Initialize FileCompiler and options |
42 | 5.73k | glslc::FileCompiler file_compiler; |
43 | 5.73k | file_compiler.options().SetSuppressWarnings(); |
44 | 5.73k | file_compiler.AddIncludeDirectory(fs::temp_directory_path().string()); |
45 | 5.73k | file_compiler.SetOutputFileName("-"); |
46 | | |
47 | 5.73k | file_compiler.SetSpirvBinaryOutputFormat( |
48 | 5.73k | glslc::FileCompiler::SpirvBinaryEmissionFormat::WGSL); |
49 | | |
50 | 5.73k | glslc::InputFileSpec input_spec = {temp_shader_file, |
51 | 5.73k | shaderc_glsl_fragment_shader, |
52 | 5.73k | shaderc_source_language_glsl, "main"}; |
53 | | |
54 | | // Fuzz common CompileShaderFile |
55 | 5.73k | file_compiler.CompileShaderFile(input_spec); |
56 | | |
57 | | // Fuzz Preprocessing |
58 | 5.73k | file_compiler.SetPreprocessingOnlyFlag(); |
59 | 5.73k | file_compiler.ValidateOptions(1); |
60 | 5.73k | file_compiler.CompileShaderFile(input_spec); |
61 | | |
62 | | // Fuzz CompileShaderFile with DisassemblyFlag |
63 | 5.73k | file_compiler.SetDisassemblyFlag(); |
64 | 5.73k | file_compiler.CompileShaderFile(input_spec); |
65 | | |
66 | | // Cleanup the temporary file |
67 | 5.73k | fs::remove(temp_shader_file); |
68 | | |
69 | 5.73k | return 0; |
70 | 5.73k | } |