Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/MachOUniversal.cpp
Line
Count
Source (jump to first uncovered line)
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/MachO.h"
16
#include "llvm/Object/ObjectFile.h"
17
#include "llvm/Support/Casting.h"
18
#include "llvm/Support/Host.h"
19
#include "llvm/Support/MemoryBuffer.h"
20
21
using namespace llvm;
22
using namespace object;
23
24
static Error
25
506
malformedError(Twine Msg) {
26
506
  std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")";
27
506
  return make_error<GenericBinaryError>(std::move(StringMsg),
28
506
                                        object_error::parse_failed);
29
506
}
30
31
template<typename T>
32
1.77k
static T getUniversalBinaryStruct(const char *Ptr) {
33
1.77k
  T Res;
34
1.77k
  memcpy(&Res, Ptr, sizeof(T));
35
1.77k
  // Universal binary headers have big-endian byte order.
36
1.77k
  if (sys::IsLittleEndianHost)
37
1.77k
    swapStruct(Res);
38
1.77k
  return Res;
39
1.77k
}
MachOUniversal.cpp:llvm::MachO::fat_arch getUniversalBinaryStruct<llvm::MachO::fat_arch>(char const*)
Line
Count
Source
32
1.12k
static T getUniversalBinaryStruct(const char *Ptr) {
33
1.12k
  T Res;
34
1.12k
  memcpy(&Res, Ptr, sizeof(T));
35
1.12k
  // Universal binary headers have big-endian byte order.
36
1.12k
  if (sys::IsLittleEndianHost)
37
1.12k
    swapStruct(Res);
38
1.12k
  return Res;
39
1.12k
}
MachOUniversal.cpp:llvm::MachO::fat_arch_64 getUniversalBinaryStruct<llvm::MachO::fat_arch_64>(char const*)
Line
Count
Source
32
23
static T getUniversalBinaryStruct(const char *Ptr) {
33
23
  T Res;
34
23
  memcpy(&Res, Ptr, sizeof(T));
35
23
  // Universal binary headers have big-endian byte order.
36
23
  if (sys::IsLittleEndianHost)
37
23
    swapStruct(Res);
38
23
  return Res;
39
23
}
MachOUniversal.cpp:llvm::MachO::fat_header getUniversalBinaryStruct<llvm::MachO::fat_header>(char const*)
Line
Count
Source
32
626
static T getUniversalBinaryStruct(const char *Ptr) {
33
626
  T Res;
34
626
  memcpy(&Res, Ptr, sizeof(T));
35
626
  // Universal binary headers have big-endian byte order.
36
626
  if (sys::IsLittleEndianHost)
37
626
    swapStruct(Res);
38
626
  return Res;
39
626
}
40
41
MachOUniversalBinary::ObjectForArch::ObjectForArch(
42
    const MachOUniversalBinary *Parent, uint32_t Index)
43
1.35k
    : Parent(Parent), Index(Index) {
44
1.35k
  // The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
45
1.35k
  if (!Parent || 
Index >= Parent->getNumberOfObjects()1.23k
) {
46
207
    clear();
47
1.15k
  } else {
48
1.15k
    // Parse object header.
49
1.15k
    StringRef ParentData = Parent->getData();
50
1.15k
    if (Parent->getMagic() == MachO::FAT_MAGIC) {
51
1.12k
      const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
52
1.12k
                              Index * sizeof(MachO::fat_arch);
53
1.12k
      Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
54
1.12k
    } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
55
23
      const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
56
23
                              Index * sizeof(MachO::fat_arch_64);
57
23
      Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
58
23
    }
59
1.15k
  }
60
1.35k
}
61
62
Expected<std::unique_ptr<MachOObjectFile>>
63
128
MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
64
128
  if (!Parent)
65
0
    report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsObjectFile() "
66
0
                       "called when Parent is a nullptr");
67
128
68
128
  StringRef ParentData = Parent->getData();
69
128
  StringRef ObjectData;
70
128
  uint32_t cputype;
71
128
  if (Parent->getMagic() == MachO::FAT_MAGIC) {
72
123
    ObjectData = ParentData.substr(Header.offset, Header.size);
73
123
    cputype = Header.cputype;
74
123
  } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
75
5
    ObjectData = ParentData.substr(Header64.offset, Header64.size);
76
5
    cputype = Header64.cputype;
77
5
  }
78
128
  StringRef ObjectName = Parent->getFileName();
79
128
  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
80
128
  return ObjectFile::createMachOObjectFile(ObjBuffer, cputype, Index);
81
128
}
82
83
Expected<std::unique_ptr<Archive>>
84
58
MachOUniversalBinary::ObjectForArch::getAsArchive() const {
85
58
  if (!Parent)
86
0
    report_fatal_error("MachOUniversalBinary::ObjectForArch::getAsArchive() "
87
0
                       "called when Parent is a nullptr");
88
58
89
58
  StringRef ParentData = Parent->getData();
90
58
  StringRef ObjectData;
91
58
  if (Parent->getMagic() == MachO::FAT_MAGIC)
92
56
    ObjectData = ParentData.substr(Header.offset, Header.size);
93
2
  else // Parent->getMagic() == MachO::FAT_MAGIC_64
94
2
    ObjectData = ParentData.substr(Header64.offset, Header64.size);
95
58
  StringRef ObjectName = Parent->getFileName();
96
58
  MemoryBufferRef ObjBuffer(ObjectData, ObjectName);
97
58
  return Archive::create(ObjBuffer);
98
58
}
99
100
0
void MachOUniversalBinary::anchor() { }
101
102
Expected<std::unique_ptr<MachOUniversalBinary>>
103
626
MachOUniversalBinary::create(MemoryBufferRef Source) {
104
626
  Error Err = Error::success();
105
626
  std::unique_ptr<MachOUniversalBinary> Ret(
106
626
      new MachOUniversalBinary(Source, Err));
107
626
  if (Err)
108
506
    return std::move(Err);
109
120
  return std::move(Ret);
110
120
}
111
112
MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
113
    : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
114
626
      NumberOfObjects(0) {
115
626
  ErrorAsOutParameter ErrAsOutParam(&Err);
116
626
  if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
117
0
    Err = make_error<GenericBinaryError>("File too small to be a Mach-O "
118
0
                                         "universal file",
119
0
                                         object_error::invalid_file_type);
120
0
    return;
121
0
  }
122
626
  // Check for magic value and sufficient header size.
123
626
  StringRef Buf = getData();
124
626
  MachO::fat_header H =
125
626
      getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
126
626
  Magic = H.magic;
127
626
  NumberOfObjects = H.nfat_arch;
128
626
  if (NumberOfObjects == 0) {
129
1
    Err = malformedError("contains zero architecture types");
130
1
    return;
131
1
  }
132
625
  uint32_t MinSize = sizeof(MachO::fat_header);
133
625
  if (Magic == MachO::FAT_MAGIC)
134
123
    MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
135
502
  else if (Magic == MachO::FAT_MAGIC_64)
136
4
    MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects;
137
498
  else {
138
498
    Err = malformedError("bad magic number");
139
498
    return;
140
498
  }
141
127
  if (Buf.size() < MinSize) {
142
1
    Err = malformedError("fat_arch" +
143
1
                         Twine(Magic == MachO::FAT_MAGIC ? 
""0
: "_64") +
144
1
                         " structs would extend past the end of the file");
145
1
    return;
146
1
  }
147
427
  
for (uint32_t i = 0; 126
i < NumberOfObjects;
i++301
) {
148
305
    ObjectForArch A(this, i);
149
305
    uint64_t bigSize = A.getOffset();
150
305
    bigSize += A.getSize();
151
305
    if (bigSize > Buf.size()) {
152
1
      Err = malformedError("offset plus size of cputype (" +
153
1
        Twine(A.getCPUType()) + ") cpusubtype (" +
154
1
        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
155
1
        ") extends past the end of the file");
156
1
      return;
157
1
    }
158
305
#define MAXSECTALIGN 15 /* 2**15 or 0x8000 */
159
304
    if (A.getAlign() > MAXSECTALIGN) {
160
1
      Err = malformedError("align (2^" + Twine(A.getAlign()) + ") too large "
161
1
        "for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
162
1
        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
163
1
        ") (maximum 2^" + Twine(MAXSECTALIGN) + ")");
164
1
      return;
165
1
    }
166
303
    if(A.getOffset() % (1 << A.getAlign()) != 0){
167
1
      Err = malformedError("offset: " + Twine(A.getOffset()) +
168
1
        " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
169
1
        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
170
1
        ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
171
1
      return;
172
1
    }
173
302
    if (A.getOffset() < MinSize) {
174
1
      Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
175
1
        "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
176
1
        ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
177
1
      return;
178
1
    }
179
302
  }
180
418
  
for (uint32_t i = 0; 122
i < NumberOfObjects;
i++296
) {
181
298
    ObjectForArch A(this, i);
182
554
    for (uint32_t j = i + 1; j < NumberOfObjects; 
j++256
) {
183
258
      ObjectForArch B(this, j);
184
258
      if (A.getCPUType() == B.getCPUType() &&
185
258
          (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
186
66
          (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
187
1
        Err = malformedError("contains two of the same architecture (cputype "
188
1
          "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
189
1
          Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
190
1
        return;
191
1
      }
192
257
      if ((A.getOffset() >= B.getOffset() &&
193
257
           
A.getOffset() < B.getOffset() + B.getSize()32
) ||
194
257
          (A.getOffset() + A.getSize() > B.getOffset() &&
195
257
           
A.getOffset() + A.getSize() < B.getOffset() + B.getSize()33
) ||
196
257
          
(256
A.getOffset() <= B.getOffset()256
&&
197
256
           
A.getOffset() + A.getSize() >= B.getOffset() + B.getSize()224
)) {
198
1
        Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
199
1
          "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
200
1
          ") at offset " + Twine(A.getOffset()) + " with a size of " +
201
1
          Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
202
1
          ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
203
1
          + ") at offset " + Twine(B.getOffset()) + " with a size of "
204
1
          + Twine(B.getSize()));
205
1
        return;
206
1
      }
207
257
    }
208
298
  }
209
122
  Err = Error::success();
210
120
}
211
212
Expected<std::unique_ptr<MachOObjectFile>>
213
23
MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
214
23
  if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
215
1
    return make_error<GenericBinaryError>("Unknown architecture "
216
1
                                          "named: " +
217
1
                                              ArchName,
218
1
                                          object_error::arch_not_found);
219
22
220
22
  for (auto &Obj : objects())
221
39
    if (Obj.getArchFlagName() == ArchName)
222
20
      return Obj.getAsObjectFile();
223
22
  return make_error<GenericBinaryError>("fat file does not "
224
2
                                        "contain " +
225
2
                                            ArchName,
226
2
                                        object_error::arch_not_found);
227
22
}