Coverage Report

Created: 2025-07-04 07:23

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