File: | llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h |
Warning: | line 182, column 11 Called function pointer is null (null dereference) |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===// | |||
2 | // | |||
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 | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | ||||
9 | #include "OrcCBindingsStack.h" | |||
10 | #include "llvm-c/OrcBindings.h" | |||
11 | #include "llvm/ExecutionEngine/JITEventListener.h" | |||
12 | ||||
13 | using namespace llvm; | |||
14 | ||||
15 | LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) { | |||
16 | TargetMachine *TM2(unwrap(TM)); | |||
17 | ||||
18 | Triple T(TM2->getTargetTriple()); | |||
19 | ||||
20 | auto IndirectStubsMgrBuilder = | |||
21 | orc::createLocalIndirectStubsManagerBuilder(T); | |||
22 | ||||
23 | OrcCBindingsStack *JITStack = | |||
24 | new OrcCBindingsStack(*TM2, std::move(IndirectStubsMgrBuilder)); | |||
25 | ||||
26 | return wrap(JITStack); | |||
27 | } | |||
28 | ||||
29 | const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) { | |||
30 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
31 | return J.getErrorMessage().c_str(); | |||
32 | } | |||
33 | ||||
34 | void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName, | |||
35 | const char *SymbolName) { | |||
36 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
37 | std::string Mangled = J.mangle(SymbolName); | |||
38 | *MangledName = new char[Mangled.size() + 1]; | |||
39 | strcpy(*MangledName, Mangled.c_str()); | |||
40 | } | |||
41 | ||||
42 | void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; } | |||
43 | ||||
44 | LLVMErrorRef LLVMOrcCreateLazyCompileCallback( | |||
45 | LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr, | |||
46 | LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx) { | |||
47 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
48 | if (auto Addr = J.createLazyCompileCallback(Callback, CallbackCtx)) { | |||
49 | *RetAddr = *Addr; | |||
50 | return LLVMErrorSuccess0; | |||
51 | } else | |||
52 | return wrap(Addr.takeError()); | |||
53 | } | |||
54 | ||||
55 | LLVMErrorRef LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack, | |||
56 | const char *StubName, | |||
57 | LLVMOrcTargetAddress InitAddr) { | |||
58 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
59 | return wrap(J.createIndirectStub(StubName, InitAddr)); | |||
60 | } | |||
61 | ||||
62 | LLVMErrorRef LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack, | |||
63 | const char *StubName, | |||
64 | LLVMOrcTargetAddress NewAddr) { | |||
65 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
66 | return wrap(J.setIndirectStubPointer(StubName, NewAddr)); | |||
67 | } | |||
68 | ||||
69 | LLVMErrorRef LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, | |||
70 | LLVMOrcModuleHandle *RetHandle, | |||
71 | LLVMModuleRef Mod, | |||
72 | LLVMOrcSymbolResolverFn SymbolResolver, | |||
73 | void *SymbolResolverCtx) { | |||
74 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
75 | std::unique_ptr<Module> M(unwrap(Mod)); | |||
76 | if (auto Handle = | |||
77 | J.addIRModuleEager(std::move(M), SymbolResolver, SymbolResolverCtx)) { | |||
78 | *RetHandle = *Handle; | |||
79 | return LLVMErrorSuccess0; | |||
80 | } else | |||
81 | return wrap(Handle.takeError()); | |||
82 | } | |||
83 | ||||
84 | LLVMErrorRef LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, | |||
85 | LLVMOrcModuleHandle *RetHandle, | |||
86 | LLVMModuleRef Mod, | |||
87 | LLVMOrcSymbolResolverFn SymbolResolver, | |||
88 | void *SymbolResolverCtx) { | |||
89 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
90 | std::unique_ptr<Module> M(unwrap(Mod)); | |||
91 | if (auto Handle = | |||
92 | J.addIRModuleLazy(std::move(M), SymbolResolver, SymbolResolverCtx)) { | |||
93 | *RetHandle = *Handle; | |||
94 | return LLVMErrorSuccess0; | |||
95 | } else | |||
96 | return wrap(Handle.takeError()); | |||
97 | } | |||
98 | ||||
99 | LLVMErrorRef LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack, | |||
100 | LLVMOrcModuleHandle *RetHandle, | |||
101 | LLVMMemoryBufferRef Obj, | |||
102 | LLVMOrcSymbolResolverFn SymbolResolver, | |||
103 | void *SymbolResolverCtx) { | |||
104 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
105 | std::unique_ptr<MemoryBuffer> O(unwrap(Obj)); | |||
106 | if (auto Handle = | |||
107 | J.addObject(std::move(O), SymbolResolver, SymbolResolverCtx)) { | |||
108 | *RetHandle = *Handle; | |||
109 | return LLVMErrorSuccess0; | |||
110 | } else | |||
111 | return wrap(Handle.takeError()); | |||
112 | } | |||
113 | ||||
114 | LLVMErrorRef LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, | |||
115 | LLVMOrcModuleHandle H) { | |||
116 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
117 | return wrap(J.removeModule(H)); | |||
118 | } | |||
119 | ||||
120 | LLVMErrorRef LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack, | |||
121 | LLVMOrcTargetAddress *RetAddr, | |||
122 | const char *SymbolName) { | |||
123 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
124 | if (auto Addr = J.findSymbolAddress(SymbolName, true)) { | |||
125 | *RetAddr = *Addr; | |||
126 | return LLVMErrorSuccess0; | |||
127 | } else | |||
128 | return wrap(Addr.takeError()); | |||
129 | } | |||
130 | ||||
131 | LLVMErrorRef LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack, | |||
132 | LLVMOrcTargetAddress *RetAddr, | |||
133 | LLVMOrcModuleHandle H, | |||
134 | const char *SymbolName) { | |||
135 | OrcCBindingsStack &J = *unwrap(JITStack); | |||
136 | if (auto Addr = J.findSymbolAddressIn(H, SymbolName, true)) { | |||
137 | *RetAddr = *Addr; | |||
138 | return LLVMErrorSuccess0; | |||
139 | } else | |||
140 | return wrap(Addr.takeError()); | |||
141 | } | |||
142 | ||||
143 | LLVMErrorRef LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) { | |||
144 | auto *J = unwrap(JITStack); | |||
145 | auto Err = J->shutdown(); | |||
| ||||
146 | delete J; | |||
147 | return wrap(std::move(Err)); | |||
148 | } | |||
149 | ||||
150 | void LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L) | |||
151 | { | |||
152 | unwrap(JITStack)->RegisterJITEventListener(unwrap(L)); | |||
153 | } | |||
154 | ||||
155 | void LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L) | |||
156 | { | |||
157 | unwrap(JITStack)->UnregisterJITEventListener(unwrap(L)); | |||
158 | } |
1 | //===- OrcCBindingsStack.h - Orc JIT stack for C bindings -----*- C++ -*---===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H |
10 | #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H |
11 | |
12 | #include "llvm-c/OrcBindings.h" |
13 | #include "llvm-c/TargetMachine.h" |
14 | #include "llvm/ADT/STLExtras.h" |
15 | #include "llvm/ADT/StringRef.h" |
16 | #include "llvm/ExecutionEngine/JITSymbol.h" |
17 | #include "llvm/ExecutionEngine/JITEventListener.h" |
18 | #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" |
19 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
20 | #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" |
21 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
22 | #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" |
23 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
24 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
25 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
26 | #include "llvm/IR/DataLayout.h" |
27 | #include "llvm/IR/Mangler.h" |
28 | #include "llvm/IR/Module.h" |
29 | #include "llvm/Support/CBindingWrapping.h" |
30 | #include "llvm/Support/Error.h" |
31 | #include "llvm/Support/raw_ostream.h" |
32 | #include "llvm/Target/TargetMachine.h" |
33 | #include <algorithm> |
34 | #include <cstdint> |
35 | #include <functional> |
36 | #include <map> |
37 | #include <memory> |
38 | #include <set> |
39 | #include <string> |
40 | #include <vector> |
41 | |
42 | namespace llvm { |
43 | |
44 | class OrcCBindingsStack; |
45 | |
46 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)inline OrcCBindingsStack *unwrap(LLVMOrcJITStackRef P) { return reinterpret_cast<OrcCBindingsStack*>(P); } inline LLVMOrcJITStackRef wrap(const OrcCBindingsStack *P) { return reinterpret_cast< LLVMOrcJITStackRef>(const_cast<OrcCBindingsStack*>(P )); } |
47 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)inline TargetMachine *unwrap(LLVMTargetMachineRef P) { return reinterpret_cast<TargetMachine*>(P); } inline LLVMTargetMachineRef wrap(const TargetMachine *P) { return reinterpret_cast<LLVMTargetMachineRef >(const_cast<TargetMachine*>(P)); } |
48 | |
49 | namespace detail { |
50 | |
51 | // FIXME: Kill this off once the Layer concept becomes an interface. |
52 | class GenericLayer { |
53 | public: |
54 | virtual ~GenericLayer() = default; |
55 | |
56 | virtual JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, |
57 | bool ExportedSymbolsOnly) = 0; |
58 | virtual Error removeModule(orc::VModuleKey K) = 0; |
59 | }; |
60 | |
61 | template <typename LayerT> class GenericLayerImpl : public GenericLayer { |
62 | public: |
63 | GenericLayerImpl(LayerT &Layer) : Layer(Layer) {} |
64 | |
65 | JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, |
66 | bool ExportedSymbolsOnly) override { |
67 | return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly); |
68 | } |
69 | |
70 | Error removeModule(orc::VModuleKey K) override { |
71 | return Layer.removeModule(K); |
72 | } |
73 | |
74 | private: |
75 | LayerT &Layer; |
76 | }; |
77 | |
78 | template <> |
79 | class GenericLayerImpl<orc::LegacyRTDyldObjectLinkingLayer> : public GenericLayer { |
80 | private: |
81 | using LayerT = orc::LegacyRTDyldObjectLinkingLayer; |
82 | public: |
83 | GenericLayerImpl(LayerT &Layer) : Layer(Layer) {} |
84 | |
85 | JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, |
86 | bool ExportedSymbolsOnly) override { |
87 | return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly); |
88 | } |
89 | |
90 | Error removeModule(orc::VModuleKey K) override { |
91 | return Layer.removeObject(K); |
92 | } |
93 | |
94 | private: |
95 | LayerT &Layer; |
96 | }; |
97 | |
98 | template <typename LayerT> |
99 | std::unique_ptr<GenericLayerImpl<LayerT>> createGenericLayer(LayerT &Layer) { |
100 | return std::make_unique<GenericLayerImpl<LayerT>>(Layer); |
101 | } |
102 | |
103 | } // end namespace detail |
104 | |
105 | class OrcCBindingsStack { |
106 | public: |
107 | |
108 | using CompileCallbackMgr = orc::JITCompileCallbackManager; |
109 | using ObjLayerT = orc::LegacyRTDyldObjectLinkingLayer; |
110 | using CompileLayerT = orc::LegacyIRCompileLayer<ObjLayerT, orc::SimpleCompiler>; |
111 | using CODLayerT = |
112 | orc::LegacyCompileOnDemandLayer<CompileLayerT, CompileCallbackMgr>; |
113 | |
114 | using CallbackManagerBuilder = |
115 | std::function<std::unique_ptr<CompileCallbackMgr>()>; |
116 | |
117 | using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT; |
118 | |
119 | private: |
120 | |
121 | using OwningObject = object::OwningBinary<object::ObjectFile>; |
122 | |
123 | class CBindingsResolver : public orc::SymbolResolver { |
124 | public: |
125 | CBindingsResolver(OrcCBindingsStack &Stack, |
126 | LLVMOrcSymbolResolverFn ExternalResolver, |
127 | void *ExternalResolverCtx) |
128 | : Stack(Stack), ExternalResolver(std::move(ExternalResolver)), |
129 | ExternalResolverCtx(std::move(ExternalResolverCtx)) {} |
130 | |
131 | orc::SymbolNameSet |
132 | getResponsibilitySet(const orc::SymbolNameSet &Symbols) override { |
133 | orc::SymbolNameSet Result; |
134 | |
135 | for (auto &S : Symbols) { |
136 | if (auto Sym = findSymbol(std::string(*S))) { |
137 | if (!Sym.getFlags().isStrong()) |
138 | Result.insert(S); |
139 | } else if (auto Err = Sym.takeError()) { |
140 | Stack.reportError(std::move(Err)); |
141 | return orc::SymbolNameSet(); |
142 | } |
143 | } |
144 | |
145 | return Result; |
146 | } |
147 | |
148 | orc::SymbolNameSet |
149 | lookup(std::shared_ptr<orc::AsynchronousSymbolQuery> Query, |
150 | orc::SymbolNameSet Symbols) override { |
151 | orc::SymbolNameSet UnresolvedSymbols; |
152 | |
153 | for (auto &S : Symbols) { |
154 | if (auto Sym = findSymbol(std::string(*S))) { |
155 | if (auto Addr = Sym.getAddress()) { |
156 | Query->notifySymbolMetRequiredState( |
157 | S, JITEvaluatedSymbol(*Addr, Sym.getFlags())); |
158 | } else { |
159 | Stack.ES.legacyFailQuery(*Query, Addr.takeError()); |
160 | return orc::SymbolNameSet(); |
161 | } |
162 | } else if (auto Err = Sym.takeError()) { |
163 | Stack.ES.legacyFailQuery(*Query, std::move(Err)); |
164 | return orc::SymbolNameSet(); |
165 | } else |
166 | UnresolvedSymbols.insert(S); |
167 | } |
168 | |
169 | if (Query->isComplete()) |
170 | Query->handleComplete(); |
171 | |
172 | return UnresolvedSymbols; |
173 | } |
174 | |
175 | private: |
176 | JITSymbol findSymbol(const std::string &Name) { |
177 | // Search order: |
178 | // 1. JIT'd symbols. |
179 | // 2. Runtime overrides. |
180 | // 3. External resolver (if present). |
181 | |
182 | if (Stack.CODLayer) { |
183 | if (auto Sym = Stack.CODLayer->findSymbol(Name, true)) |
184 | return Sym; |
185 | else if (auto Err = Sym.takeError()) |
186 | return Sym.takeError(); |
187 | } else { |
188 | if (auto Sym = Stack.CompileLayer.findSymbol(Name, true)) |
189 | return Sym; |
190 | else if (auto Err = Sym.takeError()) |
191 | return Sym.takeError(); |
192 | } |
193 | |
194 | if (auto Sym = Stack.CXXRuntimeOverrides.searchOverrides(Name)) |
195 | return Sym; |
196 | |
197 | if (ExternalResolver) |
198 | return JITSymbol(ExternalResolver(Name.c_str(), ExternalResolverCtx), |
199 | JITSymbolFlags::Exported); |
200 | |
201 | return JITSymbol(nullptr); |
202 | } |
203 | |
204 | OrcCBindingsStack &Stack; |
205 | LLVMOrcSymbolResolverFn ExternalResolver; |
206 | void *ExternalResolverCtx = nullptr; |
207 | }; |
208 | |
209 | public: |
210 | OrcCBindingsStack(TargetMachine &TM, |
211 | IndirectStubsManagerBuilder IndirectStubsMgrBuilder) |
212 | : CCMgr(createCompileCallbackManager(TM, ES)), DL(TM.createDataLayout()), |
213 | IndirectStubsMgr(IndirectStubsMgrBuilder()), |
214 | ObjectLayer( |
215 | AcknowledgeORCv1Deprecation, ES, |
216 | [this](orc::VModuleKey K) { |
217 | auto ResolverI = Resolvers.find(K); |
218 | assert(ResolverI != Resolvers.end() &&((ResolverI != Resolvers.end() && "No resolver for module K" ) ? static_cast<void> (0) : __assert_fail ("ResolverI != Resolvers.end() && \"No resolver for module K\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h" , 219, __PRETTY_FUNCTION__)) |
219 | "No resolver for module K")((ResolverI != Resolvers.end() && "No resolver for module K" ) ? static_cast<void> (0) : __assert_fail ("ResolverI != Resolvers.end() && \"No resolver for module K\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h" , 219, __PRETTY_FUNCTION__)); |
220 | auto Resolver = std::move(ResolverI->second); |
221 | Resolvers.erase(ResolverI); |
222 | return ObjLayerT::Resources{ |
223 | std::make_shared<SectionMemoryManager>(), Resolver}; |
224 | }, |
225 | nullptr, |
226 | [this](orc::VModuleKey K, const object::ObjectFile &Obj, |
227 | const RuntimeDyld::LoadedObjectInfo &LoadedObjInfo) { |
228 | this->notifyFinalized(K, Obj, LoadedObjInfo); |
229 | }, |
230 | [this](orc::VModuleKey K, const object::ObjectFile &Obj) { |
231 | this->notifyFreed(K, Obj); |
232 | }), |
233 | CompileLayer(AcknowledgeORCv1Deprecation, ObjectLayer, |
234 | orc::SimpleCompiler(TM)), |
235 | CODLayer(createCODLayer(ES, CompileLayer, CCMgr.get(), |
236 | std::move(IndirectStubsMgrBuilder), Resolvers)), |
237 | CXXRuntimeOverrides( |
238 | AcknowledgeORCv1Deprecation, |
239 | [this](const std::string &S) { return mangle(S); }) {} |
240 | |
241 | Error shutdown() { |
242 | // Run any destructors registered with __cxa_atexit. |
243 | CXXRuntimeOverrides.runDestructors(); |
244 | // Run any IR destructors. |
245 | for (auto &DtorRunner : IRStaticDestructorRunners) |
246 | if (auto Err = DtorRunner.runViaLayer(*this)) |
247 | return Err; |
248 | return Error::success(); |
249 | } |
250 | |
251 | std::string mangle(StringRef Name) { |
252 | std::string MangledName; |
253 | { |
254 | raw_string_ostream MangledNameStream(MangledName); |
255 | Mangler::getNameWithPrefix(MangledNameStream, Name, DL); |
256 | } |
257 | return MangledName; |
258 | } |
259 | |
260 | template <typename PtrTy> |
261 | static PtrTy fromTargetAddress(JITTargetAddress Addr) { |
262 | return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr)); |
263 | } |
264 | |
265 | Expected<JITTargetAddress> |
266 | createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback, |
267 | void *CallbackCtx) { |
268 | auto WrappedCallback = [=]() -> JITTargetAddress { |
269 | return Callback(wrap(this), CallbackCtx); |
270 | }; |
271 | |
272 | return CCMgr->getCompileCallback(std::move(WrappedCallback)); |
273 | } |
274 | |
275 | Error createIndirectStub(StringRef StubName, JITTargetAddress Addr) { |
276 | return IndirectStubsMgr->createStub(StubName, Addr, |
277 | JITSymbolFlags::Exported); |
278 | } |
279 | |
280 | Error setIndirectStubPointer(StringRef Name, JITTargetAddress Addr) { |
281 | return IndirectStubsMgr->updatePointer(Name, Addr); |
282 | } |
283 | |
284 | template <typename LayerT> |
285 | Expected<orc::VModuleKey> |
286 | addIRModule(LayerT &Layer, std::unique_ptr<Module> M, |
287 | std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr, |
288 | LLVMOrcSymbolResolverFn ExternalResolver, |
289 | void *ExternalResolverCtx) { |
290 | |
291 | // Attach a data-layout if one isn't already present. |
292 | if (M->getDataLayout().isDefault()) |
293 | M->setDataLayout(DL); |
294 | |
295 | // Record the static constructors and destructors. We have to do this before |
296 | // we hand over ownership of the module to the JIT. |
297 | std::vector<std::string> CtorNames, DtorNames; |
298 | for (auto Ctor : orc::getConstructors(*M)) |
299 | CtorNames.push_back(mangle(Ctor.Func->getName())); |
300 | for (auto Dtor : orc::getDestructors(*M)) |
301 | DtorNames.push_back(mangle(Dtor.Func->getName())); |
302 | |
303 | // Add the module to the JIT. |
304 | auto K = ES.allocateVModule(); |
305 | Resolvers[K] = std::make_shared<CBindingsResolver>(*this, ExternalResolver, |
306 | ExternalResolverCtx); |
307 | if (auto Err = Layer.addModule(K, std::move(M))) |
308 | return std::move(Err); |
309 | |
310 | KeyLayers[K] = detail::createGenericLayer(Layer); |
311 | |
312 | // Run the static constructors, and save the static destructor runner for |
313 | // execution when the JIT is torn down. |
314 | orc::LegacyCtorDtorRunner<OrcCBindingsStack> CtorRunner( |
315 | AcknowledgeORCv1Deprecation, std::move(CtorNames), K); |
316 | if (auto Err = CtorRunner.runViaLayer(*this)) |
317 | return std::move(Err); |
318 | |
319 | IRStaticDestructorRunners.emplace_back(AcknowledgeORCv1Deprecation, |
320 | std::move(DtorNames), K); |
321 | |
322 | return K; |
323 | } |
324 | |
325 | Expected<orc::VModuleKey> |
326 | addIRModuleEager(std::unique_ptr<Module> M, |
327 | LLVMOrcSymbolResolverFn ExternalResolver, |
328 | void *ExternalResolverCtx) { |
329 | return addIRModule(CompileLayer, std::move(M), |
330 | std::make_unique<SectionMemoryManager>(), |
331 | std::move(ExternalResolver), ExternalResolverCtx); |
332 | } |
333 | |
334 | Expected<orc::VModuleKey> |
335 | addIRModuleLazy(std::unique_ptr<Module> M, |
336 | LLVMOrcSymbolResolverFn ExternalResolver, |
337 | void *ExternalResolverCtx) { |
338 | if (!CODLayer) |
339 | return make_error<StringError>("Can not add lazy module: No compile " |
340 | "callback manager available", |
341 | inconvertibleErrorCode()); |
342 | |
343 | return addIRModule(*CODLayer, std::move(M), |
344 | std::make_unique<SectionMemoryManager>(), |
345 | std::move(ExternalResolver), ExternalResolverCtx); |
346 | } |
347 | |
348 | Error removeModule(orc::VModuleKey K) { |
349 | // FIXME: Should error release the module key? |
350 | if (auto Err = KeyLayers[K]->removeModule(K)) |
351 | return Err; |
352 | ES.releaseVModule(K); |
353 | KeyLayers.erase(K); |
354 | return Error::success(); |
355 | } |
356 | |
357 | Expected<orc::VModuleKey> addObject(std::unique_ptr<MemoryBuffer> ObjBuffer, |
358 | LLVMOrcSymbolResolverFn ExternalResolver, |
359 | void *ExternalResolverCtx) { |
360 | if (auto Obj = object::ObjectFile::createObjectFile( |
361 | ObjBuffer->getMemBufferRef())) { |
362 | |
363 | auto K = ES.allocateVModule(); |
364 | Resolvers[K] = std::make_shared<CBindingsResolver>( |
365 | *this, ExternalResolver, ExternalResolverCtx); |
366 | |
367 | if (auto Err = ObjectLayer.addObject(K, std::move(ObjBuffer))) |
368 | return std::move(Err); |
369 | |
370 | KeyLayers[K] = detail::createGenericLayer(ObjectLayer); |
371 | |
372 | return K; |
373 | } else |
374 | return Obj.takeError(); |
375 | } |
376 | |
377 | JITSymbol findSymbol(const std::string &Name, |
378 | bool ExportedSymbolsOnly) { |
379 | if (auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly)) |
380 | return Sym; |
381 | if (CODLayer) |
382 | return CODLayer->findSymbol(mangle(Name), ExportedSymbolsOnly); |
383 | return CompileLayer.findSymbol(mangle(Name), ExportedSymbolsOnly); |
384 | } |
385 | |
386 | JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name, |
387 | bool ExportedSymbolsOnly) { |
388 | assert(KeyLayers.count(K) && "looking up symbol in unknown module")((KeyLayers.count(K) && "looking up symbol in unknown module" ) ? static_cast<void> (0) : __assert_fail ("KeyLayers.count(K) && \"looking up symbol in unknown module\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h" , 388, __PRETTY_FUNCTION__)); |
389 | return KeyLayers[K]->findSymbolIn(K, mangle(Name), ExportedSymbolsOnly); |
390 | } |
391 | |
392 | Expected<JITTargetAddress> findSymbolAddress(const std::string &Name, |
393 | bool ExportedSymbolsOnly) { |
394 | if (auto Sym = findSymbol(Name, ExportedSymbolsOnly)) { |
395 | // Successful lookup, non-null symbol: |
396 | if (auto AddrOrErr = Sym.getAddress()) |
397 | return *AddrOrErr; |
398 | else |
399 | return AddrOrErr.takeError(); |
400 | } else if (auto Err = Sym.takeError()) { |
401 | // Lookup failure - report error. |
402 | return std::move(Err); |
403 | } |
404 | |
405 | // No symbol not found. Return 0. |
406 | return 0; |
407 | } |
408 | |
409 | Expected<JITTargetAddress> findSymbolAddressIn(orc::VModuleKey K, |
410 | const std::string &Name, |
411 | bool ExportedSymbolsOnly) { |
412 | if (auto Sym = findSymbolIn(K, Name, ExportedSymbolsOnly)) { |
413 | // Successful lookup, non-null symbol: |
414 | if (auto AddrOrErr = Sym.getAddress()) |
415 | return *AddrOrErr; |
416 | else |
417 | return AddrOrErr.takeError(); |
418 | } else if (auto Err = Sym.takeError()) { |
419 | // Lookup failure - report error. |
420 | return std::move(Err); |
421 | } |
422 | |
423 | // Symbol not found. Return 0. |
424 | return 0; |
425 | } |
426 | |
427 | const std::string &getErrorMessage() const { return ErrMsg; } |
428 | |
429 | void RegisterJITEventListener(JITEventListener *L) { |
430 | if (!L) |
431 | return; |
432 | EventListeners.push_back(L); |
433 | } |
434 | |
435 | void UnregisterJITEventListener(JITEventListener *L) { |
436 | if (!L) |
437 | return; |
438 | |
439 | auto I = find(reverse(EventListeners), L); |
440 | if (I != EventListeners.rend()) { |
441 | std::swap(*I, EventListeners.back()); |
442 | EventListeners.pop_back(); |
443 | } |
444 | } |
445 | |
446 | private: |
447 | using ResolverMap = |
448 | std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>>; |
449 | |
450 | static std::unique_ptr<CompileCallbackMgr> |
451 | createCompileCallbackManager(TargetMachine &TM, orc::ExecutionSession &ES) { |
452 | auto CCMgr = createLocalCompileCallbackManager(TM.getTargetTriple(), ES, 0); |
453 | if (!CCMgr) { |
454 | // FIXME: It would be good if we could report this somewhere, but we do |
455 | // have an instance yet. |
456 | logAllUnhandledErrors(CCMgr.takeError(), errs(), "ORC error: "); |
457 | return nullptr; |
458 | } |
459 | return std::move(*CCMgr); |
460 | } |
461 | |
462 | static std::unique_ptr<CODLayerT> |
463 | createCODLayer(orc::ExecutionSession &ES, CompileLayerT &CompileLayer, |
464 | CompileCallbackMgr *CCMgr, |
465 | IndirectStubsManagerBuilder IndirectStubsMgrBuilder, |
466 | ResolverMap &Resolvers) { |
467 | // If there is no compile callback manager available we can not create a |
468 | // compile on demand layer. |
469 | if (!CCMgr) |
470 | return nullptr; |
471 | |
472 | return std::make_unique<CODLayerT>( |
473 | AcknowledgeORCv1Deprecation, ES, CompileLayer, |
474 | [&Resolvers](orc::VModuleKey K) { |
475 | auto ResolverI = Resolvers.find(K); |
476 | assert(ResolverI != Resolvers.end() && "No resolver for module K")((ResolverI != Resolvers.end() && "No resolver for module K" ) ? static_cast<void> (0) : __assert_fail ("ResolverI != Resolvers.end() && \"No resolver for module K\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h" , 476, __PRETTY_FUNCTION__)); |
477 | return ResolverI->second; |
478 | }, |
479 | [&Resolvers](orc::VModuleKey K, |
480 | std::shared_ptr<orc::SymbolResolver> Resolver) { |
481 | assert(!Resolvers.count(K) && "Resolver already present")((!Resolvers.count(K) && "Resolver already present") ? static_cast<void> (0) : __assert_fail ("!Resolvers.count(K) && \"Resolver already present\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/lib/ExecutionEngine/Orc/OrcCBindingsStack.h" , 481, __PRETTY_FUNCTION__)); |
482 | Resolvers[K] = std::move(Resolver); |
483 | }, |
484 | [](Function &F) { return std::set<Function *>({&F}); }, *CCMgr, |
485 | std::move(IndirectStubsMgrBuilder), false); |
486 | } |
487 | |
488 | void reportError(Error Err) { |
489 | // FIXME: Report errors on the execution session. |
490 | logAllUnhandledErrors(std::move(Err), errs(), "ORC error: "); |
491 | }; |
492 | |
493 | void notifyFinalized(orc::VModuleKey K, |
494 | const object::ObjectFile &Obj, |
495 | const RuntimeDyld::LoadedObjectInfo &LoadedObjInfo) { |
496 | uint64_t Key = static_cast<uint64_t>( |
497 | reinterpret_cast<uintptr_t>(Obj.getData().data())); |
498 | for (auto &Listener : EventListeners) |
499 | Listener->notifyObjectLoaded(Key, Obj, LoadedObjInfo); |
500 | } |
501 | |
502 | void notifyFreed(orc::VModuleKey K, const object::ObjectFile &Obj) { |
503 | uint64_t Key = static_cast<uint64_t>( |
504 | reinterpret_cast<uintptr_t>(Obj.getData().data())); |
505 | for (auto &Listener : EventListeners) |
506 | Listener->notifyFreeingObject(Key); |
507 | } |
508 | |
509 | orc::ExecutionSession ES; |
510 | std::unique_ptr<CompileCallbackMgr> CCMgr; |
511 | |
512 | std::vector<JITEventListener *> EventListeners; |
513 | |
514 | DataLayout DL; |
515 | SectionMemoryManager CCMgrMemMgr; |
516 | |
517 | std::unique_ptr<orc::IndirectStubsManager> IndirectStubsMgr; |
518 | |
519 | ObjLayerT ObjectLayer; |
520 | CompileLayerT CompileLayer; |
521 | std::unique_ptr<CODLayerT> CODLayer; |
522 | |
523 | std::map<orc::VModuleKey, std::unique_ptr<detail::GenericLayer>> KeyLayers; |
524 | |
525 | orc::LegacyLocalCXXRuntimeOverrides CXXRuntimeOverrides; |
526 | std::vector<orc::LegacyCtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners; |
527 | std::string ErrMsg; |
528 | |
529 | ResolverMap Resolvers; |
530 | }; |
531 | |
532 | } // end namespace llvm |
533 | |
534 | #endif // LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H |
1 | //===- ExecutionUtils.h - Utilities for executing code in Orc ---*- C++ -*-===// | |||
2 | // | |||
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 | |||
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/Mangling.h" | |||
21 | #include "llvm/ExecutionEngine/Orc/OrcError.h" | |||
22 | #include "llvm/ExecutionEngine/RuntimeDyld.h" | |||
23 | #include "llvm/Object/Archive.h" | |||
24 | #include "llvm/Support/DynamicLibrary.h" | |||
25 | #include <algorithm> | |||
26 | #include <cstdint> | |||
27 | #include <string> | |||
28 | #include <utility> | |||
29 | #include <vector> | |||
30 | ||||
31 | namespace llvm { | |||
32 | ||||
33 | class ConstantArray; | |||
34 | class GlobalVariable; | |||
35 | class Function; | |||
36 | class Module; | |||
37 | class TargetMachine; | |||
38 | class Value; | |||
39 | ||||
40 | namespace orc { | |||
41 | ||||
42 | class ObjectLayer; | |||
43 | ||||
44 | /// Run a main function, returning the result. | |||
45 | /// | |||
46 | /// If the optional ProgramName argument is given then it will be inserted | |||
47 | /// before the strings in Args as the first argument to the called function. | |||
48 | /// | |||
49 | /// It is legal to have an empty argument list and no program name, however | |||
50 | /// many main functions will expect a name argument at least, and will fail | |||
51 | /// if none is provided. | |||
52 | int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args, | |||
53 | Optional<StringRef> ProgramName = None); | |||
54 | ||||
55 | /// This iterator provides a convenient way to iterate over the elements | |||
56 | /// of an llvm.global_ctors/llvm.global_dtors instance. | |||
57 | /// | |||
58 | /// The easiest way to get hold of instances of this class is to use the | |||
59 | /// getConstructors/getDestructors functions. | |||
60 | class CtorDtorIterator { | |||
61 | public: | |||
62 | /// Accessor for an element of the global_ctors/global_dtors array. | |||
63 | /// | |||
64 | /// This class provides a read-only view of the element with any casts on | |||
65 | /// the function stripped away. | |||
66 | struct Element { | |||
67 | Element(unsigned Priority, Function *Func, Value *Data) | |||
68 | : Priority(Priority), Func(Func), Data(Data) {} | |||
69 | ||||
70 | unsigned Priority; | |||
71 | Function *Func; | |||
72 | Value *Data; | |||
73 | }; | |||
74 | ||||
75 | /// Construct an iterator instance. If End is true then this iterator | |||
76 | /// acts as the end of the range, otherwise it is the beginning. | |||
77 | CtorDtorIterator(const GlobalVariable *GV, bool End); | |||
78 | ||||
79 | /// Test iterators for equality. | |||
80 | bool operator==(const CtorDtorIterator &Other) const; | |||
81 | ||||
82 | /// Test iterators for inequality. | |||
83 | bool operator!=(const CtorDtorIterator &Other) const; | |||
84 | ||||
85 | /// Pre-increment iterator. | |||
86 | CtorDtorIterator& operator++(); | |||
87 | ||||
88 | /// Post-increment iterator. | |||
89 | CtorDtorIterator operator++(int); | |||
90 | ||||
91 | /// Dereference iterator. The resulting value provides a read-only view | |||
92 | /// of this element of the global_ctors/global_dtors list. | |||
93 | Element operator*() const; | |||
94 | ||||
95 | private: | |||
96 | const ConstantArray *InitList; | |||
97 | unsigned I; | |||
98 | }; | |||
99 | ||||
100 | /// Create an iterator range over the entries of the llvm.global_ctors | |||
101 | /// array. | |||
102 | iterator_range<CtorDtorIterator> getConstructors(const Module &M); | |||
103 | ||||
104 | /// Create an iterator range over the entries of the llvm.global_ctors | |||
105 | /// array. | |||
106 | iterator_range<CtorDtorIterator> getDestructors(const Module &M); | |||
107 | ||||
108 | /// This iterator provides a convenient way to iterate over GlobalValues that | |||
109 | /// have initialization effects. | |||
110 | class StaticInitGVIterator { | |||
111 | public: | |||
112 | StaticInitGVIterator() = default; | |||
113 | ||||
114 | StaticInitGVIterator(Module &M) | |||
115 | : I(M.global_values().begin()), E(M.global_values().end()), | |||
116 | ObjFmt(Triple(M.getTargetTriple()).getObjectFormat()) { | |||
117 | if (I != E) { | |||
118 | if (!isStaticInitGlobal(*I)) | |||
119 | moveToNextStaticInitGlobal(); | |||
120 | } else | |||
121 | I = E = Module::global_value_iterator(); | |||
122 | } | |||
123 | ||||
124 | bool operator==(const StaticInitGVIterator &O) const { return I == O.I; } | |||
125 | bool operator!=(const StaticInitGVIterator &O) const { return I != O.I; } | |||
126 | ||||
127 | StaticInitGVIterator &operator++() { | |||
128 | assert(I != E && "Increment past end of range")((I != E && "Increment past end of range") ? static_cast <void> (0) : __assert_fail ("I != E && \"Increment past end of range\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h" , 128, __PRETTY_FUNCTION__)); | |||
129 | moveToNextStaticInitGlobal(); | |||
130 | return *this; | |||
131 | } | |||
132 | ||||
133 | GlobalValue &operator*() { return *I; } | |||
134 | ||||
135 | private: | |||
136 | bool isStaticInitGlobal(GlobalValue &GV); | |||
137 | void moveToNextStaticInitGlobal() { | |||
138 | ++I; | |||
139 | while (I != E && !isStaticInitGlobal(*I)) | |||
140 | ++I; | |||
141 | if (I == E) | |||
142 | I = E = Module::global_value_iterator(); | |||
143 | } | |||
144 | ||||
145 | Module::global_value_iterator I, E; | |||
146 | Triple::ObjectFormatType ObjFmt; | |||
147 | }; | |||
148 | ||||
149 | /// Create an iterator range over the GlobalValues that contribute to static | |||
150 | /// initialization. | |||
151 | inline iterator_range<StaticInitGVIterator> getStaticInitGVs(Module &M) { | |||
152 | return make_range(StaticInitGVIterator(M), StaticInitGVIterator()); | |||
153 | } | |||
154 | ||||
155 | /// Convenience class for recording constructor/destructor names for | |||
156 | /// later execution. | |||
157 | template <typename JITLayerT> | |||
158 | class LegacyCtorDtorRunner { | |||
159 | public: | |||
160 | /// Construct a CtorDtorRunner for the given range using the given | |||
161 | /// name mangling function. | |||
162 | LLVM_ATTRIBUTE_DEPRECATED(LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames , VModuleKey K) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 CtorDtorRunner utility instead"))) | |||
163 | LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames,LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames , VModuleKey K) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 CtorDtorRunner utility instead"))) | |||
164 | VModuleKey K),LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames , VModuleKey K) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 CtorDtorRunner utility instead"))) | |||
165 | "ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. "LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames , VModuleKey K) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 CtorDtorRunner utility instead"))) | |||
166 | "Please use the ORCv2 CtorDtorRunner utility instead")LegacyCtorDtorRunner(std::vector<std::string> CtorDtorNames , VModuleKey K) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 CtorDtorRunner utility instead"))); | |||
167 | ||||
168 | LegacyCtorDtorRunner(ORCv1DeprecationAcknowledgement, | |||
169 | std::vector<std::string> CtorDtorNames, VModuleKey K) | |||
170 | : CtorDtorNames(std::move(CtorDtorNames)), K(K) {} | |||
171 | ||||
172 | /// Run the recorded constructors/destructors through the given JIT | |||
173 | /// layer. | |||
174 | Error runViaLayer(JITLayerT &JITLayer) const { | |||
175 | using CtorDtorTy = void (*)(); | |||
176 | ||||
177 | for (const auto &CtorDtorName : CtorDtorNames) { | |||
178 | if (auto CtorDtorSym = JITLayer.findSymbolIn(K, CtorDtorName, false)) { | |||
179 | if (auto AddrOrErr = CtorDtorSym.getAddress()) { | |||
180 | CtorDtorTy CtorDtor = | |||
181 | reinterpret_cast<CtorDtorTy>(static_cast<uintptr_t>(*AddrOrErr)); | |||
182 | CtorDtor(); | |||
| ||||
183 | } else | |||
184 | return AddrOrErr.takeError(); | |||
185 | } else { | |||
186 | if (auto Err = CtorDtorSym.takeError()) | |||
187 | return Err; | |||
188 | else | |||
189 | return make_error<JITSymbolNotFound>(CtorDtorName); | |||
190 | } | |||
191 | } | |||
192 | return Error::success(); | |||
193 | } | |||
194 | ||||
195 | private: | |||
196 | std::vector<std::string> CtorDtorNames; | |||
197 | orc::VModuleKey K; | |||
198 | }; | |||
199 | ||||
200 | template <typename JITLayerT> | |||
201 | LegacyCtorDtorRunner<JITLayerT>::LegacyCtorDtorRunner( | |||
202 | std::vector<std::string> CtorDtorNames, VModuleKey K) | |||
203 | : CtorDtorNames(std::move(CtorDtorNames)), K(K) {} | |||
204 | ||||
205 | class CtorDtorRunner { | |||
206 | public: | |||
207 | CtorDtorRunner(JITDylib &JD) : JD(JD) {} | |||
208 | void add(iterator_range<CtorDtorIterator> CtorDtors); | |||
209 | Error run(); | |||
210 | ||||
211 | private: | |||
212 | using CtorDtorList = std::vector<SymbolStringPtr>; | |||
213 | using CtorDtorPriorityMap = std::map<unsigned, CtorDtorList>; | |||
214 | ||||
215 | JITDylib &JD; | |||
216 | CtorDtorPriorityMap CtorDtorsByPriority; | |||
217 | }; | |||
218 | ||||
219 | /// Support class for static dtor execution. For hosted (in-process) JITs | |||
220 | /// only! | |||
221 | /// | |||
222 | /// If a __cxa_atexit function isn't found C++ programs that use static | |||
223 | /// destructors will fail to link. However, we don't want to use the host | |||
224 | /// process's __cxa_atexit, because it will schedule JIT'd destructors to run | |||
225 | /// after the JIT has been torn down, which is no good. This class makes it easy | |||
226 | /// to override __cxa_atexit (and the related __dso_handle). | |||
227 | /// | |||
228 | /// To use, clients should manually call searchOverrides from their symbol | |||
229 | /// resolver. This should generally be done after attempting symbol resolution | |||
230 | /// inside the JIT, but before searching the host process's symbol table. When | |||
231 | /// the client determines that destructors should be run (generally at JIT | |||
232 | /// teardown or after a return from main), the runDestructors method should be | |||
233 | /// called. | |||
234 | class LocalCXXRuntimeOverridesBase { | |||
235 | public: | |||
236 | /// Run any destructors recorded by the overriden __cxa_atexit function | |||
237 | /// (CXAAtExitOverride). | |||
238 | void runDestructors(); | |||
239 | ||||
240 | protected: | |||
241 | template <typename PtrTy> JITTargetAddress toTargetAddress(PtrTy *P) { | |||
242 | return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(P)); | |||
243 | } | |||
244 | ||||
245 | using DestructorPtr = void (*)(void *); | |||
246 | using CXXDestructorDataPair = std::pair<DestructorPtr, void *>; | |||
247 | using CXXDestructorDataPairList = std::vector<CXXDestructorDataPair>; | |||
248 | CXXDestructorDataPairList DSOHandleOverride; | |||
249 | static int CXAAtExitOverride(DestructorPtr Destructor, void *Arg, | |||
250 | void *DSOHandle); | |||
251 | }; | |||
252 | ||||
253 | class LegacyLocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase { | |||
254 | public: | |||
255 | /// Create a runtime-overrides class. | |||
256 | template <typename MangleFtorT> | |||
257 | LLVM_ATTRIBUTE_DEPRECATED(LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 LocalCXXRuntimeOverrides utility instead" ))) | |||
258 | LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle),LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 LocalCXXRuntimeOverrides utility instead" ))) | |||
259 | "ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. "LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 LocalCXXRuntimeOverrides utility instead" ))) | |||
260 | "Please use the ORCv2 LocalCXXRuntimeOverrides utility instead")LegacyLocalCXXRuntimeOverrides(const MangleFtorT &Mangle) __attribute__((deprecated("ORCv1 utilities (utilities with the 'Legacy' prefix) are deprecated. " "Please use the ORCv2 LocalCXXRuntimeOverrides utility instead" ))); | |||
261 | ||||
262 | template <typename MangleFtorT> | |||
263 | LegacyLocalCXXRuntimeOverrides(ORCv1DeprecationAcknowledgement, | |||
264 | const MangleFtorT &Mangle) { | |||
265 | addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride)); | |||
266 | addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride)); | |||
267 | } | |||
268 | ||||
269 | /// Search overrided symbols. | |||
270 | JITEvaluatedSymbol searchOverrides(const std::string &Name) { | |||
271 | auto I = CXXRuntimeOverrides.find(Name); | |||
272 | if (I != CXXRuntimeOverrides.end()) | |||
273 | return JITEvaluatedSymbol(I->second, JITSymbolFlags::Exported); | |||
274 | return nullptr; | |||
275 | } | |||
276 | ||||
277 | private: | |||
278 | void addOverride(const std::string &Name, JITTargetAddress Addr) { | |||
279 | CXXRuntimeOverrides.insert(std::make_pair(Name, Addr)); | |||
280 | } | |||
281 | ||||
282 | StringMap<JITTargetAddress> CXXRuntimeOverrides; | |||
283 | }; | |||
284 | ||||
285 | template <typename MangleFtorT> | |||
286 | LegacyLocalCXXRuntimeOverrides::LegacyLocalCXXRuntimeOverrides( | |||
287 | const MangleFtorT &Mangle) { | |||
288 | addOverride(Mangle("__dso_handle"), toTargetAddress(&DSOHandleOverride)); | |||
289 | addOverride(Mangle("__cxa_atexit"), toTargetAddress(&CXAAtExitOverride)); | |||
290 | } | |||
291 | ||||
292 | class LocalCXXRuntimeOverrides : public LocalCXXRuntimeOverridesBase { | |||
293 | public: | |||
294 | Error enable(JITDylib &JD, MangleAndInterner &Mangler); | |||
295 | }; | |||
296 | ||||
297 | /// An interface for Itanium __cxa_atexit interposer implementations. | |||
298 | class ItaniumCXAAtExitSupport { | |||
299 | public: | |||
300 | struct AtExitRecord { | |||
301 | void (*F)(void *); | |||
302 | void *Ctx; | |||
303 | }; | |||
304 | ||||
305 | void registerAtExit(void (*F)(void *), void *Ctx, void *DSOHandle); | |||
306 | void runAtExits(void *DSOHandle); | |||
307 | ||||
308 | private: | |||
309 | std::mutex AtExitsMutex; | |||
310 | DenseMap<void *, std::vector<AtExitRecord>> AtExitRecords; | |||
311 | }; | |||
312 | ||||
313 | /// A utility class to expose symbols found via dlsym to the JIT. | |||
314 | /// | |||
315 | /// If an instance of this class is attached to a JITDylib as a fallback | |||
316 | /// definition generator, then any symbol found in the given DynamicLibrary that | |||
317 | /// passes the 'Allow' predicate will be added to the JITDylib. | |||
318 | class DynamicLibrarySearchGenerator : public JITDylib::DefinitionGenerator { | |||
319 | public: | |||
320 | using SymbolPredicate = std::function<bool(const SymbolStringPtr &)>; | |||
321 | ||||
322 | /// Create a DynamicLibrarySearchGenerator that searches for symbols in the | |||
323 | /// given sys::DynamicLibrary. | |||
324 | /// | |||
325 | /// If the Allow predicate is given then only symbols matching the predicate | |||
326 | /// will be searched for. If the predicate is not given then all symbols will | |||
327 | /// be searched for. | |||
328 | DynamicLibrarySearchGenerator(sys::DynamicLibrary Dylib, char GlobalPrefix, | |||
329 | SymbolPredicate Allow = SymbolPredicate()); | |||
330 | ||||
331 | /// Permanently loads the library at the given path and, on success, returns | |||
332 | /// a DynamicLibrarySearchGenerator that will search it for symbol definitions | |||
333 | /// in the library. On failure returns the reason the library failed to load. | |||
334 | static Expected<std::unique_ptr<DynamicLibrarySearchGenerator>> | |||
335 | Load(const char *FileName, char GlobalPrefix, | |||
336 | SymbolPredicate Allow = SymbolPredicate()); | |||
337 | ||||
338 | /// Creates a DynamicLibrarySearchGenerator that searches for symbols in | |||
339 | /// the current process. | |||
340 | static Expected<std::unique_ptr<DynamicLibrarySearchGenerator>> | |||
341 | GetForCurrentProcess(char GlobalPrefix, | |||
342 | SymbolPredicate Allow = SymbolPredicate()) { | |||
343 | return Load(nullptr, GlobalPrefix, std::move(Allow)); | |||
344 | } | |||
345 | ||||
346 | Error tryToGenerate(LookupKind K, JITDylib &JD, | |||
347 | JITDylibLookupFlags JDLookupFlags, | |||
348 | const SymbolLookupSet &Symbols) override; | |||
349 | ||||
350 | private: | |||
351 | sys::DynamicLibrary Dylib; | |||
352 | SymbolPredicate Allow; | |||
353 | char GlobalPrefix; | |||
354 | }; | |||
355 | ||||
356 | /// A utility class to expose symbols from a static library. | |||
357 | /// | |||
358 | /// If an instance of this class is attached to a JITDylib as a fallback | |||
359 | /// definition generator, then any symbol found in the archive will result in | |||
360 | /// the containing object being added to the JITDylib. | |||
361 | class StaticLibraryDefinitionGenerator : public JITDylib::DefinitionGenerator { | |||
362 | public: | |||
363 | /// Try to create a StaticLibraryDefinitionGenerator from the given path. | |||
364 | /// | |||
365 | /// This call will succeed if the file at the given path is a static library | |||
366 | /// is a valid archive, otherwise it will return an error. | |||
367 | static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> | |||
368 | Load(ObjectLayer &L, const char *FileName); | |||
369 | ||||
370 | /// Try to create a StaticLibraryDefinitionGenerator from the given path. | |||
371 | /// | |||
372 | /// This call will succeed if the file at the given path is a static library | |||
373 | /// or a MachO universal binary containing a static library that is compatible | |||
374 | /// with the given triple. Otherwise it will return an error. | |||
375 | static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> | |||
376 | Load(ObjectLayer &L, const char *FileName, const Triple &TT); | |||
377 | ||||
378 | /// Try to create a StaticLibrarySearchGenerator from the given memory buffer. | |||
379 | /// This call will succeed if the buffer contains a valid archive, otherwise | |||
380 | /// it will return an error. | |||
381 | static Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> | |||
382 | Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer); | |||
383 | ||||
384 | Error tryToGenerate(LookupKind K, JITDylib &JD, | |||
385 | JITDylibLookupFlags JDLookupFlags, | |||
386 | const SymbolLookupSet &Symbols) override; | |||
387 | ||||
388 | private: | |||
389 | StaticLibraryDefinitionGenerator(ObjectLayer &L, | |||
390 | std::unique_ptr<MemoryBuffer> ArchiveBuffer, | |||
391 | Error &Err); | |||
392 | ||||
393 | ObjectLayer &L; | |||
394 | std::unique_ptr<MemoryBuffer> ArchiveBuffer; | |||
395 | std::unique_ptr<object::Archive> Archive; | |||
396 | }; | |||
397 | ||||
398 | } // end namespace orc | |||
399 | } // end namespace llvm | |||
400 | ||||
401 | #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTIONUTILS_H |
1 | //===- JITSymbol.h - JIT symbol abstraction ---------------------*- C++ -*-===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Abstraction for target process addresses. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H |
14 | #define LLVM_EXECUTIONENGINE_JITSYMBOL_H |
15 | |
16 | #include <algorithm> |
17 | #include <cassert> |
18 | #include <cstddef> |
19 | #include <cstdint> |
20 | #include <functional> |
21 | #include <map> |
22 | #include <set> |
23 | #include <string> |
24 | |
25 | #include "llvm/ADT/BitmaskEnum.h" |
26 | #include "llvm/ADT/FunctionExtras.h" |
27 | #include "llvm/ADT/StringRef.h" |
28 | #include "llvm/Support/Error.h" |
29 | |
30 | namespace llvm { |
31 | |
32 | class GlobalValue; |
33 | class GlobalValueSummary; |
34 | |
35 | namespace object { |
36 | |
37 | class SymbolRef; |
38 | |
39 | } // end namespace object |
40 | |
41 | /// Represents an address in the target process's address space. |
42 | using JITTargetAddress = uint64_t; |
43 | |
44 | /// Convert a JITTargetAddress to a pointer. |
45 | /// |
46 | /// Note: This is a raw cast of the address bit pattern to the given pointer |
47 | /// type. When casting to a function pointer in order to execute JIT'd code |
48 | /// jitTargetAddressToFunction should be preferred, as it will also perform |
49 | /// pointer signing on targets that require it. |
50 | template <typename T> T jitTargetAddressToPointer(JITTargetAddress Addr) { |
51 | static_assert(std::is_pointer<T>::value, "T must be a pointer type"); |
52 | uintptr_t IntPtr = static_cast<uintptr_t>(Addr); |
53 | assert(IntPtr == Addr && "JITTargetAddress value out of range for uintptr_t")((IntPtr == Addr && "JITTargetAddress value out of range for uintptr_t" ) ? static_cast<void> (0) : __assert_fail ("IntPtr == Addr && \"JITTargetAddress value out of range for uintptr_t\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/ExecutionEngine/JITSymbol.h" , 53, __PRETTY_FUNCTION__)); |
54 | return reinterpret_cast<T>(IntPtr); |
55 | } |
56 | |
57 | /// Convert a JITTargetAddress to a callable function pointer. |
58 | /// |
59 | /// Casts the given address to a callable function pointer. This operation |
60 | /// will perform pointer signing for platforms that require it (e.g. arm64e). |
61 | template <typename T> T jitTargetAddressToFunction(JITTargetAddress Addr) { |
62 | static_assert(std::is_pointer<T>::value && |
63 | std::is_function<std::remove_pointer_t<T>>::value, |
64 | "T must be a function pointer type"); |
65 | return jitTargetAddressToPointer<T>(Addr); |
66 | } |
67 | |
68 | /// Convert a pointer to a JITTargetAddress. |
69 | template <typename T> JITTargetAddress pointerToJITTargetAddress(T *Ptr) { |
70 | return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Ptr)); |
71 | } |
72 | |
73 | /// Flags for symbols in the JIT. |
74 | class JITSymbolFlags { |
75 | public: |
76 | using UnderlyingType = uint8_t; |
77 | using TargetFlagsType = uint8_t; |
78 | |
79 | enum FlagNames : UnderlyingType { |
80 | None = 0, |
81 | HasError = 1U << 0, |
82 | Weak = 1U << 1, |
83 | Common = 1U << 2, |
84 | Absolute = 1U << 3, |
85 | Exported = 1U << 4, |
86 | Callable = 1U << 5, |
87 | MaterializationSideEffectsOnly = 1U << 6, |
88 | LLVM_MARK_AS_BITMASK_ENUM( // LargestValue =LLVM_BITMASK_LARGEST_ENUMERATOR = MaterializationSideEffectsOnly |
89 | MaterializationSideEffectsOnly)LLVM_BITMASK_LARGEST_ENUMERATOR = MaterializationSideEffectsOnly |
90 | }; |
91 | |
92 | /// Default-construct a JITSymbolFlags instance. |
93 | JITSymbolFlags() = default; |
94 | |
95 | /// Construct a JITSymbolFlags instance from the given flags. |
96 | JITSymbolFlags(FlagNames Flags) : Flags(Flags) {} |
97 | |
98 | /// Construct a JITSymbolFlags instance from the given flags and target |
99 | /// flags. |
100 | JITSymbolFlags(FlagNames Flags, TargetFlagsType TargetFlags) |
101 | : TargetFlags(TargetFlags), Flags(Flags) {} |
102 | |
103 | /// Implicitly convert to bool. Returs true if any flag is set. |
104 | explicit operator bool() const { return Flags != None || TargetFlags != 0; } |
105 | |
106 | /// Compare for equality. |
107 | bool operator==(const JITSymbolFlags &RHS) const { |
108 | return Flags == RHS.Flags && TargetFlags == RHS.TargetFlags; |
109 | } |
110 | |
111 | /// Bitwise AND-assignment for FlagNames. |
112 | JITSymbolFlags &operator&=(const FlagNames &RHS) { |
113 | Flags &= RHS; |
114 | return *this; |
115 | } |
116 | |
117 | /// Bitwise OR-assignment for FlagNames. |
118 | JITSymbolFlags &operator|=(const FlagNames &RHS) { |
119 | Flags |= RHS; |
120 | return *this; |
121 | } |
122 | |
123 | /// Return true if there was an error retrieving this symbol. |
124 | bool hasError() const { |
125 | return (Flags & HasError) == HasError; |
126 | } |
127 | |
128 | /// Returns true if the Weak flag is set. |
129 | bool isWeak() const { |
130 | return (Flags & Weak) == Weak; |
131 | } |
132 | |
133 | /// Returns true if the Common flag is set. |
134 | bool isCommon() const { |
135 | return (Flags & Common) == Common; |
136 | } |
137 | |
138 | /// Returns true if the symbol isn't weak or common. |
139 | bool isStrong() const { |
140 | return !isWeak() && !isCommon(); |
141 | } |
142 | |
143 | /// Returns true if the Exported flag is set. |
144 | bool isExported() const { |
145 | return (Flags & Exported) == Exported; |
146 | } |
147 | |
148 | /// Returns true if the given symbol is known to be callable. |
149 | bool isCallable() const { return (Flags & Callable) == Callable; } |
150 | |
151 | /// Returns true if this symbol is a materialization-side-effects-only |
152 | /// symbol. Such symbols do not have a real address. They exist to trigger |
153 | /// and support synchronization of materialization side effects, e.g. for |
154 | /// collecting initialization information. These symbols will vanish from |
155 | /// the symbol table immediately upon reaching the ready state, and will |
156 | /// appear to queries as if they were never defined (except that query |
157 | /// callback execution will be delayed until they reach the ready state). |
158 | /// MaterializationSideEffectOnly symbols should only be queried using the |
159 | /// SymbolLookupFlags::WeaklyReferencedSymbol flag (see |
160 | /// llvm/include/llvm/ExecutionEngine/Orc/Core.h). |
161 | bool hasMaterializationSideEffectsOnly() const { |
162 | return (Flags & MaterializationSideEffectsOnly) == |
163 | MaterializationSideEffectsOnly; |
164 | } |
165 | |
166 | /// Get the underlying flags value as an integer. |
167 | UnderlyingType getRawFlagsValue() const { |
168 | return static_cast<UnderlyingType>(Flags); |
169 | } |
170 | |
171 | /// Return a reference to the target-specific flags. |
172 | TargetFlagsType& getTargetFlags() { return TargetFlags; } |
173 | |
174 | /// Return a reference to the target-specific flags. |
175 | const TargetFlagsType& getTargetFlags() const { return TargetFlags; } |
176 | |
177 | /// Construct a JITSymbolFlags value based on the flags of the given global |
178 | /// value. |
179 | static JITSymbolFlags fromGlobalValue(const GlobalValue &GV); |
180 | |
181 | /// Construct a JITSymbolFlags value based on the flags of the given global |
182 | /// value summary. |
183 | static JITSymbolFlags fromSummary(GlobalValueSummary *S); |
184 | |
185 | /// Construct a JITSymbolFlags value based on the flags of the given libobject |
186 | /// symbol. |
187 | static Expected<JITSymbolFlags> |
188 | fromObjectSymbol(const object::SymbolRef &Symbol); |
189 | |
190 | private: |
191 | TargetFlagsType TargetFlags = 0; |
192 | FlagNames Flags = None; |
193 | }; |
194 | |
195 | inline JITSymbolFlags operator&(const JITSymbolFlags &LHS, |
196 | const JITSymbolFlags::FlagNames &RHS) { |
197 | JITSymbolFlags Tmp = LHS; |
198 | Tmp &= RHS; |
199 | return Tmp; |
200 | } |
201 | |
202 | inline JITSymbolFlags operator|(const JITSymbolFlags &LHS, |
203 | const JITSymbolFlags::FlagNames &RHS) { |
204 | JITSymbolFlags Tmp = LHS; |
205 | Tmp |= RHS; |
206 | return Tmp; |
207 | } |
208 | |
209 | /// ARM-specific JIT symbol flags. |
210 | /// FIXME: This should be moved into a target-specific header. |
211 | class ARMJITSymbolFlags { |
212 | public: |
213 | ARMJITSymbolFlags() = default; |
214 | |
215 | enum FlagNames { |
216 | None = 0, |
217 | Thumb = 1 << 0 |
218 | }; |
219 | |
220 | operator JITSymbolFlags::TargetFlagsType&() { return Flags; } |
221 | |
222 | static ARMJITSymbolFlags fromObjectSymbol(const object::SymbolRef &Symbol); |
223 | |
224 | private: |
225 | JITSymbolFlags::TargetFlagsType Flags = 0; |
226 | }; |
227 | |
228 | /// Represents a symbol that has been evaluated to an address already. |
229 | class JITEvaluatedSymbol { |
230 | public: |
231 | JITEvaluatedSymbol() = default; |
232 | |
233 | /// Create a 'null' symbol. |
234 | JITEvaluatedSymbol(std::nullptr_t) {} |
235 | |
236 | /// Create a symbol for the given address and flags. |
237 | JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags) |
238 | : Address(Address), Flags(Flags) {} |
239 | |
240 | /// Create a symbol from the given pointer with the given flags. |
241 | template <typename T> |
242 | static JITEvaluatedSymbol |
243 | fromPointer(T *P, JITSymbolFlags Flags = JITSymbolFlags::Exported) { |
244 | return JITEvaluatedSymbol(pointerToJITTargetAddress(P), Flags); |
245 | } |
246 | |
247 | /// An evaluated symbol converts to 'true' if its address is non-zero. |
248 | explicit operator bool() const { return Address != 0; } |
249 | |
250 | /// Return the address of this symbol. |
251 | JITTargetAddress getAddress() const { return Address; } |
252 | |
253 | /// Return the flags for this symbol. |
254 | JITSymbolFlags getFlags() const { return Flags; } |
255 | |
256 | /// Set the flags for this symbol. |
257 | void setFlags(JITSymbolFlags Flags) { this->Flags = std::move(Flags); } |
258 | |
259 | private: |
260 | JITTargetAddress Address = 0; |
261 | JITSymbolFlags Flags; |
262 | }; |
263 | |
264 | /// Represents a symbol in the JIT. |
265 | class JITSymbol { |
266 | public: |
267 | using GetAddressFtor = unique_function<Expected<JITTargetAddress>()>; |
268 | |
269 | /// Create a 'null' symbol, used to represent a "symbol not found" |
270 | /// result from a successful (non-erroneous) lookup. |
271 | JITSymbol(std::nullptr_t) |
272 | : CachedAddr(0) {} |
273 | |
274 | /// Create a JITSymbol representing an error in the symbol lookup |
275 | /// process (e.g. a network failure during a remote lookup). |
276 | JITSymbol(Error Err) |
277 | : Err(std::move(Err)), Flags(JITSymbolFlags::HasError) {} |
278 | |
279 | /// Create a symbol for a definition with a known address. |
280 | JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags) |
281 | : CachedAddr(Addr), Flags(Flags) {} |
282 | |
283 | /// Construct a JITSymbol from a JITEvaluatedSymbol. |
284 | JITSymbol(JITEvaluatedSymbol Sym) |
285 | : CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {} |
286 | |
287 | /// Create a symbol for a definition that doesn't have a known address |
288 | /// yet. |
289 | /// @param GetAddress A functor to materialize a definition (fixing the |
290 | /// address) on demand. |
291 | /// |
292 | /// This constructor allows a JIT layer to provide a reference to a symbol |
293 | /// definition without actually materializing the definition up front. The |
294 | /// user can materialize the definition at any time by calling the getAddress |
295 | /// method. |
296 | JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags) |
297 | : GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {} |
298 | |
299 | JITSymbol(const JITSymbol&) = delete; |
300 | JITSymbol& operator=(const JITSymbol&) = delete; |
301 | |
302 | JITSymbol(JITSymbol &&Other) |
303 | : GetAddress(std::move(Other.GetAddress)), Flags(std::move(Other.Flags)) { |
304 | if (Flags.hasError()) |
305 | Err = std::move(Other.Err); |
306 | else |
307 | CachedAddr = std::move(Other.CachedAddr); |
308 | } |
309 | |
310 | JITSymbol& operator=(JITSymbol &&Other) { |
311 | GetAddress = std::move(Other.GetAddress); |
312 | Flags = std::move(Other.Flags); |
313 | if (Flags.hasError()) |
314 | Err = std::move(Other.Err); |
315 | else |
316 | CachedAddr = std::move(Other.CachedAddr); |
317 | return *this; |
318 | } |
319 | |
320 | ~JITSymbol() { |
321 | if (Flags.hasError()) |
322 | Err.~Error(); |
323 | else |
324 | CachedAddr.~JITTargetAddress(); |
325 | } |
326 | |
327 | /// Returns true if the symbol exists, false otherwise. |
328 | explicit operator bool() const { |
329 | return !Flags.hasError() && (CachedAddr || GetAddress); |
330 | } |
331 | |
332 | /// Move the error field value out of this JITSymbol. |
333 | Error takeError() { |
334 | if (Flags.hasError()) |
335 | return std::move(Err); |
336 | return Error::success(); |
337 | } |
338 | |
339 | /// Get the address of the symbol in the target address space. Returns |
340 | /// '0' if the symbol does not exist. |
341 | Expected<JITTargetAddress> getAddress() { |
342 | assert(!Flags.hasError() && "getAddress called on error value")((!Flags.hasError() && "getAddress called on error value" ) ? static_cast<void> (0) : __assert_fail ("!Flags.hasError() && \"getAddress called on error value\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/ExecutionEngine/JITSymbol.h" , 342, __PRETTY_FUNCTION__)); |
343 | if (GetAddress) { |
344 | if (auto CachedAddrOrErr = GetAddress()) { |
345 | GetAddress = nullptr; |
346 | CachedAddr = *CachedAddrOrErr; |
347 | assert(CachedAddr && "Symbol could not be materialized.")((CachedAddr && "Symbol could not be materialized.") ? static_cast<void> (0) : __assert_fail ("CachedAddr && \"Symbol could not be materialized.\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/ExecutionEngine/JITSymbol.h" , 347, __PRETTY_FUNCTION__)); |
348 | } else |
349 | return CachedAddrOrErr.takeError(); |
350 | } |
351 | return CachedAddr; |
352 | } |
353 | |
354 | JITSymbolFlags getFlags() const { return Flags; } |
355 | |
356 | private: |
357 | GetAddressFtor GetAddress; |
358 | union { |
359 | JITTargetAddress CachedAddr; |
360 | Error Err; |
361 | }; |
362 | JITSymbolFlags Flags; |
363 | }; |
364 | |
365 | /// Symbol resolution interface. |
366 | /// |
367 | /// Allows symbol flags and addresses to be looked up by name. |
368 | /// Symbol queries are done in bulk (i.e. you request resolution of a set of |
369 | /// symbols, rather than a single one) to reduce IPC overhead in the case of |
370 | /// remote JITing, and expose opportunities for parallel compilation. |
371 | class JITSymbolResolver { |
372 | public: |
373 | using LookupSet = std::set<StringRef>; |
374 | using LookupResult = std::map<StringRef, JITEvaluatedSymbol>; |
375 | using OnResolvedFunction = unique_function<void(Expected<LookupResult>)>; |
376 | |
377 | virtual ~JITSymbolResolver() = default; |
378 | |
379 | /// Returns the fully resolved address and flags for each of the given |
380 | /// symbols. |
381 | /// |
382 | /// This method will return an error if any of the given symbols can not be |
383 | /// resolved, or if the resolution process itself triggers an error. |
384 | virtual void lookup(const LookupSet &Symbols, |
385 | OnResolvedFunction OnResolved) = 0; |
386 | |
387 | /// Returns the subset of the given symbols that should be materialized by |
388 | /// the caller. Only weak/common symbols should be looked up, as strong |
389 | /// definitions are implicitly always part of the caller's responsibility. |
390 | virtual Expected<LookupSet> |
391 | getResponsibilitySet(const LookupSet &Symbols) = 0; |
392 | |
393 | private: |
394 | virtual void anchor(); |
395 | }; |
396 | |
397 | /// Legacy symbol resolution interface. |
398 | class LegacyJITSymbolResolver : public JITSymbolResolver { |
399 | public: |
400 | /// Performs lookup by, for each symbol, first calling |
401 | /// findSymbolInLogicalDylib and if that fails calling |
402 | /// findSymbol. |
403 | void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) final; |
404 | |
405 | /// Performs flags lookup by calling findSymbolInLogicalDylib and |
406 | /// returning the flags value for that symbol. |
407 | Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) final; |
408 | |
409 | /// This method returns the address of the specified symbol if it exists |
410 | /// within the logical dynamic library represented by this JITSymbolResolver. |
411 | /// Unlike findSymbol, queries through this interface should return addresses |
412 | /// for hidden symbols. |
413 | /// |
414 | /// This is of particular importance for the Orc JIT APIs, which support lazy |
415 | /// compilation by breaking up modules: Each of those broken out modules |
416 | /// must be able to resolve hidden symbols provided by the others. Clients |
417 | /// writing memory managers for MCJIT can usually ignore this method. |
418 | /// |
419 | /// This method will be queried by RuntimeDyld when checking for previous |
420 | /// definitions of common symbols. |
421 | virtual JITSymbol findSymbolInLogicalDylib(const std::string &Name) = 0; |
422 | |
423 | /// This method returns the address of the specified function or variable. |
424 | /// It is used to resolve symbols during module linking. |
425 | /// |
426 | /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will |
427 | /// skip all relocations for that symbol, and the client will be responsible |
428 | /// for handling them manually. |
429 | virtual JITSymbol findSymbol(const std::string &Name) = 0; |
430 | |
431 | private: |
432 | void anchor() override; |
433 | }; |
434 | |
435 | } // end namespace llvm |
436 | |
437 | #endif // LLVM_EXECUTIONENGINE_JITSYMBOL_H |
1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines an API used to report recoverable errors. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_SUPPORT_ERROR_H |
14 | #define LLVM_SUPPORT_ERROR_H |
15 | |
16 | #include "llvm-c/Error.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/ADT/Twine.h" |
21 | #include "llvm/Config/abi-breaking.h" |
22 | #include "llvm/Support/AlignOf.h" |
23 | #include "llvm/Support/Compiler.h" |
24 | #include "llvm/Support/Debug.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/Support/ErrorOr.h" |
27 | #include "llvm/Support/Format.h" |
28 | #include "llvm/Support/raw_ostream.h" |
29 | #include <algorithm> |
30 | #include <cassert> |
31 | #include <cstdint> |
32 | #include <cstdlib> |
33 | #include <functional> |
34 | #include <memory> |
35 | #include <new> |
36 | #include <string> |
37 | #include <system_error> |
38 | #include <type_traits> |
39 | #include <utility> |
40 | #include <vector> |
41 | |
42 | namespace llvm { |
43 | |
44 | class ErrorSuccess; |
45 | |
46 | /// Base class for error info classes. Do not extend this directly: Extend |
47 | /// the ErrorInfo template subclass instead. |
48 | class ErrorInfoBase { |
49 | public: |
50 | virtual ~ErrorInfoBase() = default; |
51 | |
52 | /// Print an error message to an output stream. |
53 | virtual void log(raw_ostream &OS) const = 0; |
54 | |
55 | /// Return the error message as a string. |
56 | virtual std::string message() const { |
57 | std::string Msg; |
58 | raw_string_ostream OS(Msg); |
59 | log(OS); |
60 | return OS.str(); |
61 | } |
62 | |
63 | /// Convert this error to a std::error_code. |
64 | /// |
65 | /// This is a temporary crutch to enable interaction with code still |
66 | /// using std::error_code. It will be removed in the future. |
67 | virtual std::error_code convertToErrorCode() const = 0; |
68 | |
69 | // Returns the class ID for this type. |
70 | static const void *classID() { return &ID; } |
71 | |
72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. |
73 | virtual const void *dynamicClassID() const = 0; |
74 | |
75 | // Check whether this instance is a subclass of the class identified by |
76 | // ClassID. |
77 | virtual bool isA(const void *const ClassID) const { |
78 | return ClassID == classID(); |
79 | } |
80 | |
81 | // Check whether this instance is a subclass of ErrorInfoT. |
82 | template <typename ErrorInfoT> bool isA() const { |
83 | return isA(ErrorInfoT::classID()); |
84 | } |
85 | |
86 | private: |
87 | virtual void anchor(); |
88 | |
89 | static char ID; |
90 | }; |
91 | |
92 | /// Lightweight error class with error context and mandatory checking. |
93 | /// |
94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states |
95 | /// are represented by setting the pointer to a ErrorInfoBase subclass |
96 | /// instance containing information describing the failure. Success is |
97 | /// represented by a null pointer value. |
98 | /// |
99 | /// Instances of Error also contains a 'Checked' flag, which must be set |
100 | /// before the destructor is called, otherwise the destructor will trigger a |
101 | /// runtime error. This enforces at runtime the requirement that all Error |
102 | /// instances be checked or returned to the caller. |
103 | /// |
104 | /// There are two ways to set the checked flag, depending on what state the |
105 | /// Error instance is in. For Error instances indicating success, it |
106 | /// is sufficient to invoke the boolean conversion operator. E.g.: |
107 | /// |
108 | /// @code{.cpp} |
109 | /// Error foo(<...>); |
110 | /// |
111 | /// if (auto E = foo(<...>)) |
112 | /// return E; // <- Return E if it is in the error state. |
113 | /// // We have verified that E was in the success state. It can now be safely |
114 | /// // destroyed. |
115 | /// @endcode |
116 | /// |
117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' |
118 | /// without testing the return value will raise a runtime error, even if foo |
119 | /// returns success. |
120 | /// |
121 | /// For Error instances representing failure, you must use either the |
122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: |
123 | /// |
124 | /// @code{.cpp} |
125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { |
126 | /// // Custom error info. |
127 | /// }; |
128 | /// |
129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } |
130 | /// |
131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. |
132 | /// auto NewE = |
133 | /// handleErrors(E, |
134 | /// [](const MyErrorInfo &M) { |
135 | /// // Deal with the error. |
136 | /// }, |
137 | /// [](std::unique_ptr<OtherError> M) -> Error { |
138 | /// if (canHandle(*M)) { |
139 | /// // handle error. |
140 | /// return Error::success(); |
141 | /// } |
142 | /// // Couldn't handle this error instance. Pass it up the stack. |
143 | /// return Error(std::move(M)); |
144 | /// ); |
145 | /// // Note - we must check or return NewE in case any of the handlers |
146 | /// // returned a new error. |
147 | /// @endcode |
148 | /// |
149 | /// The handleAllErrors function is identical to handleErrors, except |
150 | /// that it has a void return type, and requires all errors to be handled and |
151 | /// no new errors be returned. It prevents errors (assuming they can all be |
152 | /// handled) from having to be bubbled all the way to the top-level. |
153 | /// |
154 | /// *All* Error instances must be checked before destruction, even if |
155 | /// they're moved-assigned or constructed from Success values that have already |
156 | /// been checked. This enforces checking through all levels of the call stack. |
157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { |
158 | // ErrorList needs to be able to yank ErrorInfoBase pointers out of Errors |
159 | // to add to the error list. It can't rely on handleErrors for this, since |
160 | // handleErrors does not support ErrorList handlers. |
161 | friend class ErrorList; |
162 | |
163 | // handleErrors needs to be able to set the Checked flag. |
164 | template <typename... HandlerTs> |
165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
166 | |
167 | // Expected<T> needs to be able to steal the payload when constructed from an |
168 | // error. |
169 | template <typename T> friend class Expected; |
170 | |
171 | // wrap needs to be able to steal the payload. |
172 | friend LLVMErrorRef wrap(Error); |
173 | |
174 | protected: |
175 | /// Create a success value. Prefer using 'Error::success()' for readability |
176 | Error() { |
177 | setPtr(nullptr); |
178 | setChecked(false); |
179 | } |
180 | |
181 | public: |
182 | /// Create a success value. |
183 | static ErrorSuccess success(); |
184 | |
185 | // Errors are not copy-constructable. |
186 | Error(const Error &Other) = delete; |
187 | |
188 | /// Move-construct an error value. The newly constructed error is considered |
189 | /// unchecked, even if the source error had been checked. The original error |
190 | /// becomes a checked Success value, regardless of its original state. |
191 | Error(Error &&Other) { |
192 | setChecked(true); |
193 | *this = std::move(Other); |
194 | } |
195 | |
196 | /// Create an error value. Prefer using the 'make_error' function, but |
197 | /// this constructor can be useful when "re-throwing" errors from handlers. |
198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { |
199 | setPtr(Payload.release()); |
200 | setChecked(false); |
201 | } |
202 | |
203 | // Errors are not copy-assignable. |
204 | Error &operator=(const Error &Other) = delete; |
205 | |
206 | /// Move-assign an error value. The current error must represent success, you |
207 | /// you cannot overwrite an unhandled error. The current error is then |
208 | /// considered unchecked. The source error becomes a checked success value, |
209 | /// regardless of its original state. |
210 | Error &operator=(Error &&Other) { |
211 | // Don't allow overwriting of unchecked values. |
212 | assertIsChecked(); |
213 | setPtr(Other.getPtr()); |
214 | |
215 | // This Error is unchecked, even if the source error was checked. |
216 | setChecked(false); |
217 | |
218 | // Null out Other's payload and set its checked bit. |
219 | Other.setPtr(nullptr); |
220 | Other.setChecked(true); |
221 | |
222 | return *this; |
223 | } |
224 | |
225 | /// Destroy a Error. Fails with a call to abort() if the error is |
226 | /// unchecked. |
227 | ~Error() { |
228 | assertIsChecked(); |
229 | delete getPtr(); |
230 | } |
231 | |
232 | /// Bool conversion. Returns true if this Error is in a failure state, |
233 | /// and false if it is in an accept state. If the error is in a Success state |
234 | /// it will be considered checked. |
235 | explicit operator bool() { |
236 | setChecked(getPtr() == nullptr); |
237 | return getPtr() != nullptr; |
238 | } |
239 | |
240 | /// Check whether one error is a subclass of another. |
241 | template <typename ErrT> bool isA() const { |
242 | return getPtr() && getPtr()->isA(ErrT::classID()); |
243 | } |
244 | |
245 | /// Returns the dynamic class id of this error, or null if this is a success |
246 | /// value. |
247 | const void* dynamicClassID() const { |
248 | if (!getPtr()) |
249 | return nullptr; |
250 | return getPtr()->dynamicClassID(); |
251 | } |
252 | |
253 | private: |
254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
255 | // assertIsChecked() happens very frequently, but under normal circumstances |
256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch |
257 | // of debug prints can cause the function to be too large for inlining. So |
258 | // it's important that we define this function out of line so that it can't be |
259 | // inlined. |
260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
261 | void fatalUncheckedError() const; |
262 | #endif |
263 | |
264 | void assertIsChecked() { |
265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) |
267 | fatalUncheckedError(); |
268 | #endif |
269 | } |
270 | |
271 | ErrorInfoBase *getPtr() const { |
272 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
273 | return reinterpret_cast<ErrorInfoBase*>( |
274 | reinterpret_cast<uintptr_t>(Payload) & |
275 | ~static_cast<uintptr_t>(0x1)); |
276 | #else |
277 | return Payload; |
278 | #endif |
279 | } |
280 | |
281 | void setPtr(ErrorInfoBase *EI) { |
282 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
283 | Payload = reinterpret_cast<ErrorInfoBase*>( |
284 | (reinterpret_cast<uintptr_t>(EI) & |
285 | ~static_cast<uintptr_t>(0x1)) | |
286 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); |
287 | #else |
288 | Payload = EI; |
289 | #endif |
290 | } |
291 | |
292 | bool getChecked() const { |
293 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
294 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; |
295 | #else |
296 | return true; |
297 | #endif |
298 | } |
299 | |
300 | void setChecked(bool V) { |
301 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
302 | Payload = reinterpret_cast<ErrorInfoBase*>( |
303 | (reinterpret_cast<uintptr_t>(Payload) & |
304 | ~static_cast<uintptr_t>(0x1)) | |
305 | (V ? 0 : 1)); |
306 | #endif |
307 | } |
308 | |
309 | std::unique_ptr<ErrorInfoBase> takePayload() { |
310 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); |
311 | setPtr(nullptr); |
312 | setChecked(true); |
313 | return Tmp; |
314 | } |
315 | |
316 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { |
317 | if (auto P = E.getPtr()) |
318 | P->log(OS); |
319 | else |
320 | OS << "success"; |
321 | return OS; |
322 | } |
323 | |
324 | ErrorInfoBase *Payload = nullptr; |
325 | }; |
326 | |
327 | /// Subclass of Error for the sole purpose of identifying the success path in |
328 | /// the type system. This allows to catch invalid conversion to Expected<T> at |
329 | /// compile time. |
330 | class ErrorSuccess final : public Error {}; |
331 | |
332 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } |
333 | |
334 | /// Make a Error instance representing failure using the given error info |
335 | /// type. |
336 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { |
337 | return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); |
338 | } |
339 | |
340 | /// Base class for user error types. Users should declare their error types |
341 | /// like: |
342 | /// |
343 | /// class MyError : public ErrorInfo<MyError> { |
344 | /// .... |
345 | /// }; |
346 | /// |
347 | /// This class provides an implementation of the ErrorInfoBase::kind |
348 | /// method, which is used by the Error RTTI system. |
349 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> |
350 | class ErrorInfo : public ParentErrT { |
351 | public: |
352 | using ParentErrT::ParentErrT; // inherit constructors |
353 | |
354 | static const void *classID() { return &ThisErrT::ID; } |
355 | |
356 | const void *dynamicClassID() const override { return &ThisErrT::ID; } |
357 | |
358 | bool isA(const void *const ClassID) const override { |
359 | return ClassID == classID() || ParentErrT::isA(ClassID); |
360 | } |
361 | }; |
362 | |
363 | /// Special ErrorInfo subclass representing a list of ErrorInfos. |
364 | /// Instances of this class are constructed by joinError. |
365 | class ErrorList final : public ErrorInfo<ErrorList> { |
366 | // handleErrors needs to be able to iterate the payload list of an |
367 | // ErrorList. |
368 | template <typename... HandlerTs> |
369 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); |
370 | |
371 | // joinErrors is implemented in terms of join. |
372 | friend Error joinErrors(Error, Error); |
373 | |
374 | public: |
375 | void log(raw_ostream &OS) const override { |
376 | OS << "Multiple errors:\n"; |
377 | for (auto &ErrPayload : Payloads) { |
378 | ErrPayload->log(OS); |
379 | OS << "\n"; |
380 | } |
381 | } |
382 | |
383 | std::error_code convertToErrorCode() const override; |
384 | |
385 | // Used by ErrorInfo::classID. |
386 | static char ID; |
387 | |
388 | private: |
389 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, |
390 | std::unique_ptr<ErrorInfoBase> Payload2) { |
391 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 392, __PRETTY_FUNCTION__)) |
392 | "ErrorList constructor payloads should be singleton errors")((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 392, __PRETTY_FUNCTION__)); |
393 | Payloads.push_back(std::move(Payload1)); |
394 | Payloads.push_back(std::move(Payload2)); |
395 | } |
396 | |
397 | static Error join(Error E1, Error E2) { |
398 | if (!E1) |
399 | return E2; |
400 | if (!E2) |
401 | return E1; |
402 | if (E1.isA<ErrorList>()) { |
403 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); |
404 | if (E2.isA<ErrorList>()) { |
405 | auto E2Payload = E2.takePayload(); |
406 | auto &E2List = static_cast<ErrorList &>(*E2Payload); |
407 | for (auto &Payload : E2List.Payloads) |
408 | E1List.Payloads.push_back(std::move(Payload)); |
409 | } else |
410 | E1List.Payloads.push_back(E2.takePayload()); |
411 | |
412 | return E1; |
413 | } |
414 | if (E2.isA<ErrorList>()) { |
415 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); |
416 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); |
417 | return E2; |
418 | } |
419 | return Error(std::unique_ptr<ErrorList>( |
420 | new ErrorList(E1.takePayload(), E2.takePayload()))); |
421 | } |
422 | |
423 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; |
424 | }; |
425 | |
426 | /// Concatenate errors. The resulting Error is unchecked, and contains the |
427 | /// ErrorInfo(s), if any, contained in E1, followed by the |
428 | /// ErrorInfo(s), if any, contained in E2. |
429 | inline Error joinErrors(Error E1, Error E2) { |
430 | return ErrorList::join(std::move(E1), std::move(E2)); |
431 | } |
432 | |
433 | /// Tagged union holding either a T or a Error. |
434 | /// |
435 | /// This class parallels ErrorOr, but replaces error_code with Error. Since |
436 | /// Error cannot be copied, this class replaces getError() with |
437 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the |
438 | /// error class type. |
439 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { |
440 | template <class T1> friend class ExpectedAsOutParameter; |
441 | template <class OtherT> friend class Expected; |
442 | |
443 | static constexpr bool isRef = std::is_reference<T>::value; |
444 | |
445 | using wrap = std::reference_wrapper<std::remove_reference_t<T>>; |
446 | |
447 | using error_type = std::unique_ptr<ErrorInfoBase>; |
448 | |
449 | public: |
450 | using storage_type = std::conditional_t<isRef, wrap, T>; |
451 | using value_type = T; |
452 | |
453 | private: |
454 | using reference = std::remove_reference_t<T> &; |
455 | using const_reference = const std::remove_reference_t<T> &; |
456 | using pointer = std::remove_reference_t<T> *; |
457 | using const_pointer = const std::remove_reference_t<T> *; |
458 | |
459 | public: |
460 | /// Create an Expected<T> error value from the given Error. |
461 | Expected(Error Err) |
462 | : HasError(true) |
463 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
464 | // Expected is unchecked upon construction in Debug builds. |
465 | , Unchecked(true) |
466 | #endif |
467 | { |
468 | assert(Err && "Cannot create Expected<T> from Error success value.")((Err && "Cannot create Expected<T> from Error success value." ) ? static_cast<void> (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 468, __PRETTY_FUNCTION__)); |
469 | new (getErrorStorage()) error_type(Err.takePayload()); |
470 | } |
471 | |
472 | /// Forbid to convert from Error::success() implicitly, this avoids having |
473 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise |
474 | /// but triggers the assertion above. |
475 | Expected(ErrorSuccess) = delete; |
476 | |
477 | /// Create an Expected<T> success value from the given OtherT value, which |
478 | /// must be convertible to T. |
479 | template <typename OtherT> |
480 | Expected(OtherT &&Val, |
481 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) |
482 | : HasError(false) |
483 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
484 | // Expected is unchecked upon construction in Debug builds. |
485 | , |
486 | Unchecked(true) |
487 | #endif |
488 | { |
489 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); |
490 | } |
491 | |
492 | /// Move construct an Expected<T> value. |
493 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } |
494 | |
495 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
496 | /// must be convertible to T. |
497 | template <class OtherT> |
498 | Expected( |
499 | Expected<OtherT> &&Other, |
500 | std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) { |
501 | moveConstruct(std::move(Other)); |
502 | } |
503 | |
504 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT |
505 | /// isn't convertible to T. |
506 | template <class OtherT> |
507 | explicit Expected( |
508 | Expected<OtherT> &&Other, |
509 | std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) { |
510 | moveConstruct(std::move(Other)); |
511 | } |
512 | |
513 | /// Move-assign from another Expected<T>. |
514 | Expected &operator=(Expected &&Other) { |
515 | moveAssign(std::move(Other)); |
516 | return *this; |
517 | } |
518 | |
519 | /// Destroy an Expected<T>. |
520 | ~Expected() { |
521 | assertIsChecked(); |
522 | if (!HasError) |
523 | getStorage()->~storage_type(); |
524 | else |
525 | getErrorStorage()->~error_type(); |
526 | } |
527 | |
528 | /// Return false if there is an error. |
529 | explicit operator bool() { |
530 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
531 | Unchecked = HasError; |
532 | #endif |
533 | return !HasError; |
534 | } |
535 | |
536 | /// Returns a reference to the stored T value. |
537 | reference get() { |
538 | assertIsChecked(); |
539 | return *getStorage(); |
540 | } |
541 | |
542 | /// Returns a const reference to the stored T value. |
543 | const_reference get() const { |
544 | assertIsChecked(); |
545 | return const_cast<Expected<T> *>(this)->get(); |
546 | } |
547 | |
548 | /// Check that this Expected<T> is an error of type ErrT. |
549 | template <typename ErrT> bool errorIsA() const { |
550 | return HasError && (*getErrorStorage())->template isA<ErrT>(); |
551 | } |
552 | |
553 | /// Take ownership of the stored error. |
554 | /// After calling this the Expected<T> is in an indeterminate state that can |
555 | /// only be safely destructed. No further calls (beside the destructor) should |
556 | /// be made on the Expected<T> value. |
557 | Error takeError() { |
558 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
559 | Unchecked = false; |
560 | #endif |
561 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); |
562 | } |
563 | |
564 | /// Returns a pointer to the stored T value. |
565 | pointer operator->() { |
566 | assertIsChecked(); |
567 | return toPointer(getStorage()); |
568 | } |
569 | |
570 | /// Returns a const pointer to the stored T value. |
571 | const_pointer operator->() const { |
572 | assertIsChecked(); |
573 | return toPointer(getStorage()); |
574 | } |
575 | |
576 | /// Returns a reference to the stored T value. |
577 | reference operator*() { |
578 | assertIsChecked(); |
579 | return *getStorage(); |
580 | } |
581 | |
582 | /// Returns a const reference to the stored T value. |
583 | const_reference operator*() const { |
584 | assertIsChecked(); |
585 | return *getStorage(); |
586 | } |
587 | |
588 | private: |
589 | template <class T1> |
590 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { |
591 | return &a == &b; |
592 | } |
593 | |
594 | template <class T1, class T2> |
595 | static bool compareThisIfSameType(const T1 &a, const T2 &b) { |
596 | return false; |
597 | } |
598 | |
599 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { |
600 | HasError = Other.HasError; |
601 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
602 | Unchecked = true; |
603 | Other.Unchecked = false; |
604 | #endif |
605 | |
606 | if (!HasError) |
607 | new (getStorage()) storage_type(std::move(*Other.getStorage())); |
608 | else |
609 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); |
610 | } |
611 | |
612 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { |
613 | assertIsChecked(); |
614 | |
615 | if (compareThisIfSameType(*this, Other)) |
616 | return; |
617 | |
618 | this->~Expected(); |
619 | new (this) Expected(std::move(Other)); |
620 | } |
621 | |
622 | pointer toPointer(pointer Val) { return Val; } |
623 | |
624 | const_pointer toPointer(const_pointer Val) const { return Val; } |
625 | |
626 | pointer toPointer(wrap *Val) { return &Val->get(); } |
627 | |
628 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } |
629 | |
630 | storage_type *getStorage() { |
631 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 631, __PRETTY_FUNCTION__)); |
632 | return reinterpret_cast<storage_type *>(TStorage.buffer); |
633 | } |
634 | |
635 | const storage_type *getStorage() const { |
636 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 636, __PRETTY_FUNCTION__)); |
637 | return reinterpret_cast<const storage_type *>(TStorage.buffer); |
638 | } |
639 | |
640 | error_type *getErrorStorage() { |
641 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 641, __PRETTY_FUNCTION__)); |
642 | return reinterpret_cast<error_type *>(ErrorStorage.buffer); |
643 | } |
644 | |
645 | const error_type *getErrorStorage() const { |
646 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 646, __PRETTY_FUNCTION__)); |
647 | return reinterpret_cast<const error_type *>(ErrorStorage.buffer); |
648 | } |
649 | |
650 | // Used by ExpectedAsOutParameter to reset the checked flag. |
651 | void setUnchecked() { |
652 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
653 | Unchecked = true; |
654 | #endif |
655 | } |
656 | |
657 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
658 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) |
659 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) |
660 | void fatalUncheckedExpected() const { |
661 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; |
662 | if (HasError) { |
663 | dbgs() << "Unchecked Expected<T> contained error:\n"; |
664 | (*getErrorStorage())->log(dbgs()); |
665 | } else |
666 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " |
667 | "values in success mode must still be checked prior to being " |
668 | "destroyed).\n"; |
669 | abort(); |
670 | } |
671 | #endif |
672 | |
673 | void assertIsChecked() { |
674 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
675 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) |
676 | fatalUncheckedExpected(); |
677 | #endif |
678 | } |
679 | |
680 | union { |
681 | AlignedCharArrayUnion<storage_type> TStorage; |
682 | AlignedCharArrayUnion<error_type> ErrorStorage; |
683 | }; |
684 | bool HasError : 1; |
685 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
686 | bool Unchecked : 1; |
687 | #endif |
688 | }; |
689 | |
690 | /// Report a serious error, calling any installed error handler. See |
691 | /// ErrorHandling.h. |
692 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, |
693 | bool gen_crash_diag = true); |
694 | |
695 | /// Report a fatal error if Err is a failure value. |
696 | /// |
697 | /// This function can be used to wrap calls to fallible functions ONLY when it |
698 | /// is known that the Error will always be a success value. E.g. |
699 | /// |
700 | /// @code{.cpp} |
701 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
702 | /// // true. If DoFallibleOperation is false then foo always returns |
703 | /// // Error::success(). |
704 | /// Error foo(bool DoFallibleOperation); |
705 | /// |
706 | /// cantFail(foo(false)); |
707 | /// @endcode |
708 | inline void cantFail(Error Err, const char *Msg = nullptr) { |
709 | if (Err) { |
710 | if (!Msg) |
711 | Msg = "Failure value returned from cantFail wrapped call"; |
712 | #ifndef NDEBUG |
713 | std::string Str; |
714 | raw_string_ostream OS(Str); |
715 | OS << Msg << "\n" << Err; |
716 | Msg = OS.str().c_str(); |
717 | #endif |
718 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 718); |
719 | } |
720 | } |
721 | |
722 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
723 | /// returns the contained value. |
724 | /// |
725 | /// This function can be used to wrap calls to fallible functions ONLY when it |
726 | /// is known that the Error will always be a success value. E.g. |
727 | /// |
728 | /// @code{.cpp} |
729 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
730 | /// // true. If DoFallibleOperation is false then foo always returns an int. |
731 | /// Expected<int> foo(bool DoFallibleOperation); |
732 | /// |
733 | /// int X = cantFail(foo(false)); |
734 | /// @endcode |
735 | template <typename T> |
736 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { |
737 | if (ValOrErr) |
738 | return std::move(*ValOrErr); |
739 | else { |
740 | if (!Msg) |
741 | Msg = "Failure value returned from cantFail wrapped call"; |
742 | #ifndef NDEBUG |
743 | std::string Str; |
744 | raw_string_ostream OS(Str); |
745 | auto E = ValOrErr.takeError(); |
746 | OS << Msg << "\n" << E; |
747 | Msg = OS.str().c_str(); |
748 | #endif |
749 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 749); |
750 | } |
751 | } |
752 | |
753 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and |
754 | /// returns the contained reference. |
755 | /// |
756 | /// This function can be used to wrap calls to fallible functions ONLY when it |
757 | /// is known that the Error will always be a success value. E.g. |
758 | /// |
759 | /// @code{.cpp} |
760 | /// // foo only attempts the fallible operation if DoFallibleOperation is |
761 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. |
762 | /// Expected<Bar&> foo(bool DoFallibleOperation); |
763 | /// |
764 | /// Bar &X = cantFail(foo(false)); |
765 | /// @endcode |
766 | template <typename T> |
767 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { |
768 | if (ValOrErr) |
769 | return *ValOrErr; |
770 | else { |
771 | if (!Msg) |
772 | Msg = "Failure value returned from cantFail wrapped call"; |
773 | #ifndef NDEBUG |
774 | std::string Str; |
775 | raw_string_ostream OS(Str); |
776 | auto E = ValOrErr.takeError(); |
777 | OS << Msg << "\n" << E; |
778 | Msg = OS.str().c_str(); |
779 | #endif |
780 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 780); |
781 | } |
782 | } |
783 | |
784 | /// Helper for testing applicability of, and applying, handlers for |
785 | /// ErrorInfo types. |
786 | template <typename HandlerT> |
787 | class ErrorHandlerTraits |
788 | : public ErrorHandlerTraits<decltype( |
789 | &std::remove_reference<HandlerT>::type::operator())> {}; |
790 | |
791 | // Specialization functions of the form 'Error (const ErrT&)'. |
792 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { |
793 | public: |
794 | static bool appliesTo(const ErrorInfoBase &E) { |
795 | return E.template isA<ErrT>(); |
796 | } |
797 | |
798 | template <typename HandlerT> |
799 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
800 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 800, __PRETTY_FUNCTION__)); |
801 | return H(static_cast<ErrT &>(*E)); |
802 | } |
803 | }; |
804 | |
805 | // Specialization functions of the form 'void (const ErrT&)'. |
806 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { |
807 | public: |
808 | static bool appliesTo(const ErrorInfoBase &E) { |
809 | return E.template isA<ErrT>(); |
810 | } |
811 | |
812 | template <typename HandlerT> |
813 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
814 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 814, __PRETTY_FUNCTION__)); |
815 | H(static_cast<ErrT &>(*E)); |
816 | return Error::success(); |
817 | } |
818 | }; |
819 | |
820 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. |
821 | template <typename ErrT> |
822 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { |
823 | public: |
824 | static bool appliesTo(const ErrorInfoBase &E) { |
825 | return E.template isA<ErrT>(); |
826 | } |
827 | |
828 | template <typename HandlerT> |
829 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
830 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 830, __PRETTY_FUNCTION__)); |
831 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
832 | return H(std::move(SubE)); |
833 | } |
834 | }; |
835 | |
836 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. |
837 | template <typename ErrT> |
838 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { |
839 | public: |
840 | static bool appliesTo(const ErrorInfoBase &E) { |
841 | return E.template isA<ErrT>(); |
842 | } |
843 | |
844 | template <typename HandlerT> |
845 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { |
846 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 846, __PRETTY_FUNCTION__)); |
847 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); |
848 | H(std::move(SubE)); |
849 | return Error::success(); |
850 | } |
851 | }; |
852 | |
853 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
854 | template <typename C, typename RetT, typename ErrT> |
855 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> |
856 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
857 | |
858 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
859 | template <typename C, typename RetT, typename ErrT> |
860 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> |
861 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
862 | |
863 | // Specialization for member functions of the form 'RetT (const ErrT&)'. |
864 | template <typename C, typename RetT, typename ErrT> |
865 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> |
866 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
867 | |
868 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. |
869 | template <typename C, typename RetT, typename ErrT> |
870 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> |
871 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; |
872 | |
873 | /// Specialization for member functions of the form |
874 | /// 'RetT (std::unique_ptr<ErrT>)'. |
875 | template <typename C, typename RetT, typename ErrT> |
876 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> |
877 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
878 | |
879 | /// Specialization for member functions of the form |
880 | /// 'RetT (std::unique_ptr<ErrT>) const'. |
881 | template <typename C, typename RetT, typename ErrT> |
882 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> |
883 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; |
884 | |
885 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { |
886 | return Error(std::move(Payload)); |
887 | } |
888 | |
889 | template <typename HandlerT, typename... HandlerTs> |
890 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, |
891 | HandlerT &&Handler, HandlerTs &&... Handlers) { |
892 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) |
893 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), |
894 | std::move(Payload)); |
895 | return handleErrorImpl(std::move(Payload), |
896 | std::forward<HandlerTs>(Handlers)...); |
897 | } |
898 | |
899 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any |
900 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and |
901 | /// returned. |
902 | /// Because this function returns an error, its result must also be checked |
903 | /// or returned. If you intend to handle all errors use handleAllErrors |
904 | /// (which returns void, and will abort() on unhandled errors) instead. |
905 | template <typename... HandlerTs> |
906 | Error handleErrors(Error E, HandlerTs &&... Hs) { |
907 | if (!E) |
908 | return Error::success(); |
909 | |
910 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); |
911 | |
912 | if (Payload->isA<ErrorList>()) { |
913 | ErrorList &List = static_cast<ErrorList &>(*Payload); |
914 | Error R; |
915 | for (auto &P : List.Payloads) |
916 | R = ErrorList::join( |
917 | std::move(R), |
918 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); |
919 | return R; |
920 | } |
921 | |
922 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); |
923 | } |
924 | |
925 | /// Behaves the same as handleErrors, except that by contract all errors |
926 | /// *must* be handled by the given handlers (i.e. there must be no remaining |
927 | /// errors after running the handlers, or llvm_unreachable is called). |
928 | template <typename... HandlerTs> |
929 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { |
930 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); |
931 | } |
932 | |
933 | /// Check that E is a non-error, then drop it. |
934 | /// If E is an error, llvm_unreachable will be called. |
935 | inline void handleAllErrors(Error E) { |
936 | cantFail(std::move(E)); |
937 | } |
938 | |
939 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. |
940 | /// |
941 | /// If the incoming value is a success value it is returned unmodified. If it |
942 | /// is a failure value then it the contained error is passed to handleErrors. |
943 | /// If handleErrors is able to handle the error then the RecoveryPath functor |
944 | /// is called to supply the final result. If handleErrors is not able to |
945 | /// handle all errors then the unhandled errors are returned. |
946 | /// |
947 | /// This utility enables the follow pattern: |
948 | /// |
949 | /// @code{.cpp} |
950 | /// enum FooStrategy { Aggressive, Conservative }; |
951 | /// Expected<Foo> foo(FooStrategy S); |
952 | /// |
953 | /// auto ResultOrErr = |
954 | /// handleExpected( |
955 | /// foo(Aggressive), |
956 | /// []() { return foo(Conservative); }, |
957 | /// [](AggressiveStrategyError&) { |
958 | /// // Implicitly conusme this - we'll recover by using a conservative |
959 | /// // strategy. |
960 | /// }); |
961 | /// |
962 | /// @endcode |
963 | template <typename T, typename RecoveryFtor, typename... HandlerTs> |
964 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, |
965 | HandlerTs &&... Handlers) { |
966 | if (ValOrErr) |
967 | return ValOrErr; |
968 | |
969 | if (auto Err = handleErrors(ValOrErr.takeError(), |
970 | std::forward<HandlerTs>(Handlers)...)) |
971 | return std::move(Err); |
972 | |
973 | return RecoveryPath(); |
974 | } |
975 | |
976 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner |
977 | /// will be printed before the first one is logged. A newline will be printed |
978 | /// after each error. |
979 | /// |
980 | /// This function is compatible with the helpers from Support/WithColor.h. You |
981 | /// can pass any of them as the OS. Please consider using them instead of |
982 | /// including 'error: ' in the ErrorBanner. |
983 | /// |
984 | /// This is useful in the base level of your program to allow clean termination |
985 | /// (allowing clean deallocation of resources, etc.), while reporting error |
986 | /// information to the user. |
987 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); |
988 | |
989 | /// Write all error messages (if any) in E to a string. The newline character |
990 | /// is used to separate error messages. |
991 | inline std::string toString(Error E) { |
992 | SmallVector<std::string, 2> Errors; |
993 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { |
994 | Errors.push_back(EI.message()); |
995 | }); |
996 | return join(Errors.begin(), Errors.end(), "\n"); |
997 | } |
998 | |
999 | /// Consume a Error without doing anything. This method should be used |
1000 | /// only where an error can be considered a reasonable and expected return |
1001 | /// value. |
1002 | /// |
1003 | /// Uses of this method are potentially indicative of design problems: If it's |
1004 | /// legitimate to do nothing while processing an "error", the error-producer |
1005 | /// might be more clearly refactored to return an Optional<T>. |
1006 | inline void consumeError(Error Err) { |
1007 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); |
1008 | } |
1009 | |
1010 | /// Convert an Expected to an Optional without doing anything. This method |
1011 | /// should be used only where an error can be considered a reasonable and |
1012 | /// expected return value. |
1013 | /// |
1014 | /// Uses of this method are potentially indicative of problems: perhaps the |
1015 | /// error should be propagated further, or the error-producer should just |
1016 | /// return an Optional in the first place. |
1017 | template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) { |
1018 | if (E) |
1019 | return std::move(*E); |
1020 | consumeError(E.takeError()); |
1021 | return None; |
1022 | } |
1023 | |
1024 | /// Helper for converting an Error to a bool. |
1025 | /// |
1026 | /// This method returns true if Err is in an error state, or false if it is |
1027 | /// in a success state. Puts Err in a checked state in both cases (unlike |
1028 | /// Error::operator bool(), which only does this for success states). |
1029 | inline bool errorToBool(Error Err) { |
1030 | bool IsError = static_cast<bool>(Err); |
1031 | if (IsError) |
1032 | consumeError(std::move(Err)); |
1033 | return IsError; |
1034 | } |
1035 | |
1036 | /// Helper for Errors used as out-parameters. |
1037 | /// |
1038 | /// This helper is for use with the Error-as-out-parameter idiom, where an error |
1039 | /// is passed to a function or method by reference, rather than being returned. |
1040 | /// In such cases it is helpful to set the checked bit on entry to the function |
1041 | /// so that the error can be written to (unchecked Errors abort on assignment) |
1042 | /// and clear the checked bit on exit so that clients cannot accidentally forget |
1043 | /// to check the result. This helper performs these actions automatically using |
1044 | /// RAII: |
1045 | /// |
1046 | /// @code{.cpp} |
1047 | /// Result foo(Error &Err) { |
1048 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set |
1049 | /// // <body of foo> |
1050 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. |
1051 | /// } |
1052 | /// @endcode |
1053 | /// |
1054 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be |
1055 | /// used with optional Errors (Error pointers that are allowed to be null). If |
1056 | /// ErrorAsOutParameter took an Error reference, an instance would have to be |
1057 | /// created inside every condition that verified that Error was non-null. By |
1058 | /// taking an Error pointer we can just create one instance at the top of the |
1059 | /// function. |
1060 | class ErrorAsOutParameter { |
1061 | public: |
1062 | ErrorAsOutParameter(Error *Err) : Err(Err) { |
1063 | // Raise the checked bit if Err is success. |
1064 | if (Err) |
1065 | (void)!!*Err; |
1066 | } |
1067 | |
1068 | ~ErrorAsOutParameter() { |
1069 | // Clear the checked bit. |
1070 | if (Err && !*Err) |
1071 | *Err = Error::success(); |
1072 | } |
1073 | |
1074 | private: |
1075 | Error *Err; |
1076 | }; |
1077 | |
1078 | /// Helper for Expected<T>s used as out-parameters. |
1079 | /// |
1080 | /// See ErrorAsOutParameter. |
1081 | template <typename T> |
1082 | class ExpectedAsOutParameter { |
1083 | public: |
1084 | ExpectedAsOutParameter(Expected<T> *ValOrErr) |
1085 | : ValOrErr(ValOrErr) { |
1086 | if (ValOrErr) |
1087 | (void)!!*ValOrErr; |
1088 | } |
1089 | |
1090 | ~ExpectedAsOutParameter() { |
1091 | if (ValOrErr) |
1092 | ValOrErr->setUnchecked(); |
1093 | } |
1094 | |
1095 | private: |
1096 | Expected<T> *ValOrErr; |
1097 | }; |
1098 | |
1099 | /// This class wraps a std::error_code in a Error. |
1100 | /// |
1101 | /// This is useful if you're writing an interface that returns a Error |
1102 | /// (or Expected) and you want to call code that still returns |
1103 | /// std::error_codes. |
1104 | class ECError : public ErrorInfo<ECError> { |
1105 | friend Error errorCodeToError(std::error_code); |
1106 | |
1107 | virtual void anchor() override; |
1108 | |
1109 | public: |
1110 | void setErrorCode(std::error_code EC) { this->EC = EC; } |
1111 | std::error_code convertToErrorCode() const override { return EC; } |
1112 | void log(raw_ostream &OS) const override { OS << EC.message(); } |
1113 | |
1114 | // Used by ErrorInfo::classID. |
1115 | static char ID; |
1116 | |
1117 | protected: |
1118 | ECError() = default; |
1119 | ECError(std::error_code EC) : EC(EC) {} |
1120 | |
1121 | std::error_code EC; |
1122 | }; |
1123 | |
1124 | /// The value returned by this function can be returned from convertToErrorCode |
1125 | /// for Error values where no sensible translation to std::error_code exists. |
1126 | /// It should only be used in this situation, and should never be used where a |
1127 | /// sensible conversion to std::error_code is available, as attempts to convert |
1128 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic |
1129 | ///error to try to convert such a value). |
1130 | std::error_code inconvertibleErrorCode(); |
1131 | |
1132 | /// Helper for converting an std::error_code to a Error. |
1133 | Error errorCodeToError(std::error_code EC); |
1134 | |
1135 | /// Helper for converting an ECError to a std::error_code. |
1136 | /// |
1137 | /// This method requires that Err be Error() or an ECError, otherwise it |
1138 | /// will trigger a call to abort(). |
1139 | std::error_code errorToErrorCode(Error Err); |
1140 | |
1141 | /// Convert an ErrorOr<T> to an Expected<T>. |
1142 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { |
1143 | if (auto EC = EO.getError()) |
1144 | return errorCodeToError(EC); |
1145 | return std::move(*EO); |
1146 | } |
1147 | |
1148 | /// Convert an Expected<T> to an ErrorOr<T>. |
1149 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { |
1150 | if (auto Err = E.takeError()) |
1151 | return errorToErrorCode(std::move(Err)); |
1152 | return std::move(*E); |
1153 | } |
1154 | |
1155 | /// This class wraps a string in an Error. |
1156 | /// |
1157 | /// StringError is useful in cases where the client is not expected to be able |
1158 | /// to consume the specific error message programmatically (for example, if the |
1159 | /// error message is to be presented to the user). |
1160 | /// |
1161 | /// StringError can also be used when additional information is to be printed |
1162 | /// along with a error_code message. Depending on the constructor called, this |
1163 | /// class can either display: |
1164 | /// 1. the error_code message (ECError behavior) |
1165 | /// 2. a string |
1166 | /// 3. the error_code message and a string |
1167 | /// |
1168 | /// These behaviors are useful when subtyping is required; for example, when a |
1169 | /// specific library needs an explicit error type. In the example below, |
1170 | /// PDBError is derived from StringError: |
1171 | /// |
1172 | /// @code{.cpp} |
1173 | /// Expected<int> foo() { |
1174 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, |
1175 | /// "Additional information"); |
1176 | /// } |
1177 | /// @endcode |
1178 | /// |
1179 | class StringError : public ErrorInfo<StringError> { |
1180 | public: |
1181 | static char ID; |
1182 | |
1183 | // Prints EC + S and converts to EC |
1184 | StringError(std::error_code EC, const Twine &S = Twine()); |
1185 | |
1186 | // Prints S and converts to EC |
1187 | StringError(const Twine &S, std::error_code EC); |
1188 | |
1189 | void log(raw_ostream &OS) const override; |
1190 | std::error_code convertToErrorCode() const override; |
1191 | |
1192 | const std::string &getMessage() const { return Msg; } |
1193 | |
1194 | private: |
1195 | std::string Msg; |
1196 | std::error_code EC; |
1197 | const bool PrintMsgOnly = false; |
1198 | }; |
1199 | |
1200 | /// Create formatted StringError object. |
1201 | template <typename... Ts> |
1202 | inline Error createStringError(std::error_code EC, char const *Fmt, |
1203 | const Ts &... Vals) { |
1204 | std::string Buffer; |
1205 | raw_string_ostream Stream(Buffer); |
1206 | Stream << format(Fmt, Vals...); |
1207 | return make_error<StringError>(Stream.str(), EC); |
1208 | } |
1209 | |
1210 | Error createStringError(std::error_code EC, char const *Msg); |
1211 | |
1212 | inline Error createStringError(std::error_code EC, const Twine &S) { |
1213 | return createStringError(EC, S.str().c_str()); |
1214 | } |
1215 | |
1216 | template <typename... Ts> |
1217 | inline Error createStringError(std::errc EC, char const *Fmt, |
1218 | const Ts &... Vals) { |
1219 | return createStringError(std::make_error_code(EC), Fmt, Vals...); |
1220 | } |
1221 | |
1222 | /// This class wraps a filename and another Error. |
1223 | /// |
1224 | /// In some cases, an error needs to live along a 'source' name, in order to |
1225 | /// show more detailed information to the user. |
1226 | class FileError final : public ErrorInfo<FileError> { |
1227 | |
1228 | friend Error createFileError(const Twine &, Error); |
1229 | friend Error createFileError(const Twine &, size_t, Error); |
1230 | |
1231 | public: |
1232 | void log(raw_ostream &OS) const override { |
1233 | assert(Err && !FileName.empty() && "Trying to log after takeError().")((Err && !FileName.empty() && "Trying to log after takeError()." ) ? static_cast<void> (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 1233, __PRETTY_FUNCTION__)); |
1234 | OS << "'" << FileName << "': "; |
1235 | if (Line.hasValue()) |
1236 | OS << "line " << Line.getValue() << ": "; |
1237 | Err->log(OS); |
1238 | } |
1239 | |
1240 | StringRef getFileName() { return FileName; } |
1241 | |
1242 | Error takeError() { return Error(std::move(Err)); } |
1243 | |
1244 | std::error_code convertToErrorCode() const override; |
1245 | |
1246 | // Used by ErrorInfo::classID. |
1247 | static char ID; |
1248 | |
1249 | private: |
1250 | FileError(const Twine &F, Optional<size_t> LineNum, |
1251 | std::unique_ptr<ErrorInfoBase> E) { |
1252 | assert(E && "Cannot create FileError from Error success value.")((E && "Cannot create FileError from Error success value." ) ? static_cast<void> (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 1252, __PRETTY_FUNCTION__)); |
1253 | assert(!F.isTriviallyEmpty() &&((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 1254, __PRETTY_FUNCTION__)) |
1254 | "The file name provided to FileError must not be empty.")((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-12~++20200927111121+5811d723998/llvm/include/llvm/Support/Error.h" , 1254, __PRETTY_FUNCTION__)); |
1255 | FileName = F.str(); |
1256 | Err = std::move(E); |
1257 | Line = std::move(LineNum); |
1258 | } |
1259 | |
1260 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { |
1261 | std::unique_ptr<ErrorInfoBase> Payload; |
1262 | handleAllErrors(std::move(E), |
1263 | [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error { |
1264 | Payload = std::move(EIB); |
1265 | return Error::success(); |
1266 | }); |
1267 | return Error( |
1268 | std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload)))); |
1269 | } |
1270 | |
1271 | std::string FileName; |
1272 | Optional<size_t> Line; |
1273 | std::unique_ptr<ErrorInfoBase> Err; |
1274 | }; |
1275 | |
1276 | /// Concatenate a source file path and/or name with an Error. The resulting |
1277 | /// Error is unchecked. |
1278 | inline Error createFileError(const Twine &F, Error E) { |
1279 | return FileError::build(F, Optional<size_t>(), std::move(E)); |
1280 | } |
1281 | |
1282 | /// Concatenate a source file path and/or name with line number and an Error. |
1283 | /// The resulting Error is unchecked. |
1284 | inline Error createFileError(const Twine &F, size_t Line, Error E) { |
1285 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); |
1286 | } |
1287 | |
1288 | /// Concatenate a source file path and/or name with a std::error_code |
1289 | /// to form an Error object. |
1290 | inline Error createFileError(const Twine &F, std::error_code EC) { |
1291 | return createFileError(F, errorCodeToError(EC)); |
1292 | } |
1293 | |
1294 | /// Concatenate a source file path and/or name with line number and |
1295 | /// std::error_code to form an Error object. |
1296 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { |
1297 | return createFileError(F, Line, errorCodeToError(EC)); |
1298 | } |
1299 | |
1300 | Error createFileError(const Twine &F, ErrorSuccess) = delete; |
1301 | |
1302 | /// Helper for check-and-exit error handling. |
1303 | /// |
1304 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. |
1305 | /// |
1306 | class ExitOnError { |
1307 | public: |
1308 | /// Create an error on exit helper. |
1309 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) |
1310 | : Banner(std::move(Banner)), |
1311 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} |
1312 | |
1313 | /// Set the banner string for any errors caught by operator(). |
1314 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } |
1315 | |
1316 | /// Set the exit-code mapper function. |
1317 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { |
1318 | this->GetExitCode = std::move(GetExitCode); |
1319 | } |
1320 | |
1321 | /// Check Err. If it's in a failure state log the error(s) and exit. |
1322 | void operator()(Error Err) const { checkError(std::move(Err)); } |
1323 | |
1324 | /// Check E. If it's in a success state then return the contained value. If |
1325 | /// it's in a failure state log the error(s) and exit. |
1326 | template <typename T> T operator()(Expected<T> &&E) const { |
1327 | checkError(E.takeError()); |
1328 | return std::move(*E); |
1329 | } |
1330 | |
1331 | /// Check E. If it's in a success state then return the contained reference. If |
1332 | /// it's in a failure state log the error(s) and exit. |
1333 | template <typename T> T& operator()(Expected<T&> &&E) const { |
1334 | checkError(E.takeError()); |
1335 | return *E; |
1336 | } |
1337 | |
1338 | private: |
1339 | void checkError(Error Err) const { |
1340 | if (Err) { |
1341 | int ExitCode = GetExitCode(Err); |
1342 | logAllUnhandledErrors(std::move(Err), errs(), Banner); |
1343 | exit(ExitCode); |
1344 | } |
1345 | } |
1346 | |
1347 | std::string Banner; |
1348 | std::function<int(const Error &)> GetExitCode; |
1349 | }; |
1350 | |
1351 | /// Conversion from Error to LLVMErrorRef for C error bindings. |
1352 | inline LLVMErrorRef wrap(Error Err) { |
1353 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); |
1354 | } |
1355 | |
1356 | /// Conversion from LLVMErrorRef to Error for C error bindings. |
1357 | inline Error unwrap(LLVMErrorRef ErrRef) { |
1358 | return Error(std::unique_ptr<ErrorInfoBase>( |
1359 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); |
1360 | } |
1361 | |
1362 | } // end namespace llvm |
1363 | |
1364 | #endif // LLVM_SUPPORT_ERROR_H |