r320978 - No -fsanitize=function warning when calling noexcept function through non-noexcept pointer in C++17

Stephan Bergmann via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 18 05:05:42 PST 2017


Author: sberg
Date: Mon Dec 18 05:05:42 2017
New Revision: 320978

URL: http://llvm.org/viewvc/llvm-project?rev=320978&view=rev
Log:
No -fsanitize=function warning when calling noexcept function through non-noexcept pointer in C++17

As discussed in the mail thread <https://groups.google.com/a/isocpp.org/forum/
#!topic/std-discussion/T64_dW3WKUk> "Calling noexcept function throug non-
noexcept pointer is undefined behavior?", such a call should not be UB.
However, Clang currently warns about it.

There is no cheap check whether two function type_infos only differ in noexcept,
so pass those two type_infos as additional data to the function_type_mismatch
handler (with the optimization of passing a null "static callee type" info when
that is already noexcept, so the additional check can be avoided anyway).  For
the Itanium ABI (which appears to be the only one that happens to be used on
platforms that support -fsanitize=function, and which appears to only record
noexcept information for pointer-to-function type_infos, not for function
type_infos themselves), we then need to check the mangled names for occurrence
of "Do" representing "noexcept".

This is the cfe part of a patch covering both cfe and compiler-rt.

Differential Revision: https://reviews.llvm.org/D40720

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=320978&r1=320977&r2=320978&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Mon Dec 18 05:05:42 2017
@@ -4504,10 +4504,14 @@ RValue CodeGenFunction::EmitCall(QualTyp
           Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
       llvm::Constant *StaticData[] = {
         EmitCheckSourceLocation(E->getLocStart()),
-        EmitCheckTypeDescriptor(CalleeType)
+        EmitCheckTypeDescriptor(CalleeType),
+        cast<FunctionProtoType>(FnType)->isNothrow(getContext())
+          ? llvm::Constant::getNullValue(FTRTTIConst->getType())
+          : FTRTTIConst
       };
       EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function),
-                SanitizerHandler::FunctionTypeMismatch, StaticData, CalleePtr);
+                SanitizerHandler::FunctionTypeMismatch, StaticData,
+                {CalleePtr, CalleeRTTI});
 
       Builder.CreateBr(Cont);
       EmitBlock(Cont);




More information about the cfe-commits mailing list