diff options
author | serge-sans-paille <sguelton@redhat.com> | 2019-12-05 17:08:10 +0100 |
---|---|---|
committer | serge-sans-paille <sguelton@redhat.com> | 2020-01-10 09:44:20 +0100 |
commit | 921f871ac438175ca8fcfcafdfcfac4d7ddf3905 (patch) | |
tree | 7e7d223ab1fb1ec6111231b9151f0c5629acac02 /clang | |
parent | 164da673009ba6c100ce45b6fa9a5dfd3b0b8e38 (diff) | |
download | bcm5719-llvm-921f871ac438175ca8fcfcafdfcfac4d7ddf3905.tar.gz bcm5719-llvm-921f871ac438175ca8fcfcafdfcfac4d7ddf3905.zip |
Allow system header to provide their own implementation of some builtin
If a system header provides an (inline) implementation of some of their
function, clang still matches on the function name and generate the appropriate
llvm builtin, e.g. memcpy. This behavior is in line with glibc recommendation «
users may not provide their own version of symbols » but doesn't account for the
fact that glibc itself can provide inline version of some functions.
It is the case for the memcpy function when -D_FORTIFY_SOURCE=1 is on. In that
case an inline version of memcpy calls __memcpy_chk, a function that performs
extra runtime checks. Clang currently ignores the inline version and thus
provides no runtime check.
This code fixes the issue by detecting functions whose name is a builtin name
but also have an inline implementation.
Differential Revision: https://reviews.llvm.org/D71082
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/AST/Decl.h | 3 | ||||
-rw-r--r-- | clang/lib/AST/Decl.cpp | 8 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExpr.cpp | 9 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 5 | ||||
-rw-r--r-- | clang/test/CodeGen/memcpy-nobuiltin.c | 15 | ||||
-rw-r--r-- | clang/test/CodeGen/memcpy-nobuiltin.inc | 19 |
6 files changed, 58 insertions, 1 deletions
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 002d1434b1c..aa5cfb6e2c8 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2309,6 +2309,9 @@ public: /// true through IsAligned. bool isReplaceableGlobalAllocationFunction(bool *IsAligned = nullptr) const; + /// Determine if this function provides an inline implementation of a builtin. + bool isInlineBuiltinDeclaration() const; + /// Determine whether this is a destroying operator delete. bool isDestroyingOperatorDelete() const; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index be59d88b73f..0d30f64b992 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3046,6 +3046,14 @@ bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const return Params == FPT->getNumParams(); } +bool FunctionDecl::isInlineBuiltinDeclaration() const { + if (!getBuiltinID()) + return false; + + const FunctionDecl *Definition; + return hasBody(Definition) && Definition->isInlineSpecified(); +} + bool FunctionDecl::isDestroyingOperatorDelete() const { // C++ P0722: // Within a class C, a single object deallocation function with signature diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index c1a0d5639d1..1c961f0ce69 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -4622,8 +4622,15 @@ RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E, } static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) { + if (auto builtinID = FD->getBuiltinID()) { - return CGCallee::forBuiltin(builtinID, FD); + // Replaceable builtin provide their own implementation of a builtin. Unless + // we are in the builtin implementation itself, don't call the actual + // builtin. If we are in the builtin implementation, avoid trivial infinite + // recursion. + if (!FD->isInlineBuiltinDeclaration() || + CGF.CurFn->getName() == FD->getName()) + return CGCallee::forBuiltin(builtinID, FD); } llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 963cd82210c..5e0f0db90b0 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1840,6 +1840,11 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, else if (const auto *SA = FD->getAttr<SectionAttr>()) F->setSection(SA->getName()); + if (FD->isInlineBuiltinDeclaration()) { + F->addAttribute(llvm::AttributeList::FunctionIndex, + llvm::Attribute::NoBuiltin); + } + if (FD->isReplaceableGlobalAllocationFunction()) { // A replaceable global allocation function does not act like a builtin by // default, only if it is invoked by a new-expression or delete-expression. diff --git a/clang/test/CodeGen/memcpy-nobuiltin.c b/clang/test/CodeGen/memcpy-nobuiltin.c new file mode 100644 index 00000000000..fb51d87413a --- /dev/null +++ b/clang/test/CodeGen/memcpy-nobuiltin.c @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -DWITH_DECL | FileCheck --check-prefix=CHECK-WITH-DECL %s +// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -UWITH_DECL | FileCheck --check-prefix=CHECK-NO-DECL %s +// RUN: %clang_cc1 -verify -S -emit-llvm -o- %s -isystem %S -DWITH_SELF_REFERENCE_DECL | FileCheck --check-prefix=CHECK-SELF-REF-DECL %s +// +// CHECK-WITH-DECL-NOT: @llvm.memcpy +// CHECK-NO-DECL: @llvm.memcpy +// CHECK-SELF-REF-DECL: @llvm.memcpy +// +#include <memcpy-nobuiltin.inc> +void test(void *dest, void const *from, size_t n) { + memcpy(dest, from, n); + + static char buffer[1]; + memcpy(buffer, from, 2); // expected-warning {{'memcpy' will always overflow; destination buffer has size 1, but size argument is 2}} +} diff --git a/clang/test/CodeGen/memcpy-nobuiltin.inc b/clang/test/CodeGen/memcpy-nobuiltin.inc new file mode 100644 index 00000000000..25eab0a9ffd --- /dev/null +++ b/clang/test/CodeGen/memcpy-nobuiltin.inc @@ -0,0 +1,19 @@ +#include <stddef.h> +extern void *memcpy(void *dest, void const *from, size_t n); + +#ifdef WITH_DECL +inline void *memcpy(void *dest, void const *from, size_t n) { + char const *ifrom = from; + char *idest = dest; + while (n--) + *idest++ = *ifrom++; + return dest; +} +#endif +#ifdef WITH_SELF_REFERENCE_DECL +inline void *memcpy(void *dest, void const *from, size_t n) { + if (n != 0) + memcpy(dest, from, n); + return dest; +} +#endif |