Coverage Report

Created: 2026-01-05 06:41

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