xref: /aosp_15_r20/external/compiler-rt/lib/dfsan/dfsan_custom.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- dfsan.cc ----------------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of DataFlowSanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // This file defines the custom functions listed in done_abilist.txt.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
16*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_internal_defs.h"
17*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_linux.h"
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot #include "dfsan/dfsan.h"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot #include <arpa/inet.h>
22*7c3d14c8STreehugger Robot #include <assert.h>
23*7c3d14c8STreehugger Robot #include <ctype.h>
24*7c3d14c8STreehugger Robot #include <dlfcn.h>
25*7c3d14c8STreehugger Robot #include <link.h>
26*7c3d14c8STreehugger Robot #include <poll.h>
27*7c3d14c8STreehugger Robot #include <pthread.h>
28*7c3d14c8STreehugger Robot #include <pwd.h>
29*7c3d14c8STreehugger Robot #include <sched.h>
30*7c3d14c8STreehugger Robot #include <signal.h>
31*7c3d14c8STreehugger Robot #include <stdarg.h>
32*7c3d14c8STreehugger Robot #include <stdint.h>
33*7c3d14c8STreehugger Robot #include <stdio.h>
34*7c3d14c8STreehugger Robot #include <stdlib.h>
35*7c3d14c8STreehugger Robot #include <string.h>
36*7c3d14c8STreehugger Robot #include <sys/resource.h>
37*7c3d14c8STreehugger Robot #include <sys/select.h>
38*7c3d14c8STreehugger Robot #include <sys/stat.h>
39*7c3d14c8STreehugger Robot #include <sys/time.h>
40*7c3d14c8STreehugger Robot #include <sys/types.h>
41*7c3d14c8STreehugger Robot #include <time.h>
42*7c3d14c8STreehugger Robot #include <unistd.h>
43*7c3d14c8STreehugger Robot 
44*7c3d14c8STreehugger Robot using namespace __dfsan;
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)                                     \
47*7c3d14c8STreehugger Robot   do {                                                                         \
48*7c3d14c8STreehugger Robot     if (f)                                                                     \
49*7c3d14c8STreehugger Robot       f(__VA_ARGS__);                                                          \
50*7c3d14c8STreehugger Robot   } while (false)
51*7c3d14c8STreehugger Robot #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
52*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void f(__VA_ARGS__);
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot extern "C" {
55*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_stat(const char * path,struct stat * buf,dfsan_label path_label,dfsan_label buf_label,dfsan_label * ret_label)56*7c3d14c8STreehugger Robot __dfsw_stat(const char *path, struct stat *buf, dfsan_label path_label,
57*7c3d14c8STreehugger Robot             dfsan_label buf_label, dfsan_label *ret_label) {
58*7c3d14c8STreehugger Robot   int ret = stat(path, buf);
59*7c3d14c8STreehugger Robot   if (ret == 0)
60*7c3d14c8STreehugger Robot     dfsan_set_label(0, buf, sizeof(struct stat));
61*7c3d14c8STreehugger Robot   *ret_label = 0;
62*7c3d14c8STreehugger Robot   return ret;
63*7c3d14c8STreehugger Robot }
64*7c3d14c8STreehugger Robot 
__dfsw_fstat(int fd,struct stat * buf,dfsan_label fd_label,dfsan_label buf_label,dfsan_label * ret_label)65*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_fstat(int fd, struct stat *buf,
66*7c3d14c8STreehugger Robot                                                dfsan_label fd_label,
67*7c3d14c8STreehugger Robot                                                dfsan_label buf_label,
68*7c3d14c8STreehugger Robot                                                dfsan_label *ret_label) {
69*7c3d14c8STreehugger Robot   int ret = fstat(fd, buf);
70*7c3d14c8STreehugger Robot   if (ret == 0)
71*7c3d14c8STreehugger Robot     dfsan_set_label(0, buf, sizeof(struct stat));
72*7c3d14c8STreehugger Robot   *ret_label = 0;
73*7c3d14c8STreehugger Robot   return ret;
74*7c3d14c8STreehugger Robot }
75*7c3d14c8STreehugger Robot 
__dfsw_strchr(const char * s,int c,dfsan_label s_label,dfsan_label c_label,dfsan_label * ret_label)76*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strchr(const char *s, int c,
77*7c3d14c8STreehugger Robot                                                   dfsan_label s_label,
78*7c3d14c8STreehugger Robot                                                   dfsan_label c_label,
79*7c3d14c8STreehugger Robot                                                   dfsan_label *ret_label) {
80*7c3d14c8STreehugger Robot   for (size_t i = 0;; ++i) {
81*7c3d14c8STreehugger Robot     if (s[i] == c || s[i] == 0) {
82*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
83*7c3d14c8STreehugger Robot         *ret_label = s_label;
84*7c3d14c8STreehugger Robot       } else {
85*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(s, i + 1),
86*7c3d14c8STreehugger Robot                                  dfsan_union(s_label, c_label));
87*7c3d14c8STreehugger Robot       }
88*7c3d14c8STreehugger Robot       return s[i] == 0 ? nullptr : const_cast<char *>(s+i);
89*7c3d14c8STreehugger Robot     }
90*7c3d14c8STreehugger Robot   }
91*7c3d14c8STreehugger Robot }
92*7c3d14c8STreehugger Robot 
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp,uptr caller_pc,const void * s1,const void * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label)93*7c3d14c8STreehugger Robot DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, uptr caller_pc,
94*7c3d14c8STreehugger Robot                               const void *s1, const void *s2, size_t n,
95*7c3d14c8STreehugger Robot                               dfsan_label s1_label, dfsan_label s2_label,
96*7c3d14c8STreehugger Robot                               dfsan_label n_label)
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_memcmp(const void *s1, const void *s2,
99*7c3d14c8STreehugger Robot                                                 size_t n, dfsan_label s1_label,
100*7c3d14c8STreehugger Robot                                                 dfsan_label s2_label,
101*7c3d14c8STreehugger Robot                                                 dfsan_label n_label,
102*7c3d14c8STreehugger Robot                                                 dfsan_label *ret_label) {
103*7c3d14c8STreehugger Robot   CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_memcmp, GET_CALLER_PC(), s1, s2, n,
104*7c3d14c8STreehugger Robot                              s1_label, s2_label, n_label);
105*7c3d14c8STreehugger Robot   const char *cs1 = (const char *) s1, *cs2 = (const char *) s2;
106*7c3d14c8STreehugger Robot   for (size_t i = 0; i != n; ++i) {
107*7c3d14c8STreehugger Robot     if (cs1[i] != cs2[i]) {
108*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
109*7c3d14c8STreehugger Robot         *ret_label = 0;
110*7c3d14c8STreehugger Robot       } else {
111*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(cs1, i + 1),
112*7c3d14c8STreehugger Robot                                  dfsan_read_label(cs2, i + 1));
113*7c3d14c8STreehugger Robot       }
114*7c3d14c8STreehugger Robot       return cs1[i] - cs2[i];
115*7c3d14c8STreehugger Robot     }
116*7c3d14c8STreehugger Robot   }
117*7c3d14c8STreehugger Robot 
118*7c3d14c8STreehugger Robot   if (flags().strict_data_dependencies) {
119*7c3d14c8STreehugger Robot     *ret_label = 0;
120*7c3d14c8STreehugger Robot   } else {
121*7c3d14c8STreehugger Robot     *ret_label = dfsan_union(dfsan_read_label(cs1, n),
122*7c3d14c8STreehugger Robot                              dfsan_read_label(cs2, n));
123*7c3d14c8STreehugger Robot   }
124*7c3d14c8STreehugger Robot   return 0;
125*7c3d14c8STreehugger Robot }
126*7c3d14c8STreehugger Robot 
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp,uptr caller_pc,const char * s1,const char * s2,dfsan_label s1_label,dfsan_label s2_label)127*7c3d14c8STreehugger Robot DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, uptr caller_pc,
128*7c3d14c8STreehugger Robot                               const char *s1, const char *s2,
129*7c3d14c8STreehugger Robot                               dfsan_label s1_label, dfsan_label s2_label)
130*7c3d14c8STreehugger Robot 
131*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strcmp(const char *s1, const char *s2,
132*7c3d14c8STreehugger Robot                                                 dfsan_label s1_label,
133*7c3d14c8STreehugger Robot                                                 dfsan_label s2_label,
134*7c3d14c8STreehugger Robot                                                 dfsan_label *ret_label) {
135*7c3d14c8STreehugger Robot   CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strcmp, GET_CALLER_PC(), s1, s2,
136*7c3d14c8STreehugger Robot                              s1_label, s2_label);
137*7c3d14c8STreehugger Robot   for (size_t i = 0;; ++i) {
138*7c3d14c8STreehugger Robot     if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0) {
139*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
140*7c3d14c8STreehugger Robot         *ret_label = 0;
141*7c3d14c8STreehugger Robot       } else {
142*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
143*7c3d14c8STreehugger Robot                                  dfsan_read_label(s2, i + 1));
144*7c3d14c8STreehugger Robot       }
145*7c3d14c8STreehugger Robot       return s1[i] - s2[i];
146*7c3d14c8STreehugger Robot     }
147*7c3d14c8STreehugger Robot   }
148*7c3d14c8STreehugger Robot   return 0;
149*7c3d14c8STreehugger Robot }
150*7c3d14c8STreehugger Robot 
151*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_strcasecmp(const char * s1,const char * s2,dfsan_label s1_label,dfsan_label s2_label,dfsan_label * ret_label)152*7c3d14c8STreehugger Robot __dfsw_strcasecmp(const char *s1, const char *s2, dfsan_label s1_label,
153*7c3d14c8STreehugger Robot                   dfsan_label s2_label, dfsan_label *ret_label) {
154*7c3d14c8STreehugger Robot   for (size_t i = 0;; ++i) {
155*7c3d14c8STreehugger Robot     if (tolower(s1[i]) != tolower(s2[i]) || s1[i] == 0 || s2[i] == 0) {
156*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
157*7c3d14c8STreehugger Robot         *ret_label = 0;
158*7c3d14c8STreehugger Robot       } else {
159*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
160*7c3d14c8STreehugger Robot                                  dfsan_read_label(s2, i + 1));
161*7c3d14c8STreehugger Robot       }
162*7c3d14c8STreehugger Robot       return s1[i] - s2[i];
163*7c3d14c8STreehugger Robot     }
164*7c3d14c8STreehugger Robot   }
165*7c3d14c8STreehugger Robot   return 0;
166*7c3d14c8STreehugger Robot }
167*7c3d14c8STreehugger Robot 
DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp,uptr caller_pc,const char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label)168*7c3d14c8STreehugger Robot DECLARE_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, uptr caller_pc,
169*7c3d14c8STreehugger Robot                               const char *s1, const char *s2, size_t n,
170*7c3d14c8STreehugger Robot                               dfsan_label s1_label, dfsan_label s2_label,
171*7c3d14c8STreehugger Robot                               dfsan_label n_label)
172*7c3d14c8STreehugger Robot 
173*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_strncmp(const char *s1, const char *s2,
174*7c3d14c8STreehugger Robot                                                  size_t n, dfsan_label s1_label,
175*7c3d14c8STreehugger Robot                                                  dfsan_label s2_label,
176*7c3d14c8STreehugger Robot                                                  dfsan_label n_label,
177*7c3d14c8STreehugger Robot                                                  dfsan_label *ret_label) {
178*7c3d14c8STreehugger Robot   if (n == 0) {
179*7c3d14c8STreehugger Robot     *ret_label = 0;
180*7c3d14c8STreehugger Robot     return 0;
181*7c3d14c8STreehugger Robot   }
182*7c3d14c8STreehugger Robot 
183*7c3d14c8STreehugger Robot   CALL_WEAK_INTERCEPTOR_HOOK(dfsan_weak_hook_strncmp, GET_CALLER_PC(), s1, s2,
184*7c3d14c8STreehugger Robot                              n, s1_label, s2_label, n_label);
185*7c3d14c8STreehugger Robot 
186*7c3d14c8STreehugger Robot   for (size_t i = 0;; ++i) {
187*7c3d14c8STreehugger Robot     if (s1[i] != s2[i] || s1[i] == 0 || s2[i] == 0 || i == n - 1) {
188*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
189*7c3d14c8STreehugger Robot         *ret_label = 0;
190*7c3d14c8STreehugger Robot       } else {
191*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
192*7c3d14c8STreehugger Robot                                  dfsan_read_label(s2, i + 1));
193*7c3d14c8STreehugger Robot       }
194*7c3d14c8STreehugger Robot       return s1[i] - s2[i];
195*7c3d14c8STreehugger Robot     }
196*7c3d14c8STreehugger Robot   }
197*7c3d14c8STreehugger Robot   return 0;
198*7c3d14c8STreehugger Robot }
199*7c3d14c8STreehugger Robot 
200*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_strncasecmp(const char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)201*7c3d14c8STreehugger Robot __dfsw_strncasecmp(const char *s1, const char *s2, size_t n,
202*7c3d14c8STreehugger Robot                    dfsan_label s1_label, dfsan_label s2_label,
203*7c3d14c8STreehugger Robot                    dfsan_label n_label, dfsan_label *ret_label) {
204*7c3d14c8STreehugger Robot   if (n == 0) {
205*7c3d14c8STreehugger Robot     *ret_label = 0;
206*7c3d14c8STreehugger Robot     return 0;
207*7c3d14c8STreehugger Robot   }
208*7c3d14c8STreehugger Robot 
209*7c3d14c8STreehugger Robot   for (size_t i = 0;; ++i) {
210*7c3d14c8STreehugger Robot     if (tolower(s1[i]) != tolower(s2[i]) || s1[i] == 0 || s2[i] == 0 ||
211*7c3d14c8STreehugger Robot         i == n - 1) {
212*7c3d14c8STreehugger Robot       if (flags().strict_data_dependencies) {
213*7c3d14c8STreehugger Robot         *ret_label = 0;
214*7c3d14c8STreehugger Robot       } else {
215*7c3d14c8STreehugger Robot         *ret_label = dfsan_union(dfsan_read_label(s1, i + 1),
216*7c3d14c8STreehugger Robot                                  dfsan_read_label(s2, i + 1));
217*7c3d14c8STreehugger Robot       }
218*7c3d14c8STreehugger Robot       return s1[i] - s2[i];
219*7c3d14c8STreehugger Robot     }
220*7c3d14c8STreehugger Robot   }
221*7c3d14c8STreehugger Robot   return 0;
222*7c3d14c8STreehugger Robot }
223*7c3d14c8STreehugger Robot 
__dfsw_calloc(size_t nmemb,size_t size,dfsan_label nmemb_label,dfsan_label size_label,dfsan_label * ret_label)224*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE void *__dfsw_calloc(size_t nmemb, size_t size,
225*7c3d14c8STreehugger Robot                                                   dfsan_label nmemb_label,
226*7c3d14c8STreehugger Robot                                                   dfsan_label size_label,
227*7c3d14c8STreehugger Robot                                                   dfsan_label *ret_label) {
228*7c3d14c8STreehugger Robot   void *p = calloc(nmemb, size);
229*7c3d14c8STreehugger Robot   dfsan_set_label(0, p, nmemb * size);
230*7c3d14c8STreehugger Robot   *ret_label = 0;
231*7c3d14c8STreehugger Robot   return p;
232*7c3d14c8STreehugger Robot }
233*7c3d14c8STreehugger Robot 
234*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE size_t
__dfsw_strlen(const char * s,dfsan_label s_label,dfsan_label * ret_label)235*7c3d14c8STreehugger Robot __dfsw_strlen(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
236*7c3d14c8STreehugger Robot   size_t ret = strlen(s);
237*7c3d14c8STreehugger Robot   if (flags().strict_data_dependencies) {
238*7c3d14c8STreehugger Robot     *ret_label = 0;
239*7c3d14c8STreehugger Robot   } else {
240*7c3d14c8STreehugger Robot     *ret_label = dfsan_read_label(s, ret + 1);
241*7c3d14c8STreehugger Robot   }
242*7c3d14c8STreehugger Robot   return ret;
243*7c3d14c8STreehugger Robot }
244*7c3d14c8STreehugger Robot 
245*7c3d14c8STreehugger Robot 
dfsan_memcpy(void * dest,const void * src,size_t n)246*7c3d14c8STreehugger Robot static void *dfsan_memcpy(void *dest, const void *src, size_t n) {
247*7c3d14c8STreehugger Robot   dfsan_label *sdest = shadow_for(dest);
248*7c3d14c8STreehugger Robot   const dfsan_label *ssrc = shadow_for(src);
249*7c3d14c8STreehugger Robot   internal_memcpy((void *)sdest, (const void *)ssrc, n * sizeof(dfsan_label));
250*7c3d14c8STreehugger Robot   return internal_memcpy(dest, src, n);
251*7c3d14c8STreehugger Robot }
252*7c3d14c8STreehugger Robot 
dfsan_memset(void * s,int c,dfsan_label c_label,size_t n)253*7c3d14c8STreehugger Robot static void dfsan_memset(void *s, int c, dfsan_label c_label, size_t n) {
254*7c3d14c8STreehugger Robot   internal_memset(s, c, n);
255*7c3d14c8STreehugger Robot   dfsan_set_label(c_label, s, n);
256*7c3d14c8STreehugger Robot }
257*7c3d14c8STreehugger Robot 
258*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_memcpy(void * dest,const void * src,size_t n,dfsan_label dest_label,dfsan_label src_label,dfsan_label n_label,dfsan_label * ret_label)259*7c3d14c8STreehugger Robot void *__dfsw_memcpy(void *dest, const void *src, size_t n,
260*7c3d14c8STreehugger Robot                     dfsan_label dest_label, dfsan_label src_label,
261*7c3d14c8STreehugger Robot                     dfsan_label n_label, dfsan_label *ret_label) {
262*7c3d14c8STreehugger Robot   *ret_label = dest_label;
263*7c3d14c8STreehugger Robot   return dfsan_memcpy(dest, src, n);
264*7c3d14c8STreehugger Robot }
265*7c3d14c8STreehugger Robot 
266*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_memset(void * s,int c,size_t n,dfsan_label s_label,dfsan_label c_label,dfsan_label n_label,dfsan_label * ret_label)267*7c3d14c8STreehugger Robot void *__dfsw_memset(void *s, int c, size_t n,
268*7c3d14c8STreehugger Robot                     dfsan_label s_label, dfsan_label c_label,
269*7c3d14c8STreehugger Robot                     dfsan_label n_label, dfsan_label *ret_label) {
270*7c3d14c8STreehugger Robot   dfsan_memset(s, c, c_label, n);
271*7c3d14c8STreehugger Robot   *ret_label = s_label;
272*7c3d14c8STreehugger Robot   return s;
273*7c3d14c8STreehugger Robot }
274*7c3d14c8STreehugger Robot 
275*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE char *
__dfsw_strdup(const char * s,dfsan_label s_label,dfsan_label * ret_label)276*7c3d14c8STreehugger Robot __dfsw_strdup(const char *s, dfsan_label s_label, dfsan_label *ret_label) {
277*7c3d14c8STreehugger Robot   size_t len = strlen(s);
278*7c3d14c8STreehugger Robot   void *p = malloc(len+1);
279*7c3d14c8STreehugger Robot   dfsan_memcpy(p, s, len+1);
280*7c3d14c8STreehugger Robot   *ret_label = 0;
281*7c3d14c8STreehugger Robot   return static_cast<char *>(p);
282*7c3d14c8STreehugger Robot }
283*7c3d14c8STreehugger Robot 
284*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE char *
__dfsw_strncpy(char * s1,const char * s2,size_t n,dfsan_label s1_label,dfsan_label s2_label,dfsan_label n_label,dfsan_label * ret_label)285*7c3d14c8STreehugger Robot __dfsw_strncpy(char *s1, const char *s2, size_t n, dfsan_label s1_label,
286*7c3d14c8STreehugger Robot                dfsan_label s2_label, dfsan_label n_label,
287*7c3d14c8STreehugger Robot                dfsan_label *ret_label) {
288*7c3d14c8STreehugger Robot   size_t len = strlen(s2);
289*7c3d14c8STreehugger Robot   if (len < n) {
290*7c3d14c8STreehugger Robot     dfsan_memcpy(s1, s2, len+1);
291*7c3d14c8STreehugger Robot     dfsan_memset(s1+len+1, 0, 0, n-len-1);
292*7c3d14c8STreehugger Robot   } else {
293*7c3d14c8STreehugger Robot     dfsan_memcpy(s1, s2, n);
294*7c3d14c8STreehugger Robot   }
295*7c3d14c8STreehugger Robot 
296*7c3d14c8STreehugger Robot   *ret_label = s1_label;
297*7c3d14c8STreehugger Robot   return s1;
298*7c3d14c8STreehugger Robot }
299*7c3d14c8STreehugger Robot 
300*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE ssize_t
__dfsw_pread(int fd,void * buf,size_t count,off_t offset,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label offset_label,dfsan_label * ret_label)301*7c3d14c8STreehugger Robot __dfsw_pread(int fd, void *buf, size_t count, off_t offset,
302*7c3d14c8STreehugger Robot              dfsan_label fd_label, dfsan_label buf_label,
303*7c3d14c8STreehugger Robot              dfsan_label count_label, dfsan_label offset_label,
304*7c3d14c8STreehugger Robot              dfsan_label *ret_label) {
305*7c3d14c8STreehugger Robot   ssize_t ret = pread(fd, buf, count, offset);
306*7c3d14c8STreehugger Robot   if (ret > 0)
307*7c3d14c8STreehugger Robot     dfsan_set_label(0, buf, ret);
308*7c3d14c8STreehugger Robot   *ret_label = 0;
309*7c3d14c8STreehugger Robot   return ret;
310*7c3d14c8STreehugger Robot }
311*7c3d14c8STreehugger Robot 
312*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE ssize_t
__dfsw_read(int fd,void * buf,size_t count,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label * ret_label)313*7c3d14c8STreehugger Robot __dfsw_read(int fd, void *buf, size_t count,
314*7c3d14c8STreehugger Robot              dfsan_label fd_label, dfsan_label buf_label,
315*7c3d14c8STreehugger Robot              dfsan_label count_label,
316*7c3d14c8STreehugger Robot              dfsan_label *ret_label) {
317*7c3d14c8STreehugger Robot   ssize_t ret = read(fd, buf, count);
318*7c3d14c8STreehugger Robot   if (ret > 0)
319*7c3d14c8STreehugger Robot     dfsan_set_label(0, buf, ret);
320*7c3d14c8STreehugger Robot   *ret_label = 0;
321*7c3d14c8STreehugger Robot   return ret;
322*7c3d14c8STreehugger Robot }
323*7c3d14c8STreehugger Robot 
__dfsw_clock_gettime(clockid_t clk_id,struct timespec * tp,dfsan_label clk_id_label,dfsan_label tp_label,dfsan_label * ret_label)324*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_clock_gettime(clockid_t clk_id,
325*7c3d14c8STreehugger Robot                                                        struct timespec *tp,
326*7c3d14c8STreehugger Robot                                                        dfsan_label clk_id_label,
327*7c3d14c8STreehugger Robot                                                        dfsan_label tp_label,
328*7c3d14c8STreehugger Robot                                                        dfsan_label *ret_label) {
329*7c3d14c8STreehugger Robot   int ret = clock_gettime(clk_id, tp);
330*7c3d14c8STreehugger Robot   if (ret == 0)
331*7c3d14c8STreehugger Robot     dfsan_set_label(0, tp, sizeof(struct timespec));
332*7c3d14c8STreehugger Robot   *ret_label = 0;
333*7c3d14c8STreehugger Robot   return ret;
334*7c3d14c8STreehugger Robot }
335*7c3d14c8STreehugger Robot 
unpoison(const void * ptr,uptr size)336*7c3d14c8STreehugger Robot static void unpoison(const void *ptr, uptr size) {
337*7c3d14c8STreehugger Robot   dfsan_set_label(0, const_cast<void *>(ptr), size);
338*7c3d14c8STreehugger Robot }
339*7c3d14c8STreehugger Robot 
340*7c3d14c8STreehugger Robot // dlopen() ultimately calls mmap() down inside the loader, which generally
341*7c3d14c8STreehugger Robot // doesn't participate in dynamic symbol resolution.  Therefore we won't
342*7c3d14c8STreehugger Robot // intercept its calls to mmap, and we have to hook it here.
343*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE void *
__dfsw_dlopen(const char * filename,int flag,dfsan_label filename_label,dfsan_label flag_label,dfsan_label * ret_label)344*7c3d14c8STreehugger Robot __dfsw_dlopen(const char *filename, int flag, dfsan_label filename_label,
345*7c3d14c8STreehugger Robot               dfsan_label flag_label, dfsan_label *ret_label) {
346*7c3d14c8STreehugger Robot   void *handle = dlopen(filename, flag);
347*7c3d14c8STreehugger Robot   link_map *map = GET_LINK_MAP_BY_DLOPEN_HANDLE(handle);
348*7c3d14c8STreehugger Robot   if (map)
349*7c3d14c8STreehugger Robot     ForEachMappedRegion(map, unpoison);
350*7c3d14c8STreehugger Robot   *ret_label = 0;
351*7c3d14c8STreehugger Robot   return handle;
352*7c3d14c8STreehugger Robot }
353*7c3d14c8STreehugger Robot 
354*7c3d14c8STreehugger Robot struct pthread_create_info {
355*7c3d14c8STreehugger Robot   void *(*start_routine_trampoline)(void *, void *, dfsan_label, dfsan_label *);
356*7c3d14c8STreehugger Robot   void *start_routine;
357*7c3d14c8STreehugger Robot   void *arg;
358*7c3d14c8STreehugger Robot };
359*7c3d14c8STreehugger Robot 
pthread_create_cb(void * p)360*7c3d14c8STreehugger Robot static void *pthread_create_cb(void *p) {
361*7c3d14c8STreehugger Robot   pthread_create_info pci(*(pthread_create_info *)p);
362*7c3d14c8STreehugger Robot   free(p);
363*7c3d14c8STreehugger Robot   dfsan_label ret_label;
364*7c3d14c8STreehugger Robot   return pci.start_routine_trampoline(pci.start_routine, pci.arg, 0,
365*7c3d14c8STreehugger Robot                                       &ret_label);
366*7c3d14c8STreehugger Robot }
367*7c3d14c8STreehugger Robot 
__dfsw_pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine_trampoline)(void *,void *,dfsan_label,dfsan_label *),void * start_routine,void * arg,dfsan_label thread_label,dfsan_label attr_label,dfsan_label start_routine_label,dfsan_label arg_label,dfsan_label * ret_label)368*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_pthread_create(
369*7c3d14c8STreehugger Robot     pthread_t *thread, const pthread_attr_t *attr,
370*7c3d14c8STreehugger Robot     void *(*start_routine_trampoline)(void *, void *, dfsan_label,
371*7c3d14c8STreehugger Robot                                       dfsan_label *),
372*7c3d14c8STreehugger Robot     void *start_routine, void *arg, dfsan_label thread_label,
373*7c3d14c8STreehugger Robot     dfsan_label attr_label, dfsan_label start_routine_label,
374*7c3d14c8STreehugger Robot     dfsan_label arg_label, dfsan_label *ret_label) {
375*7c3d14c8STreehugger Robot   pthread_create_info *pci =
376*7c3d14c8STreehugger Robot       (pthread_create_info *)malloc(sizeof(pthread_create_info));
377*7c3d14c8STreehugger Robot   pci->start_routine_trampoline = start_routine_trampoline;
378*7c3d14c8STreehugger Robot   pci->start_routine = start_routine;
379*7c3d14c8STreehugger Robot   pci->arg = arg;
380*7c3d14c8STreehugger Robot   int rv = pthread_create(thread, attr, pthread_create_cb, (void *)pci);
381*7c3d14c8STreehugger Robot   if (rv != 0)
382*7c3d14c8STreehugger Robot     free(pci);
383*7c3d14c8STreehugger Robot   *ret_label = 0;
384*7c3d14c8STreehugger Robot   return rv;
385*7c3d14c8STreehugger Robot }
386*7c3d14c8STreehugger Robot 
387*7c3d14c8STreehugger Robot struct dl_iterate_phdr_info {
388*7c3d14c8STreehugger Robot   int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
389*7c3d14c8STreehugger Robot                              size_t size, void *data, dfsan_label info_label,
390*7c3d14c8STreehugger Robot                              dfsan_label size_label, dfsan_label data_label,
391*7c3d14c8STreehugger Robot                              dfsan_label *ret_label);
392*7c3d14c8STreehugger Robot   void *callback;
393*7c3d14c8STreehugger Robot   void *data;
394*7c3d14c8STreehugger Robot };
395*7c3d14c8STreehugger Robot 
dl_iterate_phdr_cb(struct dl_phdr_info * info,size_t size,void * data)396*7c3d14c8STreehugger Robot int dl_iterate_phdr_cb(struct dl_phdr_info *info, size_t size, void *data) {
397*7c3d14c8STreehugger Robot   dl_iterate_phdr_info *dipi = (dl_iterate_phdr_info *)data;
398*7c3d14c8STreehugger Robot   dfsan_set_label(0, *info);
399*7c3d14c8STreehugger Robot   dfsan_set_label(0, const_cast<char *>(info->dlpi_name),
400*7c3d14c8STreehugger Robot                   strlen(info->dlpi_name) + 1);
401*7c3d14c8STreehugger Robot   dfsan_set_label(
402*7c3d14c8STreehugger Robot       0, const_cast<char *>(reinterpret_cast<const char *>(info->dlpi_phdr)),
403*7c3d14c8STreehugger Robot       sizeof(*info->dlpi_phdr) * info->dlpi_phnum);
404*7c3d14c8STreehugger Robot   dfsan_label ret_label;
405*7c3d14c8STreehugger Robot   return dipi->callback_trampoline(dipi->callback, info, size, dipi->data, 0, 0,
406*7c3d14c8STreehugger Robot                                    0, &ret_label);
407*7c3d14c8STreehugger Robot }
408*7c3d14c8STreehugger Robot 
__dfsw_dl_iterate_phdr(int (* callback_trampoline)(void * callback,struct dl_phdr_info * info,size_t size,void * data,dfsan_label info_label,dfsan_label size_label,dfsan_label data_label,dfsan_label * ret_label),void * callback,void * data,dfsan_label callback_label,dfsan_label data_label,dfsan_label * ret_label)409*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_dl_iterate_phdr(
410*7c3d14c8STreehugger Robot     int (*callback_trampoline)(void *callback, struct dl_phdr_info *info,
411*7c3d14c8STreehugger Robot                                size_t size, void *data, dfsan_label info_label,
412*7c3d14c8STreehugger Robot                                dfsan_label size_label, dfsan_label data_label,
413*7c3d14c8STreehugger Robot                                dfsan_label *ret_label),
414*7c3d14c8STreehugger Robot     void *callback, void *data, dfsan_label callback_label,
415*7c3d14c8STreehugger Robot     dfsan_label data_label, dfsan_label *ret_label) {
416*7c3d14c8STreehugger Robot   dl_iterate_phdr_info dipi = { callback_trampoline, callback, data };
417*7c3d14c8STreehugger Robot   *ret_label = 0;
418*7c3d14c8STreehugger Robot   return dl_iterate_phdr(dl_iterate_phdr_cb, &dipi);
419*7c3d14c8STreehugger Robot }
420*7c3d14c8STreehugger Robot 
421*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_ctime_r(const time_t * timep,char * buf,dfsan_label timep_label,dfsan_label buf_label,dfsan_label * ret_label)422*7c3d14c8STreehugger Robot char *__dfsw_ctime_r(const time_t *timep, char *buf, dfsan_label timep_label,
423*7c3d14c8STreehugger Robot                      dfsan_label buf_label, dfsan_label *ret_label) {
424*7c3d14c8STreehugger Robot   char *ret = ctime_r(timep, buf);
425*7c3d14c8STreehugger Robot   if (ret) {
426*7c3d14c8STreehugger Robot     dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), buf,
427*7c3d14c8STreehugger Robot                     strlen(buf) + 1);
428*7c3d14c8STreehugger Robot     *ret_label = buf_label;
429*7c3d14c8STreehugger Robot   } else {
430*7c3d14c8STreehugger Robot     *ret_label = 0;
431*7c3d14c8STreehugger Robot   }
432*7c3d14c8STreehugger Robot   return ret;
433*7c3d14c8STreehugger Robot }
434*7c3d14c8STreehugger Robot 
435*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_fgets(char * s,int size,FILE * stream,dfsan_label s_label,dfsan_label size_label,dfsan_label stream_label,dfsan_label * ret_label)436*7c3d14c8STreehugger Robot char *__dfsw_fgets(char *s, int size, FILE *stream, dfsan_label s_label,
437*7c3d14c8STreehugger Robot                    dfsan_label size_label, dfsan_label stream_label,
438*7c3d14c8STreehugger Robot                    dfsan_label *ret_label) {
439*7c3d14c8STreehugger Robot   char *ret = fgets(s, size, stream);
440*7c3d14c8STreehugger Robot   if (ret) {
441*7c3d14c8STreehugger Robot     dfsan_set_label(0, ret, strlen(ret) + 1);
442*7c3d14c8STreehugger Robot     *ret_label = s_label;
443*7c3d14c8STreehugger Robot   } else {
444*7c3d14c8STreehugger Robot     *ret_label = 0;
445*7c3d14c8STreehugger Robot   }
446*7c3d14c8STreehugger Robot   return ret;
447*7c3d14c8STreehugger Robot }
448*7c3d14c8STreehugger Robot 
449*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getcwd(char * buf,size_t size,dfsan_label buf_label,dfsan_label size_label,dfsan_label * ret_label)450*7c3d14c8STreehugger Robot char *__dfsw_getcwd(char *buf, size_t size, dfsan_label buf_label,
451*7c3d14c8STreehugger Robot                     dfsan_label size_label, dfsan_label *ret_label) {
452*7c3d14c8STreehugger Robot   char *ret = getcwd(buf, size);
453*7c3d14c8STreehugger Robot   if (ret) {
454*7c3d14c8STreehugger Robot     dfsan_set_label(0, ret, strlen(ret) + 1);
455*7c3d14c8STreehugger Robot     *ret_label = buf_label;
456*7c3d14c8STreehugger Robot   } else {
457*7c3d14c8STreehugger Robot     *ret_label = 0;
458*7c3d14c8STreehugger Robot   }
459*7c3d14c8STreehugger Robot   return ret;
460*7c3d14c8STreehugger Robot }
461*7c3d14c8STreehugger Robot 
462*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_get_current_dir_name(dfsan_label * ret_label)463*7c3d14c8STreehugger Robot char *__dfsw_get_current_dir_name(dfsan_label *ret_label) {
464*7c3d14c8STreehugger Robot   char *ret = get_current_dir_name();
465*7c3d14c8STreehugger Robot   if (ret) {
466*7c3d14c8STreehugger Robot     dfsan_set_label(0, ret, strlen(ret) + 1);
467*7c3d14c8STreehugger Robot   }
468*7c3d14c8STreehugger Robot   *ret_label = 0;
469*7c3d14c8STreehugger Robot   return ret;
470*7c3d14c8STreehugger Robot }
471*7c3d14c8STreehugger Robot 
472*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_gethostname(char * name,size_t len,dfsan_label name_label,dfsan_label len_label,dfsan_label * ret_label)473*7c3d14c8STreehugger Robot int __dfsw_gethostname(char *name, size_t len, dfsan_label name_label,
474*7c3d14c8STreehugger Robot                        dfsan_label len_label, dfsan_label *ret_label) {
475*7c3d14c8STreehugger Robot   int ret = gethostname(name, len);
476*7c3d14c8STreehugger Robot   if (ret == 0) {
477*7c3d14c8STreehugger Robot     dfsan_set_label(0, name, strlen(name) + 1);
478*7c3d14c8STreehugger Robot   }
479*7c3d14c8STreehugger Robot   *ret_label = 0;
480*7c3d14c8STreehugger Robot   return ret;
481*7c3d14c8STreehugger Robot }
482*7c3d14c8STreehugger Robot 
483*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getrlimit(int resource,struct rlimit * rlim,dfsan_label resource_label,dfsan_label rlim_label,dfsan_label * ret_label)484*7c3d14c8STreehugger Robot int __dfsw_getrlimit(int resource, struct rlimit *rlim,
485*7c3d14c8STreehugger Robot                      dfsan_label resource_label, dfsan_label rlim_label,
486*7c3d14c8STreehugger Robot                      dfsan_label *ret_label) {
487*7c3d14c8STreehugger Robot   int ret = getrlimit(resource, rlim);
488*7c3d14c8STreehugger Robot   if (ret == 0) {
489*7c3d14c8STreehugger Robot     dfsan_set_label(0, rlim, sizeof(struct rlimit));
490*7c3d14c8STreehugger Robot   }
491*7c3d14c8STreehugger Robot   *ret_label = 0;
492*7c3d14c8STreehugger Robot   return ret;
493*7c3d14c8STreehugger Robot }
494*7c3d14c8STreehugger Robot 
495*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getrusage(int who,struct rusage * usage,dfsan_label who_label,dfsan_label usage_label,dfsan_label * ret_label)496*7c3d14c8STreehugger Robot int __dfsw_getrusage(int who, struct rusage *usage, dfsan_label who_label,
497*7c3d14c8STreehugger Robot                      dfsan_label usage_label, dfsan_label *ret_label) {
498*7c3d14c8STreehugger Robot   int ret = getrusage(who, usage);
499*7c3d14c8STreehugger Robot   if (ret == 0) {
500*7c3d14c8STreehugger Robot     dfsan_set_label(0, usage, sizeof(struct rusage));
501*7c3d14c8STreehugger Robot   }
502*7c3d14c8STreehugger Robot   *ret_label = 0;
503*7c3d14c8STreehugger Robot   return ret;
504*7c3d14c8STreehugger Robot }
505*7c3d14c8STreehugger Robot 
506*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strcpy(char * dest,const char * src,dfsan_label dst_label,dfsan_label src_label,dfsan_label * ret_label)507*7c3d14c8STreehugger Robot char *__dfsw_strcpy(char *dest, const char *src, dfsan_label dst_label,
508*7c3d14c8STreehugger Robot                     dfsan_label src_label, dfsan_label *ret_label) {
509*7c3d14c8STreehugger Robot   char *ret = strcpy(dest, src);
510*7c3d14c8STreehugger Robot   if (ret) {
511*7c3d14c8STreehugger Robot     internal_memcpy(shadow_for(dest), shadow_for(src),
512*7c3d14c8STreehugger Robot                     sizeof(dfsan_label) * (strlen(src) + 1));
513*7c3d14c8STreehugger Robot   }
514*7c3d14c8STreehugger Robot   *ret_label = dst_label;
515*7c3d14c8STreehugger Robot   return ret;
516*7c3d14c8STreehugger Robot }
517*7c3d14c8STreehugger Robot 
518*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtol(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)519*7c3d14c8STreehugger Robot long int __dfsw_strtol(const char *nptr, char **endptr, int base,
520*7c3d14c8STreehugger Robot                        dfsan_label nptr_label, dfsan_label endptr_label,
521*7c3d14c8STreehugger Robot                        dfsan_label base_label, dfsan_label *ret_label) {
522*7c3d14c8STreehugger Robot   char *tmp_endptr;
523*7c3d14c8STreehugger Robot   long int ret = strtol(nptr, &tmp_endptr, base);
524*7c3d14c8STreehugger Robot   if (endptr) {
525*7c3d14c8STreehugger Robot     *endptr = tmp_endptr;
526*7c3d14c8STreehugger Robot   }
527*7c3d14c8STreehugger Robot   if (tmp_endptr > nptr) {
528*7c3d14c8STreehugger Robot     // If *tmp_endptr is '\0' include its label as well.
529*7c3d14c8STreehugger Robot     *ret_label = dfsan_union(
530*7c3d14c8STreehugger Robot         base_label,
531*7c3d14c8STreehugger Robot         dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
532*7c3d14c8STreehugger Robot   } else {
533*7c3d14c8STreehugger Robot     *ret_label = 0;
534*7c3d14c8STreehugger Robot   }
535*7c3d14c8STreehugger Robot   return ret;
536*7c3d14c8STreehugger Robot }
537*7c3d14c8STreehugger Robot 
538*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtod(const char * nptr,char ** endptr,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label * ret_label)539*7c3d14c8STreehugger Robot double __dfsw_strtod(const char *nptr, char **endptr,
540*7c3d14c8STreehugger Robot                        dfsan_label nptr_label, dfsan_label endptr_label,
541*7c3d14c8STreehugger Robot                        dfsan_label *ret_label) {
542*7c3d14c8STreehugger Robot   char *tmp_endptr;
543*7c3d14c8STreehugger Robot   double ret = strtod(nptr, &tmp_endptr);
544*7c3d14c8STreehugger Robot   if (endptr) {
545*7c3d14c8STreehugger Robot     *endptr = tmp_endptr;
546*7c3d14c8STreehugger Robot   }
547*7c3d14c8STreehugger Robot   if (tmp_endptr > nptr) {
548*7c3d14c8STreehugger Robot     // If *tmp_endptr is '\0' include its label as well.
549*7c3d14c8STreehugger Robot     *ret_label = dfsan_read_label(
550*7c3d14c8STreehugger Robot         nptr,
551*7c3d14c8STreehugger Robot         tmp_endptr - nptr + (*tmp_endptr ? 0 : 1));
552*7c3d14c8STreehugger Robot   } else {
553*7c3d14c8STreehugger Robot     *ret_label = 0;
554*7c3d14c8STreehugger Robot   }
555*7c3d14c8STreehugger Robot   return ret;
556*7c3d14c8STreehugger Robot }
557*7c3d14c8STreehugger Robot 
558*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoll(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)559*7c3d14c8STreehugger Robot long long int __dfsw_strtoll(const char *nptr, char **endptr, int base,
560*7c3d14c8STreehugger Robot                        dfsan_label nptr_label, dfsan_label endptr_label,
561*7c3d14c8STreehugger Robot                        dfsan_label base_label, dfsan_label *ret_label) {
562*7c3d14c8STreehugger Robot   char *tmp_endptr;
563*7c3d14c8STreehugger Robot   long long int ret = strtoll(nptr, &tmp_endptr, base);
564*7c3d14c8STreehugger Robot   if (endptr) {
565*7c3d14c8STreehugger Robot     *endptr = tmp_endptr;
566*7c3d14c8STreehugger Robot   }
567*7c3d14c8STreehugger Robot   if (tmp_endptr > nptr) {
568*7c3d14c8STreehugger Robot     // If *tmp_endptr is '\0' include its label as well.
569*7c3d14c8STreehugger Robot     *ret_label = dfsan_union(
570*7c3d14c8STreehugger Robot         base_label,
571*7c3d14c8STreehugger Robot         dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
572*7c3d14c8STreehugger Robot   } else {
573*7c3d14c8STreehugger Robot     *ret_label = 0;
574*7c3d14c8STreehugger Robot   }
575*7c3d14c8STreehugger Robot   return ret;
576*7c3d14c8STreehugger Robot }
577*7c3d14c8STreehugger Robot 
578*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoul(const char * nptr,char ** endptr,int base,dfsan_label nptr_label,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)579*7c3d14c8STreehugger Robot unsigned long int __dfsw_strtoul(const char *nptr, char **endptr, int base,
580*7c3d14c8STreehugger Robot                        dfsan_label nptr_label, dfsan_label endptr_label,
581*7c3d14c8STreehugger Robot                        dfsan_label base_label, dfsan_label *ret_label) {
582*7c3d14c8STreehugger Robot   char *tmp_endptr;
583*7c3d14c8STreehugger Robot   unsigned long int ret = strtoul(nptr, &tmp_endptr, base);
584*7c3d14c8STreehugger Robot   if (endptr) {
585*7c3d14c8STreehugger Robot     *endptr = tmp_endptr;
586*7c3d14c8STreehugger Robot   }
587*7c3d14c8STreehugger Robot   if (tmp_endptr > nptr) {
588*7c3d14c8STreehugger Robot     // If *tmp_endptr is '\0' include its label as well.
589*7c3d14c8STreehugger Robot     *ret_label = dfsan_union(
590*7c3d14c8STreehugger Robot         base_label,
591*7c3d14c8STreehugger Robot         dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
592*7c3d14c8STreehugger Robot   } else {
593*7c3d14c8STreehugger Robot     *ret_label = 0;
594*7c3d14c8STreehugger Robot   }
595*7c3d14c8STreehugger Robot   return ret;
596*7c3d14c8STreehugger Robot }
597*7c3d14c8STreehugger Robot 
598*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_strtoull(const char * nptr,char ** endptr,dfsan_label nptr_label,int base,dfsan_label endptr_label,dfsan_label base_label,dfsan_label * ret_label)599*7c3d14c8STreehugger Robot long long unsigned int __dfsw_strtoull(const char *nptr, char **endptr,
600*7c3d14c8STreehugger Robot                                        dfsan_label nptr_label,
601*7c3d14c8STreehugger Robot                                        int base, dfsan_label endptr_label,
602*7c3d14c8STreehugger Robot                                        dfsan_label base_label,
603*7c3d14c8STreehugger Robot                                        dfsan_label *ret_label) {
604*7c3d14c8STreehugger Robot   char *tmp_endptr;
605*7c3d14c8STreehugger Robot   long long unsigned int ret = strtoull(nptr, &tmp_endptr, base);
606*7c3d14c8STreehugger Robot   if (endptr) {
607*7c3d14c8STreehugger Robot     *endptr = tmp_endptr;
608*7c3d14c8STreehugger Robot   }
609*7c3d14c8STreehugger Robot   if (tmp_endptr > nptr) {
610*7c3d14c8STreehugger Robot     // If *tmp_endptr is '\0' include its label as well.
611*7c3d14c8STreehugger Robot     *ret_label = dfsan_union(
612*7c3d14c8STreehugger Robot         base_label,
613*7c3d14c8STreehugger Robot         dfsan_read_label(nptr, tmp_endptr - nptr + (*tmp_endptr ? 0 : 1)));
614*7c3d14c8STreehugger Robot   } else {
615*7c3d14c8STreehugger Robot     *ret_label = 0;
616*7c3d14c8STreehugger Robot   }
617*7c3d14c8STreehugger Robot   return ret;
618*7c3d14c8STreehugger Robot }
619*7c3d14c8STreehugger Robot 
620*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_time(time_t * t,dfsan_label t_label,dfsan_label * ret_label)621*7c3d14c8STreehugger Robot time_t __dfsw_time(time_t *t, dfsan_label t_label, dfsan_label *ret_label) {
622*7c3d14c8STreehugger Robot   time_t ret = time(t);
623*7c3d14c8STreehugger Robot   if (ret != (time_t) -1 && t) {
624*7c3d14c8STreehugger Robot     dfsan_set_label(0, t, sizeof(time_t));
625*7c3d14c8STreehugger Robot   }
626*7c3d14c8STreehugger Robot   *ret_label = 0;
627*7c3d14c8STreehugger Robot   return ret;
628*7c3d14c8STreehugger Robot }
629*7c3d14c8STreehugger Robot 
630*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_inet_pton(int af,const char * src,void * dst,dfsan_label af_label,dfsan_label src_label,dfsan_label dst_label,dfsan_label * ret_label)631*7c3d14c8STreehugger Robot int __dfsw_inet_pton(int af, const char *src, void *dst, dfsan_label af_label,
632*7c3d14c8STreehugger Robot                      dfsan_label src_label, dfsan_label dst_label,
633*7c3d14c8STreehugger Robot                      dfsan_label *ret_label) {
634*7c3d14c8STreehugger Robot   int ret = inet_pton(af, src, dst);
635*7c3d14c8STreehugger Robot   if (ret == 1) {
636*7c3d14c8STreehugger Robot     dfsan_set_label(dfsan_read_label(src, strlen(src) + 1), dst,
637*7c3d14c8STreehugger Robot                     af == AF_INET ? sizeof(struct in_addr) : sizeof(in6_addr));
638*7c3d14c8STreehugger Robot   }
639*7c3d14c8STreehugger Robot   *ret_label = 0;
640*7c3d14c8STreehugger Robot   return ret;
641*7c3d14c8STreehugger Robot }
642*7c3d14c8STreehugger Robot 
643*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_localtime_r(const time_t * timep,struct tm * result,dfsan_label timep_label,dfsan_label result_label,dfsan_label * ret_label)644*7c3d14c8STreehugger Robot struct tm *__dfsw_localtime_r(const time_t *timep, struct tm *result,
645*7c3d14c8STreehugger Robot                               dfsan_label timep_label, dfsan_label result_label,
646*7c3d14c8STreehugger Robot                               dfsan_label *ret_label) {
647*7c3d14c8STreehugger Robot   struct tm *ret = localtime_r(timep, result);
648*7c3d14c8STreehugger Robot   if (ret) {
649*7c3d14c8STreehugger Robot     dfsan_set_label(dfsan_read_label(timep, sizeof(time_t)), result,
650*7c3d14c8STreehugger Robot                     sizeof(struct tm));
651*7c3d14c8STreehugger Robot     *ret_label = result_label;
652*7c3d14c8STreehugger Robot   } else {
653*7c3d14c8STreehugger Robot     *ret_label = 0;
654*7c3d14c8STreehugger Robot   }
655*7c3d14c8STreehugger Robot   return ret;
656*7c3d14c8STreehugger Robot }
657*7c3d14c8STreehugger Robot 
658*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_getpwuid_r(id_t uid,struct passwd * pwd,char * buf,size_t buflen,struct passwd ** result,dfsan_label uid_label,dfsan_label pwd_label,dfsan_label buf_label,dfsan_label buflen_label,dfsan_label result_label,dfsan_label * ret_label)659*7c3d14c8STreehugger Robot int __dfsw_getpwuid_r(id_t uid, struct passwd *pwd,
660*7c3d14c8STreehugger Robot                       char *buf, size_t buflen, struct passwd **result,
661*7c3d14c8STreehugger Robot                       dfsan_label uid_label, dfsan_label pwd_label,
662*7c3d14c8STreehugger Robot                       dfsan_label buf_label, dfsan_label buflen_label,
663*7c3d14c8STreehugger Robot                       dfsan_label result_label, dfsan_label *ret_label) {
664*7c3d14c8STreehugger Robot   // Store the data in pwd, the strings referenced from pwd in buf, and the
665*7c3d14c8STreehugger Robot   // address of pwd in *result.  On failure, NULL is stored in *result.
666*7c3d14c8STreehugger Robot   int ret = getpwuid_r(uid, pwd, buf, buflen, result);
667*7c3d14c8STreehugger Robot   if (ret == 0) {
668*7c3d14c8STreehugger Robot     dfsan_set_label(0, pwd, sizeof(struct passwd));
669*7c3d14c8STreehugger Robot     dfsan_set_label(0, buf, strlen(buf) + 1);
670*7c3d14c8STreehugger Robot   }
671*7c3d14c8STreehugger Robot   *ret_label = 0;
672*7c3d14c8STreehugger Robot   dfsan_set_label(0, result, sizeof(struct passwd*));
673*7c3d14c8STreehugger Robot   return ret;
674*7c3d14c8STreehugger Robot }
675*7c3d14c8STreehugger Robot 
676*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_poll(struct pollfd * fds,nfds_t nfds,int timeout,dfsan_label dfs_label,dfsan_label nfds_label,dfsan_label timeout_label,dfsan_label * ret_label)677*7c3d14c8STreehugger Robot int __dfsw_poll(struct pollfd *fds, nfds_t nfds, int timeout,
678*7c3d14c8STreehugger Robot                 dfsan_label dfs_label, dfsan_label nfds_label,
679*7c3d14c8STreehugger Robot                 dfsan_label timeout_label, dfsan_label *ret_label) {
680*7c3d14c8STreehugger Robot   int ret = poll(fds, nfds, timeout);
681*7c3d14c8STreehugger Robot   if (ret >= 0) {
682*7c3d14c8STreehugger Robot     for (; nfds > 0; --nfds) {
683*7c3d14c8STreehugger Robot       dfsan_set_label(0, &fds[nfds - 1].revents, sizeof(fds[nfds - 1].revents));
684*7c3d14c8STreehugger Robot     }
685*7c3d14c8STreehugger Robot   }
686*7c3d14c8STreehugger Robot   *ret_label = 0;
687*7c3d14c8STreehugger Robot   return ret;
688*7c3d14c8STreehugger Robot }
689*7c3d14c8STreehugger Robot 
690*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout,dfsan_label nfds_label,dfsan_label readfds_label,dfsan_label writefds_label,dfsan_label exceptfds_label,dfsan_label timeout_label,dfsan_label * ret_label)691*7c3d14c8STreehugger Robot int __dfsw_select(int nfds, fd_set *readfds, fd_set *writefds,
692*7c3d14c8STreehugger Robot                   fd_set *exceptfds, struct timeval *timeout,
693*7c3d14c8STreehugger Robot                   dfsan_label nfds_label, dfsan_label readfds_label,
694*7c3d14c8STreehugger Robot                   dfsan_label writefds_label, dfsan_label exceptfds_label,
695*7c3d14c8STreehugger Robot                   dfsan_label timeout_label, dfsan_label *ret_label) {
696*7c3d14c8STreehugger Robot   int ret = select(nfds, readfds, writefds, exceptfds, timeout);
697*7c3d14c8STreehugger Robot   // Clear everything (also on error) since their content is either set or
698*7c3d14c8STreehugger Robot   // undefined.
699*7c3d14c8STreehugger Robot   if (readfds) {
700*7c3d14c8STreehugger Robot     dfsan_set_label(0, readfds, sizeof(fd_set));
701*7c3d14c8STreehugger Robot   }
702*7c3d14c8STreehugger Robot   if (writefds) {
703*7c3d14c8STreehugger Robot     dfsan_set_label(0, writefds, sizeof(fd_set));
704*7c3d14c8STreehugger Robot   }
705*7c3d14c8STreehugger Robot   if (exceptfds) {
706*7c3d14c8STreehugger Robot     dfsan_set_label(0, exceptfds, sizeof(fd_set));
707*7c3d14c8STreehugger Robot   }
708*7c3d14c8STreehugger Robot   dfsan_set_label(0, timeout, sizeof(struct timeval));
709*7c3d14c8STreehugger Robot   *ret_label = 0;
710*7c3d14c8STreehugger Robot   return ret;
711*7c3d14c8STreehugger Robot }
712*7c3d14c8STreehugger Robot 
713*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sched_getaffinity(pid_t pid,size_t cpusetsize,cpu_set_t * mask,dfsan_label pid_label,dfsan_label cpusetsize_label,dfsan_label mask_label,dfsan_label * ret_label)714*7c3d14c8STreehugger Robot int __dfsw_sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask,
715*7c3d14c8STreehugger Robot                              dfsan_label pid_label,
716*7c3d14c8STreehugger Robot                              dfsan_label cpusetsize_label,
717*7c3d14c8STreehugger Robot                              dfsan_label mask_label, dfsan_label *ret_label) {
718*7c3d14c8STreehugger Robot   int ret = sched_getaffinity(pid, cpusetsize, mask);
719*7c3d14c8STreehugger Robot   if (ret == 0) {
720*7c3d14c8STreehugger Robot     dfsan_set_label(0, mask, cpusetsize);
721*7c3d14c8STreehugger Robot   }
722*7c3d14c8STreehugger Robot   *ret_label = 0;
723*7c3d14c8STreehugger Robot   return ret;
724*7c3d14c8STreehugger Robot }
725*7c3d14c8STreehugger Robot 
726*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sigemptyset(sigset_t * set,dfsan_label set_label,dfsan_label * ret_label)727*7c3d14c8STreehugger Robot int __dfsw_sigemptyset(sigset_t *set, dfsan_label set_label,
728*7c3d14c8STreehugger Robot                        dfsan_label *ret_label) {
729*7c3d14c8STreehugger Robot   int ret = sigemptyset(set);
730*7c3d14c8STreehugger Robot   dfsan_set_label(0, set, sizeof(sigset_t));
731*7c3d14c8STreehugger Robot   return ret;
732*7c3d14c8STreehugger Robot }
733*7c3d14c8STreehugger Robot 
734*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sigaction(int signum,const struct sigaction * act,struct sigaction * oldact,dfsan_label signum_label,dfsan_label act_label,dfsan_label oldact_label,dfsan_label * ret_label)735*7c3d14c8STreehugger Robot int __dfsw_sigaction(int signum, const struct sigaction *act,
736*7c3d14c8STreehugger Robot                      struct sigaction *oldact, dfsan_label signum_label,
737*7c3d14c8STreehugger Robot                      dfsan_label act_label, dfsan_label oldact_label,
738*7c3d14c8STreehugger Robot                      dfsan_label *ret_label) {
739*7c3d14c8STreehugger Robot   int ret = sigaction(signum, act, oldact);
740*7c3d14c8STreehugger Robot   if (oldact) {
741*7c3d14c8STreehugger Robot     dfsan_set_label(0, oldact, sizeof(struct sigaction));
742*7c3d14c8STreehugger Robot   }
743*7c3d14c8STreehugger Robot   *ret_label = 0;
744*7c3d14c8STreehugger Robot   return ret;
745*7c3d14c8STreehugger Robot }
746*7c3d14c8STreehugger Robot 
747*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_gettimeofday(struct timeval * tv,struct timezone * tz,dfsan_label tv_label,dfsan_label tz_label,dfsan_label * ret_label)748*7c3d14c8STreehugger Robot int __dfsw_gettimeofday(struct timeval *tv, struct timezone *tz,
749*7c3d14c8STreehugger Robot                         dfsan_label tv_label, dfsan_label tz_label,
750*7c3d14c8STreehugger Robot                         dfsan_label *ret_label) {
751*7c3d14c8STreehugger Robot   int ret = gettimeofday(tv, tz);
752*7c3d14c8STreehugger Robot   if (tv) {
753*7c3d14c8STreehugger Robot     dfsan_set_label(0, tv, sizeof(struct timeval));
754*7c3d14c8STreehugger Robot   }
755*7c3d14c8STreehugger Robot   if (tz) {
756*7c3d14c8STreehugger Robot     dfsan_set_label(0, tz, sizeof(struct timezone));
757*7c3d14c8STreehugger Robot   }
758*7c3d14c8STreehugger Robot   *ret_label = 0;
759*7c3d14c8STreehugger Robot   return ret;
760*7c3d14c8STreehugger Robot }
761*7c3d14c8STreehugger Robot 
__dfsw_memchr(void * s,int c,size_t n,dfsan_label s_label,dfsan_label c_label,dfsan_label n_label,dfsan_label * ret_label)762*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE void *__dfsw_memchr(void *s, int c, size_t n,
763*7c3d14c8STreehugger Robot                                                   dfsan_label s_label,
764*7c3d14c8STreehugger Robot                                                   dfsan_label c_label,
765*7c3d14c8STreehugger Robot                                                   dfsan_label n_label,
766*7c3d14c8STreehugger Robot                                                   dfsan_label *ret_label) {
767*7c3d14c8STreehugger Robot   void *ret = memchr(s, c, n);
768*7c3d14c8STreehugger Robot   if (flags().strict_data_dependencies) {
769*7c3d14c8STreehugger Robot     *ret_label = ret ? s_label : 0;
770*7c3d14c8STreehugger Robot   } else {
771*7c3d14c8STreehugger Robot     size_t len =
772*7c3d14c8STreehugger Robot         ret ? reinterpret_cast<char *>(ret) - reinterpret_cast<char *>(s) + 1
773*7c3d14c8STreehugger Robot             : n;
774*7c3d14c8STreehugger Robot     *ret_label =
775*7c3d14c8STreehugger Robot         dfsan_union(dfsan_read_label(s, len), dfsan_union(s_label, c_label));
776*7c3d14c8STreehugger Robot   }
777*7c3d14c8STreehugger Robot   return ret;
778*7c3d14c8STreehugger Robot }
779*7c3d14c8STreehugger Robot 
__dfsw_strrchr(char * s,int c,dfsan_label s_label,dfsan_label c_label,dfsan_label * ret_label)780*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strrchr(char *s, int c,
781*7c3d14c8STreehugger Robot                                                    dfsan_label s_label,
782*7c3d14c8STreehugger Robot                                                    dfsan_label c_label,
783*7c3d14c8STreehugger Robot                                                    dfsan_label *ret_label) {
784*7c3d14c8STreehugger Robot   char *ret = strrchr(s, c);
785*7c3d14c8STreehugger Robot   if (flags().strict_data_dependencies) {
786*7c3d14c8STreehugger Robot     *ret_label = ret ? s_label : 0;
787*7c3d14c8STreehugger Robot   } else {
788*7c3d14c8STreehugger Robot     *ret_label =
789*7c3d14c8STreehugger Robot         dfsan_union(dfsan_read_label(s, strlen(s) + 1),
790*7c3d14c8STreehugger Robot                     dfsan_union(s_label, c_label));
791*7c3d14c8STreehugger Robot   }
792*7c3d14c8STreehugger Robot 
793*7c3d14c8STreehugger Robot   return ret;
794*7c3d14c8STreehugger Robot }
795*7c3d14c8STreehugger Robot 
__dfsw_strstr(char * haystack,char * needle,dfsan_label haystack_label,dfsan_label needle_label,dfsan_label * ret_label)796*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strstr(char *haystack, char *needle,
797*7c3d14c8STreehugger Robot                                                   dfsan_label haystack_label,
798*7c3d14c8STreehugger Robot                                                   dfsan_label needle_label,
799*7c3d14c8STreehugger Robot                                                   dfsan_label *ret_label) {
800*7c3d14c8STreehugger Robot   char *ret = strstr(haystack, needle);
801*7c3d14c8STreehugger Robot   if (flags().strict_data_dependencies) {
802*7c3d14c8STreehugger Robot     *ret_label = ret ? haystack_label : 0;
803*7c3d14c8STreehugger Robot   } else {
804*7c3d14c8STreehugger Robot     size_t len = ret ? ret + strlen(needle) - haystack : strlen(haystack) + 1;
805*7c3d14c8STreehugger Robot     *ret_label =
806*7c3d14c8STreehugger Robot         dfsan_union(dfsan_read_label(haystack, len),
807*7c3d14c8STreehugger Robot                     dfsan_union(dfsan_read_label(needle, strlen(needle) + 1),
808*7c3d14c8STreehugger Robot                                 dfsan_union(haystack_label, needle_label)));
809*7c3d14c8STreehugger Robot   }
810*7c3d14c8STreehugger Robot 
811*7c3d14c8STreehugger Robot   return ret;
812*7c3d14c8STreehugger Robot }
813*7c3d14c8STreehugger Robot 
__dfsw_nanosleep(const struct timespec * req,struct timespec * rem,dfsan_label req_label,dfsan_label rem_label,dfsan_label * ret_label)814*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_nanosleep(const struct timespec *req,
815*7c3d14c8STreehugger Robot                                                    struct timespec *rem,
816*7c3d14c8STreehugger Robot                                                    dfsan_label req_label,
817*7c3d14c8STreehugger Robot                                                    dfsan_label rem_label,
818*7c3d14c8STreehugger Robot                                                    dfsan_label *ret_label) {
819*7c3d14c8STreehugger Robot   int ret = nanosleep(req, rem);
820*7c3d14c8STreehugger Robot   *ret_label = 0;
821*7c3d14c8STreehugger Robot   if (ret == -1) {
822*7c3d14c8STreehugger Robot     // Interrupted by a signal, rem is filled with the remaining time.
823*7c3d14c8STreehugger Robot     dfsan_set_label(0, rem, sizeof(struct timespec));
824*7c3d14c8STreehugger Robot   }
825*7c3d14c8STreehugger Robot   return ret;
826*7c3d14c8STreehugger Robot }
827*7c3d14c8STreehugger Robot 
828*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_socketpair(int domain,int type,int protocol,int sv[2],dfsan_label domain_label,dfsan_label type_label,dfsan_label protocol_label,dfsan_label sv_label,dfsan_label * ret_label)829*7c3d14c8STreehugger Robot __dfsw_socketpair(int domain, int type, int protocol, int sv[2],
830*7c3d14c8STreehugger Robot                   dfsan_label domain_label, dfsan_label type_label,
831*7c3d14c8STreehugger Robot                   dfsan_label protocol_label, dfsan_label sv_label,
832*7c3d14c8STreehugger Robot                   dfsan_label *ret_label) {
833*7c3d14c8STreehugger Robot   int ret = socketpair(domain, type, protocol, sv);
834*7c3d14c8STreehugger Robot   *ret_label = 0;
835*7c3d14c8STreehugger Robot   if (ret == 0) {
836*7c3d14c8STreehugger Robot     dfsan_set_label(0, sv, sizeof(*sv) * 2);
837*7c3d14c8STreehugger Robot   }
838*7c3d14c8STreehugger Robot   return ret;
839*7c3d14c8STreehugger Robot }
840*7c3d14c8STreehugger Robot 
841*7c3d14c8STreehugger Robot // Type of the trampoline function passed to the custom version of
842*7c3d14c8STreehugger Robot // dfsan_set_write_callback.
843*7c3d14c8STreehugger Robot typedef void (*write_trampoline_t)(
844*7c3d14c8STreehugger Robot     void *callback,
845*7c3d14c8STreehugger Robot     int fd, const void *buf, ssize_t count,
846*7c3d14c8STreehugger Robot     dfsan_label fd_label, dfsan_label buf_label, dfsan_label count_label);
847*7c3d14c8STreehugger Robot 
848*7c3d14c8STreehugger Robot // Calls to dfsan_set_write_callback() set the values in this struct.
849*7c3d14c8STreehugger Robot // Calls to the custom version of write() read (and invoke) them.
850*7c3d14c8STreehugger Robot static struct {
851*7c3d14c8STreehugger Robot   write_trampoline_t write_callback_trampoline = nullptr;
852*7c3d14c8STreehugger Robot   void *write_callback = nullptr;
853*7c3d14c8STreehugger Robot } write_callback_info;
854*7c3d14c8STreehugger Robot 
855*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE void
__dfsw_dfsan_set_write_callback(write_trampoline_t write_callback_trampoline,void * write_callback,dfsan_label write_callback_label,dfsan_label * ret_label)856*7c3d14c8STreehugger Robot __dfsw_dfsan_set_write_callback(
857*7c3d14c8STreehugger Robot     write_trampoline_t write_callback_trampoline,
858*7c3d14c8STreehugger Robot     void *write_callback,
859*7c3d14c8STreehugger Robot     dfsan_label write_callback_label,
860*7c3d14c8STreehugger Robot     dfsan_label *ret_label) {
861*7c3d14c8STreehugger Robot   write_callback_info.write_callback_trampoline = write_callback_trampoline;
862*7c3d14c8STreehugger Robot   write_callback_info.write_callback = write_callback;
863*7c3d14c8STreehugger Robot }
864*7c3d14c8STreehugger Robot 
865*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE int
__dfsw_write(int fd,const void * buf,size_t count,dfsan_label fd_label,dfsan_label buf_label,dfsan_label count_label,dfsan_label * ret_label)866*7c3d14c8STreehugger Robot __dfsw_write(int fd, const void *buf, size_t count,
867*7c3d14c8STreehugger Robot              dfsan_label fd_label, dfsan_label buf_label,
868*7c3d14c8STreehugger Robot              dfsan_label count_label, dfsan_label *ret_label) {
869*7c3d14c8STreehugger Robot   if (write_callback_info.write_callback) {
870*7c3d14c8STreehugger Robot     write_callback_info.write_callback_trampoline(
871*7c3d14c8STreehugger Robot         write_callback_info.write_callback,
872*7c3d14c8STreehugger Robot         fd, buf, count,
873*7c3d14c8STreehugger Robot         fd_label, buf_label, count_label);
874*7c3d14c8STreehugger Robot   }
875*7c3d14c8STreehugger Robot 
876*7c3d14c8STreehugger Robot   *ret_label = 0;
877*7c3d14c8STreehugger Robot   return write(fd, buf, count);
878*7c3d14c8STreehugger Robot }
879*7c3d14c8STreehugger Robot } // namespace __dfsan
880*7c3d14c8STreehugger Robot 
881*7c3d14c8STreehugger Robot // Type used to extract a dfsan_label with va_arg()
882*7c3d14c8STreehugger Robot typedef int dfsan_label_va;
883*7c3d14c8STreehugger Robot 
884*7c3d14c8STreehugger Robot // Formats a chunk either a constant string or a single format directive (e.g.,
885*7c3d14c8STreehugger Robot // '%.3f').
886*7c3d14c8STreehugger Robot struct Formatter {
FormatterFormatter887*7c3d14c8STreehugger Robot   Formatter(char *str_, const char *fmt_, size_t size_)
888*7c3d14c8STreehugger Robot       : str(str_), str_off(0), size(size_), fmt_start(fmt_), fmt_cur(fmt_),
889*7c3d14c8STreehugger Robot         width(-1) {}
890*7c3d14c8STreehugger Robot 
formatFormatter891*7c3d14c8STreehugger Robot   int format() {
892*7c3d14c8STreehugger Robot     char *tmp_fmt = build_format_string();
893*7c3d14c8STreehugger Robot     int retval =
894*7c3d14c8STreehugger Robot         snprintf(str + str_off, str_off < size ? size - str_off : 0, tmp_fmt,
895*7c3d14c8STreehugger Robot                  0 /* used only to avoid warnings */);
896*7c3d14c8STreehugger Robot     free(tmp_fmt);
897*7c3d14c8STreehugger Robot     return retval;
898*7c3d14c8STreehugger Robot   }
899*7c3d14c8STreehugger Robot 
formatFormatter900*7c3d14c8STreehugger Robot   template <typename T> int format(T arg) {
901*7c3d14c8STreehugger Robot     char *tmp_fmt = build_format_string();
902*7c3d14c8STreehugger Robot     int retval;
903*7c3d14c8STreehugger Robot     if (width >= 0) {
904*7c3d14c8STreehugger Robot       retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
905*7c3d14c8STreehugger Robot                         tmp_fmt, width, arg);
906*7c3d14c8STreehugger Robot     } else {
907*7c3d14c8STreehugger Robot       retval = snprintf(str + str_off, str_off < size ? size - str_off : 0,
908*7c3d14c8STreehugger Robot                         tmp_fmt, arg);
909*7c3d14c8STreehugger Robot     }
910*7c3d14c8STreehugger Robot     free(tmp_fmt);
911*7c3d14c8STreehugger Robot     return retval;
912*7c3d14c8STreehugger Robot   }
913*7c3d14c8STreehugger Robot 
build_format_stringFormatter914*7c3d14c8STreehugger Robot   char *build_format_string() {
915*7c3d14c8STreehugger Robot     size_t fmt_size = fmt_cur - fmt_start + 1;
916*7c3d14c8STreehugger Robot     char *new_fmt = (char *)malloc(fmt_size + 1);
917*7c3d14c8STreehugger Robot     assert(new_fmt);
918*7c3d14c8STreehugger Robot     internal_memcpy(new_fmt, fmt_start, fmt_size);
919*7c3d14c8STreehugger Robot     new_fmt[fmt_size] = '\0';
920*7c3d14c8STreehugger Robot     return new_fmt;
921*7c3d14c8STreehugger Robot   }
922*7c3d14c8STreehugger Robot 
str_curFormatter923*7c3d14c8STreehugger Robot   char *str_cur() { return str + str_off; }
924*7c3d14c8STreehugger Robot 
num_written_bytesFormatter925*7c3d14c8STreehugger Robot   size_t num_written_bytes(int retval) {
926*7c3d14c8STreehugger Robot     if (retval < 0) {
927*7c3d14c8STreehugger Robot       return 0;
928*7c3d14c8STreehugger Robot     }
929*7c3d14c8STreehugger Robot 
930*7c3d14c8STreehugger Robot     size_t num_avail = str_off < size ? size - str_off : 0;
931*7c3d14c8STreehugger Robot     if (num_avail == 0) {
932*7c3d14c8STreehugger Robot       return 0;
933*7c3d14c8STreehugger Robot     }
934*7c3d14c8STreehugger Robot 
935*7c3d14c8STreehugger Robot     size_t num_written = retval;
936*7c3d14c8STreehugger Robot     // A return value of {v,}snprintf of size or more means that the output was
937*7c3d14c8STreehugger Robot     // truncated.
938*7c3d14c8STreehugger Robot     if (num_written >= num_avail) {
939*7c3d14c8STreehugger Robot       num_written -= num_avail;
940*7c3d14c8STreehugger Robot     }
941*7c3d14c8STreehugger Robot 
942*7c3d14c8STreehugger Robot     return num_written;
943*7c3d14c8STreehugger Robot   }
944*7c3d14c8STreehugger Robot 
945*7c3d14c8STreehugger Robot   char *str;
946*7c3d14c8STreehugger Robot   size_t str_off;
947*7c3d14c8STreehugger Robot   size_t size;
948*7c3d14c8STreehugger Robot   const char *fmt_start;
949*7c3d14c8STreehugger Robot   const char *fmt_cur;
950*7c3d14c8STreehugger Robot   int width;
951*7c3d14c8STreehugger Robot };
952*7c3d14c8STreehugger Robot 
953*7c3d14c8STreehugger Robot // Formats the input and propagates the input labels to the output. The output
954*7c3d14c8STreehugger Robot // is stored in 'str'. 'size' bounds the number of output bytes. 'format' and
955*7c3d14c8STreehugger Robot // 'ap' are the format string and the list of arguments for formatting. Returns
956*7c3d14c8STreehugger Robot // the return value vsnprintf would return.
957*7c3d14c8STreehugger Robot //
958*7c3d14c8STreehugger Robot // The function tokenizes the format string in chunks representing either a
959*7c3d14c8STreehugger Robot // constant string or a single format directive (e.g., '%.3f') and formats each
960*7c3d14c8STreehugger Robot // chunk independently into the output string. This approach allows to figure
961*7c3d14c8STreehugger Robot // out which bytes of the output string depends on which argument and thus to
962*7c3d14c8STreehugger Robot // propagate labels more precisely.
963*7c3d14c8STreehugger Robot //
964*7c3d14c8STreehugger Robot // WARNING: This implementation does not support conversion specifiers with
965*7c3d14c8STreehugger Robot // positional arguments.
format_buffer(char * str,size_t size,const char * fmt,dfsan_label * va_labels,dfsan_label * ret_label,va_list ap)966*7c3d14c8STreehugger Robot static int format_buffer(char *str, size_t size, const char *fmt,
967*7c3d14c8STreehugger Robot                          dfsan_label *va_labels, dfsan_label *ret_label,
968*7c3d14c8STreehugger Robot                          va_list ap) {
969*7c3d14c8STreehugger Robot   Formatter formatter(str, fmt, size);
970*7c3d14c8STreehugger Robot 
971*7c3d14c8STreehugger Robot   while (*formatter.fmt_cur) {
972*7c3d14c8STreehugger Robot     formatter.fmt_start = formatter.fmt_cur;
973*7c3d14c8STreehugger Robot     formatter.width = -1;
974*7c3d14c8STreehugger Robot     int retval = 0;
975*7c3d14c8STreehugger Robot 
976*7c3d14c8STreehugger Robot     if (*formatter.fmt_cur != '%') {
977*7c3d14c8STreehugger Robot       // Ordinary character. Consume all the characters until a '%' or the end
978*7c3d14c8STreehugger Robot       // of the string.
979*7c3d14c8STreehugger Robot       for (; *(formatter.fmt_cur + 1) && *(formatter.fmt_cur + 1) != '%';
980*7c3d14c8STreehugger Robot            ++formatter.fmt_cur) {}
981*7c3d14c8STreehugger Robot       retval = formatter.format();
982*7c3d14c8STreehugger Robot       dfsan_set_label(0, formatter.str_cur(),
983*7c3d14c8STreehugger Robot                       formatter.num_written_bytes(retval));
984*7c3d14c8STreehugger Robot     } else {
985*7c3d14c8STreehugger Robot       // Conversion directive. Consume all the characters until a conversion
986*7c3d14c8STreehugger Robot       // specifier or the end of the string.
987*7c3d14c8STreehugger Robot       bool end_fmt = false;
988*7c3d14c8STreehugger Robot       for (; *formatter.fmt_cur && !end_fmt; ) {
989*7c3d14c8STreehugger Robot         switch (*++formatter.fmt_cur) {
990*7c3d14c8STreehugger Robot         case 'd':
991*7c3d14c8STreehugger Robot         case 'i':
992*7c3d14c8STreehugger Robot         case 'o':
993*7c3d14c8STreehugger Robot         case 'u':
994*7c3d14c8STreehugger Robot         case 'x':
995*7c3d14c8STreehugger Robot         case 'X':
996*7c3d14c8STreehugger Robot           switch (*(formatter.fmt_cur - 1)) {
997*7c3d14c8STreehugger Robot           case 'h':
998*7c3d14c8STreehugger Robot             // Also covers the 'hh' case (since the size of the arg is still
999*7c3d14c8STreehugger Robot             // an int).
1000*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, int));
1001*7c3d14c8STreehugger Robot             break;
1002*7c3d14c8STreehugger Robot           case 'l':
1003*7c3d14c8STreehugger Robot             if (formatter.fmt_cur - formatter.fmt_start >= 2 &&
1004*7c3d14c8STreehugger Robot                 *(formatter.fmt_cur - 2) == 'l') {
1005*7c3d14c8STreehugger Robot               retval = formatter.format(va_arg(ap, long long int));
1006*7c3d14c8STreehugger Robot             } else {
1007*7c3d14c8STreehugger Robot               retval = formatter.format(va_arg(ap, long int));
1008*7c3d14c8STreehugger Robot             }
1009*7c3d14c8STreehugger Robot             break;
1010*7c3d14c8STreehugger Robot           case 'q':
1011*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, long long int));
1012*7c3d14c8STreehugger Robot             break;
1013*7c3d14c8STreehugger Robot           case 'j':
1014*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, intmax_t));
1015*7c3d14c8STreehugger Robot             break;
1016*7c3d14c8STreehugger Robot           case 'z':
1017*7c3d14c8STreehugger Robot           case 't':
1018*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, size_t));
1019*7c3d14c8STreehugger Robot             break;
1020*7c3d14c8STreehugger Robot           default:
1021*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, int));
1022*7c3d14c8STreehugger Robot           }
1023*7c3d14c8STreehugger Robot           dfsan_set_label(*va_labels++, formatter.str_cur(),
1024*7c3d14c8STreehugger Robot                           formatter.num_written_bytes(retval));
1025*7c3d14c8STreehugger Robot           end_fmt = true;
1026*7c3d14c8STreehugger Robot           break;
1027*7c3d14c8STreehugger Robot 
1028*7c3d14c8STreehugger Robot         case 'a':
1029*7c3d14c8STreehugger Robot         case 'A':
1030*7c3d14c8STreehugger Robot         case 'e':
1031*7c3d14c8STreehugger Robot         case 'E':
1032*7c3d14c8STreehugger Robot         case 'f':
1033*7c3d14c8STreehugger Robot         case 'F':
1034*7c3d14c8STreehugger Robot         case 'g':
1035*7c3d14c8STreehugger Robot         case 'G':
1036*7c3d14c8STreehugger Robot           if (*(formatter.fmt_cur - 1) == 'L') {
1037*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, long double));
1038*7c3d14c8STreehugger Robot           } else {
1039*7c3d14c8STreehugger Robot             retval = formatter.format(va_arg(ap, double));
1040*7c3d14c8STreehugger Robot           }
1041*7c3d14c8STreehugger Robot           dfsan_set_label(*va_labels++, formatter.str_cur(),
1042*7c3d14c8STreehugger Robot                           formatter.num_written_bytes(retval));
1043*7c3d14c8STreehugger Robot           end_fmt = true;
1044*7c3d14c8STreehugger Robot           break;
1045*7c3d14c8STreehugger Robot 
1046*7c3d14c8STreehugger Robot         case 'c':
1047*7c3d14c8STreehugger Robot           retval = formatter.format(va_arg(ap, int));
1048*7c3d14c8STreehugger Robot           dfsan_set_label(*va_labels++, formatter.str_cur(),
1049*7c3d14c8STreehugger Robot                           formatter.num_written_bytes(retval));
1050*7c3d14c8STreehugger Robot           end_fmt = true;
1051*7c3d14c8STreehugger Robot           break;
1052*7c3d14c8STreehugger Robot 
1053*7c3d14c8STreehugger Robot         case 's': {
1054*7c3d14c8STreehugger Robot           char *arg = va_arg(ap, char *);
1055*7c3d14c8STreehugger Robot           retval = formatter.format(arg);
1056*7c3d14c8STreehugger Robot           va_labels++;
1057*7c3d14c8STreehugger Robot           internal_memcpy(shadow_for(formatter.str_cur()), shadow_for(arg),
1058*7c3d14c8STreehugger Robot                           sizeof(dfsan_label) *
1059*7c3d14c8STreehugger Robot                               formatter.num_written_bytes(retval));
1060*7c3d14c8STreehugger Robot           end_fmt = true;
1061*7c3d14c8STreehugger Robot           break;
1062*7c3d14c8STreehugger Robot         }
1063*7c3d14c8STreehugger Robot 
1064*7c3d14c8STreehugger Robot         case 'p':
1065*7c3d14c8STreehugger Robot           retval = formatter.format(va_arg(ap, void *));
1066*7c3d14c8STreehugger Robot           dfsan_set_label(*va_labels++, formatter.str_cur(),
1067*7c3d14c8STreehugger Robot                           formatter.num_written_bytes(retval));
1068*7c3d14c8STreehugger Robot           end_fmt = true;
1069*7c3d14c8STreehugger Robot           break;
1070*7c3d14c8STreehugger Robot 
1071*7c3d14c8STreehugger Robot         case 'n': {
1072*7c3d14c8STreehugger Robot           int *ptr = va_arg(ap, int *);
1073*7c3d14c8STreehugger Robot           *ptr = (int)formatter.str_off;
1074*7c3d14c8STreehugger Robot           va_labels++;
1075*7c3d14c8STreehugger Robot           dfsan_set_label(0, ptr, sizeof(ptr));
1076*7c3d14c8STreehugger Robot           end_fmt = true;
1077*7c3d14c8STreehugger Robot           break;
1078*7c3d14c8STreehugger Robot         }
1079*7c3d14c8STreehugger Robot 
1080*7c3d14c8STreehugger Robot         case '%':
1081*7c3d14c8STreehugger Robot           retval = formatter.format();
1082*7c3d14c8STreehugger Robot           dfsan_set_label(0, formatter.str_cur(),
1083*7c3d14c8STreehugger Robot                           formatter.num_written_bytes(retval));
1084*7c3d14c8STreehugger Robot           end_fmt = true;
1085*7c3d14c8STreehugger Robot           break;
1086*7c3d14c8STreehugger Robot 
1087*7c3d14c8STreehugger Robot         case '*':
1088*7c3d14c8STreehugger Robot           formatter.width = va_arg(ap, int);
1089*7c3d14c8STreehugger Robot           va_labels++;
1090*7c3d14c8STreehugger Robot           break;
1091*7c3d14c8STreehugger Robot 
1092*7c3d14c8STreehugger Robot         default:
1093*7c3d14c8STreehugger Robot           break;
1094*7c3d14c8STreehugger Robot         }
1095*7c3d14c8STreehugger Robot       }
1096*7c3d14c8STreehugger Robot     }
1097*7c3d14c8STreehugger Robot 
1098*7c3d14c8STreehugger Robot     if (retval < 0) {
1099*7c3d14c8STreehugger Robot       return retval;
1100*7c3d14c8STreehugger Robot     }
1101*7c3d14c8STreehugger Robot 
1102*7c3d14c8STreehugger Robot     formatter.fmt_cur++;
1103*7c3d14c8STreehugger Robot     formatter.str_off += retval;
1104*7c3d14c8STreehugger Robot   }
1105*7c3d14c8STreehugger Robot 
1106*7c3d14c8STreehugger Robot   *ret_label = 0;
1107*7c3d14c8STreehugger Robot 
1108*7c3d14c8STreehugger Robot   // Number of bytes written in total.
1109*7c3d14c8STreehugger Robot   return formatter.str_off;
1110*7c3d14c8STreehugger Robot }
1111*7c3d14c8STreehugger Robot 
1112*7c3d14c8STreehugger Robot extern "C" {
1113*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_sprintf(char * str,const char * format,dfsan_label str_label,dfsan_label format_label,dfsan_label * va_labels,dfsan_label * ret_label,...)1114*7c3d14c8STreehugger Robot int __dfsw_sprintf(char *str, const char *format, dfsan_label str_label,
1115*7c3d14c8STreehugger Robot                    dfsan_label format_label, dfsan_label *va_labels,
1116*7c3d14c8STreehugger Robot                    dfsan_label *ret_label, ...) {
1117*7c3d14c8STreehugger Robot   va_list ap;
1118*7c3d14c8STreehugger Robot   va_start(ap, ret_label);
1119*7c3d14c8STreehugger Robot   int ret = format_buffer(str, ~0ul, format, va_labels, ret_label, ap);
1120*7c3d14c8STreehugger Robot   va_end(ap);
1121*7c3d14c8STreehugger Robot   return ret;
1122*7c3d14c8STreehugger Robot }
1123*7c3d14c8STreehugger Robot 
1124*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__dfsw_snprintf(char * str,size_t size,const char * format,dfsan_label str_label,dfsan_label size_label,dfsan_label format_label,dfsan_label * va_labels,dfsan_label * ret_label,...)1125*7c3d14c8STreehugger Robot int __dfsw_snprintf(char *str, size_t size, const char *format,
1126*7c3d14c8STreehugger Robot                     dfsan_label str_label, dfsan_label size_label,
1127*7c3d14c8STreehugger Robot                     dfsan_label format_label, dfsan_label *va_labels,
1128*7c3d14c8STreehugger Robot                     dfsan_label *ret_label, ...) {
1129*7c3d14c8STreehugger Robot   va_list ap;
1130*7c3d14c8STreehugger Robot   va_start(ap, ret_label);
1131*7c3d14c8STreehugger Robot   int ret = format_buffer(str, size, format, va_labels, ret_label, ap);
1132*7c3d14c8STreehugger Robot   va_end(ap);
1133*7c3d14c8STreehugger Robot   return ret;
1134*7c3d14c8STreehugger Robot }
1135*7c3d14c8STreehugger Robot } // extern "C"
1136