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 : public llvm::ModulePass {
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   /// Destructor
91   ~IRForTarget() override;
92 
93   /// Run this IR transformer on a single module
94   ///
95   /// Implementation of the llvm::ModulePass::runOnModule() function.
96   ///
97   /// \param[in] llvm_module
98   ///     The module to run on.  This module is searched for the function
99   ///     $__lldb_expr, and that function is passed to the passes one by
100   ///     one.
101   ///
102   /// \return
103   ///     True on success; false otherwise
104   bool runOnModule(llvm::Module &llvm_module) override;
105 
106   /// Interface stub
107   ///
108   /// Implementation of the llvm::ModulePass::assignPassManager() function.
109   void assignPassManager(llvm::PMStack &pass_mgr_stack,
110                          llvm::PassManagerType pass_mgr_type =
111                              llvm::PMT_ModulePassManager) override;
112 
113   /// Returns PMT_ModulePassManager
114   ///
115   /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
116   /// function.
117   llvm::PassManagerType getPotentialPassManagerType() const override;
118 
119 private:
120   /// Ensures that the current function's linkage is set to external.
121   /// Otherwise the JIT may not return an address for it.
122   ///
123   /// \param[in] llvm_function
124   ///     The function whose linkage is to be fixed.
125   ///
126   /// \return
127   ///     True on success; false otherwise.
128   bool FixFunctionLinkage(llvm::Function &llvm_function);
129 
130   /// A module-level pass to replace all function pointers with their
131   /// integer equivalents.
132 
133   /// The top-level pass implementation
134   ///
135   /// \param[in] llvm_function
136   ///     The function currently being processed.
137   ///
138   /// \return
139   ///     True on success; false otherwise.
140   bool HasSideEffects(llvm::Function &llvm_function);
141 
142   /// A function-level pass to check whether the function has side
143   /// effects.
144 
145   /// Get the address of a function, and a location to put the complete Value
146   /// of the function if one is available.
147   ///
148   /// \param[in] function
149   ///     The function to find the location of.
150   ///
151   /// \param[out] ptr
152   ///     The location of the function in the target.
153   ///
154   /// \param[out] name
155   ///     The resolved name of the function (matters for intrinsics).
156   ///
157   /// \param[out] value_ptr
158   ///     A variable to put the function's completed Value* in, or NULL
159   ///     if the Value* shouldn't be stored anywhere.
160   ///
161   /// \return
162   ///     The pointer.
163   LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
164                                   lldb_private::ConstString &name,
165                                   llvm::Constant **&value_ptr);
166 
167   /// A function-level pass to take the generated global value
168   /// $__lldb_expr_result and make it into a persistent variable. Also see
169   /// ASTResultSynthesizer.
170 
171   /// Find the NamedDecl corresponding to a Value.  This interface is exposed
172   /// for the IR interpreter.
173   ///
174   /// \param[in] global_val
175   ///     The global entity to search for
176   ///
177   /// \param[in] module
178   ///     The module containing metadata to search
179   ///
180   /// \return
181   ///     The corresponding variable declaration
182 public:
183   static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
184                                          llvm::Module *module);
185 
186 private:
187   clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
188 
189   /// Set the constant result variable m_const_result to the provided
190   /// constant, assuming it can be evaluated.  The result variable will be
191   /// reset to NULL later if the expression has side effects.
192   ///
193   /// \param[in] initializer
194   ///     The constant initializer for the variable.
195   ///
196   /// \param[in] name
197   ///     The name of the result variable.
198   ///
199   /// \param[in] type
200   ///     The Clang type of the result variable.
201   void MaybeSetConstantResult(llvm::Constant *initializer,
202                               lldb_private::ConstString name,
203                               lldb_private::TypeFromParser type);
204 
205   /// If the IR represents a cast of a variable, set m_const_result to the
206   /// result of the cast.  The result variable will be reset to
207   /// NULL latger if the expression has side effects.
208   ///
209   /// \param[in] type
210   ///     The Clang type of the result variable.
211   void MaybeSetCastResult(lldb_private::TypeFromParser type);
212 
213   /// The top-level pass implementation
214   ///
215   /// \param[in] llvm_function
216   ///     The function currently being processed.
217   ///
218   /// \return
219   ///     True on success; false otherwise
220   bool CreateResultVariable(llvm::Function &llvm_function);
221 
222   /// A module-level pass to find Objective-C constant strings and
223   /// transform them to calls to CFStringCreateWithBytes.
224 
225   /// Rewrite a single Objective-C constant string.
226   ///
227   /// \param[in] NSStr
228   ///     The constant NSString to be transformed
229   ///
230   /// \param[in] CStr
231   ///     The constant C string inside the NSString.  This will be
232   ///     passed as the bytes argument to CFStringCreateWithBytes.
233   ///
234   /// \return
235   ///     True on success; false otherwise
236   bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
237                               llvm::GlobalVariable *CStr);
238 
239   /// The top-level pass implementation
240   ///
241   /// \return
242   ///     True on success; false otherwise
243   bool RewriteObjCConstStrings();
244 
245   /// A basic block-level pass to find all Objective-C method calls and
246   /// rewrite them to use sel_registerName instead of statically allocated
247   /// selectors.  The reason is that the selectors are created on the
248   /// assumption that the Objective-C runtime will scan the appropriate
249   /// section and prepare them.  This doesn't happen when code is copied into
250   /// the target, though, and there's no easy way to induce the runtime to
251   /// scan them.  So instead we get our selectors from sel_registerName.
252 
253   /// Replace a single selector reference
254   ///
255   /// \param[in] selector_load
256   ///     The load of the statically-allocated selector.
257   ///
258   /// \return
259   ///     True on success; false otherwise
260   bool RewriteObjCSelector(llvm::Instruction *selector_load);
261 
262   /// The top-level pass implementation
263   ///
264   /// \param[in] basic_block
265   ///     The basic block currently being processed.
266   ///
267   /// \return
268   ///     True on success; false otherwise
269   bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
270 
271   /// A basic block-level pass to find all Objective-C class references that
272   /// use the old-style Objective-C runtime and rewrite them to use
273   /// class_getClass instead of statically allocated class references.
274 
275   /// Replace a single old-style class reference
276   ///
277   /// \param[in] class_load
278   ///     The load of the statically-allocated selector.
279   ///
280   /// \return
281   ///     True on success; false otherwise
282   bool RewriteObjCClassReference(llvm::Instruction *class_load);
283 
284   /// The top-level pass implementation
285   ///
286   /// \param[in] basic_block
287   ///     The basic block currently being processed.
288   ///
289   /// \return
290   ///     True on success; false otherwise
291   bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block);
292 
293   /// A basic block-level pass to find all newly-declared persistent
294   /// variables and register them with the ClangExprDeclMap.  This allows them
295   /// to be materialized and dematerialized like normal external variables.
296   /// Before transformation, these persistent variables look like normal
297   /// locals, so they have an allocation. This pass excises these allocations
298   /// and makes references look like external references where they will be
299   /// resolved -- like all other external references -- by ResolveExternals().
300 
301   /// Handle a single allocation of a persistent variable
302   ///
303   /// \param[in] persistent_alloc
304   ///     The allocation of the persistent variable.
305   ///
306   /// \return
307   ///     True on success; false otherwise
308   bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
309 
310   /// The top-level pass implementation
311   ///
312   /// \param[in] basic_block
313   ///     The basic block currently being processed.
314   bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
315 
316   /// A function-level pass to find all external variables and functions
317   /// used in the IR.  Each found external variable is added to the struct,
318   /// and each external function is resolved in place, its call replaced with
319   /// a call to a function pointer whose value is the address of the function
320   /// in the target process.
321 
322   /// Handle a single externally-defined variable
323   ///
324   /// \param[in] value
325   ///     The variable.
326   ///
327   /// \return
328   ///     True on success; false otherwise
329   bool MaybeHandleVariable(llvm::Value *value);
330 
331   /// Handle a single externally-defined symbol
332   ///
333   /// \param[in] symbol
334   ///     The symbol.
335   ///
336   /// \return
337   ///     True on success; false otherwise
338   bool HandleSymbol(llvm::Value *symbol);
339 
340   /// Handle a single externally-defined Objective-C class
341   ///
342   /// \param[in] classlist_reference
343   ///     The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
344   ///     where n (if present) is an index.
345   ///
346   /// \return
347   ///     True on success; false otherwise
348   bool HandleObjCClass(llvm::Value *classlist_reference);
349 
350   /// Handle all the arguments to a function call
351   ///
352   /// \param[in] call_inst
353   ///     The call instruction.
354   ///
355   /// \return
356   ///     True on success; false otherwise
357   bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
358 
359   /// Resolve variable references in calls to external functions
360   ///
361   /// \param[in] basic_block
362   ///     The basic block currently being processed.
363   ///
364   /// \return
365   ///     True on success; false otherwise
366   bool ResolveCalls(llvm::BasicBlock &basic_block);
367 
368   /// Remove calls to __cxa_atexit, which should never be generated by
369   /// expressions.
370   ///
371   /// \param[in] basic_block
372   ///     The basic block currently being processed.
373   ///
374   /// \return
375   ///     True if the scan was successful; false if some operation
376   ///     failed
377   bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
378 
379   /// The top-level pass implementation
380   ///
381   /// \param[in] llvm_function
382   ///     The function currently being processed.
383   ///
384   /// \return
385   ///     True on success; false otherwise
386   bool ResolveExternals(llvm::Function &llvm_function);
387 
388   /// A basic block-level pass to excise guard variables from the code.
389   /// The result for the function is passed through Clang as a static
390   /// variable.  Static variables normally have guard variables to ensure that
391   /// they are only initialized once.
392 
393   /// Rewrite a load to a guard variable to return constant 0.
394   ///
395   /// \param[in] guard_load
396   ///     The load instruction to zero out.
397   void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
398 
399   /// The top-level pass implementation
400   ///
401   /// \param[in] basic_block
402   ///     The basic block currently being processed.
403   ///
404   /// \return
405   ///     True on success; false otherwise
406   bool RemoveGuards(llvm::BasicBlock &basic_block);
407 
408   /// A function-level pass to make all external variable references
409   /// point at the correct offsets from the void* passed into the function.
410   /// ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so
411   /// that the offsets are valid.
412 
413   /// The top-level pass implementation
414   ///
415   /// \param[in] llvm_function
416   ///     The function currently being processed.
417   ///
418   /// \return
419   ///     True on success; false otherwise
420   bool ReplaceVariables(llvm::Function &llvm_function);
421 
422   /// Flags
423   bool m_resolve_vars; ///< True if external variable references and persistent
424                        ///variable references should be resolved
425   lldb_private::ConstString
426       m_func_name; ///< The name of the function to translate
427   lldb_private::ConstString
428       m_result_name; ///< The name of the result variable ($0, $1, ...)
429   lldb_private::TypeFromParser
430       m_result_type;      ///< The type of the result variable.
431   llvm::Module *m_module; ///< The module being processed, or NULL if that has
432                           ///not been determined yet.
433   std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the
434                                                    ///module being processed, or
435                                                    ///NULL if there is no
436                                                    ///module.
437   lldb_private::ClangExpressionDeclMap
438       *m_decl_map; ///< The DeclMap containing the Decls
439   llvm::FunctionCallee
440       m_CFStringCreateWithBytes; ///< The address of the function
441                                  /// CFStringCreateWithBytes, cast to the
442                                  /// appropriate function pointer type
443   llvm::FunctionCallee m_sel_registerName; ///< The address of the function
444                                            /// sel_registerName, cast to the
445                                            /// appropriate function pointer type
446   llvm::FunctionCallee m_objc_getClass; ///< The address of the function
447                                         /// objc_getClass, cast to the
448                                         /// appropriate function pointer type
449   llvm::IntegerType
450       *m_intptr_ty; ///< The type of an integer large enough to hold a pointer.
451   lldb_private::Stream
452       &m_error_stream; ///< The stream on which errors should be printed
453   lldb_private::IRExecutionUnit &
454       m_execution_unit; ///< The execution unit containing the IR being created.
455 
456   llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that
457                                    ///writes to the result variable.  If
458                                    /// m_has_side_effects is true, this is
459                                    /// NULL.
460   bool m_result_is_pointer; ///< True if the function's result in the AST is a
461                             ///pointer (see comments in
462                             /// ASTResultSynthesizer::SynthesizeBodyResult)
463 
464   /// A placeholder that will be replaced by a pointer to the final location of
465   /// the static allocation.
466   llvm::GlobalVariable *m_reloc_placeholder;
467 
468   class FunctionValueCache {
469   public:
470     typedef std::function<llvm::Value *(llvm::Function *)> Maker;
471 
472     FunctionValueCache(Maker const &maker);
473     ~FunctionValueCache();
474     llvm::Value *GetValue(llvm::Function *function);
475 
476   private:
477     Maker const m_maker;
478     typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
479     FunctionValueMap m_values;
480   };
481 
482   FunctionValueCache m_entry_instruction_finder;
483 
484   /// UnfoldConstant operates on a constant [Old] which has just been replaced
485   /// with a value [New].  We assume that new_value has been properly placed
486   /// early in the function, in front of the first instruction in the entry
487   /// basic block [FirstEntryInstruction].
488   ///
489   /// UnfoldConstant reads through the uses of Old and replaces Old in those
490   /// uses with New.  Where those uses are constants, the function generates
491   /// new instructions to compute the result of the new, non-constant
492   /// expression and places them before FirstEntryInstruction.  These
493   /// instructions replace the constant uses, so UnfoldConstant calls itself
494   /// recursively for those.
495   ///
496   /// \return
497   ///     True on success; false otherwise
498   static bool UnfoldConstant(llvm::Constant *old_constant,
499                              llvm::Function *llvm_function,
500                              FunctionValueCache &value_maker,
501                              FunctionValueCache &entry_instruction_finder,
502                              lldb_private::Stream &error_stream);
503 
504   /// Commit the allocation in m_data_allocator and use its final location to
505   /// replace m_reloc_placeholder.
506   ///
507   /// \return
508   ///     True on success; false otherwise
509   bool CompleteDataAllocation();
510 };
511 
512 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
513