1251875Speter//===-- IRForTarget.cpp ---------------------------------------------------===//
2251875Speter//
3251875Speter// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4251875Speter// See https://llvm.org/LICENSE.txt for license information.
5251875Speter// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6251875Speter//
7251875Speter//===----------------------------------------------------------------------===//
8251875Speter
9251875Speter#include "IRForTarget.h"
10251875Speter
11251875Speter#include "ClangExpressionDeclMap.h"
12251875Speter#include "ClangUtil.h"
13251875Speter
14251875Speter#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15251875Speter#include "llvm/IR/Constants.h"
16251875Speter#include "llvm/IR/DataLayout.h"
17251875Speter#include "llvm/IR/Operator.h"
18251875Speter#include "llvm/IR/InstrTypes.h"
19251875Speter#include "llvm/IR/Instructions.h"
20251875Speter#include "llvm/IR/Intrinsics.h"
21251875Speter#include "llvm/IR/LegacyPassManager.h"
22251875Speter#include "llvm/IR/Metadata.h"
23251875Speter#include "llvm/IR/Module.h"
24251875Speter#include "llvm/IR/ValueSymbolTable.h"
25251875Speter#include "llvm/Support/raw_ostream.h"
26251875Speter#include "llvm/Transforms/IPO.h"
27251875Speter
28251875Speter#include "clang/AST/ASTContext.h"
29251875Speter
30251875Speter#include "lldb/Core/dwarf.h"
31251875Speter#include "lldb/Expression/IRExecutionUnit.h"
32251875Speter#include "lldb/Expression/IRInterpreter.h"
33251875Speter#include "lldb/Symbol/CompilerType.h"
34251875Speter#include "lldb/Utility/ConstString.h"
35251875Speter#include "lldb/Utility/DataBufferHeap.h"
36251875Speter#include "lldb/Utility/Endian.h"
37251875Speter#include "lldb/Utility/LLDBLog.h"
38251875Speter#include "lldb/Utility/Log.h"
39251875Speter#include "lldb/Utility/Scalar.h"
40251875Speter#include "lldb/Utility/StreamString.h"
41251875Speter
42251875Speter#include <map>
43251875Speter#include <optional>
44251875Speter
45251875Speterusing namespace llvm;
46251875Speterusing lldb_private::LLDBLog;
47251875Speter
48251875Spetertypedef SmallVector<Instruction *, 2> InstrList;
49251875Speter
50251875SpeterIRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker)
51251875Speter    : m_maker(maker), m_values() {}
52251875Speter
53251875SpeterIRForTarget::FunctionValueCache::~FunctionValueCache() = default;
54251875Speter
55251875Speterllvm::Value *
56251875SpeterIRForTarget::FunctionValueCache::GetValue(llvm::Function *function) {
57251875Speter  if (!m_values.count(function)) {
58251875Speter    llvm::Value *ret = m_maker(function);
59251875Speter    m_values[function] = ret;
60251875Speter    return ret;
61251875Speter  }
62251875Speter  return m_values[function];
63251875Speter}
64251875Speter
65251875Speterstatic llvm::Value *FindEntryInstruction(llvm::Function *function) {
66251875Speter  if (function->empty())
67251875Speter    return nullptr;
68251875Speter
69251875Speter  return function->getEntryBlock().getFirstNonPHIOrDbg();
70251875Speter}
71251875Speter
72251875SpeterIRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
73251875Speter                         bool resolve_vars,
74251875Speter                         lldb_private::IRExecutionUnit &execution_unit,
75251875Speter                         lldb_private::Stream &error_stream,
76251875Speter                         const char *func_name)
77251875Speter    : m_resolve_vars(resolve_vars), m_func_name(func_name),
78251875Speter      m_decl_map(decl_map), m_error_stream(error_stream),
79251875Speter      m_execution_unit(execution_unit),
80251875Speter      m_entry_instruction_finder(FindEntryInstruction) {}
81251875Speter
82251875Speter/* Handy utility functions used at several places in the code */
83251875Speter
84251875Speterstatic std::string PrintValue(const Value *value, bool truncate = false) {
85251875Speter  std::string s;
86251875Speter  if (value) {
87251875Speter    raw_string_ostream rso(s);
88251875Speter    value->print(rso);
89251875Speter    rso.flush();
90251875Speter    if (truncate)
91251875Speter      s.resize(s.length() - 1);
92251875Speter  }
93251875Speter  return s;
94251875Speter}
95251875Speter
96251875Speterstatic std::string PrintType(const llvm::Type *type, bool truncate = false) {
97251875Speter  std::string s;
98251875Speter  raw_string_ostream rso(s);
99251875Speter  type->print(rso);
100251875Speter  rso.flush();
101251875Speter  if (truncate)
102251875Speter    s.resize(s.length() - 1);
103251875Speter  return s;
104251875Speter}
105251875Speter
106251875Speterbool IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function) {
107251875Speter  llvm_function.setLinkage(GlobalValue::ExternalLinkage);
108251875Speter
109251875Speter  return true;
110251875Speter}
111251875Speter
112251875Speterclang::NamedDecl *IRForTarget::DeclForGlobal(const GlobalValue *global_val,
113251875Speter                                             Module *module) {
114251875Speter  NamedMDNode *named_metadata =
115251875Speter      module->getNamedMetadata("clang.global.decl.ptrs");
116251875Speter
117251875Speter  if (!named_metadata)
118251875Speter    return nullptr;
119251875Speter
120251875Speter  unsigned num_nodes = named_metadata->getNumOperands();
121251875Speter  unsigned node_index;
122251875Speter
123251875Speter  for (node_index = 0; node_index < num_nodes; ++node_index) {
124251875Speter    llvm::MDNode *metadata_node =
125251875Speter        dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
126251875Speter    if (!metadata_node)
127251875Speter      return nullptr;
128251875Speter
129251875Speter    if (metadata_node->getNumOperands() != 2)
130251875Speter      continue;
131251875Speter
132251875Speter    if (mdconst::dyn_extract_or_null<GlobalValue>(
133251875Speter            metadata_node->getOperand(0)) != global_val)
134251875Speter      continue;
135251875Speter
136251875Speter    ConstantInt *constant_int =
137251875Speter        mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
138251875Speter
139251875Speter    if (!constant_int)
140251875Speter      return nullptr;
141251875Speter
142251875Speter    uintptr_t ptr = constant_int->getZExtValue();
143251875Speter
144251875Speter    return reinterpret_cast<clang::NamedDecl *>(ptr);
145251875Speter  }
146251875Speter
147251875Speter  return nullptr;
148251875Speter}
149251875Speter
150251875Speterclang::NamedDecl *IRForTarget::DeclForGlobal(GlobalValue *global_val) {
151251875Speter  return DeclForGlobal(global_val, m_module);
152251875Speter}
153251875Speter
154251875Speter/// Returns true iff the mangled symbol is for a static guard variable.
155251875Speterstatic bool isGuardVariableSymbol(llvm::StringRef mangled_symbol,
156251875Speter                                  bool check_ms_abi = true) {
157251875Speter  bool result =
158251875Speter      mangled_symbol.starts_with("_ZGV"); // Itanium ABI guard variable
159251875Speter  if (check_ms_abi)
160251875Speter    result |= mangled_symbol.ends_with("@4IA"); // Microsoft ABI
161251875Speter  return result;
162251875Speter}
163251875Speter
164251875Speterbool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
165251875Speter  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
166251875Speter
167251875Speter  if (!m_resolve_vars)
168251875Speter    return true;
169251875Speter
170251875Speter  // Find the result variable.  If it doesn't exist, we can give up right here.
171251875Speter
172251875Speter  ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
173251875Speter
174251875Speter  llvm::StringRef result_name;
175251875Speter  bool found_result = false;
176251875Speter
177251875Speter  for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
178251875Speter    result_name = value_symbol.first();
179251875Speter
180251875Speter    // Check if this is a guard variable. It seems this causes some hiccups
181251875Speter    // on Windows, so let's only check for Itanium guard variables.
182251875Speter    bool is_guard_var = isGuardVariableSymbol(result_name, /*MS ABI*/ false);
183251875Speter
184251875Speter    if (result_name.contains("$__lldb_expr_result_ptr") && !is_guard_var) {
185251875Speter      found_result = true;
186251875Speter      m_result_is_pointer = true;
187251875Speter      break;
188251875Speter    }
189251875Speter
190251875Speter    if (result_name.contains("$__lldb_expr_result") && !is_guard_var) {
191251875Speter      found_result = true;
192251875Speter      m_result_is_pointer = false;
193251875Speter      break;
194251875Speter    }
195251875Speter  }
196251875Speter
197251875Speter  if (!found_result) {
198251875Speter    LLDB_LOG(log, "Couldn't find result variable");
199251875Speter
200251875Speter    return true;
201251875Speter  }
202251875Speter
203251875Speter  LLDB_LOG(log, "Result name: \"{0}\"", result_name);
204251875Speter
205251875Speter  Value *result_value = m_module->getNamedValue(result_name);
206251875Speter
207251875Speter  if (!result_value) {
208251875Speter    LLDB_LOG(log, "Result variable had no data");
209251875Speter
210251875Speter    m_error_stream.Format("Internal error [IRForTarget]: Result variable's "
211251875Speter                          "name ({0}) exists, but not its definition\n",
212251875Speter                          result_name);
213251875Speter
214251875Speter    return false;
215251875Speter  }
216251875Speter
217251875Speter  LLDB_LOG(log, "Found result in the IR: \"{0}\"",
218251875Speter           PrintValue(result_value, false));
219251875Speter
220251875Speter  GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
221251875Speter
222251875Speter  if (!result_global) {
223251875Speter    LLDB_LOG(log, "Result variable isn't a GlobalVariable");
224251875Speter
225251875Speter    m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
226251875Speter                          "is defined, but is not a global variable\n",
227251875Speter                          result_name);
228251875Speter
229251875Speter    return false;
230251875Speter  }
231251875Speter
232251875Speter  clang::NamedDecl *result_decl = DeclForGlobal(result_global);
233251875Speter  if (!result_decl) {
234251875Speter    LLDB_LOG(log, "Result variable doesn't have a corresponding Decl");
235251875Speter
236251875Speter    m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
237251875Speter                          "does not have a corresponding Clang entity\n",
238251875Speter                          result_name);
239251875Speter
240251875Speter    return false;
241251875Speter  }
242251875Speter
243251875Speter  if (log) {
244251875Speter    std::string decl_desc_str;
245251875Speter    raw_string_ostream decl_desc_stream(decl_desc_str);
246251875Speter    result_decl->print(decl_desc_stream);
247251875Speter    decl_desc_stream.flush();
248251875Speter
249251875Speter    LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str);
250251875Speter  }
251251875Speter
252251875Speter  clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
253251875Speter  if (!result_var) {
254251875Speter    LLDB_LOG(log, "Result variable Decl isn't a VarDecl");
255251875Speter
256251875Speter    m_error_stream.Format("Internal error [IRForTarget]: Result variable "
257251875Speter                          "({0})'s corresponding Clang entity isn't a "
258251875Speter                          "variable\n",
259251875Speter                          result_name);
260251875Speter
261251875Speter    return false;
262251875Speter  }
263251875Speter
264251875Speter  // Get the next available result name from m_decl_map and create the
265251875Speter  // persistent variable for it
266251875Speter
267251875Speter  // If the result is an Lvalue, it is emitted as a pointer; see
268251875Speter  // ASTResultSynthesizer::SynthesizeBodyResult.
269251875Speter  if (m_result_is_pointer) {
270251875Speter    clang::QualType pointer_qual_type = result_var->getType();
271251875Speter    const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
272251875Speter
273251875Speter    const clang::PointerType *pointer_pointertype =
274251875Speter        pointer_type->getAs<clang::PointerType>();
275251875Speter    const clang::ObjCObjectPointerType *pointer_objcobjpointertype =
276251875Speter        pointer_type->getAs<clang::ObjCObjectPointerType>();
277251875Speter
278251875Speter    if (pointer_pointertype) {
279251875Speter      clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
280251875Speter
281251875Speter      m_result_type = lldb_private::TypeFromParser(
282251875Speter          m_decl_map->GetTypeSystem()->GetType(element_qual_type));
283251875Speter    } else if (pointer_objcobjpointertype) {
284251875Speter      clang::QualType element_qual_type =
285251875Speter          clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
286251875Speter
287251875Speter      m_result_type = lldb_private::TypeFromParser(
288251875Speter          m_decl_map->GetTypeSystem()->GetType(element_qual_type));
289251875Speter    } else {
290251875Speter      LLDB_LOG(log, "Expected result to have pointer type, but it did not");
291251875Speter
292251875Speter      m_error_stream.Format("Internal error [IRForTarget]: Lvalue result ({0}) "
293251875Speter                            "is not a pointer variable\n",
294251875Speter                            result_name);
295251875Speter
296251875Speter      return false;
297251875Speter    }
298251875Speter  } else {
299251875Speter    m_result_type = lldb_private::TypeFromParser(
300251875Speter        m_decl_map->GetTypeSystem()->GetType(result_var->getType()));
301251875Speter  }
302251875Speter
303251875Speter  lldb::TargetSP target_sp(m_execution_unit.GetTarget());
304251875Speter  std::optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get());
305251875Speter  if (!bit_size) {
306251875Speter    lldb_private::StreamString type_desc_stream;
307251875Speter    m_result_type.DumpTypeDescription(&type_desc_stream);
308251875Speter
309251875Speter    LLDB_LOG(log, "Result type has unknown size");
310251875Speter
311251875Speter    m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' "
312251875Speter                          "couldn't be determined\n",
313251875Speter                          type_desc_stream.GetData());
314251875Speter    return false;
315251875Speter  }
316251875Speter
317251875Speter  if (log) {
318251875Speter    lldb_private::StreamString type_desc_stream;
319251875Speter    m_result_type.DumpTypeDescription(&type_desc_stream);
320251875Speter
321251875Speter    LLDB_LOG(log, "Result decl type: \"{0}\"", type_desc_stream.GetData());
322251875Speter  }
323251875Speter
324251875Speter  m_result_name = lldb_private::ConstString("$RESULT_NAME");
325251875Speter
326251875Speter  LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}",
327251875Speter           m_result_name,
328251875Speter           m_result_type.GetByteSize(target_sp.get()).value_or(0));
329251875Speter
330251875Speter  // Construct a new result global and set up its metadata
331251875Speter
332251875Speter  GlobalVariable *new_result_global = new GlobalVariable(
333251875Speter      (*m_module), result_global->getValueType(), false, /* not constant */
334251875Speter      GlobalValue::ExternalLinkage, nullptr,             /* no initializer */
335251875Speter      m_result_name.GetCString());
336251875Speter
337251875Speter  // It's too late in compilation to create a new VarDecl for this, but we
338251875Speter  // don't need to.  We point the metadata at the old VarDecl.  This creates an
339251875Speter  // odd anomaly: a variable with a Value whose name is something like $0 and a
340251875Speter  // Decl whose name is $__lldb_expr_result.  This condition is handled in
341251875Speter  // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
342251875Speter  // fixed up.
343251875Speter
344251875Speter  ConstantInt *new_constant_int =
345251875Speter      ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
346251875Speter                       reinterpret_cast<uintptr_t>(result_decl), false);
347251875Speter
348251875Speter  llvm::Metadata *values[2];
349251875Speter  values[0] = ConstantAsMetadata::get(new_result_global);
350251875Speter  values[1] = ConstantAsMetadata::get(new_constant_int);
351251875Speter
352251875Speter  ArrayRef<Metadata *> value_ref(values, 2);
353251875Speter
354251875Speter  MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
355251875Speter  NamedMDNode *named_metadata =
356251875Speter      m_module->getNamedMetadata("clang.global.decl.ptrs");
357251875Speter  named_metadata->addOperand(persistent_global_md);
358251875Speter
359251875Speter  LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(result_global),
360251875Speter           PrintValue(new_result_global));
361251875Speter
362251875Speter  if (result_global->use_empty()) {
363251875Speter    // We need to synthesize a store for this variable, because otherwise
364251875Speter    // there's nothing to put into its equivalent persistent variable.
365251875Speter
366251875Speter    BasicBlock &entry_block(llvm_function.getEntryBlock());
367251875Speter    Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
368251875Speter
369251875Speter    if (!first_entry_instruction)
370251875Speter      return false;
371251875Speter
372251875Speter    if (!result_global->hasInitializer()) {
373251875Speter      LLDB_LOG(log, "Couldn't find initializer for unused variable");
374251875Speter
375251875Speter      m_error_stream.Format("Internal error [IRForTarget]: Result variable "
376251875Speter                            "({0}) has no writes and no initializer\n",
377251875Speter                            result_name);
378251875Speter
379251875Speter      return false;
380251875Speter    }
381251875Speter
382251875Speter    Constant *initializer = result_global->getInitializer();
383251875Speter
384251875Speter    StoreInst *synthesized_store =
385251875Speter        new StoreInst(initializer, new_result_global, first_entry_instruction);
386251875Speter
387251875Speter    LLDB_LOG(log, "Synthesized result store \"{0}\"\n",
388251875Speter             PrintValue(synthesized_store));
389251875Speter  } else {
390251875Speter    result_global->replaceAllUsesWith(new_result_global);
391251875Speter  }
392251875Speter
393251875Speter  if (!m_decl_map->AddPersistentVariable(
394251875Speter          result_decl, m_result_name, m_result_type, true, m_result_is_pointer))
395251875Speter    return false;
396251875Speter
397251875Speter  result_global->eraseFromParent();
398251875Speter
399251875Speter  return true;
400251875Speter}
401251875Speter
402251875Speterbool IRForTarget::RewriteObjCConstString(llvm::GlobalVariable *ns_str,
403251875Speter                                         llvm::GlobalVariable *cstr) {
404251875Speter  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
405251875Speter
406251875Speter  Type *ns_str_ty = ns_str->getType();
407251875Speter
408251875Speter  Type *i8_ptr_ty = PointerType::getUnqual(m_module->getContext());
409251875Speter  Type *i32_ty = Type::getInt32Ty(m_module->getContext());
410251875Speter  Type *i8_ty = Type::getInt8Ty(m_module->getContext());
411251875Speter
412251875Speter  if (!m_CFStringCreateWithBytes) {
413251875Speter    lldb::addr_t CFStringCreateWithBytes_addr;
414251875Speter
415251875Speter    static lldb_private::ConstString g_CFStringCreateWithBytes_str(
416251875Speter        "CFStringCreateWithBytes");
417251875Speter
418251875Speter    bool missing_weak = false;
419251875Speter    CFStringCreateWithBytes_addr =
420251875Speter        m_execution_unit.FindSymbol(g_CFStringCreateWithBytes_str,
421251875Speter                                    missing_weak);
422251875Speter    if (CFStringCreateWithBytes_addr == LLDB_INVALID_ADDRESS || missing_weak) {
423251875Speter      LLDB_LOG(log, "Couldn't find CFStringCreateWithBytes in the target");
424251875Speter
425251875Speter      m_error_stream.Printf("Error [IRForTarget]: Rewriting an Objective-C "
426251875Speter                            "constant string requires "
427251875Speter                            "CFStringCreateWithBytes\n");
428251875Speter
429251875Speter      return false;
430251875Speter    }
431251875Speter
432251875Speter    LLDB_LOG(log, "Found CFStringCreateWithBytes at {0}",
433251875Speter             CFStringCreateWithBytes_addr);
434251875Speter
435251875Speter    // Build the function type:
436251875Speter    //
437251875Speter    // CFStringRef CFStringCreateWithBytes (
438251875Speter    //   CFAllocatorRef alloc,
439251875Speter    //   const UInt8 *bytes,
440251875Speter    //   CFIndex numBytes,
441251875Speter    //   CFStringEncoding encoding,
442251875Speter    //   Boolean isExternalRepresentation
443251875Speter    // );
444251875Speter    //
445251875Speter    // We make the following substitutions:
446251875Speter    //
447251875Speter    // CFStringRef -> i8*
448251875Speter    // CFAllocatorRef -> i8*
449251875Speter    // UInt8 * -> i8*
450251875Speter    // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its
451251875Speter    // pointer size for now) CFStringEncoding -> i32 Boolean -> i8
452251875Speter
453251875Speter    Type *arg_type_array[5];
454251875Speter
455251875Speter    arg_type_array[0] = i8_ptr_ty;
456251875Speter    arg_type_array[1] = i8_ptr_ty;
457251875Speter    arg_type_array[2] = m_intptr_ty;
458251875Speter    arg_type_array[3] = i32_ty;
459251875Speter    arg_type_array[4] = i8_ty;
460251875Speter
461251875Speter    ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
462251875Speter
463251875Speter    llvm::FunctionType *CFSCWB_ty =
464251875Speter        FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
465251875Speter
466251875Speter    // Build the constant containing the pointer to the function
467251875Speter    PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
468251875Speter    Constant *CFSCWB_addr_int =
469        ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
470    m_CFStringCreateWithBytes = {
471        CFSCWB_ty, ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty)};
472  }
473
474  ConstantDataSequential *string_array = nullptr;
475
476  if (cstr)
477    string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
478
479  Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
480  Constant *bytes_arg = cstr ? cstr : 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
539bool 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
721static bool IsObjCSelectorRef(Value *value) {
722  GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
723
724  return !(
725      !global_variable || !global_variable->hasName() ||
726      !global_variable->getName().starts_with("OBJC_SELECTOR_REFERENCES_"));
727}
728
729// This function does not report errors; its callers are responsible.
730bool IRForTarget::RewriteObjCSelector(Instruction *selector_load) {
731  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
732
733  LoadInst *load = dyn_cast<LoadInst>(selector_load);
734
735  if (!load)
736    return false;
737
738  // Unpack the message name from the selector.  In LLVM IR, an objc_msgSend
739  // gets represented as
740  //
741  //   %sel = load ptr, ptr @OBJC_SELECTOR_REFERENCES_, align 8
742  //   call i8 @objc_msgSend(ptr %obj, ptr %sel, ...)
743  //
744  // where %obj is the object pointer and %sel is the selector.
745  //
746  // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called
747  // @"\01L_OBJC_METH_VAR_NAME_".
748  // @"\01L_OBJC_METH_VAR_NAME_" contains the string.
749
750  // Find the pointer's initializer and get the string from its target.
751
752  GlobalVariable *_objc_selector_references_ =
753      dyn_cast<GlobalVariable>(load->getPointerOperand());
754
755  if (!_objc_selector_references_ ||
756      !_objc_selector_references_->hasInitializer())
757    return false;
758
759  Constant *osr_initializer = _objc_selector_references_->getInitializer();
760  if (!osr_initializer)
761    return false;
762
763  // Find the string's initializer (a ConstantArray) and get the string from it
764
765  GlobalVariable *_objc_meth_var_name_ =
766      dyn_cast<GlobalVariable>(osr_initializer);
767
768  if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
769    return false;
770
771  Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
772
773  ConstantDataArray *omvn_initializer_array =
774      dyn_cast<ConstantDataArray>(omvn_initializer);
775
776  if (!omvn_initializer_array->isString())
777    return false;
778
779  std::string omvn_initializer_string =
780      std::string(omvn_initializer_array->getAsString());
781
782  LLDB_LOG(log, "Found Objective-C selector reference \"{0}\"",
783           omvn_initializer_string);
784
785  // Construct a call to sel_registerName
786
787  if (!m_sel_registerName) {
788    lldb::addr_t sel_registerName_addr;
789
790    bool missing_weak = false;
791    static lldb_private::ConstString g_sel_registerName_str("sel_registerName");
792    sel_registerName_addr = m_execution_unit.FindSymbol(g_sel_registerName_str,
793                                                        missing_weak);
794    if (sel_registerName_addr == LLDB_INVALID_ADDRESS || missing_weak)
795      return false;
796
797    LLDB_LOG(log, "Found sel_registerName at {0}", sel_registerName_addr);
798
799    // Build the function type: struct objc_selector
800    // *sel_registerName(uint8_t*)
801
802    // The below code would be "more correct," but in actuality what's required
803    // is uint8_t*
804    // Type *sel_type = StructType::get(m_module->getContext());
805    // Type *sel_ptr_type = PointerType::getUnqual(sel_type);
806    Type *sel_ptr_type = PointerType::getUnqual(m_module->getContext());
807
808    Type *type_array[1];
809
810    type_array[0] = llvm::PointerType::getUnqual(m_module->getContext());
811
812    ArrayRef<Type *> srN_arg_types(type_array, 1);
813
814    llvm::FunctionType *srN_type =
815        FunctionType::get(sel_ptr_type, srN_arg_types, false);
816
817    // Build the constant containing the pointer to the function
818    PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
819    Constant *srN_addr_int =
820        ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
821    m_sel_registerName = {srN_type,
822                          ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty)};
823  }
824
825  CallInst *srN_call =
826      CallInst::Create(m_sel_registerName, _objc_meth_var_name_,
827                       "sel_registerName", selector_load);
828
829  // Replace the load with the call in all users
830
831  selector_load->replaceAllUsesWith(srN_call);
832
833  selector_load->eraseFromParent();
834
835  return true;
836}
837
838bool IRForTarget::RewriteObjCSelectors(BasicBlock &basic_block) {
839  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
840
841  InstrList selector_loads;
842
843  for (Instruction &inst : basic_block) {
844    if (LoadInst *load = dyn_cast<LoadInst>(&inst))
845      if (IsObjCSelectorRef(load->getPointerOperand()))
846        selector_loads.push_back(&inst);
847  }
848
849  for (Instruction *inst : selector_loads) {
850    if (!RewriteObjCSelector(inst)) {
851      m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
852                            "static reference to an Objective-C selector to a "
853                            "dynamic reference\n");
854
855      LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C selector");
856
857      return false;
858    }
859  }
860
861  return true;
862}
863
864// This function does not report errors; its callers are responsible.
865bool IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc) {
866  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
867
868  AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
869
870  MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
871
872  if (!alloc_md || !alloc_md->getNumOperands())
873    return false;
874
875  ConstantInt *constant_int =
876      mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
877
878  if (!constant_int)
879    return false;
880
881  // We attempt to register this as a new persistent variable with the DeclMap.
882
883  uintptr_t ptr = constant_int->getZExtValue();
884
885  clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
886
887  lldb_private::TypeFromParser result_decl_type(
888      m_decl_map->GetTypeSystem()->GetType(decl->getType()));
889
890  StringRef decl_name(decl->getName());
891  lldb_private::ConstString persistent_variable_name(decl_name.data(),
892                                                     decl_name.size());
893  if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name,
894                                         result_decl_type, false, false))
895    return false;
896
897  GlobalVariable *persistent_global = new GlobalVariable(
898      (*m_module), alloc->getType(), false,  /* not constant */
899      GlobalValue::ExternalLinkage, nullptr, /* no initializer */
900      alloc->getName().str());
901
902  // What we're going to do here is make believe this was a regular old
903  // external variable.  That means we need to make the metadata valid.
904
905  NamedMDNode *named_metadata =
906      m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
907
908  llvm::Metadata *values[2];
909  values[0] = ConstantAsMetadata::get(persistent_global);
910  values[1] = ConstantAsMetadata::get(constant_int);
911
912  ArrayRef<llvm::Metadata *> value_ref(values, 2);
913
914  MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
915  named_metadata->addOperand(persistent_global_md);
916
917  // Now, since the variable is a pointer variable, we will drop in a load of
918  // that pointer variable.
919
920  LoadInst *persistent_load = new LoadInst(persistent_global->getValueType(),
921                                           persistent_global, "", alloc);
922
923  LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(alloc),
924           PrintValue(persistent_load));
925
926  alloc->replaceAllUsesWith(persistent_load);
927  alloc->eraseFromParent();
928
929  return true;
930}
931
932bool IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block) {
933  if (!m_resolve_vars)
934    return true;
935
936  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
937
938  InstrList pvar_allocs;
939
940  for (Instruction &inst : basic_block) {
941
942    if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst)) {
943      llvm::StringRef alloc_name = alloc->getName();
944
945      if (alloc_name.starts_with("$") && !alloc_name.starts_with("$__lldb")) {
946        if (alloc_name.find_first_of("0123456789") == 1) {
947          LLDB_LOG(log, "Rejecting a numeric persistent variable.");
948
949          m_error_stream.Printf("Error [IRForTarget]: Names starting with $0, "
950                                "$1, ... are reserved for use as result "
951                                "names\n");
952
953          return false;
954        }
955
956        pvar_allocs.push_back(alloc);
957      }
958    }
959  }
960
961  for (Instruction *inst : pvar_allocs) {
962    if (!RewritePersistentAlloc(inst)) {
963      m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
964                            "the creation of a persistent variable\n");
965
966      LLDB_LOG(log, "Couldn't rewrite the creation of a persistent variable");
967
968      return false;
969    }
970  }
971
972  return true;
973}
974
975// This function does not report errors; its callers are responsible.
976bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) {
977  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
978
979  LLDB_LOG(log, "MaybeHandleVariable ({0})", PrintValue(llvm_value_ptr));
980
981  if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr)) {
982    switch (constant_expr->getOpcode()) {
983    default:
984      break;
985    case Instruction::GetElementPtr:
986    case Instruction::BitCast:
987      Value *s = constant_expr->getOperand(0);
988      if (!MaybeHandleVariable(s))
989        return false;
990    }
991  } else if (GlobalVariable *global_variable =
992                 dyn_cast<GlobalVariable>(llvm_value_ptr)) {
993    if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
994      return true;
995
996    clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
997
998    if (!named_decl) {
999      if (IsObjCSelectorRef(llvm_value_ptr))
1000        return true;
1001
1002      if (!global_variable->hasExternalLinkage())
1003        return true;
1004
1005      LLDB_LOG(log, "Found global variable \"{0}\" without metadata",
1006               global_variable->getName());
1007
1008      return false;
1009    }
1010
1011    llvm::StringRef name(named_decl->getName());
1012
1013    clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1014    if (value_decl == nullptr)
1015      return false;
1016
1017    lldb_private::CompilerType compiler_type =
1018        m_decl_map->GetTypeSystem()->GetType(value_decl->getType());
1019
1020    const Type *value_type = nullptr;
1021
1022    if (name.starts_with("$")) {
1023      // The $__lldb_expr_result name indicates the return value has allocated
1024      // as a static variable.  Per the comment at
1025      // ASTResultSynthesizer::SynthesizeBodyResult, accesses to this static
1026      // variable need to be redirected to the result of dereferencing a
1027      // pointer that is passed in as one of the arguments.
1028      //
1029      // Consequently, when reporting the size of the type, we report a pointer
1030      // type pointing to the type of $__lldb_expr_result, not the type itself.
1031      //
1032      // We also do this for any user-declared persistent variables.
1033      compiler_type = compiler_type.GetPointerType();
1034      value_type = PointerType::get(global_variable->getType(), 0);
1035    } else {
1036      value_type = global_variable->getType();
1037    }
1038
1039    auto *target = m_execution_unit.GetTarget().get();
1040    std::optional<uint64_t> value_size = compiler_type.GetByteSize(target);
1041    if (!value_size)
1042      return false;
1043    std::optional<size_t> opt_alignment = compiler_type.GetTypeBitAlign(target);
1044    if (!opt_alignment)
1045      return false;
1046    lldb::offset_t value_alignment = (*opt_alignment + 7ull) / 8ull;
1047
1048    LLDB_LOG(log,
1049             "Type of \"{0}\" is [clang \"{1}\", llvm \"{2}\"] [size {3}, "
1050             "align {4}]",
1051             name,
1052             lldb_private::ClangUtil::GetQualType(compiler_type).getAsString(),
1053             PrintType(value_type), *value_size, value_alignment);
1054
1055    if (named_decl)
1056      m_decl_map->AddValueToStruct(named_decl, lldb_private::ConstString(name),
1057                                   llvm_value_ptr, *value_size,
1058                                   value_alignment);
1059  } else if (isa<llvm::Function>(llvm_value_ptr)) {
1060    LLDB_LOG(log, "Function pointers aren't handled right now");
1061
1062    return false;
1063  }
1064
1065  return true;
1066}
1067
1068// This function does not report errors; its callers are responsible.
1069bool IRForTarget::HandleSymbol(Value *symbol) {
1070  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1071
1072  lldb_private::ConstString name(symbol->getName().str().c_str());
1073
1074  lldb::addr_t symbol_addr =
1075      m_decl_map->GetSymbolAddress(name, lldb::eSymbolTypeAny);
1076
1077  if (symbol_addr == LLDB_INVALID_ADDRESS) {
1078    LLDB_LOG(log, "Symbol \"{0}\" had no address", name);
1079
1080    return false;
1081  }
1082
1083  LLDB_LOG(log, "Found \"{0}\" at {1}", name, symbol_addr);
1084
1085  Type *symbol_type = symbol->getType();
1086
1087  Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
1088
1089  Value *symbol_addr_ptr =
1090      ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1091
1092  LLDB_LOG(log, "Replacing {0} with {1}", PrintValue(symbol),
1093           PrintValue(symbol_addr_ptr));
1094
1095  symbol->replaceAllUsesWith(symbol_addr_ptr);
1096
1097  return true;
1098}
1099
1100bool IRForTarget::MaybeHandleCallArguments(CallInst *Old) {
1101  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1102
1103  LLDB_LOG(log, "MaybeHandleCallArguments({0})", PrintValue(Old));
1104
1105  for (unsigned op_index = 0, num_ops = Old->arg_size();
1106       op_index < num_ops; ++op_index)
1107    // conservatively believe that this is a store
1108    if (!MaybeHandleVariable(Old->getArgOperand(op_index))) {
1109      m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1110                            "one of the arguments of a function call.\n");
1111
1112      return false;
1113    }
1114
1115  return true;
1116}
1117
1118bool IRForTarget::HandleObjCClass(Value *classlist_reference) {
1119  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1120
1121  GlobalVariable *global_variable =
1122      dyn_cast<GlobalVariable>(classlist_reference);
1123
1124  if (!global_variable)
1125    return false;
1126
1127  Constant *initializer = global_variable->getInitializer();
1128
1129  if (!initializer)
1130    return false;
1131
1132  if (!initializer->hasName())
1133    return false;
1134
1135  StringRef name(initializer->getName());
1136  lldb_private::ConstString name_cstr(name.str().c_str());
1137  lldb::addr_t class_ptr =
1138      m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
1139
1140  LLDB_LOG(log, "Found reference to Objective-C class {0} ({1})", name,
1141           (unsigned long long)class_ptr);
1142
1143  if (class_ptr == LLDB_INVALID_ADDRESS)
1144    return false;
1145
1146  if (global_variable->use_empty())
1147    return false;
1148
1149  SmallVector<LoadInst *, 2> load_instructions;
1150
1151  for (llvm::User *u : global_variable->users()) {
1152    if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
1153      load_instructions.push_back(load_instruction);
1154  }
1155
1156  if (load_instructions.empty())
1157    return false;
1158
1159  Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
1160
1161  for (LoadInst *load_instruction : load_instructions) {
1162    Constant *class_bitcast =
1163        ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1164
1165    load_instruction->replaceAllUsesWith(class_bitcast);
1166
1167    load_instruction->eraseFromParent();
1168  }
1169
1170  return true;
1171}
1172
1173bool IRForTarget::RemoveCXAAtExit(BasicBlock &basic_block) {
1174  std::vector<CallInst *> calls_to_remove;
1175
1176  for (Instruction &inst : basic_block) {
1177    CallInst *call = dyn_cast<CallInst>(&inst);
1178
1179    // MaybeHandleCallArguments handles error reporting; we are silent here
1180    if (!call)
1181      continue;
1182
1183    bool remove = false;
1184
1185    llvm::Function *func = call->getCalledFunction();
1186
1187    if (func && func->getName() == "__cxa_atexit")
1188      remove = true;
1189
1190    llvm::Value *val = call->getCalledOperand();
1191
1192    if (val && val->getName() == "__cxa_atexit")
1193      remove = true;
1194
1195    if (remove)
1196      calls_to_remove.push_back(call);
1197  }
1198
1199  for (CallInst *ci : calls_to_remove)
1200    ci->eraseFromParent();
1201
1202  return true;
1203}
1204
1205bool IRForTarget::ResolveCalls(BasicBlock &basic_block) {
1206  // Prepare the current basic block for execution in the remote process
1207
1208  for (Instruction &inst : basic_block) {
1209    CallInst *call = dyn_cast<CallInst>(&inst);
1210
1211    // MaybeHandleCallArguments handles error reporting; we are silent here
1212    if (call && !MaybeHandleCallArguments(call))
1213      return false;
1214  }
1215
1216  return true;
1217}
1218
1219bool IRForTarget::ResolveExternals(Function &llvm_function) {
1220  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1221
1222  for (GlobalVariable &global_var : m_module->globals()) {
1223    llvm::StringRef global_name = global_var.getName();
1224
1225    LLDB_LOG(log, "Examining {0}, DeclForGlobalValue returns {1}", global_name,
1226             static_cast<void *>(DeclForGlobal(&global_var)));
1227
1228    if (global_name.starts_with("OBJC_IVAR")) {
1229      if (!HandleSymbol(&global_var)) {
1230        m_error_stream.Format("Error [IRForTarget]: Couldn't find Objective-C "
1231                              "indirect ivar symbol {0}\n",
1232                              global_name);
1233
1234        return false;
1235      }
1236    } else if (global_name.contains("OBJC_CLASSLIST_REFERENCES_$")) {
1237      if (!HandleObjCClass(&global_var)) {
1238        m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1239                              "for an Objective-C static method call\n");
1240
1241        return false;
1242      }
1243    } else if (global_name.contains("OBJC_CLASSLIST_SUP_REFS_$")) {
1244      if (!HandleObjCClass(&global_var)) {
1245        m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1246                              "for an Objective-C static method call\n");
1247
1248        return false;
1249      }
1250    } else if (DeclForGlobal(&global_var)) {
1251      if (!MaybeHandleVariable(&global_var)) {
1252        m_error_stream.Format("Internal error [IRForTarget]: Couldn't rewrite "
1253                              "external variable {0}\n",
1254                              global_name);
1255
1256        return false;
1257      }
1258    }
1259  }
1260
1261  return true;
1262}
1263
1264static bool isGuardVariableRef(Value *V) {
1265  GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
1266
1267  if (!GV || !GV->hasName() || !isGuardVariableSymbol(GV->getName()))
1268    return false;
1269
1270  return true;
1271}
1272
1273void IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction *guard_load) {
1274  Constant *zero(Constant::getNullValue(guard_load->getType()));
1275  guard_load->replaceAllUsesWith(zero);
1276  guard_load->eraseFromParent();
1277}
1278
1279static void ExciseGuardStore(Instruction *guard_store) {
1280  guard_store->eraseFromParent();
1281}
1282
1283bool IRForTarget::RemoveGuards(BasicBlock &basic_block) {
1284  // Eliminate any reference to guard variables found.
1285
1286  InstrList guard_loads;
1287  InstrList guard_stores;
1288
1289  for (Instruction &inst : basic_block) {
1290
1291    if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1292      if (isGuardVariableRef(load->getPointerOperand()))
1293        guard_loads.push_back(&inst);
1294
1295    if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1296      if (isGuardVariableRef(store->getPointerOperand()))
1297        guard_stores.push_back(&inst);
1298  }
1299
1300  for (Instruction *inst : guard_loads)
1301    TurnGuardLoadIntoZero(inst);
1302
1303  for (Instruction *inst : guard_stores)
1304    ExciseGuardStore(inst);
1305
1306  return true;
1307}
1308
1309// This function does not report errors; its callers are responsible.
1310bool IRForTarget::UnfoldConstant(Constant *old_constant,
1311                                 llvm::Function *llvm_function,
1312                                 FunctionValueCache &value_maker,
1313                                 FunctionValueCache &entry_instruction_finder,
1314                                 lldb_private::Stream &error_stream) {
1315  SmallVector<User *, 16> users;
1316
1317  // We do this because the use list might change, invalidating our iterator.
1318  // Much better to keep a work list ourselves.
1319  for (llvm::User *u : old_constant->users())
1320    users.push_back(u);
1321
1322  for (size_t i = 0; i < users.size(); ++i) {
1323    User *user = users[i];
1324
1325    if (Constant *constant = dyn_cast<Constant>(user)) {
1326      // synthesize a new non-constant equivalent of the constant
1327
1328      if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
1329        switch (constant_expr->getOpcode()) {
1330        default:
1331          error_stream.Printf("error [IRForTarget internal]: Unhandled "
1332                              "constant expression type: \"%s\"",
1333                              PrintValue(constant_expr).c_str());
1334          return false;
1335        case Instruction::BitCast: {
1336          FunctionValueCache bit_cast_maker(
1337              [&value_maker, &entry_instruction_finder, old_constant,
1338               constant_expr](llvm::Function *function) -> llvm::Value * {
1339                // UnaryExpr
1340                //   OperandList[0] is value
1341
1342                if (constant_expr->getOperand(0) != old_constant)
1343                  return constant_expr;
1344
1345                return new BitCastInst(
1346                    value_maker.GetValue(function), constant_expr->getType(),
1347                    "", llvm::cast<Instruction>(
1348                            entry_instruction_finder.GetValue(function)));
1349              });
1350
1351          if (!UnfoldConstant(constant_expr, llvm_function, bit_cast_maker,
1352                              entry_instruction_finder, error_stream))
1353            return false;
1354        } break;
1355        case Instruction::GetElementPtr: {
1356          // GetElementPtrConstantExpr
1357          //   OperandList[0] is base
1358          //   OperandList[1]... are indices
1359
1360          FunctionValueCache get_element_pointer_maker(
1361              [&value_maker, &entry_instruction_finder, old_constant,
1362               constant_expr](llvm::Function *function) -> llvm::Value * {
1363                auto *gep = cast<llvm::GEPOperator>(constant_expr);
1364                Value *ptr = gep->getPointerOperand();
1365
1366                if (ptr == old_constant)
1367                  ptr = value_maker.GetValue(function);
1368
1369                std::vector<Value *> index_vector;
1370                for (Value *operand : gep->indices()) {
1371                  if (operand == old_constant)
1372                    operand = value_maker.GetValue(function);
1373
1374                  index_vector.push_back(operand);
1375                }
1376
1377                ArrayRef<Value *> indices(index_vector);
1378
1379                return GetElementPtrInst::Create(
1380                    gep->getSourceElementType(), ptr, indices, "",
1381                    llvm::cast<Instruction>(
1382                        entry_instruction_finder.GetValue(function)));
1383              });
1384
1385          if (!UnfoldConstant(constant_expr, llvm_function,
1386                              get_element_pointer_maker,
1387                              entry_instruction_finder, error_stream))
1388            return false;
1389        } break;
1390        }
1391      } else {
1392        error_stream.Printf(
1393            "error [IRForTarget internal]: Unhandled constant type: \"%s\"",
1394            PrintValue(constant).c_str());
1395        return false;
1396      }
1397    } else {
1398      if (Instruction *inst = llvm::dyn_cast<Instruction>(user)) {
1399        if (llvm_function && inst->getParent()->getParent() != llvm_function) {
1400          error_stream.PutCString("error: Capturing non-local variables in "
1401                                  "expressions is unsupported.\n");
1402          return false;
1403        }
1404        inst->replaceUsesOfWith(
1405            old_constant, value_maker.GetValue(inst->getParent()->getParent()));
1406      } else {
1407        error_stream.Printf(
1408            "error [IRForTarget internal]: Unhandled non-constant type: \"%s\"",
1409            PrintValue(user).c_str());
1410        return false;
1411      }
1412    }
1413  }
1414
1415  if (!isa<GlobalValue>(old_constant)) {
1416    old_constant->destroyConstant();
1417  }
1418
1419  return true;
1420}
1421
1422bool IRForTarget::ReplaceVariables(Function &llvm_function) {
1423  if (!m_resolve_vars)
1424    return true;
1425
1426  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1427
1428  m_decl_map->DoStructLayout();
1429
1430  LLDB_LOG(log, "Element arrangement:");
1431
1432  uint32_t num_elements;
1433  uint32_t element_index;
1434
1435  size_t size;
1436  lldb::offset_t alignment;
1437
1438  if (!m_decl_map->GetStructInfo(num_elements, size, alignment))
1439    return false;
1440
1441  Function::arg_iterator iter(llvm_function.arg_begin());
1442
1443  if (iter == llvm_function.arg_end()) {
1444    m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes no "
1445                          "arguments (should take at least a struct pointer)");
1446
1447    return false;
1448  }
1449
1450  Argument *argument = &*iter;
1451
1452  if (argument->getName().equals("this")) {
1453    ++iter;
1454
1455    if (iter == llvm_function.arg_end()) {
1456      m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1457                            "'this' argument (should take a struct pointer "
1458                            "too)");
1459
1460      return false;
1461    }
1462
1463    argument = &*iter;
1464  } else if (argument->getName().equals("self")) {
1465    ++iter;
1466
1467    if (iter == llvm_function.arg_end()) {
1468      m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1469                            "'self' argument (should take '_cmd' and a struct "
1470                            "pointer too)");
1471
1472      return false;
1473    }
1474
1475    if (!iter->getName().equals("_cmd")) {
1476      m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes '{0}' "
1477                            "after 'self' argument (should take '_cmd')",
1478                            iter->getName());
1479
1480      return false;
1481    }
1482
1483    ++iter;
1484
1485    if (iter == llvm_function.arg_end()) {
1486      m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1487                            "'self' and '_cmd' arguments (should take a struct "
1488                            "pointer too)");
1489
1490      return false;
1491    }
1492
1493    argument = &*iter;
1494  }
1495
1496  if (!argument->getName().equals("$__lldb_arg")) {
1497    m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes an "
1498                          "argument named '{0}' instead of the struct pointer",
1499                          argument->getName());
1500
1501    return false;
1502  }
1503
1504  LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument));
1505
1506  BasicBlock &entry_block(llvm_function.getEntryBlock());
1507  Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
1508
1509  if (!FirstEntryInstruction) {
1510    m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "
1511                          "first instruction in the wrapper for use in "
1512                          "rewriting");
1513
1514    return false;
1515  }
1516
1517  LLVMContext &context(m_module->getContext());
1518  IntegerType *offset_type(Type::getInt32Ty(context));
1519
1520  if (!offset_type) {
1521    m_error_stream.Printf(
1522        "Internal error [IRForTarget]: Couldn't produce an offset type");
1523
1524    return false;
1525  }
1526
1527  for (element_index = 0; element_index < num_elements; ++element_index) {
1528    const clang::NamedDecl *decl = nullptr;
1529    Value *value = nullptr;
1530    lldb::offset_t offset;
1531    lldb_private::ConstString name;
1532
1533    if (!m_decl_map->GetStructElement(decl, value, offset, name,
1534                                      element_index)) {
1535      m_error_stream.Printf(
1536          "Internal error [IRForTarget]: Structure information is incomplete");
1537
1538      return false;
1539    }
1540
1541    LLDB_LOG(log, "  \"{0}\" (\"{1}\") placed at {2}", name,
1542             decl->getNameAsString(), offset);
1543
1544    if (value) {
1545      LLDB_LOG(log, "    Replacing [{0}]", PrintValue(value));
1546
1547      FunctionValueCache body_result_maker(
1548          [this, name, offset_type, offset, argument,
1549           value](llvm::Function *function) -> llvm::Value * {
1550            // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1551            // in cases where the result variable is an rvalue, we have to
1552            // synthesize a dereference of the appropriate structure entry in
1553            // order to produce the static variable that the AST thinks it is
1554            // accessing.
1555
1556            llvm::Instruction *entry_instruction = llvm::cast<Instruction>(
1557                m_entry_instruction_finder.GetValue(function));
1558
1559            Type *int8Ty = Type::getInt8Ty(function->getContext());
1560            ConstantInt *offset_int(
1561                ConstantInt::get(offset_type, offset, true));
1562            GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(
1563                int8Ty, argument, offset_int, "", entry_instruction);
1564
1565            if (name == m_result_name && !m_result_is_pointer) {
1566              LoadInst *load = new LoadInst(value->getType(), get_element_ptr,
1567                                            "", entry_instruction);
1568
1569              return load;
1570            } else {
1571              return get_element_ptr;
1572            }
1573          });
1574
1575      if (Constant *constant = dyn_cast<Constant>(value)) {
1576        if (!UnfoldConstant(constant, &llvm_function, body_result_maker,
1577                            m_entry_instruction_finder, m_error_stream)) {
1578          return false;
1579        }
1580      } else if (Instruction *instruction = dyn_cast<Instruction>(value)) {
1581        if (instruction->getParent()->getParent() != &llvm_function) {
1582          m_error_stream.PutCString("error: Capturing non-local variables in "
1583                                    "expressions is unsupported.\n");
1584          return false;
1585        }
1586        value->replaceAllUsesWith(
1587            body_result_maker.GetValue(instruction->getParent()->getParent()));
1588      } else {
1589        LLDB_LOG(log, "Unhandled non-constant type: \"{0}\"",
1590                 PrintValue(value));
1591        return false;
1592      }
1593
1594      if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1595        var->eraseFromParent();
1596    }
1597  }
1598
1599  LLDB_LOG(log, "Total structure [align {0}, size {1}]", (int64_t)alignment,
1600           (uint64_t)size);
1601
1602  return true;
1603}
1604
1605bool IRForTarget::runOnModule(Module &llvm_module) {
1606  lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1607
1608  m_module = &llvm_module;
1609  m_target_data = std::make_unique<DataLayout>(m_module);
1610  m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(),
1611                                      m_target_data->getPointerSizeInBits());
1612
1613  if (log) {
1614    std::string s;
1615    raw_string_ostream oss(s);
1616
1617    m_module->print(oss, nullptr);
1618
1619    oss.flush();
1620
1621    LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s);
1622  }
1623
1624  Function *const main_function =
1625      m_func_name.IsEmpty() ? nullptr
1626                            : m_module->getFunction(m_func_name.GetStringRef());
1627
1628  if (!m_func_name.IsEmpty() && !main_function) {
1629    LLDB_LOG(log, "Couldn't find \"{0}()\" in the module", m_func_name);
1630
1631    m_error_stream.Format("Internal error [IRForTarget]: Couldn't find wrapper "
1632                          "'{0}' in the module",
1633                          m_func_name);
1634
1635    return false;
1636  }
1637
1638  if (main_function) {
1639    if (!FixFunctionLinkage(*main_function)) {
1640      LLDB_LOG(log, "Couldn't fix the linkage for the function");
1641
1642      return false;
1643    }
1644  }
1645
1646  ////////////////////////////////////////////////////////////
1647  // Replace $__lldb_expr_result with a persistent variable
1648  //
1649
1650  if (main_function) {
1651    if (!CreateResultVariable(*main_function)) {
1652      LLDB_LOG(log, "CreateResultVariable() failed");
1653
1654      // CreateResultVariable() reports its own errors, so we don't do so here
1655
1656      return false;
1657    }
1658  }
1659
1660  if (log && log->GetVerbose()) {
1661    std::string s;
1662    raw_string_ostream oss(s);
1663
1664    m_module->print(oss, nullptr);
1665
1666    oss.flush();
1667
1668    LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s);
1669  }
1670
1671  for (llvm::Function &function : *m_module) {
1672    for (BasicBlock &bb : function) {
1673      if (!RemoveGuards(bb)) {
1674        LLDB_LOG(log, "RemoveGuards() failed");
1675
1676        // RemoveGuards() reports its own errors, so we don't do so here
1677
1678        return false;
1679      }
1680
1681      if (!RewritePersistentAllocs(bb)) {
1682        LLDB_LOG(log, "RewritePersistentAllocs() failed");
1683
1684        // RewritePersistentAllocs() reports its own errors, so we don't do so
1685        // here
1686
1687        return false;
1688      }
1689
1690      if (!RemoveCXAAtExit(bb)) {
1691        LLDB_LOG(log, "RemoveCXAAtExit() failed");
1692
1693        // RemoveCXAAtExit() reports its own errors, so we don't do so here
1694
1695        return false;
1696      }
1697    }
1698  }
1699
1700  ///////////////////////////////////////////////////////////////////////////////
1701  // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
1702  //
1703
1704  if (!RewriteObjCConstStrings()) {
1705    LLDB_LOG(log, "RewriteObjCConstStrings() failed");
1706
1707    // RewriteObjCConstStrings() reports its own errors, so we don't do so here
1708
1709    return false;
1710  }
1711
1712  for (llvm::Function &function : *m_module) {
1713    for (llvm::BasicBlock &bb : function) {
1714      if (!RewriteObjCSelectors(bb)) {
1715        LLDB_LOG(log, "RewriteObjCSelectors() failed");
1716
1717        // RewriteObjCSelectors() reports its own errors, so we don't do so
1718        // here
1719
1720        return false;
1721      }
1722    }
1723  }
1724
1725  for (llvm::Function &function : *m_module) {
1726    for (BasicBlock &bb : function) {
1727      if (!ResolveCalls(bb)) {
1728        LLDB_LOG(log, "ResolveCalls() failed");
1729
1730        // ResolveCalls() reports its own errors, so we don't do so here
1731
1732        return false;
1733      }
1734    }
1735  }
1736
1737  ////////////////////////////////////////////////////////////////////////
1738  // Run function-level passes that only make sense on the main function
1739  //
1740
1741  if (main_function) {
1742    if (!ResolveExternals(*main_function)) {
1743      LLDB_LOG(log, "ResolveExternals() failed");
1744
1745      // ResolveExternals() reports its own errors, so we don't do so here
1746
1747      return false;
1748    }
1749
1750    if (!ReplaceVariables(*main_function)) {
1751      LLDB_LOG(log, "ReplaceVariables() failed");
1752
1753      // ReplaceVariables() reports its own errors, so we don't do so here
1754
1755      return false;
1756    }
1757  }
1758
1759  if (log && log->GetVerbose()) {
1760    std::string s;
1761    raw_string_ostream oss(s);
1762
1763    m_module->print(oss, nullptr);
1764
1765    oss.flush();
1766
1767    LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s);
1768  }
1769
1770  return true;
1771}
1772