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