[PATCH] D33911: [ubsan] Detect invalid unsigned pointer index expression (compiler-rt)

Vedant Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 5 17:55:30 PDT 2017


vsk updated this revision to Diff 101496.
vsk added a comment.

- Avoid implementation-defined behavior in the test.
- Fix the signed-ness check: to check that the sign bit is off, we need 'x >= 0'. Caught by running check-ubsan.


https://reviews.llvm.org/D33911

Files:
  lib/ubsan/ubsan_handlers.cc
  test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp


Index: test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp
===================================================================
--- /dev/null
+++ test/ubsan/TestCases/Pointer/unsigned-index-expression.cpp
@@ -0,0 +1,13 @@
+// RUN: %clangxx -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;
+
+  // 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;
+
+  return 0;
+}
Index: lib/ubsan/ubsan_handlers.cc
===================================================================
--- lib/ubsan/ubsan_handlers.cc
+++ lib/ubsan/ubsan_handlers.cc
@@ -566,8 +566,14 @@
 
   ScopedReport R(Opts, Loc, ET);
 
-  Diag(Loc, DL_Error, "pointer index expression with base %0 overflowed to %1")
-    << (void *)Base << (void*)Result;
+  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
+    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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33911.101496.patch
Type: text/x-patch
Size: 1406 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170606/f210639a/attachment.bin>


More information about the llvm-commits mailing list