1 //===--- mlir-bytecode-parser-fuzzer.cpp - Entry point to parser fuzzer ---===//
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
7 //===----------------------------------------------------------------------===//
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"
22 extern "C" LLVM_ATTRIBUTE_USED
int LLVMFuzzerTestOneInput(const uint8_t *data
,
25 if (size
<= 1 || data
[size
- 1] != 0)
27 llvm::StringRef
str(reinterpret_cast<const char *>(data
), size
- 1);
28 // Skip if not bytecode.
29 if (!str
.starts_with("ML\xefR"))
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
);
48 extern "C" LLVM_ATTRIBUTE_USED
int llvmFuzzerInitialize(int *argc
,