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

Peter Collingbourne peter at pcc.me.uk
Sat Oct 19 19:21:10 PDT 2013


    - I decided it would be better to use the function pointer than adjusting for prefix data

Hi rsmith,

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

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

Files:
  lib/asan/CMakeLists.txt
  lib/ubsan/lit_tests/CMakeLists.txt
  lib/ubsan/lit_tests/TypeCheck/function.cpp
  lib/ubsan/lit_tests/TypeCheck/vptr.cpp
  lib/ubsan/lit_tests/lit.cfg
  lib/ubsan/ubsan_diag.cc
  lib/ubsan/ubsan_diag.h
  lib/ubsan/ubsan_handlers.cc
  lib/ubsan/ubsan_handlers.h

Index: lib/asan/CMakeLists.txt
===================================================================
--- lib/asan/CMakeLists.txt
+++ lib/asan/CMakeLists.txt
@@ -137,6 +137,8 @@
 
 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
 
+add_custom_target(asan DEPENDS asan_blacklist ${ASAN_RUNTIME_LIBRARIES})
+
 if(LLVM_INCLUDE_TESTS)
   add_subdirectory(tests)
 endif()
Index: lib/ubsan/lit_tests/CMakeLists.txt
===================================================================
--- lib/ubsan/lit_tests/CMakeLists.txt
+++ lib/ubsan/lit_tests/CMakeLists.txt
@@ -8,6 +8,7 @@
   # working binaries.
   set(UBSAN_TEST_DEPS
     ${SANITIZER_COMMON_LIT_TEST_DEPS}
+    asan
     ${UBSAN_RUNTIME_LIBRARIES})
   set(UBSAN_TEST_PARAMS
     ubsan_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
Index: lib/ubsan/lit_tests/TypeCheck/function.cpp
===================================================================
--- /dev/null
+++ lib/ubsan/lit_tests/TypeCheck/function.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang -fsanitize=address,function %s -O3 -g -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)'
+  // CHECK-NEXT: function.cpp:6: note: f() defined here
+  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/TypeCheck/vptr.cpp
===================================================================
--- lib/ubsan/lit_tests/TypeCheck/vptr.cpp
+++ lib/ubsan/lit_tests/TypeCheck/vptr.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang --driver-mode=g++ -fsanitize=vptr %s -O3 -o %t
+// RUN: %clang -fsanitize=vptr %s -O3 -o %t
 // RUN: %t rT && %t mT && %t fT && %t cT
 // RUN: %t rU && %t mU && %t fU && %t cU
 // RUN: %t rS && %t rV && %t oV
Index: lib/ubsan/lit_tests/lit.cfg
===================================================================
--- lib/ubsan/lit_tests/lit.cfg
+++ lib/ubsan/lit_tests/lit.cfg
@@ -50,7 +50,11 @@
   raise SystemExit
 
 # Define %clang substitution to use in test RUN lines.
-config.substitutions.append( ("%clang ", (" " + config.clang + " ")) )
+config.substitutions.append( ("%clang ", (" " + config.clang +
+                                          " --driver-mode=g++ ")) )
+
+# 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_diag.cc
===================================================================
--- lib/ubsan/ubsan_diag.cc
+++ lib/ubsan/ubsan_diag.cc
@@ -26,12 +26,21 @@
     return Location();
 
   uptr Loc = StackTrace::GetPreviousInstructionPc(CallerLoc);
+  return getFunctionLocation(Loc, 0);
+}
+
+Location __ubsan::getFunctionLocation(uptr Loc, const char **FName) {
+  if (!Loc)
+    return Location();
 
   AddressInfo Info;
   if (!getSymbolizer()->SymbolizeCode(Loc, &Info, 1) ||
       !Info.module || !*Info.module)
     return Location(Loc);
 
+  if (FName && Info.function)
+    *FName = Info.function;
+
   if (!Info.file)
     return ModuleLocation(Info.module, Info.module_offset);
 
Index: lib/ubsan/ubsan_diag.h
===================================================================
--- lib/ubsan/ubsan_diag.h
+++ lib/ubsan/ubsan_diag.h
@@ -80,6 +80,12 @@
 /// an invalid location or a module location for the caller.
 Location getCallerLocation(uptr CallerLoc = GET_CALLER_PC());
 
+/// Try to obtain a location for the given function pointer. This might fail,
+/// and produce either an invalid location or a module location for the caller.
+/// If FName is non-null and the name of the function is known, set *FName to
+/// the function name, otherwise *FName is unchanged.
+Location getFunctionLocation(uptr Loc, const char **FName);
+
 /// A diagnostic severity level.
 enum DiagLevel {
   DL_Error, ///< An error.
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;
@@ -259,3 +260,23 @@
   __ubsan_handle_load_invalid_value(Data, Val);
   Die();
 }
+
+void __ubsan::__ubsan_handle_function_type_mismatch(
+    FunctionTypeMismatchData *Data,
+    ValueHandle Function) {
+  const char *FName = "(unknown)";
+
+  Location Loc = getFunctionLocation(Function, &FName);
+
+  Diag(Data->Loc, DL_Error,
+       "call to function %0 through pointer to incorrect function type %1")
+    << FName << Data->Type;
+  Diag(Loc, DL_Note, "%0 defined here") << FName;
+}
+
+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,17 @@
 /// \brief Handle a load of an invalid value for the type.
 RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)
 
+#if defined(__i386__) || defined(__x86_64__)
+struct FunctionTypeMismatchData {
+  SourceLocation Loc;
+  const TypeDescriptor &Type;
+};
+
+RECOVERABLE(function_type_mismatch,
+            FunctionTypeMismatchData *Data,
+            ValueHandle Val)
+
 }
+#endif
 
 #endif // UBSAN_HANDLERS_H
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1339.6.patch
Type: text/x-patch
Size: 5750 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131019/20c0ecd1/attachment.bin>


More information about the llvm-commits mailing list