From f9e1e56ea17c0171ecc2095fae6335369abc94bc Mon Sep 17 00:00:00 2001 From: Ahmed Bougacha Date: Wed, 12 Nov 2014 21:23:34 +0000 Subject: [PATCH] Add fortified (__*_chk) library functions to TLI (NFC) One of them (__memcpy_chk) was already there, the others were checked by comparing function names. Note that the fortified libfuncs are now part of TLI, but are always available, because they aren't generated, only optimized into the non-checking versions. Differential Revision: http://reviews.llvm.org/D6179 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221817 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Target/TargetLibraryInfo.h | 15 ++++++++++++ lib/Target/TargetLibraryInfo.cpp | 6 +++++ lib/Transforms/Utils/SimplifyLibCalls.cpp | 30 +++++++++-------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/include/llvm/Target/TargetLibraryInfo.h b/include/llvm/Target/TargetLibraryInfo.h index 56a89191a58..46f87b93b00 100644 --- a/include/llvm/Target/TargetLibraryInfo.h +++ b/include/llvm/Target/TargetLibraryInfo.h @@ -73,6 +73,11 @@ namespace llvm { dunder_isoc99_sscanf, /// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size); memcpy_chk, + /// void *__memmove_chk(void *s1, const void *s2, size_t n, + /// size_t s1size); + memmove_chk, + /// void *__memset_chk(void *s, char v, size_t n, size_t s1size); + memset_chk, /// double __sincospi_stret(double x); sincospi_stret, /// float __sincospif_stret(float x); @@ -87,8 +92,18 @@ namespace llvm { sqrtf_finite, /// long double __sqrt_finite(long double x); sqrtl_finite, + /// char *__stpcpy_chk(char *s1, const char *s2, size_t s1size); + stpcpy_chk, + /// char *__stpncpy_chk(char *s1, const char *s2, size_t n, + /// size_t s1size); + stpncpy_chk, + /// char *__strcpy_chk(char *s1, const char *s2, size_t s1size); + strcpy_chk, /// char * __strdup(const char *s); dunder_strdup, + /// char *__strncpy_chk(char *s1, const char *s2, size_t n, + /// size_t s1size); + strncpy_chk, /// char *__strndup(const char *s, size_t n); dunder_strndup, /// char * __strtok_r(char *s, const char *delim, char **save_ptr); diff --git a/lib/Target/TargetLibraryInfo.cpp b/lib/Target/TargetLibraryInfo.cpp index ecb38198551..bca56b5c309 100644 --- a/lib/Target/TargetLibraryInfo.cpp +++ b/lib/Target/TargetLibraryInfo.cpp @@ -51,6 +51,8 @@ const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] = "__isoc99_scanf", "__isoc99_sscanf", "__memcpy_chk", + "__memmove_chk", + "__memset_chk", "__sincospi_stret", "__sincospif_stret", "__sinpi", @@ -58,7 +60,11 @@ const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] = "__sqrt_finite", "__sqrtf_finite", "__sqrtl_finite", + "__stpcpy_chk", + "__stpncpy_chk", + "__strcpy_chk", "__strdup", + "__strncpy_chk", "__strndup", "__strtok_r", "abs", diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 207d3664789..e7beb2feb62 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2184,30 +2184,22 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { return nullptr; case LibFunc::memcpy_chk: return optimizeMemCpyChk(CI, Builder); + case LibFunc::memmove_chk: + return optimizeMemMoveChk(CI, Builder); + case LibFunc::memset_chk: + return optimizeMemSetChk(CI, Builder); + case LibFunc::strcpy_chk: + return optimizeStrCpyChk(CI, Builder); + case LibFunc::stpcpy_chk: + return optimizeStpCpyChk(CI, Builder); + case LibFunc::stpncpy_chk: + case LibFunc::strncpy_chk: + return optimizeStrNCpyChk(CI, Builder); default: return nullptr; } } - if (!isCallingConvC) - return nullptr; - - // Finally check for fortified library calls. - if (FuncName.endswith("_chk")) { - if (FuncName == "__memmove_chk") - return optimizeMemMoveChk(CI, Builder); - else if (FuncName == "__memset_chk") - return optimizeMemSetChk(CI, Builder); - else if (FuncName == "__strcpy_chk") - return optimizeStrCpyChk(CI, Builder); - else if (FuncName == "__stpcpy_chk") - return optimizeStpCpyChk(CI, Builder); - else if (FuncName == "__strncpy_chk") - return optimizeStrNCpyChk(CI, Builder); - else if (FuncName == "__stpncpy_chk") - return optimizeStrNCpyChk(CI, Builder); - } - return nullptr; }