[compiler-rt] r175948 - ubsan: Runtime handlers for array indexing checks.

Richard Smith richard-llvm at metafoo.co.uk
Fri Feb 22 18:40:07 PST 2013


Author: rsmith
Date: Fri Feb 22 20:40:07 2013
New Revision: 175948

URL: http://llvm.org/viewvc/llvm-project?rev=175948&view=rev
Log:
ubsan: Runtime handlers for array indexing checks.

Added:
    compiler-rt/trunk/lib/ubsan/lit_tests/Misc/bounds.cpp
Modified:
    compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc
    compiler-rt/trunk/lib/ubsan/ubsan_handlers.h

Added: compiler-rt/trunk/lib/ubsan/lit_tests/Misc/bounds.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/lit_tests/Misc/bounds.cpp?rev=175948&view=auto
==============================================================================
--- compiler-rt/trunk/lib/ubsan/lit_tests/Misc/bounds.cpp (added)
+++ compiler-rt/trunk/lib/ubsan/lit_tests/Misc/bounds.cpp Fri Feb 22 20:40:07 2013
@@ -0,0 +1,15 @@
+// RUN: %clang -fsanitize=bounds %s -O3 -o %T/bounds.exe
+// RUN: %T/bounds.exe 0 0 0
+// RUN: %T/bounds.exe 1 2 3
+// RUN: %T/bounds.exe 2 0 0 2>&1 | FileCheck %s --check-prefix=CHECK-A-2
+// RUN: %T/bounds.exe 0 3 0 2>&1 | FileCheck %s --check-prefix=CHECK-B-3
+// RUN: %T/bounds.exe 0 0 4 2>&1 | FileCheck %s --check-prefix=CHECK-C-4
+
+int main(int argc, char **argv) {
+  int arr[2][3][4] = {};
+
+  return arr[argv[1][0] - '0'][argv[2][0] - '0'][argv[3][0] - '0'];
+  // CHECK-A-2: bounds.cpp:11:10: runtime error: index 2 out of bounds for type 'int [2][3][4]'
+  // CHECK-B-3: bounds.cpp:11:10: runtime error: index 3 out of bounds for type 'int [3][4]'
+  // CHECK-C-4: bounds.cpp:11:10: runtime error: index 4 out of bounds for type 'int [4]'
+}

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=175948&r1=175947&r2=175948&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc Fri Feb 22 20:40:07 2013
@@ -183,6 +183,22 @@ void __ubsan::__ubsan_handle_shift_out_o
   Die();
 }
 
+void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
+                                           ValueHandle Index) {
+  SourceLocation Loc = Data->Loc.acquire();
+  if (Loc.isDisabled())
+    return;
+
+  Value IndexVal(Data->IndexType, Index);
+  Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
+    << IndexVal << Data->ArrayType;
+}
+void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
+                                                 ValueHandle Index) {
+  __ubsan_handle_out_of_bounds(Data, Index);
+  Die();
+}
+
 void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
   Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
   Die();

Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers.h?rev=175948&r1=175947&r2=175948&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers.h (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers.h Fri Feb 22 20:40:07 2013
@@ -67,6 +67,15 @@ struct ShiftOutOfBoundsData {
 RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,
             ValueHandle LHS, ValueHandle RHS)
 
+struct OutOfBoundsData {
+  SourceLocation Loc;
+  const TypeDescriptor &ArrayType;
+  const TypeDescriptor &IndexType;
+};
+
+/// \brief Handle an array index out of bounds error.
+RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)
+
 struct UnreachableData {
   SourceLocation Loc;
 };





More information about the llvm-commits mailing list