[llvm] 0498415 - Fix overflow bug impacting 32-bit testing

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 6 09:17:15 PDT 2022


Author: Chris Bieneman
Date: 2022-06-06T11:16:16-05:00
New Revision: 0498415f1d6ac0bfbda72486505c11a7b3d464c4

URL: https://github.com/llvm/llvm-project/commit/0498415f1d6ac0bfbda72486505c11a7b3d464c4
DIFF: https://github.com/llvm/llvm-project/commit/0498415f1d6ac0bfbda72486505c11a7b3d464c4.diff

LOG: Fix overflow bug impacting 32-bit testing

This test was failing on 32-bit arm builders due to an interger
overflow. This changes the math to avoid overflow and should resolve
the test failure.

Added: 
    

Modified: 
    llvm/lib/Object/DXContainer.cpp
    llvm/unittests/Object/DXContainerTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index ae3d2f00c2e4d..9fda608eee0b5 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -60,7 +60,12 @@ Error DXContainer::parsePartOffsets() {
     if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset))
       return Err;
     Current += sizeof(uint32_t);
-    if (PartOffset + sizeof(dxbc::PartHeader) > Data.getBufferSize())
+    // We need to ensure that each part offset leaves enough space for a part
+    // header. To prevent overflow, we subtract the part header size from the
+    // buffer size, rather than adding to the offset. Since the file header is
+    // larger than the part header we can't reach this code unless the buffer
+    // is larger than the part header, so this can't underflow.
+    if (PartOffset > Data.getBufferSize() - sizeof(dxbc::PartHeader))
       return parseFailed("Part offset points beyond boundary of the file");
     PartOffsets.push_back(PartOffset);
   }

diff  --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 084b727476845..14fb4b82d7c58 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -70,11 +70,7 @@ TEST(DXCFile, ParsePartMissingOffsets) {
       FailedWithMessage("Reading structure out of file bounds"));
 }
 
-#if defined(__arm__)
-TEST(DXCFile, DISABLED_ParsePartInvalidOffsets) {
-#else
 TEST(DXCFile, ParsePartInvalidOffsets) {
-#endif
   uint8_t Buffer[] = {
       0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,


        


More information about the llvm-commits mailing list