//===---- SimplePackedSerialization.h - simple serialization ----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // The behavior of the utilities in this header must be synchronized with the // behavior of the utilities in // compiler-rt/lib/orc/simple_packed_serialization.h. // // The Simple Packed Serialization (SPS) utilities are used to generate // argument and return buffers for wrapper functions using the following // serialization scheme: // // Primitives (signed types should be two's complement): // bool, char, int8_t, uint8_t -- 8-bit (0=false, 1=true) // int16_t, uint16_t -- 16-bit little endian // int32_t, uint32_t -- 32-bit little endian // int64_t, int64_t -- 64-bit little endian // // Sequence: // Serialized as the sequence length (as a uint64_t) followed by the // serialization of each of the elements without padding. // // Tuple: // Serialized as each of the element types from T1 to TN without padding. // //===----------------------------------------------------------------------===// #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEPACKEDSERIALIZATION_H #define LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEPACKEDSERIALIZATION_H #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" #include "llvm/Support/SwapByteOrder.h" #include #include #include #include #include #include #include namespace llvm { namespace orc { namespace shared { /// Output char buffer with overflow check. class SPSOutputBuffer { public: SPSOutputBuffer(char *Buffer, size_t Remaining) : Buffer(Buffer), Remaining(Remaining) {} bool write(const char *Data, size_t Size) { assert(Data && "Data must not be null"); if (Size > Remaining) return false; memcpy(Buffer, Data, Size); Buffer += Size; Remaining -= Size; return true; } private: char *Buffer = nullptr; size_t Remaining = 0; }; /// Input char buffer with underflow check. class SPSInputBuffer { public: SPSInputBuffer() = default; SPSInputBuffer(const char *Buffer, size_t Remaining) : Buffer(Buffer), Remaining(Remaining) {} bool read(char *Data, size_t Size) { if (Size > Remaining) return false; memcpy(Data, Buffer, Size); Buffer += Size; Remaining -= Size; return true; } const char *data() const { return Buffer; } bool skip(size_t Size) { if (Size > Remaining) return false; Buffer += Size; Remaining -= Size; return true; } private: const char *Buffer = nullptr; size_t Remaining = 0; }; /// Specialize to describe how to serialize/deserialize to/from the given /// concrete type. template class SPSSerializationTraits; /// A utility class for serializing to a blob from a variadic list. template class SPSArgList; // Empty list specialization for SPSArgList. template <> class SPSArgList<> { public: static size_t size() { return 0; } static bool serialize(SPSOutputBuffer &OB) { return true; } static bool deserialize(SPSInputBuffer &IB) { return true; } static bool serializeToSmallVector(SmallVectorImpl &V) { return true; } static bool deserializeFromSmallVector(const SmallVectorImpl &V) { return true; } }; // Non-empty list specialization for SPSArgList. template class SPSArgList { public: // FIXME: This typedef is here to enable SPS arg serialization from // JITLink. It can be removed once JITLink can access SPS directly. using OutputBuffer = SPSOutputBuffer; template static size_t size(const ArgT &Arg, const ArgTs &...Args) { return SPSSerializationTraits::size(Arg) + SPSArgList::size(Args...); } template static bool serialize(SPSOutputBuffer &OB, const ArgT &Arg, const ArgTs &...Args) { return SPSSerializationTraits::serialize(OB, Arg) && SPSArgList::serialize(OB, Args...); } template static bool deserialize(SPSInputBuffer &IB, ArgT &Arg, ArgTs &...Args) { return SPSSerializationTraits::deserialize(IB, Arg) && SPSArgList::deserialize(IB, Args...); } }; /// SPS serialization for integral types, bool, and char. template class SPSSerializationTraits< SPSTagT, SPSTagT, std::enable_if_t::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::value>> { public: static size_t size(const SPSTagT &Value) { return sizeof(SPSTagT); } static bool serialize(SPSOutputBuffer &OB, const SPSTagT &Value) { SPSTagT Tmp = Value; if (sys::IsBigEndianHost) sys::swapByteOrder(Tmp); return OB.write(reinterpret_cast(&Tmp), sizeof(Tmp)); } static bool deserialize(SPSInputBuffer &IB, SPSTagT &Value) { SPSTagT Tmp; if (!IB.read(reinterpret_cast(&Tmp), sizeof(Tmp))) return false; if (sys::IsBigEndianHost) sys::swapByteOrder(Tmp); Value = Tmp; return true; } }; // Any empty placeholder suitable as a substitute for void when deserializing class SPSEmpty {}; /// SPS tag type for tuples. /// /// A blob tuple should be serialized by serializing each of the elements in /// sequence. template class SPSTuple { public: /// Convenience typedef of the corresponding arg list. typedef SPSArgList AsArgList; }; /// SPS tag type for optionals. /// /// SPSOptionals should be serialized as a bool with true indicating that an /// SPSTagT value is present, and false indicating that there is no value. /// If the boolean is true then the serialized SPSTagT will follow immediately /// after it. template class SPSOptional {}; /// SPS tag type for sequences. /// /// SPSSequences should be serialized as a uint64_t sequence length, /// followed by the serialization of each of the elements. template class SPSSequence; /// SPS tag type for strings, which are equivalent to sequences of chars. using SPSString = SPSSequence; /// SPS tag type for maps. /// /// SPS maps are just sequences of (Key, Value) tuples. template using SPSMap = SPSSequence>; /// Serialization for SPSEmpty type. template <> class SPSSerializationTraits { public: static size_t size(const SPSEmpty &EP) { return 0; } static bool serialize(SPSOutputBuffer &OB, const SPSEmpty &BE) { return true; } static bool deserialize(SPSInputBuffer &IB, SPSEmpty &BE) { return true; } }; /// Specialize this to implement 'trivial' sequence serialization for /// a concrete sequence type. /// /// Trivial sequence serialization uses the sequence's 'size' member to get the /// length of the sequence, and uses a range-based for loop to iterate over the /// elements. /// /// Specializing this template class means that you do not need to provide a /// specialization of SPSSerializationTraits for your type. template class TrivialSPSSequenceSerialization { public: static constexpr bool available = false; }; /// Specialize this to implement 'trivial' sequence deserialization for /// a concrete sequence type. /// /// Trivial deserialization calls a static 'reserve(SequenceT&)' method on your /// specialization (you must implement this) to reserve space, and then calls /// a static 'append(SequenceT&, ElementT&) method to append each of the /// deserialized elements. /// /// Specializing this template class means that you do not need to provide a /// specialization of SPSSerializationTraits for your type. template class TrivialSPSSequenceDeserialization { public: static constexpr bool available = false; }; /// Trivial std::string -> SPSSequence serialization. template <> class TrivialSPSSequenceSerialization { public: static constexpr bool available = true; }; /// Trivial SPSSequence -> std::string deserialization. template <> class TrivialSPSSequenceDeserialization { public: static constexpr bool available = true; using element_type = char; static void reserve(std::string &S, uint64_t Size) { S.reserve(Size); } static bool append(std::string &S, char C) { S.push_back(C); return true; } }; /// Trivial std::vector -> SPSSequence serialization. template class TrivialSPSSequenceSerialization> { public: static constexpr bool available = true; }; /// Trivial SPSSequence -> std::vector deserialization. template class TrivialSPSSequenceDeserialization> { public: static constexpr bool available = true; using element_type = typename std::vector::value_type; static void reserve(std::vector &V, uint64_t Size) { V.reserve(Size); } static bool append(std::vector &V, T E) { V.push_back(std::move(E)); return true; } }; /// Trivial SmallVectorImpl -> SPSSequence serialization. template class TrivialSPSSequenceSerialization> { public: static constexpr bool available = true; }; /// Trivial SPSSequence -> SmallVectorImpl deserialization. template class TrivialSPSSequenceDeserialization> { public: static constexpr bool available = true; using element_type = typename SmallVectorImpl::value_type; static void reserve(SmallVectorImpl &V, uint64_t Size) { V.reserve(Size); } static bool append(SmallVectorImpl &V, T E) { V.push_back(std::move(E)); return true; } }; /// Trivial SmallVectorImpl -> SPSSequence serialization. template class TrivialSPSSequenceSerialization> : public TrivialSPSSequenceSerialization> {}; /// Trivial SPSSequence -> SmallVectorImpl deserialization. template class TrivialSPSSequenceDeserialization> : public TrivialSPSSequenceDeserialization> {}; /// Trivial ArrayRef -> SPSSequence serialization. template class TrivialSPSSequenceSerialization> { public: static constexpr bool available = true; }; /// Specialized SPSSequence -> ArrayRef serialization. /// /// On deserialize, points directly into the input buffer. template <> class SPSSerializationTraits, ArrayRef> { public: static size_t size(const ArrayRef &A) { return SPSArgList::size(static_cast(A.size())) + A.size(); } static bool serialize(SPSOutputBuffer &OB, const ArrayRef &A) { if (!SPSArgList::serialize(OB, static_cast(A.size()))) return false; if (A.empty()) // Empty ArrayRef may have null data, so bail out early. return true; return OB.write(A.data(), A.size()); } static bool deserialize(SPSInputBuffer &IB, ArrayRef &A) { uint64_t Size; if (!SPSArgList::deserialize(IB, Size)) return false; if (Size > std::numeric_limits::max()) return false; A = {Size ? IB.data() : nullptr, static_cast(Size)}; return IB.skip(Size); } }; /// 'Trivial' sequence serialization: Sequence is serialized as a uint64_t size /// followed by a for-earch loop over the elements of the sequence to serialize /// each of them. template class SPSSerializationTraits, SequenceT, std::enable_if_t::available>> { public: static size_t size(const SequenceT &S) { size_t Size = SPSArgList::size(static_cast(S.size())); for (const auto &E : S) Size += SPSArgList::size(E); return Size; } static bool serialize(SPSOutputBuffer &OB, const SequenceT &S) { if (!SPSArgList::serialize(OB, static_cast(S.size()))) return false; for (const auto &E : S) if (!SPSArgList::serialize(OB, E)) return false; return true; } static bool deserialize(SPSInputBuffer &IB, SequenceT &S) { using TBSD = TrivialSPSSequenceDeserialization; uint64_t Size; if (!SPSArgList::deserialize(IB, Size)) return false; TBSD::reserve(S, Size); for (size_t I = 0; I != Size; ++I) { typename TBSD::element_type E; if (!SPSArgList::deserialize(IB, E)) return false; if (!TBSD::append(S, std::move(E))) return false; } return true; } }; /// SPSTuple serialization for std::tuple. template class SPSSerializationTraits, std::tuple> { private: using TupleArgList = typename SPSTuple::AsArgList; using ArgIndices = std::make_index_sequence; template static size_t size(const std::tuple &T, std::index_sequence) { return TupleArgList::size(std::get(T)...); } template static bool serialize(SPSOutputBuffer &OB, const std::tuple &T, std::index_sequence) { return TupleArgList::serialize(OB, std::get(T)...); } template static bool deserialize(SPSInputBuffer &IB, std::tuple &T, std::index_sequence) { return TupleArgList::deserialize(IB, std::get(T)...); } public: static size_t size(const std::tuple &T) { return size(T, ArgIndices{}); } static bool serialize(SPSOutputBuffer &OB, const std::tuple &T) { return serialize(OB, T, ArgIndices{}); } static bool deserialize(SPSInputBuffer &IB, std::tuple &T) { return deserialize(IB, T, ArgIndices{}); } }; /// SPSTuple serialization for std::pair. template class SPSSerializationTraits, std::pair> { public: static size_t size(const std::pair &P) { return SPSArgList::size(P.first) + SPSArgList::size(P.second); } static bool serialize(SPSOutputBuffer &OB, const std::pair &P) { return SPSArgList::serialize(OB, P.first) && SPSArgList::serialize(OB, P.second); } static bool deserialize(SPSInputBuffer &IB, std::pair &P) { return SPSArgList::deserialize(IB, P.first) && SPSArgList::deserialize(IB, P.second); } }; /// SPSOptional serialization for std::optional. template class SPSSerializationTraits, std::optional> { public: static size_t size(const std::optional &Value) { size_t Size = SPSArgList::size(!!Value); if (Value) Size += SPSArgList::size(*Value); return Size; } static bool serialize(SPSOutputBuffer &OB, const std::optional &Value) { if (!SPSArgList::serialize(OB, !!Value)) return false; if (Value) return SPSArgList::serialize(OB, *Value); return true; } static bool deserialize(SPSInputBuffer &IB, std::optional &Value) { bool HasValue; if (!SPSArgList::deserialize(IB, HasValue)) return false; if (HasValue) { Value = T(); return SPSArgList::deserialize(IB, *Value); } else Value = std::optional(); return true; } }; /// Serialization for StringRefs. /// /// Serialization is as for regular strings. Deserialization points directly /// into the blob. template <> class SPSSerializationTraits { public: static size_t size(const StringRef &S) { return SPSArgList::size(static_cast(S.size())) + S.size(); } static bool serialize(SPSOutputBuffer &OB, StringRef S) { if (!SPSArgList::serialize(OB, static_cast(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()); } static bool deserialize(SPSInputBuffer &IB, StringRef &S) { const char *Data = nullptr; uint64_t Size; if (!SPSArgList::deserialize(IB, Size)) return false; Data = IB.data(); if (!IB.skip(Size)) return false; S = StringRef(Size ? Data : nullptr, Size); return true; } }; /// Serialization for StringMaps. template class SPSSerializationTraits>, StringMap> { public: static size_t size(const StringMap &M) { size_t Sz = SPSArgList::size(static_cast(M.size())); for (auto &E : M) Sz += SPSArgList::size(E.first(), E.second); return Sz; } static bool serialize(SPSOutputBuffer &OB, const StringMap &M) { if (!SPSArgList::serialize(OB, static_cast(M.size()))) return false; for (auto &E : M) if (!SPSArgList::serialize(OB, E.first(), E.second)) return false; return true; } static bool deserialize(SPSInputBuffer &IB, StringMap &M) { uint64_t Size; assert(M.empty() && "M already contains elements"); if (!SPSArgList::deserialize(IB, Size)) return false; while (Size--) { StringRef S; ValueT V; if (!SPSArgList::deserialize(IB, S, V)) return false; if (!M.insert(std::make_pair(S, V)).second) return false; } return true; } }; /// SPS tag type for errors. class SPSError; /// SPS tag type for expecteds, which are either a T or a string representing /// an error. template class SPSExpected; namespace detail { /// Helper type for serializing Errors. /// /// llvm::Errors are move-only, and not inspectable except by consuming them. /// This makes them unsuitable for direct serialization via /// SPSSerializationTraits, which needs to inspect values twice (once to /// determine the amount of space to reserve, and then again to serialize). /// /// The SPSSerializableError type is a helper that can be /// constructed from an llvm::Error, but inspected more than once. struct SPSSerializableError { bool HasError = false; std::string ErrMsg; }; /// Helper type for serializing Expecteds. /// /// See SPSSerializableError for more details. /// // FIXME: Use std::variant for storage once we have c++17. template struct SPSSerializableExpected { bool HasValue = false; T Value{}; std::string ErrMsg; }; inline SPSSerializableError toSPSSerializable(Error Err) { if (Err) return {true, toString(std::move(Err))}; return {false, {}}; } inline Error fromSPSSerializable(SPSSerializableError BSE) { if (BSE.HasError) return make_error(BSE.ErrMsg, inconvertibleErrorCode()); return Error::success(); } template SPSSerializableExpected toSPSSerializable(Expected E) { if (E) return {true, std::move(*E), {}}; else return {false, T(), toString(E.takeError())}; } template Expected fromSPSSerializable(SPSSerializableExpected BSE) { if (BSE.HasValue) return std::move(BSE.Value); else return make_error(BSE.ErrMsg, inconvertibleErrorCode()); } } // end namespace detail /// Serialize to a SPSError from a detail::SPSSerializableError. template <> class SPSSerializationTraits { public: static size_t size(const detail::SPSSerializableError &BSE) { size_t Size = SPSArgList::size(BSE.HasError); if (BSE.HasError) Size += SPSArgList::size(BSE.ErrMsg); return Size; } static bool serialize(SPSOutputBuffer &OB, const detail::SPSSerializableError &BSE) { if (!SPSArgList::serialize(OB, BSE.HasError)) return false; if (BSE.HasError) if (!SPSArgList::serialize(OB, BSE.ErrMsg)) return false; return true; } static bool deserialize(SPSInputBuffer &IB, detail::SPSSerializableError &BSE) { if (!SPSArgList::deserialize(IB, BSE.HasError)) return false; if (!BSE.HasError) return true; return SPSArgList::deserialize(IB, BSE.ErrMsg); } }; /// Serialize to a SPSExpected from a /// detail::SPSSerializableExpected. template class SPSSerializationTraits, detail::SPSSerializableExpected> { public: static size_t size(const detail::SPSSerializableExpected &BSE) { size_t Size = SPSArgList::size(BSE.HasValue); if (BSE.HasValue) Size += SPSArgList::size(BSE.Value); else Size += SPSArgList::size(BSE.ErrMsg); return Size; } static bool serialize(SPSOutputBuffer &OB, const detail::SPSSerializableExpected &BSE) { if (!SPSArgList::serialize(OB, BSE.HasValue)) return false; if (BSE.HasValue) return SPSArgList::serialize(OB, BSE.Value); return SPSArgList::serialize(OB, BSE.ErrMsg); } static bool deserialize(SPSInputBuffer &IB, detail::SPSSerializableExpected &BSE) { if (!SPSArgList::deserialize(IB, BSE.HasValue)) return false; if (BSE.HasValue) return SPSArgList::deserialize(IB, BSE.Value); return SPSArgList::deserialize(IB, BSE.ErrMsg); } }; /// Serialize to a SPSExpected from a detail::SPSSerializableError. template class SPSSerializationTraits, detail::SPSSerializableError> { public: static size_t size(const detail::SPSSerializableError &BSE) { assert(BSE.HasError && "Cannot serialize expected from a success value"); return SPSArgList::size(false) + SPSArgList::size(BSE.ErrMsg); } static bool serialize(SPSOutputBuffer &OB, const detail::SPSSerializableError &BSE) { assert(BSE.HasError && "Cannot serialize expected from a success value"); if (!SPSArgList::serialize(OB, false)) return false; return SPSArgList::serialize(OB, BSE.ErrMsg); } }; /// Serialize to a SPSExpected from a T. template class SPSSerializationTraits, T> { public: static size_t size(const T &Value) { return SPSArgList::size(true) + SPSArgList::size(Value); } static bool serialize(SPSOutputBuffer &OB, const T &Value) { if (!SPSArgList::serialize(OB, true)) return false; return SPSArgList::serialize(Value); } }; } // end namespace shared } // end namespace orc } // end namespace llvm #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEPACKEDSERIALIZATION_H