[lld] 77b6efb - [ADT] [lld-macho] Check for end iterator deref in filter_iterator_base
Nico Weber via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 21 12:50:06 PDT 2022
Author: Daniel Bertalan
Date: 2022-06-21T15:47:45-04:00
New Revision: 77b6efbd8290b4799b69bdbf93e22d30836a16b4
URL: https://github.com/llvm/llvm-project/commit/77b6efbd8290b4799b69bdbf93e22d30836a16b4
DIFF: https://github.com/llvm/llvm-project/commit/77b6efbd8290b4799b69bdbf93e22d30836a16b4.diff
LOG: [ADT] [lld-macho] Check for end iterator deref in filter_iterator_base
If ld64.lld was supplied an object file that had a `__debug_abbrev` or
`__debug_str` section, but didn't have any compile unit DIEs in
`__debug_info`, it would dereference an iterator pointing to the empty
array of DIEs. This underlying issue started causing segmentation faults
when parsing for `__debug_info` was addded in D128184. That commit was
reverted, and this one fixes the invalid dereference to allow relanding
it.
This commit adds an assertion to `filter_iterator_base`'s dereference
operators to catch bugs like this one.
Ran check-llvm, check-clang and check-lld.
Differential Revision: https://reviews.llvm.org/D128294
Added:
lld/test/MachO/dwarf-no-compile-unit.s
Modified:
lld/MachO/InputFiles.cpp
llvm/include/llvm/ADT/STLExtras.h
Removed:
################################################################################
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 89405d21878a2..b10adf5088e62 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1013,7 +1013,7 @@ void ObjFile::parseDebugInfo() {
// FIXME: There can be more than one compile unit per object file. See
// PR48637.
auto it = units.begin();
- compileUnit = it->get();
+ compileUnit = it != units.end() ? it->get() : nullptr;
}
ArrayRef<data_in_code_entry> ObjFile::getDataInCode() const {
diff --git a/lld/test/MachO/dwarf-no-compile-unit.s b/lld/test/MachO/dwarf-no-compile-unit.s
new file mode 100644
index 0000000000000..ced2467ca95d0
--- /dev/null
+++ b/lld/test/MachO/dwarf-no-compile-unit.s
@@ -0,0 +1,15 @@
+# REQUIRES: aarch64
+
+## Check that LLD does not crash if it encounters DWARF sections
+## without __debug_info compile unit DIEs being present.
+
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
+# RUN: %lld -arch arm64 %t.o -o /dev/null
+
+.text
+.globl _main
+_main:
+ ret
+
+.section __DWARF,__debug_abbrev,regular,debug
+ .byte 0
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 0f713ede8b9e2..0efa96e69a8ce 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -444,6 +444,16 @@ class filter_iterator_base
findNextValid();
return *this;
}
+
+ decltype(auto) operator*() const {
+ assert(BaseT::wrapped() != End && "Cannot dereference end iterator!");
+ return BaseT::operator*();
+ }
+
+ decltype(auto) operator->() const {
+ assert(BaseT::wrapped() != End && "Cannot dereference end iterator!");
+ return BaseT::operator->();
+ }
};
/// Specialization of filter_iterator_base for forward iteration only.
More information about the llvm-commits
mailing list