Bug Summary

File:llvm/include/llvm/Support/Error.h
Warning:line 521, column 5
Assigned value is garbage or undefined

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name llvm-jitlink.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/llvm-jitlink -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/tools/llvm-jitlink -I include -I /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-19-134126-35450-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/tools/llvm-jitlink/llvm-jitlink.cpp

/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/tools/llvm-jitlink/llvm-jitlink.cpp

1//===- llvm-jitlink.cpp -- Command line interface/tester for llvm-jitlink -===//
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 utility provides a simple command line interface to the llvm jitlink
10// library, which makes relocatable object files executable in memory. Its
11// primary function is as a testing utility for the jitlink library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-jitlink.h"
16
17#include "llvm/BinaryFormat/Magic.h"
18#include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
19#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
20#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
21#include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
22#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
23#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
24#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
25#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
26#include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
27#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
28#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
29#include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
30#include "llvm/MC/MCAsmInfo.h"
31#include "llvm/MC/MCContext.h"
32#include "llvm/MC/MCDisassembler/MCDisassembler.h"
33#include "llvm/MC/MCInstPrinter.h"
34#include "llvm/MC/MCInstrAnalysis.h"
35#include "llvm/MC/MCInstrInfo.h"
36#include "llvm/MC/MCRegisterInfo.h"
37#include "llvm/MC/MCSubtargetInfo.h"
38#include "llvm/MC/MCTargetOptions.h"
39#include "llvm/MC/TargetRegistry.h"
40#include "llvm/Object/COFF.h"
41#include "llvm/Object/MachO.h"
42#include "llvm/Object/ObjectFile.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/InitLLVM.h"
46#include "llvm/Support/MemoryBuffer.h"
47#include "llvm/Support/Path.h"
48#include "llvm/Support/Process.h"
49#include "llvm/Support/TargetSelect.h"
50#include "llvm/Support/Timer.h"
51
52#include <cstring>
53#include <list>
54#include <string>
55
56#ifdef LLVM_ON_UNIX1
57#include <netdb.h>
58#include <netinet/in.h>
59#include <sys/socket.h>
60#include <unistd.h>
61#endif // LLVM_ON_UNIX
62
63#define DEBUG_TYPE"llvm_jitlink" "llvm_jitlink"
64
65using namespace llvm;
66using namespace llvm::jitlink;
67using namespace llvm::orc;
68
69static cl::OptionCategory JITLinkCategory("JITLink Options");
70
71static cl::list<std::string> InputFiles(cl::Positional, cl::OneOrMore,
72 cl::desc("input files"),
73 cl::cat(JITLinkCategory));
74
75static cl::list<std::string>
76 LibrarySearchPaths("L",
77 cl::desc("Add dir to the list of library search paths"),
78 cl::Prefix, cl::cat(JITLinkCategory));
79
80static cl::list<std::string>
81 Libraries("l",
82 cl::desc("Link against library X in the library search paths"),
83 cl::Prefix, cl::cat(JITLinkCategory));
84
85static cl::list<std::string>
86 LibrariesHidden("hidden-l",
87 cl::desc("Link against library X in the library search "
88 "paths with hidden visibility"),
89 cl::Prefix, cl::cat(JITLinkCategory));
90
91static cl::list<std::string>
92 LoadHidden("load_hidden",
93 cl::desc("Link against library X with hidden visibility"),
94 cl::cat(JITLinkCategory));
95
96static cl::opt<bool> NoExec("noexec", cl::desc("Do not execute loaded code"),
97 cl::init(false), cl::cat(JITLinkCategory));
98
99static cl::list<std::string>
100 CheckFiles("check", cl::desc("File containing verifier checks"),
101 cl::ZeroOrMore, cl::cat(JITLinkCategory));
102
103static cl::opt<std::string>
104 CheckName("check-name", cl::desc("Name of checks to match against"),
105 cl::init("jitlink-check"), cl::cat(JITLinkCategory));
106
107static cl::opt<std::string>
108 EntryPointName("entry", cl::desc("Symbol to call as main entry point"),
109 cl::init(""), cl::cat(JITLinkCategory));
110
111static cl::list<std::string> JITDylibs(
112 "jd",
113 cl::desc("Specifies the JITDylib to be used for any subsequent "
114 "input file, -L<seacrh-path>, and -l<library> arguments"),
115 cl::cat(JITLinkCategory));
116
117static cl::list<std::string>
118 Dylibs("preload",
119 cl::desc("Pre-load dynamic libraries (e.g. language runtimes "
120 "required by the ORC runtime)"),
121 cl::ZeroOrMore, cl::cat(JITLinkCategory));
122
123static cl::list<std::string> InputArgv("args", cl::Positional,
124 cl::desc("<program arguments>..."),
125 cl::ZeroOrMore, cl::PositionalEatsArgs,
126 cl::cat(JITLinkCategory));
127
128static cl::opt<bool>
129 DebuggerSupport("debugger-support",
130 cl::desc("Enable debugger suppport (default = !-noexec)"),
131 cl::init(true), cl::Hidden, cl::cat(JITLinkCategory));
132
133static cl::opt<bool>
134 NoProcessSymbols("no-process-syms",
135 cl::desc("Do not resolve to llvm-jitlink process symbols"),
136 cl::init(false), cl::cat(JITLinkCategory));
137
138static cl::list<std::string> AbsoluteDefs(
139 "define-abs",
140 cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
141 cl::ZeroOrMore, cl::cat(JITLinkCategory));
142
143static cl::list<std::string> TestHarnesses("harness", cl::Positional,
144 cl::desc("Test harness files"),
145 cl::ZeroOrMore,
146 cl::PositionalEatsArgs,
147 cl::cat(JITLinkCategory));
148
149static cl::opt<bool> ShowInitialExecutionSessionState(
150 "show-init-es",
151 cl::desc("Print ExecutionSession state before resolving entry point"),
152 cl::init(false), cl::cat(JITLinkCategory));
153
154static cl::opt<bool> ShowEntryExecutionSessionState(
155 "show-entry-es",
156 cl::desc("Print ExecutionSession state after resolving entry point"),
157 cl::init(false), cl::cat(JITLinkCategory));
158
159static cl::opt<bool> ShowAddrs(
160 "show-addrs",
161 cl::desc("Print registered symbol, section, got and stub addresses"),
162 cl::init(false), cl::cat(JITLinkCategory));
163
164static cl::opt<bool> ShowLinkGraph(
165 "show-graph",
166 cl::desc("Print the link graph after fixups have been applied"),
167 cl::init(false), cl::cat(JITLinkCategory));
168
169static cl::opt<bool> ShowSizes(
170 "show-sizes",
171 cl::desc("Show sizes pre- and post-dead stripping, and allocations"),
172 cl::init(false), cl::cat(JITLinkCategory));
173
174static cl::opt<bool> ShowTimes("show-times",
175 cl::desc("Show times for llvm-jitlink phases"),
176 cl::init(false), cl::cat(JITLinkCategory));
177
178static cl::opt<std::string> SlabAllocateSizeString(
179 "slab-allocate",
180 cl::desc("Allocate from a slab of the given size "
181 "(allowable suffixes: Kb, Mb, Gb. default = "
182 "Kb)"),
183 cl::init(""), cl::cat(JITLinkCategory));
184
185static cl::opt<uint64_t> SlabAddress(
186 "slab-address",
187 cl::desc("Set slab target address (requires -slab-allocate and -noexec)"),
188 cl::init(~0ULL), cl::cat(JITLinkCategory));
189
190static cl::opt<uint64_t> SlabPageSize(
191 "slab-page-size",
192 cl::desc("Set page size for slab (requires -slab-allocate and -noexec)"),
193 cl::init(0), cl::cat(JITLinkCategory));
194
195static cl::opt<bool> ShowRelocatedSectionContents(
196 "show-relocated-section-contents",
197 cl::desc("show section contents after fixups have been applied"),
198 cl::init(false), cl::cat(JITLinkCategory));
199
200static cl::opt<bool> PhonyExternals(
201 "phony-externals",
202 cl::desc("resolve all otherwise unresolved externals to null"),
203 cl::init(false), cl::cat(JITLinkCategory));
204
205static cl::opt<std::string> OutOfProcessExecutor(
206 "oop-executor", cl::desc("Launch an out-of-process executor to run code"),
207 cl::ValueOptional, cl::cat(JITLinkCategory));
208
209static cl::opt<std::string> OutOfProcessExecutorConnect(
210 "oop-executor-connect",
211 cl::desc("Connect to an out-of-process executor via TCP"),
212 cl::cat(JITLinkCategory));
213
214static cl::opt<std::string>
215 OrcRuntime("orc-runtime", cl::desc("Use ORC runtime from given path"),
216 cl::init(""), cl::cat(JITLinkCategory));
217
218static cl::opt<bool> AddSelfRelocations(
219 "add-self-relocations",
220 cl::desc("Add relocations to function pointers to the current function"),
221 cl::init(false), cl::cat(JITLinkCategory));
222
223ExitOnError ExitOnErr;
224
225LLVM_ATTRIBUTE_USED__attribute__((__used__)) void linkComponents() {
226 errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
227 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
228 << (void *)&llvm_orc_registerJITLoaderGDBWrapper;
229}
230
231static bool UseTestResultOverride = false;
232static int64_t TestResultOverride = 0;
233
234extern "C" LLVM_ATTRIBUTE_USED__attribute__((__used__)) void
235llvm_jitlink_setTestResultOverride(int64_t Value) {
236 TestResultOverride = Value;
237 UseTestResultOverride = true;
238}
239
240static Error addSelfRelocations(LinkGraph &G);
241
242namespace llvm {
243
244static raw_ostream &
245operator<<(raw_ostream &OS, const Session::MemoryRegionInfo &MRI) {
246 return OS << "target addr = "
247 << format("0x%016" PRIx64"l" "x", MRI.getTargetAddress())
248 << ", content: " << (const void *)MRI.getContent().data() << " -- "
249 << (const void *)(MRI.getContent().data() + MRI.getContent().size())
250 << " (" << MRI.getContent().size() << " bytes)";
251}
252
253static raw_ostream &
254operator<<(raw_ostream &OS, const Session::SymbolInfoMap &SIM) {
255 OS << "Symbols:\n";
256 for (auto &SKV : SIM)
257 OS << " \"" << SKV.first() << "\" " << SKV.second << "\n";
258 return OS;
259}
260
261static raw_ostream &
262operator<<(raw_ostream &OS, const Session::FileInfo &FI) {
263 for (auto &SIKV : FI.SectionInfos)
264 OS << " Section \"" << SIKV.first() << "\": " << SIKV.second << "\n";
265 for (auto &GOTKV : FI.GOTEntryInfos)
266 OS << " GOT \"" << GOTKV.first() << "\": " << GOTKV.second << "\n";
267 for (auto &StubKV : FI.StubInfos)
268 OS << " Stub \"" << StubKV.first() << "\": " << StubKV.second << "\n";
269 return OS;
270}
271
272static raw_ostream &
273operator<<(raw_ostream &OS, const Session::FileInfoMap &FIM) {
274 for (auto &FIKV : FIM)
275 OS << "File \"" << FIKV.first() << "\":\n" << FIKV.second;
276 return OS;
277}
278
279static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
280
281 // If this graph is part of the test harness there's nothing to do.
282 if (S.HarnessFiles.empty() || S.HarnessFiles.count(G.getName()))
283 return Error::success();
284
285 LLVM_DEBUG(dbgs() << "Applying promotions to graph " << G.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Applying promotions to graph "
<< G.getName() << "\n"; } } while (false)
;
286
287 // If this graph is part of the test then promote any symbols referenced by
288 // the harness to default scope, remove all symbols that clash with harness
289 // definitions.
290 std::vector<Symbol *> DefinitionsToRemove;
291 for (auto *Sym : G.defined_symbols()) {
292
293 if (!Sym->hasName())
294 continue;
295
296 if (Sym->getLinkage() == Linkage::Weak) {
297 if (!S.CanonicalWeakDefs.count(Sym->getName()) ||
298 S.CanonicalWeakDefs[Sym->getName()] != G.getName()) {
299 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Externalizing weak symbol "
<< Sym->getName() << "\n"; }; } } while (false
)
300 dbgs() << " Externalizing weak symbol " << Sym->getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Externalizing weak symbol "
<< Sym->getName() << "\n"; }; } } while (false
)
301 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Externalizing weak symbol "
<< Sym->getName() << "\n"; }; } } while (false
)
;
302 DefinitionsToRemove.push_back(Sym);
303 } else {
304 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Making weak symbol "
<< Sym->getName() << " strong\n"; }; } } while
(false)
305 dbgs() << " Making weak symbol " << Sym->getName() << " strong\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Making weak symbol "
<< Sym->getName() << " strong\n"; }; } } while
(false)
306 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " Making weak symbol "
<< Sym->getName() << " strong\n"; }; } } while
(false)
;
307 if (S.HarnessExternals.count(Sym->getName()))
308 Sym->setScope(Scope::Default);
309 else
310 Sym->setScope(Scope::Hidden);
311 Sym->setLinkage(Linkage::Strong);
312 }
313 } else if (S.HarnessExternals.count(Sym->getName())) {
314 LLVM_DEBUG(dbgs() << " Promoting " << Sym->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " Promoting " << Sym
->getName() << "\n"; } } while (false)
;
315 Sym->setScope(Scope::Default);
316 Sym->setLive(true);
317 continue;
318 } else if (S.HarnessDefinitions.count(Sym->getName())) {
319 LLVM_DEBUG(dbgs() << " Externalizing " << Sym->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " Externalizing " <<
Sym->getName() << "\n"; } } while (false)
;
320 DefinitionsToRemove.push_back(Sym);
321 }
322 }
323
324 for (auto *Sym : DefinitionsToRemove)
325 G.makeExternal(*Sym);
326
327 return Error::success();
328}
329
330static uint64_t computeTotalBlockSizes(LinkGraph &G) {
331 uint64_t TotalSize = 0;
332 for (auto *B : G.blocks())
333 TotalSize += B->getSize();
334 return TotalSize;
335}
336
337static void dumpSectionContents(raw_ostream &OS, LinkGraph &G) {
338 constexpr orc::ExecutorAddrDiff DumpWidth = 16;
339 static_assert(isPowerOf2_64(DumpWidth), "DumpWidth must be a power of two");
340
341 // Put sections in address order.
342 std::vector<Section *> Sections;
343 for (auto &S : G.sections())
344 Sections.push_back(&S);
345
346 llvm::sort(Sections, [](const Section *LHS, const Section *RHS) {
347 if (llvm::empty(LHS->symbols()) && llvm::empty(RHS->symbols()))
348 return false;
349 if (llvm::empty(LHS->symbols()))
350 return false;
351 if (llvm::empty(RHS->symbols()))
352 return true;
353 SectionRange LHSRange(*LHS);
354 SectionRange RHSRange(*RHS);
355 return LHSRange.getStart() < RHSRange.getStart();
356 });
357
358 for (auto *S : Sections) {
359 OS << S->getName() << " content:";
360 if (llvm::empty(S->symbols())) {
361 OS << "\n section empty\n";
362 continue;
363 }
364
365 // Sort symbols into order, then render.
366 std::vector<Symbol *> Syms(S->symbols().begin(), S->symbols().end());
367 llvm::sort(Syms, [](const Symbol *LHS, const Symbol *RHS) {
368 return LHS->getAddress() < RHS->getAddress();
369 });
370
371 orc::ExecutorAddr NextAddr(Syms.front()->getAddress().getValue() &
372 ~(DumpWidth - 1));
373 for (auto *Sym : Syms) {
374 bool IsZeroFill = Sym->getBlock().isZeroFill();
375 auto SymStart = Sym->getAddress();
376 auto SymSize = Sym->getSize();
377 auto SymEnd = SymStart + SymSize;
378 const uint8_t *SymData = IsZeroFill ? nullptr
379 : reinterpret_cast<const uint8_t *>(
380 Sym->getSymbolContent().data());
381
382 // Pad any space before the symbol starts.
383 while (NextAddr != SymStart) {
384 if (NextAddr % DumpWidth == 0)
385 OS << formatv("\n{0:x16}:", NextAddr);
386 OS << " ";
387 ++NextAddr;
388 }
389
390 // Render the symbol content.
391 while (NextAddr != SymEnd) {
392 if (NextAddr % DumpWidth == 0)
393 OS << formatv("\n{0:x16}:", NextAddr);
394 if (IsZeroFill)
395 OS << " 00";
396 else
397 OS << formatv(" {0:x-2}", SymData[NextAddr - SymStart]);
398 ++NextAddr;
399 }
400 }
401 OS << "\n";
402 }
403}
404
405class JITLinkSlabAllocator final : public JITLinkMemoryManager {
406private:
407 struct FinalizedAllocInfo {
408 FinalizedAllocInfo(sys::MemoryBlock Mem,
409 std::vector<shared::WrapperFunctionCall> DeallocActions)
410 : Mem(Mem), DeallocActions(std::move(DeallocActions)) {}
411 sys::MemoryBlock Mem;
412 std::vector<shared::WrapperFunctionCall> DeallocActions;
413 };
414
415public:
416 static Expected<std::unique_ptr<JITLinkSlabAllocator>>
417 Create(uint64_t SlabSize) {
418 Error Err = Error::success();
419 std::unique_ptr<JITLinkSlabAllocator> Allocator(
420 new JITLinkSlabAllocator(SlabSize, Err));
421 if (Err)
422 return std::move(Err);
423 return std::move(Allocator);
424 }
425
426 void allocate(const JITLinkDylib *JD, LinkGraph &G,
427 OnAllocatedFunction OnAllocated) override {
428
429 // Local class for allocation.
430 class IPMMAlloc : public InFlightAlloc {
431 public:
432 IPMMAlloc(JITLinkSlabAllocator &Parent, BasicLayout BL,
433 sys::MemoryBlock StandardSegs, sys::MemoryBlock FinalizeSegs)
434 : Parent(Parent), BL(std::move(BL)),
435 StandardSegs(std::move(StandardSegs)),
436 FinalizeSegs(std::move(FinalizeSegs)) {}
437
438 void finalize(OnFinalizedFunction OnFinalized) override {
439 if (auto Err = applyProtections()) {
440 OnFinalized(std::move(Err));
441 return;
442 }
443
444 auto DeallocActions = runFinalizeActions(BL.graphAllocActions());
445 if (!DeallocActions) {
446 OnFinalized(DeallocActions.takeError());
447 return;
448 }
449
450 if (auto Err = Parent.freeBlock(FinalizeSegs)) {
451 OnFinalized(
452 joinErrors(std::move(Err), runDeallocActions(*DeallocActions)));
453 return;
454 }
455
456 OnFinalized(FinalizedAlloc(ExecutorAddr::fromPtr(
457 new FinalizedAllocInfo(StandardSegs, std::move(*DeallocActions)))));
458 }
459
460 void abandon(OnAbandonedFunction OnAbandoned) override {
461 OnAbandoned(joinErrors(Parent.freeBlock(StandardSegs),
462 Parent.freeBlock(FinalizeSegs)));
463 }
464
465 private:
466 Error applyProtections() {
467 for (auto &KV : BL.segments()) {
468 const auto &Group = KV.first;
469 auto &Seg = KV.second;
470
471 auto Prot = toSysMemoryProtectionFlags(Group.getMemProt());
472
473 uint64_t SegSize =
474 alignTo(Seg.ContentSize + Seg.ZeroFillSize, Parent.PageSize);
475 sys::MemoryBlock MB(Seg.WorkingMem, SegSize);
476 if (auto EC = sys::Memory::protectMappedMemory(MB, Prot))
477 return errorCodeToError(EC);
478 if (Prot & sys::Memory::MF_EXEC)
479 sys::Memory::InvalidateInstructionCache(MB.base(),
480 MB.allocatedSize());
481 }
482 return Error::success();
483 }
484
485 JITLinkSlabAllocator &Parent;
486 BasicLayout BL;
487 sys::MemoryBlock StandardSegs;
488 sys::MemoryBlock FinalizeSegs;
489 };
490
491 BasicLayout BL(G);
492 auto SegsSizes = BL.getContiguousPageBasedLayoutSizes(PageSize);
493
494 if (!SegsSizes) {
495 OnAllocated(SegsSizes.takeError());
496 return;
497 }
498
499 char *AllocBase = nullptr;
500 {
501 std::lock_guard<std::mutex> Lock(SlabMutex);
502
503 if (SegsSizes->total() > SlabRemaining.allocatedSize()) {
504 OnAllocated(make_error<StringError>(
505 "Slab allocator out of memory: request for " +
506 formatv("{0:x}", SegsSizes->total()) +
507 " bytes exceeds remaining capacity of " +
508 formatv("{0:x}", SlabRemaining.allocatedSize()) + " bytes",
509 inconvertibleErrorCode()));
510 return;
511 }
512
513 AllocBase = reinterpret_cast<char *>(SlabRemaining.base());
514 SlabRemaining =
515 sys::MemoryBlock(AllocBase + SegsSizes->total(),
516 SlabRemaining.allocatedSize() - SegsSizes->total());
517 }
518
519 sys::MemoryBlock StandardSegs(AllocBase, SegsSizes->StandardSegs);
520 sys::MemoryBlock FinalizeSegs(AllocBase + SegsSizes->StandardSegs,
521 SegsSizes->FinalizeSegs);
522
523 auto NextStandardSegAddr = ExecutorAddr::fromPtr(StandardSegs.base());
524 auto NextFinalizeSegAddr = ExecutorAddr::fromPtr(FinalizeSegs.base());
525
526 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
527 dbgs() << "JITLinkSlabAllocator allocated:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
528 if (SegsSizes->StandardSegs)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
529 dbgs() << formatv(" [ {0:x16} -- {1:x16} ]", NextStandardSegAddr,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
530 NextStandardSegAddr + StandardSegs.allocatedSize())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
531 << " to stardard segs\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
532 elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
533 dbgs() << " no standard segs\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
534 if (SegsSizes->FinalizeSegs)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
535 dbgs() << formatv(" [ {0:x16} -- {1:x16} ]", NextFinalizeSegAddr,do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
536 NextFinalizeSegAddr + FinalizeSegs.allocatedSize())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
537 << " to finalize segs\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
538 elsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
539 dbgs() << " no finalize segs\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
540 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "JITLinkSlabAllocator allocated:\n"
; if (SegsSizes->StandardSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextStandardSegAddr, NextStandardSegAddr + StandardSegs.allocatedSize
()) << " to stardard segs\n"; else dbgs() << " no standard segs\n"
; if (SegsSizes->FinalizeSegs) dbgs() << formatv(" [ {0:x16} -- {1:x16} ]"
, NextFinalizeSegAddr, NextFinalizeSegAddr + FinalizeSegs.allocatedSize
()) << " to finalize segs\n"; else dbgs() << " no finalize segs\n"
; }; } } while (false)
;
541
542 for (auto &KV : BL.segments()) {
543 auto &Group = KV.first;
544 auto &Seg = KV.second;
545
546 auto &SegAddr =
547 (Group.getMemDeallocPolicy() == MemDeallocPolicy::Standard)
548 ? NextStandardSegAddr
549 : NextFinalizeSegAddr;
550
551 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " " << Group <<
" -> " << formatv("{0:x16}", SegAddr) << "\n"
; }; } } while (false)
552 dbgs() << " " << Group << " -> " << formatv("{0:x16}", SegAddr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " " << Group <<
" -> " << formatv("{0:x16}", SegAddr) << "\n"
; }; } } while (false)
553 << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " " << Group <<
" -> " << formatv("{0:x16}", SegAddr) << "\n"
; }; } } while (false)
554 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << " " << Group <<
" -> " << formatv("{0:x16}", SegAddr) << "\n"
; }; } } while (false)
;
555 Seg.WorkingMem = SegAddr.toPtr<char *>();
556 Seg.Addr = SegAddr + NextSlabDelta;
557
558 SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize);
559
560 // Zero out the zero-fill memory.
561 if (Seg.ZeroFillSize != 0)
562 memset(Seg.WorkingMem + Seg.ContentSize, 0, Seg.ZeroFillSize);
563 }
564
565 NextSlabDelta += SegsSizes->total();
566
567 if (auto Err = BL.apply()) {
568 OnAllocated(std::move(Err));
569 return;
570 }
571
572 OnAllocated(std::unique_ptr<InProcessMemoryManager::InFlightAlloc>(
573 new IPMMAlloc(*this, std::move(BL), std::move(StandardSegs),
574 std::move(FinalizeSegs))));
575 }
576
577 void deallocate(std::vector<FinalizedAlloc> FinalizedAllocs,
578 OnDeallocatedFunction OnDeallocated) override {
579 Error Err = Error::success();
580 for (auto &FA : FinalizedAllocs) {
581 std::unique_ptr<FinalizedAllocInfo> FAI(
582 FA.release().toPtr<FinalizedAllocInfo *>());
583
584 // FIXME: Run dealloc actions.
585
586 Err = joinErrors(std::move(Err), freeBlock(FAI->Mem));
587 }
588 OnDeallocated(std::move(Err));
589 }
590
591private:
592 JITLinkSlabAllocator(uint64_t SlabSize, Error &Err) {
593 ErrorAsOutParameter _(&Err);
594
595 if (!SlabPageSize) {
596 if (auto PageSizeOrErr = sys::Process::getPageSize())
597 PageSize = *PageSizeOrErr;
598 else {
599 Err = PageSizeOrErr.takeError();
600 return;
601 }
602
603 if (PageSize == 0) {
604 Err = make_error<StringError>("Page size is zero",
605 inconvertibleErrorCode());
606 return;
607 }
608 } else
609 PageSize = SlabPageSize;
610
611 if (!isPowerOf2_64(PageSize)) {
612 Err = make_error<StringError>("Page size is not a power of 2",
613 inconvertibleErrorCode());
614 return;
615 }
616
617 // Round slab request up to page size.
618 SlabSize = (SlabSize + PageSize - 1) & ~(PageSize - 1);
619
620 const sys::Memory::ProtectionFlags ReadWrite =
621 static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
622 sys::Memory::MF_WRITE);
623
624 std::error_code EC;
625 SlabRemaining =
626 sys::Memory::allocateMappedMemory(SlabSize, nullptr, ReadWrite, EC);
627
628 if (EC) {
629 Err = errorCodeToError(EC);
630 return;
631 }
632
633 // Calculate the target address delta to link as-if slab were at
634 // SlabAddress.
635 if (SlabAddress != ~0ULL)
636 NextSlabDelta = ExecutorAddr(SlabAddress) -
637 ExecutorAddr::fromPtr(SlabRemaining.base());
638 }
639
640 Error freeBlock(sys::MemoryBlock MB) {
641 // FIXME: Return memory to slab.
642 return Error::success();
643 }
644
645 std::mutex SlabMutex;
646 sys::MemoryBlock SlabRemaining;
647 uint64_t PageSize = 0;
648 int64_t NextSlabDelta = 0;
649};
650
651Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
652 SizeString = SizeString.trim();
653
654 uint64_t Units = 1024;
655
656 if (SizeString.endswith_insensitive("kb"))
657 SizeString = SizeString.drop_back(2).rtrim();
658 else if (SizeString.endswith_insensitive("mb")) {
659 Units = 1024 * 1024;
660 SizeString = SizeString.drop_back(2).rtrim();
661 } else if (SizeString.endswith_insensitive("gb")) {
662 Units = 1024 * 1024 * 1024;
663 SizeString = SizeString.drop_back(2).rtrim();
664 }
665
666 uint64_t SlabSize = 0;
667 if (SizeString.getAsInteger(10, SlabSize))
668 return make_error<StringError>("Invalid numeric format for slab size",
669 inconvertibleErrorCode());
670
671 return SlabSize * Units;
672}
673
674static std::unique_ptr<JITLinkMemoryManager> createMemoryManager() {
675 if (!SlabAllocateSizeString.empty()) {
676 auto SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
677 return ExitOnErr(JITLinkSlabAllocator::Create(SlabSize));
678 }
679 return ExitOnErr(InProcessMemoryManager::Create());
680}
681
682static Expected<MaterializationUnit::Interface>
683getTestObjectFileInterface(Session &S, MemoryBufferRef O) {
684
685 // Get the standard interface for this object, but ignore the symbols field.
686 // We'll handle that manually to include promotion.
687 auto I = getObjectFileInterface(S.ES, O);
688 if (!I)
689 return I.takeError();
690 I->SymbolFlags.clear();
691
692 // If creating an object file was going to fail it would have happened above,
693 // so we can 'cantFail' this.
694 auto Obj = cantFail(object::ObjectFile::createObjectFile(O));
695
696 // The init symbol must be included in the SymbolFlags map if present.
697 if (I->InitSymbol)
698 I->SymbolFlags[I->InitSymbol] =
699 JITSymbolFlags::MaterializationSideEffectsOnly;
700
701 for (auto &Sym : Obj->symbols()) {
702 Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
703 if (!SymFlagsOrErr)
704 // TODO: Test this error.
705 return SymFlagsOrErr.takeError();
706
707 // Skip symbols not defined in this object file.
708 if ((*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined))
709 continue;
710
711 auto Name = Sym.getName();
712 if (!Name)
713 return Name.takeError();
714
715 // Skip symbols that have type SF_File.
716 if (auto SymType = Sym.getType()) {
717 if (*SymType == object::SymbolRef::ST_File)
718 continue;
719 } else
720 return SymType.takeError();
721
722 auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
723 if (!SymFlags)
724 return SymFlags.takeError();
725
726 if (SymFlags->isWeak()) {
727 // If this is a weak symbol that's not defined in the harness then we
728 // need to either mark it as strong (if this is the first definition
729 // that we've seen) or discard it.
730 if (S.HarnessDefinitions.count(*Name) || S.CanonicalWeakDefs.count(*Name))
731 continue;
732 S.CanonicalWeakDefs[*Name] = O.getBufferIdentifier();
733 *SymFlags &= ~JITSymbolFlags::Weak;
734 if (!S.HarnessExternals.count(*Name))
735 *SymFlags &= ~JITSymbolFlags::Exported;
736 } else if (S.HarnessExternals.count(*Name)) {
737 *SymFlags |= JITSymbolFlags::Exported;
738 } else if (S.HarnessDefinitions.count(*Name) ||
739 !(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
740 continue;
741
742 auto InternedName = S.ES.intern(*Name);
743 I->SymbolFlags[InternedName] = std::move(*SymFlags);
744 }
745
746 return I;
747}
748
749static Error loadProcessSymbols(Session &S) {
750 auto FilterMainEntryPoint =
751 [EPName = S.ES.intern(EntryPointName)](SymbolStringPtr Name) {
752 return Name != EPName;
753 };
754 S.MainJD->addGenerator(
755 ExitOnErr(orc::EPCDynamicLibrarySearchGenerator::GetForTargetProcess(
756 S.ES, std::move(FilterMainEntryPoint))));
757
758 return Error::success();
759}
760
761static Error loadDylibs(Session &S) {
762 LLVM_DEBUG(dbgs() << "Loading dylibs...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Loading dylibs...\n"; } }
while (false)
;
763 for (const auto &Dylib : Dylibs) {
764 LLVM_DEBUG(dbgs() << " " << Dylib << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " " << Dylib <<
"\n"; } } while (false)
;
765 auto G = orc::EPCDynamicLibrarySearchGenerator::Load(S.ES, Dylib.c_str());
766 if (!G)
767 return G.takeError();
768 S.MainJD->addGenerator(std::move(*G));
769 }
770
771 return Error::success();
772}
773
774static Expected<std::unique_ptr<ExecutorProcessControl>> launchExecutor() {
775#ifndef LLVM_ON_UNIX1
776 // FIXME: Add support for Windows.
777 return make_error<StringError>("-" + OutOfProcessExecutor.ArgStr +
778 " not supported on non-unix platforms",
779 inconvertibleErrorCode());
780#elif !LLVM_ENABLE_THREADS1
781 // Out of process mode using SimpleRemoteEPC depends on threads.
782 return make_error<StringError>(
783 "-" + OutOfProcessExecutor.ArgStr +
784 " requires threads, but LLVM was built with "
785 "LLVM_ENABLE_THREADS=Off",
786 inconvertibleErrorCode());
787#else
788
789 constexpr int ReadEnd = 0;
790 constexpr int WriteEnd = 1;
791
792 // Pipe FDs.
793 int ToExecutor[2];
794 int FromExecutor[2];
795
796 pid_t ChildPID;
797
798 // Create pipes to/from the executor..
799 if (pipe(ToExecutor) != 0 || pipe(FromExecutor) != 0)
800 return make_error<StringError>("Unable to create pipe for executor",
801 inconvertibleErrorCode());
802
803 ChildPID = fork();
804
805 if (ChildPID == 0) {
806 // In the child...
807
808 // Close the parent ends of the pipes
809 close(ToExecutor[WriteEnd]);
810 close(FromExecutor[ReadEnd]);
811
812 // Execute the child process.
813 std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
814 {
815 ExecutorPath = std::make_unique<char[]>(OutOfProcessExecutor.size() + 1);
816 strcpy(ExecutorPath.get(), OutOfProcessExecutor.data());
817
818 std::string FDSpecifierStr("filedescs=");
819 FDSpecifierStr += utostr(ToExecutor[ReadEnd]);
820 FDSpecifierStr += ',';
821 FDSpecifierStr += utostr(FromExecutor[WriteEnd]);
822 FDSpecifier = std::make_unique<char[]>(FDSpecifierStr.size() + 1);
823 strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
824 }
825
826 char *const Args[] = {ExecutorPath.get(), FDSpecifier.get(), nullptr};
827 int RC = execvp(ExecutorPath.get(), Args);
828 if (RC != 0) {
829 errs() << "unable to launch out-of-process executor \""
830 << ExecutorPath.get() << "\"\n";
831 exit(1);
832 }
833 }
834 // else we're the parent...
835
836 // Close the child ends of the pipes
837 close(ToExecutor[ReadEnd]);
838 close(FromExecutor[WriteEnd]);
839
840 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
841 std::make_unique<DynamicThreadPoolTaskDispatcher>(),
842 SimpleRemoteEPC::Setup(), FromExecutor[ReadEnd], ToExecutor[WriteEnd]);
843#endif
844}
845
846#if LLVM_ON_UNIX1 && LLVM_ENABLE_THREADS1
847static Error createTCPSocketError(Twine Details) {
848 return make_error<StringError>(
849 formatv("Failed to connect TCP socket '{0}': {1}",
850 OutOfProcessExecutorConnect, Details),
851 inconvertibleErrorCode());
852}
853
854static Expected<int> connectTCPSocket(std::string Host, std::string PortStr) {
855 addrinfo *AI;
856 addrinfo Hints{};
857 Hints.ai_family = AF_INET2;
858 Hints.ai_socktype = SOCK_STREAMSOCK_STREAM;
859 Hints.ai_flags = AI_NUMERICSERV0x0400;
860
861 if (int EC = getaddrinfo(Host.c_str(), PortStr.c_str(), &Hints, &AI))
862 return createTCPSocketError("Address resolution failed (" +
863 StringRef(gai_strerror(EC)) + ")");
864
865 // Cycle through the returned addrinfo structures and connect to the first
866 // reachable endpoint.
867 int SockFD;
868 addrinfo *Server;
869 for (Server = AI; Server != nullptr; Server = Server->ai_next) {
870 // socket might fail, e.g. if the address family is not supported. Skip to
871 // the next addrinfo structure in such a case.
872 if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
873 continue;
874
875 // If connect returns null, we exit the loop with a working socket.
876 if (connect(SockFD, Server->ai_addr, Server->ai_addrlen) == 0)
877 break;
878
879 close(SockFD);
880 }
881 freeaddrinfo(AI);
882
883 // If we reached the end of the loop without connecting to a valid endpoint,
884 // dump the last error that was logged in socket() or connect().
885 if (Server == nullptr)
886 return createTCPSocketError(std::strerror(errno(*__errno_location ())));
887
888 return SockFD;
889}
890#endif
891
892static Expected<std::unique_ptr<ExecutorProcessControl>> connectToExecutor() {
893#ifndef LLVM_ON_UNIX1
894 // FIXME: Add TCP support for Windows.
895 return make_error<StringError>("-" + OutOfProcessExecutorConnect.ArgStr +
896 " not supported on non-unix platforms",
897 inconvertibleErrorCode());
898#elif !LLVM_ENABLE_THREADS1
899 // Out of process mode using SimpleRemoteEPC depends on threads.
900 return make_error<StringError>(
901 "-" + OutOfProcessExecutorConnect.ArgStr +
902 " requires threads, but LLVM was built with "
903 "LLVM_ENABLE_THREADS=Off",
904 inconvertibleErrorCode());
905#else
906
907 StringRef Host, PortStr;
908 std::tie(Host, PortStr) = StringRef(OutOfProcessExecutorConnect).split(':');
909 if (Host.empty())
910 return createTCPSocketError("Host name for -" +
911 OutOfProcessExecutorConnect.ArgStr +
912 " can not be empty");
913 if (PortStr.empty())
914 return createTCPSocketError("Port number in -" +
915 OutOfProcessExecutorConnect.ArgStr +
916 " can not be empty");
917 int Port = 0;
918 if (PortStr.getAsInteger(10, Port))
919 return createTCPSocketError("Port number '" + PortStr +
920 "' is not a valid integer");
921
922 Expected<int> SockFD = connectTCPSocket(Host.str(), PortStr.str());
923 if (!SockFD)
924 return SockFD.takeError();
925
926 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
927 std::make_unique<DynamicThreadPoolTaskDispatcher>(),
928 SimpleRemoteEPC::Setup(), *SockFD, *SockFD);
929#endif
930}
931
932class PhonyExternalsGenerator : public DefinitionGenerator {
933public:
934 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
935 JITDylibLookupFlags JDLookupFlags,
936 const SymbolLookupSet &LookupSet) override {
937 SymbolMap PhonySymbols;
938 for (auto &KV : LookupSet)
939 PhonySymbols[KV.first] = JITEvaluatedSymbol(0, JITSymbolFlags::Exported);
940 return JD.define(absoluteSymbols(std::move(PhonySymbols)));
941 }
942};
943
944Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
945
946 std::unique_ptr<ExecutorProcessControl> EPC;
947 if (OutOfProcessExecutor.getNumOccurrences()) {
948 /// If -oop-executor is passed then launch the executor.
949 if (auto REPC = launchExecutor())
950 EPC = std::move(*REPC);
951 else
952 return REPC.takeError();
953 } else if (OutOfProcessExecutorConnect.getNumOccurrences()) {
954 /// If -oop-executor-connect is passed then connect to the executor.
955 if (auto REPC = connectToExecutor())
956 EPC = std::move(*REPC);
957 else
958 return REPC.takeError();
959 } else {
960 /// Otherwise use SelfExecutorProcessControl to target the current process.
961 auto PageSize = sys::Process::getPageSize();
962 if (!PageSize)
963 return PageSize.takeError();
964 EPC = std::make_unique<SelfExecutorProcessControl>(
965 std::make_shared<SymbolStringPool>(),
966 std::make_unique<InPlaceTaskDispatcher>(), std::move(TT), *PageSize,
967 createMemoryManager());
968 }
969
970 Error Err = Error::success();
971 std::unique_ptr<Session> S(new Session(std::move(EPC), Err));
972 if (Err)
973 return std::move(Err);
974 return std::move(S);
975}
976
977Session::~Session() {
978 if (auto Err = ES.endSession())
979 ES.reportError(std::move(Err));
980}
981
982Session::Session(std::unique_ptr<ExecutorProcessControl> EPC, Error &Err)
983 : ES(std::move(EPC)),
984 ObjLayer(ES, ES.getExecutorProcessControl().getMemMgr()) {
985
986 /// Local ObjectLinkingLayer::Plugin class to forward modifyPassConfig to the
987 /// Session.
988 class JITLinkSessionPlugin : public ObjectLinkingLayer::Plugin {
989 public:
990 JITLinkSessionPlugin(Session &S) : S(S) {}
991 void modifyPassConfig(MaterializationResponsibility &MR, LinkGraph &G,
992 PassConfiguration &PassConfig) override {
993 S.modifyPassConfig(G.getTargetTriple(), PassConfig);
994 }
995
996 Error notifyFailed(MaterializationResponsibility &MR) override {
997 return Error::success();
998 }
999 Error notifyRemovingResources(ResourceKey K) override {
1000 return Error::success();
1001 }
1002 void notifyTransferringResources(ResourceKey DstKey,
1003 ResourceKey SrcKey) override {}
1004
1005 private:
1006 Session &S;
1007 };
1008
1009 ErrorAsOutParameter _(&Err);
1010
1011 if (auto MainJDOrErr = ES.createJITDylib("main"))
1012 MainJD = &*MainJDOrErr;
1013 else {
1014 Err = MainJDOrErr.takeError();
1015 return;
1016 }
1017
1018 if (!NoProcessSymbols)
1019 ExitOnErr(loadProcessSymbols(*this));
1020 ExitOnErr(loadDylibs(*this));
1021
1022 auto &TT = ES.getExecutorProcessControl().getTargetTriple();
1023
1024 if (DebuggerSupport && TT.isOSBinFormatMachO())
1025 ObjLayer.addPlugin(ExitOnErr(
1026 GDBJITDebugInfoRegistrationPlugin::Create(this->ES, *MainJD, TT)));
1027
1028 // Set up the platform.
1029 if (TT.isOSBinFormatMachO() && !OrcRuntime.empty()) {
1030 if (auto P =
1031 MachOPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str()))
1032 ES.setPlatform(std::move(*P));
1033 else {
1034 Err = P.takeError();
1035 return;
1036 }
1037 } else if (TT.isOSBinFormatELF() && !OrcRuntime.empty()) {
1038 if (auto P =
1039 ELFNixPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str()))
1040 ES.setPlatform(std::move(*P));
1041 else {
1042 Err = P.takeError();
1043 return;
1044 }
1045 } else if (!TT.isOSWindows() && !TT.isOSBinFormatMachO()) {
1046 if (!NoExec)
1047 ObjLayer.addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
1048 ES, ExitOnErr(EPCEHFrameRegistrar::Create(this->ES))));
1049 if (DebuggerSupport)
1050 ObjLayer.addPlugin(std::make_unique<DebugObjectManagerPlugin>(
1051 ES, ExitOnErr(createJITLoaderGDBRegistrar(this->ES))));
1052 }
1053
1054 ObjLayer.addPlugin(std::make_unique<JITLinkSessionPlugin>(*this));
1055
1056 // Process any harness files.
1057 for (auto &HarnessFile : TestHarnesses) {
1058 HarnessFiles.insert(HarnessFile);
1059
1060 auto ObjBuffer =
1061 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(HarnessFile)));
1062
1063 auto ObjInterface =
1064 ExitOnErr(getObjectFileInterface(ES, ObjBuffer->getMemBufferRef()));
1065
1066 for (auto &KV : ObjInterface.SymbolFlags)
1067 HarnessDefinitions.insert(*KV.first);
1068
1069 auto Obj = ExitOnErr(
1070 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
1071
1072 for (auto &Sym : Obj->symbols()) {
1073 uint32_t SymFlags = ExitOnErr(Sym.getFlags());
1074 auto Name = ExitOnErr(Sym.getName());
1075
1076 if (Name.empty())
1077 continue;
1078
1079 if (SymFlags & object::BasicSymbolRef::SF_Undefined)
1080 HarnessExternals.insert(Name);
1081 }
1082 }
1083
1084 // If a name is defined by some harness file then it's a definition, not an
1085 // external.
1086 for (auto &DefName : HarnessDefinitions)
1087 HarnessExternals.erase(DefName.getKey());
1088}
1089
1090void Session::dumpSessionInfo(raw_ostream &OS) {
1091 OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
1092}
1093
1094void Session::modifyPassConfig(const Triple &TT,
1095 PassConfiguration &PassConfig) {
1096 if (!CheckFiles.empty())
1097 PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) {
1098 auto &EPC = ES.getExecutorProcessControl();
1099 if (EPC.getTargetTriple().getObjectFormat() == Triple::ELF)
1100 return registerELFGraphInfo(*this, G);
1101
1102 if (EPC.getTargetTriple().getObjectFormat() == Triple::MachO)
1103 return registerMachOGraphInfo(*this, G);
1104
1105 return make_error<StringError>("Unsupported object format for GOT/stub "
1106 "registration",
1107 inconvertibleErrorCode());
1108 });
1109
1110 if (ShowLinkGraph)
1111 PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
1112 outs() << "Link graph \"" << G.getName() << "\" post-fixup:\n";
1113 G.dump(outs());
1114 return Error::success();
1115 });
1116
1117 PassConfig.PrePrunePasses.push_back(
1118 [this](LinkGraph &G) { return applyHarnessPromotions(*this, G); });
1119
1120 if (ShowSizes) {
1121 PassConfig.PrePrunePasses.push_back([this](LinkGraph &G) -> Error {
1122 SizeBeforePruning += computeTotalBlockSizes(G);
1123 return Error::success();
1124 });
1125 PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) -> Error {
1126 SizeAfterFixups += computeTotalBlockSizes(G);
1127 return Error::success();
1128 });
1129 }
1130
1131 if (ShowRelocatedSectionContents)
1132 PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
1133 outs() << "Relocated section contents for " << G.getName() << ":\n";
1134 dumpSectionContents(outs(), G);
1135 return Error::success();
1136 });
1137
1138 if (AddSelfRelocations)
1139 PassConfig.PostPrunePasses.push_back(addSelfRelocations);
1140}
1141
1142Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
1143 auto FileInfoItr = FileInfos.find(FileName);
1144 if (FileInfoItr == FileInfos.end())
1145 return make_error<StringError>("file \"" + FileName + "\" not recognized",
1146 inconvertibleErrorCode());
1147 return FileInfoItr->second;
1148}
1149
1150Expected<Session::MemoryRegionInfo &>
1151Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
1152 auto FI = findFileInfo(FileName);
1153 if (!FI)
1154 return FI.takeError();
1155 auto SecInfoItr = FI->SectionInfos.find(SectionName);
1156 if (SecInfoItr == FI->SectionInfos.end())
1157 return make_error<StringError>("no section \"" + SectionName +
1158 "\" registered for file \"" + FileName +
1159 "\"",
1160 inconvertibleErrorCode());
1161 return SecInfoItr->second;
1162}
1163
1164Expected<Session::MemoryRegionInfo &>
1165Session::findStubInfo(StringRef FileName, StringRef TargetName) {
1166 auto FI = findFileInfo(FileName);
1167 if (!FI)
1168 return FI.takeError();
1169 auto StubInfoItr = FI->StubInfos.find(TargetName);
1170 if (StubInfoItr == FI->StubInfos.end())
1171 return make_error<StringError>("no stub for \"" + TargetName +
1172 "\" registered for file \"" + FileName +
1173 "\"",
1174 inconvertibleErrorCode());
1175 return StubInfoItr->second;
1176}
1177
1178Expected<Session::MemoryRegionInfo &>
1179Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
1180 auto FI = findFileInfo(FileName);
1181 if (!FI)
1182 return FI.takeError();
1183 auto GOTInfoItr = FI->GOTEntryInfos.find(TargetName);
1184 if (GOTInfoItr == FI->GOTEntryInfos.end())
1185 return make_error<StringError>("no GOT entry for \"" + TargetName +
1186 "\" registered for file \"" + FileName +
1187 "\"",
1188 inconvertibleErrorCode());
1189 return GOTInfoItr->second;
1190}
1191
1192bool Session::isSymbolRegistered(StringRef SymbolName) {
1193 return SymbolInfos.count(SymbolName);
1194}
1195
1196Expected<Session::MemoryRegionInfo &>
1197Session::findSymbolInfo(StringRef SymbolName, Twine ErrorMsgStem) {
1198 auto SymInfoItr = SymbolInfos.find(SymbolName);
1199 if (SymInfoItr == SymbolInfos.end())
1200 return make_error<StringError>(ErrorMsgStem + ": symbol " + SymbolName +
1201 " not found",
1202 inconvertibleErrorCode());
1203 return SymInfoItr->second;
1204}
1205
1206} // end namespace llvm
1207
1208static Triple getFirstFileTriple() {
1209 static Triple FirstTT = []() {
1210 assert(!InputFiles.empty() && "InputFiles can not be empty")(static_cast <bool> (!InputFiles.empty() && "InputFiles can not be empty"
) ? void (0) : __assert_fail ("!InputFiles.empty() && \"InputFiles can not be empty\""
, "llvm/tools/llvm-jitlink/llvm-jitlink.cpp", 1210, __extension__
__PRETTY_FUNCTION__))
;
1211 for (auto InputFile : InputFiles) {
1212 auto ObjBuffer =
1213 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(InputFile)));
1214 switch (identify_magic(ObjBuffer->getBuffer())) {
1215 case file_magic::elf_relocatable:
1216 case file_magic::macho_object:
1217 case file_magic::coff_object: {
1218 auto Obj = ExitOnErr(
1219 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
1220 return Obj->makeTriple();
1221 }
1222 default:
1223 break;
1224 }
1225 }
1226 return Triple();
1227 }();
1228
1229 return FirstTT;
1230}
1231
1232static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
1233
1234 // -noexec and --args should not be used together.
1235 if (NoExec && !InputArgv.empty())
1236 errs() << "Warning: --args passed to -noexec run will be ignored.\n";
1237
1238 // Set the entry point name if not specified.
1239 if (EntryPointName.empty())
1240 EntryPointName = TT.getObjectFormat() == Triple::MachO ? "_main" : "main";
1241
1242 // Disable debugger support by default in noexec tests.
1243 if (DebuggerSupport.getNumOccurrences() == 0 && NoExec)
1244 DebuggerSupport = false;
1245
1246 // If -slab-allocate is passed, check that we're not trying to use it in
1247 // -oop-executor or -oop-executor-connect mode.
1248 //
1249 // FIXME: Remove once we enable remote slab allocation.
1250 if (SlabAllocateSizeString != "") {
1251 if (OutOfProcessExecutor.getNumOccurrences() ||
1252 OutOfProcessExecutorConnect.getNumOccurrences())
1253 return make_error<StringError>(
1254 "-slab-allocate cannot be used with -oop-executor or "
1255 "-oop-executor-connect",
1256 inconvertibleErrorCode());
1257 }
1258
1259 // If -slab-address is passed, require -slab-allocate and -noexec
1260 if (SlabAddress != ~0ULL) {
1261 if (SlabAllocateSizeString == "" || !NoExec)
1262 return make_error<StringError>(
1263 "-slab-address requires -slab-allocate and -noexec",
1264 inconvertibleErrorCode());
1265
1266 if (SlabPageSize == 0)
1267 errs() << "Warning: -slab-address used without -slab-page-size.\n";
1268 }
1269
1270 if (SlabPageSize != 0) {
1271 // -slab-page-size requires slab alloc.
1272 if (SlabAllocateSizeString == "")
1273 return make_error<StringError>("-slab-page-size requires -slab-allocate",
1274 inconvertibleErrorCode());
1275
1276 // Check -slab-page-size / -noexec interactions.
1277 if (!NoExec) {
1278 if (auto RealPageSize = sys::Process::getPageSize()) {
1279 if (SlabPageSize % *RealPageSize)
1280 return make_error<StringError>(
1281 "-slab-page-size must be a multiple of real page size for exec "
1282 "tests (did you mean to use -noexec ?)\n",
1283 inconvertibleErrorCode());
1284 } else {
1285 errs() << "Could not retrieve process page size:\n";
1286 logAllUnhandledErrors(RealPageSize.takeError(), errs(), "");
1287 errs() << "Executing with slab page size = "
1288 << formatv("{0:x}", SlabPageSize) << ".\n"
1289 << "Tool may crash if " << formatv("{0:x}", SlabPageSize)
1290 << " is not a multiple of the real process page size.\n"
1291 << "(did you mean to use -noexec ?)";
1292 }
1293 }
1294 }
1295
1296 // Only one of -oop-executor and -oop-executor-connect can be used.
1297 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1298 !!OutOfProcessExecutorConnect.getNumOccurrences())
1299 return make_error<StringError>(
1300 "Only one of -" + OutOfProcessExecutor.ArgStr + " and -" +
1301 OutOfProcessExecutorConnect.ArgStr + " can be specified",
1302 inconvertibleErrorCode());
1303
1304 // If -oop-executor was used but no value was specified then use a sensible
1305 // default.
1306 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1307 OutOfProcessExecutor.empty()) {
1308 SmallString<256> OOPExecutorPath(sys::fs::getMainExecutable(
1309 ArgV0, reinterpret_cast<void *>(&sanitizeArguments)));
1310 sys::path::remove_filename(OOPExecutorPath);
1311 sys::path::append(OOPExecutorPath, "llvm-jitlink-executor");
1312 OutOfProcessExecutor = OOPExecutorPath.str().str();
1313 }
1314
1315 return Error::success();
1316}
1317
1318static void addPhonyExternalsGenerator(Session &S) {
1319 S.MainJD->addGenerator(std::make_unique<PhonyExternalsGenerator>());
1320}
1321
1322static Error createJITDylibs(Session &S,
1323 std::map<unsigned, JITDylib *> &IdxToJD) {
1324 // First, set up JITDylibs.
1325 LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Creating JITDylibs...\n"
; } } while (false)
;
1326 {
1327 // Create a "main" JITLinkDylib.
1328 IdxToJD[0] = S.MainJD;
1329 S.JDSearchOrder.push_back({S.MainJD, JITDylibLookupFlags::MatchAllSymbols});
1330 LLVM_DEBUG(dbgs() << " 0: " << S.MainJD->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " 0: " << S.MainJD
->getName() << "\n"; } } while (false)
;
1331
1332 // Add any extra JITDylibs from the command line.
1333 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1334 JDItr != JDEnd; ++JDItr) {
1335 auto JD = S.ES.createJITDylib(*JDItr);
1336 if (!JD)
1337 return JD.takeError();
1338 unsigned JDIdx = JITDylibs.getPosition(JDItr - JITDylibs.begin());
1339 IdxToJD[JDIdx] = &*JD;
1340 S.JDSearchOrder.push_back({&*JD, JITDylibLookupFlags::MatchAllSymbols});
1341 LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " " << JDIdx <<
": " << JD->getName() << "\n"; } } while (false
)
;
1342 }
1343 }
1344
1345 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
1346 dbgs() << "Dylib search order is [ ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
1347 for (auto &KV : S.JDSearchOrder)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
1348 dbgs() << KV.first->getName() << " ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
1349 dbgs() << "]\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
1350 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Dylib search order is [ "
; for (auto &KV : S.JDSearchOrder) dbgs() << KV.first
->getName() << " "; dbgs() << "]\n"; }; } } while
(false)
;
1351
1352 return Error::success();
1353}
1354
1355static Error addAbsoluteSymbols(Session &S,
1356 const std::map<unsigned, JITDylib *> &IdxToJD) {
1357 // Define absolute symbols.
1358 LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Defining absolute symbols...\n"
; } } while (false)
;
1359 for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
1360 AbsDefItr != AbsDefEnd; ++AbsDefItr) {
1361 unsigned AbsDefArgIdx =
1362 AbsoluteDefs.getPosition(AbsDefItr - AbsoluteDefs.begin());
1363 auto &JD = *std::prev(IdxToJD.lower_bound(AbsDefArgIdx))->second;
1364
1365 StringRef AbsDefStmt = *AbsDefItr;
1366 size_t EqIdx = AbsDefStmt.find_first_of('=');
1367 if (EqIdx == StringRef::npos)
1368 return make_error<StringError>("Invalid absolute define \"" + AbsDefStmt +
1369 "\". Syntax: <name>=<addr>",
1370 inconvertibleErrorCode());
1371 StringRef Name = AbsDefStmt.substr(0, EqIdx).trim();
1372 StringRef AddrStr = AbsDefStmt.substr(EqIdx + 1).trim();
1373
1374 uint64_t Addr;
1375 if (AddrStr.getAsInteger(0, Addr))
1376 return make_error<StringError>("Invalid address expression \"" + AddrStr +
1377 "\" in absolute define \"" + AbsDefStmt +
1378 "\"",
1379 inconvertibleErrorCode());
1380 JITEvaluatedSymbol AbsDef(Addr, JITSymbolFlags::Exported);
1381 if (auto Err = JD.define(absoluteSymbols({{S.ES.intern(Name), AbsDef}})))
1382 return Err;
1383
1384 // Register the absolute symbol with the session symbol infos.
1385 S.SymbolInfos[Name] = {ArrayRef<char>(), Addr};
1386 }
1387
1388 return Error::success();
1389}
1390
1391static Error addTestHarnesses(Session &S) {
1392 LLVM_DEBUG(dbgs() << "Adding test harness objects...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Adding test harness objects...\n"
; } } while (false)
;
1393 for (auto HarnessFile : TestHarnesses) {
1394 LLVM_DEBUG(dbgs() << " " << HarnessFile << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " " << HarnessFile
<< "\n"; } } while (false)
;
1395 auto ObjBuffer = errorOrToExpected(MemoryBuffer::getFile(HarnessFile));
1396 if (!ObjBuffer)
1397 return ObjBuffer.takeError();
1398 if (auto Err = S.ObjLayer.add(*S.MainJD, std::move(*ObjBuffer)))
1399 return Err;
1400 }
1401 return Error::success();
1402}
1403
1404static Error addObjects(Session &S,
1405 const std::map<unsigned, JITDylib *> &IdxToJD) {
1406
1407 // Load each object into the corresponding JITDylib..
1408 LLVM_DEBUG(dbgs() << "Adding objects...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Adding objects...\n"; } }
while (false)
;
1409 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
1410 InputFileItr != InputFileEnd; ++InputFileItr) {
1411 unsigned InputFileArgIdx =
1412 InputFiles.getPosition(InputFileItr - InputFiles.begin());
1413 const std::string &InputFile = *InputFileItr;
1414 if (StringRef(InputFile).endswith(".a"))
1415 continue;
1416 auto &JD = *std::prev(IdxToJD.lower_bound(InputFileArgIdx))->second;
1417 LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFiledo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " " << InputFileArgIdx
<< ": \"" << InputFile << "\" to " <<
JD.getName() << "\n";; } } while (false)
1418 << "\" to " << JD.getName() << "\n";)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << " " << InputFileArgIdx
<< ": \"" << InputFile << "\" to " <<
JD.getName() << "\n";; } } while (false)
;
1419 auto ObjBuffer = errorOrToExpected(MemoryBuffer::getFile(InputFile));
1420 if (!ObjBuffer)
1421 return ObjBuffer.takeError();
1422
1423 if (S.HarnessFiles.empty()) {
1424 if (auto Err = S.ObjLayer.add(JD, std::move(*ObjBuffer)))
1425 return Err;
1426 } else {
1427 // We're in -harness mode. Use a custom interface for this
1428 // test object.
1429 auto ObjInterface =
1430 getTestObjectFileInterface(S, (*ObjBuffer)->getMemBufferRef());
1431 if (!ObjInterface)
1432 return ObjInterface.takeError();
1433 if (auto Err = S.ObjLayer.add(JD, std::move(*ObjBuffer),
1434 std::move(*ObjInterface)))
1435 return Err;
1436 }
1437 }
1438
1439 return Error::success();
1440}
1441
1442static Expected<MaterializationUnit::Interface>
1443getObjectFileInterfaceHidden(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
1444 auto I = getObjectFileInterface(ES, ObjBuffer);
1445 if (I) {
1446 for (auto &KV : I->SymbolFlags)
1447 KV.second &= ~JITSymbolFlags::Exported;
1448 }
1449 return I;
1450}
1451
1452static Error addLibraries(Session &S,
1453 const std::map<unsigned, JITDylib *> &IdxToJD) {
1454
1455 // 1. Collect search paths for each JITDylib.
1456 DenseMap<const JITDylib *, SmallVector<StringRef, 2>> JDSearchPaths;
1457
1458 for (auto LSPItr = LibrarySearchPaths.begin(),
1459 LSPEnd = LibrarySearchPaths.end();
1460 LSPItr != LSPEnd; ++LSPItr) {
1461 unsigned LibrarySearchPathIdx =
1462 LibrarySearchPaths.getPosition(LSPItr - LibrarySearchPaths.begin());
1463 auto &JD = *std::prev(IdxToJD.lower_bound(LibrarySearchPathIdx))->second;
1464
1465 StringRef LibrarySearchPath = *LSPItr;
1466 if (sys::fs::get_file_type(LibrarySearchPath) !=
1467 sys::fs::file_type::directory_file)
1468 return make_error<StringError>("While linking " + JD.getName() + ", -L" +
1469 LibrarySearchPath +
1470 " does not point to a directory",
1471 inconvertibleErrorCode());
1472
1473 JDSearchPaths[&JD].push_back(*LSPItr);
1474 }
1475
1476 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1477 if (!JDSearchPaths.empty())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1478 dbgs() << "Search paths:\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1479 for (auto &KV : JDSearchPaths) {do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1480 dbgs() << " " << KV.first->getName() << ": [";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1481 for (auto &LibSearchPath : KV.second)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1482 dbgs() << " \"" << LibSearchPath << "\"";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1483 dbgs() << " ]\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1484 }do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
1485 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { if (!JDSearchPaths.empty()) dbgs() <<
"Search paths:\n"; for (auto &KV : JDSearchPaths) { dbgs
() << " " << KV.first->getName() << ": ["
; for (auto &LibSearchPath : KV.second) dbgs() << " \""
<< LibSearchPath << "\""; dbgs() << " ]\n"
; } }; } } while (false)
;
1486
1487 // 2. Collect library loads
1488 struct LibraryLoad {
1489 StringRef LibName;
1490 bool IsPath = false;
1491 unsigned Position;
1492 StringRef *CandidateExtensions;
1493 enum { Standard, Hidden } Modifier;
1494 };
1495 std::vector<LibraryLoad> LibraryLoads;
1496 // Add archive files from the inputs to LibraryLoads.
1497 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
1498 InputFileItr != InputFileEnd; ++InputFileItr) {
1499 StringRef InputFile = *InputFileItr;
1500 if (!InputFile.endswith(".a"))
1501 continue;
1502 LibraryLoad LL;
1503 LL.LibName = InputFile;
1504 LL.IsPath = true;
1505 LL.Position = InputFiles.getPosition(InputFileItr - InputFiles.begin());
1506 LL.CandidateExtensions = nullptr;
1507 LL.Modifier = LibraryLoad::Standard;
1508 LibraryLoads.push_back(std::move(LL));
1509 }
1510
1511 // Add -load_hidden arguments to LibraryLoads.
1512 for (auto LibItr = LoadHidden.begin(), LibEnd = LoadHidden.end();
1513 LibItr != LibEnd; ++LibItr) {
1514 LibraryLoad LL;
1515 LL.LibName = *LibItr;
1516 LL.IsPath = true;
1517 LL.Position = LoadHidden.getPosition(LibItr - LoadHidden.begin());
1518 LL.CandidateExtensions = nullptr;
1519 LL.Modifier = LibraryLoad::Hidden;
1520 LibraryLoads.push_back(std::move(LL));
1521 }
1522 StringRef StandardExtensions[] = {".so", ".dylib", ".a"};
1523 StringRef ArchiveExtensionsOnly[] = {".a"};
1524
1525 // Add -lx arguments to LibraryLoads.
1526 for (auto LibItr = Libraries.begin(), LibEnd = Libraries.end();
1527 LibItr != LibEnd; ++LibItr) {
1528 LibraryLoad LL;
1529 LL.LibName = *LibItr;
1530 LL.Position = Libraries.getPosition(LibItr - Libraries.begin());
1531 LL.CandidateExtensions = StandardExtensions;
1532 LL.Modifier = LibraryLoad::Standard;
1533 LibraryLoads.push_back(std::move(LL));
1534 }
1535
1536 // Add -hidden-lx arguments to LibraryLoads.
1537 for (auto LibHiddenItr = LibrariesHidden.begin(),
1538 LibHiddenEnd = LibrariesHidden.end();
1539 LibHiddenItr != LibHiddenEnd; ++LibHiddenItr) {
1540 LibraryLoad LL;
1541 LL.LibName = *LibHiddenItr;
1542 LL.Position =
1543 LibrariesHidden.getPosition(LibHiddenItr - LibrariesHidden.begin());
1544 LL.CandidateExtensions = ArchiveExtensionsOnly;
1545 LL.Modifier = LibraryLoad::Hidden;
1546 LibraryLoads.push_back(std::move(LL));
1547 }
1548
1549 // If there are any load-<modified> options then turn on flag overrides
1550 // to avoid flag mismatch errors.
1551 if (!LibrariesHidden.empty() || !LoadHidden.empty())
1552 S.ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
1553
1554 // Sort library loads by position in the argument list.
1555 llvm::sort(LibraryLoads, [](const LibraryLoad &LHS, const LibraryLoad &RHS) {
1556 return LHS.Position < RHS.Position;
1557 });
1558
1559 // 3. Process library loads.
1560 auto AddArchive = [&](const char *Path, const LibraryLoad &LL)
1561 -> Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> {
1562 unique_function<Expected<MaterializationUnit::Interface>(
1563 ExecutionSession & ES, MemoryBufferRef ObjBuffer)>
1564 GetObjFileInterface;
1565 switch (LL.Modifier) {
1566 case LibraryLoad::Standard:
1567 GetObjFileInterface = getObjectFileInterface;
1568 break;
1569 case LibraryLoad::Hidden:
1570 GetObjFileInterface = getObjectFileInterfaceHidden;
1571 break;
1572 }
1573 return StaticLibraryDefinitionGenerator::Load(
1574 S.ObjLayer, Path, S.ES.getExecutorProcessControl().getTargetTriple(),
1575 std::move(GetObjFileInterface));
1576 };
1577
1578 for (auto &LL : LibraryLoads) {
1579 bool LibFound = false;
1580 auto &JD = *std::prev(IdxToJD.lower_bound(LL.Position))->second;
1581
1582 // If this is the name of a JITDylib then link against that.
1583 if (auto *LJD = S.ES.getJITDylibByName(LL.LibName)) {
1584 JD.addToLinkOrder(*LJD);
1585 continue;
1586 }
1587
1588 if (LL.IsPath) {
1589 auto G = AddArchive(LL.LibName.str().c_str(), LL);
1590 if (!G)
1591 return createFileError(LL.LibName, G.takeError());
1592 JD.addGenerator(std::move(*G));
1593 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LL.LibName << " to " << JD.getName() <<
"\n"; }; } } while (false)
1594 dbgs() << "Adding generator for static library " << LL.LibName << " to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LL.LibName << " to " << JD.getName() <<
"\n"; }; } } while (false)
1595 << JD.getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LL.LibName << " to " << JD.getName() <<
"\n"; }; } } while (false)
1596 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LL.LibName << " to " << JD.getName() <<
"\n"; }; } } while (false)
;
1597 continue;
1598 }
1599
1600 // Otherwise look through the search paths.
1601 auto JDSearchPathsItr = JDSearchPaths.find(&JD);
1602 if (JDSearchPathsItr != JDSearchPaths.end()) {
1603 for (StringRef SearchPath : JDSearchPathsItr->second) {
1604 for (const char *LibExt : {".dylib", ".so", ".a"}) {
1605 SmallVector<char, 256> LibPath;
1606 LibPath.reserve(SearchPath.size() + strlen("lib") +
1607 LL.LibName.size() + strlen(LibExt) +
1608 2); // +2 for pathsep, null term.
1609 llvm::copy(SearchPath, std::back_inserter(LibPath));
1610 sys::path::append(LibPath, "lib" + LL.LibName + LibExt);
1611 LibPath.push_back('\0');
1612
1613 // Skip missing or non-regular paths.
1614 if (sys::fs::get_file_type(LibPath.data()) !=
1615 sys::fs::file_type::regular_file) {
1616 continue;
1617 }
1618
1619 file_magic Magic;
1620 if (auto EC = identify_magic(LibPath, Magic)) {
1621 // If there was an error loading the file then skip it.
1622 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but could not identify file type ("
<< EC.message() << "). Skipping.\n"; }; } } while
(false)
1623 dbgs() << "Library search found \"" << LibPathdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but could not identify file type ("
<< EC.message() << "). Skipping.\n"; }; } } while
(false)
1624 << "\", but could not identify file type (" << EC.message()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but could not identify file type ("
<< EC.message() << "). Skipping.\n"; }; } } while
(false)
1625 << "). Skipping.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but could not identify file type ("
<< EC.message() << "). Skipping.\n"; }; } } while
(false)
1626 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but could not identify file type ("
<< EC.message() << "). Skipping.\n"; }; } } while
(false)
;
1627 continue;
1628 }
1629
1630 // We identified the magic. Assume that we can load it -- we'll reset
1631 // in the default case.
1632 LibFound = true;
1633 switch (Magic) {
1634 case file_magic::elf_shared_object:
1635 case file_magic::macho_dynamically_linked_shared_lib: {
1636 // TODO: On first reference to LibPath this should create a JITDylib
1637 // with a generator and add it to JD's links-against list. Subsquent
1638 // references should use the JITDylib created on the first
1639 // reference.
1640 auto G =
1641 EPCDynamicLibrarySearchGenerator::Load(S.ES, LibPath.data());
1642 if (!G)
1643 return G.takeError();
1644 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for dynamic library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1645 dbgs() << "Adding generator for dynamic library "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for dynamic library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1646 << LibPath.data() << " to " << JD.getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for dynamic library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1647 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for dynamic library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
;
1648 JD.addGenerator(std::move(*G));
1649 break;
1650 }
1651 case file_magic::archive:
1652 case file_magic::macho_universal_binary: {
1653 auto G = AddArchive(LibPath.data(), LL);
1654 if (!G)
1655 return G.takeError();
1656 JD.addGenerator(std::move(*G));
1657 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1658 dbgs() << "Adding generator for static library " << LibPath.data()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1659 << " to " << JD.getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
1660 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Adding generator for static library "
<< LibPath.data() << " to " << JD.getName(
) << "\n"; }; } } while (false)
;
1661 break;
1662 }
1663 default:
1664 // This file isn't a recognized library kind.
1665 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but file type is not supported. Skipping.\n"
; }; } } while (false)
1666 dbgs() << "Library search found \"" << LibPathdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but file type is not supported. Skipping.\n"
; }; } } while (false)
1667 << "\", but file type is not supported. Skipping.\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but file type is not supported. Skipping.\n"
; }; } } while (false)
1668 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Library search found \""
<< LibPath << "\", but file type is not supported. Skipping.\n"
; }; } } while (false)
;
1669 LibFound = false;
1670 break;
1671 }
1672 if (LibFound)
1673 break;
1674 }
1675 if (LibFound)
1676 break;
1677 }
1678 }
1679
1680 if (!LibFound)
1681 return make_error<StringError>("While linking " + JD.getName() +
1682 ", could not find library for -l" +
1683 LL.LibName,
1684 inconvertibleErrorCode());
1685 }
1686
1687 return Error::success();
1688}
1689
1690static Error addProcessSymbols(Session &S,
1691 const std::map<unsigned, JITDylib *> &IdxToJD) {
1692
1693 if (NoProcessSymbols)
1694 return Error::success();
1695
1696 for (auto &KV : IdxToJD) {
1697 auto &JD = *KV.second;
1698 JD.addGenerator(ExitOnErr(
1699 orc::EPCDynamicLibrarySearchGenerator::GetForTargetProcess(S.ES)));
1700 }
1701
1702 return Error::success();
1703}
1704
1705static Error addSessionInputs(Session &S) {
1706 std::map<unsigned, JITDylib *> IdxToJD;
1707
1708 if (auto Err = createJITDylibs(S, IdxToJD))
1709 return Err;
1710
1711 if (auto Err = addAbsoluteSymbols(S, IdxToJD))
1712 return Err;
1713
1714 if (!TestHarnesses.empty())
1715 if (auto Err = addTestHarnesses(S))
1716 return Err;
1717
1718 if (auto Err = addObjects(S, IdxToJD))
1719 return Err;
1720
1721 if (auto Err = addLibraries(S, IdxToJD))
1722 return Err;
1723
1724 if (auto Err = addProcessSymbols(S, IdxToJD))
1725 return Err;
1726
1727 return Error::success();
1728}
1729
1730namespace {
1731struct TargetInfo {
1732 const Target *TheTarget;
1733 std::unique_ptr<MCSubtargetInfo> STI;
1734 std::unique_ptr<MCRegisterInfo> MRI;
1735 std::unique_ptr<MCAsmInfo> MAI;
1736 std::unique_ptr<MCContext> Ctx;
1737 std::unique_ptr<MCDisassembler> Disassembler;
1738 std::unique_ptr<MCInstrInfo> MII;
1739 std::unique_ptr<MCInstrAnalysis> MIA;
1740 std::unique_ptr<MCInstPrinter> InstPrinter;
1741};
1742} // anonymous namespace
1743
1744static TargetInfo getTargetInfo(const Triple &TT) {
1745 auto TripleName = TT.str();
1746 std::string ErrorStr;
1747 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
1748 if (!TheTarget)
1749 ExitOnErr(make_error<StringError>("Error accessing target '" + TripleName +
1750 "': " + ErrorStr,
1751 inconvertibleErrorCode()));
1752
1753 std::unique_ptr<MCSubtargetInfo> STI(
1754 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
1755 if (!STI)
1756 ExitOnErr(
1757 make_error<StringError>("Unable to create subtarget for " + TripleName,
1758 inconvertibleErrorCode()));
1759
1760 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
1761 if (!MRI)
1762 ExitOnErr(make_error<StringError>("Unable to create target register info "
1763 "for " +
1764 TripleName,
1765 inconvertibleErrorCode()));
1766
1767 MCTargetOptions MCOptions;
1768 std::unique_ptr<MCAsmInfo> MAI(
1769 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1770 if (!MAI)
1771 ExitOnErr(make_error<StringError>("Unable to create target asm info " +
1772 TripleName,
1773 inconvertibleErrorCode()));
1774
1775 auto Ctx = std::make_unique<MCContext>(Triple(TripleName), MAI.get(),
1776 MRI.get(), STI.get());
1777
1778 std::unique_ptr<MCDisassembler> Disassembler(
1779 TheTarget->createMCDisassembler(*STI, *Ctx));
1780 if (!Disassembler)
1781 ExitOnErr(make_error<StringError>("Unable to create disassembler for " +
1782 TripleName,
1783 inconvertibleErrorCode()));
1784
1785 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
1786 if (!MII)
1787 ExitOnErr(make_error<StringError>("Unable to create instruction info for" +
1788 TripleName,
1789 inconvertibleErrorCode()));
1790
1791 std::unique_ptr<MCInstrAnalysis> MIA(
1792 TheTarget->createMCInstrAnalysis(MII.get()));
1793 if (!MIA)
1794 ExitOnErr(make_error<StringError>(
1795 "Unable to create instruction analysis for" + TripleName,
1796 inconvertibleErrorCode()));
1797
1798 std::unique_ptr<MCInstPrinter> InstPrinter(
1799 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
1800 if (!InstPrinter)
1801 ExitOnErr(make_error<StringError>(
1802 "Unable to create instruction printer for" + TripleName,
1803 inconvertibleErrorCode()));
1804 return {TheTarget, std::move(STI), std::move(MRI),
1805 std::move(MAI), std::move(Ctx), std::move(Disassembler),
1806 std::move(MII), std::move(MIA), std::move(InstPrinter)};
1807}
1808
1809static Error runChecks(Session &S) {
1810 const auto &TT = S.ES.getExecutorProcessControl().getTargetTriple();
1811
1812 if (CheckFiles.empty())
1813 return Error::success();
1814
1815 LLVM_DEBUG(dbgs() << "Running checks...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Running checks...\n"; } }
while (false)
;
1816
1817 auto TI = getTargetInfo(TT);
1818
1819 auto IsSymbolValid = [&S](StringRef Symbol) {
1820 return S.isSymbolRegistered(Symbol);
1821 };
1822
1823 auto GetSymbolInfo = [&S](StringRef Symbol) {
1824 return S.findSymbolInfo(Symbol, "Can not get symbol info");
1825 };
1826
1827 auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
1828 return S.findSectionInfo(FileName, SectionName);
1829 };
1830
1831 auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName) {
1832 return S.findStubInfo(FileName, SectionName);
1833 };
1834
1835 auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
1836 return S.findGOTEntryInfo(FileName, SectionName);
1837 };
1838
1839 RuntimeDyldChecker Checker(
1840 IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
1841 TT.isLittleEndian() ? support::little : support::big,
1842 TI.Disassembler.get(), TI.InstPrinter.get(), dbgs());
1843
1844 std::string CheckLineStart = "# " + CheckName + ":";
1845 for (auto &CheckFile : CheckFiles) {
1846 auto CheckerFileBuf =
1847 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(CheckFile)));
1848 if (!Checker.checkAllRulesInBuffer(CheckLineStart, &*CheckerFileBuf))
1849 ExitOnErr(make_error<StringError>(
1850 "Some checks in " + CheckFile + " failed", inconvertibleErrorCode()));
1851 }
1852
1853 return Error::success();
1854}
1855
1856static Error addSelfRelocations(LinkGraph &G) {
1857 auto TI = getTargetInfo(G.getTargetTriple());
1858 for (auto *Sym : G.defined_symbols())
1859 if (Sym->isCallable())
1860 if (auto Err = addFunctionPointerRelocationsToCurrentSymbol(
1861 *Sym, G, *TI.Disassembler, *TI.MIA))
1862 return Err;
1863 return Error::success();
1864}
1865
1866static void dumpSessionStats(Session &S) {
1867 if (!ShowSizes)
1868 return;
1869 if (!OrcRuntime.empty())
1870 outs() << "Note: Session stats include runtime and entry point lookup, but "
1871 "not JITDylib initialization/deinitialization.\n";
1872 if (ShowSizes)
1873 outs() << " Total size of all blocks before pruning: "
1874 << S.SizeBeforePruning
1875 << "\n Total size of all blocks after fixups: " << S.SizeAfterFixups
1876 << "\n";
1877}
1878
1879static Expected<JITEvaluatedSymbol> getMainEntryPoint(Session &S) {
1880 return S.ES.lookup(S.JDSearchOrder, S.ES.intern(EntryPointName));
1881}
1882
1883static Expected<JITEvaluatedSymbol> getOrcRuntimeEntryPoint(Session &S) {
1884 std::string RuntimeEntryPoint = "__orc_rt_run_program_wrapper";
1885 const auto &TT = S.ES.getExecutorProcessControl().getTargetTriple();
1886 if (TT.getObjectFormat() == Triple::MachO)
1887 RuntimeEntryPoint = '_' + RuntimeEntryPoint;
1888 return S.ES.lookup(S.JDSearchOrder, S.ES.intern(RuntimeEntryPoint));
1889}
1890
1891static Expected<int> runWithRuntime(Session &S, ExecutorAddr EntryPointAddr) {
1892 StringRef DemangledEntryPoint = EntryPointName;
1893 const auto &TT = S.ES.getExecutorProcessControl().getTargetTriple();
1894 if (TT.getObjectFormat() == Triple::MachO &&
25
Assuming the condition is false
1895 DemangledEntryPoint.front() == '_')
1896 DemangledEntryPoint = DemangledEntryPoint.drop_front();
1897 using SPSRunProgramSig =
1898 int64_t(SPSString, SPSString, SPSSequence<SPSString>);
1899 int64_t Result;
1900 if (auto Err = S.ES.callSPSWrapper<SPSRunProgramSig>(
26
Assuming the condition is false
27
Taking false branch
1901 EntryPointAddr, Result, S.MainJD->getName(), DemangledEntryPoint,
1902 static_cast<std::vector<std::string> &>(InputArgv)))
1903 return std::move(Err);
1904 return Result;
28
Calling constructor for 'Expected<int>'
1905}
1906
1907static Expected<int> runWithoutRuntime(Session &S,
1908 ExecutorAddr EntryPointAddr) {
1909 return S.ES.getExecutorProcessControl().runAsMain(EntryPointAddr, InputArgv);
1910}
1911
1912namespace {
1913struct JITLinkTimers {
1914 TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
1915 Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
1916 Timer LinkTimer{"link", "time to link object files", JITLinkTG};
1917 Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
1918};
1919} // namespace
1920
1921int main(int argc, char *argv[]) {
1922 InitLLVM X(argc, argv);
1923
1924 InitializeAllTargetInfos();
1925 InitializeAllTargetMCs();
1926 InitializeAllDisassemblers();
1927
1928 cl::HideUnrelatedOptions({&JITLinkCategory, &getColorCategory()});
1929 cl::ParseCommandLineOptions(argc, argv, "llvm jitlink tool");
1930 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
1931
1932 /// If timers are enabled, create a JITLinkTimers instance.
1933 std::unique_ptr<JITLinkTimers> Timers =
1934 ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
1
Assuming the condition is false
2
'?' condition is false
1935
1936 ExitOnErr(sanitizeArguments(getFirstFileTriple(), argv[0]));
1937
1938 auto S = ExitOnErr(Session::Create(getFirstFileTriple()));
1939
1940 {
1941 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
3
'?' condition is false
1942 ExitOnErr(addSessionInputs(*S));
1943 }
1944
1945 if (PhonyExternals)
4
Assuming the condition is false
5
Taking false branch
1946 addPhonyExternalsGenerator(*S);
1947
1948 if (ShowInitialExecutionSessionState)
6
Assuming the condition is false
7
Taking false branch
1949 S->ES.dump(outs());
1950
1951 JITEvaluatedSymbol EntryPoint = nullptr;
1952 {
1953 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
8
'?' condition is false
1954 // Find the entry-point function unconditionally, since we want to force
1955 // it to be materialized to collect stats.
1956 EntryPoint = ExitOnErr(getMainEntryPoint(*S));
1957 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Using entry point \"" <<
EntryPointName << "\": " << formatv("{0:x16}", EntryPoint
.getAddress()) << "\n"; }; } } while (false)
9
Assuming 'DebugFlag' is false
10
Loop condition is false. Exiting loop
1958 dbgs() << "Using entry point \"" << EntryPointNamedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Using entry point \"" <<
EntryPointName << "\": " << formatv("{0:x16}", EntryPoint
.getAddress()) << "\n"; }; } } while (false)
1959 << "\": " << formatv("{0:x16}", EntryPoint.getAddress()) << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Using entry point \"" <<
EntryPointName << "\": " << formatv("{0:x16}", EntryPoint
.getAddress()) << "\n"; }; } } while (false)
1960 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "Using entry point \"" <<
EntryPointName << "\": " << formatv("{0:x16}", EntryPoint
.getAddress()) << "\n"; }; } } while (false)
;
1961
1962 // If we're running with the ORC runtime then replace the entry-point
1963 // with the __orc_rt_run_program symbol.
1964 if (!OrcRuntime.empty()) {
11
Assuming the condition is false
12
Taking false branch
1965 EntryPoint = ExitOnErr(getOrcRuntimeEntryPoint(*S));
1966 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "(called via __orc_rt_run_program_wrapper at "
<< formatv("{0:x16}", EntryPoint.getAddress()) <<
")\n"; }; } } while (false)
1967 dbgs() << "(called via __orc_rt_run_program_wrapper at "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "(called via __orc_rt_run_program_wrapper at "
<< formatv("{0:x16}", EntryPoint.getAddress()) <<
")\n"; }; } } while (false)
1968 << formatv("{0:x16}", EntryPoint.getAddress()) << ")\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "(called via __orc_rt_run_program_wrapper at "
<< formatv("{0:x16}", EntryPoint.getAddress()) <<
")\n"; }; } } while (false)
1969 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { { dbgs() << "(called via __orc_rt_run_program_wrapper at "
<< formatv("{0:x16}", EntryPoint.getAddress()) <<
")\n"; }; } } while (false)
;
1970 }
1971 }
1972
1973 if (ShowEntryExecutionSessionState)
13
Assuming the condition is false
14
Taking false branch
1974 S->ES.dump(outs());
1975
1976 if (ShowAddrs)
15
Assuming the condition is false
16
Taking false branch
1977 S->dumpSessionInfo(outs());
1978
1979 ExitOnErr(runChecks(*S));
1980
1981 dumpSessionStats(*S);
1982
1983 if (NoExec)
17
Assuming the condition is false
18
Taking false branch
1984 return 0;
1985
1986 int Result = 0;
1987 {
1988 LLVM_DEBUG(dbgs() << "Running \"" << EntryPointName << "\"...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("llvm_jitlink")) { dbgs() << "Running \"" << EntryPointName
<< "\"...\n"; } } while (false)
;
19
Assuming 'DebugFlag' is false
1989 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
20
Loop condition is false. Exiting loop
21
'?' condition is false
1990 if (!OrcRuntime.empty())
22
Assuming the condition is true
23
Taking true branch
1991 Result =
1992 ExitOnErr(runWithRuntime(*S, ExecutorAddr(EntryPoint.getAddress())));
24
Calling 'runWithRuntime'
1993 else
1994 Result = ExitOnErr(
1995 runWithoutRuntime(*S, ExecutorAddr(EntryPoint.getAddress())));
1996 }
1997
1998 // Destroy the session.
1999 ExitOnErr(S->ES.endSession());
2000 S.reset();
2001
2002 // If the executing code set a test result override then use that.
2003 if (UseTestResultOverride)
2004 Result = TestResultOverride;
2005
2006 return Result;
2007}

/build/llvm-toolchain-snapshot-14~++20220119111520+da61cb019eb2/llvm/include/llvm/Support/Error.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
42namespace llvm {
43
44class ErrorSuccess;
45
46/// Base class for error info classes. Do not extend this directly: Extend
47/// the ErrorInfo template subclass instead.
48class ErrorInfoBase {
49public:
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
86private:
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.
157class 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
174protected:
175 /// Create a success value. Prefer using 'Error::success()' for readability
176 Error() {
177 setPtr(nullptr);
178 setChecked(false);
179 }
180
181public:
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
253private:
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 [[noreturn]] void fatalUncheckedError() const;
261#endif
262
263 void assertIsChecked() {
264#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
265 if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false))
266 fatalUncheckedError();
267#endif
268 }
269
270 ErrorInfoBase *getPtr() const {
271#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
272 return reinterpret_cast<ErrorInfoBase*>(
273 reinterpret_cast<uintptr_t>(Payload) &
274 ~static_cast<uintptr_t>(0x1));
275#else
276 return Payload;
277#endif
278 }
279
280 void setPtr(ErrorInfoBase *EI) {
281#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
282 Payload = reinterpret_cast<ErrorInfoBase*>(
283 (reinterpret_cast<uintptr_t>(EI) &
284 ~static_cast<uintptr_t>(0x1)) |
285 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
286#else
287 Payload = EI;
288#endif
289 }
290
291 bool getChecked() const {
292#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
293 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
294#else
295 return true;
296#endif
297 }
298
299 void setChecked(bool V) {
300#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
301 Payload = reinterpret_cast<ErrorInfoBase*>(
302 (reinterpret_cast<uintptr_t>(Payload) &
303 ~static_cast<uintptr_t>(0x1)) |
304 (V ? 0 : 1));
305#endif
306 }
307
308 std::unique_ptr<ErrorInfoBase> takePayload() {
309 std::unique_ptr<ErrorInfoBase> Tmp(getPtr());
310 setPtr(nullptr);
311 setChecked(true);
312 return Tmp;
313 }
314
315 friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) {
316 if (auto *P = E.getPtr())
317 P->log(OS);
318 else
319 OS << "success";
320 return OS;
321 }
322
323 ErrorInfoBase *Payload = nullptr;
324};
325
326/// Subclass of Error for the sole purpose of identifying the success path in
327/// the type system. This allows to catch invalid conversion to Expected<T> at
328/// compile time.
329class ErrorSuccess final : public Error {};
330
331inline ErrorSuccess Error::success() { return ErrorSuccess(); }
332
333/// Make a Error instance representing failure using the given error info
334/// type.
335template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) {
336 return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
337}
338
339/// Base class for user error types. Users should declare their error types
340/// like:
341///
342/// class MyError : public ErrorInfo<MyError> {
343/// ....
344/// };
345///
346/// This class provides an implementation of the ErrorInfoBase::kind
347/// method, which is used by the Error RTTI system.
348template <typename ThisErrT, typename ParentErrT = ErrorInfoBase>
349class ErrorInfo : public ParentErrT {
350public:
351 using ParentErrT::ParentErrT; // inherit constructors
352
353 static const void *classID() { return &ThisErrT::ID; }
354
355 const void *dynamicClassID() const override { return &ThisErrT::ID; }
356
357 bool isA(const void *const ClassID) const override {
358 return ClassID == classID() || ParentErrT::isA(ClassID);
359 }
360};
361
362/// Special ErrorInfo subclass representing a list of ErrorInfos.
363/// Instances of this class are constructed by joinError.
364class ErrorList final : public ErrorInfo<ErrorList> {
365 // handleErrors needs to be able to iterate the payload list of an
366 // ErrorList.
367 template <typename... HandlerTs>
368 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
369
370 // joinErrors is implemented in terms of join.
371 friend Error joinErrors(Error, Error);
372
373public:
374 void log(raw_ostream &OS) const override {
375 OS << "Multiple errors:\n";
376 for (const auto &ErrPayload : Payloads) {
377 ErrPayload->log(OS);
378 OS << "\n";
379 }
380 }
381
382 std::error_code convertToErrorCode() const override;
383
384 // Used by ErrorInfo::classID.
385 static char ID;
386
387private:
388 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
389 std::unique_ptr<ErrorInfoBase> Payload2) {
390 assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&(static_cast <bool> (!Payload1->isA<ErrorList>
() && !Payload2->isA<ErrorList>() &&
"ErrorList constructor payloads should be singleton errors")
? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "llvm/include/llvm/Support/Error.h", 391, __extension__ __PRETTY_FUNCTION__
))
391 "ErrorList constructor payloads should be singleton errors")(static_cast <bool> (!Payload1->isA<ErrorList>
() && !Payload2->isA<ErrorList>() &&
"ErrorList constructor payloads should be singleton errors")
? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "llvm/include/llvm/Support/Error.h", 391, __extension__ __PRETTY_FUNCTION__
))
;
392 Payloads.push_back(std::move(Payload1));
393 Payloads.push_back(std::move(Payload2));
394 }
395
396 static Error join(Error E1, Error E2) {
397 if (!E1)
398 return E2;
399 if (!E2)
400 return E1;
401 if (E1.isA<ErrorList>()) {
402 auto &E1List = static_cast<ErrorList &>(*E1.getPtr());
403 if (E2.isA<ErrorList>()) {
404 auto E2Payload = E2.takePayload();
405 auto &E2List = static_cast<ErrorList &>(*E2Payload);
406 for (auto &Payload : E2List.Payloads)
407 E1List.Payloads.push_back(std::move(Payload));
408 } else
409 E1List.Payloads.push_back(E2.takePayload());
410
411 return E1;
412 }
413 if (E2.isA<ErrorList>()) {
414 auto &E2List = static_cast<ErrorList &>(*E2.getPtr());
415 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
416 return E2;
417 }
418 return Error(std::unique_ptr<ErrorList>(
419 new ErrorList(E1.takePayload(), E2.takePayload())));
420 }
421
422 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
423};
424
425/// Concatenate errors. The resulting Error is unchecked, and contains the
426/// ErrorInfo(s), if any, contained in E1, followed by the
427/// ErrorInfo(s), if any, contained in E2.
428inline Error joinErrors(Error E1, Error E2) {
429 return ErrorList::join(std::move(E1), std::move(E2));
430}
431
432/// Tagged union holding either a T or a Error.
433///
434/// This class parallels ErrorOr, but replaces error_code with Error. Since
435/// Error cannot be copied, this class replaces getError() with
436/// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the
437/// error class type.
438///
439/// Example usage of 'Expected<T>' as a function return type:
440///
441/// @code{.cpp}
442/// Expected<int> myDivide(int A, int B) {
443/// if (B == 0) {
444/// // return an Error
445/// return createStringError(inconvertibleErrorCode(),
446/// "B must not be zero!");
447/// }
448/// // return an integer
449/// return A / B;
450/// }
451/// @endcode
452///
453/// Checking the results of to a function returning 'Expected<T>':
454/// @code{.cpp}
455/// if (auto E = Result.takeError()) {
456/// // We must consume the error. Typically one of:
457/// // - return the error to our caller
458/// // - toString(), when logging
459/// // - consumeError(), to silently swallow the error
460/// // - handleErrors(), to distinguish error types
461/// errs() << "Problem with division " << toString(std::move(E)) << "\n";
462/// return;
463/// }
464/// // use the result
465/// outs() << "The answer is " << *Result << "\n";
466/// @endcode
467///
468/// For unit-testing a function returning an 'Expceted<T>', see the
469/// 'EXPECT_THAT_EXPECTED' macros in llvm/Testing/Support/Error.h
470
471template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected {
472 template <class T1> friend class ExpectedAsOutParameter;
473 template <class OtherT> friend class Expected;
474
475 static constexpr bool isRef = std::is_reference<T>::value;
476
477 using wrap = std::reference_wrapper<std::remove_reference_t<T>>;
478
479 using error_type = std::unique_ptr<ErrorInfoBase>;
480
481public:
482 using storage_type = std::conditional_t<isRef, wrap, T>;
483 using value_type = T;
484
485private:
486 using reference = std::remove_reference_t<T> &;
487 using const_reference = const std::remove_reference_t<T> &;
488 using pointer = std::remove_reference_t<T> *;
489 using const_pointer = const std::remove_reference_t<T> *;
490
491public:
492 /// Create an Expected<T> error value from the given Error.
493 Expected(Error Err)
494 : HasError(true)
495#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
496 // Expected is unchecked upon construction in Debug builds.
497 , Unchecked(true)
498#endif
499 {
500 assert(Err && "Cannot create Expected<T> from Error success value.")(static_cast <bool> (Err && "Cannot create Expected<T> from Error success value."
) ? void (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\""
, "llvm/include/llvm/Support/Error.h", 500, __extension__ __PRETTY_FUNCTION__
))
;
501 new (getErrorStorage()) error_type(Err.takePayload());
502 }
503
504 /// Forbid to convert from Error::success() implicitly, this avoids having
505 /// Expected<T> foo() { return Error::success(); } which compiles otherwise
506 /// but triggers the assertion above.
507 Expected(ErrorSuccess) = delete;
508
509 /// Create an Expected<T> success value from the given OtherT value, which
510 /// must be convertible to T.
511 template <typename OtherT>
512 Expected(OtherT &&Val,
513 std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
514 : HasError(false)
515#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
516 // Expected is unchecked upon construction in Debug builds.
517 ,
518 Unchecked(true)
519#endif
520 {
521 new (getStorage()) storage_type(std::forward<OtherT>(Val));
29
Assigned value is garbage or undefined
522 }
523
524 /// Move construct an Expected<T> value.
525 Expected(Expected &&Other) { moveConstruct(std::move(Other)); }
526
527 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
528 /// must be convertible to T.
529 template <class OtherT>
530 Expected(
531 Expected<OtherT> &&Other,
532 std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) {
533 moveConstruct(std::move(Other));
534 }
535
536 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
537 /// isn't convertible to T.
538 template <class OtherT>
539 explicit Expected(
540 Expected<OtherT> &&Other,
541 std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) {
542 moveConstruct(std::move(Other));
543 }
544
545 /// Move-assign from another Expected<T>.
546 Expected &operator=(Expected &&Other) {
547 moveAssign(std::move(Other));
548 return *this;
549 }
550
551 /// Destroy an Expected<T>.
552 ~Expected() {
553 assertIsChecked();
554 if (!HasError)
555 getStorage()->~storage_type();
556 else
557 getErrorStorage()->~error_type();
558 }
559
560 /// Return false if there is an error.
561 explicit operator bool() {
562#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
563 Unchecked = HasError;
564#endif
565 return !HasError;
566 }
567
568 /// Returns a reference to the stored T value.
569 reference get() {
570 assertIsChecked();
571 return *getStorage();
572 }
573
574 /// Returns a const reference to the stored T value.
575 const_reference get() const {
576 assertIsChecked();
577 return const_cast<Expected<T> *>(this)->get();
578 }
579
580 /// Returns \a takeError() after moving the held T (if any) into \p V.
581 template <class OtherT>
582 Error moveInto(OtherT &Value,
583 std::enable_if_t<std::is_assignable<OtherT &, T &&>::value> * =
584 nullptr) && {
585 if (*this)
586 Value = std::move(get());
587 return takeError();
588 }
589
590 /// Check that this Expected<T> is an error of type ErrT.
591 template <typename ErrT> bool errorIsA() const {
592 return HasError && (*getErrorStorage())->template isA<ErrT>();
593 }
594
595 /// Take ownership of the stored error.
596 /// After calling this the Expected<T> is in an indeterminate state that can
597 /// only be safely destructed. No further calls (beside the destructor) should
598 /// be made on the Expected<T> value.
599 Error takeError() {
600#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
601 Unchecked = false;
602#endif
603 return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
604 }
605
606 /// Returns a pointer to the stored T value.
607 pointer operator->() {
608 assertIsChecked();
609 return toPointer(getStorage());
610 }
611
612 /// Returns a const pointer to the stored T value.
613 const_pointer operator->() const {
614 assertIsChecked();
615 return toPointer(getStorage());
616 }
617
618 /// Returns a reference to the stored T value.
619 reference operator*() {
620 assertIsChecked();
621 return *getStorage();
622 }
623
624 /// Returns a const reference to the stored T value.
625 const_reference operator*() const {
626 assertIsChecked();
627 return *getStorage();
628 }
629
630private:
631 template <class T1>
632 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
633 return &a == &b;
634 }
635
636 template <class T1, class T2>
637 static bool compareThisIfSameType(const T1 &, const T2 &) {
638 return false;
639 }
640
641 template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) {
642 HasError = Other.HasError;
643#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
644 Unchecked = true;
645 Other.Unchecked = false;
646#endif
647
648 if (!HasError)
649 new (getStorage()) storage_type(std::move(*Other.getStorage()));
650 else
651 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
652 }
653
654 template <class OtherT> void moveAssign(Expected<OtherT> &&Other) {
655 assertIsChecked();
656
657 if (compareThisIfSameType(*this, Other))
658 return;
659
660 this->~Expected();
661 new (this) Expected(std::move(Other));
662 }
663
664 pointer toPointer(pointer Val) { return Val; }
665
666 const_pointer toPointer(const_pointer Val) const { return Val; }
667
668 pointer toPointer(wrap *Val) { return &Val->get(); }
669
670 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
671
672 storage_type *getStorage() {
673 assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!"
) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "llvm/include/llvm/Support/Error.h", 673, __extension__ __PRETTY_FUNCTION__
))
;
674 return reinterpret_cast<storage_type *>(&TStorage);
675 }
676
677 const storage_type *getStorage() const {
678 assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!"
) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "llvm/include/llvm/Support/Error.h", 678, __extension__ __PRETTY_FUNCTION__
))
;
679 return reinterpret_cast<const storage_type *>(&TStorage);
680 }
681
682 error_type *getErrorStorage() {
683 assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!"
) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "llvm/include/llvm/Support/Error.h", 683, __extension__ __PRETTY_FUNCTION__
))
;
684 return reinterpret_cast<error_type *>(&ErrorStorage);
685 }
686
687 const error_type *getErrorStorage() const {
688 assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!"
) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "llvm/include/llvm/Support/Error.h", 688, __extension__ __PRETTY_FUNCTION__
))
;
689 return reinterpret_cast<const error_type *>(&ErrorStorage);
690 }
691
692 // Used by ExpectedAsOutParameter to reset the checked flag.
693 void setUnchecked() {
694#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
695 Unchecked = true;
696#endif
697 }
698
699#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
700 [[noreturn]] LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) void fatalUncheckedExpected() const {
701 dbgs() << "Expected<T> must be checked before access or destruction.\n";
702 if (HasError) {
703 dbgs() << "Unchecked Expected<T> contained error:\n";
704 (*getErrorStorage())->log(dbgs());
705 } else
706 dbgs() << "Expected<T> value was in success state. (Note: Expected<T> "
707 "values in success mode must still be checked prior to being "
708 "destroyed).\n";
709 abort();
710 }
711#endif
712
713 void assertIsChecked() const {
714#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
715 if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false))
716 fatalUncheckedExpected();
717#endif
718 }
719
720 union {
721 AlignedCharArrayUnion<storage_type> TStorage;
722 AlignedCharArrayUnion<error_type> ErrorStorage;
723 };
724 bool HasError : 1;
725#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
726 bool Unchecked : 1;
727#endif
728};
729
730/// Report a serious error, calling any installed error handler. See
731/// ErrorHandling.h.
732[[noreturn]] void report_fatal_error(Error Err, bool gen_crash_diag = true);
733
734/// Report a fatal error if Err is a failure value.
735///
736/// This function can be used to wrap calls to fallible functions ONLY when it
737/// is known that the Error will always be a success value. E.g.
738///
739/// @code{.cpp}
740/// // foo only attempts the fallible operation if DoFallibleOperation is
741/// // true. If DoFallibleOperation is false then foo always returns
742/// // Error::success().
743/// Error foo(bool DoFallibleOperation);
744///
745/// cantFail(foo(false));
746/// @endcode
747inline void cantFail(Error Err, const char *Msg = nullptr) {
748 if (Err) {
749 if (!Msg)
750 Msg = "Failure value returned from cantFail wrapped call";
751#ifndef NDEBUG
752 std::string Str;
753 raw_string_ostream OS(Str);
754 OS << Msg << "\n" << Err;
755 Msg = OS.str().c_str();
756#endif
757 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "llvm/include/llvm/Support/Error.h"
, 757)
;
758 }
759}
760
761/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
762/// returns the contained value.
763///
764/// This function can be used to wrap calls to fallible functions ONLY when it
765/// is known that the Error will always be a success value. E.g.
766///
767/// @code{.cpp}
768/// // foo only attempts the fallible operation if DoFallibleOperation is
769/// // true. If DoFallibleOperation is false then foo always returns an int.
770/// Expected<int> foo(bool DoFallibleOperation);
771///
772/// int X = cantFail(foo(false));
773/// @endcode
774template <typename T>
775T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
776 if (ValOrErr)
777 return std::move(*ValOrErr);
778 else {
779 if (!Msg)
780 Msg = "Failure value returned from cantFail wrapped call";
781#ifndef NDEBUG
782 std::string Str;
783 raw_string_ostream OS(Str);
784 auto E = ValOrErr.takeError();
785 OS << Msg << "\n" << E;
786 Msg = OS.str().c_str();
787#endif
788 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "llvm/include/llvm/Support/Error.h"
, 788)
;
789 }
790}
791
792/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
793/// returns the contained reference.
794///
795/// This function can be used to wrap calls to fallible functions ONLY when it
796/// is known that the Error will always be a success value. E.g.
797///
798/// @code{.cpp}
799/// // foo only attempts the fallible operation if DoFallibleOperation is
800/// // true. If DoFallibleOperation is false then foo always returns a Bar&.
801/// Expected<Bar&> foo(bool DoFallibleOperation);
802///
803/// Bar &X = cantFail(foo(false));
804/// @endcode
805template <typename T>
806T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
807 if (ValOrErr)
808 return *ValOrErr;
809 else {
810 if (!Msg)
811 Msg = "Failure value returned from cantFail wrapped call";
812#ifndef NDEBUG
813 std::string Str;
814 raw_string_ostream OS(Str);
815 auto E = ValOrErr.takeError();
816 OS << Msg << "\n" << E;
817 Msg = OS.str().c_str();
818#endif
819 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "llvm/include/llvm/Support/Error.h"
, 819)
;
820 }
821}
822
823/// Helper for testing applicability of, and applying, handlers for
824/// ErrorInfo types.
825template <typename HandlerT>
826class ErrorHandlerTraits
827 : public ErrorHandlerTraits<decltype(
828 &std::remove_reference<HandlerT>::type::operator())> {};
829
830// Specialization functions of the form 'Error (const ErrT&)'.
831template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> {
832public:
833 static bool appliesTo(const ErrorInfoBase &E) {
834 return E.template isA<ErrT>();
835 }
836
837 template <typename HandlerT>
838 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
839 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "llvm/include/llvm/Support/Error.h", 839, __extension__ __PRETTY_FUNCTION__
))
;
840 return H(static_cast<ErrT &>(*E));
841 }
842};
843
844// Specialization functions of the form 'void (const ErrT&)'.
845template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> {
846public:
847 static bool appliesTo(const ErrorInfoBase &E) {
848 return E.template isA<ErrT>();
849 }
850
851 template <typename HandlerT>
852 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
853 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "llvm/include/llvm/Support/Error.h", 853, __extension__ __PRETTY_FUNCTION__
))
;
854 H(static_cast<ErrT &>(*E));
855 return Error::success();
856 }
857};
858
859/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
860template <typename ErrT>
861class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> {
862public:
863 static bool appliesTo(const ErrorInfoBase &E) {
864 return E.template isA<ErrT>();
865 }
866
867 template <typename HandlerT>
868 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
869 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "llvm/include/llvm/Support/Error.h", 869, __extension__ __PRETTY_FUNCTION__
))
;
870 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
871 return H(std::move(SubE));
872 }
873};
874
875/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
876template <typename ErrT>
877class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
878public:
879 static bool appliesTo(const ErrorInfoBase &E) {
880 return E.template isA<ErrT>();
881 }
882
883 template <typename HandlerT>
884 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
885 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "llvm/include/llvm/Support/Error.h", 885, __extension__ __PRETTY_FUNCTION__
))
;
886 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
887 H(std::move(SubE));
888 return Error::success();
889 }
890};
891
892// Specialization for member functions of the form 'RetT (const ErrT&)'.
893template <typename C, typename RetT, typename ErrT>
894class ErrorHandlerTraits<RetT (C::*)(ErrT &)>
895 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
896
897// Specialization for member functions of the form 'RetT (const ErrT&) const'.
898template <typename C, typename RetT, typename ErrT>
899class ErrorHandlerTraits<RetT (C::*)(ErrT &) const>
900 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
901
902// Specialization for member functions of the form 'RetT (const ErrT&)'.
903template <typename C, typename RetT, typename ErrT>
904class ErrorHandlerTraits<RetT (C::*)(const ErrT &)>
905 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
906
907// Specialization for member functions of the form 'RetT (const ErrT&) const'.
908template <typename C, typename RetT, typename ErrT>
909class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const>
910 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
911
912/// Specialization for member functions of the form
913/// 'RetT (std::unique_ptr<ErrT>)'.
914template <typename C, typename RetT, typename ErrT>
915class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
916 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
917
918/// Specialization for member functions of the form
919/// 'RetT (std::unique_ptr<ErrT>) const'.
920template <typename C, typename RetT, typename ErrT>
921class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const>
922 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
923
924inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) {
925 return Error(std::move(Payload));
926}
927
928template <typename HandlerT, typename... HandlerTs>
929Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload,
930 HandlerT &&Handler, HandlerTs &&... Handlers) {
931 if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload))
932 return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler),
933 std::move(Payload));
934 return handleErrorImpl(std::move(Payload),
935 std::forward<HandlerTs>(Handlers)...);
936}
937
938/// Pass the ErrorInfo(s) contained in E to their respective handlers. Any
939/// unhandled errors (or Errors returned by handlers) are re-concatenated and
940/// returned.
941/// Because this function returns an error, its result must also be checked
942/// or returned. If you intend to handle all errors use handleAllErrors
943/// (which returns void, and will abort() on unhandled errors) instead.
944template <typename... HandlerTs>
945Error handleErrors(Error E, HandlerTs &&... Hs) {
946 if (!E)
947 return Error::success();
948
949 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
950
951 if (Payload->isA<ErrorList>()) {
952 ErrorList &List = static_cast<ErrorList &>(*Payload);
953 Error R;
954 for (auto &P : List.Payloads)
955 R = ErrorList::join(
956 std::move(R),
957 handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...));
958 return R;
959 }
960
961 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
962}
963
964/// Behaves the same as handleErrors, except that by contract all errors
965/// *must* be handled by the given handlers (i.e. there must be no remaining
966/// errors after running the handlers, or llvm_unreachable is called).
967template <typename... HandlerTs>
968void handleAllErrors(Error E, HandlerTs &&... Handlers) {
969 cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...));
970}
971
972/// Check that E is a non-error, then drop it.
973/// If E is an error, llvm_unreachable will be called.
974inline void handleAllErrors(Error E) {
975 cantFail(std::move(E));
976}
977
978/// Handle any errors (if present) in an Expected<T>, then try a recovery path.
979///
980/// If the incoming value is a success value it is returned unmodified. If it
981/// is a failure value then it the contained error is passed to handleErrors.
982/// If handleErrors is able to handle the error then the RecoveryPath functor
983/// is called to supply the final result. If handleErrors is not able to
984/// handle all errors then the unhandled errors are returned.
985///
986/// This utility enables the follow pattern:
987///
988/// @code{.cpp}
989/// enum FooStrategy { Aggressive, Conservative };
990/// Expected<Foo> foo(FooStrategy S);
991///
992/// auto ResultOrErr =
993/// handleExpected(
994/// foo(Aggressive),
995/// []() { return foo(Conservative); },
996/// [](AggressiveStrategyError&) {
997/// // Implicitly conusme this - we'll recover by using a conservative
998/// // strategy.
999/// });
1000///
1001/// @endcode
1002template <typename T, typename RecoveryFtor, typename... HandlerTs>
1003Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath,
1004 HandlerTs &&... Handlers) {
1005 if (ValOrErr)
1006 return ValOrErr;
1007
1008 if (auto Err = handleErrors(ValOrErr.takeError(),
1009 std::forward<HandlerTs>(Handlers)...))
1010 return std::move(Err);
1011
1012 return RecoveryPath();
1013}
1014
1015/// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner
1016/// will be printed before the first one is logged. A newline will be printed
1017/// after each error.
1018///
1019/// This function is compatible with the helpers from Support/WithColor.h. You
1020/// can pass any of them as the OS. Please consider using them instead of
1021/// including 'error: ' in the ErrorBanner.
1022///
1023/// This is useful in the base level of your program to allow clean termination
1024/// (allowing clean deallocation of resources, etc.), while reporting error
1025/// information to the user.
1026void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {});
1027
1028/// Write all error messages (if any) in E to a string. The newline character
1029/// is used to separate error messages.
1030inline std::string toString(Error E) {
1031 SmallVector<std::string, 2> Errors;
1032 handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) {
1033 Errors.push_back(EI.message());
1034 });
1035 return join(Errors.begin(), Errors.end(), "\n");
1036}
1037
1038/// Consume a Error without doing anything. This method should be used
1039/// only where an error can be considered a reasonable and expected return
1040/// value.
1041///
1042/// Uses of this method are potentially indicative of design problems: If it's
1043/// legitimate to do nothing while processing an "error", the error-producer
1044/// might be more clearly refactored to return an Optional<T>.
1045inline void consumeError(Error Err) {
1046 handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {});
1047}
1048
1049/// Convert an Expected to an Optional without doing anything. This method
1050/// should be used only where an error can be considered a reasonable and
1051/// expected return value.
1052///
1053/// Uses of this method are potentially indicative of problems: perhaps the
1054/// error should be propagated further, or the error-producer should just
1055/// return an Optional in the first place.
1056template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) {
1057 if (E)
1058 return std::move(*E);
1059 consumeError(E.takeError());
1060 return None;
1061}
1062
1063/// Helper for converting an Error to a bool.
1064///
1065/// This method returns true if Err is in an error state, or false if it is
1066/// in a success state. Puts Err in a checked state in both cases (unlike
1067/// Error::operator bool(), which only does this for success states).
1068inline bool errorToBool(Error Err) {
1069 bool IsError = static_cast<bool>(Err);
1070 if (IsError)
1071 consumeError(std::move(Err));
1072 return IsError;
1073}
1074
1075/// Helper for Errors used as out-parameters.
1076///
1077/// This helper is for use with the Error-as-out-parameter idiom, where an error
1078/// is passed to a function or method by reference, rather than being returned.
1079/// In such cases it is helpful to set the checked bit on entry to the function
1080/// so that the error can be written to (unchecked Errors abort on assignment)
1081/// and clear the checked bit on exit so that clients cannot accidentally forget
1082/// to check the result. This helper performs these actions automatically using
1083/// RAII:
1084///
1085/// @code{.cpp}
1086/// Result foo(Error &Err) {
1087/// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set
1088/// // <body of foo>
1089/// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed.
1090/// }
1091/// @endcode
1092///
1093/// ErrorAsOutParameter takes an Error* rather than Error& so that it can be
1094/// used with optional Errors (Error pointers that are allowed to be null). If
1095/// ErrorAsOutParameter took an Error reference, an instance would have to be
1096/// created inside every condition that verified that Error was non-null. By
1097/// taking an Error pointer we can just create one instance at the top of the
1098/// function.
1099class ErrorAsOutParameter {
1100public:
1101 ErrorAsOutParameter(Error *Err) : Err(Err) {
1102 // Raise the checked bit if Err is success.
1103 if (Err)
1104 (void)!!*Err;
1105 }
1106
1107 ~ErrorAsOutParameter() {
1108 // Clear the checked bit.
1109 if (Err && !*Err)
1110 *Err = Error::success();
1111 }
1112
1113private:
1114 Error *Err;
1115};
1116
1117/// Helper for Expected<T>s used as out-parameters.
1118///
1119/// See ErrorAsOutParameter.
1120template <typename T>
1121class ExpectedAsOutParameter {
1122public:
1123 ExpectedAsOutParameter(Expected<T> *ValOrErr)
1124 : ValOrErr(ValOrErr) {
1125 if (ValOrErr)
1126 (void)!!*ValOrErr;
1127 }
1128
1129 ~ExpectedAsOutParameter() {
1130 if (ValOrErr)
1131 ValOrErr->setUnchecked();
1132 }
1133
1134private:
1135 Expected<T> *ValOrErr;
1136};
1137
1138/// This class wraps a std::error_code in a Error.
1139///
1140/// This is useful if you're writing an interface that returns a Error
1141/// (or Expected) and you want to call code that still returns
1142/// std::error_codes.
1143class ECError : public ErrorInfo<ECError> {
1144 friend Error errorCodeToError(std::error_code);
1145
1146 virtual void anchor() override;
1147
1148public:
1149 void setErrorCode(std::error_code EC) { this->EC = EC; }
1150 std::error_code convertToErrorCode() const override { return EC; }
1151 void log(raw_ostream &OS) const override { OS << EC.message(); }
1152
1153 // Used by ErrorInfo::classID.
1154 static char ID;
1155
1156protected:
1157 ECError() = default;
1158 ECError(std::error_code EC) : EC(EC) {}
1159
1160 std::error_code EC;
1161};
1162
1163/// The value returned by this function can be returned from convertToErrorCode
1164/// for Error values where no sensible translation to std::error_code exists.
1165/// It should only be used in this situation, and should never be used where a
1166/// sensible conversion to std::error_code is available, as attempts to convert
1167/// to/from this error will result in a fatal error. (i.e. it is a programmatic
1168/// error to try to convert such a value).
1169std::error_code inconvertibleErrorCode();
1170
1171/// Helper for converting an std::error_code to a Error.
1172Error errorCodeToError(std::error_code EC);
1173
1174/// Helper for converting an ECError to a std::error_code.
1175///
1176/// This method requires that Err be Error() or an ECError, otherwise it
1177/// will trigger a call to abort().
1178std::error_code errorToErrorCode(Error Err);
1179
1180/// Convert an ErrorOr<T> to an Expected<T>.
1181template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) {
1182 if (auto EC = EO.getError())
1183 return errorCodeToError(EC);
1184 return std::move(*EO);
1185}
1186
1187/// Convert an Expected<T> to an ErrorOr<T>.
1188template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) {
1189 if (auto Err = E.takeError())
1190 return errorToErrorCode(std::move(Err));
1191 return std::move(*E);
1192}
1193
1194/// This class wraps a string in an Error.
1195///
1196/// StringError is useful in cases where the client is not expected to be able
1197/// to consume the specific error message programmatically (for example, if the
1198/// error message is to be presented to the user).
1199///
1200/// StringError can also be used when additional information is to be printed
1201/// along with a error_code message. Depending on the constructor called, this
1202/// class can either display:
1203/// 1. the error_code message (ECError behavior)
1204/// 2. a string
1205/// 3. the error_code message and a string
1206///
1207/// These behaviors are useful when subtyping is required; for example, when a
1208/// specific library needs an explicit error type. In the example below,
1209/// PDBError is derived from StringError:
1210///
1211/// @code{.cpp}
1212/// Expected<int> foo() {
1213/// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading,
1214/// "Additional information");
1215/// }
1216/// @endcode
1217///
1218class StringError : public ErrorInfo<StringError> {
1219public:
1220 static char ID;
1221
1222 // Prints EC + S and converts to EC
1223 StringError(std::error_code EC, const Twine &S = Twine());
1224
1225 // Prints S and converts to EC
1226 StringError(const Twine &S, std::error_code EC);
1227
1228 void log(raw_ostream &OS) const override;
1229 std::error_code convertToErrorCode() const override;
1230
1231 const std::string &getMessage() const { return Msg; }
1232
1233private:
1234 std::string Msg;
1235 std::error_code EC;
1236 const bool PrintMsgOnly = false;
1237};
1238
1239/// Create formatted StringError object.
1240template <typename... Ts>
1241inline Error createStringError(std::error_code EC, char const *Fmt,
1242 const Ts &... Vals) {
1243 std::string Buffer;
1244 raw_string_ostream Stream(Buffer);
1245 Stream << format(Fmt, Vals...);
1246 return make_error<StringError>(Stream.str(), EC);
1247}
1248
1249Error createStringError(std::error_code EC, char const *Msg);
1250
1251inline Error createStringError(std::error_code EC, const Twine &S) {
1252 return createStringError(EC, S.str().c_str());
1253}
1254
1255template <typename... Ts>
1256inline Error createStringError(std::errc EC, char const *Fmt,
1257 const Ts &... Vals) {
1258 return createStringError(std::make_error_code(EC), Fmt, Vals...);
1259}
1260
1261/// This class wraps a filename and another Error.
1262///
1263/// In some cases, an error needs to live along a 'source' name, in order to
1264/// show more detailed information to the user.
1265class FileError final : public ErrorInfo<FileError> {
1266
1267 friend Error createFileError(const Twine &, Error);
1268 friend Error createFileError(const Twine &, size_t, Error);
1269
1270public:
1271 void log(raw_ostream &OS) const override {
1272 assert(Err && "Trying to log after takeError().")(static_cast <bool> (Err && "Trying to log after takeError()."
) ? void (0) : __assert_fail ("Err && \"Trying to log after takeError().\""
, "llvm/include/llvm/Support/Error.h", 1272, __extension__ __PRETTY_FUNCTION__
))
;
1273 OS << "'" << FileName << "': ";
1274 if (Line.hasValue())
1275 OS << "line " << Line.getValue() << ": ";
1276 Err->log(OS);
1277 }
1278
1279 std::string messageWithoutFileInfo() const {
1280 std::string Msg;
1281 raw_string_ostream OS(Msg);
1282 Err->log(OS);
1283 return OS.str();
1284 }
1285
1286 StringRef getFileName() { return FileName; }
1287
1288 Error takeError() { return Error(std::move(Err)); }
1289
1290 std::error_code convertToErrorCode() const override;
1291
1292 // Used by ErrorInfo::classID.
1293 static char ID;
1294
1295private:
1296 FileError(const Twine &F, Optional<size_t> LineNum,
1297 std::unique_ptr<ErrorInfoBase> E) {
1298 assert(E && "Cannot create FileError from Error success value.")(static_cast <bool> (E && "Cannot create FileError from Error success value."
) ? void (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\""
, "llvm/include/llvm/Support/Error.h", 1298, __extension__ __PRETTY_FUNCTION__
))
;
1299 FileName = F.str();
1300 Err = std::move(E);
1301 Line = std::move(LineNum);
1302 }
1303
1304 static Error build(const Twine &F, Optional<size_t> Line, Error E) {
1305 std::unique_ptr<ErrorInfoBase> Payload;
1306 handleAllErrors(std::move(E),
1307 [&](std::unique_ptr<ErrorInfoBase> EIB) -> Error {
1308 Payload = std::move(EIB);
1309 return Error::success();
1310 });
1311 return Error(
1312 std::unique_ptr<FileError>(new FileError(F, Line, std::move(Payload))));
1313 }
1314
1315 std::string FileName;
1316 Optional<size_t> Line;
1317 std::unique_ptr<ErrorInfoBase> Err;
1318};
1319
1320/// Concatenate a source file path and/or name with an Error. The resulting
1321/// Error is unchecked.
1322inline Error createFileError(const Twine &F, Error E) {
1323 return FileError::build(F, Optional<size_t>(), std::move(E));
1324}
1325
1326/// Concatenate a source file path and/or name with line number and an Error.
1327/// The resulting Error is unchecked.
1328inline Error createFileError(const Twine &F, size_t Line, Error E) {
1329 return FileError::build(F, Optional<size_t>(Line), std::move(E));
1330}
1331
1332/// Concatenate a source file path and/or name with a std::error_code
1333/// to form an Error object.
1334inline Error createFileError(const Twine &F, std::error_code EC) {
1335 return createFileError(F, errorCodeToError(EC));
1336}
1337
1338/// Concatenate a source file path and/or name with line number and
1339/// std::error_code to form an Error object.
1340inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
1341 return createFileError(F, Line, errorCodeToError(EC));
1342}
1343
1344Error createFileError(const Twine &F, ErrorSuccess) = delete;
1345
1346/// Helper for check-and-exit error handling.
1347///
1348/// For tool use only. NOT FOR USE IN LIBRARY CODE.
1349///
1350class ExitOnError {
1351public:
1352 /// Create an error on exit helper.
1353 ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1)
1354 : Banner(std::move(Banner)),
1355 GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {}
1356
1357 /// Set the banner string for any errors caught by operator().
1358 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1359
1360 /// Set the exit-code mapper function.
1361 void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) {
1362 this->GetExitCode = std::move(GetExitCode);
1363 }
1364
1365 /// Check Err. If it's in a failure state log the error(s) and exit.
1366 void operator()(Error Err) const { checkError(std::move(Err)); }
1367
1368 /// Check E. If it's in a success state then return the contained value. If
1369 /// it's in a failure state log the error(s) and exit.
1370 template <typename T> T operator()(Expected<T> &&E) const {
1371 checkError(E.takeError());
1372 return std::move(*E);
1373 }
1374
1375 /// Check E. If it's in a success state then return the contained reference. If
1376 /// it's in a failure state log the error(s) and exit.
1377 template <typename T> T& operator()(Expected<T&> &&E) const {
1378 checkError(E.takeError());
1379 return *E;
1380 }
1381
1382private:
1383 void checkError(Error Err) const {
1384 if (Err) {
1385 int ExitCode = GetExitCode(Err);
1386 logAllUnhandledErrors(std::move(Err), errs(), Banner);
1387 exit(ExitCode);
1388 }
1389 }
1390
1391 std::string Banner;
1392 std::function<int(const Error &)> GetExitCode;
1393};
1394
1395/// Conversion from Error to LLVMErrorRef for C error bindings.
1396inline LLVMErrorRef wrap(Error Err) {
1397 return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release());
1398}
1399
1400/// Conversion from LLVMErrorRef to Error for C error bindings.
1401inline Error unwrap(LLVMErrorRef ErrRef) {
1402 return Error(std::unique_ptr<ErrorInfoBase>(
1403 reinterpret_cast<ErrorInfoBase *>(ErrRef)));
1404}
1405
1406} // end namespace llvm
1407
1408#endif // LLVM_SUPPORT_ERROR_H