| Issue |
239111
|
| Summary |
[mlir] Stack overflow in the parser on deeply nested regions
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
gfx
|
The textual parser recurses once per nested region, so a deeply nested module
overflows the stack and takes the process down with a segmentation fault rather
than returning a diagnostic.
## Reproducer
```sh
n=5000
{ yes '"t.o"() ({' | head -n "$n"; yes '}) : () -> ()' | head -n "$n"; } > nested.mlir
mlir-opt --allow-unregistered-dialect -o /dev/null nested.mlir
```
Reproduced with the `mlir-opt` of the LLVM 23.1.5 release
(`LLVM-23.1.5-Linux-X64.tar.zst`) on x86-64 Linux, with the default 8 MiB stack.
| Nesting depth | Exit status |
| ------------- | ------------- |
| 2,000 | 0 |
| 2,500 | 139 (SIGSEGV) |
| 5,000 | 139 (SIGSEGV) |
| 100,000 | 139 (SIGSEGV) |
With `ulimit -s 1048576`, a depth of 100,000 exits 0, so the limit is the stack.
## Backtrace
>From gdb at a depth of 5,000, 10,195 frames deep. Only the parser recurses:
```text
#0 (anonymous namespace)::OperationParser::parseOperation()
#1 (anonymous namespace)::OperationParser::parseBlock(mlir::Block*&)
#2 (anonymous namespace)::OperationParser::parseRegion(mlir::Region&, ...)
#3 (anonymous namespace)::OperationParser::parseGenericOperationAfterOpName(...)
#4 (anonymous namespace)::OperationParser::parseGenericOperation()
#5 (anonymous namespace)::OperationParser::parseOperation()
...
#10191 mlir::MlirOptMain(llvm::raw_ostream&, ...)
#10194 main
```
## Why it matters
An embedder that takes MLIR text from another program loses the whole process.
We hit this through a PJRT plugin, which parses a StableHLO module in-process:
the plugin's host dies with no error it can report. A diagnostic would let the
host fail the one compile.
## Related
- #63146 reported this for the verifier and mentioned the parser. Commit
7689179ac9c4 made the verifier use a worklist, and its message covers the
verifier only.
- [RFC: Update to MLIR developer policy on recursion](https://discourse.llvm.org/t/rfc-update-to-mlir-developer-policy-on-recursion/62235)
discussed a depth limit in the reader, which would turn this crash into a
diagnostic.
(Assisted by Claude Code)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs