blob: 1b4a5bf8beada7bdc71fb7aed208f21ef74b772b [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
Dan Albert02ce4012024-10-25 19:13:49 +000033
34#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhaob40e6002023-06-07 23:55:51 +000035int __poll_chk(struct pollfd* _Nullable, nfds_t, int, size_t) __INTRODUCED_IN(23);
36int __ppoll_chk(struct pollfd* _Nullable, nfds_t, const struct timespec* _Nullable, const sigset_t* _Nullable, size_t) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +000037#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
38
39
40#if __BIONIC_AVAILABILITY_GUARD(28)
zijunzhaob40e6002023-06-07 23:55:51 +000041int __ppoll64_chk(struct pollfd* _Nullable, nfds_t, const struct timespec* _Nullable, const sigset64_t* _Nullable, size_t) __INTRODUCED_IN(28);
Dan Albert02ce4012024-10-25 19:13:49 +000042#endif /* __BIONIC_AVAILABILITY_GUARD(28) */
43
George Burgess IVb97049c2017-07-24 15:05:05 -070044
45#if defined(__BIONIC_FORTIFY)
George Burgess IVa1a09b22019-05-13 17:16:20 -070046#define __bos_fd_count_trivially_safe(bos_val, fds, fd_count) \
47 __bos_dynamic_check_impl_and((bos_val), >=, (sizeof(*fds) * (fd_count)), \
48 (fd_count) <= __BIONIC_CAST(static_cast, nfds_t, -1) / sizeof(*fds))
49
George Burgess IVb97049c2017-07-24 15:05:05 -070050__BIONIC_FORTIFY_INLINE
zijunzhaob40e6002023-06-07 23:55:51 +000051int poll(struct pollfd* _Nullable const fds __pass_object_size, nfds_t fd_count, int timeout)
George Burgess IV52dde5f2017-07-31 21:16:05 -070052 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070053 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
George Burgess IV52dde5f2017-07-31 21:16:05 -070054 "in call to 'poll', fd_count is larger than the given buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080055#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070056 size_t bos_fds = __bos(fds);
57
George Burgess IVdb876072019-09-18 17:03:22 -070058 if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
59 return __poll_chk(fds, fd_count, timeout, bos_fds);
George Burgess IVb97049c2017-07-24 15:05:05 -070060 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070061#endif
George Burgess IVdb876072019-09-18 17:03:22 -070062 return __call_bypassing_fortify(poll)(fds, fd_count, timeout);
George Burgess IVb97049c2017-07-24 15:05:05 -070063}
64
George Burgess IVb97049c2017-07-24 15:05:05 -070065__BIONIC_FORTIFY_INLINE
zijunzhaob40e6002023-06-07 23:55:51 +000066int 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 IV52dde5f2017-07-31 21:16:05 -070067 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070068 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
George Burgess IV52dde5f2017-07-31 21:16:05 -070069 "in call to 'ppoll', fd_count is larger than the given buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080070#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070071 size_t bos_fds = __bos(fds);
72
George Burgess IVdb876072019-09-18 17:03:22 -070073 if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
74 return __ppoll_chk(fds, fd_count, timeout, mask, bos_fds);
George Burgess IVb97049c2017-07-24 15:05:05 -070075 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070076#endif
George Burgess IVdb876072019-09-18 17:03:22 -070077 return __call_bypassing_fortify(ppoll)(fds, fd_count, timeout, mask);
George Burgess IVb97049c2017-07-24 15:05:05 -070078}
Elliott Hughesb83bf142018-03-22 11:01:25 -070079
Elliott Hughes95c6cd72019-12-20 13:26:14 -080080#if __ANDROID_API__ >= 28
Elliott Hughesb83bf142018-03-22 11:01:25 -070081__BIONIC_FORTIFY_INLINE
zijunzhaob40e6002023-06-07 23:55:51 +000082int ppoll64(struct pollfd* _Nullable const fds __pass_object_size, nfds_t fd_count, const struct timespec* _Nullable timeout, const sigset64_t* _Nullable mask)
Elliott Hughesb83bf142018-03-22 11:01:25 -070083 __overloadable
George Burgess IV5273dc52019-05-09 13:46:57 -070084 __clang_error_if(__bos_unevaluated_lt(__bos(fds), sizeof(*fds) * fd_count),
Elliott Hughesb83bf142018-03-22 11:01:25 -070085 "in call to 'ppoll64', fd_count is larger than the given buffer") {
George Burgess IV8a0cdb12019-10-21 13:27:57 -070086#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
Elliott Hughesb83bf142018-03-22 11:01:25 -070087 size_t bos_fds = __bos(fds);
88
George Burgess IVdb876072019-09-18 17:03:22 -070089 if (!__bos_fd_count_trivially_safe(bos_fds, fds, fd_count)) {
90 return __ppoll64_chk(fds, fd_count, timeout, mask, bos_fds);
Elliott Hughesb83bf142018-03-22 11:01:25 -070091 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070092#endif
George Burgess IVdb876072019-09-18 17:03:22 -070093 return __call_bypassing_fortify(ppoll64)(fds, fd_count, timeout, mask);
Elliott Hughesb83bf142018-03-22 11:01:25 -070094}
Elliott Hughes95c6cd72019-12-20 13:26:14 -080095#endif /* __ANDROID_API__ >= 28 */
Elliott Hughesb83bf142018-03-22 11:01:25 -070096
George Burgess IVa1a09b22019-05-13 17:16:20 -070097#undef __bos_fd_count_trivially_safe
98
George Burgess IVb97049c2017-07-24 15:05:05 -070099#endif /* defined(__BIONIC_FORTIFY) */