[gn build] Port f22ecdd9b994
[llvm-project.git] / mlir / tools / mlir-parser-fuzzer / text / mlir-text-parser-fuzzer.cpp
blobfaa578bc1fde899dcd55e86ec42ccea030bda056
1 //===--- mlir-text-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.
11 //===----------------------------------------------------------------------===//
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"
20 using namespace mlir;
22 extern "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 bytecode.
29 if (str.starts_with("ML\xefR"))
30 return -1;
32 // Create a null-terminated memory buffer from the input.
33 DialectRegistry registry;
34 MLIRContext context(registry);
35 context.allowUnregisteredDialects();
37 // Register diagnostic handler to avoid triggering exit behavior.
38 context.getDiagEngine().registerHandler(
39 [](mlir::Diagnostic &diag) { return; });
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;
48 extern "C" LLVM_ATTRIBUTE_USED int llvmFuzzerInitialize(int *argc,
49 char ***argv) {
50 return 0;