Coverage Report

Created: 2026-06-04 06:41

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