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 <cstring>  | 
16  |  | #include <iostream>  | 
17  |  | #include <string>  | 
18  |  | #include <vector>  | 
19  |  |  | 
20  |  | #include <shaderc/shaderc.h>  | 
21  |  | #include <shaderc/shaderc.hpp>  | 
22  |  |  | 
23  | 2.64k  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 
24  |  |   // Skip iteration if data not enough  | 
25  | 2.64k  |   if (size == 0) { | 
26  | 0  |     return 0;  | 
27  | 0  |   }  | 
28  |  |  | 
29  |  |   // Prepare GLSL shader content with valid version  | 
30  | 2.64k  |   std::string shader_content(reinterpret_cast<const char *>(data), size);  | 
31  |  |  | 
32  |  |   // Prepare Compiler and options  | 
33  | 2.64k  |   shaderc::Compiler compiler;  | 
34  | 2.64k  |   shaderc::CompileOptions options;  | 
35  | 2.64k  |   options.SetOptimizationLevel(shaderc_optimization_level_performance);  | 
36  |  |  | 
37  |  |   // Preprocessing  | 
38  | 2.64k  |   shaderc::PreprocessedSourceCompilationResult preprocess_result =  | 
39  | 2.64k  |       compiler.PreprocessGlsl(shader_content, shaderc_glsl_vertex_shader,  | 
40  | 2.64k  |                               "input.glsl", options);  | 
41  | 2.64k  |   if (preprocess_result.GetCompilationStatus() ==  | 
42  | 2.64k  |       shaderc_compilation_status_success) { | 
43  | 1.85k  |     std::string preprocessed_code(preprocess_result.cbegin(),  | 
44  | 1.85k  |                                   preprocess_result.cend());  | 
45  | 1.85k  |   } else { | 
46  | 786  |     return 0;  | 
47  | 786  |   }  | 
48  |  |  | 
49  |  |   // Compile to SPIR-V binary  | 
50  | 1.85k  |   shaderc::SpvCompilationResult binary_result = compiler.CompileGlslToSpv(  | 
51  | 1.85k  |       shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);  | 
52  | 1.85k  |   if (binary_result.GetCompilationStatus() ==  | 
53  | 1.85k  |       shaderc_compilation_status_success) { | 
54  | 214  |     std::vector<uint32_t> spirv_binary(binary_result.cbegin(),  | 
55  | 214  |                                        binary_result.cend());  | 
56  | 214  |   }  | 
57  |  |  | 
58  |  |   // Compile to SPIR-V assembly  | 
59  | 1.85k  |   shaderc::AssemblyCompilationResult assembly_result =  | 
60  | 1.85k  |       compiler.CompileGlslToSpvAssembly(  | 
61  | 1.85k  |           shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);  | 
62  | 1.85k  |   if (assembly_result.GetCompilationStatus() ==  | 
63  | 1.85k  |       shaderc_compilation_status_success) { | 
64  | 214  |     std::string spirv_assembly(assembly_result.cbegin(),  | 
65  | 214  |                                assembly_result.cend());  | 
66  | 214  |   }  | 
67  |  |  | 
68  |  |   // Compile with C API  | 
69  | 1.85k  |   shaderc_compiler_t c_compiler = shaderc_compiler_initialize();  | 
70  | 1.85k  |   shaderc_compilation_result_t c_result = shaderc_compile_into_spv(  | 
71  | 1.85k  |       c_compiler, shader_content.c_str(), shader_content.size(),  | 
72  | 1.85k  |       shaderc_glsl_vertex_shader, "main.vert", "main", nullptr);  | 
73  |  |  | 
74  | 1.85k  |   if (shaderc_result_get_compilation_status(c_result) ==  | 
75  | 1.85k  |       shaderc_compilation_status_success) { | 
76  | 256  |     std::vector<uint32_t> spirv_c_binary(shaderc_result_get_length(c_result) /  | 
77  | 256  |                                          sizeof(uint32_t));  | 
78  | 256  |     std::memcpy(spirv_c_binary.data(), shaderc_result_get_bytes(c_result),  | 
79  | 256  |                 shaderc_result_get_length(c_result));  | 
80  | 256  |   }  | 
81  | 1.85k  |   shaderc_result_release(c_result);  | 
82  | 1.85k  |   shaderc_compiler_release(c_compiler);  | 
83  |  |  | 
84  | 1.85k  |   return 0;  | 
85  | 2.64k  | }  |