Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ExecutionUtils.h - Utilities for executing code in Orc ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Contains utilities for executing code in Orc. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H |
| 15 | #define LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H |
| 16 | |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/iterator_range.h" |
| 19 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 20 | #include "llvm/ExecutionEngine/Orc/Core.h" |
| 21 | #include "llvm/ExecutionEngine/Orc/OrcError.h" |
| 22 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 23 | #include "llvm/Support/DynamicLibrary.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <cstdint> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | class ConstantArray; |
| 33 | class GlobalVariable; |
| 34 | class Function; |
| 35 | class Module; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 36 | class TargetMachine; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | class Value; |
| 38 | |
| 39 | namespace orc { |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// This iterator provides a convenient way to iterate over the elements |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | /// of an llvm.global_ctors/llvm.global_dtors instance. |
| 43 | /// |
| 44 | /// The easiest way to get hold of instances of this class is to use the |
| 45 | /// getConstructors/getDestructors functions. |
| 46 | class CtorDtorIterator { |
| 47 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | /// Accessor for an element of the global_ctors/global_dtors array. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | /// |
| 50 | /// This class provides a read-only view of the element with any casts on |
| 51 | /// the function stripped away. |
| 52 | struct Element { |
| 53 | Element(unsigned Priority, Function *Func, Value *Data) |
| 54 | : Priority(Priority), Func(Func), Data(Data) {} |
| 55 | |
| 56 | unsigned Priority; |
| 57 | Function *Func; |
| 58 | Value *Data; |
| 59 | }; |
| 60 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | /// Construct an iterator instance. If End is true then this iterator |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 62 | /// acts as the end of the range, otherwise it is the beginning. |
| 63 | CtorDtorIterator(const GlobalVariable *GV, bool End); |
| 64 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 65 | /// Test iterators for equality. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 66 | bool operator==(const CtorDtorIterator &Other) const; |
| 67 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | /// Test iterators for inequality. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | bool operator!=(const CtorDtorIterator &Other) const; |
| 70 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// Pre-increment iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | CtorDtorIterator& operator++(); |
| 73 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 74 | /// Post-increment iterator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 75 | CtorDtorIterator operator++(int); |
| 76 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 77 | /// Dereference iterator. The resulting value provides a read-only view |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | /// of this element of the global_ctors/global_dtors list. |
| 79 | Element operator*() const; |
| 80 | |
| 81 | private: |
| 82 | const ConstantArray *InitList; |
| 83 | unsigned I; |
| 84 | }; |
| 85 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | /// Create an iterator range over the entries of the llvm.global_ctors |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | /// array. |
| 88 | iterator_range<CtorDtorIterator> getConstructors(const Module &M); |
| 89 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 90 | /// Create an iterator range over the entries of the llvm.global_ctors |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | /// array. |
| 92 | iterator_range<CtorDtorIterator> getDestructors(const Module &M); |
| 93 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 94 | /// Convenience class for recording constructor/destructor names for |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | /// later execution. |
| 96 | template <typename JITLayerT> |
| 97 | class CtorDtorRunner { |
| 98 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 99 | /// Construct a CtorDtorRunner for the given range using the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 100 | /// name mangling function. |
| 101 | CtorDtorRunner(std::vector<std::string> CtorDtorNames, VModuleKey K) |
| 102 | : CtorDtorNames(std::move(CtorDtorNames)), K(K) {} |
| 103 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 104 | /// Run the recorded constructors/destructors through the given JIT |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 105 | /// layer. |
| 106 | Error runViaLayer(JITLayerT &JITLayer) const { |
| 107 | using CtorDtorTy = void (*)(); |
| 108 | |
| 109 | for (const auto &CtorDtorName : CtorDtorNames) { |
| 110 | if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) { |
| 111 | if (auto AddrOrErr = CtorDtorSym.getAddress()) { |
| 112 | CtorDtorTy CtorDtor = |
| 113 | reinterpret_cast<CtorDtorTy>(static_cast<uintptr_t>(*AddrOrErr)); |
| 114 | CtorDtor(); |
| 115 | } else |
| 116 | return AddrOrErr.takeError(); |
| 117 | } else { |
| 118 | if (auto Err = CtorDtorSym.takeError()) |
| 119 | return Err; |
| 120 | else |
| 121 | return make_error<JITSymbolNotFound>(CtorDtorName); |
| 122 | } |
| 123 | } |
| 124 | return Error::success(); |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | std::vector<std::string> CtorDtorNames; |
| 129 | orc::VModuleKey K; |
| 130 | }; |
| 131 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 132 | class CtorDtorRunner2 { |
| 133 | public: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 134 | CtorDtorRunner2(JITDylib &JD) : JD(JD) {} |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 135 | void add(iterator_range<CtorDtorIterator> CtorDtors); |
| 136 | Error run(); |
| 137 | |
| 138 | private: |
| 139 | using CtorDtorList = std::vector<SymbolStringPtr>; |
| 140 | using CtorDtorPriorityMap = std::map<unsigned, CtorDtorList>; |
| 141 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 142 | JITDylib &JD; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 143 | CtorDtorPriorityMap CtorDtorsByPriority; |
| 144 | }; |
| 145 | |
| 146 | /// Support class for static dtor execution. For hosted (in-process) JITs |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 147 | /// only! |
| 148 | /// |
| 149 | /// If a __cxa_atexit function isn't found C++ programs that use static |
| 150 | /// destructors will fail to link. However, we don't want to use the host |
| 151 | /// process's __cxa_atexit, because it will schedule JIT'd destructors to run |
| 152 | /// after the JIT has been torn down, which is no good. This class makes it easy |
| 153 | /// to override __cxa_atexit (and the related __dso_handle). |
| 154 | /// |
| 155 | /// To use, clients should manually call searchOverrides from their symbol |
| 156 | /// resolver. This should generally be done after attempting symbol resolution |
| 157 | /// inside the JIT, but before searching the host process's symbol table. When |
| 158 | /// the client determines that destructors should be run (generally at JIT |
| 159 | /// teardown or after a return from main), the runDestructors method should be |
| 160 | /// called. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 161 | class LocalCXXRuntimeOverridesBase { |
| 162 | public: |
| 163 | /// Run any destructors recorded by the overriden __cxa_atexit function |
| 164 | /// (CXAAtExitOverride). |
| 165 | void runDestructors(); |
| 166 | |
| 167 | protected: |
| 168 | template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy *P) { |
| 169 | return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P)); |
| 170 | } |
| 171 | |
| 172 | using DestructorPtr = void (*)(void *); |
| 173 | using CXXDestructorDataPair = std::pair<DestructorPtr, void *>; |
| 174 | using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>; |
| 175 | CXXDestructorDataPairList DSOHandleOverride; |
| 176 | static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg, |
| 177 | void *DSOHandle); |
| 178 | }; |
| 179 | |
| 180 | class LocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 181 | public: |
| 182 | /// Create a runtime-overrides class. |
| 183 | template <typename MangleFtorT> |
| 184 | LocalCXXRuntimeOverrides(const MangleFtorT &Mangle) { |
| 185 | addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride)); |
| 186 | addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride)); |
| 187 | } |
| 188 | |
| 189 | /// Search overrided symbols. |
| 190 | JITEvaluatedSymbol searchOverrides(const std::string &Name) { |
| 191 | auto I = CXXRuntimeOverrides.find(Name); |
| 192 | if (I != CXXRuntimeOverrides.end()) |
| 193 | return JITEvaluatedSymbol(I->second, JITSymbolFlags::Exported); |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 197 | private: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 198 | void addOverride(const std::string &Name, JITTargetAddress Addr) { |
| 199 | CXXRuntimeOverrides.insert(std::make_pair(Name, Addr)); |
| 200 | } |
| 201 | |
| 202 | StringMap<JITTargetAddress> CXXRuntimeOverrides; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 203 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 204 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 205 | class LocalCXXRuntimeOverrides2 : public LocalCXXRuntimeOverridesBase { |
| 206 | public: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 207 | Error enable(JITDylib &JD, MangleAndInterner &Mangler); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | /// A utility class to expose symbols found via dlsym to the JIT. |
| 211 | /// |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 212 | /// If an instance of this class is attached to a JITDylib as a fallback |
| 213 | /// definition generator, then any symbol found in the given DynamicLibrary that |
| 214 | /// passes the 'Allow' predicate will be added to the JITDylib. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 215 | class DynamicLibraryFallbackGenerator { |
| 216 | public: |
| 217 | using SymbolPredicate = std::function<bool(SymbolStringPtr)>; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 218 | |
| 219 | static bool AllowAll(SymbolStringPtr Name) { return true; } |
| 220 | |
| 221 | /// Create a DynamicLibraryFallbackGenerator that searches for symbols in the |
| 222 | /// given sys::DynamicLibrary. |
| 223 | /// Only symbols that match the 'Allow' predicate will be searched for. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 224 | DynamicLibraryFallbackGenerator(sys::DynamicLibrary Dylib, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 225 | const DataLayout &DL, |
| 226 | SymbolPredicate Allow = AllowAll); |
| 227 | |
| 228 | /// Permanently loads the library at the given path and, on success, returns |
| 229 | /// a DynamicLibraryFallbackGenerator that will search it for symbol |
| 230 | /// definitions matching the Allow predicate. |
| 231 | /// On failure returns the reason the library failed to load. |
| 232 | static Expected<DynamicLibraryFallbackGenerator> |
| 233 | Load(const char *FileName, const DataLayout &DL, |
| 234 | SymbolPredicate Allow = AllowAll); |
| 235 | |
| 236 | /// Creates a DynamicLibraryFallbackGenerator that searches for symbols in |
| 237 | /// the current process. |
| 238 | static Expected<DynamicLibraryFallbackGenerator> |
| 239 | CreateForCurrentProcess(const DataLayout &DL, |
| 240 | SymbolPredicate Allow = AllowAll) { |
| 241 | return Load(nullptr, DL, std::move(Allow)); |
| 242 | } |
| 243 | |
| 244 | SymbolNameSet operator()(JITDylib &JD, const SymbolNameSet &Names); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 245 | |
| 246 | private: |
| 247 | sys::DynamicLibrary Dylib; |
| 248 | SymbolPredicate Allow; |
| 249 | char GlobalPrefix; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // end namespace orc |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 253 | } // end namespace llvm |
| 254 | |
| 255 | #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H |