Bug Summary

File:llvm/include/llvm/Object/MachOUniversal.h
Warning:line 60, column 11
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MachOUniversal.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 -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Object -I /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Object -I include -I /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-16-232930-107970-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Object/MachOUniversal.cpp

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Object/MachOUniversal.cpp

1//===- MachOUniversal.cpp - Mach-O universal binary -------------*- 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//
9// This file defines the MachOUniversalBinary class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Object/MachOUniversal.h"
14#include "llvm/Object/Archive.h"
15#include "llvm/Object/IRObjectFile.h"
16#include "llvm/Object/MachO.h"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Support/Casting.h"
19#include "llvm/Support/Host.h"
20#include "llvm/Support/MemoryBuffer.h"
21
22using namespace llvm;
23using namespace object;
24
25static Error
26malformedError(Twine Msg) {
27 std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
28 return make_error<GenericBinaryError>(std::move(StringMsg),
29 object_error::parse_failed);
30}
31
32template<typename T>
33static T getUniversalBinaryStruct(const char *Ptr) {
34 T Res;
35 memcpy(&Res, Ptr, sizeof(T));
36 // Universal binary headers have big-endian byte order.
37 if (sys::IsLittleEndianHost)
38 swapStruct(Res);
39 return Res;
40}
41
42MachOUniversalBinary::ObjectForArch::ObjectForArch(
43 const MachOUniversalBinary *Parent, uint32_t Index)
44 : Parent(Parent), Index(Index) {
45 // The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
46 if (!Parent || Index >= Parent->getNumberOfObjects()) {
47 clear();
48 } else {
49 // Parse object header.
50 StringRef ParentData = Parent->getData();
51 if (Parent->getMagic() == MachO::FAT_MAGIC) {
52 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
53 Index * sizeof(MachO::fat_arch);
54 Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
55 } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
56 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
57 Index * sizeof(MachO::fat_arch_64);
58 Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
59 }
60 }
61}
62
63Expected<std::unique_ptr<MachOObjectFile>>
64MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
65 if (!Parent)
66 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
67 "called when Parent is a nullptr");
68
69 StringRef ParentData = Parent->getData();
70 StringRef ObjectData;
71 uint32_t cputype;
72 if (Parent->getMagic() == MachO::FAT_MAGIC) {
73 ObjectData = ParentData.substr(Header.offset, Header.size);
74 cputype = Header.cputype;
75 } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
76 ObjectData = ParentData.substr(Header64.offset, Header64.size);
77 cputype = Header64.cputype;
78 }
79 StringRef ObjectName = Parent->getFileName();
80 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
81 return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
82}
83
84Expected<std::unique_ptr<IRObjectFile>>
85MachOUniversalBinary::ObjectForArch::getAsIRObject(LLVMContext &Ctx) const {
86 if (!Parent)
87 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsIRObject() "
88 "called when Parent is a nullptr");
89
90 StringRef ParentData = Parent->getData();
91 StringRef ObjectData;
92 if (Parent->getMagic() == MachO::FAT_MAGIC) {
93 ObjectData = ParentData.substr(Header.offset, Header.size);
94 } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
95 ObjectData = ParentData.substr(Header64.offset, Header64.size);
96 }
97 StringRef ObjectName = Parent->getFileName();
98 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
99
100 return IRObjectFile::create(ObjBuffer, Ctx);
101}
102
103Expected<std::unique_ptr<Archive>>
104MachOUniversalBinary::ObjectForArch::getAsArchive() const {
105 if (!Parent)
106 report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
107 "called when Parent is a nullptr");
108
109 StringRef ParentData = Parent->getData();
110 StringRef ObjectData;
111 if (Parent->getMagic() == MachO::FAT_MAGIC)
112 ObjectData = ParentData.substr(Header.offset, Header.size);
113 else // Parent->getMagic() == MachO::FAT_MAGIC_64
114 ObjectData = ParentData.substr(Header64.offset, Header64.size);
115 StringRef ObjectName = Parent->getFileName();
116 MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
117 return Archive::create(ObjBuffer);
118}
119
120void MachOUniversalBinary::anchor() { }
121
122Expected<std::unique_ptr<MachOUniversalBinary>>
123MachOUniversalBinary::create(MemoryBufferRef Source) {
124 Error Err = Error::success();
125 std::unique_ptr<MachOUniversalBinary> Ret(
126 new MachOUniversalBinary(Source, Err));
127 if (Err)
128 return std::move(Err);
129 return std::move(Ret);
130}
131
132MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
133 : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
134 NumberOfObjects(0) {
135 ErrorAsOutParameter ErrAsOutParam(&Err);
136 if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
137 Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
138 "universal file",
139 object_error::invalid_file_type);
140 return;
141 }
142 // Check for magic value and sufficient header size.
143 StringRef Buf = getData();
144 MachO::fat_header H =
145 getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
146 Magic = H.magic;
147 NumberOfObjects = H.nfat_arch;
148 if (NumberOfObjects == 0) {
149 Err = malformedError("contains zero architecture types");
150 return;
151 }
152 uint32_t MinSize = sizeof(MachO::fat_header);
153 if (Magic == MachO::FAT_MAGIC)
154 MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
155 else if (Magic == MachO::FAT_MAGIC_64)
156 MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
157 else {
158 Err = malformedError("bad magic number");
159 return;
160 }
161 if (Buf.size() < MinSize) {
162 Err = malformedError("fat_arch" +
163 Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") +
164 " structs would extend past the end of the file");
165 return;
166 }
167 for (uint32_t i = 0; i < NumberOfObjects; i++) {
168 ObjectForArch A(this, i);
169 uint64_t bigSize = A.getOffset();
170 bigSize += A.getSize();
171 if (bigSize > Buf.size()) {
172 Err = malformedError("offset plus size of cputype (" +
173 Twine(A.getCPUType()) + ") cpusubtype (" +
174 Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
175 ") extends past the end of the file");
176 return;
177 }
178
179 if (A.getAlign() > MaxSectionAlignment) {
180 Err = malformedError("align (2^" + Twine(A.getAlign()) +
181 ") too large for cputype (" + Twine(A.getCPUType()) +
182 ") cpusubtype (" +
183 Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
184 ") (maximum 2^" + Twine(MaxSectionAlignment) + ")");
185 return;
186 }
187 if(A.getOffset() % (1ull << A.getAlign()) != 0){
188 Err = malformedError("offset: " + Twine(A.getOffset()) +
189 " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
190 Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
191 ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
192 return;
193 }
194 if (A.getOffset() < MinSize) {
195 Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
196 "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
197 ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
198 return;
199 }
200 }
201 for (uint32_t i = 0; i < NumberOfObjects; i++) {
202 ObjectForArch A(this, i);
203 for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
204 ObjectForArch B(this, j);
205 if (A.getCPUType() == B.getCPUType() &&
206 (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
207 (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
208 Err = malformedError("contains two of the same architecture (cputype "
209 "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
210 Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
211 return;
212 }
213 if ((A.getOffset() >= B.getOffset() &&
214 A.getOffset() < B.getOffset() + B.getSize()) ||
215 (A.getOffset() + A.getSize() > B.getOffset() &&
216 A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
217 (A.getOffset() <= B.getOffset() &&
218 A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
219 Err = malformedError("cputype (" + Twine(A.getCPUType()) + ") "
220 "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
221 ") at offset " + Twine(A.getOffset()) + " with a size of " +
222 Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
223 ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
224 + ") at offset " + Twine(B.getOffset()) + " with a size of "
225 + Twine(B.getSize()));
226 return;
227 }
228 }
229 }
230 Err = Error::success();
231}
232
233Expected<MachOUniversalBinary::ObjectForArch>
234MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
235 if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
2
Assuming the condition is false
3
Taking false branch
236 return make_error<GenericBinaryError>("Unknown architecture "
237 "named: " +
238 ArchName,
239 object_error::arch_not_found);
240 for (const auto &Obj : objects())
6
Calling 'object_iterator::operator++'
8
Returning from 'object_iterator::operator++'
241 if (Obj.getArchFlagName() == ArchName)
4
Assuming the condition is false
5
Taking false branch
9
Calling 'ObjectForArch::getArchFlagName'
242 return Obj;
243 return make_error<GenericBinaryError>("fat file does not "
244 "contain " +
245 ArchName,
246 object_error::arch_not_found);
247}
248
249Expected<std::unique_ptr<MachOObjectFile>>
250MachOUniversalBinary::getMachOObjectForArch(StringRef ArchName) const {
251 Expected<ObjectForArch> O = getObjectForArch(ArchName);
252 if (!O)
253 return O.takeError();
254 return O->getAsObjectFile();
255}
256
257Expected<std::unique_ptr<IRObjectFile>>
258MachOUniversalBinary::getIRObjectForArch(StringRef ArchName,
259 LLVMContext &Ctx) const {
260 Expected<ObjectForArch> O = getObjectForArch(ArchName);
261 if (!O)
262 return O.takeError();
263 return O->getAsIRObject(Ctx);
264}
265
266Expected<std::unique_ptr<Archive>>
267MachOUniversalBinary::getArchiveForArch(StringRef ArchName) const {
268 Expected<ObjectForArch> O = getObjectForArch(ArchName);
1
Calling 'MachOUniversalBinary::getObjectForArch'
269 if (!O)
270 return O.takeError();
271 return O->getAsArchive();
272}

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/include/llvm/Object/MachOUniversal.h

1//===- MachOUniversal.h - Mach-O universal binaries -------------*- 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//
9// This file declares Mach-O fat/universal binaries.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_MACHOUNIVERSAL_H
14#define LLVM_OBJECT_MACHOUNIVERSAL_H
15
16#include "llvm/ADT/Triple.h"
17#include "llvm/ADT/iterator_range.h"
18#include "llvm/BinaryFormat/MachO.h"
19#include "llvm/Object/Archive.h"
20#include "llvm/Object/Binary.h"
21#include "llvm/Object/MachO.h"
22
23namespace llvm {
24class StringRef;
25class LLVMContext;
26
27namespace object {
28class IRObjectFile;
29
30class MachOUniversalBinary : public Binary {
31 virtual void anchor();
32
33 uint32_t Magic;
34 uint32_t NumberOfObjects;
35public:
36 static constexpr uint32_t MaxSectionAlignment = 15; /* 2**15 or 0x8000 */
37
38 class ObjectForArch {
39 const MachOUniversalBinary *Parent;
40 /// Index of object in the universal binary.
41 uint32_t Index;
42 /// Descriptor of the object.
43 MachO::fat_arch Header;
44 MachO::fat_arch_64 Header64;
45
46 public:
47 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
48
49 void clear() {
50 Parent = nullptr;
51 Index = 0;
52 }
53
54 bool operator==(const ObjectForArch &Other) const {
55 return (Parent == Other.Parent) && (Index == Other.Index);
56 }
57
58 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
59 uint32_t getCPUType() const {
60 if (Parent->getMagic() == MachO::FAT_MAGIC)
11
Called C++ object pointer is null
61 return Header.cputype;
62 else // Parent->getMagic() == MachO::FAT_MAGIC_64
63 return Header64.cputype;
64 }
65 uint32_t getCPUSubType() const {
66 if (Parent->getMagic() == MachO::FAT_MAGIC)
67 return Header.cpusubtype;
68 else // Parent->getMagic() == MachO::FAT_MAGIC_64
69 return Header64.cpusubtype;
70 }
71 uint64_t getOffset() const {
72 if (Parent->getMagic() == MachO::FAT_MAGIC)
73 return Header.offset;
74 else // Parent->getMagic() == MachO::FAT_MAGIC_64
75 return Header64.offset;
76 }
77 uint64_t getSize() const {
78 if (Parent->getMagic() == MachO::FAT_MAGIC)
79 return Header.size;
80 else // Parent->getMagic() == MachO::FAT_MAGIC_64
81 return Header64.size;
82 }
83 uint32_t getAlign() const {
84 if (Parent->getMagic() == MachO::FAT_MAGIC)
85 return Header.align;
86 else // Parent->getMagic() == MachO::FAT_MAGIC_64
87 return Header64.align;
88 }
89 uint32_t getReserved() const {
90 if (Parent->getMagic() == MachO::FAT_MAGIC)
91 return 0;
92 else // Parent->getMagic() == MachO::FAT_MAGIC_64
93 return Header64.reserved;
94 }
95 Triple getTriple() const {
96 return MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType());
97 }
98 std::string getArchFlagName() const {
99 const char *McpuDefault, *ArchFlag;
100 MachOObjectFile::getArchTriple(getCPUType(), getCPUSubType(),
10
Calling 'ObjectForArch::getCPUType'
101 &McpuDefault, &ArchFlag);
102 return ArchFlag ? ArchFlag : std::string();
103 }
104
105 Expected<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;
106 Expected<std::unique_ptr<IRObjectFile>>
107 getAsIRObject(LLVMContext &Ctx) const;
108
109 Expected<std::unique_ptr<Archive>> getAsArchive() const;
110 };
111
112 class object_iterator {
113 ObjectForArch Obj;
114 public:
115 object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
116 const ObjectForArch *operator->() const { return &Obj; }
117 const ObjectForArch &operator*() const { return Obj; }
118
119 bool operator==(const object_iterator &Other) const {
120 return Obj == Other.Obj;
121 }
122 bool operator!=(const object_iterator &Other) const {
123 return !(*this == Other);
124 }
125
126 object_iterator& operator++() { // Preincrement
127 Obj = Obj.getNext();
7
Null pointer value stored to '__begin1.Obj.Parent'
128 return *this;
129 }
130 };
131
132 MachOUniversalBinary(MemoryBufferRef Souce, Error &Err);
133 static Expected<std::unique_ptr<MachOUniversalBinary>>
134 create(MemoryBufferRef Source);
135
136 object_iterator begin_objects() const {
137 return ObjectForArch(this, 0);
138 }
139 object_iterator end_objects() const {
140 return ObjectForArch(nullptr, 0);
141 }
142
143 iterator_range<object_iterator> objects() const {
144 return make_range(begin_objects(), end_objects());
145 }
146
147 uint32_t getMagic() const { return Magic; }
148 uint32_t getNumberOfObjects() const { return NumberOfObjects; }
149
150 // Cast methods.
151 static bool classof(Binary const *V) {
152 return V->isMachOUniversalBinary();
153 }
154
155 Expected<ObjectForArch>
156 getObjectForArch(StringRef ArchName) const;
157
158 Expected<std::unique_ptr<MachOObjectFile>>
159 getMachOObjectForArch(StringRef ArchName) const;
160
161 Expected<std::unique_ptr<IRObjectFile>>
162 getIRObjectForArch(StringRef ArchName, LLVMContext &Ctx) const;
163
164 Expected<std::unique_ptr<Archive>>
165 getArchiveForArch(StringRef ArchName) const;
166};
167
168}
169}
170
171#endif