<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99626>99626</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][BytecodeReader] Crash when reading FusedLoc with empty elements
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
uenoku
</td>
</tr>
</table>
<pre>
FusedLoc's DialectAttribute [cBuilder](https://github.com/llvm/llvm-project/blob/6b98ab95f0d36705b5a1fc1e755c992ba2329c89/mlir/include/mlir/IR/BuiltinDialectBytecode.td#L105) is `get<FusedLoc>(get<FusedLoc>(context, $_args))` but `FusedLoc::get(context, locs)` could [return](https://github.com/llvm/llvm-project/blob/main/mlir/lib/IR/Location.cpp#L87) `UnknownLoc` when `locs` is empty. So `cast<FusedLoc>(..)` crashes when reading bytecode.
Testcase:
```c++
diff --git a/mlir/unittests/Bytecode/BytecodeTest.cpp b/mlir/unittests/Bytecode/BytecodeTest.cpp
index a37a2afc2264..210499cfaf55 100644
--- a/mlir/unittests/Bytecode/BytecodeTest.cpp
+++ b/mlir/unittests/Bytecode/BytecodeTest.cpp
@@ -12,6 +12,7 @@
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/OwningOpRef.h"
+#include "mlir/IR/BuiltinOps.h"
#include "mlir/Parser/Parser.h"
#include "llvm/ADT/StringRef.h"
@@ -148,3 +149,25 @@ TEST(Bytecode, OpWithoutProperties) {
EXPECT_TRUE(OperationEquivalence::computeHash(op.get()) ==
OperationEquivalence::computeHash(roundtripped));
}
+
+TEST(Bytecode, FusedLocCrash) {
+ MLIRContext context;
+ OpBuilder builder(&context);
+ SmallVector<Location> locs;
+ FusedLoc fusedLoc = FusedLoc::get(&context, locs, {});
+
+ auto module = builder.create<mlir::ModuleOp>(fusedLoc, "test");
+
+
+ // Write the module to bytecode
+ std::string buffer;
+ llvm::raw_string_ostream ostream(buffer);
+ ASSERT_TRUE(succeeded(writeBytecodeToFile(module, ostream)));
+ ostream.flush();
+
+ // Parse it back
+ ParserConfig parseConfig(&context);
+ OwningOpRef<Operation *> roundTripModule =
+ parseSourceString<Operation *>(buffer, parseConfig);
+ ASSERT_TRUE(roundTripModule);
+}
```
Stack trace:
```bash
******************** TEST 'MLIR-Unit :: Bytecode/./MLIRBytecodeTests/2/3' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/scratch/hidetou/circt/llvm/build_tmp/tools/mlir/unittests/Bytecode/./MLIRBytecodeTests-MLIR-Unit-748224-2-3.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=3 GTEST_SHARD_INDEX=2 /scratch/hidetou/circt/llvm/build_tmp/tools/mlir/unittests/Bytecode/./MLIRBytecodeTests
--
Note: This is test shard 3 of 3.
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Bytecode
[ RUN ] Bytecode.FusedLocCrash
MLIRBytecodeTests: /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From &) [To = mlir::FusedLoc, From = mlir::Location]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
#0 0x000000000041d9d7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /scratch/hidetou/circt/llvm/llvm/lib/Support/Unix/Signals.inc:723:13
#1 0x000000000041bd42 llvm::sys::RunSignalHandlers() /scratch/hidetou/circt/llvm/llvm/lib/Support/Signals.cpp:106:18
#2 0x000000000041e31f SignalHandler(int) /scratch/hidetou/circt/llvm/llvm/lib/Support/Unix/Signals.inc:413:1
#3 0x00007fd2013dfcf0 __restore_rt (/lib64/libpthread.so.0+0x12cf0)
#4 0x00007fd200497acf raise (/lib64/libc.so.6+0x4eacf)
#5 0x00007fd20046aea5 abort (/lib64/libc.so.6+0x21ea5)
#6 0x00007fd20046ad79 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d79)
#7 0x00007fd200490426 (/lib64/libc.so.6+0x47426)
#8 0x0000000000532411 llvm::SmallVectorTemplateCommon<mlir::Location, void>::getFirstEl() const /scratch/hidetou/circt/llvm/llvm/include/llvm/ADT/SmallVector.h:143:46
#9 0x0000000000532411 llvm::SmallVectorTemplateCommon<mlir::Location, void>::SmallVectorTemplateCommon(unsigned long) /scratch/hidetou/circt/llvm/llvm/include/llvm/ADT/SmallVector.h:148:49
#10 0x0000000000532411 llvm::SmallVectorTemplateBase<mlir::Location, true>::SmallVectorTemplateBase(unsigned long) /scratch/hidetou/circt/llvm/llvm/include/llvm/ADT/SmallVector.h:500:42
#11 0x0000000000532411 llvm::SmallVectorImpl<mlir::Location>::SmallVectorImpl(unsigned int) /scratch/hidetou/circt/llvm/llvm/include/llvm/ADT/SmallVector.h:601:9
#12 0x0000000000532411 llvm::SmallVector<mlir::Location, 6u>::SmallVector() /scratch/hidetou/circt/llvm/llvm/include/llvm/ADT/SmallVector.h:1211:19
#13 0x0000000000532411 (anonymous namespace)::readFusedLocWithMetadata(mlir::MLIRContext*, mlir::DialectBytecodeReader&) /scratch/hidetou/circt/llvm/build_tmp/tools/mlir/include/mlir/IR/BuiltinDialectBytecode.cpp.inc:200:25
#14 0x0000000000532411 (anonymous namespace)::readAttribute(mlir::MLIRContext*, mlir::DialectBytecodeReader&) /scratch/hidetou/circt/llvm/build_tmp/tools/mlir/include/mlir/IR/BuiltinDialectBytecode.cpp.inc:390:14
#15 0x0000000000532411 (anonymous namespace)::BuiltinDialectBytecodeInterface::readAttribute(mlir::DialectBytecodeReader&) const /scratch/hidetou/circt/llvm/mlir/lib/IR/BuiltinDialectBytecode.cpp:94:12
#16 0x0000000000484b5b llvm::LogicalResult (anonymous namespace)::AttrTypeReader::parseCustomEntry<mlir::Attribute>((anonymous namespace)::AttrTypeReader::Entry<mlir::Attribute>&, (anonymous namespace)::EncodingReader&, llvm::StringRef) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:0:45
#17 0x0000000000484b5b mlir::Attribute (anonymous namespace)::AttrTypeReader::resolveEntry<mlir::Attribute>(llvm::SmallVectorImpl<(anonymous namespace)::AttrTypeReader::Entry<mlir::Attribute>>&, unsigned long, llvm::StringRef) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:1211:16
#18 0x0000000000484b5b (anonymous namespace)::AttrTypeReader::resolveAttribute(unsigned long) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:822:12
#19 0x0000000000487394 (anonymous namespace)::AttrTypeReader::parseAttribute((anonymous namespace)::EncodingReader&, mlir::Attribute&) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:831:12
#20 0x0000000000487394 llvm::LogicalResult (anonymous namespace)::AttrTypeReader::parseAttribute<mlir::LocationAttr>((anonymous namespace)::EncodingReader&, mlir::LocationAttr&) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:857:16
#21 0x0000000000487394 llvm::LogicalResult mlir::BytecodeReader::Impl::parseAttribute<mlir::LocationAttr>((anonymous namespace)::EncodingReader&, mlir::LocationAttr&) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:1424:27
#22 0x00000000004800e3 llvm::LogicalResult::failed() const /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:43:43
#23 0x00000000004800e3 llvm::failed(llvm::LogicalResult) /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:71:58
#24 0x00000000004800e3 mlir::BytecodeReader::Impl::parseOpWithoutRegions((anonymous namespace)::EncodingReader&, mlir::BytecodeReader::Impl::RegionReadState&, bool&) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:2233:7
#25 0x00000000004800e3 mlir::BytecodeReader::Impl::parseRegions(std::vector<mlir::BytecodeReader::Impl::RegionReadState, std::allocator<mlir::BytecodeReader::Impl::RegionReadState>>&, mlir::BytecodeReader::Impl::RegionReadState&) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:2150:13
#26 0x000000000047dbdf llvm::LogicalResult::failed() const /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:43:43
#27 0x000000000047dbdf llvm::failed(llvm::LogicalResult) /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:71:58
#28 0x000000000047dbdf mlir::BytecodeReader::Impl::parseIRSection(llvm::ArrayRef<unsigned char>, mlir::Block*) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:2087:9
#29 0x000000000047c582 mlir::BytecodeReader::Impl::read(mlir::Block*, llvm::function_ref<bool (mlir::Operation*)>) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:1710:10
#30 0x0000000000482316 llvm::LogicalResult::failed() const /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:43:43
#31 0x0000000000482316 llvm::failed(llvm::LogicalResult) /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:71:58
#32 0x0000000000482316 readBytecodeFileImpl(llvm::MemoryBufferRef, mlir::Block*, mlir::ParserConfig const&, std::shared_ptr<llvm::SourceMgr> const&, bool, llvm::function_ref<bool (mlir::Operation*)>, llvm::function_ref<bool (mlir::Operation*)>) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:2590:7
#33 0x0000000000482190 mlir::readBytecodeFile(llvm::MemoryBufferRef, mlir::Block*, mlir::ParserConfig const&, bool, llvm::function_ref<bool (mlir::Operation*)>, llvm::function_ref<bool (mlir::Operation*)>) /scratch/hidetou/circt/llvm/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:2604:10
#34 0x00000000004a8f32 mlir::parseSourceFile(llvm::SourceMgr const&, mlir::Block*, mlir::ParserConfig const&, mlir::LocationAttr*) /scratch/hidetou/circt/llvm/mlir/lib/Parser/Parser.cpp:0:12
#35 0x00000000004a9048 mlir::parseSourceString(llvm::StringRef, mlir::Block*, mlir::ParserConfig const&, llvm::StringRef, mlir::LocationAttr*) /scratch/hidetou/circt/llvm/mlir/lib/Parser/Parser.cpp:102:10
#36 0x000000000040c5ca llvm::LogicalResult::failed() const /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:43:43
#37 0x000000000040c5ca llvm::failed(llvm::LogicalResult) /scratch/hidetou/circt/llvm/llvm/include/llvm/Support/LogicalResult.h:71:58
#38 0x000000000040c5ca mlir::OwningOpRef<mlir::Operation*> mlir::parseSourceString<mlir::Operation*>(llvm::StringRef, mlir::ParserConfig const&, llvm::StringRef) /scratch/hidetou/circt/llvm/mlir/include/mlir/Parser/Parser.h:250:7
#39 0x000000000040c5ca Bytecode_FusedLocCrash_Test::TestBody() /scratch/hidetou/circt/llvm/mlir/unittests/Bytecode/BytecodeTest.cpp:171:7
#40 0x000000000045d3f9 testing::internal::UnitTestImpl::os_stack_trace_getter() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:6239:7
#41 0x000000000045d3f9 testing::Test::Run() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:2694:9
#42 0x000000000045de6f testing::TestInfo::Run() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:2839:12
#43 0x000000000045e927 testing::TestSuite::Run() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:3017:9
#44 0x000000000046e397 testing::internal::UnitTestImpl::RunAllTests() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:5921:15
#45 0x000000000046dce8 testing::UnitTest::Run() /scratch/hidetou/circt/llvm/third-party/unittest/googletest/src/gtest.cc:5484:10
#46 0x000000000044d0ec main /scratch/hidetou/circt/llvm/third-party/unittest/UnitTestMain/TestMain.cpp:55:3
#47 0x00007fd200483d85 __libc_start_main (/lib64/libc.so.6+0x3ad85)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkW1tT47iX_zTiRZWULfn6wEMu5N9U0UNXCDPzlpJlOdG0Y3ktubv59luS7yYEEga2d5dKJcaWjs7v3HUxkZLvMsaugTsH7vKKlGoviuuSZeJ7eRWJ-Ol6VUoW3wkKkC_hkpOUUTVTquBRqRgE7pzOS57GrADuEqBgr1QuAZ4BtAJoteNqX0ZTKg4ArdL0R_MzyQvxD6MKoFWUigiglReFAYlCN7Fi7PmWG7nETqjNfNelYYgigjAKaRACtDqkvABoxTOaljHrbtyuAVppZhTPakbnT4pREbOpigHCd7blAhRCLiHwrB1TAC9aePgGoODYPSoyxX4pgBYQIGdLip0EKNQfz4JRqTStrscM4JmmMuiXCirrDlSUaazFVjBVFtn7hHYgPOvwpzxqpHAnKFFcZFOa5xp44GvcwLMes--Z-JlpVj0L_tyzTN81_HmWFgw75OppCh-Evk-JfC6P6bSBUhC5Z7KiUjAS82wHo0biEAJrCaxZ_3vDpKJEMoBnzVPPqj4UoLn-mLsxTxI4mey4gqTDV2ZcKSaV1Gquh-ldauIaL4zO7lKNyrOY_YIE-wSRhCLkOdMpsi0nDGlCEteFtmV5jlM1nkwmF_BWg66govnFrALHAo4FJzYCaOFBgObmyofVg6oRBAjXTgIBQke8pPVjOd0DhF7tdp_fHvKUHVimKvN6W6-fGc929_maJb0ORgKv8Hefv87YN1JI1l0M2h_rVjvUbLkBaPWgCp7tRow1snUCgBbYCNcJAVogtxYv3Nw8bAAKOjUt4H3-F1d7UapvhchZoTiTxuX82qThzd_fbhab7Wb9qJ3oPmeFEeHNf5X8B0lZRlkVPKg45KViX4jcAxSIfFpFkyrkQICX-lORfCuRQpRZrAqe5yyuQxdu2AL-stNHc3EEXxMDFoWh2UOm7Rh-vbtdL6qAB5vAh3sN7vM6S8CozhYaktfGyHDQGj4cSJr-yagSBcCLJpoBfFOF0kHbhjOYNBcAL-GxiNwfsAnKC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