Skip to content

Commit 450692d

Browse files
committed
[mlir] Add simple fuzzer for textual format
Only use this on generic parser for now by not registering any dialect. For flushing out some parser bugs. The textual format is not meant to be load bearing in production runs, but still useful to remove edge cases/failures. Differential Revision: https://reviews.llvm.org/D122267
1 parent 528e6eb commit 450692d

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

mlir/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_subdirectory(mlir-lsp-server)
22
add_subdirectory(mlir-opt)
3+
add_subdirectory(mlir-parser-fuzzer)
34
add_subdirectory(mlir-pdll)
45
add_subdirectory(mlir-pdll-lsp-server)
56
add_subdirectory(mlir-reduce)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(LLVM_LINK_COMPONENTS
2+
FuzzMutate
3+
Support
4+
)
5+
add_llvm_fuzzer(mlir-parser-fuzzer
6+
mlir-parser-fuzzer.cpp
7+
DUMMY_MAIN DummyParserFuzzer.cpp
8+
)
9+
target_link_libraries(mlir-parser-fuzzer
10+
PUBLIC
11+
MLIRIR
12+
MLIRParser
13+
MLIRSupport
14+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===--- DummyParserFuzzer.cpp - Entry point to sanity check the 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 "llvm/FuzzMutate/FuzzerCLI.h"
14+
15+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
16+
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
17+
int main(int argc, char *argv[]) {
18+
return llvm::runFuzzerOnInputs(argc, argv, LLVMFuzzerTestOneInput,
19+
LLVMFuzzerInitialize);
20+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===--- mlir-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/Diagnostics.h"
14+
#include "mlir/IR/Dialect.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+
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 0;
27+
--size;
28+
29+
// Create a null-terminated memory buffer from the input.
30+
DialectRegistry registry;
31+
MLIRContext context(registry);
32+
context.allowUnregisteredDialects();
33+
34+
// Register diagnostic handler to avoid triggering exit behavior.
35+
context.getDiagEngine().registerHandler(
36+
[](mlir::Diagnostic &diag) { return; });
37+
38+
llvm::StringRef str(reinterpret_cast<const char *>(data), size);
39+
40+
// Parse module. The parsed module isn't used, so it is discarded post parse
41+
// (successful or failure). The returned module is wrapped in a unique_ptr
42+
// such that it is freed upon exit if returned.
43+
(void)parseSourceString<ModuleOp>(str, &context);
44+
return 0;
45+
}
46+
47+
extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
48+
char ***argv) {
49+
return 0;
50+
}

0 commit comments

Comments
 (0)