clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name LazyReexports.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 -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "llvm/ExecutionEngine/Orc/LazyReexports.h" |
10 | |
11 | #include "llvm/ADT/Triple.h" |
12 | #include "llvm/ExecutionEngine/Orc/OrcABISupport.h" |
13 | |
14 | #define DEBUG_TYPE "orc" |
15 | |
16 | namespace llvm { |
17 | namespace orc { |
18 | |
19 | LazyCallThroughManager::LazyCallThroughManager( |
20 | ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr, TrampolinePool *TP) |
21 | : ES(ES), ErrorHandlerAddr(ErrorHandlerAddr), TP(TP) {} |
22 | |
23 | Expected<JITTargetAddress> LazyCallThroughManager::getCallThroughTrampoline( |
24 | JITDylib &SourceJD, SymbolStringPtr SymbolName, |
25 | NotifyResolvedFunction NotifyResolved) { |
26 | assert(TP && "TrampolinePool not set"); |
27 | |
28 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
29 | auto Trampoline = TP->getTrampoline(); |
30 | |
31 | if (!Trampoline) |
32 | return Trampoline.takeError(); |
33 | |
34 | Reexports[*Trampoline] = ReexportsEntry{&SourceJD, std::move(SymbolName)}; |
35 | Notifiers[*Trampoline] = std::move(NotifyResolved); |
36 | return *Trampoline; |
37 | } |
38 | |
39 | JITTargetAddress LazyCallThroughManager::reportCallThroughError(Error Err) { |
40 | ES.reportError(std::move(Err)); |
41 | return ErrorHandlerAddr; |
42 | } |
43 | |
44 | Expected<LazyCallThroughManager::ReexportsEntry> |
45 | LazyCallThroughManager::findReexport(JITTargetAddress TrampolineAddr) { |
46 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
47 | auto I = Reexports.find(TrampolineAddr); |
48 | if (I == Reexports.end()) |
49 | return createStringError(inconvertibleErrorCode(), |
50 | "Missing reexport for trampoline address %p", |
51 | TrampolineAddr); |
52 | return I->second; |
53 | } |
54 | |
55 | Error LazyCallThroughManager::notifyResolved(JITTargetAddress TrampolineAddr, |
56 | JITTargetAddress ResolvedAddr) { |
57 | NotifyResolvedFunction NotifyResolved; |
| 3 | | Calling defaulted default constructor for 'unique_function<llvm::Error (unsigned long long)>' | |
|
| 8 | | Returning from default constructor for 'unique_function<llvm::Error (unsigned long long)>' | |
|
58 | { |
59 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
60 | auto I = Notifiers.find(TrampolineAddr); |
61 | if (I != Notifiers.end()) { |
| |
62 | NotifyResolved = std::move(I->second); |
| 10 | | Calling defaulted move assignment operator for 'unique_function<llvm::Error (unsigned long long)>' | |
|
| 20 | | Returning from move assignment operator for 'unique_function<llvm::Error (unsigned long long)>' | |
|
63 | Notifiers.erase(I); |
64 | } |
65 | } |
66 | |
67 | return NotifyResolved ? NotifyResolved(ResolvedAddr) : Error::success(); |
| |
| 22 | | Calling 'unique_function::operator()' | |
|
68 | } |
69 | |
70 | void LazyCallThroughManager::resolveTrampolineLandingAddress( |
71 | JITTargetAddress TrampolineAddr, |
72 | NotifyLandingResolvedFunction NotifyLandingResolved) { |
73 | |
74 | auto Entry = findReexport(TrampolineAddr); |
75 | if (!Entry) |
76 | return NotifyLandingResolved(reportCallThroughError(Entry.takeError())); |
77 | |
78 | |
79 | |
80 | SymbolLookupSet SLS({Entry->SymbolName}); |
81 | auto Callback = [this, TrampolineAddr, SymbolName = Entry->SymbolName, |
82 | NotifyLandingResolved = std::move(NotifyLandingResolved)]( |
83 | Expected<SymbolMap> Result) mutable { |
84 | if (Result) { |
| |
85 | assert(Result->size() == 1 && "Unexpected result size"); |
86 | assert(Result->count(SymbolName) && "Unexpected result value"); |
87 | JITTargetAddress LandingAddr = (*Result)[SymbolName].getAddress(); |
88 | |
89 | if (auto Err = notifyResolved(TrampolineAddr, LandingAddr)) |
| 2 | | Calling 'LazyCallThroughManager::notifyResolved' | |
|
90 | NotifyLandingResolved(reportCallThroughError(std::move(Err))); |
91 | else |
92 | NotifyLandingResolved(LandingAddr); |
93 | } else { |
94 | NotifyLandingResolved(reportCallThroughError(Result.takeError())); |
95 | } |
96 | }; |
97 | |
98 | ES.lookup(LookupKind::Static, |
99 | makeJITDylibSearchOrder(Entry->SourceJD, |
100 | JITDylibLookupFlags::MatchAllSymbols), |
101 | std::move(SLS), SymbolState::Ready, std::move(Callback), |
102 | NoDependenciesToRegister); |
103 | } |
104 | |
105 | Expected<std::unique_ptr<LazyCallThroughManager>> |
106 | createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, |
107 | JITTargetAddress ErrorHandlerAddr) { |
108 | switch (T.getArch()) { |
109 | default: |
110 | return make_error<StringError>( |
111 | std::string("No callback manager available for ") + T.str(), |
112 | inconvertibleErrorCode()); |
113 | |
114 | case Triple::aarch64: |
115 | case Triple::aarch64_32: |
116 | return LocalLazyCallThroughManager::Create<OrcAArch64>(ES, |
117 | ErrorHandlerAddr); |
118 | |
119 | case Triple::x86: |
120 | return LocalLazyCallThroughManager::Create<OrcI386>(ES, ErrorHandlerAddr); |
121 | |
122 | case Triple::mips: |
123 | return LocalLazyCallThroughManager::Create<OrcMips32Be>(ES, |
124 | ErrorHandlerAddr); |
125 | |
126 | case Triple::mipsel: |
127 | return LocalLazyCallThroughManager::Create<OrcMips32Le>(ES, |
128 | ErrorHandlerAddr); |
129 | |
130 | case Triple::mips64: |
131 | case Triple::mips64el: |
132 | return LocalLazyCallThroughManager::Create<OrcMips64>(ES, ErrorHandlerAddr); |
133 | |
134 | case Triple::x86_64: |
135 | if (T.getOS() == Triple::OSType::Win32) |
136 | return LocalLazyCallThroughManager::Create<OrcX86_64_Win32>( |
137 | ES, ErrorHandlerAddr); |
138 | else |
139 | return LocalLazyCallThroughManager::Create<OrcX86_64_SysV>( |
140 | ES, ErrorHandlerAddr); |
141 | } |
142 | } |
143 | |
144 | LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit( |
145 | LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, |
146 | JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc) |
147 | : MaterializationUnit(extractFlags(CallableAliases), nullptr), |
148 | LCTManager(LCTManager), ISManager(ISManager), SourceJD(SourceJD), |
149 | CallableAliases(std::move(CallableAliases)), AliaseeTable(SrcJDLoc) {} |
150 | |
151 | StringRef LazyReexportsMaterializationUnit::getName() const { |
152 | return "<Lazy Reexports>"; |
153 | } |
154 | |
155 | void LazyReexportsMaterializationUnit::materialize( |
156 | std::unique_ptr<MaterializationResponsibility> R) { |
157 | auto RequestedSymbols = R->getRequestedSymbols(); |
158 | |
159 | SymbolAliasMap RequestedAliases; |
160 | for (auto &RequestedSymbol : RequestedSymbols) { |
161 | auto I = CallableAliases.find(RequestedSymbol); |
162 | assert(I != CallableAliases.end() && "Symbol not found in alias map?"); |
163 | RequestedAliases[I->first] = std::move(I->second); |
164 | CallableAliases.erase(I); |
165 | } |
166 | |
167 | if (!CallableAliases.empty()) |
168 | if (auto Err = R->replace(lazyReexports(LCTManager, ISManager, SourceJD, |
169 | std::move(CallableAliases), |
170 | AliaseeTable))) { |
171 | R->getExecutionSession().reportError(std::move(Err)); |
172 | R->failMaterialization(); |
173 | return; |
174 | } |
175 | |
176 | IndirectStubsManager::StubInitsMap StubInits; |
177 | for (auto &Alias : RequestedAliases) { |
178 | |
179 | auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline( |
180 | SourceJD, Alias.second.Aliasee, |
181 | [&ISManager = this->ISManager, |
182 | StubSym = Alias.first](JITTargetAddress ResolvedAddr) -> Error { |
183 | return ISManager.updatePointer(*StubSym, ResolvedAddr); |
184 | }); |
185 | |
186 | if (!CallThroughTrampoline) { |
187 | SourceJD.getExecutionSession().reportError( |
188 | CallThroughTrampoline.takeError()); |
189 | R->failMaterialization(); |
190 | return; |
191 | } |
192 | |
193 | StubInits[*Alias.first] = |
194 | std::make_pair(*CallThroughTrampoline, Alias.second.AliasFlags); |
195 | } |
196 | |
197 | if (AliaseeTable != nullptr && !RequestedAliases.empty()) |
198 | AliaseeTable->trackImpls(RequestedAliases, &SourceJD); |
199 | |
200 | if (auto Err = ISManager.createStubs(StubInits)) { |
201 | SourceJD.getExecutionSession().reportError(std::move(Err)); |
202 | R->failMaterialization(); |
203 | return; |
204 | } |
205 | |
206 | SymbolMap Stubs; |
207 | for (auto &Alias : RequestedAliases) |
208 | Stubs[Alias.first] = ISManager.findStub(*Alias.first, false); |
209 | |
210 | |
211 | cantFail(R->notifyResolved(Stubs)); |
212 | cantFail(R->notifyEmitted()); |
213 | } |
214 | |
215 | void LazyReexportsMaterializationUnit::discard(const JITDylib &JD, |
216 | const SymbolStringPtr &Name) { |
217 | assert(CallableAliases.count(Name) && |
218 | "Symbol not covered by this MaterializationUnit"); |
219 | CallableAliases.erase(Name); |
220 | } |
221 | |
222 | SymbolFlagsMap |
223 | LazyReexportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) { |
224 | SymbolFlagsMap SymbolFlags; |
225 | for (auto &KV : Aliases) { |
226 | assert(KV.second.AliasFlags.isCallable() && |
227 | "Lazy re-exports must be callable symbols"); |
228 | SymbolFlags[KV.first] = KV.second.AliasFlags; |
229 | } |
230 | return SymbolFlags; |
231 | } |
232 | |
233 | } |
234 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | #ifndef LLVM_ADT_FUNCTIONEXTRAS_H |
33 | #define LLVM_ADT_FUNCTIONEXTRAS_H |
34 | |
35 | #include "llvm/ADT/PointerIntPair.h" |
36 | #include "llvm/ADT/PointerUnion.h" |
37 | #include "llvm/ADT/STLForwardCompat.h" |
38 | #include "llvm/Support/MemAlloc.h" |
39 | #include "llvm/Support/type_traits.h" |
40 | #include <memory> |
41 | #include <type_traits> |
42 | |
43 | namespace llvm { |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | template <typename FunctionT> class unique_function; |
56 | |
57 | namespace detail { |
58 | |
59 | template <typename T> |
60 | using EnableIfTrivial = |
61 | std::enable_if_t<llvm::is_trivially_move_constructible<T>::value && |
62 | std::is_trivially_destructible<T>::value>; |
63 | template <typename CallableT, typename ThisT> |
64 | using EnableUnlessSameType = |
65 | std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>::value>; |
66 | template <typename CallableT, typename Ret, typename... Params> |
67 | using EnableIfCallable = |
68 | std::enable_if_t<std::is_void<Ret>::value || |
69 | std::is_convertible<decltype(std::declval<CallableT>()( |
70 | std::declval<Params>()...)), |
71 | Ret>::value>; |
72 | |
73 | template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase { |
74 | protected: |
75 | static constexpr size_t InlineStorageSize = sizeof(void *) * 3; |
76 | |
77 | template <typename T, class = void> |
78 | struct IsSizeLessThanThresholdT : std::false_type {}; |
79 | |
80 | template <typename T> |
81 | struct IsSizeLessThanThresholdT< |
82 | T, std::enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {}; |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | template <typename T> struct AdjustedParamTBase { |
94 | static_assert(!std::is_reference<T>::value, |
95 | "references should be handled by template specialization"); |
96 | using type = typename std::conditional< |
97 | llvm::is_trivially_copy_constructible<T>::value && |
98 | llvm::is_trivially_move_constructible<T>::value && |
99 | IsSizeLessThanThresholdT<T>::value, |
100 | T, T &>::type; |
101 | }; |
102 | |
103 | |
104 | |
105 | |
106 | template <typename T> struct AdjustedParamTBase<T &> { using type = T &; }; |
107 | template <typename T> struct AdjustedParamTBase<T &&> { using type = T &; }; |
108 | |
109 | template <typename T> |
110 | using AdjustedParamT = typename AdjustedParamTBase<T>::type; |
111 | |
112 | |
113 | |
114 | using CallPtrT = ReturnT (*)(void *CallableAddr, |
115 | AdjustedParamT<ParamTs>... Params); |
116 | using MovePtrT = void (*)(void *LHSCallableAddr, void *RHSCallableAddr); |
117 | using DestroyPtrT = void (*)(void *CallableAddr); |
118 | |
119 | |
120 | |
121 | struct alignas(8) TrivialCallback { |
122 | CallPtrT CallPtr; |
123 | }; |
124 | |
125 | |
126 | |
127 | struct alignas(8) NonTrivialCallbacks { |
128 | CallPtrT CallPtr; |
129 | MovePtrT MovePtr; |
130 | DestroyPtrT DestroyPtr; |
131 | }; |
132 | |
133 | |
134 | |
135 | |
136 | using CallbackPointerUnionT = |
137 | PointerUnion<TrivialCallback *, NonTrivialCallbacks *>; |
138 | |
139 | |
140 | |
141 | union StorageUnionT { |
142 | |
143 | |
144 | struct OutOfLineStorageT { |
145 | void *StoragePtr; |
146 | size_t Size; |
147 | size_t Alignment; |
148 | } OutOfLineStorage; |
149 | static_assert( |
150 | sizeof(OutOfLineStorageT) <= InlineStorageSize, |
151 | "Should always use all of the out-of-line storage for inline storage!"); |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | mutable |
158 | typename std::aligned_storage<InlineStorageSize, alignof(void *)>::type |
159 | InlineStorage; |
160 | } StorageUnion; |
161 | |
162 | |
163 | |
164 | |
165 | PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag; |
166 | |
167 | bool isInlineStorage() const { return CallbackAndInlineFlag.getInt(); } |
168 | |
169 | bool isTrivialCallback() const { |
170 | return CallbackAndInlineFlag.getPointer().template is<TrivialCallback *>(); |
171 | } |
172 | |
173 | CallPtrT getTrivialCallback() const { |
174 | return CallbackAndInlineFlag.getPointer().template get<TrivialCallback *>()->CallPtr; |
175 | } |
176 | |
177 | NonTrivialCallbacks *getNonTrivialCallbacks() const { |
178 | return CallbackAndInlineFlag.getPointer() |
179 | .template get<NonTrivialCallbacks *>(); |
180 | } |
181 | |
182 | CallPtrT getCallPtr() const { |
183 | return isTrivialCallback() ? getTrivialCallback() |
184 | : getNonTrivialCallbacks()->CallPtr; |
185 | } |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | void *getCalleePtr() const { |
194 | return isInlineStorage() ? getInlineStorage() : getOutOfLineStorage(); |
| 24 | | Assuming the condition is false | |
|
| |
| 26 | | Calling 'UniqueFunctionBase::getOutOfLineStorage' | |
|
195 | } |
196 | void *getInlineStorage() const { return &StorageUnion.InlineStorage; } |
197 | void *getOutOfLineStorage() const { |
198 | return StorageUnion.OutOfLineStorage.StoragePtr; |
| 27 | | Undefined or garbage value returned to caller |
|
199 | } |
200 | |
201 | size_t getOutOfLineStorageSize() const { |
202 | return StorageUnion.OutOfLineStorage.Size; |
203 | } |
204 | size_t getOutOfLineStorageAlignment() const { |
205 | return StorageUnion.OutOfLineStorage.Alignment; |
206 | } |
207 | |
208 | void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment) { |
209 | StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment}; |
210 | } |
211 | |
212 | template <typename CalledAsT> |
213 | static ReturnT CallImpl(void *CallableAddr, |
214 | AdjustedParamT<ParamTs>... Params) { |
215 | auto &Func = *reinterpret_cast<CalledAsT *>(CallableAddr); |
216 | return Func(std::forward<ParamTs>(Params)...); |
217 | } |
218 | |
219 | template <typename CallableT> |
220 | static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept { |
221 | new (LHSCallableAddr) |
222 | CallableT(std::move(*reinterpret_cast<CallableT *>(RHSCallableAddr))); |
223 | } |
224 | |
225 | template <typename CallableT> |
226 | static void DestroyImpl(void *CallableAddr) noexcept { |
227 | reinterpret_cast<CallableT *>(CallableAddr)->~CallableT(); |
228 | } |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | template <typename CallableT, typename CalledAs, typename Enable = void> |
239 | struct CallbacksHolder { |
240 | static NonTrivialCallbacks Callbacks; |
241 | }; |
242 | |
243 | |
244 | |
245 | template <typename CallableT, typename CalledAs> |
246 | struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> { |
247 | static TrivialCallback Callbacks; |
248 | }; |
249 | |
250 | |
251 | template <typename T> struct CalledAs {}; |
252 | |
253 | |
254 | |
255 | |
256 | template <typename CallableT, typename CalledAsT> |
257 | UniqueFunctionBase(CallableT Callable, CalledAs<CalledAsT>) { |
258 | bool IsInlineStorage = true; |
259 | void *CallableAddr = getInlineStorage(); |
260 | if (sizeof(CallableT) > InlineStorageSize || |
261 | alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) { |
262 | IsInlineStorage = false; |
263 | |
264 | |
265 | auto Size = sizeof(CallableT); |
266 | auto Alignment = alignof(CallableT); |
267 | CallableAddr = allocate_buffer(Size, Alignment); |
268 | setOutOfLineStorage(CallableAddr, Size, Alignment); |
269 | } |
270 | |
271 | |
272 | new (CallableAddr) CallableT(std::move(Callable)); |
273 | CallbackAndInlineFlag.setPointerAndInt( |
274 | &CallbacksHolder<CallableT, CalledAsT>::Callbacks, IsInlineStorage); |
275 | } |
276 | |
277 | ~UniqueFunctionBase() { |
278 | if (!CallbackAndInlineFlag.getPointer()) |
279 | return; |
280 | |
281 | |
282 | bool IsInlineStorage = isInlineStorage(); |
283 | |
284 | if (!isTrivialCallback()) |
285 | getNonTrivialCallbacks()->DestroyPtr( |
286 | IsInlineStorage ? getInlineStorage() : getOutOfLineStorage()); |
287 | |
288 | if (!IsInlineStorage) |
289 | deallocate_buffer(getOutOfLineStorage(), getOutOfLineStorageSize(), |
290 | getOutOfLineStorageAlignment()); |
291 | } |
292 | |
293 | UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept { |
294 | |
295 | CallbackAndInlineFlag = RHS.CallbackAndInlineFlag; |
296 | |
297 | |
298 | if (!RHS) |
| |
299 | return; |
| 17 | | Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr' | |
|
300 | |
301 | if (!isInlineStorage()) { |
302 | |
303 | StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage; |
304 | } else if (isTrivialCallback()) { |
305 | |
306 | memcpy(getInlineStorage(), RHS.getInlineStorage(), InlineStorageSize); |
307 | } else { |
308 | |
309 | getNonTrivialCallbacks()->MovePtr(getInlineStorage(), |
310 | RHS.getInlineStorage()); |
311 | } |
312 | |
313 | |
314 | RHS.CallbackAndInlineFlag = {}; |
315 | |
316 | #ifndef NDEBUG |
317 | |
318 | memset(RHS.getInlineStorage(), 0xAD, InlineStorageSize); |
319 | #endif |
320 | } |
321 | |
322 | UniqueFunctionBase &operator=(UniqueFunctionBase &&RHS) noexcept { |
323 | if (this == &RHS) |
| |
324 | return *this; |
325 | |
326 | |
327 | |
328 | |
329 | this->~UniqueFunctionBase(); |
330 | new (this) UniqueFunctionBase(std::move(RHS)); |
| |
| 14 | | Returning from 'operator new' | |
|
| 15 | | Calling move constructor for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
| 18 | | Returning from move constructor for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
331 | return *this; |
332 | } |
333 | |
334 | UniqueFunctionBase() = default; |
| 5 | | Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr' | |
|
335 | |
336 | public: |
337 | explicit operator bool() const { |
338 | return (bool)CallbackAndInlineFlag.getPointer(); |
339 | } |
340 | }; |
341 | |
342 | template <typename R, typename... P> |
343 | template <typename CallableT, typename CalledAsT, typename Enable> |
344 | typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase< |
345 | R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = { |
346 | &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>}; |
347 | |
348 | template <typename R, typename... P> |
349 | template <typename CallableT, typename CalledAsT> |
350 | typename UniqueFunctionBase<R, P...>::TrivialCallback |
351 | UniqueFunctionBase<R, P...>::CallbacksHolder< |
352 | CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{ |
353 | &CallImpl<CalledAsT>}; |
354 | |
355 | } |
356 | |
357 | template <typename R, typename... P> |
358 | class unique_function<R(P...)> : public detail::UniqueFunctionBase<R, P...> { |
359 | using Base = detail::UniqueFunctionBase<R, P...>; |
360 | |
361 | public: |
362 | unique_function() = default; |
| 4 | | Calling defaulted default constructor for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
| 6 | | Returning from default constructor for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
| 7 | | Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr' | |
|
363 | unique_function(std::nullptr_t) {} |
364 | unique_function(unique_function &&) = default; |
365 | unique_function(const unique_function &) = delete; |
366 | unique_function &operator=(unique_function &&) = default; |
| 11 | | Calling move assignment operator for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
| 19 | | Returning from move assignment operator for 'UniqueFunctionBase<llvm::Error, unsigned long long>' | |
|
367 | unique_function &operator=(const unique_function &) = delete; |
368 | |
369 | template <typename CallableT> |
370 | unique_function( |
371 | CallableT Callable, |
372 | detail::EnableUnlessSameType<CallableT, unique_function> * = nullptr, |
373 | detail::EnableIfCallable<CallableT, R, P...> * = nullptr) |
374 | : Base(std::forward<CallableT>(Callable), |
375 | typename Base::template CalledAs<CallableT>{}) {} |
376 | |
377 | R operator()(P... Params) { |
378 | return this->getCallPtr()(this->getCalleePtr(), Params...); |
| 23 | | Calling 'UniqueFunctionBase::getCalleePtr' | |
|
379 | } |
380 | }; |
381 | |
382 | template <typename R, typename... P> |
383 | class unique_function<R(P...) const> |
384 | : public detail::UniqueFunctionBase<R, P...> { |
385 | using Base = detail::UniqueFunctionBase<R, P...>; |
386 | |
387 | public: |
388 | unique_function() = default; |
389 | unique_function(std::nullptr_t) {} |
390 | unique_function(unique_function &&) = default; |
391 | unique_function(const unique_function &) = delete; |
392 | unique_function &operator=(unique_function &&) = default; |
393 | unique_function &operator=(const unique_function &) = delete; |
394 | |
395 | template <typename CallableT> |
396 | unique_function( |
397 | CallableT Callable, |
398 | detail::EnableUnlessSameType<CallableT, unique_function> * = nullptr, |
399 | detail::EnableIfCallable<const CallableT, R, P...> * = nullptr) |
400 | : Base(std::forward<CallableT>(Callable), |
401 | typename Base::template CalledAs<const CallableT>{}) {} |
402 | |
403 | R operator()(P... Params) const { |
404 | return this->getCallPtr()(this->getCalleePtr(), Params...); |
405 | } |
406 | }; |
407 | |
408 | } |
409 | |
410 | #endif // LLVM_ADT_FUNCTIONEXTRAS_H |