2 /*--------------------------------------------------------------------*/
3 /*--- Ptrcheck: a pointer-use checker. pc_intercepts.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Ptrcheck, a Valgrind tool for checking pointer
10 Copyright (C) 2003-2017 Nicholas Nethercote
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 /* Nothing actually in here. However it appears this file is needed
32 to make malloc intercepting work. (jrs, 2 july 08 -- not sure about
36 #include "pub_tool_basics.h"
37 #include "pub_tool_hashtable.h"
38 #include "pub_tool_redir.h"
39 #include "pub_tool_tooliface.h"
40 #include "pub_tool_clreq.h"
43 /* The following intercepts are copied verbatim from
44 memcheck/mc_replace_strmem.c. If you copy more in, please keep
45 them in the same order as in mc_replace_strmem.c. */
48 #define STRRCHR(soname, fnname) \
49 char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ); \
50 char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ) \
52 HChar ch = (HChar)c; \
54 const HChar* last = NULL; \
56 if (*p == ch) last = p; \
57 if (*p == 0) return CONST_CAST(HChar *,last); \
62 // Apparently rindex() is the same thing as strrchr()
63 STRRCHR(VG_Z_LIBC_SONAME
, strrchr
)
64 STRRCHR(VG_Z_LIBC_SONAME
, rindex
)
65 #if defined(VGO_linux)
66 STRRCHR(VG_Z_LIBC_SONAME
, __GI_strrchr
)
67 STRRCHR(VG_Z_LD_LINUX_SO_2
, rindex
)
68 #elif defined(VGO_darwin)
69 STRRCHR(VG_Z_DYLD
, strrchr
)
70 STRRCHR(VG_Z_DYLD
, rindex
)
71 #elif defined(VGO_solaris)
72 STRRCHR(VG_Z_LD_SO_1
, strrchr
)
76 #define STRCHR(soname, fnname) \
77 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ); \
78 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ) \
80 HChar ch = (HChar)c ; \
83 if (*p == ch) return CONST_CAST(HChar *,p); \
84 if (*p == 0) return NULL; \
89 // Apparently index() is the same thing as strchr()
90 STRCHR(VG_Z_LIBC_SONAME
, strchr
)
91 STRCHR(VG_Z_LIBC_SONAME
, index
)
92 #if defined(VGO_linux)
93 STRCHR(VG_Z_LIBC_SONAME
, __GI_strchr
)
94 STRCHR(VG_Z_LD_LINUX_SO_2
, strchr
)
95 STRCHR(VG_Z_LD_LINUX_SO_2
, index
)
96 STRCHR(VG_Z_LD_LINUX_X86_64_SO_2
, strchr
)
97 STRCHR(VG_Z_LD_LINUX_X86_64_SO_2
, index
)
98 #elif defined(VGO_darwin)
99 STRCHR(VG_Z_DYLD
, strchr
)
100 STRCHR(VG_Z_DYLD
, index
)
101 #elif defined(VGO_solaris)
102 STRCHR(VG_Z_LD_SO_1
, strchr
)
106 #define STRNLEN(soname, fnname) \
107 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ); \
108 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ) \
111 while (i < n && str[i] != 0) i++; \
115 STRNLEN(VG_Z_LIBC_SONAME
, strnlen
)
118 // Note that this replacement often doesn't get used because gcc inlines
119 // calls to strlen() with its own built-in version. This can be very
120 // confusing if you aren't expecting it. Other small functions in this file
121 // may also be inline by gcc.
122 #define STRLEN(soname, fnname) \
123 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ); \
124 SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ) \
127 while (str[i] != 0) i++; \
131 STRLEN(VG_Z_LIBC_SONAME
, strlen
)
132 #if defined(VGO_linux)
133 STRLEN(VG_Z_LIBC_SONAME
, __GI_strlen
)
134 STRLEN(VG_Z_LD_LINUX_SO_2
, strlen
)
135 STRLEN(VG_Z_LD_LINUX_X86_64_SO_2
, strlen
)
136 STRLEN(VG_Z_LD_SO_1
, strlen
)
137 #elif defined(VGO_solaris)
138 STRLEN(VG_Z_LD_SO_1
, strlen
)
142 #define STRCPY(soname, fnname) \
143 char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ); \
144 char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ) \
146 HChar* dst_orig = dst; \
148 while (*src) *dst++ = *src++; \
154 STRCPY(VG_Z_LIBC_SONAME
, strcpy
)
155 #if defined(VGO_linux)
156 STRCPY(VG_Z_LIBC_SONAME
, __GI_strcpy
)
157 #elif defined(VGO_darwin)
158 STRCPY(VG_Z_DYLD
, strcpy
)
159 #elif defined(VGO_solaris)
160 STRCPY(VG_Z_LD_SO_1
, strcpy
)
164 #define STRNCMP(soname, fnname) \
165 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
166 ( const char* s1, const char* s2, SizeT nmax ); \
167 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
168 ( const char* s1, const char* s2, SizeT nmax ) \
172 if (n >= nmax) return 0; \
173 if (*s1 == 0 && *s2 == 0) return 0; \
174 if (*s1 == 0) return -1; \
175 if (*s2 == 0) return 1; \
177 if (*(const unsigned char*)s1 < *(const unsigned char*)s2) return -1; \
178 if (*(const unsigned char*)s1 > *(const unsigned char*)s2) return 1; \
184 STRNCMP(VG_Z_LIBC_SONAME
, strncmp
)
185 #if defined(VGO_linux)
186 STRNCMP(VG_Z_LIBC_SONAME
, __GI_strncmp
)
187 #elif defined(VGO_darwin)
188 STRNCMP(VG_Z_DYLD
, strncmp
)
192 #define STRCMP(soname, fnname) \
193 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
194 ( const char* s1, const char* s2 ); \
195 int VG_REPLACE_FUNCTION_ZU(soname,fnname) \
196 ( const char* s1, const char* s2 ) \
201 c1 = *(const UChar *)s1; \
202 c2 = *(const UChar *)s2; \
203 if (c1 != c2) break; \
204 if (c1 == 0) break; \
207 if ((UChar)c1 < (UChar)c2) return -1; \
208 if ((UChar)c1 > (UChar)c2) return 1; \
212 STRCMP(VG_Z_LIBC_SONAME
, strcmp
)
213 #if defined(VGO_linux)
214 STRCMP(VG_Z_LIBC_SONAME
, __GI_strcmp
)
215 STRCMP(VG_Z_LD_LINUX_X86_64_SO_2
, strcmp
)
216 STRCMP(VG_Z_LD64_SO_1
, strcmp
)
217 #elif defined(VGO_solaris)
218 STRCMP(VG_Z_LD_SO_1
, strcmp
)
222 #define MEMCHR(soname, fnname) \
223 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void *s, int c, SizeT n); \
224 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void *s, int c, SizeT n) \
227 UChar c0 = (UChar)c; \
228 const UChar* p = s; \
229 for (i = 0; i < n; i++) \
230 if (p[i] == c0) return CONST_CAST(void *,&p[i]); \
234 MEMCHR(VG_Z_LIBC_SONAME
, memchr
)
235 #if defined(VGO_darwin)
236 MEMCHR(VG_Z_DYLD
, memchr
)
240 #define MEMCPY(soname, fnname) \
241 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
242 ( void *dst, const void *src, SizeT len ); \
243 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
244 ( void *dst, const void *src, SizeT len ) \
246 const Addr WS = sizeof(UWord); /* 8 or 4 */ \
247 const Addr WM = WS - 1; /* 7 or 3 */ \
252 /* Copying backwards. */ \
254 Addr d = (Addr)dst; \
255 Addr s = (Addr)src; \
257 if (((s^d) & WM) == 0) { \
258 /* s and d have same UWord alignment. */ \
259 /* Pull up to a UWord boundary. */ \
260 while ((s & WM) != 0 && n >= 1) \
261 { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
264 { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \
268 if (((s|d) & 1) == 0) { \
269 /* Both are 16-aligned; copy what we can thusly. */ \
271 { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \
273 /* Copy leftovers, or everything if misaligned. */ \
275 { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
277 } else if (dst > src) { \
280 Addr d = ((Addr)dst) + n; \
281 Addr s = ((Addr)src) + n; \
283 /* Copying forwards. */ \
284 if (((s^d) & WM) == 0) { \
285 /* s and d have same UWord alignment. */ \
286 /* Back down to a UWord boundary. */ \
287 while ((s & WM) != 0 && n >= 1) \
288 { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
291 { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \
295 if (((s|d) & 1) == 0) { \
296 /* Both are 16-aligned; copy what we can thusly. */ \
298 { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \
300 /* Copy leftovers, or everything if misaligned. */ \
302 { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
310 MEMCPY(VG_Z_LIBC_SONAME
, memcpy
)
311 #if defined(VGO_linux)
312 MEMCPY(VG_Z_LD_SO_1
, memcpy
) /* ld.so.1 */
313 MEMCPY(VG_Z_LD64_SO_1
, memcpy
) /* ld64.so.1 */
314 #elif defined(VGO_solaris)
315 MEMCPY(VG_Z_LD_SO_1
, memcpy
)
319 /* Copy SRC to DEST, returning the address of the terminating '\0' in
320 DEST. (minor variant of strcpy) */
321 #define STPCPY(soname, fnname) \
322 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \
323 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \
325 while (*src) *dst++ = *src++; \
331 STPCPY(VG_Z_LIBC_SONAME
, stpcpy
)
332 #if defined(VGO_linux)
333 STPCPY(VG_Z_LD_LINUX_SO_2
, stpcpy
)
334 STPCPY(VG_Z_LD_LINUX_X86_64_SO_2
, stpcpy
)
338 /* Find the first occurrence of C in S. */
339 #define GLIBC232_RAWMEMCHR(soname, fnname) \
340 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void* s, int c_in); \
341 void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void* s, int c_in) \
343 UChar c = (UChar)c_in; \
344 const UChar* char_ptr = s; \
346 if (*char_ptr == c) return CONST_CAST(void *,char_ptr); \
351 GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME
, rawmemchr
)
352 #if defined (VGO_linux)
353 GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME
, __GI___rawmemchr
)
357 #define STRSTR(soname, fnname) \
358 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
359 (const char* haystack, const char* needle); \
360 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
361 (const char* haystack, const char* needle) \
363 const HChar* h = haystack; \
364 const HChar* n = needle; \
366 /* find the length of n, not including terminating zero */ \
368 while (n[nlen]) nlen++; \
370 /* if n is the empty string, match immediately. */ \
371 if (nlen == 0) return CONST_CAST(HChar *,h); \
373 /* assert(nlen >= 1); */ \
377 const HChar hh = *h; \
378 if (hh == 0) return NULL; \
379 if (hh != n0) { h++; continue; } \
382 for (i = 0; i < nlen; i++) { \
386 /* assert(i >= 0 && i <= nlen); */ \
388 return CONST_CAST(HChar *,h); \
394 #if defined(VGO_linux)
395 STRSTR(VG_Z_LIBC_SONAME
, strstr
)
396 #elif defined(VGO_solaris)
397 STRSTR(VG_Z_LIBC_SONAME
, strstr
)
401 #define STRPBRK(soname, fnname) \
402 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
403 (const char* sV, const char* acceptV); \
404 char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
405 (const char* sV, const char* acceptV) \
407 const HChar* s = sV; \
408 const HChar* accept = acceptV; \
410 /* find the length of 'accept', not including terminating zero */ \
412 while (accept[nacc]) nacc++; \
414 /* if n is the empty string, fail immediately. */ \
415 if (nacc == 0) return NULL; \
417 /* assert(nacc >= 1); */ \
423 for (i = 0; i < nacc; i++) { \
424 if (sc == accept[i]) \
425 return CONST_CAST(HChar *,s); \
433 #if defined(VGO_linux)
434 STRPBRK(VG_Z_LIBC_SONAME
, strpbrk
)
435 #elif defined(VGO_solaris)
436 STRPBRK(VG_Z_LIBC_SONAME
, strpbrk
)
440 /*--------------------------------------------------------------------*/
441 /*--- end pc_intercepts.c ---*/
442 /*--------------------------------------------------------------------*/