aboutsummaryrefslogtreecommitdiff
path: root/io
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@sourceware.org>2021-10-20 18:12:41 +0530
committerSiddhesh Poyarekar <siddhesh@sourceware.org>2022-03-11 20:36:24 +0530
commit0e6ebf06e43e879d9efa2a1c79b2a55f81653663 (patch)
treeaa80faf0aa64d1bd26dc6f31d7703b24c3cfd4ca /io
parentc82bdf033f93a710044e25f721340c26e89a3769 (diff)
downloadglibc-0e6ebf06e43e879d9efa2a1c79b2a55f81653663.zip
glibc-0e6ebf06e43e879d9efa2a1c79b2a55f81653663.tar.gz
glibc-0e6ebf06e43e879d9efa2a1c79b2a55f81653663.tar.bz2
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> (cherry picked from commit a643f60c53876be0d57b4b7373770e6cb356fd13)
Diffstat (limited to 'io')
-rw-r--r--io/bits/poll2.h27
1 files changed, 6 insertions, 21 deletions
diff --git a/io/bits/poll2.h b/io/bits/poll2.h
index be74d02..91cdcaf 100644
--- a/io/bits/poll2.h
+++ b/io/bits/poll2.h
@@ -36,16 +36,9 @@ extern int __REDIRECT (__poll_chk_warn, (struct pollfd *__fds, nfds_t __nfds,
__fortify_function __fortified_attr_access (__write_only__, 1, 2) int
poll (struct pollfd *__fds, nfds_t __nfds, int __timeout)
{
- if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
- {
- if (! __builtin_constant_p (__nfds))
- return __poll_chk (__fds, __nfds, __timeout, __glibc_objsize (__fds));
- else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
- return __poll_chk_warn (__fds, __nfds, __timeout,
- __glibc_objsize (__fds));
- }
-
- return __poll_alias (__fds, __nfds, __timeout);
+ return __glibc_fortify (poll, __nfds, sizeof (*__fds),
+ __glibc_objsize (__fds),
+ __fds, __nfds, __timeout);
}
@@ -68,17 +61,9 @@ __fortify_function __fortified_attr_access (__write_only__, 1, 2) int
ppoll (struct pollfd *__fds, nfds_t __nfds, const struct timespec *__timeout,
const __sigset_t *__ss)
{
- if (__glibc_objsize (__fds) != (__SIZE_TYPE__) -1)
- {
- if (! __builtin_constant_p (__nfds))
- return __ppoll_chk (__fds, __nfds, __timeout, __ss,
- __glibc_objsize (__fds));
- else if (__glibc_objsize (__fds) / sizeof (*__fds) < __nfds)
- return __ppoll_chk_warn (__fds, __nfds, __timeout, __ss,
- __glibc_objsize (__fds));
- }
-
- return __ppoll_alias (__fds, __nfds, __timeout, __ss);
+ return __glibc_fortify (ppoll, __nfds, sizeof (*__fds),
+ __glibc_objsize (__fds),
+ __fds, __nfds, __timeout, __ss);
}
#endif