George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
zijunzhao | b40e600 | 2023-06-07 23:55:51 +0000 | [diff] [blame] | 33 | int __poll_chk(struct pollfd* _Nullable, nfds_t, int, size_t) __INTRODUCED_IN(23); |
| 34 | int __ppoll_chk(struct pollfd* _Nullable, nfds_t, const struct timespec* _Nullable, const sigset_t* _Nullable, size_t) __INTRODUCED_IN(23); |
| 35 | int __ppoll64_chk(struct pollfd* _Nullable, nfds_t, const struct timespec* _Nullable, const sigset64_t* _Nullable, size_t) __INTRODUCED_IN(28); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 36 | |
| 37 | #if defined(__BIONIC_FORTIFY) |
George Burgess IV | a1a09b2 | 2019-05-13 17:16:20 -0700 | [diff] [blame] | 38 | #define __bos_fd_count_trivially_safe(bos_val, fds, fd_count) \ |
| 39 | __bos_dynamic_check_impl_and((bos_val), >=, (sizeof(*fds) * (fd_count)), \ |
| 40 | (fd_count) <= __BIONIC_CAST(static_cast, nfds_t, -1) / sizeof(*fds)) |
| 41 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 42 | __BIONIC_FORTIFY_INLINE |
zijunzhao | b40e600 | 2023-06-07 23:55:51 +0000 | [diff] [blame] | 43 | int poll(struct pollfd* _Nullable const fds __pass_object_size, nfds_t fd_count, int timeout) |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 44 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 45 | __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count), |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 46 | "in call to 'poll', fd_count is larger than the given buffer") { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 47 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 48 | size_t bos_fds = __bos(fds); |
| 49 | |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 50 | if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) { |
| 51 | return __poll_chk(fds, fd_count, timeout, bos_fds); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 52 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 53 | #endif |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 54 | return __call_bypassing_fortify(poll)(fds, fd_count, timeout); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 55 | } |
| 56 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 57 | __BIONIC_FORTIFY_INLINE |
zijunzhao | b40e600 | 2023-06-07 23:55:51 +0000 | [diff] [blame] | 58 | int ppoll(struct pollfd* _Nullable const fds __pass_object_size, nfds_t fd_count, const struct timespec* _Nullable timeout, const sigset_t* _Nullable mask) |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 59 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 60 | __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count), |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 61 | "in call to 'ppoll', fd_count is larger than the given buffer") { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 62 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 63 | size_t bos_fds = __bos(fds); |
| 64 | |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 65 | if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) { |
| 66 | return __ppoll_chk(fds, fd_count, timeout, mask, bos_fds); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 67 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 68 | #endif |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 69 | return __call_bypassing_fortify(ppoll)(fds, fd_count, timeout, mask); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 70 | } |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 71 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 72 | #if __ANDROID_API__ >= 28 |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 73 | __BIONIC_FORTIFY_INLINE |
zijunzhao | b40e600 | 2023-06-07 23:55:51 +0000 | [diff] [blame] | 74 | int ppoll64(struct pollfd* _Nullable const fds __pass_object_size, nfds_t fd_count, const struct timespec* _Nullable timeout, const sigset64_t* _Nullable mask) |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 75 | __overloadable |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 76 | __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count), |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 77 | "in call to 'ppoll64', fd_count is larger than the given buffer") { |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 78 | #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 79 | size_t bos_fds = __bos(fds); |
| 80 | |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 81 | if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) { |
| 82 | return __ppoll64_chk(fds, fd_count, timeout, mask, bos_fds); |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 83 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 84 | #endif |
George Burgess IV | db87607 | 2019-09-18 17:03:22 -0700 | [diff] [blame] | 85 | return __call_bypassing_fortify(ppoll64)(fds, fd_count, timeout, mask); |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 87 | #endif /* __ANDROID_API__ >= 28 */ |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 88 | |
George Burgess IV | a1a09b2 | 2019-05-13 17:16:20 -0700 | [diff] [blame] | 89 | #undef __bos_fd_count_trivially_safe |
| 90 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 91 | #endif /* defined(__BIONIC_FORTIFY) */ |