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 
44 using namespace llvm;
45 using lldb_private::LLDBLog;
46 
47 typedef SmallVector<Instruction *, 2> InstrList;
48 
49 IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker)
50     : m_maker(maker), m_values() {}
51 
52 IRForTarget::FunctionValueCache::~FunctionValueCache() = default;
53 
54 llvm::Value *
55 IRForTarget::FunctionValueCache::GetValue(llvm::Function *function) {
56   if (!m_values.count(function)) {
57     llvm::Value *ret = m_maker(function);
58     m_values[function] = ret;
59     return ret;
60   }
61   return m_values[function];
62 }
63 
64 static llvm::Value *FindEntryInstruction(llvm::Function *function) {
65   if (function->empty())
66     return nullptr;
67 
68   return function->getEntryBlock().getFirstNonPHIOrDbg();
69 }
70 
71 IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
72                          bool resolve_vars,
73                          lldb_private::IRExecutionUnit &execution_unit,
74                          lldb_private::Stream &error_stream,
75                          const char *func_name)
76     : m_resolve_vars(resolve_vars), m_func_name(func_name),
77       m_decl_map(decl_map), m_error_stream(error_stream),
78       m_execution_unit(execution_unit),
79       m_entry_instruction_finder(FindEntryInstruction) {}
80 
81 /* Handy utility functions used at several places in the code */
82 
83 static std::string PrintValue(const Value *value, bool truncate = false) {
84   std::string s;
85   if (value) {
86     raw_string_ostream rso(s);
87     value->print(rso);
88     rso.flush();
89     if (truncate)
90       s.resize(s.length() - 1);
91   }
92   return s;
93 }
94 
95 static std::string PrintType(const llvm::Type *type, bool truncate = false) {
96   std::string s;
97   raw_string_ostream rso(s);
98   type->print(rso);
99   rso.flush();
100   if (truncate)
101     s.resize(s.length() - 1);
102   return s;
103 }
104 
105 bool IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function) {
106   llvm_function.setLinkage(GlobalValue::ExternalLinkage);
107 
108   return true;
109 }
110 
111 clang::NamedDecl *IRForTarget::DeclForGlobal(const GlobalValue *global_val,
112                                              Module *module) {
113   NamedMDNode *named_metadata =
114       module->getNamedMetadata("clang.global.decl.ptrs");
115 
116   if (!named_metadata)
117     return nullptr;
118 
119   unsigned num_nodes = named_metadata->getNumOperands();
120   unsigned node_index;
121 
122   for (node_index = 0; node_index < num_nodes; ++node_index) {
123     llvm::MDNode *metadata_node =
124         dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
125     if (!metadata_node)
126       return nullptr;
127 
128     if (metadata_node->getNumOperands() != 2)
129       continue;
130 
131     if (mdconst::dyn_extract_or_null<GlobalValue>(
132             metadata_node->getOperand(0)) != global_val)
133       continue;
134 
135     ConstantInt *constant_int =
136         mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
137 
138     if (!constant_int)
139       return nullptr;
140 
141     uintptr_t ptr = constant_int->getZExtValue();
142 
143     return reinterpret_cast<clang::NamedDecl *>(ptr);
144   }
145 
146   return nullptr;
147 }
148 
149 clang::NamedDecl *IRForTarget::DeclForGlobal(GlobalValue *global_val) {
150   return DeclForGlobal(global_val, m_module);
151 }
152 
153 /// Returns true iff the mangled symbol is for a static guard variable.
154 static bool isGuardVariableSymbol(llvm::StringRef mangled_symbol,
155                                   bool check_ms_abi = true) {
156   bool result = mangled_symbol.startswith("_ZGV"); // Itanium ABI guard variable
157   if (check_ms_abi)
158     result |= mangled_symbol.endswith("@4IA"); // Microsoft ABI
159   return result;
160 }
161 
162 bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
163   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
164 
165   if (!m_resolve_vars)
166     return true;
167 
168   // Find the result variable.  If it doesn't exist, we can give up right here.
169 
170   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
171 
172   llvm::StringRef result_name;
173   bool found_result = false;
174 
175   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
176     result_name = value_symbol.first();
177 
178     // Check if this is a guard variable. It seems this causes some hiccups
179     // on Windows, so let's only check for Itanium guard variables.
180     bool is_guard_var = isGuardVariableSymbol(result_name, /*MS ABI*/ false);
181 
182     if (result_name.contains("$__lldb_expr_result_ptr") && !is_guard_var) {
183       found_result = true;
184       m_result_is_pointer = true;
185       break;
186     }
187 
188     if (result_name.contains("$__lldb_expr_result") && !is_guard_var) {
189       found_result = true;
190       m_result_is_pointer = false;
191       break;
192     }
193   }
194 
195   if (!found_result) {
196     LLDB_LOG(log, "Couldn't find result variable");
197 
198     return true;
199   }
200 
201   LLDB_LOG(log, "Result name: \"{0}\"", result_name);
202 
203   Value *result_value = m_module->getNamedValue(result_name);
204 
205   if (!result_value) {
206     LLDB_LOG(log, "Result variable had no data");
207 
208     m_error_stream.Format("Internal error [IRForTarget]: Result variable's "
209                           "name ({0}) exists, but not its definition\n",
210                           result_name);
211 
212     return false;
213   }
214 
215   LLDB_LOG(log, "Found result in the IR: \"{0}\"",
216            PrintValue(result_value, false));
217 
218   GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
219 
220   if (!result_global) {
221     LLDB_LOG(log, "Result variable isn't a GlobalVariable");
222 
223     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
224                           "is defined, but is not a global variable\n",
225                           result_name);
226 
227     return false;
228   }
229 
230   clang::NamedDecl *result_decl = DeclForGlobal(result_global);
231   if (!result_decl) {
232     LLDB_LOG(log, "Result variable doesn't have a corresponding Decl");
233 
234     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
235                           "does not have a corresponding Clang entity\n",
236                           result_name);
237 
238     return false;
239   }
240 
241   if (log) {
242     std::string decl_desc_str;
243     raw_string_ostream decl_desc_stream(decl_desc_str);
244     result_decl->print(decl_desc_stream);
245     decl_desc_stream.flush();
246 
247     LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str);
248   }
249 
250   clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
251   if (!result_var) {
252     LLDB_LOG(log, "Result variable Decl isn't a VarDecl");
253 
254     m_error_stream.Format("Internal error [IRForTarget]: Result variable "
255                           "({0})'s corresponding Clang entity isn't a "
256                           "variable\n",
257                           result_name);
258 
259     return false;
260   }
261 
262   // Get the next available result name from m_decl_map and create the
263   // persistent variable for it
264 
265   // If the result is an Lvalue, it is emitted as a pointer; see
266   // ASTResultSynthesizer::SynthesizeBodyResult.
267   if (m_result_is_pointer) {
268     clang::QualType pointer_qual_type = result_var->getType();
269     const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
270 
271     const clang::PointerType *pointer_pointertype =
272         pointer_type->getAs<clang::PointerType>();
273     const clang::ObjCObjectPointerType *pointer_objcobjpointertype =
274         pointer_type->getAs<clang::ObjCObjectPointerType>();
275 
276     if (pointer_pointertype) {
277       clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
278 
279       m_result_type = lldb_private::TypeFromParser(
280           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
281     } else if (pointer_objcobjpointertype) {
282       clang::QualType element_qual_type =
283           clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
284 
285       m_result_type = lldb_private::TypeFromParser(
286           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
287     } else {
288       LLDB_LOG(log, "Expected result to have pointer type, but it did not");
289 
290       m_error_stream.Format("Internal error [IRForTarget]: Lvalue result ({0}) "
291                             "is not a pointer variable\n",
292                             result_name);
293 
294       return false;
295     }
296   } else {
297     m_result_type = lldb_private::TypeFromParser(
298         m_decl_map->GetTypeSystem()->GetType(result_var->getType()));
299   }
300 
301   lldb::TargetSP target_sp(m_execution_unit.GetTarget());
302   llvm::Optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get());
303   if (!bit_size) {
304     lldb_private::StreamString type_desc_stream;
305     m_result_type.DumpTypeDescription(&type_desc_stream);
306 
307     LLDB_LOG(log, "Result type has unknown size");
308 
309     m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' "
310                           "couldn't be determined\n",
311                           type_desc_stream.GetData());
312     return false;
313   }
314 
315   if (log) {
316     lldb_private::StreamString type_desc_stream;
317     m_result_type.DumpTypeDescription(&type_desc_stream);
318 
319     LLDB_LOG(log, "Result decl type: \"{0}\"", type_desc_stream.GetData());
320   }
321 
322   m_result_name = lldb_private::ConstString("$RESULT_NAME");
323 
324   LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}",
325            m_result_name,
326            m_result_type.GetByteSize(target_sp.get()).getValueOr(0));
327 
328   // Construct a new result global and set up its metadata
329 
330   GlobalVariable *new_result_global = new GlobalVariable(
331       (*m_module), result_global->getValueType(), 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   //   %sel = load ptr, ptr @OBJC_SELECTOR_REFERENCES_, align 8
762   //   call i8 @objc_msgSend(ptr %obj, ptr %sel, ...)
763   //
764   // where %obj is the object pointer and %sel 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 and get the string from its target.
771 
772   GlobalVariable *_objc_selector_references_ =
773       dyn_cast<GlobalVariable>(load->getPointerOperand());
774 
775   if (!_objc_selector_references_ ||
776       !_objc_selector_references_->hasInitializer())
777     return false;
778 
779   Constant *osr_initializer = _objc_selector_references_->getInitializer();
780   if (!osr_initializer)
781     return false;
782 
783   // Find the string's initializer (a ConstantArray) and get the string from it
784 
785   GlobalVariable *_objc_meth_var_name_ =
786       dyn_cast<GlobalVariable>(osr_initializer);
787 
788   if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
789     return false;
790 
791   Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
792 
793   ConstantDataArray *omvn_initializer_array =
794       dyn_cast<ConstantDataArray>(omvn_initializer);
795 
796   if (!omvn_initializer_array->isString())
797     return false;
798 
799   std::string omvn_initializer_string =
800       std::string(omvn_initializer_array->getAsString());
801 
802   LLDB_LOG(log, "Found Objective-C selector reference \"{0}\"",
803            omvn_initializer_string);
804 
805   // Construct a call to sel_registerName
806 
807   if (!m_sel_registerName) {
808     lldb::addr_t sel_registerName_addr;
809 
810     bool missing_weak = false;
811     static lldb_private::ConstString g_sel_registerName_str("sel_registerName");
812     sel_registerName_addr = m_execution_unit.FindSymbol(g_sel_registerName_str,
813                                                         missing_weak);
814     if (sel_registerName_addr == LLDB_INVALID_ADDRESS || missing_weak)
815       return false;
816 
817     LLDB_LOG(log, "Found sel_registerName at {0}", sel_registerName_addr);
818 
819     // Build the function type: struct objc_selector
820     // *sel_registerName(uint8_t*)
821 
822     // The below code would be "more correct," but in actuality what's required
823     // is uint8_t*
824     // Type *sel_type = StructType::get(m_module->getContext());
825     // Type *sel_ptr_type = PointerType::getUnqual(sel_type);
826     Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
827 
828     Type *type_array[1];
829 
830     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
831 
832     ArrayRef<Type *> srN_arg_types(type_array, 1);
833 
834     llvm::FunctionType *srN_type =
835         FunctionType::get(sel_ptr_type, srN_arg_types, false);
836 
837     // Build the constant containing the pointer to the function
838     PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
839     Constant *srN_addr_int =
840         ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
841     m_sel_registerName = {srN_type,
842                           ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty)};
843   }
844 
845   Value *argument_array[1];
846 
847   Constant *omvn_pointer = ConstantExpr::getBitCast(
848       _objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
849 
850   argument_array[0] = omvn_pointer;
851 
852   ArrayRef<Value *> srN_arguments(argument_array, 1);
853 
854   CallInst *srN_call = CallInst::Create(m_sel_registerName, srN_arguments,
855                                         "sel_registerName", selector_load);
856 
857   // Replace the load with the call in all users
858 
859   selector_load->replaceAllUsesWith(srN_call);
860 
861   selector_load->eraseFromParent();
862 
863   return true;
864 }
865 
866 bool IRForTarget::RewriteObjCSelectors(BasicBlock &basic_block) {
867   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
868 
869   InstrList selector_loads;
870 
871   for (Instruction &inst : basic_block) {
872     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
873       if (IsObjCSelectorRef(load->getPointerOperand()))
874         selector_loads.push_back(&inst);
875   }
876 
877   for (Instruction *inst : selector_loads) {
878     if (!RewriteObjCSelector(inst)) {
879       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
880                             "static reference to an Objective-C selector to a "
881                             "dynamic reference\n");
882 
883       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C selector");
884 
885       return false;
886     }
887   }
888 
889   return true;
890 }
891 
892 static bool IsObjCClassReference(Value *value) {
893   GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
894 
895   return !(!global_variable || !global_variable->hasName() ||
896            !global_variable->getName().startswith("OBJC_CLASS_REFERENCES_"));
897 }
898 
899 // This function does not report errors; its callers are responsible.
900 bool IRForTarget::RewriteObjCClassReference(Instruction *class_load) {
901   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
902 
903   LoadInst *load = dyn_cast<LoadInst>(class_load);
904 
905   if (!load)
906     return false;
907 
908   // Unpack the class name from the reference.  In LLVM IR, a reference to an
909   // Objective-C class gets represented as
910   //
911   // %tmp     = load %struct._objc_class*,
912   //            %struct._objc_class** @OBJC_CLASS_REFERENCES_, align 4
913   //
914   // @"OBJC_CLASS_REFERENCES_ is a bitcast of a character array called
915   // @OBJC_CLASS_NAME_. @OBJC_CLASS_NAME contains the string.
916 
917   // Find the pointer's initializer (a ConstantExpr with opcode BitCast) and
918   // get the string from its target
919 
920   GlobalVariable *_objc_class_references_ =
921       dyn_cast<GlobalVariable>(load->getPointerOperand());
922 
923   if (!_objc_class_references_ ||
924       !_objc_class_references_->hasInitializer())
925     return false;
926 
927   Constant *ocr_initializer = _objc_class_references_->getInitializer();
928 
929   ConstantExpr *ocr_initializer_expr = dyn_cast<ConstantExpr>(ocr_initializer);
930 
931   if (!ocr_initializer_expr ||
932       ocr_initializer_expr->getOpcode() != Instruction::BitCast)
933     return false;
934 
935   Value *ocr_initializer_base = ocr_initializer_expr->getOperand(0);
936 
937   if (!ocr_initializer_base)
938     return false;
939 
940   // Find the string's initializer (a ConstantArray) and get the string from it
941 
942   GlobalVariable *_objc_class_name_ =
943       dyn_cast<GlobalVariable>(ocr_initializer_base);
944 
945   if (!_objc_class_name_ || !_objc_class_name_->hasInitializer())
946     return false;
947 
948   Constant *ocn_initializer = _objc_class_name_->getInitializer();
949 
950   ConstantDataArray *ocn_initializer_array =
951       dyn_cast<ConstantDataArray>(ocn_initializer);
952 
953   if (!ocn_initializer_array->isString())
954     return false;
955 
956   std::string ocn_initializer_string =
957       std::string(ocn_initializer_array->getAsString());
958 
959   LLDB_LOG(log, "Found Objective-C class reference \"{0}\"",
960            ocn_initializer_string);
961 
962   // Construct a call to objc_getClass
963 
964   if (!m_objc_getClass) {
965     lldb::addr_t objc_getClass_addr;
966 
967     bool missing_weak = false;
968     static lldb_private::ConstString g_objc_getClass_str("objc_getClass");
969     objc_getClass_addr = m_execution_unit.FindSymbol(g_objc_getClass_str,
970                                                      missing_weak);
971     if (objc_getClass_addr == LLDB_INVALID_ADDRESS || missing_weak)
972       return false;
973 
974     LLDB_LOG(log, "Found objc_getClass at {0}", objc_getClass_addr);
975 
976     // Build the function type: %struct._objc_class *objc_getClass(i8*)
977 
978     Type *class_type = load->getType();
979     Type *type_array[1];
980     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
981 
982     ArrayRef<Type *> ogC_arg_types(type_array, 1);
983 
984     llvm::FunctionType *ogC_type =
985         FunctionType::get(class_type, ogC_arg_types, false);
986 
987     // Build the constant containing the pointer to the function
988     PointerType *ogC_ptr_ty = PointerType::getUnqual(ogC_type);
989     Constant *ogC_addr_int =
990         ConstantInt::get(m_intptr_ty, objc_getClass_addr, false);
991     m_objc_getClass = {ogC_type,
992                        ConstantExpr::getIntToPtr(ogC_addr_int, ogC_ptr_ty)};
993   }
994 
995   Value *argument_array[1];
996 
997   Constant *ocn_pointer = ConstantExpr::getBitCast(
998       _objc_class_name_, Type::getInt8PtrTy(m_module->getContext()));
999 
1000   argument_array[0] = ocn_pointer;
1001 
1002   ArrayRef<Value *> ogC_arguments(argument_array, 1);
1003 
1004   CallInst *ogC_call = CallInst::Create(m_objc_getClass, ogC_arguments,
1005                                         "objc_getClass", class_load);
1006 
1007   // Replace the load with the call in all users
1008 
1009   class_load->replaceAllUsesWith(ogC_call);
1010 
1011   class_load->eraseFromParent();
1012 
1013   return true;
1014 }
1015 
1016 bool IRForTarget::RewriteObjCClassReferences(BasicBlock &basic_block) {
1017   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1018 
1019   InstrList class_loads;
1020 
1021   for (Instruction &inst : basic_block) {
1022     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1023       if (IsObjCClassReference(load->getPointerOperand()))
1024         class_loads.push_back(&inst);
1025   }
1026 
1027   for (Instruction *inst : class_loads) {
1028     if (!RewriteObjCClassReference(inst)) {
1029       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
1030                             "static reference to an Objective-C class to a "
1031                             "dynamic reference\n");
1032 
1033       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C class");
1034 
1035       return false;
1036     }
1037   }
1038 
1039   return true;
1040 }
1041 
1042 // This function does not report errors; its callers are responsible.
1043 bool IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc) {
1044   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1045 
1046   AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1047 
1048   MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1049 
1050   if (!alloc_md || !alloc_md->getNumOperands())
1051     return false;
1052 
1053   ConstantInt *constant_int =
1054       mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
1055 
1056   if (!constant_int)
1057     return false;
1058 
1059   // We attempt to register this as a new persistent variable with the DeclMap.
1060 
1061   uintptr_t ptr = constant_int->getZExtValue();
1062 
1063   clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
1064 
1065   lldb_private::TypeFromParser result_decl_type(
1066       m_decl_map->GetTypeSystem()->GetType(decl->getType()));
1067 
1068   StringRef decl_name(decl->getName());
1069   lldb_private::ConstString persistent_variable_name(decl_name.data(),
1070                                                      decl_name.size());
1071   if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name,
1072                                          result_decl_type, false, false))
1073     return false;
1074 
1075   GlobalVariable *persistent_global = new GlobalVariable(
1076       (*m_module), alloc->getType(), false,  /* not constant */
1077       GlobalValue::ExternalLinkage, nullptr, /* no initializer */
1078       alloc->getName().str());
1079 
1080   // What we're going to do here is make believe this was a regular old
1081   // external variable.  That means we need to make the metadata valid.
1082 
1083   NamedMDNode *named_metadata =
1084       m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
1085 
1086   llvm::Metadata *values[2];
1087   values[0] = ConstantAsMetadata::get(persistent_global);
1088   values[1] = ConstantAsMetadata::get(constant_int);
1089 
1090   ArrayRef<llvm::Metadata *> value_ref(values, 2);
1091 
1092   MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
1093   named_metadata->addOperand(persistent_global_md);
1094 
1095   // Now, since the variable is a pointer variable, we will drop in a load of
1096   // that pointer variable.
1097 
1098   LoadInst *persistent_load = new LoadInst(persistent_global->getValueType(),
1099                                            persistent_global, "", alloc);
1100 
1101   LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(alloc),
1102            PrintValue(persistent_load));
1103 
1104   alloc->replaceAllUsesWith(persistent_load);
1105   alloc->eraseFromParent();
1106 
1107   return true;
1108 }
1109 
1110 bool IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block) {
1111   if (!m_resolve_vars)
1112     return true;
1113 
1114   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1115 
1116   InstrList pvar_allocs;
1117 
1118   for (Instruction &inst : basic_block) {
1119 
1120     if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst)) {
1121       llvm::StringRef alloc_name = alloc->getName();
1122 
1123       if (alloc_name.startswith("$") && !alloc_name.startswith("$__lldb")) {
1124         if (alloc_name.find_first_of("0123456789") == 1) {
1125           LLDB_LOG(log, "Rejecting a numeric persistent variable.");
1126 
1127           m_error_stream.Printf("Error [IRForTarget]: Names starting with $0, "
1128                                 "$1, ... are reserved for use as result "
1129                                 "names\n");
1130 
1131           return false;
1132         }
1133 
1134         pvar_allocs.push_back(alloc);
1135       }
1136     }
1137   }
1138 
1139   for (Instruction *inst : pvar_allocs) {
1140     if (!RewritePersistentAlloc(inst)) {
1141       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1142                             "the creation of a persistent variable\n");
1143 
1144       LLDB_LOG(log, "Couldn't rewrite the creation of a persistent variable");
1145 
1146       return false;
1147     }
1148   }
1149 
1150   return true;
1151 }
1152 
1153 // This function does not report errors; its callers are responsible.
1154 bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) {
1155   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1156 
1157   LLDB_LOG(log, "MaybeHandleVariable ({0})", PrintValue(llvm_value_ptr));
1158 
1159   if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr)) {
1160     switch (constant_expr->getOpcode()) {
1161     default:
1162       break;
1163     case Instruction::GetElementPtr:
1164     case Instruction::BitCast:
1165       Value *s = constant_expr->getOperand(0);
1166       if (!MaybeHandleVariable(s))
1167         return false;
1168     }
1169   } else if (GlobalVariable *global_variable =
1170                  dyn_cast<GlobalVariable>(llvm_value_ptr)) {
1171     if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1172       return true;
1173 
1174     clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
1175 
1176     if (!named_decl) {
1177       if (IsObjCSelectorRef(llvm_value_ptr))
1178         return true;
1179 
1180       if (!global_variable->hasExternalLinkage())
1181         return true;
1182 
1183       LLDB_LOG(log, "Found global variable \"{0}\" without metadata",
1184                global_variable->getName());
1185 
1186       return false;
1187     }
1188 
1189     llvm::StringRef name(named_decl->getName());
1190 
1191     clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1192     if (value_decl == nullptr)
1193       return false;
1194 
1195     lldb_private::CompilerType compiler_type =
1196         m_decl_map->GetTypeSystem()->GetType(value_decl->getType());
1197 
1198     const Type *value_type = nullptr;
1199 
1200     if (name.startswith("$")) {
1201       // The $__lldb_expr_result name indicates the return value has allocated
1202       // as a static variable.  Per the comment at
1203       // ASTResultSynthesizer::SynthesizeBodyResult, accesses to this static
1204       // variable need to be redirected to the result of dereferencing a
1205       // pointer that is passed in as one of the arguments.
1206       //
1207       // Consequently, when reporting the size of the type, we report a pointer
1208       // type pointing to the type of $__lldb_expr_result, not the type itself.
1209       //
1210       // We also do this for any user-declared persistent variables.
1211       compiler_type = compiler_type.GetPointerType();
1212       value_type = PointerType::get(global_variable->getType(), 0);
1213     } else {
1214       value_type = global_variable->getType();
1215     }
1216 
1217     auto *target = m_execution_unit.GetTarget().get();
1218     llvm::Optional<uint64_t> value_size = compiler_type.GetByteSize(target);
1219     if (!value_size)
1220       return false;
1221     llvm::Optional<size_t> opt_alignment =
1222         compiler_type.GetTypeBitAlign(target);
1223     if (!opt_alignment)
1224       return false;
1225     lldb::offset_t value_alignment = (*opt_alignment + 7ull) / 8ull;
1226 
1227     LLDB_LOG(log,
1228              "Type of \"{0}\" is [clang \"{1}\", llvm \"{2}\"] [size {3}, "
1229              "align {4}]",
1230              name,
1231              lldb_private::ClangUtil::GetQualType(compiler_type).getAsString(),
1232              PrintType(value_type), *value_size, value_alignment);
1233 
1234     if (named_decl)
1235       m_decl_map->AddValueToStruct(named_decl, lldb_private::ConstString(name),
1236                                    llvm_value_ptr, *value_size,
1237                                    value_alignment);
1238   } else if (isa<llvm::Function>(llvm_value_ptr)) {
1239     LLDB_LOG(log, "Function pointers aren't handled right now");
1240 
1241     return false;
1242   }
1243 
1244   return true;
1245 }
1246 
1247 // This function does not report errors; its callers are responsible.
1248 bool IRForTarget::HandleSymbol(Value *symbol) {
1249   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1250 
1251   lldb_private::ConstString name(symbol->getName().str().c_str());
1252 
1253   lldb::addr_t symbol_addr =
1254       m_decl_map->GetSymbolAddress(name, lldb::eSymbolTypeAny);
1255 
1256   if (symbol_addr == LLDB_INVALID_ADDRESS) {
1257     LLDB_LOG(log, "Symbol \"{0}\" had no address", name);
1258 
1259     return false;
1260   }
1261 
1262   LLDB_LOG(log, "Found \"{0}\" at {1}", name, symbol_addr);
1263 
1264   Type *symbol_type = symbol->getType();
1265 
1266   Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
1267 
1268   Value *symbol_addr_ptr =
1269       ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1270 
1271   LLDB_LOG(log, "Replacing {0} with {1}", PrintValue(symbol),
1272            PrintValue(symbol_addr_ptr));
1273 
1274   symbol->replaceAllUsesWith(symbol_addr_ptr);
1275 
1276   return true;
1277 }
1278 
1279 bool IRForTarget::MaybeHandleCallArguments(CallInst *Old) {
1280   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1281 
1282   LLDB_LOG(log, "MaybeHandleCallArguments({0})", PrintValue(Old));
1283 
1284   for (unsigned op_index = 0, num_ops = Old->arg_size();
1285        op_index < num_ops; ++op_index)
1286     // conservatively believe that this is a store
1287     if (!MaybeHandleVariable(Old->getArgOperand(op_index))) {
1288       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1289                             "one of the arguments of a function call.\n");
1290 
1291       return false;
1292     }
1293 
1294   return true;
1295 }
1296 
1297 bool IRForTarget::HandleObjCClass(Value *classlist_reference) {
1298   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1299 
1300   GlobalVariable *global_variable =
1301       dyn_cast<GlobalVariable>(classlist_reference);
1302 
1303   if (!global_variable)
1304     return false;
1305 
1306   Constant *initializer = global_variable->getInitializer();
1307 
1308   if (!initializer)
1309     return false;
1310 
1311   if (!initializer->hasName())
1312     return false;
1313 
1314   StringRef name(initializer->getName());
1315   lldb_private::ConstString name_cstr(name.str().c_str());
1316   lldb::addr_t class_ptr =
1317       m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
1318 
1319   LLDB_LOG(log, "Found reference to Objective-C class {0} ({1})", name,
1320            (unsigned long long)class_ptr);
1321 
1322   if (class_ptr == LLDB_INVALID_ADDRESS)
1323     return false;
1324 
1325   if (global_variable->use_empty())
1326     return false;
1327 
1328   SmallVector<LoadInst *, 2> load_instructions;
1329 
1330   for (llvm::User *u : global_variable->users()) {
1331     if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
1332       load_instructions.push_back(load_instruction);
1333   }
1334 
1335   if (load_instructions.empty())
1336     return false;
1337 
1338   Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
1339 
1340   for (LoadInst *load_instruction : load_instructions) {
1341     Constant *class_bitcast =
1342         ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1343 
1344     load_instruction->replaceAllUsesWith(class_bitcast);
1345 
1346     load_instruction->eraseFromParent();
1347   }
1348 
1349   return true;
1350 }
1351 
1352 bool IRForTarget::RemoveCXAAtExit(BasicBlock &basic_block) {
1353   std::vector<CallInst *> calls_to_remove;
1354 
1355   for (Instruction &inst : basic_block) {
1356     CallInst *call = dyn_cast<CallInst>(&inst);
1357 
1358     // MaybeHandleCallArguments handles error reporting; we are silent here
1359     if (!call)
1360       continue;
1361 
1362     bool remove = false;
1363 
1364     llvm::Function *func = call->getCalledFunction();
1365 
1366     if (func && func->getName() == "__cxa_atexit")
1367       remove = true;
1368 
1369     llvm::Value *val = call->getCalledOperand();
1370 
1371     if (val && val->getName() == "__cxa_atexit")
1372       remove = true;
1373 
1374     if (remove)
1375       calls_to_remove.push_back(call);
1376   }
1377 
1378   for (CallInst *ci : calls_to_remove)
1379     ci->eraseFromParent();
1380 
1381   return true;
1382 }
1383 
1384 bool IRForTarget::ResolveCalls(BasicBlock &basic_block) {
1385   // Prepare the current basic block for execution in the remote process
1386 
1387   for (Instruction &inst : basic_block) {
1388     CallInst *call = dyn_cast<CallInst>(&inst);
1389 
1390     // MaybeHandleCallArguments handles error reporting; we are silent here
1391     if (call && !MaybeHandleCallArguments(call))
1392       return false;
1393   }
1394 
1395   return true;
1396 }
1397 
1398 bool IRForTarget::ResolveExternals(Function &llvm_function) {
1399   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1400 
1401   for (GlobalVariable &global_var : m_module->globals()) {
1402     llvm::StringRef global_name = global_var.getName();
1403 
1404     LLDB_LOG(log, "Examining {0}, DeclForGlobalValue returns {1}", global_name,
1405              static_cast<void *>(DeclForGlobal(&global_var)));
1406 
1407     if (global_name.startswith("OBJC_IVAR")) {
1408       if (!HandleSymbol(&global_var)) {
1409         m_error_stream.Format("Error [IRForTarget]: Couldn't find Objective-C "
1410                               "indirect ivar symbol {0}\n",
1411                               global_name);
1412 
1413         return false;
1414       }
1415     } else if (global_name.contains("OBJC_CLASSLIST_REFERENCES_$")) {
1416       if (!HandleObjCClass(&global_var)) {
1417         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1418                               "for an Objective-C static method call\n");
1419 
1420         return false;
1421       }
1422     } else if (global_name.contains("OBJC_CLASSLIST_SUP_REFS_$")) {
1423       if (!HandleObjCClass(&global_var)) {
1424         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1425                               "for an Objective-C static method call\n");
1426 
1427         return false;
1428       }
1429     } else if (DeclForGlobal(&global_var)) {
1430       if (!MaybeHandleVariable(&global_var)) {
1431         m_error_stream.Format("Internal error [IRForTarget]: Couldn't rewrite "
1432                               "external variable {0}\n",
1433                               global_name);
1434 
1435         return false;
1436       }
1437     }
1438   }
1439 
1440   return true;
1441 }
1442 
1443 static bool isGuardVariableRef(Value *V) {
1444   Constant *Old = dyn_cast<Constant>(V);
1445 
1446   if (!Old)
1447     return false;
1448 
1449   if (auto CE = dyn_cast<ConstantExpr>(V)) {
1450     if (CE->getOpcode() != Instruction::BitCast)
1451       return false;
1452 
1453     Old = CE->getOperand(0);
1454   }
1455 
1456   GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
1457 
1458   if (!GV || !GV->hasName() || !isGuardVariableSymbol(GV->getName()))
1459     return false;
1460 
1461   return true;
1462 }
1463 
1464 void IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction *guard_load) {
1465   Constant *zero(Constant::getNullValue(guard_load->getType()));
1466   guard_load->replaceAllUsesWith(zero);
1467   guard_load->eraseFromParent();
1468 }
1469 
1470 static void ExciseGuardStore(Instruction *guard_store) {
1471   guard_store->eraseFromParent();
1472 }
1473 
1474 bool IRForTarget::RemoveGuards(BasicBlock &basic_block) {
1475   // Eliminate any reference to guard variables found.
1476 
1477   InstrList guard_loads;
1478   InstrList guard_stores;
1479 
1480   for (Instruction &inst : basic_block) {
1481 
1482     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1483       if (isGuardVariableRef(load->getPointerOperand()))
1484         guard_loads.push_back(&inst);
1485 
1486     if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1487       if (isGuardVariableRef(store->getPointerOperand()))
1488         guard_stores.push_back(&inst);
1489   }
1490 
1491   for (Instruction *inst : guard_loads)
1492     TurnGuardLoadIntoZero(inst);
1493 
1494   for (Instruction *inst : guard_stores)
1495     ExciseGuardStore(inst);
1496 
1497   return true;
1498 }
1499 
1500 // This function does not report errors; its callers are responsible.
1501 bool IRForTarget::UnfoldConstant(Constant *old_constant,
1502                                  llvm::Function *llvm_function,
1503                                  FunctionValueCache &value_maker,
1504                                  FunctionValueCache &entry_instruction_finder,
1505                                  lldb_private::Stream &error_stream) {
1506   SmallVector<User *, 16> users;
1507 
1508   // We do this because the use list might change, invalidating our iterator.
1509   // Much better to keep a work list ourselves.
1510   for (llvm::User *u : old_constant->users())
1511     users.push_back(u);
1512 
1513   for (size_t i = 0; i < users.size(); ++i) {
1514     User *user = users[i];
1515 
1516     if (Constant *constant = dyn_cast<Constant>(user)) {
1517       // synthesize a new non-constant equivalent of the constant
1518 
1519       if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
1520         switch (constant_expr->getOpcode()) {
1521         default:
1522           error_stream.Printf("error [IRForTarget internal]: Unhandled "
1523                               "constant expression type: \"%s\"",
1524                               PrintValue(constant_expr).c_str());
1525           return false;
1526         case Instruction::BitCast: {
1527           FunctionValueCache bit_cast_maker(
1528               [&value_maker, &entry_instruction_finder, old_constant,
1529                constant_expr](llvm::Function *function) -> llvm::Value * {
1530                 // UnaryExpr
1531                 //   OperandList[0] is value
1532 
1533                 if (constant_expr->getOperand(0) != old_constant)
1534                   return constant_expr;
1535 
1536                 return new BitCastInst(
1537                     value_maker.GetValue(function), constant_expr->getType(),
1538                     "", llvm::cast<Instruction>(
1539                             entry_instruction_finder.GetValue(function)));
1540               });
1541 
1542           if (!UnfoldConstant(constant_expr, llvm_function, bit_cast_maker,
1543                               entry_instruction_finder, error_stream))
1544             return false;
1545         } break;
1546         case Instruction::GetElementPtr: {
1547           // GetElementPtrConstantExpr
1548           //   OperandList[0] is base
1549           //   OperandList[1]... are indices
1550 
1551           FunctionValueCache get_element_pointer_maker(
1552               [&value_maker, &entry_instruction_finder, old_constant,
1553                constant_expr](llvm::Function *function) -> llvm::Value * {
1554                 auto *gep = cast<llvm::GEPOperator>(constant_expr);
1555                 Value *ptr = gep->getPointerOperand();
1556 
1557                 if (ptr == old_constant)
1558                   ptr = value_maker.GetValue(function);
1559 
1560                 std::vector<Value *> index_vector;
1561                 for (Value *operand : gep->indices()) {
1562                   if (operand == old_constant)
1563                     operand = value_maker.GetValue(function);
1564 
1565                   index_vector.push_back(operand);
1566                 }
1567 
1568                 ArrayRef<Value *> indices(index_vector);
1569 
1570                 return GetElementPtrInst::Create(
1571                     gep->getSourceElementType(), ptr, indices, "",
1572                     llvm::cast<Instruction>(
1573                         entry_instruction_finder.GetValue(function)));
1574               });
1575 
1576           if (!UnfoldConstant(constant_expr, llvm_function,
1577                               get_element_pointer_maker,
1578                               entry_instruction_finder, error_stream))
1579             return false;
1580         } break;
1581         }
1582       } else {
1583         error_stream.Printf(
1584             "error [IRForTarget internal]: Unhandled constant type: \"%s\"",
1585             PrintValue(constant).c_str());
1586         return false;
1587       }
1588     } else {
1589       if (Instruction *inst = llvm::dyn_cast<Instruction>(user)) {
1590         if (llvm_function && inst->getParent()->getParent() != llvm_function) {
1591           error_stream.PutCString("error: Capturing non-local variables in "
1592                                   "expressions is unsupported.\n");
1593           return false;
1594         }
1595         inst->replaceUsesOfWith(
1596             old_constant, value_maker.GetValue(inst->getParent()->getParent()));
1597       } else {
1598         error_stream.Printf(
1599             "error [IRForTarget internal]: Unhandled non-constant type: \"%s\"",
1600             PrintValue(user).c_str());
1601         return false;
1602       }
1603     }
1604   }
1605 
1606   if (!isa<GlobalValue>(old_constant)) {
1607     old_constant->destroyConstant();
1608   }
1609 
1610   return true;
1611 }
1612 
1613 bool IRForTarget::ReplaceVariables(Function &llvm_function) {
1614   if (!m_resolve_vars)
1615     return true;
1616 
1617   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1618 
1619   m_decl_map->DoStructLayout();
1620 
1621   LLDB_LOG(log, "Element arrangement:");
1622 
1623   uint32_t num_elements;
1624   uint32_t element_index;
1625 
1626   size_t size;
1627   lldb::offset_t alignment;
1628 
1629   if (!m_decl_map->GetStructInfo(num_elements, size, alignment))
1630     return false;
1631 
1632   Function::arg_iterator iter(llvm_function.arg_begin());
1633 
1634   if (iter == llvm_function.arg_end()) {
1635     m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes no "
1636                           "arguments (should take at least a struct pointer)");
1637 
1638     return false;
1639   }
1640 
1641   Argument *argument = &*iter;
1642 
1643   if (argument->getName().equals("this")) {
1644     ++iter;
1645 
1646     if (iter == llvm_function.arg_end()) {
1647       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1648                             "'this' argument (should take a struct pointer "
1649                             "too)");
1650 
1651       return false;
1652     }
1653 
1654     argument = &*iter;
1655   } else if (argument->getName().equals("self")) {
1656     ++iter;
1657 
1658     if (iter == llvm_function.arg_end()) {
1659       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1660                             "'self' argument (should take '_cmd' and a struct "
1661                             "pointer too)");
1662 
1663       return false;
1664     }
1665 
1666     if (!iter->getName().equals("_cmd")) {
1667       m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes '{0}' "
1668                             "after 'self' argument (should take '_cmd')",
1669                             iter->getName());
1670 
1671       return false;
1672     }
1673 
1674     ++iter;
1675 
1676     if (iter == llvm_function.arg_end()) {
1677       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1678                             "'self' and '_cmd' arguments (should take a struct "
1679                             "pointer too)");
1680 
1681       return false;
1682     }
1683 
1684     argument = &*iter;
1685   }
1686 
1687   if (!argument->getName().equals("$__lldb_arg")) {
1688     m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes an "
1689                           "argument named '{0}' instead of the struct pointer",
1690                           argument->getName());
1691 
1692     return false;
1693   }
1694 
1695   LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument));
1696 
1697   BasicBlock &entry_block(llvm_function.getEntryBlock());
1698   Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
1699 
1700   if (!FirstEntryInstruction) {
1701     m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "
1702                           "first instruction in the wrapper for use in "
1703                           "rewriting");
1704 
1705     return false;
1706   }
1707 
1708   LLVMContext &context(m_module->getContext());
1709   IntegerType *offset_type(Type::getInt32Ty(context));
1710 
1711   if (!offset_type) {
1712     m_error_stream.Printf(
1713         "Internal error [IRForTarget]: Couldn't produce an offset type");
1714 
1715     return false;
1716   }
1717 
1718   for (element_index = 0; element_index < num_elements; ++element_index) {
1719     const clang::NamedDecl *decl = nullptr;
1720     Value *value = nullptr;
1721     lldb::offset_t offset;
1722     lldb_private::ConstString name;
1723 
1724     if (!m_decl_map->GetStructElement(decl, value, offset, name,
1725                                       element_index)) {
1726       m_error_stream.Printf(
1727           "Internal error [IRForTarget]: Structure information is incomplete");
1728 
1729       return false;
1730     }
1731 
1732     LLDB_LOG(log, "  \"{0}\" (\"{1}\") placed at {2}", name,
1733              decl->getNameAsString(), offset);
1734 
1735     if (value) {
1736       LLDB_LOG(log, "    Replacing [{0}]", PrintValue(value));
1737 
1738       FunctionValueCache body_result_maker(
1739           [this, name, offset_type, offset, argument,
1740            value](llvm::Function *function) -> llvm::Value * {
1741             // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1742             // in cases where the result variable is an rvalue, we have to
1743             // synthesize a dereference of the appropriate structure entry in
1744             // order to produce the static variable that the AST thinks it is
1745             // accessing.
1746 
1747             llvm::Instruction *entry_instruction = llvm::cast<Instruction>(
1748                 m_entry_instruction_finder.GetValue(function));
1749 
1750             Type *int8Ty = Type::getInt8Ty(function->getContext());
1751             ConstantInt *offset_int(
1752                 ConstantInt::get(offset_type, offset, true));
1753             GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(
1754                 int8Ty, argument, offset_int, "", entry_instruction);
1755 
1756             if (name == m_result_name && !m_result_is_pointer) {
1757               BitCastInst *bit_cast = new BitCastInst(
1758                   get_element_ptr, value->getType()->getPointerTo(), "",
1759                   entry_instruction);
1760 
1761               LoadInst *load = new LoadInst(value->getType(), bit_cast, "",
1762                                             entry_instruction);
1763 
1764               return load;
1765             } else {
1766               BitCastInst *bit_cast = new BitCastInst(
1767                   get_element_ptr, value->getType(), "", entry_instruction);
1768 
1769               return bit_cast;
1770             }
1771           });
1772 
1773       if (Constant *constant = dyn_cast<Constant>(value)) {
1774         if (!UnfoldConstant(constant, &llvm_function, body_result_maker,
1775                             m_entry_instruction_finder, m_error_stream)) {
1776           return false;
1777         }
1778       } else if (Instruction *instruction = dyn_cast<Instruction>(value)) {
1779         if (instruction->getParent()->getParent() != &llvm_function) {
1780           m_error_stream.PutCString("error: Capturing non-local variables in "
1781                                     "expressions is unsupported.\n");
1782           return false;
1783         }
1784         value->replaceAllUsesWith(
1785             body_result_maker.GetValue(instruction->getParent()->getParent()));
1786       } else {
1787         LLDB_LOG(log, "Unhandled non-constant type: \"{0}\"",
1788                  PrintValue(value));
1789         return false;
1790       }
1791 
1792       if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1793         var->eraseFromParent();
1794     }
1795   }
1796 
1797   LLDB_LOG(log, "Total structure [align {0}, size {1}]", (int64_t)alignment,
1798            (uint64_t)size);
1799 
1800   return true;
1801 }
1802 
1803 bool IRForTarget::runOnModule(Module &llvm_module) {
1804   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1805 
1806   m_module = &llvm_module;
1807   m_target_data = std::make_unique<DataLayout>(m_module);
1808   m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(),
1809                                       m_target_data->getPointerSizeInBits());
1810 
1811   if (log) {
1812     std::string s;
1813     raw_string_ostream oss(s);
1814 
1815     m_module->print(oss, nullptr);
1816 
1817     oss.flush();
1818 
1819     LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s);
1820   }
1821 
1822   Function *const main_function =
1823       m_func_name.IsEmpty() ? nullptr
1824                             : m_module->getFunction(m_func_name.GetStringRef());
1825 
1826   if (!m_func_name.IsEmpty() && !main_function) {
1827     LLDB_LOG(log, "Couldn't find \"{0}()\" in the module", m_func_name);
1828 
1829     m_error_stream.Format("Internal error [IRForTarget]: Couldn't find wrapper "
1830                           "'{0}' in the module",
1831                           m_func_name);
1832 
1833     return false;
1834   }
1835 
1836   if (main_function) {
1837     if (!FixFunctionLinkage(*main_function)) {
1838       LLDB_LOG(log, "Couldn't fix the linkage for the function");
1839 
1840       return false;
1841     }
1842   }
1843 
1844   llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
1845 
1846   m_reloc_placeholder = new llvm::GlobalVariable(
1847       (*m_module), int8_ty, false /* IsConstant */,
1848       GlobalVariable::InternalLinkage, Constant::getNullValue(int8_ty),
1849       "reloc_placeholder", nullptr /* InsertBefore */,
1850       GlobalVariable::NotThreadLocal /* ThreadLocal */, 0 /* AddressSpace */);
1851 
1852   ////////////////////////////////////////////////////////////
1853   // Replace $__lldb_expr_result with a persistent variable
1854   //
1855 
1856   if (main_function) {
1857     if (!CreateResultVariable(*main_function)) {
1858       LLDB_LOG(log, "CreateResultVariable() failed");
1859 
1860       // CreateResultVariable() reports its own errors, so we don't do so here
1861 
1862       return false;
1863     }
1864   }
1865 
1866   if (log && log->GetVerbose()) {
1867     std::string s;
1868     raw_string_ostream oss(s);
1869 
1870     m_module->print(oss, nullptr);
1871 
1872     oss.flush();
1873 
1874     LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s);
1875   }
1876 
1877   for (llvm::Function &function : *m_module) {
1878     for (BasicBlock &bb : function) {
1879       if (!RemoveGuards(bb)) {
1880         LLDB_LOG(log, "RemoveGuards() failed");
1881 
1882         // RemoveGuards() reports its own errors, so we don't do so here
1883 
1884         return false;
1885       }
1886 
1887       if (!RewritePersistentAllocs(bb)) {
1888         LLDB_LOG(log, "RewritePersistentAllocs() failed");
1889 
1890         // RewritePersistentAllocs() reports its own errors, so we don't do so
1891         // here
1892 
1893         return false;
1894       }
1895 
1896       if (!RemoveCXAAtExit(bb)) {
1897         LLDB_LOG(log, "RemoveCXAAtExit() failed");
1898 
1899         // RemoveCXAAtExit() reports its own errors, so we don't do so here
1900 
1901         return false;
1902       }
1903     }
1904   }
1905 
1906   ///////////////////////////////////////////////////////////////////////////////
1907   // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
1908   //
1909 
1910   if (!RewriteObjCConstStrings()) {
1911     LLDB_LOG(log, "RewriteObjCConstStrings() failed");
1912 
1913     // RewriteObjCConstStrings() reports its own errors, so we don't do so here
1914 
1915     return false;
1916   }
1917 
1918   for (llvm::Function &function : *m_module) {
1919     for (llvm::BasicBlock &bb : function) {
1920       if (!RewriteObjCSelectors(bb)) {
1921         LLDB_LOG(log, "RewriteObjCSelectors() failed");
1922 
1923         // RewriteObjCSelectors() reports its own errors, so we don't do so
1924         // here
1925 
1926         return false;
1927       }
1928 
1929       if (!RewriteObjCClassReferences(bb)) {
1930         LLDB_LOG(log, "RewriteObjCClassReferences() failed");
1931 
1932         // RewriteObjCClasses() reports its own errors, so we don't do so here
1933 
1934         return false;
1935       }
1936     }
1937   }
1938 
1939   for (llvm::Function &function : *m_module) {
1940     for (BasicBlock &bb : function) {
1941       if (!ResolveCalls(bb)) {
1942         LLDB_LOG(log, "ResolveCalls() failed");
1943 
1944         // ResolveCalls() reports its own errors, so we don't do so here
1945 
1946         return false;
1947       }
1948     }
1949   }
1950 
1951   ////////////////////////////////////////////////////////////////////////
1952   // Run function-level passes that only make sense on the main function
1953   //
1954 
1955   if (main_function) {
1956     if (!ResolveExternals(*main_function)) {
1957       LLDB_LOG(log, "ResolveExternals() failed");
1958 
1959       // ResolveExternals() reports its own errors, so we don't do so here
1960 
1961       return false;
1962     }
1963 
1964     if (!ReplaceVariables(*main_function)) {
1965       LLDB_LOG(log, "ReplaceVariables() failed");
1966 
1967       // ReplaceVariables() reports its own errors, so we don't do so here
1968 
1969       return false;
1970     }
1971   }
1972 
1973   if (log && log->GetVerbose()) {
1974     std::string s;
1975     raw_string_ostream oss(s);
1976 
1977     m_module->print(oss, nullptr);
1978 
1979     oss.flush();
1980 
1981     LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s);
1982   }
1983 
1984   return true;
1985 }
1986