Bug Summary

File:llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
Warning:line 96, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ExecutionUtils.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 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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-10/lib/clang/10.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-10~+201911111502510600c19528f1809/build-llvm/lib/ExecutionEngine/Orc -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-12-11-181444-25759-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
1//===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
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/ExecutionUtils.h"
10
11#include "llvm/ExecutionEngine/Orc/Layer.h"
12#include "llvm/IR/Constants.h"
13#include "llvm/IR/Function.h"
14#include "llvm/IR/GlobalVariable.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/TargetRegistry.h"
17#include "llvm/Target/TargetMachine.h"
18
19namespace llvm {
20namespace orc {
21
22CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
23 : InitList(
24 GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
25 I((InitList && End) ? InitList->getNumOperands() : 0) {
26}
27
28bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const {
29 assert(InitList == Other.InitList && "Incomparable iterators.")((InitList == Other.InitList && "Incomparable iterators."
) ? static_cast<void> (0) : __assert_fail ("InitList == Other.InitList && \"Incomparable iterators.\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 29, __PRETTY_FUNCTION__))
;
30 return I == Other.I;
31}
32
33bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const {
34 return !(*this == Other);
35}
36
37CtorDtorIterator& CtorDtorIterator::operator++() {
38 ++I;
39 return *this;
40}
41
42CtorDtorIterator CtorDtorIterator::operator++(int) {
43 CtorDtorIterator Temp = *this;
44 ++I;
45 return Temp;
46}
47
48CtorDtorIterator::Element CtorDtorIterator::operator*() const {
49 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
50 assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors")((CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors"
) ? static_cast<void> (0) : __assert_fail ("CS && \"Unrecognized type in llvm.global_ctors/llvm.global_dtors\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 50, __PRETTY_FUNCTION__))
;
51
52 Constant *FuncC = CS->getOperand(1);
53 Function *Func = nullptr;
54
55 // Extract function pointer, pulling off any casts.
56 while (FuncC) {
57 if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
58 Func = F;
59 break;
60 } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
61 if (CE->isCast())
62 FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
63 else
64 break;
65 } else {
66 // This isn't anything we recognize. Bail out with Func left set to null.
67 break;
68 }
69 }
70
71 auto *Priority = cast<ConstantInt>(CS->getOperand(0));
72 Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
73 if (Data && !isa<GlobalValue>(Data))
74 Data = nullptr;
75 return Element(Priority->getZExtValue(), Func, Data);
76}
77
78iterator_range<CtorDtorIterator> getConstructors(const Module &M) {
79 const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
80 return make_range(CtorDtorIterator(CtorsList, false),
81 CtorDtorIterator(CtorsList, true));
82}
83
84iterator_range<CtorDtorIterator> getDestructors(const Module &M) {
85 const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
86 return make_range(CtorDtorIterator(DtorsList, false),
87 CtorDtorIterator(DtorsList, true));
88}
89
90void CtorDtorRunner::add(iterator_range<CtorDtorIterator> CtorDtors) {
91 if (CtorDtors.empty())
1
Assuming the condition is false
2
Taking false branch
92 return;
93
94 MangleAndInterner Mangle(
95 JD.getExecutionSession(),
96 (*CtorDtors.begin()).Func->getParent()->getDataLayout());
3
Null pointer value stored to field 'Func'
4
Called C++ object pointer is null
97
98 for (const auto &CtorDtor : CtorDtors) {
99 assert(CtorDtor.Func && CtorDtor.Func->hasName() &&((CtorDtor.Func && CtorDtor.Func->hasName() &&
"Ctor/Dtor function must be named to be runnable under the JIT"
) ? static_cast<void> (0) : __assert_fail ("CtorDtor.Func && CtorDtor.Func->hasName() && \"Ctor/Dtor function must be named to be runnable under the JIT\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 100, __PRETTY_FUNCTION__))
100 "Ctor/Dtor function must be named to be runnable under the JIT")((CtorDtor.Func && CtorDtor.Func->hasName() &&
"Ctor/Dtor function must be named to be runnable under the JIT"
) ? static_cast<void> (0) : __assert_fail ("CtorDtor.Func && CtorDtor.Func->hasName() && \"Ctor/Dtor function must be named to be runnable under the JIT\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 100, __PRETTY_FUNCTION__))
;
101
102 // FIXME: Maybe use a symbol promoter here instead.
103 if (CtorDtor.Func->hasLocalLinkage()) {
104 CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage);
105 CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility);
106 }
107
108 if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
109 dbgs() << " Skipping because why now?\n";
110 continue;
111 }
112
113 CtorDtorsByPriority[CtorDtor.Priority].push_back(
114 Mangle(CtorDtor.Func->getName()));
115 }
116}
117
118Error CtorDtorRunner::run() {
119 using CtorDtorTy = void (*)();
120
121 SymbolNameSet Names;
122
123 for (auto &KV : CtorDtorsByPriority) {
124 for (auto &Name : KV.second) {
125 auto Added = Names.insert(Name).second;
126 (void)Added;
127 assert(Added && "Ctor/Dtor names clashed")((Added && "Ctor/Dtor names clashed") ? static_cast<
void> (0) : __assert_fail ("Added && \"Ctor/Dtor names clashed\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 127, __PRETTY_FUNCTION__))
;
128 }
129 }
130
131 auto &ES = JD.getExecutionSession();
132 if (auto CtorDtorMap =
133 ES.lookup(JITDylibSearchList({{&JD, true}}), std::move(Names))) {
134 for (auto &KV : CtorDtorsByPriority) {
135 for (auto &Name : KV.second) {
136 assert(CtorDtorMap->count(Name) && "No entry for Name")((CtorDtorMap->count(Name) && "No entry for Name")
? static_cast<void> (0) : __assert_fail ("CtorDtorMap->count(Name) && \"No entry for Name\""
, "/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 136, __PRETTY_FUNCTION__))
;
137 auto CtorDtor = reinterpret_cast<CtorDtorTy>(
138 static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
139 CtorDtor();
140 }
141 }
142 CtorDtorsByPriority.clear();
143 return Error::success();
144 } else
145 return CtorDtorMap.takeError();
146}
147
148void LocalCXXRuntimeOverridesBase::runDestructors() {
149 auto& CXXDestructorDataPairs = DSOHandleOverride;
150 for (auto &P : CXXDestructorDataPairs)
151 P.first(P.second);
152 CXXDestructorDataPairs.clear();
153}
154
155int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
156 void *Arg,
157 void *DSOHandle) {
158 auto& CXXDestructorDataPairs =
159 *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
160 CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
161 return 0;
162}
163
164Error LocalCXXRuntimeOverrides::enable(JITDylib &JD,
165 MangleAndInterner &Mangle) {
166 SymbolMap RuntimeInterposes;
167 RuntimeInterposes[Mangle("__dso_handle")] =
168 JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
169 JITSymbolFlags::Exported);
170 RuntimeInterposes[Mangle("__cxa_atexit")] =
171 JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
172 JITSymbolFlags::Exported);
173
174 return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
175}
176
177DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
178 sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow)
179 : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
180 GlobalPrefix(GlobalPrefix) {}
181
182Expected<std::unique_ptr<DynamicLibrarySearchGenerator>>
183DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix,
184 SymbolPredicate Allow) {
185 std::string ErrMsg;
186 auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
187 if (!Lib.isValid())
188 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
189 return std::make_unique<DynamicLibrarySearchGenerator>(
190 std::move(Lib), GlobalPrefix, std::move(Allow));
191}
192
193Expected<SymbolNameSet>
194DynamicLibrarySearchGenerator::tryToGenerate(JITDylib &JD,
195 const SymbolNameSet &Names) {
196 orc::SymbolNameSet Added;
197 orc::SymbolMap NewSymbols;
198
199 bool HasGlobalPrefix = (GlobalPrefix != '\0');
200
201 for (auto &Name : Names) {
202 if ((*Name).empty())
203 continue;
204
205 if (Allow && !Allow(Name))
206 continue;
207
208 if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
209 continue;
210
211 std::string Tmp((*Name).data() + HasGlobalPrefix,
212 (*Name).size() - HasGlobalPrefix);
213 if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
214 Added.insert(Name);
215 NewSymbols[Name] = JITEvaluatedSymbol(
216 static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
217 JITSymbolFlags::Exported);
218 }
219 }
220
221 // Add any new symbols to JD. Since the generator is only called for symbols
222 // that are not already defined, this will never trigger a duplicate
223 // definition error, so we can wrap this call in a 'cantFail'.
224 if (!NewSymbols.empty())
225 cantFail(JD.define(absoluteSymbols(std::move(NewSymbols))));
226
227 return Added;
228}
229
230Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
231StaticLibraryDefinitionGenerator::Load(ObjectLayer &L, const char *FileName) {
232 auto ArchiveBuffer = errorOrToExpected(MemoryBuffer::getFile(FileName));
233
234 if (!ArchiveBuffer)
235 return ArchiveBuffer.takeError();
236
237 return Create(L, std::move(*ArchiveBuffer));
238}
239
240Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
241StaticLibraryDefinitionGenerator::Create(
242 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer) {
243 Error Err = Error::success();
244
245 std::unique_ptr<StaticLibraryDefinitionGenerator> ADG(
246 new StaticLibraryDefinitionGenerator(L, std::move(ArchiveBuffer), Err));
247
248 if (Err)
249 return std::move(Err);
250
251 return std::move(ADG);
252}
253
254Expected<SymbolNameSet>
255StaticLibraryDefinitionGenerator::tryToGenerate(JITDylib &JD,
256 const SymbolNameSet &Names) {
257
258 DenseSet<std::pair<StringRef, StringRef>> ChildBufferInfos;
259 SymbolNameSet NewDefs;
260
261 for (const auto &Name : Names) {
262 auto Child = Archive.findSym(*Name);
263 if (!Child)
264 return Child.takeError();
265 if (*Child == None)
266 continue;
267 auto ChildBuffer = (*Child)->getMemoryBufferRef();
268 if (!ChildBuffer)
269 return ChildBuffer.takeError();
270 ChildBufferInfos.insert(
271 {ChildBuffer->getBuffer(), ChildBuffer->getBufferIdentifier()});
272 NewDefs.insert(Name);
273 }
274
275 for (auto ChildBufferInfo : ChildBufferInfos) {
276 MemoryBufferRef ChildBufferRef(ChildBufferInfo.first,
277 ChildBufferInfo.second);
278
279 if (auto Err =
280 L.add(JD, MemoryBuffer::getMemBuffer(ChildBufferRef), VModuleKey()))
281 return std::move(Err);
282
283 --UnrealizedObjects;
284 }
285
286 return NewDefs;
287}
288
289StaticLibraryDefinitionGenerator::StaticLibraryDefinitionGenerator(
290 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer, Error &Err)
291 : L(L), ArchiveBuffer(std::move(ArchiveBuffer)),
292 Archive(*this->ArchiveBuffer, Err) {
293
294 if (Err)
295 return;
296
297 Error Err2 = Error::success();
298 for (auto _ : Archive.children(Err2)) {
299 (void)_;
300 ++UnrealizedObjects;
301 }
302
303 // No need to check this: We will leave it to the caller.
304 Err = std::move(Err2);
305}
306
307} // End namespace orc.
308} // End namespace llvm.