blob: cfc78d7afe7c7741da6be06b43fc52db3fb8cf44 [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
66__BIONIC_FORTIFY_INLINE
67__printflike(3, 4)
George Burgess IV23dbf822017-07-31 21:23:34 -070068int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...)
69 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070070 va_list va;
71 va_start(va, format);
72 int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
73 va_end(va);
74 return result;
75}
76
77__BIONIC_ERROR_FUNCTION_VISIBILITY
George Burgess IV23dbf822017-07-31 21:23:34 -070078int sprintf(char* dest, const char* format)
79 __overloadable
George Burgess IVb97049c2017-07-24 15:05:05 -070080 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
81 __bos(dest) < __builtin_strlen(format),
82 "format string will always overflow destination buffer")
83 __errorattr("format string will always overflow destination buffer");
84
85__BIONIC_FORTIFY_INLINE
86__printflike(2, 3)
Elliott Hughesec6850d2017-08-01 08:28:46 -070087int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070088 va_list va;
89 va_start(va, format);
90 int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
91 va_end(va);
92 return result;
93}
94#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
95
96#if __ANDROID_API__ >= __ANDROID_API_N__
97__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -070098size_t fread(void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream)
99 __overloadable
100 __clang_error_if(__unsafe_check_mul_overflow(size, count),
101 "in call to 'fread', size * count overflows")
102 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
103 "in call to 'fread', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700104 size_t bos = __bos0(buf);
105
106 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
107 return __call_bypassing_fortify(fread)(buf, size, count, stream);
108 }
George Burgess IVb97049c2017-07-24 15:05:05 -0700109 return __fread_chk(buf, size, count, stream, bos);
110}
111
George Burgess IVb97049c2017-07-24 15:05:05 -0700112__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700113size_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 -0700114 __overloadable
115 __clang_error_if(__unsafe_check_mul_overflow(size, count),
116 "in call to 'fwrite', size * count overflows")
117 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
118 "in call to 'fwrite', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700119 size_t bos = __bos0(buf);
120
121 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
122 return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
123 }
124
125 return __fwrite_chk(buf, size, count, stream, bos);
126}
127#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
128
129#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IVb97049c2017-07-24 15:05:05 -0700130__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -0700131char* fgets(char* const __pass_object_size dest, int size, FILE* stream)
132 __overloadable
133 __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
134 __clang_error_if(size > __bos(dest),
135 "in call to 'fgets', size is larger than the destination buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700136 size_t bos = __bos(dest);
137
138 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
139 return __call_bypassing_fortify(fgets)(dest, size, stream);
140 }
141
142 return __fgets_chk(dest, size, stream, bos);
143}
144#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
145
146#else /* defined(__clang__) */
147
Elliott Hughesec6850d2017-08-01 08:28:46 -0700148size_t __fread_real(void*, size_t, size_t, FILE*) __RENAME(fread);
George Burgess IVb97049c2017-07-24 15:05:05 -0700149__errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer");
150__errordecl(__fread_overflow, "fread called with overflowing size * count");
151
152char* __fgets_real(char*, int, FILE*) __RENAME(fgets);
153__errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer");
154__errordecl(__fgets_too_small_error, "fgets called with size less than zero");
155
Elliott Hughesec6850d2017-08-01 08:28:46 -0700156size_t __fwrite_real(const void*, size_t, size_t, FILE*) __RENAME(fwrite);
George Burgess IVb97049c2017-07-24 15:05:05 -0700157__errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer");
158__errordecl(__fwrite_overflow, "fwrite called with overflowing size * count");
159
160
161#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
162__BIONIC_FORTIFY_INLINE __printflike(3, 4)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700163int snprintf(char* dest, size_t size, const char* format, ...) {
164 return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack());
George Burgess IVb97049c2017-07-24 15:05:05 -0700165}
166
167__BIONIC_FORTIFY_INLINE __printflike(2, 3)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700168int sprintf(char* dest, const char* format, ...) {
169 return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack());
George Burgess IVb97049c2017-07-24 15:05:05 -0700170}
171#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
172
173#if __ANDROID_API__ >= __ANDROID_API_N__
174__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700175size_t fread(void* buf, size_t size, size_t count, FILE* stream) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700176 size_t bos = __bos0(buf);
177
178 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
179 return __fread_real(buf, size, count, stream);
180 }
181
182 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
183 size_t total;
184 if (__size_mul_overflow(size, count, &total)) {
185 __fread_overflow();
186 }
187
188 if (total > bos) {
189 __fread_too_big_error();
190 }
191
192 return __fread_real(buf, size, count, stream);
193 }
194
195 return __fread_chk(buf, size, count, stream, bos);
196}
197
198__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700199size_t fwrite(const void* buf, size_t size, size_t count, FILE* stream) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700200 size_t bos = __bos0(buf);
201
202 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
203 return __fwrite_real(buf, size, count, stream);
204 }
205
206 if (__builtin_constant_p(size) && __builtin_constant_p(count)) {
207 size_t total;
208 if (__size_mul_overflow(size, count, &total)) {
209 __fwrite_overflow();
210 }
211
212 if (total > bos) {
213 __fwrite_too_big_error();
214 }
215
216 return __fwrite_real(buf, size, count, stream);
217 }
218
219 return __fwrite_chk(buf, size, count, stream, bos);
220}
221#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
222
223#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
224__BIONIC_FORTIFY_INLINE
225char *fgets(char* dest, int size, FILE* stream) {
226 size_t bos = __bos(dest);
227
228 // Compiler can prove, at compile time, that the passed in size
229 // is always negative. Force a compiler error.
230 if (__builtin_constant_p(size) && (size < 0)) {
231 __fgets_too_small_error();
232 }
233
234 // Compiler doesn't know destination size. Don't call __fgets_chk
235 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
236 return __fgets_real(dest, size, stream);
237 }
238
239 // Compiler can prove, at compile time, that the passed in size
240 // is always <= the actual object size. Don't call __fgets_chk
241 if (__builtin_constant_p(size) && (size <= (int) bos)) {
242 return __fgets_real(dest, size, stream);
243 }
244
245 // Compiler can prove, at compile time, that the passed in size
246 // is always > the actual object size. Force a compiler error.
247 if (__builtin_constant_p(size) && (size > (int) bos)) {
248 __fgets_too_big_error();
249 }
250
251 return __fgets_chk(dest, size, stream, bos);
252}
253#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
254
255#endif /* defined(__clang__) */
256#endif /* defined(__BIONIC_FORTIFY) */