fuzzer_stub.cc (1611B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include <jxl/thread_parallel_runner.h> 7 #include <jxl/thread_parallel_runner_cxx.h> 8 9 #include <fstream> 10 #include <iostream> 11 #include <iterator> 12 #include <vector> 13 14 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); 15 16 void ProcessInput(const char* filename) { 17 std::ifstream ifs(filename, std::ios::binary); 18 std::vector<char> contents((std::istreambuf_iterator<char>(ifs)), 19 std::istreambuf_iterator<char>()); 20 ifs.close(); 21 std::cout << "Processing " << filename << std::endl; 22 LLVMFuzzerTestOneInput(reinterpret_cast<uint8_t*>(contents.data()), 23 contents.size()); 24 } 25 26 // Read files listed in args and pass their contents to "fuzzer". 27 int main(int argc, const char* argv[]) { 28 if (argc == 2) { 29 // No threaded runner for single inputs. 30 ProcessInput(argv[1]); 31 } else if (argc > 2) { 32 auto runner = JxlThreadParallelRunnerMake( 33 nullptr, JxlThreadParallelRunnerDefaultNumWorkerThreads()); 34 return JxlThreadParallelRunner( 35 static_cast<void*>(runner.get()), reinterpret_cast<void*>(argv), 36 /* init= */ +[](void*, size_t) -> JxlParallelRetCode { return 0; }, 37 /* func= */ 38 +[](void* opaque, uint32_t value, size_t) { 39 const char** proc_argv = static_cast<const char**>(opaque); 40 ProcessInput(proc_argv[value]); 41 }, 42 /* start_range= */ 1, /* end_range= */ argc); 43 } 44 return 0; 45 }