[compiler-rt] r307956 - [ubsan] Teach the pointer overflow check that "p - <unsigned> <= p" (compiler-rt)

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 13 13:55:42 PDT 2017


Author: vedantk
Date: Thu Jul 13 13:55:41 2017
New Revision: 307956

URL: http://llvm.org/viewvc/llvm-project?rev=307956&view=rev
Log:
[ubsan] Teach the pointer overflow check that "p - <unsigned> <= p" (compiler-rt)

Compiler-rt changes associated with: D34121

Modified:
    compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc
    compiler-rt/trunk/test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp

Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc?rev=307956&r1=307955&r2=307956&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc Thu Jul 13 13:55:41 2017
@@ -573,14 +573,19 @@ static void handlePointerOverflowImpl(Po
 
   ScopedReport R(Opts, Loc, ET);
 
-  if ((sptr(Base) >= 0) == (sptr(Result) >= 0))
-    Diag(Loc, DL_Error, "unsigned pointer index expression result is %0, "
-                        "preceding its base %1")
-        << (void *)Result << (void *)Base;
-  else
+  if ((sptr(Base) >= 0) == (sptr(Result) >= 0)) {
+    if (Base > Result)
+      Diag(Loc, DL_Error, "addition of unsigned offset to %0 overflowed to %1")
+          << (void *)Base << (void *)Result;
+    else
+      Diag(Loc, DL_Error,
+           "subtraction of unsigned offset from %0 overflowed to %1")
+          << (void *)Base << (void *)Result;
+  } else {
     Diag(Loc, DL_Error,
          "pointer index expression with base %0 overflowed to %1")
         << (void *)Base << (void *)Result;
+  }
 }
 
 void __ubsan::__ubsan_handle_pointer_overflow(PointerOverflowData *Data,

Modified: compiler-rt/trunk/test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp?rev=307956&r1=307955&r2=307956&view=diff
==============================================================================
--- compiler-rt/trunk/test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp (original)
+++ compiler-rt/trunk/test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp Thu Jul 13 13:55:41 2017
@@ -1,13 +1,20 @@
-// RUN: %clangxx -fsanitize=pointer-overflow %s -o %t
+// RUN: %clangxx -std=c++11 -fsanitize=pointer-overflow %s -o %t
 // RUN: %t 2>&1 | FileCheck %s
 
 int main(int argc, char *argv[]) {
   char c;
   char *p = &c;
-  unsigned long long offset = -1;
+  unsigned long long neg_1 = -1;
 
-  // CHECK: unsigned-index-expression.cpp:[[@LINE+1]]:15: runtime error: unsigned pointer index expression result is 0x{{.*}}, preceding its base 0x{{.*}}
-  char *q = p + offset;
+  // CHECK: unsigned-index-expression.cpp:[[@LINE+1]]:15: runtime error: addition of unsigned offset to 0x{{.*}} overflowed to 0x{{.*}}
+  char *q = p + neg_1;
+
+  // CHECK: unsigned-index-expression.cpp:[[@LINE+1]]:16: runtime error: subtraction of unsigned offset from 0x{{.*}} overflowed to 0x{{.*}}
+  char *q1 = p - neg_1;
+
+  // CHECK: unsigned-index-expression.cpp:[[@LINE+2]]:16: runtime error: pointer index expression with base 0x{{0*}} overflowed to 0x{{.*}}
+  char *n = nullptr;
+  char *q2 = n - 1ULL;
 
   return 0;
 }




More information about the llvm-commits mailing list