[PATCH] D25279: [ELF] - Do not crash on large output.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 7 03:52:53 PDT 2016
grimar updated the summary for this revision.
grimar updated this revision to Diff 73904.
grimar added a comment.
- Addressed review comments.
https://reviews.llvm.org/D25279
Files:
ELF/Writer.cpp
test/ELF/invalid/Inputs/too-large-output-i386.elf
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1162,6 +1162,39 @@
}
}
+template <class T> class CheckedType {
+ T Val;
+
+public:
+ CheckedType(T Val) : Val(Val){};
+
+ operator uint64_t() { return Val; }
+ operator uint32_t() {
+ if (Val > UINT32_MAX)
+ fatal("checked type operation overflow");
+ return Val;
+ }
+
+ template <class T2> CheckedType operator+(T2 R) {
+ if (R > getMax() || (this->Val > getMax() - R))
+ fatal("checked type operation overflow");
+ return this->Val + R;
+ }
+
+ template <class T2> CheckedType operator-(T2 R) {
+ if (R > this->Val)
+ fatal("checked type operation overflow");
+ return this->Val - R;
+ }
+
+private:
+ T getMax() { return std::numeric_limits<typename T>::max(); }
+};
+
+template <class T> CheckedType<T> makeChecked(T Val) {
+ return CheckedType<T>(Val);
+}
+
// Adjusts the file alignment for a given output section and returns
// its new file offset. The file offset must be the same with its
// virtual address (modulo the page size) so that the loader can load
@@ -1182,7 +1215,7 @@
// this formula: Off2 = Off1 + (VA2 - VA1).
if (Sec == First)
return alignTo(Off, Target->MaxPageSize, Sec->getVA());
- return First->getFileOffset() + Sec->getVA() - First->getVA();
+ return makeChecked(First->getFileOffset()) + Sec->getVA() - First->getVA();
}
template <class ELFT, class uintX_t>
@@ -1194,7 +1227,7 @@
Off = getFileAlignment<ELFT>(Off, Sec);
Sec->setFileOffset(Off);
- Off += Sec->getSize();
+ Off = makeChecked(Off) + Sec->getSize();
}
template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25279.73904.patch
Type: text/x-patch
Size: 1733 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161007/d137df5f/attachment.bin>
More information about the llvm-commits
mailing list