| Issue |
237736
|
| Summary |
[mlir][bug][fuzzer-found] --normalize-memrefs crashes when erasing a memref.cast user during its walk
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
Kiritobl
|
#### Version
git commit hash: 982197fad060f461277020c0ca413d93a5f2a8a6
LLVM/MLIR 24.0.0git, optimized build with assertions.
#### Bug Description
`--normalize-memrefs` crashes while normalizing a `memref.cast` result with a non-identity layout when the cast is immediately followed by a `memref.store` using that result. The input below parses and verifies successfully without the pass. It was found by fuzzing and reduced to one cast and one store.
The pass walks the function's operations and rewrites the cast result. Replacing its dereferencing user rebuilds and erases the `memref.store`, which is already the walk iterator's next operation. The following iteration dereferences an invalid iterator. The crash reproduced in all three repeated runs on this build, although its manifestation varied between `SIGABRT` from an `ilist_iterator::operator*` assertion and `SIGSEGV`.
#### Crash Chain & Root Cause
Crash chain:
```text
`--normalize-memrefs`
→ NormalizeMemRefs::runOnOperation → normalizeFuncOpMemRefs
→ funcOp.walk visits memref.cast
→ createOpResultsNormalized changes its result to memref<5xf32>
→ replaceAllMemRefUsesWith rebuilds and erases the following memref.store
→ walk dereferences its invalidated next-operation iterator
→ ilist_iterator assertion or SIGSEGV
```
In `mlir/lib/Dialect/MemRef/Transforms/NormalizeMemRefs.cpp:426–475`, the pass uses `funcOp.walk` to process operations with the `MemRefsNormalizable` trait. The cast's result type `memref<4xf32, strided<[1], offset: 1>>` is normalized to `memref<5xf32>`, so the pass calls `affine::replaceAllMemRefUsesWith`. For a `memref.store`, that utility rebuilds the dereferencing operation and erases the old one in `mlir/lib/Dialect/Affine/Utils/Utils.cpp:1306–1307`.
The walk uses `make_early_inc_range` in `mlir/include/mlir/IR/Visitors.h:145–147`: before the cast callback runs, its iterator advances to the following store. Erasing that store invalidates the saved iterator. The next iteration then asserts at `ilist_iterator.h:168` or segfaults. Changing the cast to an identity layout avoids the result-type change and lets the pass finish. Using a non-dereferencing user avoids the iterator crash, but exposes a separate verifier error: the newly built cast to `memref<5xf32>` is incompatible with its source type.
The likely reason for the two crash forms is allocator-dependent reuse of the erased `Operation` storage. The ilist link fields are at the start of that storage; after `free`, glibc may overwrite them with allocator metadata. If the overwritten sentinel bit reads as set, `ilist_iterator::operator*` asserts and produces `SIGABRT`. If it reads as clear, the walk can continue through an invalid node and produce `SIGSEGV`. This explains why the crash occurrence is stable in the observed runs while its signal varies. As a supporting control, disabling glibc tcache with `GLIBC_TUNABLES=glibc.malloc.tcache_count=0` produced `SIGSEGV` in 8/8 runs of the same minimized input on this build.
#### Steps to Reproduce
```bash
mlir-opt --normalize-memrefs reproducer.mlir -o /dev/null
```
In three repeated runs on this build, all three crashed: two with `SIGABRT` (shell exit status 134, `ilist_iterator.h:168`) and one with `SIGSEGV` (status 139). The crash occurrence was reproducible in these runs; the signal was not stable. Running `mlir-opt reproducer.mlir -o /dev/null` without the pass succeeds.
#### Reproducer (`reproducer.mlir`)
```mlir
module {
func.func @f(%src: memref<?xf32, strided<[1], offset: 1>>, %v: f32) {
%c0 = arith.constant 0 : index
%cast = memref.cast %src : memref<?xf32, strided<[1], offset: 1>> to memref<4xf32, strided<[1], offset: 1>>
memref.store %v, %cast[%c0] : memref<4xf32, strided<[1], offset: 1>>
return
}
}
```
#### Stack Dump
The following blocks are unmodified stderr from two runs of the same command and build. The first exited 134 (`SIGABRT`); the second exited 139 (`SIGSEGV`). Both reach `mlir::detail::walk` in `NormalizeMemRefs.cpp`.
`SIGABRT` (exit 134):
```text
mlir-opt: /12T/qin/Fuzzing/MLIR_latest_20261028/llvm-project/llvm/include/llvm/ADT/ilist_iterator.h:168: llvm::ilist_iterator<OptionsT, IsReverse, IsConst>::reference llvm::ilist_iterator<OptionsT, IsReverse, IsConst>::operator*() const [with OptionsT = llvm::ilist_detail::node_options<mlir::Operation, true, false, void, false, void>; bool IsReverse = false; bool IsConst = false; llvm::ilist_iterator<OptionsT, IsReverse, IsConst>::reference = mlir::Operation&]: Assertion `!NodePtr->isKnownSentinel()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt --normalize-memrefs reproducer.mlir -o /dev/null
#0 0x00005573179cd819 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fbb819)
#1 0x00005573179ca01c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fb801c)
#2 0x00005573179ca186 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fb154ad7520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007fb154b2b9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007fb154b2b9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007fb154b2b9bc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007fb154ad7476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fb154abd7f3 abort ./stdlib/abort.c:81:7
#9 0x00007fb154abd71b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007fb154acee96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000055731e33e607 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.2) NormalizeMemRefs.cpp:0:0
#12 0x000055731e341284 (anonymous namespace)::NormalizeMemRefs::runOnOperation() NormalizeMemRefs.cpp:0:0
#13 0x000055731ff9f6d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d6d9)
#14 0x000055731ff9f9b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d9b4)
#15 0x000055731ffa1602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58f602)
#16 0x000055731ffa25d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa5905d9)
#17 0x0000557317a95c69 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x0000557317a9714e processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, mlir::SourceMgrDiagnosticVerifierHandler*, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#19 0x0000557317a97391 llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#20 0x00005573203175ed mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa9055ed)
#21 0x0000557317a9115a mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.part.0) MlirOptMain.cpp:0:0
#22 0x0000557317a979d1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x20859d1)
#23 0x0000557317a97c24 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x2085c24)
#24 0x00005573179688c7 main (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f568c7)
#25 0x00007fb154abed90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#26 0x00007fb154abee40 call_init ./csu/../csu/libc-start.c:128:20
#27 0x00007fb154abee40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#28 0x00005573179ad0b5 _start (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f9b0b5)
```
`SIGSEGV` (exit 139):
```text
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt --normalize-memrefs reproducer.mlir -o /dev/null
#0 0x0000558ad02ae819 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fbb819)
#1 0x0000558ad02ab01c llvm::sys::RunSignalHandlers() (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1fb801c)
#2 0x0000558ad02ab186 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007f52e58ba520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x0000558ad6c1f398 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.2) NormalizeMemRefs.cpp:0:0
#5 0x0000558ad6c22284 (anonymous namespace)::NormalizeMemRefs::runOnOperation() NormalizeMemRefs.cpp:0:0
#6 0x0000558ad88806d9 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d6d9)
#7 0x0000558ad88809b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58d9b4)
#8 0x0000558ad8882602 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa58f602)
#9 0x0000558ad88835d9 mlir::PassManager::run(mlir::Operation*) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa5905d9)
#10 0x0000558ad0376c69 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#11 0x0000558ad037814e processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, mlir::SourceMgrDiagnosticVerifierHandler*, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#12 0x0000558ad0378391 llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#13 0x0000558ad8bf85ed mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0xa9055ed)
#14 0x0000558ad037215a mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.part.0) MlirOptMain.cpp:0:0
#15 0x0000558ad03789d1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x20859d1)
#16 0x0000558ad0378c24 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x2085c24)
#17 0x0000558ad02498c7 main (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f568c7)
#18 0x00007f52e58a1d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#19 0x00007f52e58a1e40 call_init ./csu/../csu/libc-start.c:128:20
#20 0x00007f52e58a1e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#21 0x0000558ad028e0b5 _start (/12T/qin/Fuzzing/MLIR_latest_20261028/install/bin/mlir-opt+0x1f9b0b5)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs