Coverage Report

Created: 2025-06-13 06:48

/src/shaderc_general_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
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
static void execute_shaderc_routines(const uint8_t *data, size_t size,
24
9.72k
                                     shaderc::CompileOptions &options) {
25
9.72k
  std::string shader_content(reinterpret_cast<const char *>(data), size);
26
27
9.72k
  shaderc::Compiler compiler;
28
29
  // Preprocessing
30
9.72k
  shaderc::PreprocessedSourceCompilationResult preprocess_result =
31
9.72k
      compiler.PreprocessGlsl(shader_content, shaderc_glsl_vertex_shader,
32
9.72k
                              "input.glsl", options);
33
9.72k
  if (preprocess_result.GetCompilationStatus() ==
34
9.72k
      shaderc_compilation_status_success) {
35
4.52k
    std::string preprocessed_code(preprocess_result.cbegin(),
36
4.52k
                                  preprocess_result.cend());
37
5.19k
  } else {
38
5.19k
    return;
39
5.19k
  }
40
41
  // Compile to SPIR-V binary
42
4.52k
  shaderc::SpvCompilationResult binary_result = compiler.CompileGlslToSpv(
43
4.52k
      shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
44
4.52k
  if (binary_result.GetCompilationStatus() ==
45
4.52k
      shaderc_compilation_status_success) {
46
386
    std::vector<uint32_t> spirv_binary(binary_result.cbegin(),
47
386
                                       binary_result.cend());
48
386
  }
49
50
  // Compile to SPIR-V assembly
51
4.52k
  shaderc::AssemblyCompilationResult assembly_result =
52
4.52k
      compiler.CompileGlslToSpvAssembly(
53
4.52k
          shader_content, shaderc_glsl_vertex_shader, "input.glsl", options);
54
4.52k
  if (assembly_result.GetCompilationStatus() ==
55
4.52k
      shaderc_compilation_status_success) {
56
386
    std::string spirv_assembly(assembly_result.cbegin(),
57
386
                               assembly_result.cend());
58
386
  }
59
60
  // Compile with C API
61
4.52k
  shaderc_compiler_t c_compiler = shaderc_compiler_initialize();
62
4.52k
  shaderc_compilation_result_t c_result = shaderc_compile_into_spv(
63
4.52k
      c_compiler, shader_content.c_str(), shader_content.size(),
64
4.52k
      shaderc_glsl_vertex_shader, "main.vert", "main", nullptr);
65
66
4.52k
  if (shaderc_result_get_compilation_status(c_result) ==
67
4.52k
      shaderc_compilation_status_success) {
68
351
    std::vector<uint32_t> spirv_c_binary(shaderc_result_get_length(c_result) /
69
351
                                         sizeof(uint32_t));
70
351
    std::memcpy(spirv_c_binary.data(), shaderc_result_get_bytes(c_result),
71
351
                shaderc_result_get_length(c_result));
72
351
  }
73
4.52k
  shaderc_result_release(c_result);
74
4.52k
  shaderc_compiler_release(c_compiler);
75
4.52k
  return;
76
9.72k
}
77
78
3.24k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
79
  // Skip iteration if data not enough
80
3.24k
  if (size == 0) {
81
0
    return 0;
82
0
  }
83
84
  // Prepare GLSL shader content with valid version
85
86
  // Prepare Compiler and options
87
3.24k
  shaderc::CompileOptions options;
88
3.24k
  options.SetOptimizationLevel(shaderc_optimization_level_performance);
89
3.24k
  execute_shaderc_routines(data, size, options);
90
91
3.24k
  options.SetOptimizationLevel(shaderc_optimization_level_size);
92
3.24k
  execute_shaderc_routines(data, size, options);
93
94
3.24k
  options.SetOptimizationLevel(shaderc_optimization_level_zero);
95
3.24k
  options.SetHlslFunctionality1(true);
96
3.24k
  options.SetHlsl16BitTypes(true);
97
3.24k
  options.SetInvertY(true);
98
3.24k
  options.SetNanClamp(true);
99
3.24k
  options.SetPreserveBindings(true);
100
3.24k
  options.SetAutoMapLocations(true);
101
3.24k
  options.SetHlslOffsets(true);
102
3.24k
  options.SetAutoBindUniforms(true);
103
3.24k
  execute_shaderc_routines(data, size, options);
104
105
3.24k
  return 0;
106
3.24k
}