[llvm] r370310 - [COFF] Add a bounds checking helper for iterating a coff_resource_dir_table
Martin Storsjo via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 29 01:59:56 PDT 2019
Author: mstorsjo
Date: Thu Aug 29 01:59:56 2019
New Revision: 370310
URL: http://llvm.org/viewvc/llvm-project?rev=370310&view=rev
Log:
[COFF] Add a bounds checking helper for iterating a coff_resource_dir_table
Instead of blindly incrementing pointers in llvm-readobj, use this
helper, which does bounds checking against the available section
data.
Differential Revision: https://reviews.llvm.org/D66818
Modified:
llvm/trunk/include/llvm/Object/COFF.h
llvm/trunk/lib/Object/COFFObjectFile.cpp
llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
Modified: llvm/trunk/include/llvm/Object/COFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/COFF.h?rev=370310&r1=370309&r2=370310&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/COFF.h (original)
+++ llvm/trunk/include/llvm/Object/COFF.h Thu Aug 29 01:59:56 2019
@@ -1209,11 +1209,15 @@ public:
Expected<const coff_resource_dir_table &>
getEntrySubDir(const coff_resource_dir_entry &Entry);
Expected<const coff_resource_dir_table &> getBaseTable();
+ Expected<const coff_resource_dir_entry &>
+ getTableEntry(const coff_resource_dir_table &Table, uint32_t Index);
private:
BinaryByteStream BBS;
Expected<const coff_resource_dir_table &> getTableAtOffset(uint32_t Offset);
+ Expected<const coff_resource_dir_entry &>
+ getTableEntryAtOffset(uint32_t Offset);
Expected<ArrayRef<UTF16>> getDirStringAtOffset(uint32_t Offset);
};
Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=370310&r1=370309&r2=370310&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Thu Aug 29 01:59:56 2019
@@ -1696,6 +1696,17 @@ ResourceSectionRef::getTableAtOffset(uin
return *Table;
}
+Expected<const coff_resource_dir_entry &>
+ResourceSectionRef::getTableEntryAtOffset(uint32_t Offset) {
+ const coff_resource_dir_entry *Entry = nullptr;
+
+ BinaryStreamReader Reader(BBS);
+ Reader.setOffset(Offset);
+ RETURN_IF_ERROR(Reader.readObject(Entry));
+ assert(Entry != nullptr);
+ return *Entry;
+}
+
Expected<const coff_resource_dir_table &>
ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) {
return getTableAtOffset(Entry.Offset.value());
@@ -1704,3 +1715,14 @@ ResourceSectionRef::getEntrySubDir(const
Expected<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() {
return getTableAtOffset(0);
}
+
+Expected<const coff_resource_dir_entry &>
+ResourceSectionRef::getTableEntry(const coff_resource_dir_table &Table,
+ uint32_t Index) {
+ if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries))
+ return createStringError(object_error::parse_failed, "index out of range");
+ const uint8_t *TablePtr = reinterpret_cast<const uint8_t *>(&Table);
+ ptrdiff_t TableOffset = TablePtr - BBS.data().data();
+ return getTableEntryAtOffset(TableOffset + sizeof(Table) +
+ Index * sizeof(coff_resource_dir_entry));
+}
Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=370310&r1=370309&r2=370310&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Thu Aug 29 01:59:56 2019
@@ -171,9 +171,6 @@ private:
void printDelayImportedSymbols(
const DelayImportDirectoryEntryRef &I,
iterator_range<imported_symbol_iterator> Range);
- Expected<const coff_resource_dir_entry &>
- getResourceDirectoryTableEntry(const coff_resource_dir_table &Table,
- uint32_t Index);
typedef DenseMap<const coff_section*, std::vector<RelocationRef> > RelocMapTy;
@@ -1790,8 +1787,7 @@ COFFDumper::countTotalTableEntries(Resou
uint32_t TotalEntries = 0;
for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
i++) {
- auto Entry = unwrapOrError(Obj->getFileName(),
- getResourceDirectoryTableEntry(Table, i));
+ auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i));
if (Entry.Offset.isSubDir()) {
StringRef NextLevel;
if (Level == "Name")
@@ -1818,8 +1814,7 @@ void COFFDumper::printResourceDirectoryT
// Iterate through level in resource directory tree.
for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
i++) {
- auto Entry = unwrapOrError(Obj->getFileName(),
- getResourceDirectoryTableEntry(Table, i));
+ auto Entry = unwrapOrError(Obj->getFileName(), RSF.getTableEntry(Table, i));
StringRef Name;
SmallString<20> IDStr;
raw_svector_ostream OS(IDStr);
@@ -1873,15 +1868,6 @@ void COFFDumper::printResourceDirectoryT
}
}
-Expected<const coff_resource_dir_entry &>
-COFFDumper::getResourceDirectoryTableEntry(const coff_resource_dir_table &Table,
- uint32_t Index) {
- if (Index >= (uint32_t)(Table.NumberOfNameEntries + Table.NumberOfIDEntries))
- return createError("can't get resource directory table entry");
- auto TablePtr = reinterpret_cast<const coff_resource_dir_entry *>(&Table + 1);
- return TablePtr[Index];
-}
-
void COFFDumper::printStackMap() const {
object::SectionRef StackMapSection;
for (auto Sec : Obj->sections()) {
More information about the llvm-commits
mailing list