Bug Summary

File:lib/ExecutionEngine/Orc/ExecutionUtils.cpp
Warning:line 135, 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-eagerly-assume -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 -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc -I /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn338205/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/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-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn338205/build-llvm/lib/ExecutionEngine/Orc -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-29-043837-17923-1 -x c++ /build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp -faddrsig
1//===---- ExecutionUtils.cpp - Utilities for executing functions in Orc ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
11
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
22JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
23 : TT(std::move(TT)) {}
24
25Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
26 return JITTargetMachineBuilder(Triple(sys::getProcessTriple()));
27}
28
29Expected<std::unique_ptr<TargetMachine>>
30JITTargetMachineBuilder::createTargetMachine() {
31 if (!Arch.empty()) {
32 Triple::ArchType Type = Triple::getArchTypeForLLVMName(Arch);
33
34 if (Type == Triple::UnknownArch)
35 return make_error<StringError>(std::string("Unknown arch: ") + Arch,
36 inconvertibleErrorCode());
37 }
38
39 std::string ErrMsg;
40 auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
41 if (!TheTarget)
42 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
43
44 auto *TM =
45 TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
46 Options, RM, CM, OptLevel, /*JIT*/ true);
47 if (!TM)
48 return make_error<StringError>("Could not allocate target machine",
49 inconvertibleErrorCode());
50
51 return std::unique_ptr<TargetMachine>(TM);
52}
53
54JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures(
55 const std::vector<std::string> &FeatureVec) {
56 for (const auto &F : FeatureVec)
57 Features.AddFeature(F);
58 return *this;
59}
60
61CtorDtorIterator::CtorDtorIterator(const GlobalVariable *GV, bool End)
62 : InitList(
63 GV ? dyn_cast_or_null<ConstantArray>(GV->getInitializer()) : nullptr),
64 I((InitList && End) ? InitList->getNumOperands() : 0) {
65}
66
67bool CtorDtorIterator::operator==(const CtorDtorIterator &Other) const {
68 assert(InitList == Other.InitList && "Incomparable iterators.")(static_cast <bool> (InitList == Other.InitList &&
"Incomparable iterators.") ? void (0) : __assert_fail ("InitList == Other.InitList && \"Incomparable iterators.\""
, "/build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 68, __extension__ __PRETTY_FUNCTION__))
;
69 return I == Other.I;
70}
71
72bool CtorDtorIterator::operator!=(const CtorDtorIterator &Other) const {
73 return !(*this == Other);
74}
75
76CtorDtorIterator& CtorDtorIterator::operator++() {
77 ++I;
78 return *this;
79}
80
81CtorDtorIterator CtorDtorIterator::operator++(int) {
82 CtorDtorIterator Temp = *this;
83 ++I;
84 return Temp;
85}
86
87CtorDtorIterator::Element CtorDtorIterator::operator*() const {
88 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(I));
89 assert(CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors")(static_cast <bool> (CS && "Unrecognized type in llvm.global_ctors/llvm.global_dtors"
) ? void (0) : __assert_fail ("CS && \"Unrecognized type in llvm.global_ctors/llvm.global_dtors\""
, "/build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 89, __extension__ __PRETTY_FUNCTION__))
;
90
91 Constant *FuncC = CS->getOperand(1);
92 Function *Func = nullptr;
93
94 // Extract function pointer, pulling off any casts.
95 while (FuncC) {
96 if (Function *F = dyn_cast_or_null<Function>(FuncC)) {
97 Func = F;
98 break;
99 } else if (ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(FuncC)) {
100 if (CE->isCast())
101 FuncC = dyn_cast_or_null<ConstantExpr>(CE->getOperand(0));
102 else
103 break;
104 } else {
105 // This isn't anything we recognize. Bail out with Func left set to null.
106 break;
107 }
108 }
109
110 ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
111 Value *Data = CS->getNumOperands() == 3 ? CS->getOperand(2) : nullptr;
112 if (Data && !isa<GlobalValue>(Data))
113 Data = nullptr;
114 return Element(Priority->getZExtValue(), Func, Data);
115}
116
117iterator_range<CtorDtorIterator> getConstructors(const Module &M) {
118 const GlobalVariable *CtorsList = M.getNamedGlobal("llvm.global_ctors");
119 return make_range(CtorDtorIterator(CtorsList, false),
120 CtorDtorIterator(CtorsList, true));
121}
122
123iterator_range<CtorDtorIterator> getDestructors(const Module &M) {
124 const GlobalVariable *DtorsList = M.getNamedGlobal("llvm.global_dtors");
125 return make_range(CtorDtorIterator(DtorsList, false),
126 CtorDtorIterator(DtorsList, true));
127}
128
129void CtorDtorRunner2::add(iterator_range<CtorDtorIterator> CtorDtors) {
130 if (CtorDtors.begin() == CtorDtors.end())
1
Taking false branch
131 return;
132
133 MangleAndInterner Mangle(
134 V.getExecutionSession(),
135 (*CtorDtors.begin()).Func->getParent()->getDataLayout());
2
Null pointer value stored to field 'Func'
3
Called C++ object pointer is null
136
137 for (const auto &CtorDtor : CtorDtors) {
138 assert(CtorDtor.Func && CtorDtor.Func->hasName() &&(static_cast <bool> (CtorDtor.Func && CtorDtor.
Func->hasName() && "Ctor/Dtor function must be named to be runnable under the JIT"
) ? 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-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 139, __extension__ __PRETTY_FUNCTION__))
139 "Ctor/Dtor function must be named to be runnable under the JIT")(static_cast <bool> (CtorDtor.Func && CtorDtor.
Func->hasName() && "Ctor/Dtor function must be named to be runnable under the JIT"
) ? 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-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 139, __extension__ __PRETTY_FUNCTION__))
;
140
141 if (CtorDtor.Data && cast<GlobalValue>(CtorDtor.Data)->isDeclaration()) {
142 dbgs() << " Skipping because why now?\n";
143 continue;
144 }
145
146 CtorDtorsByPriority[CtorDtor.Priority].push_back(
147 Mangle(CtorDtor.Func->getName()));
148 }
149}
150
151Error CtorDtorRunner2::run() {
152 using CtorDtorTy = void (*)();
153
154 SymbolNameSet Names;
155
156 for (auto &KV : CtorDtorsByPriority) {
157 for (auto &Name : KV.second) {
158 auto Added = Names.insert(Name).second;
159 (void)Added;
160 assert(Added && "Ctor/Dtor names clashed")(static_cast <bool> (Added && "Ctor/Dtor names clashed"
) ? void (0) : __assert_fail ("Added && \"Ctor/Dtor names clashed\""
, "/build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 160, __extension__ __PRETTY_FUNCTION__))
;
161 }
162 }
163
164 if (auto CtorDtorMap = lookup({&V}, std::move(Names))) {
165 for (auto &KV : CtorDtorsByPriority) {
166 for (auto &Name : KV.second) {
167 assert(CtorDtorMap->count(Name) && "No entry for Name")(static_cast <bool> (CtorDtorMap->count(Name) &&
"No entry for Name") ? void (0) : __assert_fail ("CtorDtorMap->count(Name) && \"No entry for Name\""
, "/build/llvm-toolchain-snapshot-7~svn338205/lib/ExecutionEngine/Orc/ExecutionUtils.cpp"
, 167, __extension__ __PRETTY_FUNCTION__))
;
168 auto CtorDtor = reinterpret_cast<CtorDtorTy>(
169 static_cast<uintptr_t>((*CtorDtorMap)[Name].getAddress()));
170 CtorDtor();
171 }
172 }
173 return Error::success();
174 } else
175 return CtorDtorMap.takeError();
176
177 CtorDtorsByPriority.clear();
178
179 return Error::success();
180}
181
182void LocalCXXRuntimeOverridesBase::runDestructors() {
183 auto& CXXDestructorDataPairs = DSOHandleOverride;
184 for (auto &P : CXXDestructorDataPairs)
185 P.first(P.second);
186 CXXDestructorDataPairs.clear();
187}
188
189int LocalCXXRuntimeOverridesBase::CXAAtExitOverride(DestructorPtr Destructor,
190 void *Arg,
191 void *DSOHandle) {
192 auto& CXXDestructorDataPairs =
193 *reinterpret_cast<CXXDestructorDataPairList*>(DSOHandle);
194 CXXDestructorDataPairs.push_back(std::make_pair(Destructor, Arg));
195 return 0;
196}
197
198Error LocalCXXRuntimeOverrides2::enable(VSO &V, MangleAndInterner &Mangle) {
199 SymbolMap RuntimeInterposes(
200 {{Mangle("__dso_handle"),
201 JITEvaluatedSymbol(toTargetAddress(&DSOHandleOverride),
202 JITSymbolFlags::Exported)},
203 {Mangle("__cxa_atexit"),
204 JITEvaluatedSymbol(toTargetAddress(&CXAAtExitOverride),
205 JITSymbolFlags::Exported)}});
206
207 return V.define(absoluteSymbols(std::move(RuntimeInterposes)));
208}
209
210DynamicLibraryFallbackGenerator::DynamicLibraryFallbackGenerator(
211 sys::DynamicLibrary Dylib, const DataLayout &DL, SymbolPredicate Allow)
212 : Dylib(std::move(Dylib)), Allow(std::move(Allow)),
213 GlobalPrefix(DL.getGlobalPrefix()) {}
214
215SymbolNameSet DynamicLibraryFallbackGenerator::
216operator()(VSO &V, const SymbolNameSet &Names) {
217 orc::SymbolNameSet Added;
218 orc::SymbolMap NewSymbols;
219
220 bool HasGlobalPrefix = (GlobalPrefix != '\0');
221
222 for (auto &Name : Names) {
223 if (!Allow(Name) || (*Name).empty())
224 continue;
225
226 if (HasGlobalPrefix && (*Name).front() != GlobalPrefix)
227 continue;
228
229 std::string Tmp((*Name).data() + (HasGlobalPrefix ? 1 : 0), (*Name).size());
230 if (void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str())) {
231 Added.insert(Name);
232 NewSymbols[Name] = JITEvaluatedSymbol(
233 static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Addr)),
234 JITSymbolFlags::Exported);
235 }
236 }
237
238 // Add any new symbols to V. Since the fallback generator is only called for
239 // symbols that are not already defined, this will never trigger a duplicate
240 // definition error, so we can wrap this call in a 'cantFail'.
241 if (!NewSymbols.empty())
242 cantFail(V.define(absoluteSymbols(std::move(NewSymbols))));
243
244 return Added;
245}
246
247} // End namespace orc.
248} // End namespace llvm.