[PATCH] Runtime support for the indirect function call checker.
Peter Collingbourne
peter at pcc.me.uk
Thu Oct 10 19:08:23 PDT 2013
- The test depends on the asan runtime libraries
Hi rsmith,
http://llvm-reviews.chandlerc.com/D1339
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D1339?vs=4310&id=4824#toc
Files:
lib/asan/CMakeLists.txt
lib/ubsan/lit_tests/CMakeLists.txt
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/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/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
@@ -52,6 +52,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;
@@ -259,3 +260,30 @@
__ubsan_handle_load_invalid_value(Data, Val);
Die();
}
+
+void __ubsan::__ubsan_handle_function_type_mismatch(
+ FunctionTypeMismatchData *Data,
+ ValueHandle Function) {
+ Location Loc;
+
+ AddressInfo Info;
+ if (!getSymbolizer()->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.4.patch
Type: text/x-patch
Size: 4370 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131010/dfc54eaf/attachment.bin>
More information about the llvm-commits
mailing list