1 //===-- IRForTarget.cpp ---------------------------------------------------===//
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 #include "IRForTarget.h"
10 
11 #include "ClangExpressionDeclMap.h"
12 #include "ClangUtil.h"
13 
14 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Operator.h"
18 #include "llvm/IR/InstrTypes.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LegacyPassManager.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/ValueSymbolTable.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/IPO.h"
27 
28 #include "clang/AST/ASTContext.h"
29 
30 #include "lldb/Core/dwarf.h"
31 #include "lldb/Expression/IRExecutionUnit.h"
32 #include "lldb/Expression/IRInterpreter.h"
33 #include "lldb/Symbol/CompilerType.h"
34 #include "lldb/Utility/ConstString.h"
35 #include "lldb/Utility/DataBufferHeap.h"
36 #include "lldb/Utility/Endian.h"
37 #include "lldb/Utility/Log.h"
38 #include "lldb/Utility/Scalar.h"
39 #include "lldb/Utility/StreamString.h"
40 
41 #include <map>
42 
43 using namespace llvm;
44 using lldb_private::LLDBLog;
45 
46 typedef SmallVector<Instruction *, 2> InstrList;
47 
48 IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker)
49     : m_maker(maker), m_values() {}
50 
51 IRForTarget::FunctionValueCache::~FunctionValueCache() = default;
52 
53 llvm::Value *
54 IRForTarget::FunctionValueCache::GetValue(llvm::Function *function) {
55   if (!m_values.count(function)) {
56     llvm::Value *ret = m_maker(function);
57     m_values[function] = ret;
58     return ret;
59   }
60   return m_values[function];
61 }
62 
63 static llvm::Value *FindEntryInstruction(llvm::Function *function) {
64   if (function->empty())
65     return nullptr;
66 
67   return function->getEntryBlock().getFirstNonPHIOrDbg();
68 }
69 
70 IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
71                          bool resolve_vars,
72                          lldb_private::IRExecutionUnit &execution_unit,
73                          lldb_private::Stream &error_stream,
74                          const char *func_name)
75     : m_resolve_vars(resolve_vars), m_func_name(func_name),
76       m_decl_map(decl_map), m_error_stream(error_stream),
77       m_execution_unit(execution_unit),
78       m_entry_instruction_finder(FindEntryInstruction) {}
79 
80 /* Handy utility functions used at several places in the code */
81 
82 static std::string PrintValue(const Value *value, bool truncate = false) {
83   std::string s;
84   if (value) {
85     raw_string_ostream rso(s);
86     value->print(rso);
87     rso.flush();
88     if (truncate)
89       s.resize(s.length() - 1);
90   }
91   return s;
92 }
93 
94 static std::string PrintType(const llvm::Type *type, bool truncate = false) {
95   std::string s;
96   raw_string_ostream rso(s);
97   type->print(rso);
98   rso.flush();
99   if (truncate)
100     s.resize(s.length() - 1);
101   return s;
102 }
103 
104 bool IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function) {
105   llvm_function.setLinkage(GlobalValue::ExternalLinkage);
106 
107   return true;
108 }
109 
110 clang::NamedDecl *IRForTarget::DeclForGlobal(const GlobalValue *global_val,
111                                              Module *module) {
112   NamedMDNode *named_metadata =
113       module->getNamedMetadata("clang.global.decl.ptrs");
114 
115   if (!named_metadata)
116     return nullptr;
117 
118   unsigned num_nodes = named_metadata->getNumOperands();
119   unsigned node_index;
120 
121   for (node_index = 0; node_index < num_nodes; ++node_index) {
122     llvm::MDNode *metadata_node =
123         dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
124     if (!metadata_node)
125       return nullptr;
126 
127     if (metadata_node->getNumOperands() != 2)
128       continue;
129 
130     if (mdconst::dyn_extract_or_null<GlobalValue>(
131             metadata_node->getOperand(0)) != global_val)
132       continue;
133 
134     ConstantInt *constant_int =
135         mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
136 
137     if (!constant_int)
138       return nullptr;
139 
140     uintptr_t ptr = constant_int->getZExtValue();
141 
142     return reinterpret_cast<clang::NamedDecl *>(ptr);
143   }
144 
145   return nullptr;
146 }
147 
148 clang::NamedDecl *IRForTarget::DeclForGlobal(GlobalValue *global_val) {
149   return DeclForGlobal(global_val, m_module);
150 }
151 
152 /// Returns true iff the mangled symbol is for a static guard variable.
153 static bool isGuardVariableSymbol(llvm::StringRef mangled_symbol,
154                                   bool check_ms_abi = true) {
155   bool result = mangled_symbol.startswith("_ZGV"); // Itanium ABI guard variable
156   if (check_ms_abi)
157     result |= mangled_symbol.endswith("@4IA"); // Microsoft ABI
158   return result;
159 }
160 
161 bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
162   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
163 
164   if (!m_resolve_vars)
165     return true;
166 
167   // Find the result variable.  If it doesn't exist, we can give up right here.
168 
169   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
170 
171   llvm::StringRef result_name;
172   bool found_result = false;
173 
174   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
175     result_name = value_symbol.first();
176 
177     // Check if this is a guard variable. It seems this causes some hiccups
178     // on Windows, so let's only check for Itanium guard variables.
179     bool is_guard_var = isGuardVariableSymbol(result_name, /*MS ABI*/ false);
180 
181     if (result_name.contains("$__lldb_expr_result_ptr") && !is_guard_var) {
182       found_result = true;
183       m_result_is_pointer = true;
184       break;
185     }
186 
187     if (result_name.contains("$__lldb_expr_result") && !is_guard_var) {
188       found_result = true;
189       m_result_is_pointer = false;
190       break;
191     }
192   }
193 
194   if (!found_result) {
195     LLDB_LOG(log, "Couldn't find result variable");
196 
197     return true;
198   }
199 
200   LLDB_LOG(log, "Result name: \"{0}\"", result_name);
201 
202   Value *result_value = m_module->getNamedValue(result_name);
203 
204   if (!result_value) {
205     LLDB_LOG(log, "Result variable had no data");
206 
207     m_error_stream.Format("Internal error [IRForTarget]: Result variable's "
208                           "name ({0}) exists, but not its definition\n",
209                           result_name);
210 
211     return false;
212   }
213 
214   LLDB_LOG(log, "Found result in the IR: \"{0}\"",
215            PrintValue(result_value, false));
216 
217   GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
218 
219   if (!result_global) {
220     LLDB_LOG(log, "Result variable isn't a GlobalVariable");
221 
222     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
223                           "is defined, but is not a global variable\n",
224                           result_name);
225 
226     return false;
227   }
228 
229   clang::NamedDecl *result_decl = DeclForGlobal(result_global);
230   if (!result_decl) {
231     LLDB_LOG(log, "Result variable doesn't have a corresponding Decl");
232 
233     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
234                           "does not have a corresponding Clang entity\n",
235                           result_name);
236 
237     return false;
238   }
239 
240   if (log) {
241     std::string decl_desc_str;
242     raw_string_ostream decl_desc_stream(decl_desc_str);
243     result_decl->print(decl_desc_stream);
244     decl_desc_stream.flush();
245 
246     LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str);
247   }
248 
249   clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
250   if (!result_var) {
251     LLDB_LOG(log, "Result variable Decl isn't a VarDecl");
252 
253     m_error_stream.Format("Internal error [IRForTarget]: Result variable "
254                           "({0})'s corresponding Clang entity isn't a "
255                           "variable\n",
256                           result_name);
257 
258     return false;
259   }
260 
261   // Get the next available result name from m_decl_map and create the
262   // persistent variable for it
263 
264   // If the result is an Lvalue, it is emitted as a pointer; see
265   // ASTResultSynthesizer::SynthesizeBodyResult.
266   if (m_result_is_pointer) {
267     clang::QualType pointer_qual_type = result_var->getType();
268     const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
269 
270     const clang::PointerType *pointer_pointertype =
271         pointer_type->getAs<clang::PointerType>();
272     const clang::ObjCObjectPointerType *pointer_objcobjpointertype =
273         pointer_type->getAs<clang::ObjCObjectPointerType>();
274 
275     if (pointer_pointertype) {
276       clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
277 
278       m_result_type = lldb_private::TypeFromParser(
279           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
280     } else if (pointer_objcobjpointertype) {
281       clang::QualType element_qual_type =
282           clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
283 
284       m_result_type = lldb_private::TypeFromParser(
285           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
286     } else {
287       LLDB_LOG(log, "Expected result to have pointer type, but it did not");
288 
289       m_error_stream.Format("Internal error [IRForTarget]: Lvalue result ({0}) "
290                             "is not a pointer variable\n",
291                             result_name);
292 
293       return false;
294     }
295   } else {
296     m_result_type = lldb_private::TypeFromParser(
297         m_decl_map->GetTypeSystem()->GetType(result_var->getType()));
298   }
299 
300   lldb::TargetSP target_sp(m_execution_unit.GetTarget());
301   llvm::Optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get());
302   if (!bit_size) {
303     lldb_private::StreamString type_desc_stream;
304     m_result_type.DumpTypeDescription(&type_desc_stream);
305 
306     LLDB_LOG(log, "Result type has unknown size");
307 
308     m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' "
309                           "couldn't be determined\n",
310                           type_desc_stream.GetData());
311     return false;
312   }
313 
314   if (log) {
315     lldb_private::StreamString type_desc_stream;
316     m_result_type.DumpTypeDescription(&type_desc_stream);
317 
318     LLDB_LOG(log, "Result decl type: \"{0}\"", type_desc_stream.GetData());
319   }
320 
321   m_result_name = lldb_private::ConstString("$RESULT_NAME");
322 
323   LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}",
324            m_result_name,
325            m_result_type.GetByteSize(target_sp.get()).getValueOr(0));
326 
327   // Construct a new result global and set up its metadata
328 
329   GlobalVariable *new_result_global = new GlobalVariable(
330       (*m_module), result_global->getType()->getElementType(),
331       false,                                 /* not constant */
332       GlobalValue::ExternalLinkage, nullptr, /* no initializer */
333       m_result_name.GetCString());
334 
335   // It's too late in compilation to create a new VarDecl for this, but we
336   // don't need to.  We point the metadata at the old VarDecl.  This creates an
337   // odd anomaly: a variable with a Value whose name is something like $0 and a
338   // Decl whose name is $__lldb_expr_result.  This condition is handled in
339   // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
340   // fixed up.
341 
342   ConstantInt *new_constant_int =
343       ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
344                        reinterpret_cast<uintptr_t>(result_decl), false);
345 
346   llvm::Metadata *values[2];
347   values[0] = ConstantAsMetadata::get(new_result_global);
348   values[1] = ConstantAsMetadata::get(new_constant_int);
349 
350   ArrayRef<Metadata *> value_ref(values, 2);
351 
352   MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
353   NamedMDNode *named_metadata =
354       m_module->getNamedMetadata("clang.global.decl.ptrs");
355   named_metadata->addOperand(persistent_global_md);
356 
357   LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(result_global),
358            PrintValue(new_result_global));
359 
360   if (result_global->use_empty()) {
361     // We need to synthesize a store for this variable, because otherwise
362     // there's nothing to put into its equivalent persistent variable.
363 
364     BasicBlock &entry_block(llvm_function.getEntryBlock());
365     Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
366 
367     if (!first_entry_instruction)
368       return false;
369 
370     if (!result_global->hasInitializer()) {
371       LLDB_LOG(log, "Couldn't find initializer for unused variable");
372 
373       m_error_stream.Format("Internal error [IRForTarget]: Result variable "
374                             "({0}) has no writes and no initializer\n",
375                             result_name);
376 
377       return false;
378     }
379 
380     Constant *initializer = result_global->getInitializer();
381 
382     StoreInst *synthesized_store =
383         new StoreInst(initializer, new_result_global, first_entry_instruction);
384 
385     LLDB_LOG(log, "Synthesized result store \"{0}\"\n",
386              PrintValue(synthesized_store));
387   } else {
388     result_global->replaceAllUsesWith(new_result_global);
389   }
390 
391   if (!m_decl_map->AddPersistentVariable(
392           result_decl, m_result_name, m_result_type, true, m_result_is_pointer))
393     return false;
394 
395   result_global->eraseFromParent();
396 
397   return true;
398 }
399 
400 bool IRForTarget::RewriteObjCConstString(llvm::GlobalVariable *ns_str,
401                                          llvm::GlobalVariable *cstr) {
402   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
403 
404   Type *ns_str_ty = ns_str->getType();
405 
406   Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
407   Type *i32_ty = Type::getInt32Ty(m_module->getContext());
408   Type *i8_ty = Type::getInt8Ty(m_module->getContext());
409 
410   if (!m_CFStringCreateWithBytes) {
411     lldb::addr_t CFStringCreateWithBytes_addr;
412 
413     static lldb_private::ConstString g_CFStringCreateWithBytes_str(
414         "CFStringCreateWithBytes");
415 
416     bool missing_weak = false;
417     CFStringCreateWithBytes_addr =
418         m_execution_unit.FindSymbol(g_CFStringCreateWithBytes_str,
419                                     missing_weak);
420     if (CFStringCreateWithBytes_addr == LLDB_INVALID_ADDRESS || missing_weak) {
421       LLDB_LOG(log, "Couldn't find CFStringCreateWithBytes in the target");
422 
423       m_error_stream.Printf("Error [IRForTarget]: Rewriting an Objective-C "
424                             "constant string requires "
425                             "CFStringCreateWithBytes\n");
426 
427       return false;
428     }
429 
430     LLDB_LOG(log, "Found CFStringCreateWithBytes at {0}",
431              CFStringCreateWithBytes_addr);
432 
433     // Build the function type:
434     //
435     // CFStringRef CFStringCreateWithBytes (
436     //   CFAllocatorRef alloc,
437     //   const UInt8 *bytes,
438     //   CFIndex numBytes,
439     //   CFStringEncoding encoding,
440     //   Boolean isExternalRepresentation
441     // );
442     //
443     // We make the following substitutions:
444     //
445     // CFStringRef -> i8*
446     // CFAllocatorRef -> i8*
447     // UInt8 * -> i8*
448     // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its
449     // pointer size for now) CFStringEncoding -> i32 Boolean -> i8
450 
451     Type *arg_type_array[5];
452 
453     arg_type_array[0] = i8_ptr_ty;
454     arg_type_array[1] = i8_ptr_ty;
455     arg_type_array[2] = m_intptr_ty;
456     arg_type_array[3] = i32_ty;
457     arg_type_array[4] = i8_ty;
458 
459     ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
460 
461     llvm::FunctionType *CFSCWB_ty =
462         FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
463 
464     // Build the constant containing the pointer to the function
465     PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
466     Constant *CFSCWB_addr_int =
467         ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
468     m_CFStringCreateWithBytes = {
469         CFSCWB_ty, ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty)};
470   }
471 
472   ConstantDataSequential *string_array = nullptr;
473 
474   if (cstr)
475     string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
476 
477   Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
478   Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty)
479                              : Constant::getNullValue(i8_ptr_ty);
480   Constant *numBytes_arg = ConstantInt::get(
481       m_intptr_ty, cstr ? (string_array->getNumElements() - 1) * string_array->getElementByteSize() : 0, false);
482  int encoding_flags = 0;
483  switch (cstr ? string_array->getElementByteSize() : 1) {
484  case 1:
485    encoding_flags = 0x08000100; /* 0x08000100 is kCFStringEncodingUTF8 */
486    break;
487  case 2:
488    encoding_flags = 0x0100; /* 0x0100 is kCFStringEncodingUTF16 */
489    break;
490  case 4:
491    encoding_flags = 0x0c000100; /* 0x0c000100 is kCFStringEncodingUTF32 */
492    break;
493  default:
494    encoding_flags = 0x0600; /* fall back to 0x0600, kCFStringEncodingASCII */
495    LLDB_LOG(log, "Encountered an Objective-C constant string with unusual "
496                  "element size {0}",
497             string_array->getElementByteSize());
498  }
499  Constant *encoding_arg = ConstantInt::get(i32_ty, encoding_flags, false);
500  Constant *isExternal_arg =
501      ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
502 
503  Value *argument_array[5];
504 
505  argument_array[0] = alloc_arg;
506  argument_array[1] = bytes_arg;
507  argument_array[2] = numBytes_arg;
508  argument_array[3] = encoding_arg;
509  argument_array[4] = isExternal_arg;
510 
511  ArrayRef<Value *> CFSCWB_arguments(argument_array, 5);
512 
513  FunctionValueCache CFSCWB_Caller(
514      [this, &CFSCWB_arguments](llvm::Function *function) -> llvm::Value * {
515        return CallInst::Create(
516            m_CFStringCreateWithBytes, CFSCWB_arguments,
517            "CFStringCreateWithBytes",
518            llvm::cast<Instruction>(
519                m_entry_instruction_finder.GetValue(function)));
520      });
521 
522  if (!UnfoldConstant(ns_str, nullptr, CFSCWB_Caller, m_entry_instruction_finder,
523                      m_error_stream)) {
524    LLDB_LOG(log, "Couldn't replace the NSString with the result of the call");
525 
526    m_error_stream.Printf("error [IRForTarget internal]: Couldn't replace an "
527                          "Objective-C constant string with a dynamic "
528                          "string\n");
529 
530    return false;
531   }
532 
533   ns_str->eraseFromParent();
534 
535   return true;
536 }
537 
538 bool IRForTarget::RewriteObjCConstStrings() {
539   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
540 
541   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
542 
543   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
544     llvm::StringRef value_name = value_symbol.first();
545 
546     if (value_name.contains("_unnamed_cfstring_")) {
547       Value *nsstring_value = value_symbol.second;
548 
549       GlobalVariable *nsstring_global =
550           dyn_cast<GlobalVariable>(nsstring_value);
551 
552       if (!nsstring_global) {
553         LLDB_LOG(log, "NSString variable is not a GlobalVariable");
554 
555         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
556                               "constant string is not a global variable\n");
557 
558         return false;
559       }
560 
561       if (!nsstring_global->hasInitializer()) {
562         LLDB_LOG(log, "NSString variable does not have an initializer");
563 
564         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
565                               "constant string does not have an initializer\n");
566 
567         return false;
568       }
569 
570       ConstantStruct *nsstring_struct =
571           dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
572 
573       if (!nsstring_struct) {
574         LLDB_LOG(log,
575                  "NSString variable's initializer is not a ConstantStruct");
576 
577         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
578                               "constant string is not a structure constant\n");
579 
580         return false;
581       }
582 
583       // We expect the following structure:
584       //
585       // struct {
586       //   int *isa;
587       //   int flags;
588       //   char *str;
589       //   long length;
590       // };
591 
592       if (nsstring_struct->getNumOperands() != 4) {
593 
594         LLDB_LOG(log,
595                  "NSString variable's initializer structure has an "
596                  "unexpected number of members.  Should be 4, is {0}",
597                  nsstring_struct->getNumOperands());
598 
599         m_error_stream.Printf("Internal error [IRForTarget]: The struct for an "
600                               "Objective-C constant string is not as "
601                               "expected\n");
602 
603         return false;
604       }
605 
606       Constant *nsstring_member = nsstring_struct->getOperand(2);
607 
608       if (!nsstring_member) {
609         LLDB_LOG(log, "NSString initializer's str element was empty");
610 
611         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
612                               "constant string does not have a string "
613                               "initializer\n");
614 
615         return false;
616       }
617 
618       ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
619 
620       if (!nsstring_expr) {
621         LLDB_LOG(log,
622                  "NSString initializer's str element is not a ConstantExpr");
623 
624         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
625                               "constant string's string initializer is not "
626                               "constant\n");
627 
628         return false;
629       }
630 
631       GlobalVariable *cstr_global = nullptr;
632 
633       if (nsstring_expr->getOpcode() == Instruction::GetElementPtr) {
634         Constant *nsstring_cstr = nsstring_expr->getOperand(0);
635         cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
636       } else if (nsstring_expr->getOpcode() == Instruction::BitCast) {
637         Constant *nsstring_cstr = nsstring_expr->getOperand(0);
638         cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
639       }
640 
641       if (!cstr_global) {
642         LLDB_LOG(log,
643                  "NSString initializer's str element is not a GlobalVariable");
644 
645         m_error_stream.Printf("Internal error [IRForTarget]: Unhandled"
646                               "constant string initializer\n");
647 
648         return false;
649       }
650 
651       if (!cstr_global->hasInitializer()) {
652         LLDB_LOG(log, "NSString initializer's str element does not have an "
653                       "initializer");
654 
655         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
656                               "constant string's string initializer doesn't "
657                               "point to initialized data\n");
658 
659         return false;
660       }
661 
662       /*
663       if (!cstr_array)
664       {
665           if (log)
666               log->PutCString("NSString initializer's str element is not a
667       ConstantArray");
668 
669           if (m_error_stream)
670               m_error_stream.Printf("Internal error [IRForTarget]: An
671       Objective-C constant string's string initializer doesn't point to an
672       array\n");
673 
674           return false;
675       }
676 
677       if (!cstr_array->isCString())
678       {
679           if (log)
680               log->PutCString("NSString initializer's str element is not a C
681       string array");
682 
683           if (m_error_stream)
684               m_error_stream.Printf("Internal error [IRForTarget]: An
685       Objective-C constant string's string initializer doesn't point to a C
686       string\n");
687 
688           return false;
689       }
690       */
691 
692       ConstantDataArray *cstr_array =
693           dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
694 
695       if (cstr_array)
696         LLDB_LOG(log, "Found NSString constant {0}, which contains \"{1}\"",
697                  value_name, cstr_array->getAsString());
698       else
699         LLDB_LOG(log, "Found NSString constant {0}, which contains \"\"",
700                  value_name);
701 
702       if (!cstr_array)
703         cstr_global = nullptr;
704 
705       if (!RewriteObjCConstString(nsstring_global, cstr_global)) {
706         LLDB_LOG(log, "Error rewriting the constant string");
707 
708         // We don't print an error message here because RewriteObjCConstString
709         // has done so for us.
710 
711         return false;
712       }
713     }
714   }
715 
716   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
717     llvm::StringRef value_name = value_symbol.first();
718 
719     if (value_name == "__CFConstantStringClassReference") {
720       GlobalVariable *gv = dyn_cast<GlobalVariable>(value_symbol.second);
721 
722       if (!gv) {
723         LLDB_LOG(log,
724                  "__CFConstantStringClassReference is not a global variable");
725 
726         m_error_stream.Printf("Internal error [IRForTarget]: Found a "
727                               "CFConstantStringClassReference, but it is not a "
728                               "global object\n");
729 
730         return false;
731       }
732 
733       gv->eraseFromParent();
734 
735       break;
736     }
737   }
738 
739   return true;
740 }
741 
742 static bool IsObjCSelectorRef(Value *value) {
743   GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
744 
745   return !(!global_variable || !global_variable->hasName() ||
746            !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"));
747 }
748 
749 // This function does not report errors; its callers are responsible.
750 bool IRForTarget::RewriteObjCSelector(Instruction *selector_load) {
751   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
752 
753   LoadInst *load = dyn_cast<LoadInst>(selector_load);
754 
755   if (!load)
756     return false;
757 
758   // Unpack the message name from the selector.  In LLVM IR, an objc_msgSend
759   // gets represented as
760   //
761   // %tmp     = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*> %call    = call
762   // i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
763   //
764   // where %obj is the object pointer and %tmp is the selector.
765   //
766   // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called
767   // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
768   // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
769 
770   // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr)
771   // and get the string from its target
772 
773   GlobalVariable *_objc_selector_references_ =
774       dyn_cast<GlobalVariable>(load->getPointerOperand());
775 
776   if (!_objc_selector_references_ ||
777       !_objc_selector_references_->hasInitializer())
778     return false;
779 
780   Constant *osr_initializer = _objc_selector_references_->getInitializer();
781 
782   ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
783 
784   if (!osr_initializer_expr ||
785       osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
786     return false;
787 
788   Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
789 
790   if (!osr_initializer_base)
791     return false;
792 
793   // Find the string's initializer (a ConstantArray) and get the string from it
794 
795   GlobalVariable *_objc_meth_var_name_ =
796       dyn_cast<GlobalVariable>(osr_initializer_base);
797 
798   if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
799     return false;
800 
801   Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
802 
803   ConstantDataArray *omvn_initializer_array =
804       dyn_cast<ConstantDataArray>(omvn_initializer);
805 
806   if (!omvn_initializer_array->isString())
807     return false;
808 
809   std::string omvn_initializer_string =
810       std::string(omvn_initializer_array->getAsString());
811 
812   LLDB_LOG(log, "Found Objective-C selector reference \"{0}\"",
813            omvn_initializer_string);
814 
815   // Construct a call to sel_registerName
816 
817   if (!m_sel_registerName) {
818     lldb::addr_t sel_registerName_addr;
819 
820     bool missing_weak = false;
821     static lldb_private::ConstString g_sel_registerName_str("sel_registerName");
822     sel_registerName_addr = m_execution_unit.FindSymbol(g_sel_registerName_str,
823                                                         missing_weak);
824     if (sel_registerName_addr == LLDB_INVALID_ADDRESS || missing_weak)
825       return false;
826 
827     LLDB_LOG(log, "Found sel_registerName at {0}", sel_registerName_addr);
828 
829     // Build the function type: struct objc_selector
830     // *sel_registerName(uint8_t*)
831 
832     // The below code would be "more correct," but in actuality what's required
833     // is uint8_t*
834     // Type *sel_type = StructType::get(m_module->getContext());
835     // Type *sel_ptr_type = PointerType::getUnqual(sel_type);
836     Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
837 
838     Type *type_array[1];
839 
840     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
841 
842     ArrayRef<Type *> srN_arg_types(type_array, 1);
843 
844     llvm::FunctionType *srN_type =
845         FunctionType::get(sel_ptr_type, srN_arg_types, false);
846 
847     // Build the constant containing the pointer to the function
848     PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
849     Constant *srN_addr_int =
850         ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
851     m_sel_registerName = {srN_type,
852                           ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty)};
853   }
854 
855   Value *argument_array[1];
856 
857   Constant *omvn_pointer = ConstantExpr::getBitCast(
858       _objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
859 
860   argument_array[0] = omvn_pointer;
861 
862   ArrayRef<Value *> srN_arguments(argument_array, 1);
863 
864   CallInst *srN_call = CallInst::Create(m_sel_registerName, srN_arguments,
865                                         "sel_registerName", selector_load);
866 
867   // Replace the load with the call in all users
868 
869   selector_load->replaceAllUsesWith(srN_call);
870 
871   selector_load->eraseFromParent();
872 
873   return true;
874 }
875 
876 bool IRForTarget::RewriteObjCSelectors(BasicBlock &basic_block) {
877   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
878 
879   InstrList selector_loads;
880 
881   for (Instruction &inst : basic_block) {
882     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
883       if (IsObjCSelectorRef(load->getPointerOperand()))
884         selector_loads.push_back(&inst);
885   }
886 
887   for (Instruction *inst : selector_loads) {
888     if (!RewriteObjCSelector(inst)) {
889       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
890                             "static reference to an Objective-C selector to a "
891                             "dynamic reference\n");
892 
893       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C selector");
894 
895       return false;
896     }
897   }
898 
899   return true;
900 }
901 
902 static bool IsObjCClassReference(Value *value) {
903   GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
904 
905   return !(!global_variable || !global_variable->hasName() ||
906            !global_variable->getName().startswith("OBJC_CLASS_REFERENCES_"));
907 }
908 
909 // This function does not report errors; its callers are responsible.
910 bool IRForTarget::RewriteObjCClassReference(Instruction *class_load) {
911   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
912 
913   LoadInst *load = dyn_cast<LoadInst>(class_load);
914 
915   if (!load)
916     return false;
917 
918   // Unpack the class name from the reference.  In LLVM IR, a reference to an
919   // Objective-C class gets represented as
920   //
921   // %tmp     = load %struct._objc_class*,
922   //            %struct._objc_class** @OBJC_CLASS_REFERENCES_, align 4
923   //
924   // @"OBJC_CLASS_REFERENCES_ is a bitcast of a character array called
925   // @OBJC_CLASS_NAME_. @OBJC_CLASS_NAME contains the string.
926 
927   // Find the pointer's initializer (a ConstantExpr with opcode BitCast) and
928   // get the string from its target
929 
930   GlobalVariable *_objc_class_references_ =
931       dyn_cast<GlobalVariable>(load->getPointerOperand());
932 
933   if (!_objc_class_references_ ||
934       !_objc_class_references_->hasInitializer())
935     return false;
936 
937   Constant *ocr_initializer = _objc_class_references_->getInitializer();
938 
939   ConstantExpr *ocr_initializer_expr = dyn_cast<ConstantExpr>(ocr_initializer);
940 
941   if (!ocr_initializer_expr ||
942       ocr_initializer_expr->getOpcode() != Instruction::BitCast)
943     return false;
944 
945   Value *ocr_initializer_base = ocr_initializer_expr->getOperand(0);
946 
947   if (!ocr_initializer_base)
948     return false;
949 
950   // Find the string's initializer (a ConstantArray) and get the string from it
951 
952   GlobalVariable *_objc_class_name_ =
953       dyn_cast<GlobalVariable>(ocr_initializer_base);
954 
955   if (!_objc_class_name_ || !_objc_class_name_->hasInitializer())
956     return false;
957 
958   Constant *ocn_initializer = _objc_class_name_->getInitializer();
959 
960   ConstantDataArray *ocn_initializer_array =
961       dyn_cast<ConstantDataArray>(ocn_initializer);
962 
963   if (!ocn_initializer_array->isString())
964     return false;
965 
966   std::string ocn_initializer_string =
967       std::string(ocn_initializer_array->getAsString());
968 
969   LLDB_LOG(log, "Found Objective-C class reference \"{0}\"",
970            ocn_initializer_string);
971 
972   // Construct a call to objc_getClass
973 
974   if (!m_objc_getClass) {
975     lldb::addr_t objc_getClass_addr;
976 
977     bool missing_weak = false;
978     static lldb_private::ConstString g_objc_getClass_str("objc_getClass");
979     objc_getClass_addr = m_execution_unit.FindSymbol(g_objc_getClass_str,
980                                                      missing_weak);
981     if (objc_getClass_addr == LLDB_INVALID_ADDRESS || missing_weak)
982       return false;
983 
984     LLDB_LOG(log, "Found objc_getClass at {0}", objc_getClass_addr);
985 
986     // Build the function type: %struct._objc_class *objc_getClass(i8*)
987 
988     Type *class_type = load->getType();
989     Type *type_array[1];
990     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
991 
992     ArrayRef<Type *> ogC_arg_types(type_array, 1);
993 
994     llvm::FunctionType *ogC_type =
995         FunctionType::get(class_type, ogC_arg_types, false);
996 
997     // Build the constant containing the pointer to the function
998     PointerType *ogC_ptr_ty = PointerType::getUnqual(ogC_type);
999     Constant *ogC_addr_int =
1000         ConstantInt::get(m_intptr_ty, objc_getClass_addr, false);
1001     m_objc_getClass = {ogC_type,
1002                        ConstantExpr::getIntToPtr(ogC_addr_int, ogC_ptr_ty)};
1003   }
1004 
1005   Value *argument_array[1];
1006 
1007   Constant *ocn_pointer = ConstantExpr::getBitCast(
1008       _objc_class_name_, Type::getInt8PtrTy(m_module->getContext()));
1009 
1010   argument_array[0] = ocn_pointer;
1011 
1012   ArrayRef<Value *> ogC_arguments(argument_array, 1);
1013 
1014   CallInst *ogC_call = CallInst::Create(m_objc_getClass, ogC_arguments,
1015                                         "objc_getClass", class_load);
1016 
1017   // Replace the load with the call in all users
1018 
1019   class_load->replaceAllUsesWith(ogC_call);
1020 
1021   class_load->eraseFromParent();
1022 
1023   return true;
1024 }
1025 
1026 bool IRForTarget::RewriteObjCClassReferences(BasicBlock &basic_block) {
1027   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1028 
1029   InstrList class_loads;
1030 
1031   for (Instruction &inst : basic_block) {
1032     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1033       if (IsObjCClassReference(load->getPointerOperand()))
1034         class_loads.push_back(&inst);
1035   }
1036 
1037   for (Instruction *inst : class_loads) {
1038     if (!RewriteObjCClassReference(inst)) {
1039       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
1040                             "static reference to an Objective-C class to a "
1041                             "dynamic reference\n");
1042 
1043       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C class");
1044 
1045       return false;
1046     }
1047   }
1048 
1049   return true;
1050 }
1051 
1052 // This function does not report errors; its callers are responsible.
1053 bool IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc) {
1054   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1055 
1056   AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1057 
1058   MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1059 
1060   if (!alloc_md || !alloc_md->getNumOperands())
1061     return false;
1062 
1063   ConstantInt *constant_int =
1064       mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
1065 
1066   if (!constant_int)
1067     return false;
1068 
1069   // We attempt to register this as a new persistent variable with the DeclMap.
1070 
1071   uintptr_t ptr = constant_int->getZExtValue();
1072 
1073   clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
1074 
1075   lldb_private::TypeFromParser result_decl_type(
1076       m_decl_map->GetTypeSystem()->GetType(decl->getType()));
1077 
1078   StringRef decl_name(decl->getName());
1079   lldb_private::ConstString persistent_variable_name(decl_name.data(),
1080                                                      decl_name.size());
1081   if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name,
1082                                          result_decl_type, false, false))
1083     return false;
1084 
1085   GlobalVariable *persistent_global = new GlobalVariable(
1086       (*m_module), alloc->getType(), false,  /* not constant */
1087       GlobalValue::ExternalLinkage, nullptr, /* no initializer */
1088       alloc->getName().str());
1089 
1090   // What we're going to do here is make believe this was a regular old
1091   // external variable.  That means we need to make the metadata valid.
1092 
1093   NamedMDNode *named_metadata =
1094       m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
1095 
1096   llvm::Metadata *values[2];
1097   values[0] = ConstantAsMetadata::get(persistent_global);
1098   values[1] = ConstantAsMetadata::get(constant_int);
1099 
1100   ArrayRef<llvm::Metadata *> value_ref(values, 2);
1101 
1102   MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
1103   named_metadata->addOperand(persistent_global_md);
1104 
1105   // Now, since the variable is a pointer variable, we will drop in a load of
1106   // that pointer variable.
1107 
1108   LoadInst *persistent_load =
1109       new LoadInst(persistent_global->getType()->getPointerElementType(),
1110                    persistent_global, "", alloc);
1111 
1112   LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(alloc),
1113            PrintValue(persistent_load));
1114 
1115   alloc->replaceAllUsesWith(persistent_load);
1116   alloc->eraseFromParent();
1117 
1118   return true;
1119 }
1120 
1121 bool IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block) {
1122   if (!m_resolve_vars)
1123     return true;
1124 
1125   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1126 
1127   InstrList pvar_allocs;
1128 
1129   for (Instruction &inst : basic_block) {
1130 
1131     if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst)) {
1132       llvm::StringRef alloc_name = alloc->getName();
1133 
1134       if (alloc_name.startswith("$") && !alloc_name.startswith("$__lldb")) {
1135         if (alloc_name.find_first_of("0123456789") == 1) {
1136           LLDB_LOG(log, "Rejecting a numeric persistent variable.");
1137 
1138           m_error_stream.Printf("Error [IRForTarget]: Names starting with $0, "
1139                                 "$1, ... are reserved for use as result "
1140                                 "names\n");
1141 
1142           return false;
1143         }
1144 
1145         pvar_allocs.push_back(alloc);
1146       }
1147     }
1148   }
1149 
1150   for (Instruction *inst : pvar_allocs) {
1151     if (!RewritePersistentAlloc(inst)) {
1152       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1153                             "the creation of a persistent variable\n");
1154 
1155       LLDB_LOG(log, "Couldn't rewrite the creation of a persistent variable");
1156 
1157       return false;
1158     }
1159   }
1160 
1161   return true;
1162 }
1163 
1164 // This function does not report errors; its callers are responsible.
1165 bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) {
1166   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1167 
1168   LLDB_LOG(log, "MaybeHandleVariable ({0})", PrintValue(llvm_value_ptr));
1169 
1170   if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr)) {
1171     switch (constant_expr->getOpcode()) {
1172     default:
1173       break;
1174     case Instruction::GetElementPtr:
1175     case Instruction::BitCast:
1176       Value *s = constant_expr->getOperand(0);
1177       if (!MaybeHandleVariable(s))
1178         return false;
1179     }
1180   } else if (GlobalVariable *global_variable =
1181                  dyn_cast<GlobalVariable>(llvm_value_ptr)) {
1182     if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1183       return true;
1184 
1185     clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
1186 
1187     if (!named_decl) {
1188       if (IsObjCSelectorRef(llvm_value_ptr))
1189         return true;
1190 
1191       if (!global_variable->hasExternalLinkage())
1192         return true;
1193 
1194       LLDB_LOG(log, "Found global variable \"{0}\" without metadata",
1195                global_variable->getName());
1196 
1197       return false;
1198     }
1199 
1200     llvm::StringRef name(named_decl->getName());
1201 
1202     clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1203     if (value_decl == nullptr)
1204       return false;
1205 
1206     lldb_private::CompilerType compiler_type =
1207         m_decl_map->GetTypeSystem()->GetType(value_decl->getType());
1208 
1209     const Type *value_type = nullptr;
1210 
1211     if (name.startswith("$")) {
1212       // The $__lldb_expr_result name indicates the return value has allocated
1213       // as a static variable.  Per the comment at
1214       // ASTResultSynthesizer::SynthesizeBodyResult, accesses to this static
1215       // variable need to be redirected to the result of dereferencing a
1216       // pointer that is passed in as one of the arguments.
1217       //
1218       // Consequently, when reporting the size of the type, we report a pointer
1219       // type pointing to the type of $__lldb_expr_result, not the type itself.
1220       //
1221       // We also do this for any user-declared persistent variables.
1222       compiler_type = compiler_type.GetPointerType();
1223       value_type = PointerType::get(global_variable->getType(), 0);
1224     } else {
1225       value_type = global_variable->getType();
1226     }
1227 
1228     auto *target = m_execution_unit.GetTarget().get();
1229     llvm::Optional<uint64_t> value_size = compiler_type.GetByteSize(target);
1230     if (!value_size)
1231       return false;
1232     llvm::Optional<size_t> opt_alignment =
1233         compiler_type.GetTypeBitAlign(target);
1234     if (!opt_alignment)
1235       return false;
1236     lldb::offset_t value_alignment = (*opt_alignment + 7ull) / 8ull;
1237 
1238     LLDB_LOG(log,
1239              "Type of \"{0}\" is [clang \"{1}\", llvm \"{2}\"] [size {3}, "
1240              "align {4}]",
1241              name,
1242              lldb_private::ClangUtil::GetQualType(compiler_type).getAsString(),
1243              PrintType(value_type), *value_size, value_alignment);
1244 
1245     if (named_decl)
1246       m_decl_map->AddValueToStruct(named_decl, lldb_private::ConstString(name),
1247                                    llvm_value_ptr, *value_size,
1248                                    value_alignment);
1249   } else if (isa<llvm::Function>(llvm_value_ptr)) {
1250     LLDB_LOG(log, "Function pointers aren't handled right now");
1251 
1252     return false;
1253   }
1254 
1255   return true;
1256 }
1257 
1258 // This function does not report errors; its callers are responsible.
1259 bool IRForTarget::HandleSymbol(Value *symbol) {
1260   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1261 
1262   lldb_private::ConstString name(symbol->getName().str().c_str());
1263 
1264   lldb::addr_t symbol_addr =
1265       m_decl_map->GetSymbolAddress(name, lldb::eSymbolTypeAny);
1266 
1267   if (symbol_addr == LLDB_INVALID_ADDRESS) {
1268     LLDB_LOG(log, "Symbol \"{0}\" had no address", name);
1269 
1270     return false;
1271   }
1272 
1273   LLDB_LOG(log, "Found \"{0}\" at {1}", name, symbol_addr);
1274 
1275   Type *symbol_type = symbol->getType();
1276 
1277   Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
1278 
1279   Value *symbol_addr_ptr =
1280       ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1281 
1282   LLDB_LOG(log, "Replacing {0} with {1}", PrintValue(symbol),
1283            PrintValue(symbol_addr_ptr));
1284 
1285   symbol->replaceAllUsesWith(symbol_addr_ptr);
1286 
1287   return true;
1288 }
1289 
1290 bool IRForTarget::MaybeHandleCallArguments(CallInst *Old) {
1291   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1292 
1293   LLDB_LOG(log, "MaybeHandleCallArguments({0})", PrintValue(Old));
1294 
1295   for (unsigned op_index = 0, num_ops = Old->arg_size();
1296        op_index < num_ops; ++op_index)
1297     // conservatively believe that this is a store
1298     if (!MaybeHandleVariable(Old->getArgOperand(op_index))) {
1299       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1300                             "one of the arguments of a function call.\n");
1301 
1302       return false;
1303     }
1304 
1305   return true;
1306 }
1307 
1308 bool IRForTarget::HandleObjCClass(Value *classlist_reference) {
1309   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1310 
1311   GlobalVariable *global_variable =
1312       dyn_cast<GlobalVariable>(classlist_reference);
1313 
1314   if (!global_variable)
1315     return false;
1316 
1317   Constant *initializer = global_variable->getInitializer();
1318 
1319   if (!initializer)
1320     return false;
1321 
1322   if (!initializer->hasName())
1323     return false;
1324 
1325   StringRef name(initializer->getName());
1326   lldb_private::ConstString name_cstr(name.str().c_str());
1327   lldb::addr_t class_ptr =
1328       m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
1329 
1330   LLDB_LOG(log, "Found reference to Objective-C class {0} ({1})", name,
1331            (unsigned long long)class_ptr);
1332 
1333   if (class_ptr == LLDB_INVALID_ADDRESS)
1334     return false;
1335 
1336   if (global_variable->use_empty())
1337     return false;
1338 
1339   SmallVector<LoadInst *, 2> load_instructions;
1340 
1341   for (llvm::User *u : global_variable->users()) {
1342     if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
1343       load_instructions.push_back(load_instruction);
1344   }
1345 
1346   if (load_instructions.empty())
1347     return false;
1348 
1349   Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
1350 
1351   for (LoadInst *load_instruction : load_instructions) {
1352     Constant *class_bitcast =
1353         ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1354 
1355     load_instruction->replaceAllUsesWith(class_bitcast);
1356 
1357     load_instruction->eraseFromParent();
1358   }
1359 
1360   return true;
1361 }
1362 
1363 bool IRForTarget::RemoveCXAAtExit(BasicBlock &basic_block) {
1364   std::vector<CallInst *> calls_to_remove;
1365 
1366   for (Instruction &inst : basic_block) {
1367     CallInst *call = dyn_cast<CallInst>(&inst);
1368 
1369     // MaybeHandleCallArguments handles error reporting; we are silent here
1370     if (!call)
1371       continue;
1372 
1373     bool remove = false;
1374 
1375     llvm::Function *func = call->getCalledFunction();
1376 
1377     if (func && func->getName() == "__cxa_atexit")
1378       remove = true;
1379 
1380     llvm::Value *val = call->getCalledOperand();
1381 
1382     if (val && val->getName() == "__cxa_atexit")
1383       remove = true;
1384 
1385     if (remove)
1386       calls_to_remove.push_back(call);
1387   }
1388 
1389   for (CallInst *ci : calls_to_remove)
1390     ci->eraseFromParent();
1391 
1392   return true;
1393 }
1394 
1395 bool IRForTarget::ResolveCalls(BasicBlock &basic_block) {
1396   // Prepare the current basic block for execution in the remote process
1397 
1398   for (Instruction &inst : basic_block) {
1399     CallInst *call = dyn_cast<CallInst>(&inst);
1400 
1401     // MaybeHandleCallArguments handles error reporting; we are silent here
1402     if (call && !MaybeHandleCallArguments(call))
1403       return false;
1404   }
1405 
1406   return true;
1407 }
1408 
1409 bool IRForTarget::ResolveExternals(Function &llvm_function) {
1410   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1411 
1412   for (GlobalVariable &global_var : m_module->globals()) {
1413     llvm::StringRef global_name = global_var.getName();
1414 
1415     LLDB_LOG(log, "Examining {0}, DeclForGlobalValue returns {1}", global_name,
1416              static_cast<void *>(DeclForGlobal(&global_var)));
1417 
1418     if (global_name.startswith("OBJC_IVAR")) {
1419       if (!HandleSymbol(&global_var)) {
1420         m_error_stream.Format("Error [IRForTarget]: Couldn't find Objective-C "
1421                               "indirect ivar symbol {0}\n",
1422                               global_name);
1423 
1424         return false;
1425       }
1426     } else if (global_name.contains("OBJC_CLASSLIST_REFERENCES_$")) {
1427       if (!HandleObjCClass(&global_var)) {
1428         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1429                               "for an Objective-C static method call\n");
1430 
1431         return false;
1432       }
1433     } else if (global_name.contains("OBJC_CLASSLIST_SUP_REFS_$")) {
1434       if (!HandleObjCClass(&global_var)) {
1435         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1436                               "for an Objective-C static method call\n");
1437 
1438         return false;
1439       }
1440     } else if (DeclForGlobal(&global_var)) {
1441       if (!MaybeHandleVariable(&global_var)) {
1442         m_error_stream.Format("Internal error [IRForTarget]: Couldn't rewrite "
1443                               "external variable {0}\n",
1444                               global_name);
1445 
1446         return false;
1447       }
1448     }
1449   }
1450 
1451   return true;
1452 }
1453 
1454 static bool isGuardVariableRef(Value *V) {
1455   Constant *Old = dyn_cast<Constant>(V);
1456 
1457   if (!Old)
1458     return false;
1459 
1460   if (auto CE = dyn_cast<ConstantExpr>(V)) {
1461     if (CE->getOpcode() != Instruction::BitCast)
1462       return false;
1463 
1464     Old = CE->getOperand(0);
1465   }
1466 
1467   GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
1468 
1469   if (!GV || !GV->hasName() || !isGuardVariableSymbol(GV->getName()))
1470     return false;
1471 
1472   return true;
1473 }
1474 
1475 void IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction *guard_load) {
1476   Constant *zero(Constant::getNullValue(guard_load->getType()));
1477   guard_load->replaceAllUsesWith(zero);
1478   guard_load->eraseFromParent();
1479 }
1480 
1481 static void ExciseGuardStore(Instruction *guard_store) {
1482   guard_store->eraseFromParent();
1483 }
1484 
1485 bool IRForTarget::RemoveGuards(BasicBlock &basic_block) {
1486   // Eliminate any reference to guard variables found.
1487 
1488   InstrList guard_loads;
1489   InstrList guard_stores;
1490 
1491   for (Instruction &inst : basic_block) {
1492 
1493     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1494       if (isGuardVariableRef(load->getPointerOperand()))
1495         guard_loads.push_back(&inst);
1496 
1497     if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1498       if (isGuardVariableRef(store->getPointerOperand()))
1499         guard_stores.push_back(&inst);
1500   }
1501 
1502   for (Instruction *inst : guard_loads)
1503     TurnGuardLoadIntoZero(inst);
1504 
1505   for (Instruction *inst : guard_stores)
1506     ExciseGuardStore(inst);
1507 
1508   return true;
1509 }
1510 
1511 // This function does not report errors; its callers are responsible.
1512 bool IRForTarget::UnfoldConstant(Constant *old_constant,
1513                                  llvm::Function *llvm_function,
1514                                  FunctionValueCache &value_maker,
1515                                  FunctionValueCache &entry_instruction_finder,
1516                                  lldb_private::Stream &error_stream) {
1517   SmallVector<User *, 16> users;
1518 
1519   // We do this because the use list might change, invalidating our iterator.
1520   // Much better to keep a work list ourselves.
1521   for (llvm::User *u : old_constant->users())
1522     users.push_back(u);
1523 
1524   for (size_t i = 0; i < users.size(); ++i) {
1525     User *user = users[i];
1526 
1527     if (Constant *constant = dyn_cast<Constant>(user)) {
1528       // synthesize a new non-constant equivalent of the constant
1529 
1530       if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
1531         switch (constant_expr->getOpcode()) {
1532         default:
1533           error_stream.Printf("error [IRForTarget internal]: Unhandled "
1534                               "constant expression type: \"%s\"",
1535                               PrintValue(constant_expr).c_str());
1536           return false;
1537         case Instruction::BitCast: {
1538           FunctionValueCache bit_cast_maker(
1539               [&value_maker, &entry_instruction_finder, old_constant,
1540                constant_expr](llvm::Function *function) -> llvm::Value * {
1541                 // UnaryExpr
1542                 //   OperandList[0] is value
1543 
1544                 if (constant_expr->getOperand(0) != old_constant)
1545                   return constant_expr;
1546 
1547                 return new BitCastInst(
1548                     value_maker.GetValue(function), constant_expr->getType(),
1549                     "", llvm::cast<Instruction>(
1550                             entry_instruction_finder.GetValue(function)));
1551               });
1552 
1553           if (!UnfoldConstant(constant_expr, llvm_function, bit_cast_maker,
1554                               entry_instruction_finder, error_stream))
1555             return false;
1556         } break;
1557         case Instruction::GetElementPtr: {
1558           // GetElementPtrConstantExpr
1559           //   OperandList[0] is base
1560           //   OperandList[1]... are indices
1561 
1562           FunctionValueCache get_element_pointer_maker(
1563               [&value_maker, &entry_instruction_finder, old_constant,
1564                constant_expr](llvm::Function *function) -> llvm::Value * {
1565                 auto *gep = cast<llvm::GEPOperator>(constant_expr);
1566                 Value *ptr = gep->getPointerOperand();
1567 
1568                 if (ptr == old_constant)
1569                   ptr = value_maker.GetValue(function);
1570 
1571                 std::vector<Value *> index_vector;
1572                 for (Value *operand : gep->indices()) {
1573                   if (operand == old_constant)
1574                     operand = value_maker.GetValue(function);
1575 
1576                   index_vector.push_back(operand);
1577                 }
1578 
1579                 ArrayRef<Value *> indices(index_vector);
1580 
1581                 return GetElementPtrInst::Create(
1582                     gep->getSourceElementType(), ptr, indices, "",
1583                     llvm::cast<Instruction>(
1584                         entry_instruction_finder.GetValue(function)));
1585               });
1586 
1587           if (!UnfoldConstant(constant_expr, llvm_function,
1588                               get_element_pointer_maker,
1589                               entry_instruction_finder, error_stream))
1590             return false;
1591         } break;
1592         }
1593       } else {
1594         error_stream.Printf(
1595             "error [IRForTarget internal]: Unhandled constant type: \"%s\"",
1596             PrintValue(constant).c_str());
1597         return false;
1598       }
1599     } else {
1600       if (Instruction *inst = llvm::dyn_cast<Instruction>(user)) {
1601         if (llvm_function && inst->getParent()->getParent() != llvm_function) {
1602           error_stream.PutCString("error: Capturing non-local variables in "
1603                                   "expressions is unsupported.\n");
1604           return false;
1605         }
1606         inst->replaceUsesOfWith(
1607             old_constant, value_maker.GetValue(inst->getParent()->getParent()));
1608       } else {
1609         error_stream.Printf(
1610             "error [IRForTarget internal]: Unhandled non-constant type: \"%s\"",
1611             PrintValue(user).c_str());
1612         return false;
1613       }
1614     }
1615   }
1616 
1617   if (!isa<GlobalValue>(old_constant)) {
1618     old_constant->destroyConstant();
1619   }
1620 
1621   return true;
1622 }
1623 
1624 bool IRForTarget::ReplaceVariables(Function &llvm_function) {
1625   if (!m_resolve_vars)
1626     return true;
1627 
1628   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1629 
1630   m_decl_map->DoStructLayout();
1631 
1632   LLDB_LOG(log, "Element arrangement:");
1633 
1634   uint32_t num_elements;
1635   uint32_t element_index;
1636 
1637   size_t size;
1638   lldb::offset_t alignment;
1639 
1640   if (!m_decl_map->GetStructInfo(num_elements, size, alignment))
1641     return false;
1642 
1643   Function::arg_iterator iter(llvm_function.arg_begin());
1644 
1645   if (iter == llvm_function.arg_end()) {
1646     m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes no "
1647                           "arguments (should take at least a struct pointer)");
1648 
1649     return false;
1650   }
1651 
1652   Argument *argument = &*iter;
1653 
1654   if (argument->getName().equals("this")) {
1655     ++iter;
1656 
1657     if (iter == llvm_function.arg_end()) {
1658       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1659                             "'this' argument (should take a struct pointer "
1660                             "too)");
1661 
1662       return false;
1663     }
1664 
1665     argument = &*iter;
1666   } else if (argument->getName().equals("self")) {
1667     ++iter;
1668 
1669     if (iter == llvm_function.arg_end()) {
1670       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1671                             "'self' argument (should take '_cmd' and a struct "
1672                             "pointer too)");
1673 
1674       return false;
1675     }
1676 
1677     if (!iter->getName().equals("_cmd")) {
1678       m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes '{0}' "
1679                             "after 'self' argument (should take '_cmd')",
1680                             iter->getName());
1681 
1682       return false;
1683     }
1684 
1685     ++iter;
1686 
1687     if (iter == llvm_function.arg_end()) {
1688       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1689                             "'self' and '_cmd' arguments (should take a struct "
1690                             "pointer too)");
1691 
1692       return false;
1693     }
1694 
1695     argument = &*iter;
1696   }
1697 
1698   if (!argument->getName().equals("$__lldb_arg")) {
1699     m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes an "
1700                           "argument named '{0}' instead of the struct pointer",
1701                           argument->getName());
1702 
1703     return false;
1704   }
1705 
1706   LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument));
1707 
1708   BasicBlock &entry_block(llvm_function.getEntryBlock());
1709   Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
1710 
1711   if (!FirstEntryInstruction) {
1712     m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "
1713                           "first instruction in the wrapper for use in "
1714                           "rewriting");
1715 
1716     return false;
1717   }
1718 
1719   LLVMContext &context(m_module->getContext());
1720   IntegerType *offset_type(Type::getInt32Ty(context));
1721 
1722   if (!offset_type) {
1723     m_error_stream.Printf(
1724         "Internal error [IRForTarget]: Couldn't produce an offset type");
1725 
1726     return false;
1727   }
1728 
1729   for (element_index = 0; element_index < num_elements; ++element_index) {
1730     const clang::NamedDecl *decl = nullptr;
1731     Value *value = nullptr;
1732     lldb::offset_t offset;
1733     lldb_private::ConstString name;
1734 
1735     if (!m_decl_map->GetStructElement(decl, value, offset, name,
1736                                       element_index)) {
1737       m_error_stream.Printf(
1738           "Internal error [IRForTarget]: Structure information is incomplete");
1739 
1740       return false;
1741     }
1742 
1743     LLDB_LOG(log, "  \"{0}\" (\"{1}\") placed at {2}", name,
1744              decl->getNameAsString(), offset);
1745 
1746     if (value) {
1747       LLDB_LOG(log, "    Replacing [{0}]", PrintValue(value));
1748 
1749       FunctionValueCache body_result_maker(
1750           [this, name, offset_type, offset, argument,
1751            value](llvm::Function *function) -> llvm::Value * {
1752             // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1753             // in cases where the result variable is an rvalue, we have to
1754             // synthesize a dereference of the appropriate structure entry in
1755             // order to produce the static variable that the AST thinks it is
1756             // accessing.
1757 
1758             llvm::Instruction *entry_instruction = llvm::cast<Instruction>(
1759                 m_entry_instruction_finder.GetValue(function));
1760 
1761             ConstantInt *offset_int(
1762                 ConstantInt::get(offset_type, offset, true));
1763             GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(
1764                 argument->getType()->getPointerElementType(), argument,
1765                 offset_int, "", entry_instruction);
1766 
1767             if (name == m_result_name && !m_result_is_pointer) {
1768               BitCastInst *bit_cast = new BitCastInst(
1769                   get_element_ptr, value->getType()->getPointerTo(), "",
1770                   entry_instruction);
1771 
1772               LoadInst *load =
1773                   new LoadInst(bit_cast->getType()->getPointerElementType(),
1774                                bit_cast, "", entry_instruction);
1775 
1776               return load;
1777             } else {
1778               BitCastInst *bit_cast = new BitCastInst(
1779                   get_element_ptr, value->getType(), "", entry_instruction);
1780 
1781               return bit_cast;
1782             }
1783           });
1784 
1785       if (Constant *constant = dyn_cast<Constant>(value)) {
1786         if (!UnfoldConstant(constant, &llvm_function, body_result_maker,
1787                             m_entry_instruction_finder, m_error_stream)) {
1788           return false;
1789         }
1790       } else if (Instruction *instruction = dyn_cast<Instruction>(value)) {
1791         if (instruction->getParent()->getParent() != &llvm_function) {
1792           m_error_stream.PutCString("error: Capturing non-local variables in "
1793                                     "expressions is unsupported.\n");
1794           return false;
1795         }
1796         value->replaceAllUsesWith(
1797             body_result_maker.GetValue(instruction->getParent()->getParent()));
1798       } else {
1799         LLDB_LOG(log, "Unhandled non-constant type: \"{0}\"",
1800                  PrintValue(value));
1801         return false;
1802       }
1803 
1804       if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1805         var->eraseFromParent();
1806     }
1807   }
1808 
1809   LLDB_LOG(log, "Total structure [align {0}, size {1}]", (int64_t)alignment,
1810            (uint64_t)size);
1811 
1812   return true;
1813 }
1814 
1815 bool IRForTarget::runOnModule(Module &llvm_module) {
1816   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1817 
1818   m_module = &llvm_module;
1819   m_target_data = std::make_unique<DataLayout>(m_module);
1820   m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(),
1821                                       m_target_data->getPointerSizeInBits());
1822 
1823   if (log) {
1824     std::string s;
1825     raw_string_ostream oss(s);
1826 
1827     m_module->print(oss, nullptr);
1828 
1829     oss.flush();
1830 
1831     LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s);
1832   }
1833 
1834   Function *const main_function =
1835       m_func_name.IsEmpty() ? nullptr
1836                             : m_module->getFunction(m_func_name.GetStringRef());
1837 
1838   if (!m_func_name.IsEmpty() && !main_function) {
1839     LLDB_LOG(log, "Couldn't find \"{0}()\" in the module", m_func_name);
1840 
1841     m_error_stream.Format("Internal error [IRForTarget]: Couldn't find wrapper "
1842                           "'{0}' in the module",
1843                           m_func_name);
1844 
1845     return false;
1846   }
1847 
1848   if (main_function) {
1849     if (!FixFunctionLinkage(*main_function)) {
1850       LLDB_LOG(log, "Couldn't fix the linkage for the function");
1851 
1852       return false;
1853     }
1854   }
1855 
1856   llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
1857 
1858   m_reloc_placeholder = new llvm::GlobalVariable(
1859       (*m_module), int8_ty, false /* IsConstant */,
1860       GlobalVariable::InternalLinkage, Constant::getNullValue(int8_ty),
1861       "reloc_placeholder", nullptr /* InsertBefore */,
1862       GlobalVariable::NotThreadLocal /* ThreadLocal */, 0 /* AddressSpace */);
1863 
1864   ////////////////////////////////////////////////////////////
1865   // Replace $__lldb_expr_result with a persistent variable
1866   //
1867 
1868   if (main_function) {
1869     if (!CreateResultVariable(*main_function)) {
1870       LLDB_LOG(log, "CreateResultVariable() failed");
1871 
1872       // CreateResultVariable() reports its own errors, so we don't do so here
1873 
1874       return false;
1875     }
1876   }
1877 
1878   if (log && log->GetVerbose()) {
1879     std::string s;
1880     raw_string_ostream oss(s);
1881 
1882     m_module->print(oss, nullptr);
1883 
1884     oss.flush();
1885 
1886     LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s);
1887   }
1888 
1889   for (llvm::Function &function : *m_module) {
1890     for (BasicBlock &bb : function) {
1891       if (!RemoveGuards(bb)) {
1892         LLDB_LOG(log, "RemoveGuards() failed");
1893 
1894         // RemoveGuards() reports its own errors, so we don't do so here
1895 
1896         return false;
1897       }
1898 
1899       if (!RewritePersistentAllocs(bb)) {
1900         LLDB_LOG(log, "RewritePersistentAllocs() failed");
1901 
1902         // RewritePersistentAllocs() reports its own errors, so we don't do so
1903         // here
1904 
1905         return false;
1906       }
1907 
1908       if (!RemoveCXAAtExit(bb)) {
1909         LLDB_LOG(log, "RemoveCXAAtExit() failed");
1910 
1911         // RemoveCXAAtExit() reports its own errors, so we don't do so here
1912 
1913         return false;
1914       }
1915     }
1916   }
1917 
1918   ///////////////////////////////////////////////////////////////////////////////
1919   // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
1920   //
1921 
1922   if (!RewriteObjCConstStrings()) {
1923     LLDB_LOG(log, "RewriteObjCConstStrings() failed");
1924 
1925     // RewriteObjCConstStrings() reports its own errors, so we don't do so here
1926 
1927     return false;
1928   }
1929 
1930   for (llvm::Function &function : *m_module) {
1931     for (llvm::BasicBlock &bb : function) {
1932       if (!RewriteObjCSelectors(bb)) {
1933         LLDB_LOG(log, "RewriteObjCSelectors() failed");
1934 
1935         // RewriteObjCSelectors() reports its own errors, so we don't do so
1936         // here
1937 
1938         return false;
1939       }
1940 
1941       if (!RewriteObjCClassReferences(bb)) {
1942         LLDB_LOG(log, "RewriteObjCClassReferences() failed");
1943 
1944         // RewriteObjCClasses() reports its own errors, so we don't do so here
1945 
1946         return false;
1947       }
1948     }
1949   }
1950 
1951   for (llvm::Function &function : *m_module) {
1952     for (BasicBlock &bb : function) {
1953       if (!ResolveCalls(bb)) {
1954         LLDB_LOG(log, "ResolveCalls() failed");
1955 
1956         // ResolveCalls() reports its own errors, so we don't do so here
1957 
1958         return false;
1959       }
1960     }
1961   }
1962 
1963   ////////////////////////////////////////////////////////////////////////
1964   // Run function-level passes that only make sense on the main function
1965   //
1966 
1967   if (main_function) {
1968     if (!ResolveExternals(*main_function)) {
1969       LLDB_LOG(log, "ResolveExternals() failed");
1970 
1971       // ResolveExternals() reports its own errors, so we don't do so here
1972 
1973       return false;
1974     }
1975 
1976     if (!ReplaceVariables(*main_function)) {
1977       LLDB_LOG(log, "ReplaceVariables() failed");
1978 
1979       // ReplaceVariables() reports its own errors, so we don't do so here
1980 
1981       return false;
1982     }
1983   }
1984 
1985   if (log && log->GetVerbose()) {
1986     std::string s;
1987     raw_string_ostream oss(s);
1988 
1989     m_module->print(oss, nullptr);
1990 
1991     oss.flush();
1992 
1993     LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s);
1994   }
1995 
1996   return true;
1997 }
1998