blob: 6a6b43390a2393bae0ac8f2bbfede50c57424549 [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 _STDIO_H_
30#error "Never include this file directly; instead, include <stdio.h>"
31#endif
32
33char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
Elliott Hughesec6850d2017-08-01 08:28:46 -070034size_t __fread_chk(void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24);
35size_t __fwrite_chk(const void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24);
George Burgess IVb97049c2017-07-24 15:05:05 -070036
37#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
38
39#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
40__BIONIC_FORTIFY_INLINE __printflike(3, 0)
George Burgess IV23dbf822017-07-31 21:23:34 -070041int vsnprintf(char* const __pass_object_size dest, size_t size, const char* format, va_list ap)
42 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070043 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
44}
45
46__BIONIC_FORTIFY_INLINE __printflike(2, 0)
George Burgess IV23dbf822017-07-31 21:23:34 -070047int vsprintf(char* const __pass_object_size dest, const char* format, va_list ap) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070048 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
49}
50#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
51
52#if defined(__clang__)
53#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
54/*
55 * Simple case: `format` can't have format specifiers, so we can just compare
56 * its length to the length of `dest`
57 */
58__BIONIC_ERROR_FUNCTION_VISIBILITY
Elliott Hughesec6850d2017-08-01 08:28:46 -070059int snprintf(char* dest, size_t size, const char* format)
George Burgess IVb97049c2017-07-24 15:05:05 -070060 __overloadable
61 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
George Burgess IV23dbf822017-07-31 21:23:34 -070062 __bos(dest) < __builtin_strlen(format),
George Burgess IVb97049c2017-07-24 15:05:05 -070063 "format string will always overflow destination buffer")
64 __errorattr("format string will always overflow destination buffer");
65
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -080066__BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
George Burgess IV23dbf822017-07-31 21:23:34 -070067int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...)
68 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070069 va_list va;
70 va_start(va, format);
71 int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
72 va_end(va);
73 return result;
74}
75
76__BIONIC_ERROR_FUNCTION_VISIBILITY
George Burgess IV23dbf822017-07-31 21:23:34 -070077int sprintf(char* dest, const char* format)
78 __overloadable
George Burgess IVb97049c2017-07-24 15:05:05 -070079 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
80 __bos(dest) < __builtin_strlen(format),
81 "format string will always overflow destination buffer")
82 __errorattr("format string will always overflow destination buffer");
83
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -080084__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
Elliott Hughesec6850d2017-08-01 08:28:46 -070085int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070086 va_list va;
87 va_start(va, format);
88 int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
89 va_end(va);
90 return result;
91}
92#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
93
94#if __ANDROID_API__ >= __ANDROID_API_N__
95__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -070096size_t fread(void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream)
97 __overloadable
98 __clang_error_if(__unsafe_check_mul_overflow(size, count),
99 "in call to 'fread', size * count overflows")
100 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
101 "in call to 'fread', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700102 size_t bos = __bos0(buf);
103
104 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
105 return __call_bypassing_fortify(fread)(buf, size, count, stream);
106 }
George Burgess IVb97049c2017-07-24 15:05:05 -0700107 return __fread_chk(buf, size, count, stream, bos);
108}
109
George Burgess IVb97049c2017-07-24 15:05:05 -0700110__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700111size_t fwrite(const void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream)
George Burgess IV23dbf822017-07-31 21:23:34 -0700112 __overloadable
113 __clang_error_if(__unsafe_check_mul_overflow(size, count),
114 "in call to 'fwrite', size * count overflows")
115 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
116 "in call to 'fwrite', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700117 size_t bos = __bos0(buf);
118
119 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
120 return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
121 }
122
123 return __fwrite_chk(buf, size, count, stream, bos);
124}
125#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
126
127#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IVb97049c2017-07-24 15:05:05 -0700128__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -0700129char* fgets(char* const __pass_object_size dest, int size, FILE* stream)
130 __overloadable
131 __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
132 __clang_error_if(size > __bos(dest),
133 "in call to 'fgets', size is larger than the destination buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700134 size_t bos = __bos(dest);
135
136 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
137 return __call_bypassing_fortify(fgets)(dest, size, stream);
138 }
139
140 return __fgets_chk(dest, size, stream, bos);
141}
142#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
143
144#else /* defined(__clang__) */
145
Elliott Hughesec6850d2017-08-01 08:28:46 -0700146size_t __fread_real(void*, size_t, size_t, FILE*) __RENAME(fread);
George Burgess IVb97049c2017-07-24 15:05:05 -0700147__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
148__errordecl(__fread_overflow, "fread called with overflowing size * count");
149
150char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
151__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
152__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
153
Elliott Hughesec6850d2017-08-01 08:28:46 -0700154size_t __fwrite_real(const void*, size_t, size_t, FILE*) __RENAME(fwrite);
George Burgess IVb97049c2017-07-24 15:05:05 -0700155__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
156__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
157
158
159#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -0800160__BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700161int snprintf(char* dest, size_t size, const char* format, ...) {
162 return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack());
George Burgess IVb97049c2017-07-24 15:05:05 -0700163}
164
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -0800165__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700166int sprintf(char* dest, const char* format, ...) {
167 return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack());
George Burgess IVb97049c2017-07-24 15:05:05 -0700168}
169#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
170
171#if __ANDROID_API__ >= __ANDROID_API_N__
172__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700173size_t fread(void* buf, size_t size, size_t count, FILE* stream) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700174 size_t bos = __bos0(buf);
175
176 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
177 return __fread_real(buf, size, count, stream);
178 }
179
180 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
181 size_t total;
182 if (__size_mul_overflow(size, count, &total)) {
183 __fread_overflow();
184 }
185
186 if (total > bos) {
187 __fread_too_big_error();
188 }
189
190 return __fread_real(buf, size, count, stream);
191 }
192
193 return __fread_chk(buf, size, count, stream, bos);
194}
195
196__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700197size_t fwrite(const void* buf, size_t size, size_t count, FILE* stream) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700198 size_t bos = __bos0(buf);
199
200 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
201 return __fwrite_real(buf, size, count, stream);
202 }
203
204 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
205 size_t total;
206 if (__size_mul_overflow(size, count, &total)) {
207 __fwrite_overflow();
208 }
209
210 if (total > bos) {
211 __fwrite_too_big_error();
212 }
213
214 return __fwrite_real(buf, size, count, stream);
215 }
216
217 return __fwrite_chk(buf, size, count, stream, bos);
218}
219#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
220
221#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
222__BIONIC_FORTIFY_INLINE
223char *fgets(char* dest, int size, FILE* stream) {
224 size_t bos = __bos(dest);
225
226 // Compiler can prove, at compile time, that the passed in size
227 // is always negative. Force a compiler error.
228 if (__builtin_constant_p(size) && (size < 0)) {
229 __fgets_too_small_error();
230 }
231
232 // Compiler doesn't know destination size. Don't call __fgets_chk
233 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
234 return __fgets_real(dest, size, stream);
235 }
236
237 // Compiler can prove, at compile time, that the passed in size
238 // is always <= the actual object size. Don't call __fgets_chk
239 if (__builtin_constant_p(size) && (size <= (int) bos)) {
240 return __fgets_real(dest, size, stream);
241 }
242
243 // Compiler can prove, at compile time, that the passed in size
244 // is always > the actual object size. Force a compiler error.
245 if (__builtin_constant_p(size) && (size > (int) bos)) {
246 __fgets_too_big_error();
247 }
248
249 return __fgets_chk(dest, size, stream, bos);
250}
251#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
252
253#endif /* defined(__clang__) */
254#endif /* defined(__BIONIC_FORTIFY) */