1 //===--- simple_packed_serialization.h - simple serialization ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of the ORC runtime support library.
10 //
11 // The behavior of the utilities in this header must be synchronized with the
12 // behavior of the utilities in
13 // llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h.
14 //
15 // The Simple Packed Serialization (SPS) utilities are used to generate
16 // argument and return buffers for wrapper functions using the following
17 // serialization scheme:
18 //
19 // Primitives:
20 //   bool, char, int8_t, uint8_t -- Two's complement 8-bit (0=false, 1=true)
21 //   int16_t, uint16_t           -- Two's complement 16-bit little endian
22 //   int32_t, uint32_t           -- Two's complement 32-bit little endian
23 //   int64_t, int64_t            -- Two's complement 64-bit little endian
24 //
25 // Sequence<T>:
26 //   Serialized as the sequence length (as a uint64_t) followed by the
27 //   serialization of each of the elements without padding.
28 //
29 // Tuple<T1, ..., TN>:
30 //   Serialized as each of the element types from T1 to TN without padding.
31 //
32 //===----------------------------------------------------------------------===//
33 
34 #ifndef ORC_RT_SIMPLE_PACKED_SERIALIZATION_H
35 #define ORC_RT_SIMPLE_PACKED_SERIALIZATION_H
36 
37 #include "adt.h"
38 #include "endianness.h"
39 #include "error.h"
40 #include "stl_extras.h"
41 
42 #include <string>
43 #include <tuple>
44 #include <type_traits>
45 #include <unordered_map>
46 #include <utility>
47 #include <vector>
48 
49 namespace __orc_rt {
50 
51 /// Output char buffer with overflow check.
52 class SPSOutputBuffer {
53 public:
54   SPSOutputBuffer(char *Buffer, size_t Remaining)
55       : Buffer(Buffer), Remaining(Remaining) {}
56   bool write(const char *Data, size_t Size) {
57     if (Size > Remaining)
58       return false;
59     memcpy(Buffer, Data, Size);
60     Buffer += Size;
61     Remaining -= Size;
62     return true;
63   }
64 
65 private:
66   char *Buffer = nullptr;
67   size_t Remaining = 0;
68 };
69 
70 /// Input char buffer with underflow check.
71 class SPSInputBuffer {
72 public:
73   SPSInputBuffer() = default;
74   SPSInputBuffer(const char *Buffer, size_t Remaining)
75       : Buffer(Buffer), Remaining(Remaining) {}
76   bool read(char *Data, size_t Size) {
77     if (Size > Remaining)
78       return false;
79     memcpy(Data, Buffer, Size);
80     Buffer += Size;
81     Remaining -= Size;
82     return true;
83   }
84 
85   const char *data() const { return Buffer; }
86   bool skip(size_t Size) {
87     if (Size > Remaining)
88       return false;
89     Buffer += Size;
90     Remaining -= Size;
91     return true;
92   }
93 
94 private:
95   const char *Buffer = nullptr;
96   size_t Remaining = 0;
97 };
98 
99 /// Specialize to describe how to serialize/deserialize to/from the given
100 /// concrete type.
101 template <typename SPSTagT, typename ConcreteT, typename _ = void>
102 class SPSSerializationTraits;
103 
104 /// A utility class for serializing to a blob from a variadic list.
105 template <typename... ArgTs> class SPSArgList;
106 
107 // Empty list specialization for SPSArgList.
108 template <> class SPSArgList<> {
109 public:
110   static size_t size() { return 0; }
111 
112   static bool serialize(SPSOutputBuffer &OB) { return true; }
113   static bool deserialize(SPSInputBuffer &IB) { return true; }
114 };
115 
116 // Non-empty list specialization for SPSArgList.
117 template <typename SPSTagT, typename... SPSTagTs>
118 class SPSArgList<SPSTagT, SPSTagTs...> {
119 public:
120   template <typename ArgT, typename... ArgTs>
121   static size_t size(const ArgT &Arg, const ArgTs &...Args) {
122     return SPSSerializationTraits<SPSTagT, ArgT>::size(Arg) +
123            SPSArgList<SPSTagTs...>::size(Args...);
124   }
125 
126   template <typename ArgT, typename... ArgTs>
127   static bool serialize(SPSOutputBuffer &OB, const ArgT &Arg,
128                         const ArgTs &...Args) {
129     return SPSSerializationTraits<SPSTagT, ArgT>::serialize(OB, Arg) &&
130            SPSArgList<SPSTagTs...>::serialize(OB, Args...);
131   }
132 
133   template <typename ArgT, typename... ArgTs>
134   static bool deserialize(SPSInputBuffer &IB, ArgT &Arg, ArgTs &...Args) {
135     return SPSSerializationTraits<SPSTagT, ArgT>::deserialize(IB, Arg) &&
136            SPSArgList<SPSTagTs...>::deserialize(IB, Args...);
137   }
138 };
139 
140 /// SPS serialization for integral types, bool, and char.
141 template <typename SPSTagT>
142 class SPSSerializationTraits<
143     SPSTagT, SPSTagT,
144     std::enable_if_t<std::is_same<SPSTagT, bool>::value ||
145                      std::is_same<SPSTagT, char>::value ||
146                      std::is_same<SPSTagT, int8_t>::value ||
147                      std::is_same<SPSTagT, int16_t>::value ||
148                      std::is_same<SPSTagT, int32_t>::value ||
149                      std::is_same<SPSTagT, int64_t>::value ||
150                      std::is_same<SPSTagT, uint8_t>::value ||
151                      std::is_same<SPSTagT, uint16_t>::value ||
152                      std::is_same<SPSTagT, uint32_t>::value ||
153                      std::is_same<SPSTagT, uint64_t>::value>> {
154 public:
155   static size_t size(const SPSTagT &Value) { return sizeof(SPSTagT); }
156 
157   static bool serialize(SPSOutputBuffer &OB, const SPSTagT &Value) {
158     SPSTagT Tmp = Value;
159     if (IsBigEndianHost)
160       swapByteOrder(Tmp);
161     return OB.write(reinterpret_cast<const char *>(&Tmp), sizeof(Tmp));
162   }
163 
164   static bool deserialize(SPSInputBuffer &IB, SPSTagT &Value) {
165     SPSTagT Tmp;
166     if (!IB.read(reinterpret_cast<char *>(&Tmp), sizeof(Tmp)))
167       return false;
168     if (IsBigEndianHost)
169       swapByteOrder(Tmp);
170     Value = Tmp;
171     return true;
172   }
173 };
174 
175 /// Any empty placeholder suitable as a substitute for void when deserializing
176 class SPSEmpty {};
177 
178 /// Represents an address in the executor.
179 class SPSExecutorAddr {};
180 
181 /// SPS tag type for tuples.
182 ///
183 /// A blob tuple should be serialized by serializing each of the elements in
184 /// sequence.
185 template <typename... SPSTagTs> class SPSTuple {
186 public:
187   /// Convenience typedef of the corresponding arg list.
188   typedef SPSArgList<SPSTagTs...> AsArgList;
189 };
190 
191 /// SPS tag type for sequences.
192 ///
193 /// SPSSequences should be serialized as a uint64_t sequence length,
194 /// followed by the serialization of each of the elements.
195 template <typename SPSElementTagT> class SPSSequence;
196 
197 /// SPS tag type for strings, which are equivalent to sequences of chars.
198 using SPSString = SPSSequence<char>;
199 
200 /// SPS tag type for maps.
201 ///
202 /// SPS maps are just sequences of (Key, Value) tuples.
203 template <typename SPSTagT1, typename SPSTagT2>
204 using SPSMap = SPSSequence<SPSTuple<SPSTagT1, SPSTagT2>>;
205 
206 /// Serialization for SPSEmpty type.
207 template <> class SPSSerializationTraits<SPSEmpty, SPSEmpty> {
208 public:
209   static size_t size(const SPSEmpty &EP) { return 0; }
210   static bool serialize(SPSOutputBuffer &OB, const SPSEmpty &BE) {
211     return true;
212   }
213   static bool deserialize(SPSInputBuffer &IB, SPSEmpty &BE) { return true; }
214 };
215 
216 /// Specialize this to implement 'trivial' sequence serialization for
217 /// a concrete sequence type.
218 ///
219 /// Trivial sequence serialization uses the sequence's 'size' member to get the
220 /// length of the sequence, and uses a range-based for loop to iterate over the
221 /// elements.
222 ///
223 /// Specializing this template class means that you do not need to provide a
224 /// specialization of SPSSerializationTraits for your type.
225 template <typename SPSElementTagT, typename ConcreteSequenceT>
226 class TrivialSPSSequenceSerialization {
227 public:
228   static constexpr bool available = false;
229 };
230 
231 /// Specialize this to implement 'trivial' sequence deserialization for
232 /// a concrete sequence type.
233 ///
234 /// Trivial deserialization calls a static 'reserve(SequenceT&)' method on your
235 /// specialization (you must implement this) to reserve space, and then calls
236 /// a static 'append(SequenceT&, ElementT&) method to append each of the
237 /// deserialized elements.
238 ///
239 /// Specializing this template class means that you do not need to provide a
240 /// specialization of SPSSerializationTraits for your type.
241 template <typename SPSElementTagT, typename ConcreteSequenceT>
242 class TrivialSPSSequenceDeserialization {
243 public:
244   static constexpr bool available = false;
245 };
246 
247 /// Trivial std::string -> SPSSequence<char> serialization.
248 template <> class TrivialSPSSequenceSerialization<char, std::string> {
249 public:
250   static constexpr bool available = true;
251 };
252 
253 /// Trivial SPSSequence<char> -> std::string deserialization.
254 template <> class TrivialSPSSequenceDeserialization<char, std::string> {
255 public:
256   static constexpr bool available = true;
257 
258   using element_type = char;
259 
260   static void reserve(std::string &S, uint64_t Size) { S.reserve(Size); }
261   static bool append(std::string &S, char C) {
262     S.push_back(C);
263     return true;
264   }
265 };
266 
267 /// Trivial std::vector<T> -> SPSSequence<SPSElementTagT> serialization.
268 template <typename SPSElementTagT, typename T>
269 class TrivialSPSSequenceSerialization<SPSElementTagT, std::vector<T>> {
270 public:
271   static constexpr bool available = true;
272 };
273 
274 /// Trivial SPSSequence<SPSElementTagT> -> std::vector<T> deserialization.
275 template <typename SPSElementTagT, typename T>
276 class TrivialSPSSequenceDeserialization<SPSElementTagT, std::vector<T>> {
277 public:
278   static constexpr bool available = true;
279 
280   using element_type = typename std::vector<T>::value_type;
281 
282   static void reserve(std::vector<T> &V, uint64_t Size) { V.reserve(Size); }
283   static bool append(std::vector<T> &V, T E) {
284     V.push_back(std::move(E));
285     return true;
286   }
287 };
288 
289 /// Trivial std::unordered_map<K, V> -> SPSSequence<SPSTuple<SPSKey, SPSValue>>
290 /// serialization.
291 template <typename SPSKeyTagT, typename SPSValueTagT, typename K, typename V>
292 class TrivialSPSSequenceSerialization<SPSTuple<SPSKeyTagT, SPSValueTagT>,
293                                       std::unordered_map<K, V>> {
294 public:
295   static constexpr bool available = true;
296 };
297 
298 /// Trivial SPSSequence<SPSTuple<SPSKey, SPSValue>> -> std::unordered_map<K, V>
299 /// deserialization.
300 template <typename SPSKeyTagT, typename SPSValueTagT, typename K, typename V>
301 class TrivialSPSSequenceDeserialization<SPSTuple<SPSKeyTagT, SPSValueTagT>,
302                                         std::unordered_map<K, V>> {
303 public:
304   static constexpr bool available = true;
305 
306   using element_type = std::pair<K, V>;
307 
308   static void reserve(std::unordered_map<K, V> &M, uint64_t Size) {
309     M.reserve(Size);
310   }
311   static bool append(std::unordered_map<K, V> &M, element_type E) {
312     return M.insert(std::move(E)).second;
313   }
314 };
315 
316 /// 'Trivial' sequence serialization: Sequence is serialized as a uint64_t size
317 /// followed by a for-earch loop over the elements of the sequence to serialize
318 /// each of them.
319 template <typename SPSElementTagT, typename SequenceT>
320 class SPSSerializationTraits<SPSSequence<SPSElementTagT>, SequenceT,
321                              std::enable_if_t<TrivialSPSSequenceSerialization<
322                                  SPSElementTagT, SequenceT>::available>> {
323 public:
324   static size_t size(const SequenceT &S) {
325     size_t Size = SPSArgList<uint64_t>::size(static_cast<uint64_t>(S.size()));
326     for (const auto &E : S)
327       Size += SPSArgList<SPSElementTagT>::size(E);
328     return Size;
329   }
330 
331   static bool serialize(SPSOutputBuffer &OB, const SequenceT &S) {
332     if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(S.size())))
333       return false;
334     for (const auto &E : S)
335       if (!SPSArgList<SPSElementTagT>::serialize(OB, E))
336         return false;
337     return true;
338   }
339 
340   static bool deserialize(SPSInputBuffer &IB, SequenceT &S) {
341     using TBSD = TrivialSPSSequenceDeserialization<SPSElementTagT, SequenceT>;
342     uint64_t Size;
343     if (!SPSArgList<uint64_t>::deserialize(IB, Size))
344       return false;
345     TBSD::reserve(S, Size);
346     for (size_t I = 0; I != Size; ++I) {
347       typename TBSD::element_type E;
348       if (!SPSArgList<SPSElementTagT>::deserialize(IB, E))
349         return false;
350       if (!TBSD::append(S, std::move(E)))
351         return false;
352     }
353     return true;
354   }
355 };
356 
357 /// SPSTuple serialization for std::pair.
358 template <typename SPSTagT1, typename SPSTagT2, typename T1, typename T2>
359 class SPSSerializationTraits<SPSTuple<SPSTagT1, SPSTagT2>, std::pair<T1, T2>> {
360 public:
361   static size_t size(const std::pair<T1, T2> &P) {
362     return SPSArgList<SPSTagT1>::size(P.first) +
363            SPSArgList<SPSTagT2>::size(P.second);
364   }
365 
366   static bool serialize(SPSOutputBuffer &OB, const std::pair<T1, T2> &P) {
367     return SPSArgList<SPSTagT1>::serialize(OB, P.first) &&
368            SPSArgList<SPSTagT2>::serialize(OB, P.second);
369   }
370 
371   static bool deserialize(SPSInputBuffer &IB, std::pair<T1, T2> &P) {
372     return SPSArgList<SPSTagT1>::deserialize(IB, P.first) &&
373            SPSArgList<SPSTagT2>::deserialize(IB, P.second);
374   }
375 };
376 
377 /// Serialization for string_views.
378 ///
379 /// Serialization is as for regular strings. Deserialization points directly
380 /// into the blob.
381 template <> class SPSSerializationTraits<SPSString, __orc_rt::string_view> {
382 public:
383   static size_t size(const __orc_rt::string_view &S) {
384     return SPSArgList<uint64_t>::size(static_cast<uint64_t>(S.size())) +
385            S.size();
386   }
387 
388   static bool serialize(SPSOutputBuffer &OB, const __orc_rt::string_view &S) {
389     if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(S.size())))
390       return false;
391     return OB.write(S.data(), S.size());
392   }
393 
394   static bool deserialize(SPSInputBuffer &IB, __orc_rt::string_view &S) {
395     const char *Data = nullptr;
396     uint64_t Size;
397     if (!SPSArgList<uint64_t>::deserialize(IB, Size))
398       return false;
399     if (Size > std::numeric_limits<size_t>::max())
400       return false;
401     Data = IB.data();
402     if (!IB.skip(Size))
403       return false;
404     S = {Data, static_cast<size_t>(Size)};
405     return true;
406   }
407 };
408 
409 /// SPS tag type for errors.
410 class SPSError;
411 
412 /// SPS tag type for expecteds, which are either a T or a string representing
413 /// an error.
414 template <typename SPSTagT> class SPSExpected;
415 
416 namespace detail {
417 
418 /// Helper type for serializing Errors.
419 ///
420 /// llvm::Errors are move-only, and not inspectable except by consuming them.
421 /// This makes them unsuitable for direct serialization via
422 /// SPSSerializationTraits, which needs to inspect values twice (once to
423 /// determine the amount of space to reserve, and then again to serialize).
424 ///
425 /// The SPSSerializableError type is a helper that can be
426 /// constructed from an llvm::Error, but inspected more than once.
427 struct SPSSerializableError {
428   bool HasError = false;
429   std::string ErrMsg;
430 };
431 
432 /// Helper type for serializing Expected<T>s.
433 ///
434 /// See SPSSerializableError for more details.
435 ///
436 // FIXME: Use std::variant for storage once we have c++17.
437 template <typename T> struct SPSSerializableExpected {
438   bool HasValue = false;
439   T Value{};
440   std::string ErrMsg;
441 };
442 
443 inline SPSSerializableError toSPSSerializable(Error Err) {
444   if (Err)
445     return {true, toString(std::move(Err))};
446   return {false, {}};
447 }
448 
449 inline Error fromSPSSerializable(SPSSerializableError BSE) {
450   if (BSE.HasError)
451     return make_error<StringError>(BSE.ErrMsg);
452   return Error::success();
453 }
454 
455 template <typename T>
456 SPSSerializableExpected<T> toSPSSerializable(Expected<T> E) {
457   if (E)
458     return {true, std::move(*E), {}};
459   else
460     return {false, {}, toString(E.takeError())};
461 }
462 
463 template <typename T>
464 Expected<T> fromSPSSerializable(SPSSerializableExpected<T> BSE) {
465   if (BSE.HasValue)
466     return std::move(BSE.Value);
467   else
468     return make_error<StringError>(BSE.ErrMsg);
469 }
470 
471 } // end namespace detail
472 
473 /// Serialize to a SPSError from a detail::SPSSerializableError.
474 template <>
475 class SPSSerializationTraits<SPSError, detail::SPSSerializableError> {
476 public:
477   static size_t size(const detail::SPSSerializableError &BSE) {
478     size_t Size = SPSArgList<bool>::size(BSE.HasError);
479     if (BSE.HasError)
480       Size += SPSArgList<SPSString>::size(BSE.ErrMsg);
481     return Size;
482   }
483 
484   static bool serialize(SPSOutputBuffer &OB,
485                         const detail::SPSSerializableError &BSE) {
486     if (!SPSArgList<bool>::serialize(OB, BSE.HasError))
487       return false;
488     if (BSE.HasError)
489       if (!SPSArgList<SPSString>::serialize(OB, BSE.ErrMsg))
490         return false;
491     return true;
492   }
493 
494   static bool deserialize(SPSInputBuffer &IB,
495                           detail::SPSSerializableError &BSE) {
496     if (!SPSArgList<bool>::deserialize(IB, BSE.HasError))
497       return false;
498 
499     if (!BSE.HasError)
500       return true;
501 
502     return SPSArgList<SPSString>::deserialize(IB, BSE.ErrMsg);
503   }
504 };
505 
506 /// Serialize to a SPSExpected<SPSTagT> from a
507 /// detail::SPSSerializableExpected<T>.
508 template <typename SPSTagT, typename T>
509 class SPSSerializationTraits<SPSExpected<SPSTagT>,
510                              detail::SPSSerializableExpected<T>> {
511 public:
512   static size_t size(const detail::SPSSerializableExpected<T> &BSE) {
513     size_t Size = SPSArgList<bool>::size(BSE.HasValue);
514     if (BSE.HasValue)
515       Size += SPSArgList<SPSTagT>::size(BSE.Value);
516     else
517       Size += SPSArgList<SPSString>::size(BSE.ErrMsg);
518     return Size;
519   }
520 
521   static bool serialize(SPSOutputBuffer &OB,
522                         const detail::SPSSerializableExpected<T> &BSE) {
523     if (!SPSArgList<bool>::serialize(OB, BSE.HasValue))
524       return false;
525 
526     if (BSE.HasValue)
527       return SPSArgList<SPSTagT>::serialize(OB, BSE.Value);
528 
529     return SPSArgList<SPSString>::serialize(OB, BSE.ErrMsg);
530   }
531 
532   static bool deserialize(SPSInputBuffer &IB,
533                           detail::SPSSerializableExpected<T> &BSE) {
534     if (!SPSArgList<bool>::deserialize(IB, BSE.HasValue))
535       return false;
536 
537     if (BSE.HasValue)
538       return SPSArgList<SPSTagT>::deserialize(IB, BSE.Value);
539 
540     return SPSArgList<SPSString>::deserialize(IB, BSE.ErrMsg);
541   }
542 };
543 
544 /// Serialize to a SPSExpected<SPSTagT> from a detail::SPSSerializableError.
545 template <typename SPSTagT>
546 class SPSSerializationTraits<SPSExpected<SPSTagT>,
547                              detail::SPSSerializableError> {
548 public:
549   static size_t size(const detail::SPSSerializableError &BSE) {
550     assert(BSE.HasError && "Cannot serialize expected from a success value");
551     return SPSArgList<bool>::size(false) +
552            SPSArgList<SPSString>::size(BSE.ErrMsg);
553   }
554 
555   static bool serialize(SPSOutputBuffer &OB,
556                         const detail::SPSSerializableError &BSE) {
557     assert(BSE.HasError && "Cannot serialize expected from a success value");
558     if (!SPSArgList<bool>::serialize(OB, false))
559       return false;
560     return SPSArgList<SPSString>::serialize(OB, BSE.ErrMsg);
561   }
562 };
563 
564 /// Serialize to a SPSExpected<SPSTagT> from a T.
565 template <typename SPSTagT, typename T>
566 class SPSSerializationTraits<SPSExpected<SPSTagT>, T> {
567 public:
568   static size_t size(const T &Value) {
569     return SPSArgList<bool>::size(true) + SPSArgList<SPSTagT>::size(Value);
570   }
571 
572   static bool serialize(SPSOutputBuffer &OB, const T &Value) {
573     if (!SPSArgList<bool>::serialize(OB, true))
574       return false;
575     return SPSArgList<SPSTagT>::serialize(Value);
576   }
577 };
578 
579 } // end namespace __orc_rt
580 
581 #endif // ORC_RT_SIMPLE_PACKED_SERIALIZATION_H
582