Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ObjectContainerBSDArchive.cpp -------------------------------------===//
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 "ObjectContainerBSDArchive.h"
10
11
#if defined(_WIN32) || defined(__ANDROID__)
12
// Defines from ar, missing on Windows
13
#define ARMAG "!<arch>\n"
14
#define SARMAG 8
15
#define ARFMAG "`\n"
16
17
typedef struct ar_hdr {
18
  char ar_name[16];
19
  char ar_date[12];
20
  char ar_uid[6], ar_gid[6];
21
  char ar_mode[8];
22
  char ar_size[10];
23
  char ar_fmag[2];
24
} ar_hdr;
25
#else
26
#include <ar.h>
27
#endif
28
29
#include "lldb/Core/Module.h"
30
#include "lldb/Core/ModuleSpec.h"
31
#include "lldb/Core/PluginManager.h"
32
#include "lldb/Host/FileSystem.h"
33
#include "lldb/Symbol/ObjectFile.h"
34
#include "lldb/Utility/ArchSpec.h"
35
#include "lldb/Utility/Stream.h"
36
#include "lldb/Utility/Timer.h"
37
38
#include "llvm/Support/MemoryBuffer.h"
39
40
using namespace lldb;
41
using namespace lldb_private;
42
43
LLDB_PLUGIN_DEFINE(ObjectContainerBSDArchive)
44
45
13
ObjectContainerBSDArchive::Object::Object() : ar_name() {}
46
47
184
void ObjectContainerBSDArchive::Object::Clear() {
48
184
  ar_name.Clear();
49
184
  modification_time = 0;
50
184
  uid = 0;
51
184
  gid = 0;
52
184
  mode = 0;
53
184
  size = 0;
54
184
  file_offset = 0;
55
184
  file_size = 0;
56
184
}
57
58
lldb::offset_t
59
ObjectContainerBSDArchive::Object::Extract(const DataExtractor &data,
60
184
                                           lldb::offset_t offset) {
61
184
  size_t ar_name_len = 0;
62
184
  std::string str;
63
184
  char *err;
64
65
  // File header
66
  //
67
  // The common format is as follows.
68
  //
69
  //  Offset  Length  Name            Format
70
  //  0       16      File name       ASCII right padded with spaces (no spaces
71
  //  allowed in file name)
72
  //  16      12      File mod        Decimal as cstring right padded with
73
  //  spaces
74
  //  28      6       Owner ID        Decimal as cstring right padded with
75
  //  spaces
76
  //  34      6       Group ID        Decimal as cstring right padded with
77
  //  spaces
78
  //  40      8       File mode       Octal   as cstring right padded with
79
  //  spaces
80
  //  48      10      File byte size  Decimal as cstring right padded with
81
  //  spaces
82
  //  58      2       File magic      0x60 0x0A
83
84
  // Make sure there is enough data for the file header and bail if not
85
184
  if (!data.ValidOffsetForDataOfSize(offset, 60))
86
0
    return LLDB_INVALID_OFFSET;
87
88
184
  str.assign((const char *)data.GetData(&offset, 16), 16);
89
184
  if (llvm::StringRef(str).startswith("#1/")) {
90
    // If the name is longer than 16 bytes, or contains an embedded space then
91
    // it will use this format where the length of the name is here and the
92
    // name characters are after this header.
93
184
    ar_name_len = strtoul(str.c_str() + 3, &err, 10);
94
184
  } else {
95
    // Strip off any trailing spaces.
96
0
    const size_t last_pos = str.find_last_not_of(' ');
97
0
    if (last_pos != std::string::npos) {
98
0
      if (last_pos + 1 < 16)
99
0
        str.erase(last_pos + 1);
100
0
    }
101
0
    ar_name.SetCString(str.c_str());
102
0
  }
103
104
184
  str.assign((const char *)data.GetData(&offset, 12), 12);
105
184
  modification_time = strtoul(str.c_str(), &err, 10);
106
107
184
  str.assign((const char *)data.GetData(&offset, 6), 6);
108
184
  uid = strtoul(str.c_str(), &err, 10);
109
110
184
  str.assign((const char *)data.GetData(&offset, 6), 6);
111
184
  gid = strtoul(str.c_str(), &err, 10);
112
113
184
  str.assign((const char *)data.GetData(&offset, 8), 8);
114
184
  mode = strtoul(str.c_str(), &err, 8);
115
116
184
  str.assign((const char *)data.GetData(&offset, 10), 10);
117
184
  size = strtoul(str.c_str(), &err, 10);
118
119
184
  str.assign((const char *)data.GetData(&offset, 2), 2);
120
184
  if (str == ARFMAG) {
121
184
    if (ar_name_len > 0) {
122
184
      const void *ar_name_ptr = data.GetData(&offset, ar_name_len);
123
      // Make sure there was enough data for the string value and bail if not
124
184
      if (ar_name_ptr == nullptr)
125
0
        return LLDB_INVALID_OFFSET;
126
184
      str.assign((const char *)ar_name_ptr, ar_name_len);
127
184
      ar_name.SetCString(str.c_str());
128
184
    }
129
184
    file_offset = offset;
130
184
    file_size = size - ar_name_len;
131
184
    return offset;
132
184
  }
133
0
  return LLDB_INVALID_OFFSET;
134
184
}
135
136
ObjectContainerBSDArchive::Archive::Archive(const lldb_private::ArchSpec &arch,
137
                                            const llvm::sys::TimePoint<> &time,
138
                                            lldb::offset_t file_offset,
139
                                            lldb_private::DataExtractor &data)
140
    : m_arch(arch), m_modification_time(time), m_file_offset(file_offset),
141
13
      m_objects(), m_data(data) {}
142
143
13
ObjectContainerBSDArchive::Archive::~Archive() = default;
144
145
13
size_t ObjectContainerBSDArchive::Archive::ParseObjects() {
146
13
  DataExtractor &data = m_data;
147
13
  std::string str;
148
13
  lldb::offset_t offset = 0;
149
13
  str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
150
13
  if (str == ARMAG) {
151
13
    Object obj;
152
184
    do {
153
184
      offset = obj.Extract(data, offset);
154
184
      if (offset == LLDB_INVALID_OFFSET)
155
0
        break;
156
184
      size_t obj_idx = m_objects.size();
157
184
      m_objects.push_back(obj);
158
      // Insert all of the C strings out of order for now...
159
184
      m_object_name_to_index_map.Append(obj.ar_name, obj_idx);
160
184
      offset += obj.file_size;
161
184
      obj.Clear();
162
184
    } while (data.ValidOffset(offset));
163
164
    // Now sort all of the object name pointers
165
0
    m_object_name_to_index_map.Sort();
166
13
  }
167
0
  return m_objects.size();
168
13
}
169
170
ObjectContainerBSDArchive::Object *
171
ObjectContainerBSDArchive::Archive::FindObject(
172
201
    ConstString object_name, const llvm::sys::TimePoint<> &object_mod_time) {
173
201
  const ObjectNameToIndexMap::Entry *match =
174
201
      m_object_name_to_index_map.FindFirstValueForName(object_name);
175
201
  if (!match)
176
0
    return nullptr;
177
201
  if (object_mod_time == llvm::sys::TimePoint<>())
178
0
    return &m_objects[match->value];
179
180
201
  const uint64_t object_modification_date = llvm::sys::toTimeT(object_mod_time);
181
201
  if (m_objects[match->value].modification_time == object_modification_date)
182
198
    return &m_objects[match->value];
183
184
3
  const ObjectNameToIndexMap::Entry *next_match =
185
3
      m_object_name_to_index_map.FindNextValueForName(match);
186
3
  while (next_match) {
187
3
    if (m_objects[next_match->value].modification_time ==
188
3
        object_modification_date)
189
3
      return &m_objects[next_match->value];
190
0
    next_match = m_object_name_to_index_map.FindNextValueForName(next_match);
191
0
  }
192
193
0
  return nullptr;
194
3
}
195
196
ObjectContainerBSDArchive::Archive::shared_ptr
197
ObjectContainerBSDArchive::Archive::FindCachedArchive(
198
    const FileSpec &file, const ArchSpec &arch,
199
408
    const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset) {
200
408
  std::lock_guard<std::recursive_mutex> guard(Archive::GetArchiveCacheMutex());
201
408
  shared_ptr archive_sp;
202
408
  Archive::Map &archive_map = Archive::GetArchiveCache();
203
408
  Archive::Map::iterator pos = archive_map.find(file);
204
  // Don't cache a value for "archive_map.end()" below since we might delete an
205
  // archive entry...
206
591
  while (pos != archive_map.end() && 
pos->first == file371
) {
207
371
    bool match = true;
208
371
    if (arch.IsValid() &&
209
371
        !pos->second->GetArchitecture().IsCompatibleMatch(arch))
210
0
      match = false;
211
371
    else if (file_offset != LLDB_INVALID_OFFSET &&
212
371
             pos->second->GetFileOffset() != file_offset)
213
183
      match = false;
214
371
    if (match) {
215
188
      if (pos->second->GetModificationTime() == time) {
216
188
        return pos->second;
217
188
      } else {
218
        // We have a file at the same path with the same architecture whose
219
        // modification time doesn't match. It doesn't make sense for us to
220
        // continue to use this BSD archive since we cache only the object info
221
        // which consists of file time info and also the file offset and file
222
        // size of any contained objects. Since this information is now out of
223
        // date, we won't get the correct information if we go and extract the
224
        // file data, so we should remove the old and outdated entry.
225
0
        archive_map.erase(pos);
226
0
        pos = archive_map.find(file);
227
0
        continue; // Continue to next iteration so we don't increment pos
228
                  // below...
229
0
      }
230
188
    }
231
183
    ++pos;
232
183
  }
233
220
  return archive_sp;
234
408
}
235
236
ObjectContainerBSDArchive::Archive::shared_ptr
237
ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile(
238
    const FileSpec &file, const ArchSpec &arch,
239
    const llvm::sys::TimePoint<> &time, lldb::offset_t file_offset,
240
13
    DataExtractor &data) {
241
13
  shared_ptr archive_sp(new Archive(arch, time, file_offset, data));
242
13
  if (archive_sp) {
243
13
    const size_t num_objects = archive_sp->ParseObjects();
244
13
    if (num_objects > 0) {
245
13
      std::lock_guard<std::recursive_mutex> guard(
246
13
          Archive::GetArchiveCacheMutex());
247
13
      Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
248
13
    } else {
249
0
      archive_sp.reset();
250
0
    }
251
13
  }
252
13
  return archive_sp;
253
13
}
254
255
ObjectContainerBSDArchive::Archive::Map &
256
421
ObjectContainerBSDArchive::Archive::GetArchiveCache() {
257
421
  static Archive::Map g_archive_map;
258
421
  return g_archive_map;
259
421
}
260
261
std::recursive_mutex &
262
421
ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex() {
263
421
  static std::recursive_mutex g_archive_map_mutex;
264
421
  return g_archive_map_mutex;
265
421
}
266
267
3.44k
void ObjectContainerBSDArchive::Initialize() {
268
3.44k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
269
3.44k
                                GetPluginDescriptionStatic(), CreateInstance,
270
3.44k
                                GetModuleSpecifications);
271
3.44k
}
272
273
3.43k
void ObjectContainerBSDArchive::Terminate() {
274
3.43k
  PluginManager::UnregisterPlugin(CreateInstance);
275
3.43k
}
276
277
ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
278
    const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
279
    lldb::offset_t data_offset, const FileSpec *file,
280
601
    lldb::offset_t file_offset, lldb::offset_t length) {
281
601
  ConstString object_name(module_sp->GetObjectName());
282
601
  if (!object_name)
283
1
    return nullptr;
284
285
600
  if (data_sp) {
286
    // We have data, which means this is the first 512 bytes of the file Check
287
    // to see if the magic bytes match and if they do, read the entire table of
288
    // contents for the archive and cache it
289
205
    DataExtractor data;
290
205
    data.SetData(data_sp, data_offset, length);
291
205
    if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data)) {
292
13
      LLDB_SCOPED_TIMERF(
293
13
          "ObjectContainerBSDArchive::CreateInstance (module = %s, file = "
294
13
          "%p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
295
13
          module_sp->GetFileSpec().GetPath().c_str(),
296
13
          static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
297
13
          static_cast<uint64_t>(length));
298
299
      // Map the entire .a file to be sure that we don't lose any data if the
300
      // file gets updated by a new build while this .a file is being used for
301
      // debugging
302
13
      DataBufferSP archive_data_sp =
303
13
          FileSystem::Instance().CreateDataBuffer(*file, length, file_offset);
304
13
      if (!archive_data_sp)
305
0
        return nullptr;
306
307
13
      lldb::offset_t archive_data_offset = 0;
308
309
13
      Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
310
13
          *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
311
13
          file_offset));
312
13
      std::unique_ptr<ObjectContainerBSDArchive> container_up(
313
13
          new ObjectContainerBSDArchive(module_sp, archive_data_sp,
314
13
                                        archive_data_offset, file, file_offset,
315
13
                                        length));
316
317
13
      if (container_up) {
318
13
        if (archive_sp) {
319
          // We already have this archive in our cache, use it
320
0
          container_up->SetArchive(archive_sp);
321
0
          return container_up.release();
322
13
        } else if (container_up->ParseHeader())
323
13
          return container_up.release();
324
13
      }
325
13
    }
326
395
  } else {
327
    // No data, just check for a cached archive
328
395
    Archive::shared_ptr archive_sp(Archive::FindCachedArchive(
329
395
        *file, module_sp->GetArchitecture(), module_sp->GetModificationTime(),
330
395
        file_offset));
331
395
    if (archive_sp) {
332
188
      std::unique_ptr<ObjectContainerBSDArchive> container_up(
333
188
          new ObjectContainerBSDArchive(module_sp, data_sp, data_offset, file,
334
188
                                        file_offset, length));
335
336
188
      if (container_up) {
337
        // We already have this archive in our cache, use it
338
188
        container_up->SetArchive(archive_sp);
339
188
        return container_up.release();
340
188
      }
341
188
    }
342
395
  }
343
399
  return nullptr;
344
600
}
345
346
2.88k
bool ObjectContainerBSDArchive::MagicBytesMatch(const DataExtractor &data) {
347
2.88k
  uint32_t offset = 0;
348
2.88k
  const char *armag = (const char *)data.PeekData(offset, sizeof(ar_hdr));
349
2.88k
  if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0) {
350
13
    armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
351
13
    if (strncmp(armag, ARFMAG, 2) == 0)
352
13
      return true;
353
13
  }
354
2.86k
  return false;
355
2.88k
}
356
357
ObjectContainerBSDArchive::ObjectContainerBSDArchive(
358
    const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
359
    lldb::offset_t data_offset, const lldb_private::FileSpec *file,
360
    lldb::offset_t file_offset, lldb::offset_t size)
361
    : ObjectContainer(module_sp, file, file_offset, size, data_sp, data_offset),
362
201
      m_archive_sp() {}
363
188
void ObjectContainerBSDArchive::SetArchive(Archive::shared_ptr &archive_sp) {
364
188
  m_archive_sp = archive_sp;
365
188
}
366
367
201
ObjectContainerBSDArchive::~ObjectContainerBSDArchive() = default;
368
369
13
bool ObjectContainerBSDArchive::ParseHeader() {
370
13
  if (m_archive_sp.get() == nullptr) {
371
13
    if (m_data.GetByteSize() > 0) {
372
13
      ModuleSP module_sp(GetModule());
373
13
      if (module_sp) {
374
13
        m_archive_sp = Archive::ParseAndCacheArchiveForFile(
375
13
            m_file, module_sp->GetArchitecture(),
376
13
            module_sp->GetModificationTime(), m_offset, m_data);
377
13
      }
378
      // Clear the m_data that contains the entire archive data and let our
379
      // m_archive_sp hold onto the data.
380
13
      m_data.Clear();
381
13
    }
382
13
  }
383
13
  return m_archive_sp.get() != nullptr;
384
13
}
385
386
0
void ObjectContainerBSDArchive::Dump(Stream *s) const {
387
0
  s->Printf("%p: ", static_cast<const void *>(this));
388
0
  s->Indent();
389
0
  const size_t num_archs = GetNumArchitectures();
390
0
  const size_t num_objects = GetNumObjects();
391
0
  s->Printf("ObjectContainerBSDArchive, num_archs = %" PRIu64
392
0
            ", num_objects = %" PRIu64 "",
393
0
            (uint64_t)num_archs, (uint64_t)num_objects);
394
0
  uint32_t i;
395
0
  ArchSpec arch;
396
0
  s->IndentMore();
397
0
  for (i = 0; i < num_archs; i++) {
398
0
    s->Indent();
399
0
    GetArchitectureAtIndex(i, arch);
400
0
    s->Printf("arch[%u] = %s\n", i, arch.GetArchitectureName());
401
0
  }
402
0
  for (i = 0; i < num_objects; i++) {
403
0
    s->Indent();
404
0
    s->Printf("object[%u] = %s\n", i, GetObjectNameAtIndex(i));
405
0
  }
406
0
  s->IndentLess();
407
0
  s->EOL();
408
0
}
409
410
201
ObjectFileSP ObjectContainerBSDArchive::GetObjectFile(const FileSpec *file) {
411
201
  ModuleSP module_sp(GetModule());
412
201
  if (module_sp) {
413
201
    if (module_sp->GetObjectName() && m_archive_sp) {
414
201
      Object *object = m_archive_sp->FindObject(
415
201
          module_sp->GetObjectName(), module_sp->GetObjectModificationTime());
416
201
      if (object) {
417
201
        lldb::offset_t data_offset = object->file_offset;
418
201
        return ObjectFile::FindPlugin(
419
201
            module_sp, file, m_offset + object->file_offset, object->file_size,
420
201
            m_archive_sp->GetData().GetSharedDataBuffer(), data_offset);
421
201
      }
422
201
    }
423
201
  }
424
0
  return ObjectFileSP();
425
201
}
426
427
size_t ObjectContainerBSDArchive::GetModuleSpecifications(
428
    const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
429
    lldb::offset_t data_offset, lldb::offset_t file_offset,
430
2.67k
    lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
431
432
  // We have data, which means this is the first 512 bytes of the file Check to
433
  // see if the magic bytes match and if they do, read the entire table of
434
  // contents for the archive and cache it
435
2.67k
  DataExtractor data;
436
2.67k
  data.SetData(data_sp, data_offset, data_sp->GetByteSize());
437
2.67k
  if (!file || !data_sp || !ObjectContainerBSDArchive::MagicBytesMatch(data))
438
2.67k
    return 0;
439
440
0
  const size_t initial_count = specs.GetSize();
441
0
  llvm::sys::TimePoint<> file_mod_time = FileSystem::Instance().GetModificationTime(file);
442
0
  Archive::shared_ptr archive_sp(
443
0
      Archive::FindCachedArchive(file, ArchSpec(), file_mod_time, file_offset));
444
0
  bool set_archive_arch = false;
445
0
  if (!archive_sp) {
446
0
    set_archive_arch = true;
447
0
    data_sp =
448
0
        FileSystem::Instance().CreateDataBuffer(file, file_size, file_offset);
449
0
    if (data_sp) {
450
0
      data.SetData(data_sp, 0, data_sp->GetByteSize());
451
0
      archive_sp = Archive::ParseAndCacheArchiveForFile(
452
0
          file, ArchSpec(), file_mod_time, file_offset, data);
453
0
    }
454
0
  }
455
456
0
  if (archive_sp) {
457
0
    const size_t num_objects = archive_sp->GetNumObjects();
458
0
    for (size_t idx = 0; idx < num_objects; ++idx) {
459
0
      const Object *object = archive_sp->GetObjectAtIndex(idx);
460
0
      if (object) {
461
0
        const lldb::offset_t object_file_offset =
462
0
            file_offset + object->file_offset;
463
0
        if (object->file_offset < file_size && file_size > object_file_offset) {
464
0
          if (ObjectFile::GetModuleSpecifications(
465
0
                  file, object_file_offset, file_size - object_file_offset,
466
0
                  specs)) {
467
0
            ModuleSpec &spec =
468
0
                specs.GetModuleSpecRefAtIndex(specs.GetSize() - 1);
469
0
            llvm::sys::TimePoint<> object_mod_time(
470
0
                std::chrono::seconds(object->modification_time));
471
0
            spec.GetObjectName() = object->ar_name;
472
0
            spec.SetObjectOffset(object_file_offset);
473
0
            spec.SetObjectSize(file_size - object_file_offset);
474
0
            spec.GetObjectModificationTime() = object_mod_time;
475
0
          }
476
0
        }
477
0
      }
478
0
    }
479
0
  }
480
0
  const size_t end_count = specs.GetSize();
481
0
  size_t num_specs_added = end_count - initial_count;
482
0
  if (set_archive_arch && num_specs_added > 0) {
483
    // The archive was created but we didn't have an architecture so we need to
484
    // set it
485
0
    for (size_t i = initial_count; i < end_count; ++i) {
486
0
      ModuleSpec module_spec;
487
0
      if (specs.GetModuleSpecAtIndex(i, module_spec)) {
488
0
        if (module_spec.GetArchitecture().IsValid()) {
489
0
          archive_sp->SetArchitecture(module_spec.GetArchitecture());
490
0
          break;
491
0
        }
492
0
      }
493
0
    }
494
0
  }
495
0
  return num_specs_added;
496
2.67k
}