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