[PATCH] D25279: [ELF] - Do not crash on large output.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 5 07:22:06 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, davide.
grimar added subscribers: llvm-commits, grimar, evgeny777.

I wound at least 2 possible situation when we crash on large output:

1. Imagine we have large section sizes. It s simulated in a testcase by providing alignment of 0xFFFFFFFF for 32 bit target.

Then overflow may happen during assigning offsets.

2. On 32 bit configuration, size_t is 32 bit value. When we have FileSize larger than that, we will crash. Unfortunately I do not know way how

to force test to run only on 32/64 bits hosts, so did not prepare testcase for this.


https://reviews.llvm.org/D25279

Files:
  ELF/Writer.cpp
  test/ELF/invalid/too-large-output-i386.s


Index: test/ELF/invalid/too-large-output-i386.s
===================================================================
--- test/ELF/invalid/too-large-output-i386.s
+++ test/ELF/invalid/too-large-output-i386.s
@@ -0,0 +1,7 @@
+# REQUIRES: x86
+
+## too-large-output-i386.elf contains section with address align.
+## of 0xFFFFFFFF. Total file size calculation overflows because of that.
+# RUN: not ld.lld -shared %S/Inputs/too-large-output-i386.elf -o %t 2>&1 \
+# RUN:  | FileCheck %s
+# CHECK: attemp to set offset for .text1 failed: output is too large
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -1216,9 +1216,13 @@
     return;
   }
 
+  uintX_t T = Off;
   Off = getFileAlignment<ELFT>(Off, Sec);
   Sec->setFileOffset(Off);
   Off += Sec->getSize();
+  if (T > Off)
+    fatal("attemp to set offset for " + Sec->getName() +
+          " failed: output is too large");
 }
 
 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
@@ -1381,6 +1385,8 @@
 }
 
 template <class ELFT> void Writer<ELFT>::openFile() {
+  if (FileSize > SIZE_MAX)
+    fatal("output file size is too large for this configuration");
   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
       FileOutputBuffer::create(Config->OutputFile, FileSize,
                                FileOutputBuffer::F_executable);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25279.73644.patch
Type: text/x-patch
Size: 1426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161005/f6e4f91d/attachment.bin>


More information about the llvm-commits mailing list