1 //===-- ReproducerInstrumentation.h -----------------------------*- C++ -*-===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #ifndef LLDB_UTILITY_REPRODUCERINSTRUMENTATION_H
9 #define LLDB_UTILITY_REPRODUCERINSTRUMENTATION_H
10 
11 #include "lldb/Utility/FileSpec.h"
12 #include "lldb/Utility/Log.h"
13 #include "lldb/Utility/Logging.h"
14 
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/ErrorHandling.h"
18 
19 #include <map>
20 #include <thread>
21 #include <type_traits>
22 
23 template <typename T,
24           typename std::enable_if<std::is_fundamental<T>::value, int>::type = 0>
stringify_append(llvm::raw_string_ostream & ss,const T & t)25 inline void stringify_append(llvm::raw_string_ostream &ss, const T &t) {
26   ss << t;
27 }
28 
29 template <typename T, typename std::enable_if<!std::is_fundamental<T>::value,
30                                               int>::type = 0>
stringify_append(llvm::raw_string_ostream & ss,const T & t)31 inline void stringify_append(llvm::raw_string_ostream &ss, const T &t) {
32   ss << &t;
33 }
34 
35 template <typename T>
stringify_append(llvm::raw_string_ostream & ss,T * t)36 inline void stringify_append(llvm::raw_string_ostream &ss, T *t) {
37   ss << reinterpret_cast<void *>(t);
38 }
39 
40 template <typename T>
stringify_append(llvm::raw_string_ostream & ss,const T * t)41 inline void stringify_append(llvm::raw_string_ostream &ss, const T *t) {
42   ss << reinterpret_cast<const void *>(t);
43 }
44 
45 template <>
46 inline void stringify_append<char>(llvm::raw_string_ostream &ss,
47                                    const char *t) {
48   ss << '\"' << t << '\"';
49 }
50 
51 template <>
52 inline void stringify_append<std::nullptr_t>(llvm::raw_string_ostream &ss,
53                                              const std::nullptr_t &t) {
54   ss << "\"nullptr\"";
55 }
56 
57 template <typename Head>
stringify_helper(llvm::raw_string_ostream & ss,const Head & head)58 inline void stringify_helper(llvm::raw_string_ostream &ss, const Head &head) {
59   stringify_append(ss, head);
60 }
61 
62 template <typename Head, typename... Tail>
stringify_helper(llvm::raw_string_ostream & ss,const Head & head,const Tail &...tail)63 inline void stringify_helper(llvm::raw_string_ostream &ss, const Head &head,
64                              const Tail &... tail) {
65   stringify_append(ss, head);
66   ss << ", ";
67   stringify_helper(ss, tail...);
68 }
69 
stringify_args(const Ts &...ts)70 template <typename... Ts> inline std::string stringify_args(const Ts &... ts) {
71   std::string buffer;
72   llvm::raw_string_ostream ss(buffer);
73   stringify_helper(ss, ts...);
74   return ss.str();
75 }
76 
77 // Define LLDB_REPRO_INSTR_TRACE to trace to stderr instead of LLDB's log
78 // infrastructure. This is useful when you need to see traces before the logger
79 // is initialized or enabled.
80 // #define LLDB_REPRO_INSTR_TRACE
81 
82 #ifdef LLDB_REPRO_INSTR_TRACE
this_thread_id()83 inline llvm::raw_ostream &this_thread_id() {
84   size_t tid = std::hash<std::thread::id>{}(std::this_thread::get_id());
85   return llvm::errs().write_hex(tid) << " :: ";
86 }
87 #endif
88 
89 #define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)                            \
90   R.Register<Class * Signature>(&construct<Class Signature>::record, "",       \
91                                 #Class, #Class, #Signature)
92 
93 #define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)                 \
94   R.Register(                                                                  \
95       &invoke<Result(Class::*) Signature>::method<(&Class::Method)>::record,   \
96       #Result, #Class, #Method, #Signature)
97 
98 #define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)           \
99   R.Register(&invoke<Result(Class::*)                                          \
100                          Signature const>::method<(&Class::Method)>::record,   \
101              #Result, #Class, #Method, #Signature)
102 
103 #define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)          \
104   R.Register(&invoke<Result(*) Signature>::method<(&Class::Method)>::record,   \
105              #Result, #Class, #Method, #Signature)
106 
107 #define LLDB_REGISTER_CHAR_PTR_METHOD_STATIC(Result, Class, Method)            \
108   R.Register(                                                                  \
109       &invoke<Result (*)(char *, size_t)>::method<(&Class::Method)>::record,   \
110       &invoke_char_ptr<Result (*)(char *,                                      \
111                                   size_t)>::method<(&Class::Method)>::record,  \
112       #Result, #Class, #Method, "(char*, size_t");
113 
114 #define LLDB_REGISTER_CHAR_PTR_METHOD(Result, Class, Method)                   \
115   R.Register(&invoke<Result (Class::*)(char *, size_t)>::method<(              \
116                  &Class::Method)>::record,                                     \
117              &invoke_char_ptr<Result (Class::*)(char *, size_t)>::method<(     \
118                  &Class::Method)>::record,                                     \
119              #Result, #Class, #Method, "(char*, size_t");
120 
121 #define LLDB_REGISTER_CHAR_PTR_METHOD_CONST(Result, Class, Method)             \
122   R.Register(&invoke<Result (Class::*)(char *, size_t)                         \
123                          const>::method<(&Class::Method)>::record,             \
124              &invoke_char_ptr<Result (Class::*)(char *, size_t)                \
125                                   const>::method<(&Class::Method)>::record,    \
126              #Result, #Class, #Method, "(char*, size_t");
127 
128 #define LLDB_CONSTRUCT_(T, Class, ...)                                         \
129   lldb_private::repro::Recorder _recorder(LLVM_PRETTY_FUNCTION);               \
130   lldb_private::repro::construct<T>::handle(LLDB_GET_INSTRUMENTATION_DATA(),   \
131                                             _recorder, Class, __VA_ARGS__);
132 
133 #define LLDB_RECORD_CONSTRUCTOR(Class, Signature, ...)                         \
134   LLDB_CONSTRUCT_(Class Signature, this, __VA_ARGS__)
135 
136 #define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)                                 \
137   LLDB_CONSTRUCT_(Class(), this, lldb_private::repro::EmptyArg())
138 
139 #define LLDB_RECORD_(T1, T2, ...)                                              \
140   lldb_private::repro::Recorder _recorder(LLVM_PRETTY_FUNCTION,                \
141                                           stringify_args(__VA_ARGS__));        \
142   if (lldb_private::repro::InstrumentationData _data =                         \
143           LLDB_GET_INSTRUMENTATION_DATA()) {                                   \
144     if (lldb_private::repro::Serializer *_serializer =                         \
145             _data.GetSerializer()) {                                           \
146       _recorder.Record(*_serializer, _data.GetRegistry(),                      \
147                        &lldb_private::repro::invoke<T1>::method<T2>::record,   \
148                        __VA_ARGS__);                                           \
149     } else if (lldb_private::repro::Deserializer *_deserializer =              \
150                    _data.GetDeserializer()) {                                  \
151       if (_recorder.ShouldCapture()) {                                         \
152         return lldb_private::repro::invoke<T1>::method<T2>::replay(            \
153             _recorder, *_deserializer, _data.GetRegistry());                   \
154       }                                                                        \
155     }                                                                          \
156   }
157 
158 #define LLDB_RECORD_METHOD(Result, Class, Method, Signature, ...)              \
159   LLDB_RECORD_(Result(Class::*) Signature, (&Class::Method), this, __VA_ARGS__)
160 
161 #define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature, ...)        \
162   LLDB_RECORD_(Result(Class::*) Signature const, (&Class::Method), this,       \
163                __VA_ARGS__)
164 
165 #define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)                      \
166   LLDB_RECORD_(Result (Class::*)(), (&Class::Method), this)
167 
168 #define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)                \
169   LLDB_RECORD_(Result (Class::*)() const, (&Class::Method), this)
170 
171 #define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature, ...)       \
172   LLDB_RECORD_(Result(*) Signature, (&Class::Method), __VA_ARGS__)
173 
174 #define LLDB_RECORD_STATIC_METHOD_NO_ARGS(Result, Class, Method)               \
175   LLDB_RECORD_(Result (*)(), (&Class::Method), lldb_private::repro::EmptyArg())
176 
177 #define LLDB_RECORD_CHAR_PTR_(T1, T2, StrOut, ...)                             \
178   lldb_private::repro::Recorder _recorder(LLVM_PRETTY_FUNCTION,                \
179                                           stringify_args(__VA_ARGS__));        \
180   if (lldb_private::repro::InstrumentationData _data =                         \
181           LLDB_GET_INSTRUMENTATION_DATA()) {                                   \
182     if (lldb_private::repro::Serializer *_serializer =                         \
183             _data.GetSerializer()) {                                           \
184       _recorder.Record(*_serializer, _data.GetRegistry(),                      \
185                        &lldb_private::repro::invoke<T1>::method<(T2)>::record, \
186                        __VA_ARGS__);                                           \
187     } else if (lldb_private::repro::Deserializer *_deserializer =              \
188                    _data.GetDeserializer()) {                                  \
189       if (_recorder.ShouldCapture()) {                                         \
190         return lldb_private::repro::invoke_char_ptr<T1>::method<T2>::replay(   \
191             _recorder, *_deserializer, _data.GetRegistry(), StrOut);           \
192       }                                                                        \
193     }                                                                          \
194   }
195 
196 #define LLDB_RECORD_CHAR_PTR_METHOD(Result, Class, Method, Signature, StrOut,  \
197                                     ...)                                       \
198   LLDB_RECORD_CHAR_PTR_(Result(Class::*) Signature, (&Class::Method), StrOut,  \
199                         this, __VA_ARGS__)
200 
201 #define LLDB_RECORD_CHAR_PTR_METHOD_CONST(Result, Class, Method, Signature,    \
202                                           StrOut, ...)                         \
203   LLDB_RECORD_CHAR_PTR_(Result(Class::*) Signature const, (&Class::Method),    \
204                         StrOut, this, __VA_ARGS__)
205 
206 #define LLDB_RECORD_CHAR_PTR_STATIC_METHOD(Result, Class, Method, Signature,   \
207                                            StrOut, ...)                        \
208   LLDB_RECORD_CHAR_PTR_(Result(*) Signature, (&Class::Method), StrOut,         \
209                         __VA_ARGS__)
210 
211 #define LLDB_RECORD_RESULT(Result) _recorder.RecordResult(Result, true);
212 
213 /// The LLDB_RECORD_DUMMY macro is special because it doesn't actually record
214 /// anything. It's used to track API boundaries when we cannot record for
215 /// technical reasons.
216 #define LLDB_RECORD_DUMMY(Result, Class, Method, Signature, ...)               \
217   lldb_private::repro::Recorder _recorder;
218 
219 #define LLDB_RECORD_DUMMY_NO_ARGS(Result, Class, Method)                       \
220   lldb_private::repro::Recorder _recorder;
221 
222 namespace lldb_private {
223 namespace repro {
224 
225 template <class T>
226 struct is_trivially_serializable
227     : std::integral_constant<bool, std::is_fundamental<T>::value ||
228                                        std::is_enum<T>::value> {};
229 
230 /// Mapping between serialized indices and their corresponding objects.
231 ///
232 /// This class is used during replay to map indices back to in-memory objects.
233 ///
234 /// When objects are constructed, they are added to this mapping using
235 /// AddObjectForIndex.
236 ///
237 /// When an object is passed to a function, its index is deserialized and
238 /// AddObjectForIndex returns the corresponding object. If there is no object
239 /// for the given index, a nullptr is returend. The latter is valid when custom
240 /// replay code is in place and the actual object is ignored.
241 class IndexToObject {
242 public:
243   /// Returns an object as a pointer for the given index or nullptr if not
244   /// present in the map.
GetObjectForIndex(unsigned idx)245   template <typename T> T *GetObjectForIndex(unsigned idx) {
246     assert(idx != 0 && "Cannot get object for sentinel");
247     void *object = GetObjectForIndexImpl(idx);
248     return static_cast<T *>(object);
249   }
250 
251   /// Adds a pointer to an object to the mapping for the given index.
AddObjectForIndex(unsigned idx,T * object)252   template <typename T> T *AddObjectForIndex(unsigned idx, T *object) {
253     AddObjectForIndexImpl(
254         idx, static_cast<void *>(
255                  const_cast<typename std::remove_const<T>::type *>(object)));
256     return object;
257   }
258 
259   /// Adds a reference to an object to the mapping for the given index.
AddObjectForIndex(unsigned idx,T & object)260   template <typename T> T &AddObjectForIndex(unsigned idx, T &object) {
261     AddObjectForIndexImpl(
262         idx, static_cast<void *>(
263                  const_cast<typename std::remove_const<T>::type *>(&object)));
264     return object;
265   }
266 
267   /// Get all objects sorted by their index.
268   std::vector<void *> GetAllObjects() const;
269 
270 private:
271   /// Helper method that does the actual lookup. The void* result is later cast
272   /// by the caller.
273   void *GetObjectForIndexImpl(unsigned idx);
274 
275   /// Helper method that does the actual insertion.
276   void AddObjectForIndexImpl(unsigned idx, void *object);
277 
278   /// Keeps a mapping between indices and their corresponding object.
279   llvm::DenseMap<unsigned, void *> m_mapping;
280 };
281 
282 /// We need to differentiate between pointers to fundamental and
283 /// non-fundamental types. See the corresponding Deserializer::Read method
284 /// for the reason why.
285 struct PointerTag {};
286 struct ReferenceTag {};
287 struct ValueTag {};
288 struct FundamentalPointerTag {};
289 struct FundamentalReferenceTag {};
290 
291 /// Return the deserialization tag for the given type T.
292 template <class T> struct serializer_tag {
293   typedef typename std::conditional<std::is_trivially_copyable<T>::value,
294                                     ValueTag, ReferenceTag>::type type;
295 };
296 template <class T> struct serializer_tag<T *> {
297   typedef
298       typename std::conditional<std::is_fundamental<T>::value,
299                                 FundamentalPointerTag, PointerTag>::type type;
300 };
301 template <class T> struct serializer_tag<T &> {
302   typedef typename std::conditional<std::is_fundamental<T>::value,
303                                     FundamentalReferenceTag, ReferenceTag>::type
304       type;
305 };
306 
307 /// Deserializes data from a buffer. It is used to deserialize function indices
308 /// to replay, their arguments and return values.
309 ///
310 /// Fundamental types and strings are read by value. Objects are read by their
311 /// index, which get translated by the IndexToObject mapping maintained in
312 /// this class.
313 ///
314 /// Additional bookkeeping with regards to the IndexToObject is required to
315 /// deserialize objects. When a constructor is run or an object is returned by
316 /// value, we need to capture the object and add it to the index together with
317 /// its index. This is the job of HandleReplayResult(Void).
318 class Deserializer {
319 public:
320   Deserializer(llvm::StringRef buffer) : m_buffer(buffer) {}
321 
322   /// Returns true when the buffer has unread data.
323   bool HasData(unsigned size) { return size <= m_buffer.size(); }
324 
325   /// Deserialize and interpret value as T.
326   template <typename T> T Deserialize() {
327     T t = Read<T>(typename serializer_tag<T>::type());
328 #ifdef LLDB_REPRO_INSTR_TRACE
329     llvm::errs() << "Deserializing with " << LLVM_PRETTY_FUNCTION << " -> "
330                  << stringify_args(t) << "\n";
331 #endif
332     return t;
333   }
334 
335   template <typename T> const T &HandleReplayResult(const T &t) {
336     unsigned result = Deserialize<unsigned>();
337     if (is_trivially_serializable<T>::value)
338       return t;
339     // We need to make a copy as the original object might go out of scope.
340     return *m_index_to_object.AddObjectForIndex(result, new T(t));
341   }
342 
343   /// Store the returned value in the index-to-object mapping.
344   template <typename T> T &HandleReplayResult(T &t) {
345     unsigned result = Deserialize<unsigned>();
346     if (is_trivially_serializable<T>::value)
347       return t;
348     // We need to make a copy as the original object might go out of scope.
349     return *m_index_to_object.AddObjectForIndex(result, new T(t));
350   }
351 
352   /// Store the returned value in the index-to-object mapping.
353   template <typename T> T *HandleReplayResult(T *t) {
354     unsigned result = Deserialize<unsigned>();
355     if (is_trivially_serializable<T>::value)
356       return t;
357     return m_index_to_object.AddObjectForIndex(result, t);
358   }
359 
360   /// All returned types are recorded, even when the function returns a void.
361   /// The latter requires special handling.
362   void HandleReplayResultVoid() {
363     unsigned result = Deserialize<unsigned>();
364     assert(result == 0);
365     (void)result;
366   }
367 
368   std::vector<void *> GetAllObjects() const {
369     return m_index_to_object.GetAllObjects();
370   }
371 
372 private:
373   template <typename T> T Read(ValueTag) {
374     assert(HasData(sizeof(T)));
375     T t;
376     std::memcpy(reinterpret_cast<char *>(&t), m_buffer.data(), sizeof(T));
377     m_buffer = m_buffer.drop_front(sizeof(T));
378     return t;
379   }
380 
381   template <typename T> T Read(PointerTag) {
382     typedef typename std::remove_pointer<T>::type UnderlyingT;
383     return m_index_to_object.template GetObjectForIndex<UnderlyingT>(
384         Deserialize<unsigned>());
385   }
386 
387   template <typename T> T Read(ReferenceTag) {
388     typedef typename std::remove_reference<T>::type UnderlyingT;
389     // If this is a reference to a fundamental type we just read its value.
390     return *m_index_to_object.template GetObjectForIndex<UnderlyingT>(
391         Deserialize<unsigned>());
392   }
393 
394   /// This method is used to parse references to fundamental types. Because
395   /// they're not recorded in the object table we have serialized their value.
396   /// We read its value, allocate a copy on the heap, and return a pointer to
397   /// the copy.
398   template <typename T> T Read(FundamentalPointerTag) {
399     typedef typename std::remove_pointer<T>::type UnderlyingT;
400     return new UnderlyingT(Deserialize<UnderlyingT>());
401   }
402 
403   /// This method is used to parse references to fundamental types. Because
404   /// they're not recorded in the object table we have serialized their value.
405   /// We read its value, allocate a copy on the heap, and return a reference to
406   /// the copy.
407   template <typename T> T Read(FundamentalReferenceTag) {
408     // If this is a reference to a fundamental type we just read its value.
409     typedef typename std::remove_reference<T>::type UnderlyingT;
410     return *(new UnderlyingT(Deserialize<UnderlyingT>()));
411   }
412 
413   /// Mapping of indices to objects.
414   IndexToObject m_index_to_object;
415 
416   /// Buffer containing the serialized data.
417   llvm::StringRef m_buffer;
418 };
419 
420 /// Partial specialization for C-style strings. We read the string value
421 /// instead of treating it as pointer.
422 template <> const char *Deserializer::Deserialize<const char *>();
423 template <> const char **Deserializer::Deserialize<const char **>();
424 template <> const uint8_t *Deserializer::Deserialize<const uint8_t *>();
425 template <> const void *Deserializer::Deserialize<const void *>();
426 template <> char *Deserializer::Deserialize<char *>();
427 template <> void *Deserializer::Deserialize<void *>();
428 
429 /// Helpers to auto-synthesize function replay code. It deserializes the replay
430 /// function's arguments one by one and finally calls the corresponding
431 /// function.
432 template <typename... Remaining> struct DeserializationHelper;
433 
434 template <typename Head, typename... Tail>
435 struct DeserializationHelper<Head, Tail...> {
436   template <typename Result, typename... Deserialized> struct deserialized {
437     static Result doit(Deserializer &deserializer,
438                        Result (*f)(Deserialized..., Head, Tail...),
439                        Deserialized... d) {
440       return DeserializationHelper<Tail...>::
441           template deserialized<Result, Deserialized..., Head>::doit(
442               deserializer, f, d..., deserializer.Deserialize<Head>());
443     }
444   };
445 };
446 
447 template <> struct DeserializationHelper<> {
448   template <typename Result, typename... Deserialized> struct deserialized {
449     static Result doit(Deserializer &deserializer, Result (*f)(Deserialized...),
450                        Deserialized... d) {
451       return f(d...);
452     }
453   };
454 };
455 
456 /// The replayer interface.
457 struct Replayer {
458   virtual ~Replayer() {}
459   virtual void operator()(Deserializer &deserializer) const = 0;
460 };
461 
462 /// The default replayer deserializes the arguments and calls the function.
463 template <typename Signature> struct DefaultReplayer;
464 template <typename Result, typename... Args>
465 struct DefaultReplayer<Result(Args...)> : public Replayer {
466   DefaultReplayer(Result (*f)(Args...)) : Replayer(), f(f) {}
467 
468   void operator()(Deserializer &deserializer) const override {
469     Replay(deserializer);
470   }
471 
472   Result Replay(Deserializer &deserializer) const {
473     return deserializer.HandleReplayResult(
474         DeserializationHelper<Args...>::template deserialized<Result>::doit(
475             deserializer, f));
476   }
477 
478   Result (*f)(Args...);
479 };
480 
481 /// Partial specialization for function returning a void type. It ignores the
482 /// (absent) return value.
483 template <typename... Args>
484 struct DefaultReplayer<void(Args...)> : public Replayer {
485   DefaultReplayer(void (*f)(Args...)) : Replayer(), f(f) {}
486 
487   void operator()(Deserializer &deserializer) const override {
488     Replay(deserializer);
489   }
490 
491   void Replay(Deserializer &deserializer) const {
492     DeserializationHelper<Args...>::template deserialized<void>::doit(
493         deserializer, f);
494     deserializer.HandleReplayResultVoid();
495   }
496 
497   void (*f)(Args...);
498 };
499 
500 /// The registry contains a unique mapping between functions and their ID. The
501 /// IDs can be serialized and deserialized to replay a function. Functions need
502 /// to be registered with the registry for this to work.
503 class Registry {
504 private:
505   struct SignatureStr {
506     SignatureStr(llvm::StringRef result = {}, llvm::StringRef scope = {},
507                  llvm::StringRef name = {}, llvm::StringRef args = {})
508         : result(result), scope(scope), name(name), args(args) {}
509 
510     std::string ToString() const;
511 
512     llvm::StringRef result;
513     llvm::StringRef scope;
514     llvm::StringRef name;
515     llvm::StringRef args;
516   };
517 
518 public:
519   Registry() = default;
520   virtual ~Registry() = default;
521 
522   /// Register a default replayer for a function.
523   template <typename Signature>
524   void Register(Signature *f, llvm::StringRef result = {},
525                 llvm::StringRef scope = {}, llvm::StringRef name = {},
526                 llvm::StringRef args = {}) {
527     DoRegister(uintptr_t(f), std::make_unique<DefaultReplayer<Signature>>(f),
528                SignatureStr(result, scope, name, args));
529   }
530 
531   /// Register a replayer that invokes a custom function with the same
532   /// signature as the replayed function.
533   template <typename Signature>
534   void Register(Signature *f, Signature *g, llvm::StringRef result = {},
535                 llvm::StringRef scope = {}, llvm::StringRef name = {},
536                 llvm::StringRef args = {}) {
537     DoRegister(uintptr_t(f), std::make_unique<DefaultReplayer<Signature>>(g),
538                SignatureStr(result, scope, name, args));
539   }
540 
541   /// Replay functions from a file.
542   bool Replay(const FileSpec &file);
543 
544   /// Replay functions from a buffer.
545   bool Replay(llvm::StringRef buffer);
546 
547   /// Replay functions from a deserializer.
548   bool Replay(Deserializer &deserializer);
549 
550   /// Returns the ID for a given function address.
551   unsigned GetID(uintptr_t addr);
552 
553   /// Get the replayer matching the given ID.
554   Replayer *GetReplayer(unsigned id);
555 
556   std::string GetSignature(unsigned id);
557 
558   void CheckID(unsigned expected, unsigned actual);
559 
560 protected:
561   /// Register the given replayer for a function (and the ID mapping).
562   void DoRegister(uintptr_t RunID, std::unique_ptr<Replayer> replayer,
563                   SignatureStr signature);
564 
565 private:
566   /// Mapping of function addresses to replayers and their ID.
567   std::map<uintptr_t, std::pair<std::unique_ptr<Replayer>, unsigned>>
568       m_replayers;
569 
570   /// Mapping of IDs to replayer instances.
571   std::map<unsigned, std::pair<Replayer *, SignatureStr>> m_ids;
572 };
573 
574 /// Maps an object to an index for serialization. Indices are unique and
575 /// incremented for every new object.
576 ///
577 /// Indices start at 1 in order to differentiate with an invalid index (0) in
578 /// the serialized buffer.
579 class ObjectToIndex {
580 public:
581   template <typename T> unsigned GetIndexForObject(T *t) {
582     return GetIndexForObjectImpl(static_cast<const void *>(t));
583   }
584 
585 private:
586   unsigned GetIndexForObjectImpl(const void *object);
587 
588   llvm::DenseMap<const void *, unsigned> m_mapping;
589 };
590 
591 /// Serializes functions, their arguments and their return type to a stream.
592 class Serializer {
593 public:
594   Serializer(llvm::raw_ostream &stream = llvm::outs()) : m_stream(stream) {}
595 
596   /// Recursively serialize all the given arguments.
597   template <typename Head, typename... Tail>
598   void SerializeAll(const Head &head, const Tail &... tail) {
599     Serialize(head);
600     SerializeAll(tail...);
601   }
602 
603   void SerializeAll() { m_stream.flush(); }
604 
605 private:
606   /// Serialize pointers. We need to differentiate between pointers to
607   /// fundamental types (in which case we serialize its value) and pointer to
608   /// objects (in which case we serialize their index).
609   template <typename T> void Serialize(T *t) {
610 #ifdef LLDB_REPRO_INSTR_TRACE
611     this_thread_id() << "Serializing with " << LLVM_PRETTY_FUNCTION << " -> "
612                      << stringify_args(t) << "\n";
613 #endif
614     if (std::is_fundamental<T>::value) {
615       Serialize(*t);
616     } else {
617       unsigned idx = m_tracker.GetIndexForObject(t);
618       Serialize(idx);
619     }
620   }
621 
622   /// Serialize references. We need to differentiate between references to
623   /// fundamental types (in which case we serialize its value) and references
624   /// to objects (in which case we serialize their index).
625   template <typename T> void Serialize(T &t) {
626 #ifdef LLDB_REPRO_INSTR_TRACE
627     this_thread_id() << "Serializing with " << LLVM_PRETTY_FUNCTION << " -> "
628                      << stringify_args(t) << "\n";
629 #endif
630     if (is_trivially_serializable<T>::value) {
631       m_stream.write(reinterpret_cast<const char *>(&t), sizeof(T));
632     } else {
633       unsigned idx = m_tracker.GetIndexForObject(&t);
634       Serialize(idx);
635     }
636   }
637 
638   void Serialize(const void *v) {
639     // FIXME: Support void*
640   }
641 
642   void Serialize(void *v) {
643     // FIXME: Support void*
644   }
645 
646   void Serialize(const char *t) {
647 #ifdef LLDB_REPRO_INSTR_TRACE
648     this_thread_id() << "Serializing with " << LLVM_PRETTY_FUNCTION << " -> "
649                      << stringify_args(t) << "\n";
650 #endif
651     const size_t size = t ? strlen(t) : std::numeric_limits<size_t>::max();
652     Serialize(size);
653     if (t) {
654       m_stream << t;
655       m_stream.write(0x0);
656     }
657   }
658 
659   void Serialize(const char **t) {
660     size_t size = 0;
661     if (!t) {
662       Serialize(size);
663       return;
664     }
665 
666     // Compute the size of the array.
667     const char *const *temp = t;
668     while (*temp++)
669       size++;
670     Serialize(size);
671 
672     // Serialize the content of the array.
673     while (*t)
674       Serialize(*t++);
675   }
676 
677   /// Serialization stream.
678   llvm::raw_ostream &m_stream;
679 
680   /// Mapping of objects to indices.
681   ObjectToIndex m_tracker;
682 }; // namespace repro
683 
684 class InstrumentationData {
685 public:
686   Serializer *GetSerializer() { return m_serializer; }
687   Deserializer *GetDeserializer() { return m_deserializer; }
688   Registry &GetRegistry() { return *m_registry; }
689 
690   operator bool() {
691     return (m_serializer != nullptr || m_deserializer != nullptr) &&
692            m_registry != nullptr;
693   }
694 
695   static void Initialize(Serializer &serializer, Registry &registry);
696   static void Initialize(Deserializer &serializer, Registry &registry);
697   static InstrumentationData &Instance();
698 
699 protected:
700   friend llvm::optional_detail::OptionalStorage<InstrumentationData, true>;
701   friend llvm::Optional<InstrumentationData>;
702 
703   InstrumentationData()
704       : m_serializer(nullptr), m_deserializer(nullptr), m_registry(nullptr) {}
705   InstrumentationData(Serializer &serializer, Registry &registry)
706       : m_serializer(&serializer), m_deserializer(nullptr),
707         m_registry(&registry) {}
708   InstrumentationData(Deserializer &deserializer, Registry &registry)
709       : m_serializer(nullptr), m_deserializer(&deserializer),
710         m_registry(&registry) {}
711 
712 private:
713   static llvm::Optional<InstrumentationData> &InstanceImpl();
714 
715   Serializer *m_serializer;
716   Deserializer *m_deserializer;
717   Registry *m_registry;
718 };
719 
720 struct EmptyArg {};
721 
722 /// RAII object that records function invocations and their return value.
723 ///
724 /// API calls are only captured when the API boundary is crossed. Once we're in
725 /// the API layer, and another API function is called, it doesn't need to be
726 /// recorded.
727 ///
728 /// When a call is recored, its result is always recorded as well, even if the
729 /// function returns a void. For functions that return by value, RecordResult
730 /// should be used. Otherwise a sentinel value (0) will be serialized.
731 ///
732 /// Because of the functional overlap between logging and recording API calls,
733 /// this class is also used for logging.
734 class Recorder {
735 public:
736   Recorder();
737   Recorder(llvm::StringRef pretty_func, std::string &&pretty_args = {});
738   ~Recorder();
739 
740   /// Records a single function call.
741   template <typename Result, typename... FArgs, typename... RArgs>
742   void Record(Serializer &serializer, Registry &registry, Result (*f)(FArgs...),
743               const RArgs &... args) {
744     m_serializer = &serializer;
745     if (!ShouldCapture())
746       return;
747 
748     unsigned id = registry.GetID(uintptr_t(f));
749 
750 #ifdef LLDB_REPRO_INSTR_TRACE
751     Log(id);
752 #endif
753 
754     serializer.SerializeAll(id);
755     serializer.SerializeAll(args...);
756 
757     if (std::is_class<typename std::remove_pointer<
758             typename std::remove_reference<Result>::type>::type>::value) {
759       m_result_recorded = false;
760     } else {
761       serializer.SerializeAll(0);
762       m_result_recorded = true;
763     }
764   }
765 
766   /// Records a single function call.
767   template <typename... Args>
768   void Record(Serializer &serializer, Registry &registry, void (*f)(Args...),
769               const Args &... args) {
770     m_serializer = &serializer;
771     if (!ShouldCapture())
772       return;
773 
774     unsigned id = registry.GetID(uintptr_t(f));
775 
776 #ifdef LLDB_REPRO_INSTR_TRACE
777     Log(id);
778 #endif
779 
780     serializer.SerializeAll(id);
781     serializer.SerializeAll(args...);
782 
783     // Record result.
784     serializer.SerializeAll(0);
785     m_result_recorded = true;
786   }
787 
788   /// Specializations for the no-argument methods. These are passed an empty
789   /// dummy argument so the same variadic macro can be used. These methods
790   /// strip the arguments before forwarding them.
791   template <typename Result>
792   void Record(Serializer &serializer, Registry &registry, Result (*f)(),
793               const EmptyArg &arg) {
794     Record(serializer, registry, f);
795   }
796 
797   /// Record the result of a function call.
798   template <typename Result>
799   Result RecordResult(Result &&r, bool update_boundary) {
800     // When recording the result from the LLDB_RECORD_RESULT macro, we need to
801     // update the boundary so we capture the copy constructor. However, when
802     // called to record the this pointer of the (copy) constructor, the
803     // boundary should not be toggled, because it is called from the
804     // LLDB_RECORD_CONSTRUCTOR macro, which might be followed by other API
805     // calls.
806     if (update_boundary)
807       UpdateBoundary();
808     if (m_serializer && ShouldCapture()) {
809       assert(!m_result_recorded);
810       m_serializer->SerializeAll(r);
811       m_result_recorded = true;
812     }
813     return std::forward<Result>(r);
814   }
815 
816   template <typename Result, typename T>
817   Result Replay(Deserializer &deserializer, Registry &registry, uintptr_t addr,
818                 bool update_boundary) {
819     unsigned actual_id = registry.GetID(addr);
820     unsigned id = deserializer.Deserialize<unsigned>();
821     registry.CheckID(id, actual_id);
822     return ReplayResult<Result>(
823         static_cast<DefaultReplayer<T> *>(registry.GetReplayer(id))
824             ->Replay(deserializer),
825         update_boundary);
826   }
827 
828   void Replay(Deserializer &deserializer, Registry &registry, uintptr_t addr) {
829     unsigned actual_id = registry.GetID(addr);
830     unsigned id = deserializer.Deserialize<unsigned>();
831     registry.CheckID(id, actual_id);
832     registry.GetReplayer(id)->operator()(deserializer);
833   }
834 
835   template <typename Result>
836   Result ReplayResult(Result &&r, bool update_boundary) {
837     if (update_boundary)
838       UpdateBoundary();
839     return std::forward<Result>(r);
840   }
841 
842   bool ShouldCapture() { return m_local_boundary; }
843 
844   /// Mark the current thread as a private thread and pretend that everything
845   /// on this thread is behind happening behind the API boundary.
846   static void PrivateThread() { g_global_boundary = true; }
847 
848 private:
849   template <typename T> friend struct replay;
850   void UpdateBoundary() {
851     if (m_local_boundary)
852       g_global_boundary = false;
853   }
854 
855 #ifdef LLDB_REPRO_INSTR_TRACE
856   void Log(unsigned id) {
857     this_thread_id() << "Recording " << id << ": " << m_pretty_func << " ("
858                      << m_pretty_args << ")\n";
859   }
860 #endif
861 
862   Serializer *m_serializer;
863 
864   /// Pretty function for logging.
865   llvm::StringRef m_pretty_func;
866   std::string m_pretty_args;
867 
868   /// Whether this function call was the one crossing the API boundary.
869   bool m_local_boundary;
870 
871   /// Whether the return value was recorded explicitly.
872   bool m_result_recorded;
873 
874   /// Whether we're currently across the API boundary.
875   static thread_local bool g_global_boundary;
876 };
877 
878 /// To be used as the "Runtime ID" of a constructor. It also invokes the
879 /// constructor when called.
880 template <typename Signature> struct construct;
881 template <typename Class, typename... Args> struct construct<Class(Args...)> {
882   static Class *handle(lldb_private::repro::InstrumentationData data,
883                        lldb_private::repro::Recorder &recorder, Class *c,
884                        const EmptyArg &) {
885     return handle(data, recorder, c);
886   }
887 
888   static Class *handle(lldb_private::repro::InstrumentationData data,
889                        lldb_private::repro::Recorder &recorder, Class *c,
890                        Args... args) {
891     if (!data)
892       return nullptr;
893 
894     if (Serializer *serializer = data.GetSerializer()) {
895       recorder.Record(*serializer, data.GetRegistry(), &record, args...);
896       recorder.RecordResult(c, false);
897     } else if (Deserializer *deserializer = data.GetDeserializer()) {
898       if (recorder.ShouldCapture()) {
899         replay(recorder, *deserializer, data.GetRegistry());
900       }
901     }
902 
903     return nullptr;
904   }
905 
906   static Class *record(Args... args) { return new Class(args...); }
907 
908   static Class *replay(Recorder &recorder, Deserializer &deserializer,
909                        Registry &registry) {
910     return recorder.Replay<Class *, Class *(Args...)>(
911         deserializer, registry, uintptr_t(&record), false);
912   }
913 };
914 
915 /// To be used as the "Runtime ID" of a member function. It also invokes the
916 /// member function when called.
917 template <typename Signature> struct invoke;
918 template <typename Result, typename Class, typename... Args>
919 struct invoke<Result (Class::*)(Args...)> {
920   template <Result (Class::*m)(Args...)> struct method {
921     static Result record(Class *c, Args... args) { return (c->*m)(args...); }
922 
923     static Result replay(Recorder &recorder, Deserializer &deserializer,
924                          Registry &registry) {
925       return recorder.Replay<Result, Result(Class *, Args...)>(
926           deserializer, registry, uintptr_t(&record), true);
927     }
928   };
929 };
930 
931 template <typename Class, typename... Args>
932 struct invoke<void (Class::*)(Args...)> {
933   template <void (Class::*m)(Args...)> struct method {
934     static void record(Class *c, Args... args) { (c->*m)(args...); }
935     static void replay(Recorder &recorder, Deserializer &deserializer,
936                        Registry &registry) {
937       recorder.Replay(deserializer, registry, uintptr_t(&record));
938     }
939   };
940 };
941 
942 template <typename Result, typename Class, typename... Args>
943 struct invoke<Result (Class::*)(Args...) const> {
944   template <Result (Class::*m)(Args...) const> struct method {
945     static Result record(Class *c, Args... args) { return (c->*m)(args...); }
946     static Result replay(Recorder &recorder, Deserializer &deserializer,
947                          Registry &registry) {
948       return recorder.Replay<Result, Result(Class *, Args...)>(
949           deserializer, registry, uintptr_t(&record), true);
950     }
951   };
952 };
953 
954 template <typename Class, typename... Args>
955 struct invoke<void (Class::*)(Args...) const> {
956   template <void (Class::*m)(Args...) const> struct method {
957     static void record(Class *c, Args... args) { return (c->*m)(args...); }
958     static void replay(Recorder &recorder, Deserializer &deserializer,
959                        Registry &registry) {
960       recorder.Replay(deserializer, registry, uintptr_t(&record));
961     }
962   };
963 };
964 
965 template <typename Signature> struct replay;
966 
967 template <typename Result, typename Class, typename... Args>
968 struct replay<Result (Class::*)(Args...)> {
969   template <Result (Class::*m)(Args...)> struct method {};
970 };
971 
972 template <typename Result, typename... Args>
973 struct invoke<Result (*)(Args...)> {
974   template <Result (*m)(Args...)> struct method {
975     static Result record(Args... args) { return (*m)(args...); }
976     static Result replay(Recorder &recorder, Deserializer &deserializer,
977                          Registry &registry) {
978       return recorder.Replay<Result, Result(Args...)>(deserializer, registry,
979                                                       uintptr_t(&record), true);
980     }
981   };
982 };
983 
984 template <typename... Args> struct invoke<void (*)(Args...)> {
985   template <void (*m)(Args...)> struct method {
986     static void record(Args... args) { return (*m)(args...); }
987     static void replay(Recorder &recorder, Deserializer &deserializer,
988                        Registry &registry) {
989       recorder.Replay(deserializer, registry, uintptr_t(&record));
990     }
991   };
992 };
993 
994 /// Special handling for functions returning strings as (char*, size_t).
995 /// {
996 
997 /// For inline replay, we ignore the arguments and use the ones from the
998 /// serializer instead. This doesn't work for methods that use a char* and a
999 /// size to return a string. For one these functions have a custom replayer to
1000 /// prevent override the input buffer. Furthermore, the template-generated
1001 /// deserialization is not easy to hook into.
1002 ///
1003 /// The specializations below hand-implement the serialization logic for the
1004 /// inline replay. Instead of using the function from the registry, it uses the
1005 /// one passed into the macro.
1006 template <typename Signature> struct invoke_char_ptr;
1007 template <typename Result, typename Class, typename... Args>
1008 struct invoke_char_ptr<Result (Class::*)(Args...) const> {
1009   template <Result (Class::*m)(Args...) const> struct method {
1010     static Result record(Class *c, char *s, size_t l) {
1011       char *buffer = reinterpret_cast<char *>(calloc(l, sizeof(char)));
1012       return (c->*m)(buffer, l);
1013     }
1014 
1015     static Result replay(Recorder &recorder, Deserializer &deserializer,
1016                          Registry &registry, char *str) {
1017       deserializer.Deserialize<unsigned>();
1018       Class *c = deserializer.Deserialize<Class *>();
1019       deserializer.Deserialize<const char *>();
1020       size_t l = deserializer.Deserialize<size_t>();
1021       return recorder.ReplayResult(
1022           std::move(deserializer.HandleReplayResult((c->*m)(str, l))), true);
1023     }
1024   };
1025 };
1026 
1027 template <typename Signature> struct invoke_char_ptr;
1028 template <typename Result, typename Class, typename... Args>
1029 struct invoke_char_ptr<Result (Class::*)(Args...)> {
1030   template <Result (Class::*m)(Args...)> struct method {
1031     static Result record(Class *c, char *s, size_t l) {
1032       char *buffer = reinterpret_cast<char *>(calloc(l, sizeof(char)));
1033       return (c->*m)(buffer, l);
1034     }
1035 
1036     static Result replay(Recorder &recorder, Deserializer &deserializer,
1037                          Registry &registry, char *str) {
1038       deserializer.Deserialize<unsigned>();
1039       Class *c = deserializer.Deserialize<Class *>();
1040       deserializer.Deserialize<const char *>();
1041       size_t l = deserializer.Deserialize<size_t>();
1042       return recorder.ReplayResult(
1043           std::move(deserializer.HandleReplayResult((c->*m)(str, l))), true);
1044     }
1045   };
1046 };
1047 
1048 template <typename Result, typename... Args>
1049 struct invoke_char_ptr<Result (*)(Args...)> {
1050   template <Result (*m)(Args...)> struct method {
1051     static Result record(char *s, size_t l) {
1052       char *buffer = reinterpret_cast<char *>(calloc(l, sizeof(char)));
1053       return (*m)(buffer, l);
1054     }
1055 
1056     static Result replay(Recorder &recorder, Deserializer &deserializer,
1057                          Registry &registry, char *str) {
1058       deserializer.Deserialize<unsigned>();
1059       deserializer.Deserialize<const char *>();
1060       size_t l = deserializer.Deserialize<size_t>();
1061       return recorder.ReplayResult(
1062           std::move(deserializer.HandleReplayResult((*m)(str, l))), true);
1063     }
1064   };
1065 };
1066 /// }
1067 
1068 } // namespace repro
1069 } // namespace lldb_private
1070 
1071 #endif // LLDB_UTILITY_REPRODUCERINSTRUMENTATION_H
1072