From a643f60c53876be0d57b4b7373770e6cb356fd13 Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Wed, 20 Oct 2021 18:12:41 +0530 Subject: Make sure that the fortified function conditionals are constant In _FORTIFY_SOURCE=3, the size expression may be non-constant, resulting in branches in the inline functions remaining intact and causing a tiny overhead. Clang (and in future, gcc) make sure that the -1 case is always safe, i.e. any comparison of the generated expression with (size_t)-1 is always false so that bit is taken care of. The rest is avoidable since we want the _chk variant whenever we have a size expression and it's not -1. Rework the conditionals in a uniform way to clearly indicate two conditions at compile time: - Either the size is unknown (-1) or we know at compile time that the operation length is less than the object size. We can call the original function in this case. It could be that either the length, object size or both are non-constant, but the compiler, through range analysis, is able to fold the *comparison* to a constant. - The size and length are known and the compiler can see at compile time that operation length > object size. This is valid grounds for a warning at compile time, followed by emitting the _chk variant. For everything else, emit the _chk variant. This simplifies most of the fortified function implementations and at the same time, ensures that only one call from _chk or the regular function is emitted. Signed-off-by: Siddhesh Poyarekar Reviewed-by: Adhemerval Zanella --- stdlib/bits/stdlib.h | 57 ++++++++++++++++------------------------------------ 1 file changed, 17 insertions(+), 40 deletions(-) (limited to 'stdlib/bits/stdlib.h') diff --git a/stdlib/bits/stdlib.h b/stdlib/bits/stdlib.h index eae31b38f0..067115eeca 100644 --- a/stdlib/bits/stdlib.h +++ b/stdlib/bits/stdlib.h @@ -36,17 +36,16 @@ extern char *__REDIRECT_NTH (__realpath_chk_warn, __fortify_function __wur char * __NTH (realpath (const char *__restrict __name, char *__restrict __resolved)) { - if (__glibc_objsize (__resolved) != (size_t) -1) - { + size_t sz = __glibc_objsize (__resolved); + + if (sz == (size_t) -1) + return __realpath_alias (__name, __resolved); + #if defined _LIBC_LIMITS_H_ && defined PATH_MAX - if (__glibc_objsize (__resolved) < PATH_MAX) - return __realpath_chk_warn (__name, __resolved, - __glibc_objsize (__resolved)); + if (__glibc_unsafe_len (sz, sizeof (char), PATH_MAX)) + return __realpath_chk_warn (__name, __resolved, sz); #endif - return __realpath_chk (__name, __resolved, __glibc_objsize (__resolved)); - } - - return __realpath_alias (__name, __resolved); + return __realpath_chk (__name, __resolved, sz); } @@ -65,16 +64,9 @@ extern int __REDIRECT_NTH (__ptsname_r_chk_warn, __fortify_function int __NTH (ptsname_r (int __fd, char *__buf, size_t __buflen)) { - if (__glibc_objsize (__buf) != (size_t) -1) - { - if (!__builtin_constant_p (__buflen)) - return __ptsname_r_chk (__fd, __buf, __buflen, - __glibc_objsize (__buf)); - if (__buflen > __glibc_objsize (__buf)) - return __ptsname_r_chk_warn (__fd, __buf, __buflen, - __glibc_objsize (__buf)); - } - return __ptsname_r_alias (__fd, __buf, __buflen); + return __glibc_fortify (ptsname_r, __buflen, sizeof (char), + __glibc_objsize (__buf), + __fd, __buf, __buflen); } @@ -120,18 +112,9 @@ __fortify_function size_t __NTH (mbstowcs (wchar_t *__restrict __dst, const char *__restrict __src, size_t __len)) { - if (__glibc_objsize (__dst) != (size_t) -1) - { - if (!__builtin_constant_p (__len)) - return __mbstowcs_chk (__dst, __src, __len, - __glibc_objsize (__dst) / sizeof (wchar_t)); - - if (__len > __glibc_objsize (__dst) / sizeof (wchar_t)) - return __mbstowcs_chk_warn (__dst, __src, __len, - (__glibc_objsize (__dst) - / sizeof (wchar_t))); - } - return __mbstowcs_alias (__dst, __src, __len); + return __glibc_fortify_n (mbstowcs, __len, sizeof (wchar_t), + __glibc_objsize (__dst), + __dst, __src, __len); } @@ -154,13 +137,7 @@ __fortify_function size_t __NTH (wcstombs (char *__restrict __dst, const wchar_t *__restrict __src, size_t __len)) { - if (__glibc_objsize (__dst) != (size_t) -1) - { - if (!__builtin_constant_p (__len)) - return __wcstombs_chk (__dst, __src, __len, __glibc_objsize (__dst)); - if (__len > __glibc_objsize (__dst)) - return __wcstombs_chk_warn (__dst, __src, __len, - __glibc_objsize (__dst)); - } - return __wcstombs_alias (__dst, __src, __len); + return __glibc_fortify (wcstombs, __len, sizeof (char), + __glibc_objsize (__dst), + __dst, __src, __len); } -- cgit 1.4.1