<div dir="ltr">Hi Stephan,<div><br></div><div>Is there a more principled way to use sanitizer_platform.h from the test?</div><div><br></div><div>We are running clang tests in a distributed environment and don't have a full source checkout when running the tests.<br></div><div>One way to make it work for us is to make the test depend on the build target producing the headers and configure the compiler being run (by passing the -I flag or something similar).</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 16, 2019 at 8:23 AM Stephan Bergmann via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: sberg<br>
Date: Mon Jul 15 23:23:27 2019<br>
New Revision: 366186<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=366186&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=366186&view=rev</a><br>
Log:<br>
Finish "Adapt -fsanitize=function to SANITIZER_NON_UNIQUE_TYPEINFO"<br>
<br>
i.e., recent 5745eccef54ddd3caca278d1d292a88b2281528b:<br>
<br>
* Bump the function_type_mismatch handler version, as its signature has changed.<br>
<br>
* The function_type_mismatch handler can return successfully now, so<br>
  SanitizerKind::Function must be AlwaysRecoverable (like for<br>
  SanitizerKind::Vptr).<br>
<br>
* But the minimal runtime would still unconditionally treat a call to the<br>
  function_type_mismatch handler as failure, so disallow -fsanitize=function in<br>
  combination with -fsanitize-minimal-runtime (like it was already done for<br>
  -fsanitize=vptr).<br>
<br>
* Add tests.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D61479" rel="noreferrer" target="_blank">https://reviews.llvm.org/D61479</a><br>
<br>
Modified:<br>
    compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc<br>
    compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h<br>
    compiler-rt/trunk/lib/ubsan/ubsan_interface.inc<br>
    compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp<br>
<br>
Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc?rev=366186&r1=366185&r2=366186&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc?rev=366186&r1=366185&r2=366186&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc (original)<br>
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc Mon Jul 15 23:23:27 2019<br>
@@ -185,18 +185,17 @@ static bool handleFunctionTypeMismatch(F<br>
   return true;<br>
 }<br>
<br>
-void __ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,<br>
-                                           ValueHandle Function,<br>
-                                           ValueHandle calleeRTTI,<br>
-                                           ValueHandle fnRTTI) {<br>
+void __ubsan_handle_function_type_mismatch_v1(FunctionTypeMismatchData *Data,<br>
+                                              ValueHandle Function,<br>
+                                              ValueHandle calleeRTTI,<br>
+                                              ValueHandle fnRTTI) {<br>
   GET_REPORT_OPTIONS(false);<br>
   handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts);<br>
 }<br>
<br>
-void __ubsan_handle_function_type_mismatch_abort(FunctionTypeMismatchData *Data,<br>
-                                                 ValueHandle Function,<br>
-                                                 ValueHandle calleeRTTI,<br>
-                                                 ValueHandle fnRTTI) {<br>
+void __ubsan_handle_function_type_mismatch_v1_abort(<br>
+    FunctionTypeMismatchData *Data, ValueHandle Function,<br>
+    ValueHandle calleeRTTI, ValueHandle fnRTTI) {<br>
   GET_REPORT_OPTIONS(true);<br>
   if (handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts))<br>
     Die();<br>
<br>
Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h?rev=366186&r1=366185&r2=366186&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h?rev=366186&r1=366185&r2=366186&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h (original)<br>
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.h Mon Jul 15 23:23:27 2019<br>
@@ -40,14 +40,15 @@ struct FunctionTypeMismatchData {<br>
 };<br>
<br>
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void<br>
-__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,<br>
-                                      ValueHandle Val, ValueHandle calleeRTTI,<br>
-                                      ValueHandle fnRTTI);<br>
+__ubsan_handle_function_type_mismatch_v1(FunctionTypeMismatchData *Data,<br>
+                                         ValueHandle Val,<br>
+                                         ValueHandle calleeRTTI,<br>
+                                         ValueHandle fnRTTI);<br>
 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void<br>
-__ubsan_handle_function_type_mismatch_abort(FunctionTypeMismatchData *Data,<br>
-                                            ValueHandle Val,<br>
-                                            ValueHandle calleeRTTI,<br>
-                                            ValueHandle fnRTTI);<br>
+__ubsan_handle_function_type_mismatch_v1_abort(FunctionTypeMismatchData *Data,<br>
+                                               ValueHandle Val,<br>
+                                               ValueHandle calleeRTTI,<br>
+                                               ValueHandle fnRTTI);<br>
 }<br>
<br>
 #endif // UBSAN_HANDLERS_H<br>
<br>
Modified: compiler-rt/trunk/lib/ubsan/ubsan_interface.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_interface.inc?rev=366186&r1=366185&r2=366186&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_interface.inc?rev=366186&r1=366185&r2=366186&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/ubsan/ubsan_interface.inc (original)<br>
+++ compiler-rt/trunk/lib/ubsan/ubsan_interface.inc Mon Jul 15 23:23:27 2019<br>
@@ -21,8 +21,8 @@ INTERFACE_FUNCTION(__ubsan_handle_dynami<br>
 INTERFACE_FUNCTION(__ubsan_handle_dynamic_type_cache_miss_abort)<br>
 INTERFACE_FUNCTION(__ubsan_handle_float_cast_overflow)<br>
 INTERFACE_FUNCTION(__ubsan_handle_float_cast_overflow_abort)<br>
-INTERFACE_FUNCTION(__ubsan_handle_function_type_mismatch)<br>
-INTERFACE_FUNCTION(__ubsan_handle_function_type_mismatch_abort)<br>
+INTERFACE_FUNCTION(__ubsan_handle_function_type_mismatch_v1)<br>
+INTERFACE_FUNCTION(__ubsan_handle_function_type_mismatch_v1_abort)<br>
 INTERFACE_FUNCTION(__ubsan_handle_implicit_conversion)<br>
 INTERFACE_FUNCTION(__ubsan_handle_implicit_conversion_abort)<br>
 INTERFACE_FUNCTION(__ubsan_handle_invalid_builtin)<br>
<br>
Modified: compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp?rev=366186&r1=366185&r2=366186&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp?rev=366186&r1=366185&r2=366186&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp (original)<br>
+++ compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/Function/function.cpp Mon Jul 15 23:23:27 2019<br>
@@ -1,11 +1,53 @@<br>
-// RUN: %clangxx -std=c++17 -fsanitize=function %s -O3 -g -o %t<br>
-// RUN: %run %t 2>&1 | FileCheck %s<br>
+// RUN: %clangxx -DDETERMINE_UNIQUE %s -o %t-unique<br>
+// RUN: %clangxx -std=c++17 -fsanitize=function %s -O3 -g -DSHARED_LIB -fPIC -shared -o %t-so.so<br>
+// RUN: %clangxx -std=c++17 -fsanitize=function %s -O3 -g -o %t %t-so.so<br>
+// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK $(%run %t-unique UNIQUE)<br>
 // Verify that we can disable symbolization if needed:<br>
-// RUN: %env_ubsan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOSYM<br>
+// RUN: %env_ubsan_opts=symbolize=0 %run %t 2>&1 | FileCheck %s --check-prefix=NOSYM $(%run %t-unique NOSYM-UNIQUE)<br>
 // XFAIL: windows-msvc<br>
 // Unsupported function flag<br>
 // UNSUPPORTED: openbsd<br>
<br>
+#ifdef DETERMINE_UNIQUE<br>
+<br>
+#include <iostream><br>
+<br>
+#include "../../../../../lib/sanitizer_common/sanitizer_platform.h"<br>
+<br>
+int main(int, char **argv) {<br>
+  if (!SANITIZER_NON_UNIQUE_TYPEINFO)<br>
+    std::cout << "--check-prefix=" << argv[1];<br>
+}<br>
+<br>
+#else<br>
+<br>
+struct Shared {};<br>
+using FnShared = void (*)(Shared *);<br>
+FnShared getShared();<br>
+<br>
+struct __attribute__((visibility("hidden"))) Hidden {};<br>
+using FnHidden = void (*)(Hidden *);<br>
+FnHidden getHidden();<br>
+<br>
+namespace {<br>
+struct Private {};<br>
+} // namespace<br>
+using FnPrivate = void (*)(void *);<br>
+FnPrivate getPrivate();<br>
+<br>
+#ifdef SHARED_LIB<br>
+<br>
+void fnShared(Shared *) {}<br>
+FnShared getShared() { return fnShared; }<br>
+<br>
+void fnHidden(Hidden *) {}<br>
+FnHidden getHidden() { return fnHidden; }<br>
+<br>
+void fnPrivate(Private *) {}<br>
+FnPrivate getPrivate() { return reinterpret_cast<FnPrivate>(fnPrivate); }<br>
+<br>
+#else<br>
+<br>
 #include <stdint.h><br>
<br>
 void f() {}<br>
@@ -64,12 +106,31 @@ void check_noexcept_calls() {<br>
   p2(0);<br>
 }<br>
<br>
+void check_cross_dso() {<br>
+  getShared()(nullptr);<br>
+<br>
+  // UNIQUE: function.cpp:[[@LINE+2]]:3: runtime error: call to function fnHidden(Hidden*) through pointer to incorrect function type 'void (*)(Hidden *)'<br>
+  // NOSYM-UNIQUE: function.cpp:[[@LINE+1]]:3: runtime error: call to function (unknown) through pointer to incorrect function type 'void (*)(Hidden *)'<br>
+  getHidden()(nullptr);<br>
+<br>
+  // TODO: Unlike GCC, Clang fails to prefix the typeinfo name for the function<br>
+  // type with "*", so this erroneously only fails for "*UNIQUE":<br>
+  // UNIQUE: function.cpp:[[@LINE+2]]:3: runtime error: call to function fnPrivate((anonymous namespace)::Private*) through pointer to incorrect function type 'void (*)((anonymous namespace)::Private *)'<br>
+  // NOSYM-UNIQUE: function.cpp:[[@LINE+1]]:3: runtime error: call to function (unknown) through pointer to incorrect function type 'void (*)((anonymous namespace)::Private *)'<br>
+  reinterpret_cast<void (*)(Private *)>(getPrivate())(nullptr);<br>
+}<br>
+<br>
 int main(void) {<br>
   make_valid_call();<br>
   make_invalid_call();<br>
   check_noexcept_calls();<br>
+  check_cross_dso();<br>
   // Check that no more errors will be printed.<br>
   // CHECK-NOT: runtime error: call to function<br>
   // NOSYM-NOT: runtime error: call to function<br>
   make_invalid_call();<br>
 }<br>
+<br>
+#endif<br>
+<br>
+#endif<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Ilya Biryukov</div></div></div></div></div>