1 //===--- mlir-bytecode-parser-fuzzer.cpp - Entry point to parser fuzzer ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Implementation of main so we can build and test without linking libFuzzer. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/IR/BuiltinOps.h" 14 #include "mlir/IR/Diagnostics.h" 15 #include "mlir/IR/MLIRContext.h" 16 #include "mlir/Parser/Parser.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Compiler.h" 19 20 using namespace mlir; 21 LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)22extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerTestOneInput(const uint8_t *data, 23 size_t size) { 24 // Skip empty inputs. 25 if (size <= 1 || data[size - 1] != 0) 26 return -1; 27 llvm::StringRef str(reinterpret_cast<const char *>(data), size - 1); 28 // Skip if not bytecode. 29 if (!str.starts_with("ML\xefR")) 30 return -1; 31 32 // Create a null-terminated memory buffer from the input. 33 DialectRegistry registry; 34 MLIRContext context(registry); 35 context.allowUnregisteredDialects(); 36 37 // Register diagnostic handler to avoid triggering exit behavior. 38 context.getDiagEngine().registerHandler( 39 [](mlir::Diagnostic &diag) { return; }); 40 41 // Parse module. The parsed module isn't used, so it is discarded post parse 42 // (successful or failure). The returned module is wrapped in a unique_ptr 43 // such that it is freed upon exit if returned. 44 (void)parseSourceString<ModuleOp>(str, &context); 45 return 0; 46 } 47 llvmFuzzerInitialize(int * argc,char *** argv)48extern "C" LLVM_ATTRIBUTE_USED int llvmFuzzerInitialize(int *argc, 49 char ***argv) { 50 return 0; 51 } 52