1 //===-- IRForTarget.h ---------------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
11 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
12 
13 #include "lldb/Symbol/TaggedASTType.h"
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/Utility/StreamString.h"
18 #include "lldb/lldb-public.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/Pass.h"
21 
22 #include <functional>
23 #include <map>
24 
25 namespace llvm {
26 class BasicBlock;
27 class CallInst;
28 class Constant;
29 class ConstantInt;
30 class Function;
31 class GlobalValue;
32 class GlobalVariable;
33 class Instruction;
34 class Module;
35 class StoreInst;
36 class DataLayout;
37 class Value;
38 }
39 
40 namespace clang {
41 class NamedDecl;
42 }
43 
44 namespace lldb_private {
45 class ClangExpressionDeclMap;
46 class IRExecutionUnit;
47 class IRMemoryMap;
48 }
49 
50 /// \class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
51 /// Transforms the IR for a function to run in the target
52 ///
53 /// Once an expression has been parsed and converted to IR, it can run in two
54 /// contexts: interpreted by LLDB as a DWARF location expression, or compiled
55 /// by the JIT and inserted into the target process for execution.
56 ///
57 /// IRForTarget makes the second possible, by applying a series of
58 /// transformations to the IR which make it relocatable.  These
59 /// transformations are discussed in more detail next to their relevant
60 /// functions.
61 class IRForTarget {
62 public:
63   enum class LookupResult { Success, Fail, Ignore };
64 
65   /// Constructor
66   ///
67   /// \param[in] decl_map
68   ///     The list of externally-referenced variables for the expression,
69   ///     for use in looking up globals and allocating the argument
70   ///     struct.  See the documentation for ClangExpressionDeclMap.
71   ///
72   /// \param[in] resolve_vars
73   ///     True if the external variable references (including persistent
74   ///     variables) should be resolved.  If not, only external functions
75   ///     are resolved.
76   ///
77   /// \param[in] execution_unit
78   ///     The holder for raw data associated with the expression.
79   ///
80   /// \param[in] error_stream
81   ///     If non-NULL, a stream on which errors can be printed.
82   ///
83   /// \param[in] func_name
84   ///     The name of the function to prepare for execution in the target.
85   IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars,
86               lldb_private::IRExecutionUnit &execution_unit,
87               lldb_private::Stream &error_stream,
88               const char *func_name = "$__lldb_expr");
89 
90   /// Run this IR transformer on a single module
91   ///
92   /// Implementation of the llvm::ModulePass::runOnModule() function.
93   ///
94   /// \param[in] llvm_module
95   ///     The module to run on.  This module is searched for the function
96   ///     $__lldb_expr, and that function is passed to the passes one by
97   ///     one.
98   ///
99   /// \return
100   ///     True on success; false otherwise
101   bool runOnModule(llvm::Module &llvm_module);
102 
103 private:
104   /// Ensures that the current function's linkage is set to external.
105   /// Otherwise the JIT may not return an address for it.
106   ///
107   /// \param[in] llvm_function
108   ///     The function whose linkage is to be fixed.
109   ///
110   /// \return
111   ///     True on success; false otherwise.
112   bool FixFunctionLinkage(llvm::Function &llvm_function);
113 
114   /// A module-level pass to replace all function pointers with their
115   /// integer equivalents.
116 
117   /// The top-level pass implementation
118   ///
119   /// \param[in] llvm_function
120   ///     The function currently being processed.
121   ///
122   /// \return
123   ///     True on success; false otherwise.
124   bool HasSideEffects(llvm::Function &llvm_function);
125 
126   /// A function-level pass to check whether the function has side
127   /// effects.
128 
129   /// Get the address of a function, and a location to put the complete Value
130   /// of the function if one is available.
131   ///
132   /// \param[in] function
133   ///     The function to find the location of.
134   ///
135   /// \param[out] ptr
136   ///     The location of the function in the target.
137   ///
138   /// \param[out] name
139   ///     The resolved name of the function (matters for intrinsics).
140   ///
141   /// \param[out] value_ptr
142   ///     A variable to put the function's completed Value* in, or NULL
143   ///     if the Value* shouldn't be stored anywhere.
144   ///
145   /// \return
146   ///     The pointer.
147   LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
148                                   lldb_private::ConstString &name,
149                                   llvm::Constant **&value_ptr);
150 
151   /// A function-level pass to take the generated global value
152   /// $__lldb_expr_result and make it into a persistent variable. Also see
153   /// ASTResultSynthesizer.
154 
155   /// Find the NamedDecl corresponding to a Value.  This interface is exposed
156   /// for the IR interpreter.
157   ///
158   /// \param[in] global_val
159   ///     The global entity to search for
160   ///
161   /// \param[in] module
162   ///     The module containing metadata to search
163   ///
164   /// \return
165   ///     The corresponding variable declaration
166 public:
167   static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
168                                          llvm::Module *module);
169 
170 private:
171   clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
172 
173   /// Set the constant result variable m_const_result to the provided
174   /// constant, assuming it can be evaluated.  The result variable will be
175   /// reset to NULL later if the expression has side effects.
176   ///
177   /// \param[in] initializer
178   ///     The constant initializer for the variable.
179   ///
180   /// \param[in] name
181   ///     The name of the result variable.
182   ///
183   /// \param[in] type
184   ///     The Clang type of the result variable.
185   void MaybeSetConstantResult(llvm::Constant *initializer,
186                               lldb_private::ConstString name,
187                               lldb_private::TypeFromParser type);
188 
189   /// If the IR represents a cast of a variable, set m_const_result to the
190   /// result of the cast.  The result variable will be reset to
191   /// NULL latger if the expression has side effects.
192   ///
193   /// \param[in] type
194   ///     The Clang type of the result variable.
195   void MaybeSetCastResult(lldb_private::TypeFromParser type);
196 
197   /// The top-level pass implementation
198   ///
199   /// \param[in] llvm_function
200   ///     The function currently being processed.
201   ///
202   /// \return
203   ///     True on success; false otherwise
204   bool CreateResultVariable(llvm::Function &llvm_function);
205 
206   /// A module-level pass to find Objective-C constant strings and
207   /// transform them to calls to CFStringCreateWithBytes.
208 
209   /// Rewrite a single Objective-C constant string.
210   ///
211   /// \param[in] NSStr
212   ///     The constant NSString to be transformed
213   ///
214   /// \param[in] CStr
215   ///     The constant C string inside the NSString.  This will be
216   ///     passed as the bytes argument to CFStringCreateWithBytes.
217   ///
218   /// \return
219   ///     True on success; false otherwise
220   bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
221                               llvm::GlobalVariable *CStr);
222 
223   /// The top-level pass implementation
224   ///
225   /// \return
226   ///     True on success; false otherwise
227   bool RewriteObjCConstStrings();
228 
229   /// A basic block-level pass to find all Objective-C method calls and
230   /// rewrite them to use sel_registerName instead of statically allocated
231   /// selectors.  The reason is that the selectors are created on the
232   /// assumption that the Objective-C runtime will scan the appropriate
233   /// section and prepare them.  This doesn't happen when code is copied into
234   /// the target, though, and there's no easy way to induce the runtime to
235   /// scan them.  So instead we get our selectors from sel_registerName.
236 
237   /// Replace a single selector reference
238   ///
239   /// \param[in] selector_load
240   ///     The load of the statically-allocated selector.
241   ///
242   /// \return
243   ///     True on success; false otherwise
244   bool RewriteObjCSelector(llvm::Instruction *selector_load);
245 
246   /// The top-level pass implementation
247   ///
248   /// \param[in] basic_block
249   ///     The basic block currently being processed.
250   ///
251   /// \return
252   ///     True on success; false otherwise
253   bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
254 
255   /// A basic block-level pass to find all newly-declared persistent
256   /// variables and register them with the ClangExprDeclMap.  This allows them
257   /// to be materialized and dematerialized like normal external variables.
258   /// Before transformation, these persistent variables look like normal
259   /// locals, so they have an allocation. This pass excises these allocations
260   /// and makes references look like external references where they will be
261   /// resolved -- like all other external references -- by ResolveExternals().
262 
263   /// Handle a single allocation of a persistent variable
264   ///
265   /// \param[in] persistent_alloc
266   ///     The allocation of the persistent variable.
267   ///
268   /// \return
269   ///     True on success; false otherwise
270   bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
271 
272   /// The top-level pass implementation
273   ///
274   /// \param[in] basic_block
275   ///     The basic block currently being processed.
276   bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
277 
278   /// A function-level pass to find all external variables and functions
279   /// used in the IR.  Each found external variable is added to the struct,
280   /// and each external function is resolved in place, its call replaced with
281   /// a call to a function pointer whose value is the address of the function
282   /// in the target process.
283 
284   /// Handle a single externally-defined variable
285   ///
286   /// \param[in] value
287   ///     The variable.
288   ///
289   /// \return
290   ///     True on success; false otherwise
291   bool MaybeHandleVariable(llvm::Value *value);
292 
293   /// Handle a single externally-defined symbol
294   ///
295   /// \param[in] symbol
296   ///     The symbol.
297   ///
298   /// \return
299   ///     True on success; false otherwise
300   bool HandleSymbol(llvm::Value *symbol);
301 
302   /// Handle a single externally-defined Objective-C class
303   ///
304   /// \param[in] classlist_reference
305   ///     The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
306   ///     where n (if present) is an index.
307   ///
308   /// \return
309   ///     True on success; false otherwise
310   bool HandleObjCClass(llvm::Value *classlist_reference);
311 
312   /// Handle all the arguments to a function call
313   ///
314   /// \param[in] call_inst
315   ///     The call instruction.
316   ///
317   /// \return
318   ///     True on success; false otherwise
319   bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
320 
321   /// Resolve variable references in calls to external functions
322   ///
323   /// \param[in] basic_block
324   ///     The basic block currently being processed.
325   ///
326   /// \return
327   ///     True on success; false otherwise
328   bool ResolveCalls(llvm::BasicBlock &basic_block);
329 
330   /// Remove calls to __cxa_atexit, which should never be generated by
331   /// expressions.
332   ///
333   /// \param[in] basic_block
334   ///     The basic block currently being processed.
335   ///
336   /// \return
337   ///     True if the scan was successful; false if some operation
338   ///     failed
339   bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
340 
341   /// The top-level pass implementation
342   ///
343   /// \param[in] llvm_function
344   ///     The function currently being processed.
345   ///
346   /// \return
347   ///     True on success; false otherwise
348   bool ResolveExternals(llvm::Function &llvm_function);
349 
350   /// A basic block-level pass to excise guard variables from the code.
351   /// The result for the function is passed through Clang as a static
352   /// variable.  Static variables normally have guard variables to ensure that
353   /// they are only initialized once.
354 
355   /// Rewrite a load to a guard variable to return constant 0.
356   ///
357   /// \param[in] guard_load
358   ///     The load instruction to zero out.
359   void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
360 
361   /// The top-level pass implementation
362   ///
363   /// \param[in] basic_block
364   ///     The basic block currently being processed.
365   ///
366   /// \return
367   ///     True on success; false otherwise
368   bool RemoveGuards(llvm::BasicBlock &basic_block);
369 
370   /// A function-level pass to make all external variable references
371   /// point at the correct offsets from the void* passed into the function.
372   /// ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so
373   /// that the offsets are valid.
374 
375   /// The top-level pass implementation
376   ///
377   /// \param[in] llvm_function
378   ///     The function currently being processed.
379   ///
380   /// \return
381   ///     True on success; false otherwise
382   bool ReplaceVariables(llvm::Function &llvm_function);
383 
384   /// True if external variable references and persistent variable references
385   /// should be resolved
386   bool m_resolve_vars;
387   /// The name of the function to translate
388   lldb_private::ConstString m_func_name;
389   /// The name of the result variable ($0, $1, ...)
390   lldb_private::ConstString m_result_name;
391   /// The type of the result variable.
392   lldb_private::TypeFromParser m_result_type;
393   /// The module being processed, or NULL if that has not been determined yet.
394   llvm::Module *m_module = nullptr;
395   /// The target data for the module being processed, or NULL if there is no
396   /// module.
397   std::unique_ptr<llvm::DataLayout> m_target_data;
398   /// The DeclMap containing the Decls
399   lldb_private::ClangExpressionDeclMap *m_decl_map;
400   /// The address of the function CFStringCreateWithBytes, cast to the
401   /// appropriate function pointer type
402   llvm::FunctionCallee m_CFStringCreateWithBytes;
403   /// The address of the function sel_registerName, cast to the appropriate
404   /// function pointer type.
405   llvm::FunctionCallee m_sel_registerName;
406   /// The type of an integer large enough to hold a pointer.
407   llvm::IntegerType *m_intptr_ty = nullptr;
408   /// The stream on which errors should be printed.
409   lldb_private::Stream &m_error_stream;
410   /// The execution unit containing the IR being created.
411   lldb_private::IRExecutionUnit &m_execution_unit;
412   /// If non-NULL, the store instruction that writes to the result variable.  If
413   /// m_has_side_effects is true, this is NULL.
414   llvm::StoreInst *m_result_store = nullptr;
415   /// True if the function's result in the AST is a pointer (see comments in
416   /// ASTResultSynthesizer::SynthesizeBodyResult)
417   bool m_result_is_pointer = false;
418   /// A placeholder that will be replaced by a pointer to the final location of
419   /// the static allocation.
420   llvm::GlobalVariable *m_reloc_placeholder = nullptr;
421 
422   class FunctionValueCache {
423   public:
424     typedef std::function<llvm::Value *(llvm::Function *)> Maker;
425 
426     FunctionValueCache(Maker const &maker);
427     ~FunctionValueCache();
428     llvm::Value *GetValue(llvm::Function *function);
429 
430   private:
431     Maker const m_maker;
432     typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
433     FunctionValueMap m_values;
434   };
435 
436   FunctionValueCache m_entry_instruction_finder;
437 
438   /// UnfoldConstant operates on a constant [Old] which has just been replaced
439   /// with a value [New].  We assume that new_value has been properly placed
440   /// early in the function, in front of the first instruction in the entry
441   /// basic block [FirstEntryInstruction].
442   ///
443   /// UnfoldConstant reads through the uses of Old and replaces Old in those
444   /// uses with New.  Where those uses are constants, the function generates
445   /// new instructions to compute the result of the new, non-constant
446   /// expression and places them before FirstEntryInstruction.  These
447   /// instructions replace the constant uses, so UnfoldConstant calls itself
448   /// recursively for those.
449   ///
450   /// \return
451   ///     True on success; false otherwise
452   static bool UnfoldConstant(llvm::Constant *old_constant,
453                              llvm::Function *llvm_function,
454                              FunctionValueCache &value_maker,
455                              FunctionValueCache &entry_instruction_finder,
456                              lldb_private::Stream &error_stream);
457 
458   /// Commit the allocation in m_data_allocator and use its final location to
459   /// replace m_reloc_placeholder.
460   ///
461   /// \return
462   ///     True on success; false otherwise
463   bool CompleteDataAllocation();
464 };
465 
466 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
467