blob: 9acb94239fe22dacf9dace428842ffc797b904ea [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#ifndef _UNISTD_H_
29#error "Never include this file directly; instead, include <unistd.h>"
30#endif
31
George Burgess IVb97049c2017-07-24 15:05:05 -070032
Dan Albert02ce4012024-10-25 19:13:49 +000033#if __BIONIC_AVAILABILITY_GUARD(24)
34char* _Nullable __getcwd_chk(char* _Nullable, size_t, size_t) __INTRODUCED_IN(24);
35#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
36
37
38
39#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhao30cf6d02023-06-01 19:23:18 +000040ssize_t __pread_chk(int, void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +000041#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
42
zijunzhao30cf6d02023-06-01 19:23:18 +000043ssize_t __pread_real(int, void* _Nonnull, size_t, off_t) __RENAME(pread);
George Burgess IVb97049c2017-07-24 15:05:05 -070044
Dan Albert02ce4012024-10-25 19:13:49 +000045
46#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhao30cf6d02023-06-01 19:23:18 +000047ssize_t __pread64_chk(int, void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +000048#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
49
zijunzhao30cf6d02023-06-01 19:23:18 +000050ssize_t __pread64_real(int, void* _Nonnull, size_t, off64_t) __RENAME(pread64);
George Burgess IVb97049c2017-07-24 15:05:05 -070051
Dan Albert02ce4012024-10-25 19:13:49 +000052
53#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhao30cf6d02023-06-01 19:23:18 +000054ssize_t __pwrite_chk(int, const void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +000055#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
56
zijunzhao30cf6d02023-06-01 19:23:18 +000057ssize_t __pwrite_real(int, const void* _Nonnull, size_t, off_t) __RENAME(pwrite);
George Burgess IVb97049c2017-07-24 15:05:05 -070058
Dan Albert02ce4012024-10-25 19:13:49 +000059
60#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhao30cf6d02023-06-01 19:23:18 +000061ssize_t __pwrite64_chk(int, const void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +000062#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
63
zijunzhao30cf6d02023-06-01 19:23:18 +000064ssize_t __pwrite64_real(int, const void* _Nonnull, size_t, off64_t) __RENAME(pwrite64);
George Burgess IVb97049c2017-07-24 15:05:05 -070065
Elliott Hughes655e4302023-06-16 12:39:33 -070066ssize_t __read_chk(int, void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t);
Dan Albert02ce4012024-10-25 19:13:49 +000067
68#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhao30cf6d02023-06-01 19:23:18 +000069ssize_t __write_chk(int, const void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +000070#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
71
72
73#if __BIONIC_AVAILABILITY_GUARD(23)
zijunzhao30cf6d02023-06-01 19:23:18 +000074ssize_t __readlink_chk(const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
75ssize_t __readlinkat_chk(int dirfd, const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
Dan Albert02ce4012024-10-25 19:13:49 +000076#endif /* __BIONIC_AVAILABILITY_GUARD(23) */
77
George Burgess IVb97049c2017-07-24 15:05:05 -070078
79#if defined(__BIONIC_FORTIFY)
80
81#if defined(__USE_FILE_OFFSET64)
82#define __PREAD_PREFIX(x) __pread64_ ## x
83#define __PWRITE_PREFIX(x) __pwrite64_ ## x
84#else
85#define __PREAD_PREFIX(x) __pread_ ## x
86#define __PWRITE_PREFIX(x) __pwrite_ ## x
87#endif
88
George Burgess IV16c17392017-07-31 21:30:47 -070089#define __error_if_overflows_ssizet(what, fn) \
90 __clang_error_if((what) > SSIZE_MAX, "in call to '" #fn "', '" #what "' must be <= SSIZE_MAX")
George Burgess IVb97049c2017-07-24 15:05:05 -070091
George Burgess IV16c17392017-07-31 21:30:47 -070092#define __error_if_overflows_objectsize(what, objsize, fn) \
George Burgess IV5273dc52019-05-09 13:46:57 -070093 __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \
George Burgess IV16c17392017-07-31 21:30:47 -070094 "in call to '" #fn "', '" #what "' bytes overflows the given object")
George Burgess IVb97049c2017-07-24 15:05:05 -070095
George Burgess IVda8d30f2019-09-18 17:09:43 -070096#define __bos_trivially_ge_no_overflow(bos_val, index) \
George Burgess IV74519e72019-06-06 17:54:00 -070097 ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \
98 __builtin_constant_p(index) && (index) <= SSIZE_MAX))
George Burgess IVd9865e72019-05-13 17:20:04 -070099
George Burgess IVb97049c2017-07-24 15:05:05 -0700100__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000101char* _Nullable getcwd(char* const _Nullable __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -0700102 __overloadable
103 __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800104#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700105 size_t bos = __bos(buf);
106
George Burgess IV3aedee92019-09-24 11:46:22 -0700107 if (!__bos_trivially_ge(bos, size)) {
George Burgess IVda8d30f2019-09-18 17:09:43 -0700108 return __getcwd_chk(buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700109 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700110#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700111 return __call_bypassing_fortify(getcwd)(buf, size);
112}
George Burgess IVb97049c2017-07-24 15:05:05 -0700113
George Burgess IVda8d30f2019-09-18 17:09:43 -0700114#if !defined(__USE_FILE_OFFSET64)
George Burgess IVb97049c2017-07-24 15:05:05 -0700115__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000116ssize_t pread(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700117 __overloadable
118 __error_if_overflows_ssizet(count, pread)
119 __error_if_overflows_objectsize(count, __bos0(buf), pread) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800120#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700121 size_t bos = __bos0(buf);
122
George Burgess IVda8d30f2019-09-18 17:09:43 -0700123 if (!__bos_trivially_ge_no_overflow(bos, count)) {
124 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700125 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700126#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700127 return __PREAD_PREFIX(real)(fd, buf, count, offset);
George Burgess IVb97049c2017-07-24 15:05:05 -0700128}
George Burgess IVda8d30f2019-09-18 17:09:43 -0700129#endif /* !defined(__USE_FILE_OFFSET64) */
George Burgess IVb97049c2017-07-24 15:05:05 -0700130
George Burgess IVb97049c2017-07-24 15:05:05 -0700131__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000132ssize_t pread64(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700133 __overloadable
134 __error_if_overflows_ssizet(count, pread64)
135 __error_if_overflows_objectsize(count, __bos0(buf), pread64) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800136#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700137 size_t bos = __bos0(buf);
138
George Burgess IVda8d30f2019-09-18 17:09:43 -0700139 if (!__bos_trivially_ge_no_overflow(bos, count)) {
140 return __pread64_chk(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700141 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700142#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700143 return __pread64_real(fd, buf, count, offset);
144}
George Burgess IVb97049c2017-07-24 15:05:05 -0700145
George Burgess IVda8d30f2019-09-18 17:09:43 -0700146#if !defined(__USE_FILE_OFFSET64)
George Burgess IVb97049c2017-07-24 15:05:05 -0700147__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000148ssize_t pwrite(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700149 __overloadable
150 __error_if_overflows_ssizet(count, pwrite)
151 __error_if_overflows_objectsize(count, __bos0(buf), pwrite) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800152#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700153 size_t bos = __bos0(buf);
154
George Burgess IVda8d30f2019-09-18 17:09:43 -0700155 if (!__bos_trivially_ge_no_overflow(bos, count)) {
156 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700157 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700158#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700159 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
George Burgess IVb97049c2017-07-24 15:05:05 -0700160}
George Burgess IVda8d30f2019-09-18 17:09:43 -0700161#endif /* !defined(__USE_FILE_OFFSET64) */
George Burgess IVb97049c2017-07-24 15:05:05 -0700162
George Burgess IVb97049c2017-07-24 15:05:05 -0700163__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000164ssize_t pwrite64(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700165 __overloadable
166 __error_if_overflows_ssizet(count, pwrite64)
167 __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800168#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700169 size_t bos = __bos0(buf);
170
George Burgess IVda8d30f2019-09-18 17:09:43 -0700171 if (!__bos_trivially_ge_no_overflow(bos, count)) {
172 return __pwrite64_chk(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700173 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700174#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700175 return __pwrite64_real(fd, buf, count, offset);
176}
George Burgess IVb97049c2017-07-24 15:05:05 -0700177
George Burgess IVb97049c2017-07-24 15:05:05 -0700178__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000179ssize_t read(int fd, void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700180 __overloadable
181 __error_if_overflows_ssizet(count, read)
182 __error_if_overflows_objectsize(count, __bos0(buf), read) {
Elliott Hughesf4ace9d2023-02-23 17:38:37 +0000183#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700184 size_t bos = __bos0(buf);
185
George Burgess IVda8d30f2019-09-18 17:09:43 -0700186 if (!__bos_trivially_ge_no_overflow(bos, count)) {
187 return __read_chk(fd, buf, count, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700188 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700189#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700190 return __call_bypassing_fortify(read)(fd, buf, count);
191}
George Burgess IVb97049c2017-07-24 15:05:05 -0700192
George Burgess IVb97049c2017-07-24 15:05:05 -0700193__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000194ssize_t write(int fd, const void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700195 __overloadable
196 __error_if_overflows_ssizet(count, write)
197 __error_if_overflows_objectsize(count, __bos0(buf), write) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800198#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700199 size_t bos = __bos0(buf);
200
George Burgess IVda8d30f2019-09-18 17:09:43 -0700201 if (!__bos_trivially_ge_no_overflow(bos, count)) {
202 return __write_chk(fd, buf, count, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700203 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700204#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700205 return __call_bypassing_fortify(write)(fd, buf, count);
206}
George Burgess IVb97049c2017-07-24 15:05:05 -0700207
George Burgess IVb97049c2017-07-24 15:05:05 -0700208__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000209ssize_t readlink(const char* _Nonnull path, char* _Nonnull const __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -0700210 __overloadable
211 __error_if_overflows_ssizet(size, readlink)
212 __error_if_overflows_objectsize(size, __bos(buf), readlink) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800213#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700214 size_t bos = __bos(buf);
215
George Burgess IVda8d30f2019-09-18 17:09:43 -0700216 if (!__bos_trivially_ge_no_overflow(bos, size)) {
217 return __readlink_chk(path, buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700218 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700219#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700220 return __call_bypassing_fortify(readlink)(path, buf, size);
George Burgess IVb97049c2017-07-24 15:05:05 -0700221}
222
George Burgess IVb97049c2017-07-24 15:05:05 -0700223__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000224ssize_t readlinkat(int dirfd, const char* _Nonnull path, char* const _Nonnull __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -0700225 __overloadable
226 __error_if_overflows_ssizet(size, readlinkat)
227 __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800228#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700229 size_t bos = __bos(buf);
230
George Burgess IVda8d30f2019-09-18 17:09:43 -0700231 if (!__bos_trivially_ge_no_overflow(bos, size)) {
232 return __readlinkat_chk(dirfd, path, buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700233 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700234#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700235 return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
236}
George Burgess IVb97049c2017-07-24 15:05:05 -0700237
George Burgess IVda8d30f2019-09-18 17:09:43 -0700238#undef __bos_trivially_ge_no_overflow
George Burgess IVb97049c2017-07-24 15:05:05 -0700239#undef __enable_if_no_overflow_ssizet
240#undef __error_if_overflows_objectsize
241#undef __error_if_overflows_ssizet
George Burgess IVb97049c2017-07-24 15:05:05 -0700242#undef __PREAD_PREFIX
243#undef __PWRITE_PREFIX
244#endif /* defined(__BIONIC_FORTIFY) */