Bug Summary

File:llvm/include/llvm/ADT/FunctionExtras.h
Warning:line 177, column 5
Undefined or garbage value returned to caller

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/build-llvm/lib/ExecutionEngine/Orc -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-08-06-171148-17323-1 -x c++ /build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp

/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp

1//===---------- LazyReexports.cpp - Utilities for lazy reexports ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "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" "orc"
15
16namespace llvm {
17namespace orc {
18
19LazyCallThroughManager::LazyCallThroughManager(
20 ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr, TrampolinePool *TP)
21 : ES(ES), ErrorHandlerAddr(ErrorHandlerAddr), TP(TP) {}
22
23Expected<JITTargetAddress> LazyCallThroughManager::getCallThroughTrampoline(
24 JITDylib &SourceJD, SymbolStringPtr SymbolName,
25 NotifyResolvedFunction NotifyResolved) {
26 assert(TP && "TrampolinePool not set")((TP && "TrampolinePool not set") ? static_cast<void
> (0) : __assert_fail ("TP && \"TrampolinePool not set\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 26, __PRETTY_FUNCTION__))
;
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
39JITTargetAddress LazyCallThroughManager::reportCallThroughError(Error Err) {
40 ES.reportError(std::move(Err));
41 return ErrorHandlerAddr;
42}
43
44Expected<LazyCallThroughManager::ReexportsEntry>
45LazyCallThroughManager::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
55Error LazyCallThroughManager::notifyResolved(JITTargetAddress TrampolineAddr,
56 JITTargetAddress ResolvedAddr) {
57 NotifyResolvedFunction NotifyResolved;
7
Calling defaulted default constructor for 'unique_function<llvm::Error (unsigned long)>'
12
Returning from default constructor for 'unique_function<llvm::Error (unsigned long)>'
58 {
59 std::lock_guard<std::mutex> Lock(LCTMMutex);
60 auto I = Notifiers.find(TrampolineAddr);
61 if (I != Notifiers.end()) {
13
Assuming the condition is true
14
Taking true branch
62 NotifyResolved = std::move(I->second);
15
Calling defaulted move assignment operator for 'unique_function<llvm::Error (unsigned long)>'
25
Returning from move assignment operator for 'unique_function<llvm::Error (unsigned long)>'
63 Notifiers.erase(I);
64 }
65 }
66
67 return NotifyResolved ? NotifyResolved(ResolvedAddr) : Error::success();
26
'?' condition is true
27
Calling 'unique_function::operator()'
68}
69
70void 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 // Declaring SLS and the callback outside of the call to ES.lookup is a
79 // workaround to fix build failures on AIX and on z/OS platforms.
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) {
1
Taking true branch
85 assert(Result->size() == 1 && "Unexpected result size")((Result->size() == 1 && "Unexpected result size")
? static_cast<void> (0) : __assert_fail ("Result->size() == 1 && \"Unexpected result size\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 85, __PRETTY_FUNCTION__))
;
2
Assuming the condition is true
3
'?' condition is true
86 assert(Result->count(SymbolName) && "Unexpected result value")((Result->count(SymbolName) && "Unexpected result value"
) ? static_cast<void> (0) : __assert_fail ("Result->count(SymbolName) && \"Unexpected result value\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 86, __PRETTY_FUNCTION__))
;
4
Assuming the condition is true
5
'?' condition is true
87 JITTargetAddress LandingAddr = (*Result)[SymbolName].getAddress();
88
89 if (auto Err = notifyResolved(TrampolineAddr, LandingAddr))
6
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
105Expected<std::unique_ptr<LazyCallThroughManager>>
106createLocalLazyCallThroughManager(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
144LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit(
145 LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager,
146 JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc,
147 VModuleKey K)
148 : MaterializationUnit(extractFlags(CallableAliases), nullptr, std::move(K)),
149 LCTManager(LCTManager), ISManager(ISManager), SourceJD(SourceJD),
150 CallableAliases(std::move(CallableAliases)), AliaseeTable(SrcJDLoc) {}
151
152StringRef LazyReexportsMaterializationUnit::getName() const {
153 return "<Lazy Reexports>";
154}
155
156void LazyReexportsMaterializationUnit::materialize(
157 MaterializationResponsibility R) {
158 auto RequestedSymbols = R.getRequestedSymbols();
159
160 SymbolAliasMap RequestedAliases;
161 for (auto &RequestedSymbol : RequestedSymbols) {
162 auto I = CallableAliases.find(RequestedSymbol);
163 assert(I != CallableAliases.end() && "Symbol not found in alias map?")((I != CallableAliases.end() && "Symbol not found in alias map?"
) ? static_cast<void> (0) : __assert_fail ("I != CallableAliases.end() && \"Symbol not found in alias map?\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 163, __PRETTY_FUNCTION__))
;
164 RequestedAliases[I->first] = std::move(I->second);
165 CallableAliases.erase(I);
166 }
167
168 if (!CallableAliases.empty())
169 R.replace(lazyReexports(LCTManager, ISManager, SourceJD,
170 std::move(CallableAliases), AliaseeTable));
171
172 IndirectStubsManager::StubInitsMap StubInits;
173 for (auto &Alias : RequestedAliases) {
174
175 auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline(
176 SourceJD, Alias.second.Aliasee,
177 [&ISManager = this->ISManager,
178 StubSym = Alias.first](JITTargetAddress ResolvedAddr) -> Error {
179 return ISManager.updatePointer(*StubSym, ResolvedAddr);
180 });
181
182 if (!CallThroughTrampoline) {
183 SourceJD.getExecutionSession().reportError(
184 CallThroughTrampoline.takeError());
185 R.failMaterialization();
186 return;
187 }
188
189 StubInits[*Alias.first] =
190 std::make_pair(*CallThroughTrampoline, Alias.second.AliasFlags);
191 }
192
193 if (AliaseeTable != nullptr && !RequestedAliases.empty())
194 AliaseeTable->trackImpls(RequestedAliases, &SourceJD);
195
196 if (auto Err = ISManager.createStubs(StubInits)) {
197 SourceJD.getExecutionSession().reportError(std::move(Err));
198 R.failMaterialization();
199 return;
200 }
201
202 SymbolMap Stubs;
203 for (auto &Alias : RequestedAliases)
204 Stubs[Alias.first] = ISManager.findStub(*Alias.first, false);
205
206 // No registered dependencies, so these calls cannot fail.
207 cantFail(R.notifyResolved(Stubs));
208 cantFail(R.notifyEmitted());
209}
210
211void LazyReexportsMaterializationUnit::discard(const JITDylib &JD,
212 const SymbolStringPtr &Name) {
213 assert(CallableAliases.count(Name) &&((CallableAliases.count(Name) && "Symbol not covered by this MaterializationUnit"
) ? static_cast<void> (0) : __assert_fail ("CallableAliases.count(Name) && \"Symbol not covered by this MaterializationUnit\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 214, __PRETTY_FUNCTION__))
214 "Symbol not covered by this MaterializationUnit")((CallableAliases.count(Name) && "Symbol not covered by this MaterializationUnit"
) ? static_cast<void> (0) : __assert_fail ("CallableAliases.count(Name) && \"Symbol not covered by this MaterializationUnit\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 214, __PRETTY_FUNCTION__))
;
215 CallableAliases.erase(Name);
216}
217
218SymbolFlagsMap
219LazyReexportsMaterializationUnit::extractFlags(const SymbolAliasMap &Aliases) {
220 SymbolFlagsMap SymbolFlags;
221 for (auto &KV : Aliases) {
222 assert(KV.second.AliasFlags.isCallable() &&((KV.second.AliasFlags.isCallable() && "Lazy re-exports must be callable symbols"
) ? static_cast<void> (0) : __assert_fail ("KV.second.AliasFlags.isCallable() && \"Lazy re-exports must be callable symbols\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 223, __PRETTY_FUNCTION__))
223 "Lazy re-exports must be callable symbols")((KV.second.AliasFlags.isCallable() && "Lazy re-exports must be callable symbols"
) ? static_cast<void> (0) : __assert_fail ("KV.second.AliasFlags.isCallable() && \"Lazy re-exports must be callable symbols\""
, "/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/lib/ExecutionEngine/Orc/LazyReexports.cpp"
, 223, __PRETTY_FUNCTION__))
;
224 SymbolFlags[KV.first] = KV.second.AliasFlags;
225 }
226 return SymbolFlags;
227}
228
229} // End namespace orc.
230} // End namespace llvm.

/build/llvm-toolchain-snapshot-12~++20200806111125+5446ec85070/llvm/include/llvm/ADT/FunctionExtras.h

1//===- FunctionExtras.h - Function type erasure utilities -------*- 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/// \file
9/// This file provides a collection of function (or more generally, callable)
10/// type erasure utilities supplementing those provided by the standard library
11/// in `<function>`.
12///
13/// It provides `unique_function`, which works like `std::function` but supports
14/// move-only callable objects and const-qualification.
15///
16/// Future plans:
17/// - Add a `function` that provides ref-qualified support, which doesn't work
18/// with `std::function`.
19/// - Provide support for specifying multiple signatures to type erase callable
20/// objects with an overload set, such as those produced by generic lambdas.
21/// - Expand to include a copyable utility that directly replaces std::function
22/// but brings the above improvements.
23///
24/// Note that LLVM's utilities are greatly simplified by not supporting
25/// allocators.
26///
27/// If the standard library ever begins to provide comparable facilities we can
28/// consider switching to those.
29///
30//===----------------------------------------------------------------------===//
31
32#ifndef LLVM_ADT_FUNCTION_EXTRAS_H
33#define LLVM_ADT_FUNCTION_EXTRAS_H
34
35#include "llvm/ADT/PointerIntPair.h"
36#include "llvm/ADT/PointerUnion.h"
37#include "llvm/Support/MemAlloc.h"
38#include "llvm/Support/type_traits.h"
39#include <memory>
40#include <type_traits>
41
42namespace llvm {
43
44/// unique_function is a type-erasing functor similar to std::function.
45///
46/// It can hold move-only function objects, like lambdas capturing unique_ptrs.
47/// Accordingly, it is movable but not copyable.
48///
49/// It supports const-qualification:
50/// - unique_function<int() const> has a const operator().
51/// It can only hold functions which themselves have a const operator().
52/// - unique_function<int()> has a non-const operator().
53/// It can hold functions with a non-const operator(), like mutable lambdas.
54template <typename FunctionT> class unique_function;
55
56namespace detail {
57
58template <typename T>
59using EnableIfTrivial =
60 std::enable_if_t<llvm::is_trivially_move_constructible<T>::value &&
61 std::is_trivially_destructible<T>::value>;
62
63template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
64protected:
65 static constexpr size_t InlineStorageSize = sizeof(void *) * 3;
66
67 // MSVC has a bug and ICEs if we give it a particular dependent value
68 // expression as part of the `std::conditional` below. To work around this,
69 // we build that into a template struct's constexpr bool.
70 template <typename T> struct IsSizeLessThanThresholdT {
71 static constexpr bool value = sizeof(T) <= (2 * sizeof(void *));
72 };
73
74 // Provide a type function to map parameters that won't observe extra copies
75 // or moves and which are small enough to likely pass in register to values
76 // and all other types to l-value reference types. We use this to compute the
77 // types used in our erased call utility to minimize copies and moves unless
78 // doing so would force things unnecessarily into memory.
79 //
80 // The heuristic used is related to common ABI register passing conventions.
81 // It doesn't have to be exact though, and in one way it is more strict
82 // because we want to still be able to observe either moves *or* copies.
83 template <typename T>
84 using AdjustedParamT = typename std::conditional<
85 !std::is_reference<T>::value &&
86 llvm::is_trivially_copy_constructible<T>::value &&
87 llvm::is_trivially_move_constructible<T>::value &&
88 IsSizeLessThanThresholdT<T>::value,
89 T, T &>::type;
90
91 // The type of the erased function pointer we use as a callback to dispatch to
92 // the stored callable when it is trivial to move and destroy.
93 using CallPtrT = ReturnT (*)(void *CallableAddr,
94 AdjustedParamT<ParamTs>... Params);
95 using MovePtrT = void (*)(void *LHSCallableAddr, void *RHSCallableAddr);
96 using DestroyPtrT = void (*)(void *CallableAddr);
97
98 /// A struct to hold a single trivial callback with sufficient alignment for
99 /// our bitpacking.
100 struct alignas(8) TrivialCallback {
101 CallPtrT CallPtr;
102 };
103
104 /// A struct we use to aggregate three callbacks when we need full set of
105 /// operations.
106 struct alignas(8) NonTrivialCallbacks {
107 CallPtrT CallPtr;
108 MovePtrT MovePtr;
109 DestroyPtrT DestroyPtr;
110 };
111
112 // Create a pointer union between either a pointer to a static trivial call
113 // pointer in a struct or a pointer to a static struct of the call, move, and
114 // destroy pointers.
115 using CallbackPointerUnionT =
116 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
117
118 // The main storage buffer. This will either have a pointer to out-of-line
119 // storage or an inline buffer storing the callable.
120 union StorageUnionT {
121 // For out-of-line storage we keep a pointer to the underlying storage and
122 // the size. This is enough to deallocate the memory.
123 struct OutOfLineStorageT {
124 void *StoragePtr;
125 size_t Size;
126 size_t Alignment;
127 } OutOfLineStorage;
128 static_assert(
129 sizeof(OutOfLineStorageT) <= InlineStorageSize,
130 "Should always use all of the out-of-line storage for inline storage!");
131
132 // For in-line storage, we just provide an aligned character buffer. We
133 // provide three pointers worth of storage here.
134 // This is mutable as an inlined `const unique_function<void() const>` may
135 // still modify its own mutable members.
136 mutable
137 typename std::aligned_storage<InlineStorageSize, alignof(void *)>::type
138 InlineStorage;
139 } StorageUnion;
140
141 // A compressed pointer to either our dispatching callback or our table of
142 // dispatching callbacks and the flag for whether the callable itself is
143 // stored inline or not.
144 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
145
146 bool isInlineStorage() const { return CallbackAndInlineFlag.getInt(); }
147
148 bool isTrivialCallback() const {
149 return CallbackAndInlineFlag.getPointer().template is<TrivialCallback *>();
150 }
151
152 CallPtrT getTrivialCallback() const {
153 return CallbackAndInlineFlag.getPointer().template get<TrivialCallback *>()->CallPtr;
154 }
155
156 NonTrivialCallbacks *getNonTrivialCallbacks() const {
157 return CallbackAndInlineFlag.getPointer()
158 .template get<NonTrivialCallbacks *>();
159 }
160
161 CallPtrT getCallPtr() const {
162 return isTrivialCallback() ? getTrivialCallback()
163 : getNonTrivialCallbacks()->CallPtr;
164 }
165
166 // These three functions are only const in the narrow sense. They return
167 // mutable pointers to function state.
168 // This allows unique_function<T const>::operator() to be const, even if the
169 // underlying functor may be internally mutable.
170 //
171 // const callers must ensure they're only used in const-correct ways.
172 void *getCalleePtr() const {
173 return isInlineStorage() ? getInlineStorage() : getOutOfLineStorage();
29
Assuming the condition is false
30
'?' condition is false
31
Calling 'UniqueFunctionBase::getOutOfLineStorage'
174 }
175 void *getInlineStorage() const { return &StorageUnion.InlineStorage; }
176 void *getOutOfLineStorage() const {
177 return StorageUnion.OutOfLineStorage.StoragePtr;
32
Undefined or garbage value returned to caller
178 }
179
180 size_t getOutOfLineStorageSize() const {
181 return StorageUnion.OutOfLineStorage.Size;
182 }
183 size_t getOutOfLineStorageAlignment() const {
184 return StorageUnion.OutOfLineStorage.Alignment;
185 }
186
187 void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment) {
188 StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment};
189 }
190
191 template <typename CalledAsT>
192 static ReturnT CallImpl(void *CallableAddr,
193 AdjustedParamT<ParamTs>... Params) {
194 auto &Func = *reinterpret_cast<CalledAsT *>(CallableAddr);
195 return Func(std::forward<ParamTs>(Params)...);
196 }
197
198 template <typename CallableT>
199 static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept {
200 new (LHSCallableAddr)
201 CallableT(std::move(*reinterpret_cast<CallableT *>(RHSCallableAddr)));
202 }
203
204 template <typename CallableT>
205 static void DestroyImpl(void *CallableAddr) noexcept {
206 reinterpret_cast<CallableT *>(CallableAddr)->~CallableT();
207 }
208
209 // The pointers to call/move/destroy functions are determined for each
210 // callable type (and called-as type, which determines the overload chosen).
211 // (definitions are out-of-line).
212
213 // By default, we need an object that contains all the different
214 // type erased behaviors needed. Create a static instance of the struct type
215 // here and each instance will contain a pointer to it.
216 // Wrap in a struct to avoid https://gcc.gnu.org/PR71954
217 template <typename CallableT, typename CalledAs, typename Enable = void>
218 struct CallbacksHolder {
219 static NonTrivialCallbacks Callbacks;
220 };
221 // See if we can create a trivial callback. We need the callable to be
222 // trivially moved and trivially destroyed so that we don't have to store
223 // type erased callbacks for those operations.
224 template <typename CallableT, typename CalledAs>
225 struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
226 static TrivialCallback Callbacks;
227 };
228
229 // A simple tag type so the call-as type to be passed to the constructor.
230 template <typename T> struct CalledAs {};
231
232 // Essentially the "main" unique_function constructor, but subclasses
233 // provide the qualified type to be used for the call.
234 // (We always store a T, even if the call will use a pointer to const T).
235 template <typename CallableT, typename CalledAsT>
236 UniqueFunctionBase(CallableT Callable, CalledAs<CalledAsT>) {
237 bool IsInlineStorage = true;
238 void *CallableAddr = getInlineStorage();
239 if (sizeof(CallableT) > InlineStorageSize ||
240 alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) {
241 IsInlineStorage = false;
242 // Allocate out-of-line storage. FIXME: Use an explicit alignment
243 // parameter in C++17 mode.
244 auto Size = sizeof(CallableT);
245 auto Alignment = alignof(CallableT);
246 CallableAddr = allocate_buffer(Size, Alignment);
247 setOutOfLineStorage(CallableAddr, Size, Alignment);
248 }
249
250 // Now move into the storage.
251 new (CallableAddr) CallableT(std::move(Callable));
252 CallbackAndInlineFlag.setPointerAndInt(
253 &CallbacksHolder<CallableT, CalledAsT>::Callbacks, IsInlineStorage);
254 }
255
256 ~UniqueFunctionBase() {
257 if (!CallbackAndInlineFlag.getPointer())
258 return;
259
260 // Cache this value so we don't re-check it after type-erased operations.
261 bool IsInlineStorage = isInlineStorage();
262
263 if (!isTrivialCallback())
264 getNonTrivialCallbacks()->DestroyPtr(
265 IsInlineStorage ? getInlineStorage() : getOutOfLineStorage());
266
267 if (!IsInlineStorage)
268 deallocate_buffer(getOutOfLineStorage(), getOutOfLineStorageSize(),
269 getOutOfLineStorageAlignment());
270 }
271
272 UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept {
273 // Copy the callback and inline flag.
274 CallbackAndInlineFlag = RHS.CallbackAndInlineFlag;
275
276 // If the RHS is empty, just copying the above is sufficient.
277 if (!RHS)
21
Taking true branch
278 return;
22
Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr'
279
280 if (!isInlineStorage()) {
281 // The out-of-line case is easiest to move.
282 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
283 } else if (isTrivialCallback()) {
284 // Move is trivial, just memcpy the bytes across.
285 memcpy(getInlineStorage(), RHS.getInlineStorage(), InlineStorageSize);
286 } else {
287 // Non-trivial move, so dispatch to a type-erased implementation.
288 getNonTrivialCallbacks()->MovePtr(getInlineStorage(),
289 RHS.getInlineStorage());
290 }
291
292 // Clear the old callback and inline flag to get back to as-if-null.
293 RHS.CallbackAndInlineFlag = {};
294
295#ifndef NDEBUG
296 // In debug builds, we also scribble across the rest of the storage.
297 memset(RHS.getInlineStorage(), 0xAD, InlineStorageSize);
298#endif
299 }
300
301 UniqueFunctionBase &operator=(UniqueFunctionBase &&RHS) noexcept {
302 if (this == &RHS)
17
Taking false branch
303 return *this;
304
305 // Because we don't try to provide any exception safety guarantees we can
306 // implement move assignment very simply by first destroying the current
307 // object and then move-constructing over top of it.
308 this->~UniqueFunctionBase();
309 new (this) UniqueFunctionBase(std::move(RHS));
18
Calling 'operator new'
19
Returning from 'operator new'
20
Calling move constructor for 'UniqueFunctionBase<llvm::Error, unsigned long>'
23
Returning from move constructor for 'UniqueFunctionBase<llvm::Error, unsigned long>'
310 return *this;
311 }
312
313 UniqueFunctionBase() = default;
9
Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr'
314
315public:
316 explicit operator bool() const {
317 return (bool)CallbackAndInlineFlag.getPointer();
318 }
319};
320
321template <typename R, typename... P>
322template <typename CallableT, typename CalledAsT, typename Enable>
323typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
324 R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
325 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
326
327template <typename R, typename... P>
328template <typename CallableT, typename CalledAsT>
329typename UniqueFunctionBase<R, P...>::TrivialCallback
330 UniqueFunctionBase<R, P...>::CallbacksHolder<
331 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
332 &CallImpl<CalledAsT>};
333
334} // namespace detail
335
336template <typename R, typename... P>
337class unique_function<R(P...)> : public detail::UniqueFunctionBase<R, P...> {
338 using Base = detail::UniqueFunctionBase<R, P...>;
339
340public:
341 unique_function() = default;
8
Calling defaulted default constructor for 'UniqueFunctionBase<llvm::Error, unsigned long>'
10
Returning from default constructor for 'UniqueFunctionBase<llvm::Error, unsigned long>'
11
Returning without writing to 'this->StorageUnion.OutOfLineStorage.StoragePtr'
342 unique_function(std::nullptr_t) {}
343 unique_function(unique_function &&) = default;
344 unique_function(const unique_function &) = delete;
345 unique_function &operator=(unique_function &&) = default;
16
Calling move assignment operator for 'UniqueFunctionBase<llvm::Error, unsigned long>'
24
Returning from move assignment operator for 'UniqueFunctionBase<llvm::Error, unsigned long>'
346 unique_function &operator=(const unique_function &) = delete;
347
348 template <typename CallableT>
349 unique_function(CallableT Callable)
350 : Base(std::forward<CallableT>(Callable),
351 typename Base::template CalledAs<CallableT>{}) {}
352
353 R operator()(P... Params) {
354 return this->getCallPtr()(this->getCalleePtr(), Params...);
28
Calling 'UniqueFunctionBase::getCalleePtr'
355 }
356};
357
358template <typename R, typename... P>
359class unique_function<R(P...) const>
360 : public detail::UniqueFunctionBase<R, P...> {
361 using Base = detail::UniqueFunctionBase<R, P...>;
362
363public:
364 unique_function() = default;
365 unique_function(std::nullptr_t) {}
366 unique_function(unique_function &&) = default;
367 unique_function(const unique_function &) = delete;
368 unique_function &operator=(unique_function &&) = default;
369 unique_function &operator=(const unique_function &) = delete;
370
371 template <typename CallableT>
372 unique_function(CallableT Callable)
373 : Base(std::forward<CallableT>(Callable),
374 typename Base::template CalledAs<const CallableT>{}) {}
375
376 R operator()(P... Params) const {
377 return this->getCallPtr()(this->getCalleePtr(), Params...);
378 }
379};
380
381} // end namespace llvm
382
383#endif // LLVM_ADT_FUNCTION_H