1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #ifndef _STDIO_H_
30*8d67ca89SAndroid Build Coastguard Worker #error "Never include this file directly; instead, include <stdio.h>"
31*8d67ca89SAndroid Build Coastguard Worker #endif
32*8d67ca89SAndroid Build Coastguard Worker
33*8d67ca89SAndroid Build Coastguard Worker char* _Nullable __fgets_chk(char* _Nonnull, int, FILE* _Nonnull, size_t);
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(24)
36*8d67ca89SAndroid Build Coastguard Worker size_t __fread_chk(void* _Nonnull, size_t, size_t, FILE* _Nonnull, size_t) __INTRODUCED_IN(24);
37*8d67ca89SAndroid Build Coastguard Worker size_t __fwrite_chk(const void* _Nonnull, size_t, size_t, FILE* _Nonnull, size_t) __INTRODUCED_IN(24);
38*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
39*8d67ca89SAndroid Build Coastguard Worker
40*8d67ca89SAndroid Build Coastguard Worker
41*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
42*8d67ca89SAndroid Build Coastguard Worker
43*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
44*8d67ca89SAndroid Build Coastguard Worker /* No diag -- clang diagnoses misuses of this on its own. */
45*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_INLINE __printflike(3, 0)
vsnprintf(char * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest,size_t size,const char * _Nonnull format,va_list ap)46*8d67ca89SAndroid Build Coastguard Worker int vsnprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, size_t size, const char* _Nonnull format, va_list ap)
47*8d67ca89SAndroid Build Coastguard Worker __diagnose_as_builtin(__builtin_vsnprintf, 1, 2, 3, 4)
48*8d67ca89SAndroid Build Coastguard Worker __overloadable {
49*8d67ca89SAndroid Build Coastguard Worker return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
50*8d67ca89SAndroid Build Coastguard Worker }
51*8d67ca89SAndroid Build Coastguard Worker
52*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_INLINE __printflike(2, 0)
vsprintf(char * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest,const char * _Nonnull format,va_list ap)53*8d67ca89SAndroid Build Coastguard Worker int vsprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, const char* _Nonnull format, va_list ap) __overloadable {
54*8d67ca89SAndroid Build Coastguard Worker return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
55*8d67ca89SAndroid Build Coastguard Worker }
56*8d67ca89SAndroid Build Coastguard Worker #endif
57*8d67ca89SAndroid Build Coastguard Worker
58*8d67ca89SAndroid Build Coastguard Worker __BIONIC_ERROR_FUNCTION_VISIBILITY
59*8d67ca89SAndroid Build Coastguard Worker int sprintf(char* __BIONIC_COMPLICATED_NULLNESS dest, const char* _Nonnull format)
60*8d67ca89SAndroid Build Coastguard Worker __overloadable
61*8d67ca89SAndroid Build Coastguard Worker __enable_if(__bos_unevaluated_lt(__bos(dest), __builtin_strlen(format)),
62*8d67ca89SAndroid Build Coastguard Worker "format string will always overflow destination buffer")
63*8d67ca89SAndroid Build Coastguard Worker __errorattr("format string will always overflow destination buffer");
64*8d67ca89SAndroid Build Coastguard Worker
65*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
66*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
sprintf(char * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest,const char * _Nonnull format,...)67*8d67ca89SAndroid Build Coastguard Worker int sprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, const char* _Nonnull format, ...) __overloadable {
68*8d67ca89SAndroid Build Coastguard Worker va_list va;
69*8d67ca89SAndroid Build Coastguard Worker va_start(va, format);
70*8d67ca89SAndroid Build Coastguard Worker int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
71*8d67ca89SAndroid Build Coastguard Worker va_end(va);
72*8d67ca89SAndroid Build Coastguard Worker return result;
73*8d67ca89SAndroid Build Coastguard Worker }
74*8d67ca89SAndroid Build Coastguard Worker
75*8d67ca89SAndroid Build Coastguard Worker /* No diag -- clang diagnoses misuses of this on its own. */
76*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
snprintf(char * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest,size_t size,const char * _Nonnull format,...)77*8d67ca89SAndroid Build Coastguard Worker int snprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, size_t size, const char* _Nonnull format, ...)
78*8d67ca89SAndroid Build Coastguard Worker __diagnose_as_builtin(__builtin_snprintf, 1, 2, 3)
79*8d67ca89SAndroid Build Coastguard Worker __overloadable {
80*8d67ca89SAndroid Build Coastguard Worker va_list va;
81*8d67ca89SAndroid Build Coastguard Worker va_start(va, format);
82*8d67ca89SAndroid Build Coastguard Worker int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
83*8d67ca89SAndroid Build Coastguard Worker va_end(va);
84*8d67ca89SAndroid Build Coastguard Worker return result;
85*8d67ca89SAndroid Build Coastguard Worker }
86*8d67ca89SAndroid Build Coastguard Worker #endif
87*8d67ca89SAndroid Build Coastguard Worker
88*8d67ca89SAndroid Build Coastguard Worker #define __bos_trivially_ge_mul(bos_val, size, count) \
89*8d67ca89SAndroid Build Coastguard Worker __bos_dynamic_check_impl_and(bos_val, >=, (size) * (count), \
90*8d67ca89SAndroid Build Coastguard Worker !__unsafe_check_mul_overflow(size, count))
91*8d67ca89SAndroid Build Coastguard Worker
92*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_INLINE
fread(void * const _Nonnull __pass_object_size0 buf,size_t size,size_t count,FILE * _Nonnull stream)93*8d67ca89SAndroid Build Coastguard Worker size_t fread(void* const _Nonnull __pass_object_size0 buf, size_t size, size_t count, FILE* _Nonnull stream)
94*8d67ca89SAndroid Build Coastguard Worker __overloadable
95*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(__unsafe_check_mul_overflow(size, count),
96*8d67ca89SAndroid Build Coastguard Worker "in call to 'fread', size * count overflows")
97*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
98*8d67ca89SAndroid Build Coastguard Worker "in call to 'fread', size * count is too large for the given buffer") {
99*8d67ca89SAndroid Build Coastguard Worker #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
100*8d67ca89SAndroid Build Coastguard Worker size_t bos = __bos0(buf);
101*8d67ca89SAndroid Build Coastguard Worker
102*8d67ca89SAndroid Build Coastguard Worker if (!__bos_trivially_ge_mul(bos, size, count)) {
103*8d67ca89SAndroid Build Coastguard Worker return __fread_chk(buf, size, count, stream, bos);
104*8d67ca89SAndroid Build Coastguard Worker }
105*8d67ca89SAndroid Build Coastguard Worker #endif
106*8d67ca89SAndroid Build Coastguard Worker return __call_bypassing_fortify(fread)(buf, size, count, stream);
107*8d67ca89SAndroid Build Coastguard Worker }
108*8d67ca89SAndroid Build Coastguard Worker
109*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_INLINE
fwrite(const void * const _Nonnull __pass_object_size0 buf,size_t size,size_t count,FILE * _Nonnull stream)110*8d67ca89SAndroid Build Coastguard Worker size_t fwrite(const void* const _Nonnull __pass_object_size0 buf, size_t size, size_t count, FILE* _Nonnull stream)
111*8d67ca89SAndroid Build Coastguard Worker __overloadable
112*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(__unsafe_check_mul_overflow(size, count),
113*8d67ca89SAndroid Build Coastguard Worker "in call to 'fwrite', size * count overflows")
114*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
115*8d67ca89SAndroid Build Coastguard Worker "in call to 'fwrite', size * count is too large for the given buffer") {
116*8d67ca89SAndroid Build Coastguard Worker #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
117*8d67ca89SAndroid Build Coastguard Worker size_t bos = __bos0(buf);
118*8d67ca89SAndroid Build Coastguard Worker
119*8d67ca89SAndroid Build Coastguard Worker if (!__bos_trivially_ge_mul(bos, size, count)) {
120*8d67ca89SAndroid Build Coastguard Worker return __fwrite_chk(buf, size, count, stream, bos);
121*8d67ca89SAndroid Build Coastguard Worker }
122*8d67ca89SAndroid Build Coastguard Worker #endif
123*8d67ca89SAndroid Build Coastguard Worker return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
124*8d67ca89SAndroid Build Coastguard Worker }
125*8d67ca89SAndroid Build Coastguard Worker #undef __bos_trivially_ge_mul
126*8d67ca89SAndroid Build Coastguard Worker
127*8d67ca89SAndroid Build Coastguard Worker __BIONIC_FORTIFY_INLINE
fgets(char * const _Nonnull __pass_object_size dest,int size,FILE * _Nonnull stream)128*8d67ca89SAndroid Build Coastguard Worker char* _Nullable fgets(char* const _Nonnull __pass_object_size dest, int size, FILE* _Nonnull stream)
129*8d67ca89SAndroid Build Coastguard Worker __overloadable
130*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
131*8d67ca89SAndroid Build Coastguard Worker __clang_error_if(__bos_unevaluated_lt(__bos(dest), size),
132*8d67ca89SAndroid Build Coastguard Worker "in call to 'fgets', size is larger than the destination buffer") {
133*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
134*8d67ca89SAndroid Build Coastguard Worker size_t bos = __bos(dest);
135*8d67ca89SAndroid Build Coastguard Worker
136*8d67ca89SAndroid Build Coastguard Worker if (!__bos_dynamic_check_impl_and(bos, >=, (size_t)size, size >= 0)) {
137*8d67ca89SAndroid Build Coastguard Worker return __fgets_chk(dest, size, stream, bos);
138*8d67ca89SAndroid Build Coastguard Worker }
139*8d67ca89SAndroid Build Coastguard Worker #endif
140*8d67ca89SAndroid Build Coastguard Worker return __call_bypassing_fortify(fgets)(dest, size, stream);
141*8d67ca89SAndroid Build Coastguard Worker }
142*8d67ca89SAndroid Build Coastguard Worker
143*8d67ca89SAndroid Build Coastguard Worker #endif /* defined(__BIONIC_FORTIFY) */
144