1fd4e5da5Sopenharmony_ci// Copyright (c) 2018 Google Inc. 2fd4e5da5Sopenharmony_ci// 3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License. 5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at 6fd4e5da5Sopenharmony_ci// 7fd4e5da5Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8fd4e5da5Sopenharmony_ci// 9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and 13fd4e5da5Sopenharmony_ci// limitations under the License. 14fd4e5da5Sopenharmony_ci 15fd4e5da5Sopenharmony_ci#include <cstdint> 16fd4e5da5Sopenharmony_ci#include <vector> 17fd4e5da5Sopenharmony_ci 18fd4e5da5Sopenharmony_ci#include "spirv-tools/libspirv.hpp" 19fd4e5da5Sopenharmony_ci#include "test/fuzzers/random_generator.h" 20fd4e5da5Sopenharmony_ci 21fd4e5da5Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 22fd4e5da5Sopenharmony_ci if (size < 1) { 23fd4e5da5Sopenharmony_ci return 0; 24fd4e5da5Sopenharmony_ci } 25fd4e5da5Sopenharmony_ci 26fd4e5da5Sopenharmony_ci spvtools::fuzzers::RandomGenerator random_gen(data, size); 27fd4e5da5Sopenharmony_ci spvtools::SpirvTools tools(random_gen.GetTargetEnv()); 28fd4e5da5Sopenharmony_ci tools.SetMessageConsumer([](spv_message_level_t, const char*, 29fd4e5da5Sopenharmony_ci const spv_position_t&, const char*) {}); 30fd4e5da5Sopenharmony_ci 31fd4e5da5Sopenharmony_ci std::vector<uint32_t> input; 32fd4e5da5Sopenharmony_ci input.resize(size >> 2); 33fd4e5da5Sopenharmony_ci 34fd4e5da5Sopenharmony_ci size_t count = 0; 35fd4e5da5Sopenharmony_ci for (size_t i = 0; (i + 3) < size; i += 4) { 36fd4e5da5Sopenharmony_ci input[count++] = data[i] | (data[i + 1] << 8) | (data[i + 2] << 16) | 37fd4e5da5Sopenharmony_ci (data[i + 3]) << 24; 38fd4e5da5Sopenharmony_ci } 39fd4e5da5Sopenharmony_ci 40fd4e5da5Sopenharmony_ci tools.Validate(input); 41fd4e5da5Sopenharmony_ci return 0; 42fd4e5da5Sopenharmony_ci} 43