[llvm] 94e6d26 - [ORC] Fix serialization / deserialization of default-constructed StringRef.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 19 17:23:47 PDT 2022
Author: Lang Hames
Date: 2022-07-19T17:22:21-07:00
New Revision: 94e6d2677bd3748b344aa5abccfc7284dbea8be0
URL: https://github.com/llvm/llvm-project/commit/94e6d2677bd3748b344aa5abccfc7284dbea8be0
DIFF: https://github.com/llvm/llvm-project/commit/94e6d2677bd3748b344aa5abccfc7284dbea8be0.diff
LOG: [ORC] Fix serialization / deserialization of default-constructed StringRef.
Avoids accessing the data field on zero-length strings. This is the StringRef
counterpart to the ArrayRef<char> fix in 67220c2ad72e3.
rdar://97285294
Added:
Modified:
llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
index 794c07b4e4c0b..c388259482088 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h
@@ -479,6 +479,8 @@ template <> class SPSSerializationTraits<SPSString, StringRef> {
static bool serialize(SPSOutputBuffer &OB, StringRef S) {
if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(S.size())))
return false;
+ if (S.empty()) // Empty StringRef may have null data, so bail out early.
+ return true;
return OB.write(S.data(), S.size());
}
@@ -490,7 +492,7 @@ template <> class SPSSerializationTraits<SPSString, StringRef> {
Data = IB.data();
if (!IB.skip(Size))
return false;
- S = StringRef(Data, Size);
+ S = StringRef(Size ? Data : nullptr, Size);
return true;
}
};
diff --git a/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp b/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
index f5319e3287766..ee73614d53d44 100644
--- a/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp
@@ -208,3 +208,24 @@ TEST(SimplePackedSerializationTest, ArrayRefEmpty) {
EXPECT_EQ(AIn.data(), nullptr);
EXPECT_EQ(AIn.size(), 0U);
}
+
+TEST(SimplePackedSerializationTest, StringRefEmpty) {
+ // Make sure that empty StringRefs serialize and deserialize as expected.
+ // Empty StringRefs should not succeed even when the data field is null, and
+ // should deserialize to a default-constructed StringRef, not a pointer into
+ // the stream.
+ constexpr unsigned BufferSize = sizeof(uint64_t);
+ char Buffer[BufferSize];
+ memset(Buffer, 0, BufferSize);
+
+ StringRef SROut;
+ SPSOutputBuffer OB(Buffer, BufferSize);
+ EXPECT_TRUE(SPSArgList<SPSSequence<char>>::serialize(OB, SROut));
+
+ StringRef SRIn;
+ SPSInputBuffer IB(Buffer, BufferSize);
+ EXPECT_TRUE(SPSArgList<SPSSequence<char>>::deserialize(IB, SRIn));
+
+ EXPECT_EQ(SRIn.data(), nullptr);
+ EXPECT_EQ(SRIn.size(), 0U);
+}
More information about the llvm-commits
mailing list