Bug Summary

File:build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include/llvm/Support/Error.h
Warning:line 519, 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-15~++20220420111733+e13d2efed663/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-15/lib/clang/15.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-15~++20220420111733+e13d2efed663/llvm/tools/llvm-jitlink -I include -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/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-15/lib/clang/15.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-15~++20220420111733+e13d2efed663/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -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-15~++20220420111733+e13d2efed663/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -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-04-20-140412-16051-1 -x c++ /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/tools/llvm-jitlink/llvm-jitlink.cpp

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

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