diff options
author | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2021-10-20 18:12:41 +0530 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@sourceware.org> | 2021-10-20 18:12:41 +0530 |
commit | a643f60c53876be0d57b4b7373770e6cb356fd13 (patch) | |
tree | d11ac8a63cd4f7820eb635e1ade8809b2a5aa913 /libio | |
parent | e938c02748402c50f60ba0eb983273e7b52937d1 (diff) | |
download | glibc-a643f60c53876be0d57b4b7373770e6cb356fd13.tar.gz glibc-a643f60c53876be0d57b4b7373770e6cb356fd13.tar.xz glibc-a643f60c53876be0d57b4b7373770e6cb356fd13.zip |
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 <siddhesh@sourceware.org> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'libio')
-rw-r--r-- | libio/bits/stdio2.h | 106 |
1 files changed, 43 insertions, 63 deletions
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h index 4f016a5638..40ff16b01b 100644 --- a/libio/bits/stdio2.h +++ b/libio/bits/stdio2.h @@ -261,15 +261,12 @@ extern char *__REDIRECT (__fgets_chk_warn, __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char * fgets (char *__restrict __s, int __n, FILE *__restrict __stream) { - if (__glibc_objsize (__s) != (size_t) -1) - { - if (!__builtin_constant_p (__n) || __n <= 0) - return __fgets_chk (__s, __glibc_objsize (__s), __n, __stream); - - if ((size_t) __n > __glibc_objsize (__s)) - return __fgets_chk_warn (__s, __glibc_objsize (__s), __n, __stream); - } - return __fgets_alias (__s, __n, __stream); + size_t sz = __glibc_objsize (__s); + if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz)) + return __fgets_alias (__s, __n, __stream); + if (__glibc_unsafe_len (__n, sizeof (char), sz)) + return __fgets_chk_warn (__s, sz, __n, __stream); + return __fgets_chk (__s, sz, __n, __stream); } extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen, @@ -291,19 +288,12 @@ __fortify_function __wur size_t fread (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream) { - if (__glibc_objsize0 (__ptr) != (size_t) -1) - { - if (!__builtin_constant_p (__size) - || !__builtin_constant_p (__n) - || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2))) - return __fread_chk (__ptr, __glibc_objsize0 (__ptr), __size, __n, - __stream); - - if (__size * __n > __glibc_objsize0 (__ptr)) - return __fread_chk_warn (__ptr, __glibc_objsize0 (__ptr), __size, __n, - __stream); - } - return __fread_alias (__ptr, __size, __n, __stream); + size_t sz = __glibc_objsize0 (__ptr); + if (__glibc_safe_or_unknown_len (__n, __size, sz)) + return __fread_alias (__ptr, __size, __n, __stream); + if (__glibc_unsafe_len (__n, __size, sz)) + return __fread_chk_warn (__ptr, sz, __size, __n, __stream); + return __fread_chk (__ptr, sz, __size, __n, __stream); } #ifdef __USE_GNU @@ -323,17 +313,12 @@ extern char *__REDIRECT (__fgets_unlocked_chk_warn, __fortify_function __wur __fortified_attr_access (__write_only__, 1, 2) char * fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream) { - if (__glibc_objsize (__s) != (size_t) -1) - { - if (!__builtin_constant_p (__n) || __n <= 0) - return __fgets_unlocked_chk (__s, __glibc_objsize (__s), __n, - __stream); - - if ((size_t) __n > __glibc_objsize (__s)) - return __fgets_unlocked_chk_warn (__s, __glibc_objsize (__s), __n, - __stream); - } - return __fgets_unlocked_alias (__s, __n, __stream); + size_t sz = __glibc_objsize (__s); + if (__glibc_safe_or_unknown_len (__n, sizeof (char), sz)) + return __fgets_unlocked_alias (__s, __n, __stream); + if (__glibc_unsafe_len (__n, sizeof (char), sz)) + return __fgets_unlocked_chk_warn (__s, sz, __n, __stream); + return __fgets_unlocked_chk (__s, sz, __n, __stream); } #endif @@ -358,41 +343,36 @@ __fortify_function __wur size_t fread_unlocked (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream) { - if (__glibc_objsize0 (__ptr) != (size_t) -1) + size_t sz = __glibc_objsize0 (__ptr); + if (__glibc_safe_or_unknown_len (__n, __size, sz)) { - if (!__builtin_constant_p (__size) - || !__builtin_constant_p (__n) - || (__size | __n) >= (((size_t) 1) << (8 * sizeof (size_t) / 2))) - return __fread_unlocked_chk (__ptr, __glibc_objsize0 (__ptr), __size, - __n, __stream); - - if (__size * __n > __glibc_objsize0 (__ptr)) - return __fread_unlocked_chk_warn (__ptr, __glibc_objsize0 (__ptr), - __size, __n, __stream); - } - # ifdef __USE_EXTERN_INLINES - if (__builtin_constant_p (__size) - && __builtin_constant_p (__n) - && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2)) - && __size * __n <= 8) - { - size_t __cnt = __size * __n; - char *__cptr = (char *) __ptr; - if (__cnt == 0) - return 0; - - for (; __cnt > 0; --__cnt) + if (__builtin_constant_p (__size) + && __builtin_constant_p (__n) + && (__size | __n) < (((size_t) 1) << (8 * sizeof (size_t) / 2)) + && __size * __n <= 8) { - int __c = getc_unlocked (__stream); - if (__c == EOF) - break; - *__cptr++ = __c; + size_t __cnt = __size * __n; + char *__cptr = (char *) __ptr; + if (__cnt == 0) + return 0; + + for (; __cnt > 0; --__cnt) + { + int __c = getc_unlocked (__stream); + if (__c == EOF) + break; + *__cptr++ = __c; + } + return (__cptr - (char *) __ptr) / __size; } - return (__cptr - (char *) __ptr) / __size; - } # endif - return __fread_unlocked_alias (__ptr, __size, __n, __stream); + return __fread_unlocked_alias (__ptr, __size, __n, __stream); + } + if (__glibc_unsafe_len (__n, __size, sz)) + return __fread_unlocked_chk_warn (__ptr, sz, __size, __n, __stream); + return __fread_unlocked_chk (__ptr, sz, __size, __n, __stream); + } #endif |