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