[PATCH] D157300: [lld-macho]Rework error-checking in peeking at first-member in archive to avoid segfault.
Vy Nguyen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 9 06:09:25 PDT 2023
oontvoo updated this revision to Diff 548592.
oontvoo marked an inline comment as done.
oontvoo added a comment.
addressed review comment
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D157300/new/
https://reviews.llvm.org/D157300
Files:
lld/MachO/InputFiles.cpp
lld/test/MachO/force-load.s
Index: lld/test/MachO/force-load.s
===================================================================
--- lld/test/MachO/force-load.s
+++ lld/test/MachO/force-load.s
@@ -42,6 +42,12 @@
# TWICE-DAG: __TEXT,archive _bar
# TWICE-DAG: __TEXT,archive _baz
+## Loading an empty-archive should not crash.
+# RUN: llvm-ar --format=darwin rcs %t/libEmpty.a
+# RUN: %lld -lSystem %t/test.o -force_load %t/libEmpty.a -o %t/loadEmpty.out
+# RUN: llvm-objdump --macho --syms %t/loadEmpty.out | FileCheck %s --check-prefix=EMPTY
+# EMPTY: F __TEXT,__text _main
+
#--- archive-foo.s
.section __TEXT,archive
.globl _foo, _bar
Index: lld/MachO/InputFiles.cpp
===================================================================
--- lld/MachO/InputFiles.cpp
+++ lld/MachO/InputFiles.cpp
@@ -2106,28 +2106,34 @@
forceHidden(forceHidden) {}
void ArchiveFile::addLazySymbols() {
- Error err = Error::success();
- Expected<MemoryBufferRef> mbOrErr =
- this->file->child_begin(err)->getMemoryBufferRef();
+ // Avoid calling getMemoryBufferRef() on zero-symbol archive
+ // since that crashes.
+ if (file->isEmpty() || file->getNumberOfSymbols() == 0)
+ return;
+ Error err = Error::success();
+ auto child = file->child_begin(err);
// Ignore the I/O error here - will be reported later.
- if (!mbOrErr) {
- llvm::consumeError(mbOrErr.takeError());
- } else if (!err) {
- if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) {
- if (target->wordSize == 8)
- compatArch = compatWithTargetArch(
- this, reinterpret_cast<const LP64::mach_header *>(
- mbOrErr->getBufferStart()));
- else
- compatArch = compatWithTargetArch(
- this, reinterpret_cast<const ILP32::mach_header *>(
- mbOrErr->getBufferStart()));
-
- if (!compatArch)
- return;
+ if (!err) {
+ Expected<MemoryBufferRef> mbOrErr = child->getMemoryBufferRef();
+ if (!mbOrErr) {
+ llvm::consumeError(mbOrErr.takeError());
+ } else {
+ if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) {
+ if (target->wordSize == 8)
+ compatArch = compatWithTargetArch(
+ this, reinterpret_cast<const LP64::mach_header *>(
+ mbOrErr->getBufferStart()));
+ else
+ compatArch = compatWithTargetArch(
+ this, reinterpret_cast<const ILP32::mach_header *>(
+ mbOrErr->getBufferStart()));
+ if (!compatArch)
+ return;
+ }
}
}
+
for (const object::Archive::Symbol &sym : file->symbols())
symtab->addLazyArchive(sym.getName(), this, sym);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157300.548592.patch
Type: text/x-patch
Size: 2693 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230809/2c79033a/attachment.bin>
More information about the llvm-commits
mailing list