[PATCH] Runtime support for the indirect function call checker.

Peter Collingbourne peter at pcc.me.uk
Fri Aug 9 13:13:16 PDT 2013


    - Add a test

Hi rsmith,

http://llvm-reviews.chandlerc.com/D1339

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1339?vs=3314&id=3325#toc

Files:
  lib/ubsan/lit_tests/Misc/deduplication.cpp
  lib/ubsan/lit_tests/TypeCheck/function.cpp
  lib/ubsan/lit_tests/lit.cfg
  lib/ubsan/ubsan_handlers.cc
  lib/ubsan/ubsan_handlers.h

Index: lib/ubsan/lit_tests/Misc/deduplication.cpp
===================================================================
--- lib/ubsan/lit_tests/Misc/deduplication.cpp
+++ lib/ubsan/lit_tests/Misc/deduplication.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang -fsanitize=undefined %s -o %t && %t 2>&1 | FileCheck %s
+// RUN: %clang --driver-mode=g++ -fsanitize=undefined %s -o %t && %t 2>&1 | FileCheck %s
 // Verify deduplication works by ensuring only one diag is emitted.
 #include <limits.h>
 #include <stdio.h>
Index: lib/ubsan/lit_tests/TypeCheck/function.cpp
===================================================================
--- /dev/null
+++ lib/ubsan/lit_tests/TypeCheck/function.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang --driver-mode=g++ -fsanitize=address,function %s -O3 -o %t
+// RUN: %t 2>&1 | FileCheck %s
+
+#include <stdint.h>
+
+void f() {}
+
+void g(int x) {}
+
+int main(void) {
+  // CHECK: runtime error: call to function f() through pointer to incorrect function type 'void (*)(int)'
+  reinterpret_cast<void (*)(int)>(reinterpret_cast<uintptr_t>(f))(42);
+
+  // CHECK-NOT: runtime error: call to function g
+  reinterpret_cast<void (*)(int)>(reinterpret_cast<uintptr_t>(g))(42);
+}
Index: lib/ubsan/lit_tests/lit.cfg
===================================================================
--- lib/ubsan/lit_tests/lit.cfg
+++ lib/ubsan/lit_tests/lit.cfg
@@ -51,6 +51,9 @@
 # Define %clang substitution to use in test RUN lines.
 config.substitutions.append( ("%clang ", (" " + config.clang + " ")) )
 
+# Setup path to external LLVM symbolizer.
+config.environment['ASAN_SYMBOLIZER_PATH'] = config.llvm_symbolizer_path
+
 # Default test suffixes.
 config.suffixes = ['.c', '.cc', '.cpp']
 
Index: lib/ubsan/ubsan_handlers.cc
===================================================================
--- lib/ubsan/ubsan_handlers.cc
+++ lib/ubsan/ubsan_handlers.cc
@@ -15,6 +15,7 @@
 #include "ubsan_diag.h"
 
 #include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_symbolizer.h"
 
 using namespace __sanitizer;
 using namespace __ubsan;
@@ -258,3 +259,30 @@
     << Value(Data->Type, Val) << Data->Type;
   Die();
 }
+
+void __ubsan::__ubsan_handle_function_type_mismatch(
+    FunctionTypeMismatchData *Data,
+    ValueHandle Function) {
+  Location Loc;
+
+  AddressInfo Info;
+  if (!SymbolizeCode(Function, &Info, 1))
+    Info.file = Info.function = 0;
+
+  const char *FName = Info.function ? Info.function : "(unknown)";
+  Diag(Data->Loc, DL_Error,
+       "call to function %0 through pointer to incorrect function type %1")
+    << FName << Data->Type;
+  if (Info.file) {
+    Diag(SourceLocation(Info.file, Info.line, Info.column), DL_Note,
+         "%0 defined here")
+      << (Info.function ? Info.function : "(unknown)") << Data->Type;
+  }
+}
+
+void __ubsan::__ubsan_handle_function_type_mismatch_abort(
+    FunctionTypeMismatchData *Data,
+    ValueHandle Function) {
+  __ubsan_handle_function_type_mismatch(Data, Function);
+  Die();
+}
Index: lib/ubsan/ubsan_handlers.h
===================================================================
--- lib/ubsan/ubsan_handlers.h
+++ lib/ubsan/ubsan_handlers.h
@@ -112,6 +112,15 @@
 /// \brief Handle a load of an invalid value for the type.
 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
 
+struct FunctionTypeMismatchData {
+  SourceLocation Loc;
+  const TypeDescriptor &Type;
+};
+
+RECOVERABLE(function_type_mismatch,
+            FunctionTypeMismatchData *Data,
+            ValueHandle Val)
+
 }
 
 #endif // UBSAN_HANDLERS_H
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1339.2.patch
Type: text/x-patch
Size: 3548 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130809/55cdff21/attachment.bin>


More information about the llvm-commits mailing list