• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 Google Inc.
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 <cstdint>
16 #include <cstring>  // memcpy
17 #include <vector>
18 
19 #include "source/spirv_target_env.h"
20 #include "spirv-tools/libspirv.hpp"
21 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
23   if (size < sizeof(spv_target_env) + 1) return 0;
24 
25   const spv_context context =
26       spvContextCreate(*reinterpret_cast<const spv_target_env*>(data));
27   if (context == nullptr) return 0;
28 
29   data += sizeof(spv_target_env);
30   size -= sizeof(spv_target_env);
31 
32   std::vector<uint32_t> input;
33   input.resize(size >> 2);
34   size_t count = 0;
35   for (size_t i = 0; (i + 3) < size; i += 4) {
36     input[count++] = data[i] | (data[i + 1] << 8) | (data[i + 2] << 16) |
37                      (data[i + 3]) << 24;
38   }
39 
40   std::vector<char> input_str;
41   size_t char_count = input.size() * sizeof(uint32_t) / sizeof(char);
42   input_str.resize(char_count);
43   memcpy(input_str.data(), input.data(), input.size() * sizeof(uint32_t));
44 
45   spv_text text = nullptr;
46   spv_diagnostic diagnostic = nullptr;
47 
48   for (uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
49        options <
50        (SPV_BINARY_TO_TEXT_OPTION_PRINT | SPV_BINARY_TO_TEXT_OPTION_COLOR |
51         SPV_BINARY_TO_TEXT_OPTION_INDENT |
52         SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET |
53         SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
54         SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
55        options++) {
56     spvBinaryToText(context, input.data(), input.size(), options, &text,
57                     &diagnostic);
58     if (diagnostic) {
59       spvDiagnosticDestroy(diagnostic);
60       diagnostic = nullptr;
61     }
62 
63     if (text) {
64       spvTextDestroy(text);
65       text = nullptr;
66     }
67   }
68 
69   spvContextDestroy(context);
70   return 0;
71 }
72