Bionic: Always use fortified versions of FD_X macros

When compiling on/for at least Lollipop, always use the fortified
versions of FD_X macros. This works around side-effect issues (which
are explicitly called out in the specification) and generally
increases robustness of code.

(cherry picked from commit 00a6d5fe0a)

Bug: 77986327
Test: mmma bionic
Test: m
Test: bionic_unit_tests
Merged-In: I9096c6872770e46ba5ab64e7375ff83fc0518e07
Change-Id: I9096c6872770e46ba5ab64e7375ff83fc0518e07
This commit is contained in:
Andreas Gampe 2018-04-13 13:52:10 -07:00
parent 5a0fe3e8bf
commit 9302cf2526
3 changed files with 23 additions and 10 deletions

View file

@ -80,17 +80,17 @@
int __FD_ISSET_chk(int fd, const fd_set* set, size_t set_size) {
__check_fd_set("FD_ISSET", fd, set_size);
return FD_ISSET(fd, set);
return __FD_ISSET(fd, set);
}
void __FD_CLR_chk(int fd, fd_set* set, size_t set_size) {
__check_fd_set("FD_CLR", fd, set_size);
FD_CLR(fd, set);
__FD_CLR(fd, set);
}
void __FD_SET_chk(int fd, fd_set* set, size_t set_size) {
__check_fd_set("FD_SET", fd, set_size);
FD_SET(fd, set);
__FD_SET(fd, set);
}
char* __fgets_chk(char* dst, int supplied_size, FILE* stream, size_t dst_len_from_compiler) {

View file

@ -289,14 +289,22 @@
# endif
#endif
// As we move some FORTIFY checks to be always on, __bos needs to be
// always available.
#if defined(__BIONIC_FORTIFY)
# if _FORTIFY_SOURCE == 2
# define __bos_level 1
# else
# define __bos_level 0
# endif
# define __bosn(s, n) __builtin_object_size((s), (n))
# define __bos(s) __bosn((s), __bos_level)
#else
# define __bos_level 0
#endif
#define __bosn(s, n) __builtin_object_size((s), (n))
#define __bos(s) __bosn((s), __bos_level)
#if defined(__BIONIC_FORTIFY)
# define __bos0(s) __bosn((s), 0)
# if defined(__clang__)
# define __pass_object_size_n(n) __attribute__((pass_object_size(n)))

View file

@ -63,15 +63,20 @@ void __FD_CLR_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
void __FD_SET_chk(int, fd_set*, size_t) __INTRODUCED_IN(21);
int __FD_ISSET_chk(int, const fd_set*, size_t) __INTRODUCED_IN(21);
#if defined(__BIONIC_FORTIFY) && __ANDROID_API__ >= __ANDROID_API_L__
#define __FD_CLR(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] &= ~__FDMASK(fd))
#define __FD_SET(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] |= __FDMASK(fd))
#define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*,set)[__FDELT(fd)] & __FDMASK(fd)) != 0)
#if __ANDROID_API__ >= __ANDROID_API_L__
#define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set))
#define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set))
#define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set))
#else
#define FD_CLR(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] &= ~__FDMASK(fd))
#define FD_SET(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] |= __FDMASK(fd))
#define FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*,set)[__FDELT(fd)] & __FDMASK(fd)) != 0)
#endif /* defined(__BIONIC_FORTIFY) && __ANDROID_API >= 21 */
#define FD_CLR(fd, set) __FD_CLR(fd, set)
#define FD_SET(fd, set) __FD_SET(fd, set)
#define FD_ISSET(fd, set) __FD_ISSET(fd, set)
#endif /* __ANDROID_API >= 21 */
int select(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, struct timeval* __timeout);
int pselect(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset_t* __mask);