Bug Summary

File:llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp
Warning:line 142, 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 -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/lib/ExecutionEngine/Orc -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -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-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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
22int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args,
23 Optional<StringRef> ProgramName) {
24 std::vector<std::unique_ptr<char[]>> ArgVStorage;
25 std::vector<char *> ArgV;
26
27 ArgVStorage.reserve(Args.size() + (ProgramName ? 1 : 0));
28 ArgV.reserve(Args.size() + 1 + (ProgramName ? 1 : 0));
29
30 if (ProgramName) {
31 ArgVStorage.push_back(std::make_unique<char[]>(ProgramName->size() + 1));
32 llvm::copy(*ProgramName, &ArgVStorage.back()[0]);
33 ArgVStorage.back()[ProgramName->size()] = '\0';
34 ArgV.push_back(ArgVStorage.back().get());
35 }
36
37 for (auto &Arg : Args) {
38 ArgVStorage.push_back(std::make_unique<char[]>(Arg.size() + 1));
39 llvm::copy(Arg, &ArgVStorage.back()[0]);
40 ArgVStorage.back()[Arg.size()] = '\0';
41 ArgV.push_back(ArgVStorage.back().get());
42 }
43 ArgV.push_back(nullptr);
44
45 return Main(Args.size() + !!ProgramName, ArgV.data());
46}
47
48CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
49 : InitList(
50 GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
51 I((InitList && End) ? InitList->getNumOperands() : 0) {
52}
53
54bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const {
55 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-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 55, __PRETTY_FUNCTION__))
;
56 return I == Other.I;
57}
58
59bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const {
60 return !(*this == Other);
61}
62
63CtorDtorIterator& CtorDtorIterator::operator++() {
64 ++I;
65 return *this;
66}
67
68CtorDtorIterator CtorDtorIterator::operator++(int) {
69 CtorDtorIterator Temp = *this;
70 ++I;
71 return Temp;
72}
73
74CtorDtorIterator::Element CtorDtorIterator::operator*() const {
75 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
76 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-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 76, __PRETTY_FUNCTION__))
;
77
78 Constant *FuncC = CS->getOperand(1);
79 Function *Func = nullptr;
80
81 // Extract function pointer, pulling off any casts.
82 while (FuncC) {
83 if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
84 Func = F;
85 break;
86 } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
87 if (CE->isCast())
88 FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
89 else
90 break;
91 } else {
92 // This isn't anything we recognize. Bail out with Func left set to null.
93 break;
94 }
95 }
96
97 auto *Priority = cast<ConstantInt>(CS->getOperand(0));
98 Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
99 if (Data && !isa<GlobalValue>(Data))
100 Data = nullptr;
101 return Element(Priority->getZExtValue(), Func, Data);
102}
103
104iterator_range<CtorDtorIterator> getConstructors(const Module &M) {
105 const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
106 return make_range(CtorDtorIterator(CtorsList, false),
107 CtorDtorIterator(CtorsList, true));
108}
109
110iterator_range<CtorDtorIterator> getDestructors(const Module &M) {
111 const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
112 return make_range(CtorDtorIterator(DtorsList, false),
113 CtorDtorIterator(DtorsList, true));
114}
115
116bool StaticInitGVIterator::isStaticInitGlobal(GlobalValue &GV) {
117 if (GV.isDeclaration())
118 return false;
119
120 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
121 GV.getName() == "llvm.global_dtors"))
122 return true;
123
124 if (ObjFmt == Triple::MachO) {
125 // FIXME: These section checks are too strict: We should match first and
126 // second word split by comma.
127 if (GV.hasSection() &&
128 (GV.getSection().startswith("__DATA,__objc_classlist") ||
129 GV.getSection().startswith("__DATA,__objc_selrefs")))
130 return true;
131 }
132
133 return false;
134}
135
136void CtorDtorRunner::add(iterator_range<CtorDtorIterator> CtorDtors) {
137 if (CtorDtors.empty())
1
Assuming the condition is false
2
Taking false branch
138 return;
139
140 MangleAndInterner Mangle(
141 JD.getExecutionSession(),
142 (*CtorDtors.begin()).Func->getParent()->getDataLayout());
3
Null pointer value stored to field 'Func'
4
Called C++ object pointer is null
143
144 for (auto CtorDtor : CtorDtors) {
145 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-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 146, __PRETTY_FUNCTION__))
146 "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-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 146, __PRETTY_FUNCTION__))
;
147
148 // FIXME: Maybe use a symbol promoter here instead.
149 if (CtorDtor.Func->hasLocalLinkage()) {
150 CtorDtor.Func->setLinkage(GlobalValue::ExternalLinkage);
151 CtorDtor.Func->setVisibility(GlobalValue::HiddenVisibility);
152 }
153
154 if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
155 dbgs() << " Skipping because why now?\n";
156 continue;
157 }
158
159 CtorDtorsByPriority[CtorDtor.Priority].push_back(
160 Mangle(CtorDtor.Func->getName()));
161 }
162}
163
164Error CtorDtorRunner::run() {
165 using CtorDtorTy = void (*)();
166
167 SymbolLookupSet LookupSet;
168 for (auto &KV : CtorDtorsByPriority)
169 for (auto &Name : KV.second)
170 LookupSet.add(Name);
171 assert(!LookupSet.containsDuplicates() &&((!LookupSet.containsDuplicates() && "Ctor/Dtor list contains duplicates"
) ? static_cast<void> (0) : __assert_fail ("!LookupSet.containsDuplicates() && \"Ctor/Dtor list contains duplicates\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 172, __PRETTY_FUNCTION__))
172 "Ctor/Dtor list contains duplicates")((!LookupSet.containsDuplicates() && "Ctor/Dtor list contains duplicates"
) ? static_cast<void> (0) : __assert_fail ("!LookupSet.containsDuplicates() && \"Ctor/Dtor list contains duplicates\""
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 172, __PRETTY_FUNCTION__))
;
173
174 auto &ES = JD.getExecutionSession();
175 if (auto CtorDtorMap = ES.lookup(
176 makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
177 std::move(LookupSet))) {
178 for (auto &KV : CtorDtorsByPriority) {
179 for (auto &Name : KV.second) {
180 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-11~++20200309111110+2c36c23f347/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 180, __PRETTY_FUNCTION__))
;
181 auto CtorDtor = reinterpret_cast<CtorDtorTy>(
182 static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
183 CtorDtor();
184 }
185 }
186 CtorDtorsByPriority.clear();
187 return Error::success();
188 } else
189 return CtorDtorMap.takeError();
190}
191
192void LocalCXXRuntimeOverridesBase::runDestructors() {
193 auto& CXXDestructorDataPairs = DSOHandleOverride;
194 for (auto &P : CXXDestructorDataPairs)
195 P.first(P.second);
196 CXXDestructorDataPairs.clear();
197}
198
199int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
200 void *Arg,
201 void *DSOHandle) {
202 auto& CXXDestructorDataPairs =
203 *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
204 CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
205 return 0;
206}
207
208Error LocalCXXRuntimeOverrides::enable(JITDylib &JD,
209 MangleAndInterner &Mangle) {
210 SymbolMap RuntimeInterposes;
211 RuntimeInterposes[Mangle("__dso_handle")] =
212 JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
213 JITSymbolFlags::Exported);
214 RuntimeInterposes[Mangle("__cxa_atexit")] =
215 JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
216 JITSymbolFlags::Exported);
217
218 return JD.define(absoluteSymbols(std::move(RuntimeInterposes)));
219}
220
221void ItaniumCXAAtExitSupport::registerAtExit(void (*F)(void *), void *Ctx,
222 void *DSOHandle) {
223 std::lock_guard<std::mutex> Lock(AtExitsMutex);
224 AtExitRecords[DSOHandle].push_back({F, Ctx});
225}
226
227void ItaniumCXAAtExitSupport::runAtExits(void *DSOHandle) {
228 std::vector<AtExitRecord> AtExitsToRun;
229
230 {
231 std::lock_guard<std::mutex> Lock(AtExitsMutex);
232 auto I = AtExitRecords.find(DSOHandle);
233 if (I != AtExitRecords.end()) {
234 AtExitsToRun = std::move(I->second);
235 AtExitRecords.erase(I);
236 }
237 }
238
239 while (!AtExitsToRun.empty()) {
240 AtExitsToRun.back().F(AtExitsToRun.back().Ctx);
241 AtExitsToRun.pop_back();
242 }
243}
244
245DynamicLibrarySearchGenerator::DynamicLibrarySearchGenerator(
246 sys::DynamicLibrary Dylib, char GlobalPrefix, SymbolPredicate Allow)
247 : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
248 GlobalPrefix(GlobalPrefix) {}
249
250Expected<std::unique_ptr<DynamicLibrarySearchGenerator>>
251DynamicLibrarySearchGenerator::Load(const char *FileName, char GlobalPrefix,
252 SymbolPredicate Allow) {
253 std::string ErrMsg;
254 auto Lib = sys::DynamicLibrary::getPermanentLibrary(FileName, &ErrMsg);
255 if (!Lib.isValid())
256 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
257 return std::make_unique<DynamicLibrarySearchGenerator>(
258 std::move(Lib), GlobalPrefix, std::move(Allow));
259}
260
261Error DynamicLibrarySearchGenerator::tryToGenerate(
262 LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
263 const SymbolLookupSet &Symbols) {
264 orc::SymbolMap NewSymbols;
265
266 bool HasGlobalPrefix = (GlobalPrefix != '\0');
267
268 for (auto &KV : Symbols) {
269 auto &Name = KV.first;
270
271 if ((*Name).empty())
272 continue;
273
274 if (Allow && !Allow(Name))
275 continue;
276
277 if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
278 continue;
279
280 std::string Tmp((*Name).data() + HasGlobalPrefix,
281 (*Name).size() - HasGlobalPrefix);
282 if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
283 NewSymbols[Name] = JITEvaluatedSymbol(
284 static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
285 JITSymbolFlags::Exported);
286 }
287 }
288
289 if (NewSymbols.empty())
290 return Error::success();
291
292 return JD.define(absoluteSymbols(std::move(NewSymbols)));
293}
294
295Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
296StaticLibraryDefinitionGenerator::Load(ObjectLayer &L, const char *FileName) {
297 auto ArchiveBuffer = errorOrToExpected(MemoryBuffer::getFile(FileName));
298
299 if (!ArchiveBuffer)
300 return ArchiveBuffer.takeError();
301
302 return Create(L, std::move(*ArchiveBuffer));
303}
304
305Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>>
306StaticLibraryDefinitionGenerator::Create(
307 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer) {
308 Error Err = Error::success();
309
310 std::unique_ptr<StaticLibraryDefinitionGenerator> ADG(
311 new StaticLibraryDefinitionGenerator(L, std::move(ArchiveBuffer), Err));
312
313 if (Err)
314 return std::move(Err);
315
316 return std::move(ADG);
317}
318
319Error StaticLibraryDefinitionGenerator::tryToGenerate(
320 LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags,
321 const SymbolLookupSet &Symbols) {
322
323 // Don't materialize symbols from static archives unless this is a static
324 // lookup.
325 if (K != LookupKind::Static)
326 return Error::success();
327
328 // Bail out early if we've already freed the archive.
329 if (!Archive)
330 return Error::success();
331
332 DenseSet<std::pair<StringRef, StringRef>> ChildBufferInfos;
333
334 for (const auto &KV : Symbols) {
335 const auto &Name = KV.first;
336 auto Child = Archive->findSym(*Name);
337 if (!Child)
338 return Child.takeError();
339 if (*Child == None)
340 continue;
341 auto ChildBuffer = (*Child)->getMemoryBufferRef();
342 if (!ChildBuffer)
343 return ChildBuffer.takeError();
344 ChildBufferInfos.insert(
345 {ChildBuffer->getBuffer(), ChildBuffer->getBufferIdentifier()});
346 }
347
348 for (auto ChildBufferInfo : ChildBufferInfos) {
349 MemoryBufferRef ChildBufferRef(ChildBufferInfo.first,
350 ChildBufferInfo.second);
351
352 if (auto Err =
353 L.add(JD, MemoryBuffer::getMemBuffer(ChildBufferRef), VModuleKey()))
354 return Err;
355 }
356
357 return Error::success();
358}
359
360StaticLibraryDefinitionGenerator::StaticLibraryDefinitionGenerator(
361 ObjectLayer &L, std::unique_ptr<MemoryBuffer> ArchiveBuffer, Error &Err)
362 : L(L), ArchiveBuffer(std::move(ArchiveBuffer)),
363 Archive(std::make_unique<object::Archive>(*this->ArchiveBuffer, Err)) {}
364
365} // End namespace orc.
366} // End namespace llvm.