• 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 #include "test/fuzzers/random_generator.h"
22 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
24   spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
25   if (size > 0) {
26     spvtools::fuzzers::RandomGenerator random_gen(data, size);
27     target_env = random_gen.GetTargetEnv();
28   }
29 
30   const spv_context context = spvContextCreate(target_env);
31   if (context == nullptr) {
32     return 0;
33   }
34 
35   std::vector<char> contents;
36   contents.resize(size);
37   memcpy(contents.data(), data, size);
38 
39   spv_binary binary = nullptr;
40   spv_diagnostic diagnostic = nullptr;
41   spvTextToBinaryWithOptions(context, contents.data(), contents.size(),
42                              SPV_TEXT_TO_BINARY_OPTION_NONE, &binary,
43                              &diagnostic);
44   if (diagnostic) {
45     spvDiagnosticPrint(diagnostic);
46     spvDiagnosticDestroy(diagnostic);
47     diagnostic = nullptr;
48   }
49 
50   if (binary) {
51     spvBinaryDestroy(binary);
52     binary = nullptr;
53   }
54 
55   spvTextToBinaryWithOptions(context, contents.data(), contents.size(),
56                              SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS,
57                              &binary, &diagnostic);
58   if (diagnostic) {
59     spvDiagnosticPrint(diagnostic);
60     spvDiagnosticDestroy(diagnostic);
61     diagnostic = nullptr;
62   }
63 
64   if (binary) {
65     spvBinaryDestroy(binary);
66     binary = nullptr;
67   }
68 
69   spvContextDestroy(context);
70 
71   return 0;
72 }
73