diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-02-23 02:40:07 +0000 | 
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-02-23 02:40:07 +0000 | 
| commit | 0404ec839661c24af48cd49ef055e290cba67a79 (patch) | |
| tree | eefb7036b3d832517afadd0c20cb9a75df01db16 /compiler-rt | |
| parent | e9ca4af4c8b30854b54330257d7e4b3bba3ded9b (diff) | |
| download | bcm5719-llvm-0404ec839661c24af48cd49ef055e290cba67a79.tar.gz bcm5719-llvm-0404ec839661c24af48cd49ef055e290cba67a79.zip  | |
ubsan: Runtime handlers for array indexing checks.
llvm-svn: 175948
Diffstat (limited to 'compiler-rt')
| -rw-r--r-- | compiler-rt/lib/ubsan/lit_tests/Misc/bounds.cpp | 15 | ||||
| -rw-r--r-- | compiler-rt/lib/ubsan/ubsan_handlers.cc | 16 | ||||
| -rw-r--r-- | compiler-rt/lib/ubsan/ubsan_handlers.h | 9 | 
3 files changed, 40 insertions, 0 deletions
diff --git a/compiler-rt/lib/ubsan/lit_tests/Misc/bounds.cpp b/compiler-rt/lib/ubsan/lit_tests/Misc/bounds.cpp new file mode 100644 index 00000000000..07b30d384df --- /dev/null +++ b/compiler-rt/lib/ubsan/lit_tests/Misc/bounds.cpp @@ -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]' +} diff --git a/compiler-rt/lib/ubsan/ubsan_handlers.cc b/compiler-rt/lib/ubsan/ubsan_handlers.cc index 33e044dfe08..fa93b095d0b 100644 --- a/compiler-rt/lib/ubsan/ubsan_handlers.cc +++ b/compiler-rt/lib/ubsan/ubsan_handlers.cc @@ -183,6 +183,22 @@ void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(    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(); diff --git a/compiler-rt/lib/ubsan/ubsan_handlers.h b/compiler-rt/lib/ubsan/ubsan_handlers.h index d6a042481ff..5e237e1aa2d 100644 --- a/compiler-rt/lib/ubsan/ubsan_handlers.h +++ b/compiler-rt/lib/ubsan/ubsan_handlers.h @@ -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;  };  | 

