Skip to content

Commit 2c4e073

Browse files
committed
[mlir] Split parser fuzzer for bytecode & text
Enable fuzzing these independently. Currently still not linking in dialects beyond Builtin.
1 parent 78739fd commit 2c4e073

File tree

7 files changed

+109
-20
lines changed

7 files changed

+109
-20
lines changed
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
set(LLVM_LINK_COMPONENTS
2-
FuzzerCLI
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-
)
1+
add_subdirectory(bytecode)
2+
add_subdirectory(text)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
set(LLVM_LINK_COMPONENTS
2+
FuzzerCLI
3+
Support
4+
)
5+
add_llvm_fuzzer(mlir-bytecode-parser-fuzzer
6+
mlir-bytecode-parser-fuzzer.cpp
7+
DUMMY_MAIN DummyParserFuzzer.cpp
8+
)
9+
target_link_libraries(mlir-bytecode-parser-fuzzer
10+
PUBLIC
11+
MLIRIR
12+
MLIRParser
13+
MLIRSupport
14+
)
15+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
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 not bytecode.
29+
if (!str.startswith("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+
48+
extern "C" LLVM_ATTRIBUTE_USED int llvmFuzzerInitialize(int *argc,
49+
char ***argv) {
50+
return 0;
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
set(LLVM_LINK_COMPONENTS
2+
FuzzerCLI
3+
Support
4+
)
5+
add_llvm_fuzzer(mlir-text-parser-fuzzer
6+
mlir-text-parser-fuzzer.cpp
7+
DUMMY_MAIN DummyParserFuzzer.cpp
8+
)
9+
target_link_libraries(mlir-text-parser-fuzzer
10+
PUBLIC
11+
MLIRIR
12+
MLIRParser
13+
MLIRSupport
14+
)
15+
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+
}

mlir/tools/mlir-parser-fuzzer/mlir-parser-fuzzer.cpp renamed to mlir/tools/mlir-parser-fuzzer/text/mlir-text-parser-fuzzer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- mlir-parser-fuzzer.cpp - Entry point to parser fuzzer ------------===//
1+
//===--- mlir-text-parser-fuzzer.cpp - Entry point to parser fuzzer -------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -12,7 +12,6 @@
1212

1313
#include "mlir/IR/BuiltinOps.h"
1414
#include "mlir/IR/Diagnostics.h"
15-
#include "mlir/IR/Dialect.h"
1615
#include "mlir/IR/MLIRContext.h"
1716
#include "mlir/Parser/Parser.h"
1817
#include "llvm/ADT/StringRef.h"
@@ -24,8 +23,11 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerTestOneInput(const uint8_t *data,
2423
size_t size) {
2524
// Skip empty inputs.
2625
if (size <= 1 || data[size - 1] != 0)
27-
return 0;
28-
--size;
26+
return -1;
27+
llvm::StringRef str(reinterpret_cast<const char *>(data), size - 1);
28+
// Skip if bytecode.
29+
if (str.startswith("ML\xefR"))
30+
return -1;
2931

3032
// Create a null-terminated memory buffer from the input.
3133
DialectRegistry registry;
@@ -36,8 +38,6 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerTestOneInput(const uint8_t *data,
3638
context.getDiagEngine().registerHandler(
3739
[](mlir::Diagnostic &diag) { return; });
3840

39-
llvm::StringRef str(reinterpret_cast<const char *>(data), size);
40-
4141
// Parse module. The parsed module isn't used, so it is discarded post parse
4242
// (successful or failure). The returned module is wrapped in a unique_ptr
4343
// such that it is freed upon exit if returned.

0 commit comments

Comments
 (0)