blob: 660dfca01ac0aa4443532e705a50495b7f4ba987 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _POLL_H_
30#error "Never include this file directly; instead, include <poll.h>"
31#endif
32
33int __poll_chk(struct pollfd*, nfds_t, int, size_t) __INTRODUCED_IN(23);
Elliott Hughesb83bf142018-03-22 11:01:25 -070034int __ppoll_chk(struct pollfd*, nfds_t, const struct timespec*, const sigset_t*, size_t) __INTRODUCED_IN(23);
35int __ppoll64_chk(struct pollfd*, nfds_t, const struct timespec*, const sigset64_t*, size_t) __INTRODUCED_IN(28);
George Burgess IVb97049c2017-07-24 15:05:05 -070036
37#if defined(__BIONIC_FORTIFY)
38#if __ANDROID_API__ >= __ANDROID_API_M__
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000039
George Burgess IVa1a09b22019-05-13 17:16:20 -070040#define __bos_fd_count_trivially_safe(bos_val, fds, fd_count) \
41 __bos_dynamic_check_impl_and((bos_val), >=, (sizeof(*fds) * (fd_count)), \
42 (fd_count) <= __BIONIC_CAST(static_cast, nfds_t, -1) / sizeof(*fds))
43
George Burgess IVb97049c2017-07-24 15:05:05 -070044__BIONIC_FORTIFY_INLINE
George Burgess IV52dde5f2017-07-31 21:16:05 -070045int poll(struct pollfd* const fds __pass_object_size, nfds_t fd_count, int timeout)
46 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070047 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
George Burgess IV52dde5f2017-07-31 21:16:05 -070048 "in call to 'poll', fd_count is larger than the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070049 size_t bos_fds = __bos(fds);
50
George Burgess IVa1a09b22019-05-13 17:16:20 -070051 if (__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
George Burgess IV52dde5f2017-07-31 21:16:05 -070052 return __call_bypassing_fortify(poll)(fds, fd_count, timeout);
George Burgess IVb97049c2017-07-24 15:05:05 -070053 }
George Burgess IVb97049c2017-07-24 15:05:05 -070054 return __poll_chk(fds, fd_count, timeout, bos_fds);
55}
56
George Burgess IVb97049c2017-07-24 15:05:05 -070057__BIONIC_FORTIFY_INLINE
George Burgess IV52dde5f2017-07-31 21:16:05 -070058int ppoll(struct pollfd* const fds __pass_object_size, nfds_t fd_count, const struct timespec* timeout, const sigset_t* mask)
59 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070060 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
George Burgess IV52dde5f2017-07-31 21:16:05 -070061 "in call to 'ppoll', fd_count is larger than the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -070062 size_t bos_fds = __bos(fds);
63
George Burgess IVa1a09b22019-05-13 17:16:20 -070064 if (__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
George Burgess IV52dde5f2017-07-31 21:16:05 -070065 return __call_bypassing_fortify(ppoll)(fds, fd_count, timeout, mask);
George Burgess IVb97049c2017-07-24 15:05:05 -070066 }
George Burgess IVb97049c2017-07-24 15:05:05 -070067 return __ppoll_chk(fds, fd_count, timeout, mask, bos_fds);
68}
Elliott Hughesb83bf142018-03-22 11:01:25 -070069
70#if __ANDROID_API__ >= __ANDROID_API_P__
71__BIONIC_FORTIFY_INLINE
72int ppoll64(struct pollfd* const fds __pass_object_size, nfds_t fd_count, const struct timespec* timeout, const sigset64_t* mask)
73 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070074 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
Elliott Hughesb83bf142018-03-22 11:01:25 -070075 "in call to 'ppoll64', fd_count is larger than the given buffer") {
76 size_t bos_fds = __bos(fds);
77
George Burgess IVa1a09b22019-05-13 17:16:20 -070078 if (__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
Elliott Hughesb83bf142018-03-22 11:01:25 -070079 return __call_bypassing_fortify(ppoll64)(fds, fd_count, timeout, mask);
80 }
81 return __ppoll64_chk(fds, fd_count, timeout, mask, bos_fds);
82}
83#endif
84
George Burgess IVa1a09b22019-05-13 17:16:20 -070085#undef __bos_fd_count_trivially_safe
86
George Burgess IVb97049c2017-07-24 15:05:05 -070087#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
88#endif /* defined(__BIONIC_FORTIFY) */