Coverage Report

Created: 2025-06-13 06:48

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