Debian Codegraph @

shared/vg_replace_strmem.c

raw
1
2 /*--------------------------------------------------------------------*/
3 /*--- Replacements for strcpy(), memcpy() et al, which run on the ---*/
4 /*--- simulated CPU. ---*/
5 /*--- vg_replace_strmem.c ---*/
6 /*--------------------------------------------------------------------*/
7
8 /*
9 This file is part of Valgrind.
10
11 Copyright (C) 2000-2017 Julian Seward
12 [email protected]
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
26
27 The GNU General Public License is contained in the file COPYING.
28 */
29
30 #include "pub_tool_basics.h"
31 #include "pub_tool_poolalloc.h"
32 #include "pub_tool_hashtable.h"
33 #include "pub_tool_redir.h"
34 #include "pub_tool_tooliface.h"
35 #include "pub_tool_clreq.h"
36
37 /* ---------------------------------------------------------------------
38 We have our own versions of these functions for multiple reasons:
39 (a) it allows us to do overlap checking
40 (b) it allows us to do copy tracking
41 (c) some of the normal versions are hyper-optimised, which fools
42 Memcheck and cause spurious value warnings. Our versions are
43 simpler.
44 (d) the glibc SSE-variants can read past the end of the input data
45 ranges. This can cause false-positive Memcheck / Helgrind / DRD
46 reports.
47
48 Note that overenthusiastic use of PLT bypassing by the glibc people also
49 means that we need to patch multiple versions of some of the functions to
50 our own implementations.
51
52 THEY RUN ON THE SIMD CPU!
53 ------------------------------------------------------------------ */
54
55 /* Assignment of behavioural equivalence class tags: 2NNNP is intended
56 to be reserved for str/mem intercepts. Current usage:
57
58 20010 STRRCHR
59 20020 STRCHR
60 20030 STRCAT
61 20040 STRNCAT
62 20050 STRLCAT
63 20060 STRNLEN
64 20070 STRLEN
65 20080 STRCPY
66 20090 STRNCPY
67 20100 STRLCPY
68 20110 STRNCMP
69 20120 STRCASECMP
70 20130 STRNCASECMP
71 20140 STRCASECMP_L
72 20150 STRNCASECMP_L
73 20160 STRCMP
74 20170 MEMCHR
75
76 20180 MEMCPY if there's a conflict between memcpy and
77 20181 MEMMOVE memmove, prefer memmove
78
79 20190 MEMCMP
80 20200 STPCPY
81 20210 MEMSET
82 2022P unused (was previously MEMMOVE)
83 20230 BCOPY
84 20240 GLIBC25___MEMMOVE_CHK
85 20250 GLIBC232_STRCHRNUL
86 20260 GLIBC232_RAWMEMCHR
87 20270 GLIBC25___STRCPY_CHK
88 20280 GLIBC25___STPCPY_CHK
89 20290 GLIBC25_MEMPCPY
90 20300 GLIBC26___MEMCPY_CHK
91 20310 STRSTR
92 20320 STRPBRK
93 20330 STRCSPN
94 20340 STRSPN
95 20350 STRCASESTR
96 20360 MEMRCHR
97 20370 WCSLEN
98 20380 WCSCMP
99 20390 WCSCPY
100 20400 WCSCHR
101 20410 WCSRCHR
102 20420 STPNCPY
103 20430 WMEMCHR
104 20440 WCSNLEN
105 20450 WSTRNCMP
106 20460 MEMMEM
107 20470 WMEMCMP
108 20480 WCSNCPY
109 20490 MEMCCPY
110 20500 WCPNCPY
111 20510 WCSCAT
112 */
113
114 #if defined(VGO_solaris)
115 /*
116 Detour functions in the libc and the runtime linker. If a function isn't
117 much optimized (and no overlap checking is necessary) then redir the
118 function only in the libc. This way we can keep stacktraces in the tests
119 consistent.
120 */
121 #endif
122
123
124 /* Figure out if [dst .. dst+dstlen-1] overlaps with
125 [src .. src+srclen-1].
126 We assume that the address ranges do not wrap around
127 (which is safe since on Linux addresses >= 0xC0000000
128 are not accessible and the program will segfault in this
129 circumstance, presumably).
130 */
131 static inline
132 Bool is_overlap ( void* dst, const void* src, SizeT dstlen, SizeT srclen )
133 {
134 Addr loS, hiS, loD, hiD;
135
136 if (dstlen == 0 || srclen == 0)
137 return False;
138
139 loS = (Addr)src;
140 loD = (Addr)dst;
141 hiS = loS + srclen - 1;
142 hiD = loD + dstlen - 1;
143
144 /* So figure out if [loS .. hiS] overlaps with [loD .. hiD]. */
145 if (loS < loD) {
146 return !(hiS < loD);
147 }
148 else if (loD < loS) {
149 return !(hiD < loS);
150 }
151 else {
152 /* They start at same place. Since we know neither of them has
153 zero length, they must overlap. */
154 return True;
155 }
156 }
157
158 #if defined(VGO_linux)
159 /* Call here to exit if we can't continue. On Android we can't call
160 _exit for some reason, so we have to blunt-instrument it. */
161 __attribute__ ((__noreturn__))
162 static inline void my_exit ( int x )
163 {
164 # if defined(VGPV_arm_linux_android) || defined(VGPV_mips32_linux_android) \
165 || defined(VGPV_arm64_linux_android)
166 __asm__ __volatile__(".word 0xFFFFFFFF");
167 while (1) {}
168 # elif defined(VGPV_x86_linux_android)
169 __asm__ __volatile__("ud2");
170 while (1) {}
171 # else
172 extern __attribute__ ((__noreturn__)) void _exit(int status);
173 _exit(x);
174 # endif
175 }
176 #endif
177
178 // This is a macro rather than a function because we don't want to have an
179 // extra function in the stack trace.
180 #ifndef RECORD_OVERLAP_ERROR
181 #define RECORD_OVERLAP_ERROR(s, src, dst, len) do { } while (0)
182 #endif
183
184 // Used for tools that record bulk copies: memcpy, strcpy, etc.
185 #ifndef RECORD_COPY
186 #define RECORD_COPY(len) do { } while (0)
187 #define FOR_COPY(x)
188 #else
189 #define FOR_COPY(x) x
190 #endif
191
192 #ifndef VALGRIND_CHECK_VALUE_IS_DEFINED
193 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) 1
194 #endif
195
196
197 /*---------------------- strrchr ----------------------*/
198
199 #define STRRCHR(soname, fnname) \
200 char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ); \
201 char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ) \
202 { \
203 HChar ch = (HChar)c; \
204 const HChar* p = s; \
205 const HChar* last = NULL; \
206 while (True) { \
207 if (*p == ch) last = p; \
208 if (*p == 0) return CONST_CAST(HChar *,last); \
209 p++; \
210 } \
211 }
212
213 // Apparently rindex() is the same thing as strrchr()
214 #if defined(VGO_linux)
215 STRRCHR(VG_Z_LIBC_SONAME, strrchr)
216 STRRCHR(VG_Z_LIBC_SONAME, rindex)
217 STRRCHR(VG_Z_LIBC_SONAME, __GI_strrchr)
218 STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse2)
219 STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse2_no_bsf)
220 STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse42)
221 STRRCHR(VG_Z_LD_LINUX_SO_2, rindex)
222 #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
223 || defined(VGPV_mips32_linux_android)
224 STRRCHR(NONE, __dl_strrchr); /* in /system/bin/linker */
225 #endif
226
227 #elif defined(VGO_freebsd)
228 STRRCHR(VG_Z_LIBC_SONAME, strrchr)
229 STRRCHR(VG_Z_LIBC_SONAME, rindex)
230 STRRCHR(VG_Z_LD_ELF_SO_1, strrchr)
231 STRRCHR(VG_Z_LD_ELF32_SO_1, strrchr)
232
233 #elif defined(VGO_darwin)
234 //STRRCHR(VG_Z_LIBC_SONAME, strrchr)
235 //STRRCHR(VG_Z_LIBC_SONAME, rindex)
236 //STRRCHR(VG_Z_DYLD, strrchr)
237 //STRRCHR(VG_Z_DYLD, rindex)
238 STRRCHR(VG_Z_LIBC_SONAME, strrchr)
239 # if DARWIN_VERS >= DARWIN_10_9
240 STRRCHR(libsystemZucZddylib, strrchr)
241 # endif
242
243 #elif defined(VGO_solaris)
244 STRRCHR(VG_Z_LIBC_SONAME, strrchr)
245 STRRCHR(VG_Z_LIBC_SONAME, rindex)
246 STRRCHR(VG_Z_LD_SO_1, strrchr)
247
248 #endif
249
250
251 /*---------------------- strchr ----------------------*/
252
253 #define STRCHR(soname, fnname) \
254 char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ); \
255 char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ) \
256 { \
257 HChar ch = (HChar)c ; \
258 const HChar* p = s; \
259 while (True) { \
260 if (*p == ch) return CONST_CAST(HChar *,p); \
261 if (*p == 0) return NULL; \
262 p++; \
263 } \
264 }
265
266 // Apparently index() is the same thing as strchr()
267 #if defined(VGO_linux)
268 STRCHR(VG_Z_LIBC_SONAME, strchr)
269 STRCHR(VG_Z_LIBC_SONAME, __GI_strchr)
270 STRCHR(VG_Z_LIBC_SONAME, __strchr_sse2)
271 STRCHR(VG_Z_LIBC_SONAME, __strchr_sse2_no_bsf)
272 STRCHR(VG_Z_LIBC_SONAME, index)
273 # if !defined(VGP_x86_linux) && !defined(VGP_amd64_linux)
274 STRCHR(VG_Z_LD_LINUX_SO_2, strchr)
275 STRCHR(VG_Z_LD_LINUX_SO_2, index)
276 STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr)
277 STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index)
278 # endif
279
280 #if defined(VGPV_mips32_linux_android)
281 STRCHR(NONE, __dl_strchr)
282 #endif
283
284 #elif defined(VGO_freebsd)
285 STRCHR(VG_Z_LIBC_SONAME, strchr)
286 STRCHR(VG_Z_LIBC_SONAME, index)
287 STRCHR(VG_Z_LD_ELF_SO_1, strchr)
288 STRCHR(VG_Z_LD_ELF32_SO_1, strchr)
289
290 #elif defined(VGO_darwin)
291 STRCHR(VG_Z_LIBC_SONAME, strchr)
292 # if DARWIN_VERS == DARWIN_10_9
293 STRCHR(libsystemZuplatformZddylib, _platform_strchr)
294 # endif
295 # if DARWIN_VERS >= DARWIN_10_10
296 /* _platform_strchr$VARIANT$Generic */
297 STRCHR(libsystemZuplatformZddylib, _platform_strchr$VARIANT$Generic)
298 /* _platform_strchr$VARIANT$Haswell */
299 STRCHR(libsystemZuplatformZddylib, _platform_strchr$VARIANT$Haswell)
300 # endif
301 STRCHR(libsystemZuplatformZddylib, _platform_strchr$VARIANT$Base)
302
303 #elif defined(VGO_solaris)
304 STRCHR(VG_Z_LIBC_SONAME, strchr)
305 STRCHR(VG_Z_LIBC_SONAME, index)
306 STRCHR(VG_Z_LD_SO_1, strchr)
307
308 #endif
309
310
311 /*---------------------- strcat ----------------------*/
312
313 #define STRCAT(soname, fnname) \
314 char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \
315 ( char* dst, const char* src ); \
316 char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \
317 ( char* dst, const char* src ) \
318 { \
319 const HChar* src_orig = src; \
320 HChar* dst_orig = dst; \
321 while (*dst) dst++; \
322 while (*src) *dst++ = *src++; \
323 *dst = 0; \
324 \
325 /* This is a bit redundant, I think; any overlap and the strcat will */ \
326 /* go forever... or until a seg fault occurs. */ \
327 if (is_overlap(dst_orig, \
328 src_orig, \
329 (Addr)dst-(Addr)dst_orig+1, \
330 (Addr)src-(Addr)src_orig+1)) \
331 RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \
332 \
333 return dst_orig; \
334 }
335
336 #if defined(VGO_linux)
337 STRCAT(VG_Z_LIBC_SONAME, strcat)
338 STRCAT(VG_Z_LIBC_SONAME, __GI_strcat)
339
340 #elif defined(VGO_freebsd)
341 STRCAT(VG_Z_LIBC_SONAME, strcat)
342 STRCAT(VG_Z_LD_ELF_SO_1, strcat)
343 STRCAT(VG_Z_LD_ELF32_SO_1, strcat)
344
345 #elif defined(VGO_darwin)
346 //STRCAT(VG_Z_LIBC_SONAME, strcat)
347
348 #elif defined(VGO_solaris)
349 STRCAT(VG_Z_LIBC_SONAME, strcat)
350 STRCAT(VG_Z_LD_SO_1, strcat)
351
352 #endif
353
354
355 /*---------------------- strncat ----------------------*/
356
357 #define STRNCAT(soname, fnname) \
358 char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \
359 ( char* dst, const char* src, SizeT n ); \
360 char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \
361 ( char* dst, const char* src, SizeT n ) \
362 { \
363 const HChar* src_orig = src; \
364 HChar* dst_orig = dst; \
365 SizeT m = 0; \
366 \
367 while (*dst) dst++; \
368 while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \
369 *dst = 0; /* always add null */ \
370 \
371 /* This checks for overlap after copying, unavoidable without */ \
372 /* pre-counting lengths... should be ok */ \
373 if (is_overlap(dst_orig, \
374 src_orig, \
375 (Addr)dst-(Addr)dst_orig+1, \
376 (Addr)src-(Addr)src_orig)) \
377 RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \
378 \
379 return dst_orig; \
380 }
381
382 #if defined(VGO_linux)
383 STRNCAT(VG_Z_LIBC_SONAME, strncat)
384
385 #elif defined(VGO_freebsd)
386 STRNCAT(VG_Z_LIBC_SONAME, strncat)
387
388 #elif defined(VGO_darwin)
389 //STRNCAT(VG_Z_LIBC_SONAME, strncat)
390 //STRNCAT(VG_Z_DYLD, strncat)
391
392 #elif defined(VGO_solaris)
393 STRNCAT(VG_Z_LIBC_SONAME, strncat)
394
395 #endif
396
397
398 /*---------------------- strlcat ----------------------*/
399
400 /* Append src to dst. n is the size of dst's buffer. dst is guaranteed
401 to be nul-terminated after the copy, unless n <= strlen(dst_orig).
402 Returns min(n, strlen(dst_orig)) + strlen(src_orig).
403 Truncation occurred if retval >= n.
404 */
405 #define STRLCAT(soname, fnname) \
406 SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \
407 ( char* dst, const char* src, SizeT n ); \
408 SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \
409 ( char* dst, const char* src, SizeT n ) \
410 { \
411 const HChar* src_orig = src; \
412 HChar* dst_orig = dst; \
413 SizeT m = 0; \
414 \
415 while (m < n && *dst) { m++; dst++; } \
416 if (m < n) { \
417 /* Fill as far as dst_orig[n-2], then nul-terminate. */ \
418 while (m+1 < n && *src) { m++; *dst++ = *src++; } \
419 *dst = 0; \
420 } else { \
421 /* No space to copy anything to dst. m == n */ \
422 } \
423 /* Finish counting min(n, strlen(dst_orig)) + strlen(src_orig) */ \
424 while (*src) { m++; src++; } \
425 /* This checks for overlap after copying, unavoidable without */ \
426 /* pre-counting lengths... should be ok */ \
427 if (is_overlap(dst_orig, \
428 src_orig, \
429 (Addr)dst-(Addr)dst_orig+1, \
430 (Addr)src-(Addr)src_orig+1)) \
431 RECORD_OVERLAP_ERROR("strlcat", dst_orig, src_orig, n); \
432 \
433 return m; \
434 }
435
436 #if defined(VGO_linux)
437 STRLCAT(VG_Z_LIBC_SONAME, strlcat)
438
439 #elif defined(VGO_freebsd)
440 STRLCAT(VG_Z_LD_ELF_SO_1, strlcat)
441 STRLCAT(VG_Z_LIBC_SONAME, strlcat)
442 STRLCAT(VG_Z_LD_ELF32_SO_1, strlcat)
443
444 #elif defined(VGO_darwin)
445 //STRLCAT(VG_Z_LIBC_SONAME, strlcat)
446 //STRLCAT(VG_Z_DYLD, strlcat)
447 STRLCAT(VG_Z_LIBC_SONAME, strlcat)
448
449 #elif defined(VGO_solaris)
450 STRLCAT(VG_Z_LIBC_SONAME, strlcat)
451
452 #endif
453
454
455 /*---------------------- strnlen ----------------------*/
456
457 #define STRNLEN(soname, fnname) \
458 SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \
459 ( const char* str, SizeT n ); \
460 SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \
461 ( const char* str, SizeT n ) \
462 { \
463 SizeT i = 0; \
464 while (i < n && str[i] != 0) i++; \
465 return i; \
466 }
467
468 #if defined(VGO_linux)
469 STRNLEN(VG_Z_LIBC_SONAME, strnlen)
470 STRNLEN(VG_Z_LIBC_SONAME, __GI_strnlen)
471
472 #elif defined(VGO_freebsd)
473
474 STRNLEN(VG_Z_LIBC_SONAME, srtnlen)
475
476 #elif defined(VGO_darwin)
477 # if DARWIN_VERS == DARWIN_10_9
478 STRNLEN(libsystemZucZddylib, strnlen)
479 # endif
480
481 #elif defined(VGO_solaris)
482 STRNLEN(VG_Z_LIBC_SONAME, strnlen)
483
484 #endif
485
486
487 /*---------------------- strlen ----------------------*/
488
489 // Note that this replacement often doesn't get used because gcc inlines
490 // calls to strlen() with its own built-in version. This can be very
491 // confusing if you aren't expecting it. Other small functions in
492 // this file may also be inline by gcc.
493
494 #define STRLEN(soname, fnname) \
495 SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \
496 ( const char* str ); \
497 SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \
498 ( const char* str ) \
499 { \
500 SizeT i = 0; \
501 while (str[i] != 0) i++; \
502 return i; \
503 }
504
505 #if defined(VGO_linux)
506 STRLEN(VG_Z_LIBC_SONAME, strlen)
507 STRLEN(VG_Z_LIBC_SONAME, __GI_strlen)
508 STRLEN(VG_Z_LIBC_SONAME, __strlen_sse2)
509 STRLEN(VG_Z_LIBC_SONAME, __strlen_sse2_no_bsf)
510 STRLEN(VG_Z_LIBC_SONAME, __strlen_sse42)
511 STRLEN(VG_Z_LD_LINUX_SO_2, strlen)
512 STRLEN(VG_Z_LD_LINUX_X86_64_SO_2, strlen)
513 # if defined(VGPV_arm_linux_android) \
514 || defined(VGPV_x86_linux_android) \
515 || defined(VGPV_mips32_linux_android)
516 STRLEN(NONE, __dl_strlen); /* in /system/bin/linker */
517 # endif
518
519 #elif defined(VGO_freebsd)
520 STRLEN(VG_Z_LIBC_SONAME, strlen)
521 STRLEN(VG_Z_LD_ELF_SO_1, strlen)
522 STRLEN(VG_Z_LD_ELF32_SO_1, strlen)
523
524 #elif defined(VGO_darwin)
525 STRLEN(VG_Z_LIBC_SONAME, strlen)
526 # if DARWIN_VERS >= DARWIN_10_9
527 STRLEN(libsystemZucZddylib, strlen)
528 # endif
529
530 #elif defined(VGO_solaris)
531 STRLEN(VG_Z_LIBC_SONAME, strlen)
532 STRLEN(VG_Z_LD_SO_1, strlen)
533
534 #endif
535
536
537 /*---------------------- strcpy ----------------------*/
538
539 #define STRCPY(soname, fnname) \
540 char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \
541 ( char* dst, const char* src ); \
542 char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \
543 ( char* dst, const char* src ) \
544 { \
545 const HChar* src_orig = src; \
546 HChar* dst_orig = dst; \
547 \
548 while (*src) *dst++ = *src++; \
549 *dst = 0; \
550 \
551 /* This happens after copying, unavoidable without */ \
552 /* pre-counting length... should be ok */ \
553 SizeT srclen = (Addr)src-(Addr)src_orig+1; \
554 RECORD_COPY(srclen); \
555 if (is_overlap(dst_orig, \
556 src_orig, \
557 (Addr)dst-(Addr)dst_orig+1, \
558 srclen)) \
559 RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \
560 \
561 return dst_orig; \
562 }
563
564 #if defined(VGO_linux)
565 STRCPY(VG_Z_LIBC_SONAME, strcpy)
566 STRCPY(VG_Z_LIBC_SONAME, __GI_strcpy)
567
568 #elif defined(VGO_freebsd)
569 STRCPY(VG_Z_LIBC_SONAME, strcpy)
570 STRCPY(VG_Z_LD_ELF_SO_1, strcpy)
571 STRCPY(VG_Z_LD_ELF32_SO_1, strcpy)
572
573 #elif defined(VGO_darwin)
574 STRCPY(VG_Z_LIBC_SONAME, strcpy)
575 # if DARWIN_VERS == DARWIN_10_9
576 STRCPY(libsystemZucZddylib, strcpy)
577 # endif
578
579 #elif defined(VGO_solaris)
580 STRCPY(VG_Z_LIBC_SONAME, strcpy)
581 STRCPY(VG_Z_LD_SO_1, strcpy)
582
583 #endif
584
585
586 /*---------------------- strncpy ----------------------*/
587
588 #define STRNCPY(soname, fnname) \
589 char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \
590 ( char* dst, const char* src, SizeT n ); \
591 char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \
592 ( char* dst, const char* src, SizeT n ) \
593 { \
594 const HChar* src_orig = src; \
595 HChar* dst_orig = dst; \
596 SizeT m = 0; \
597 \
598 while (m < n && *src) { m++; *dst++ = *src++; } \
599 /* Check for overlap after copying; all n bytes of dst are relevant, */ \
600 /* but only m+1 bytes of src if terminator was found */ \
601 SizeT srclen = (m < n) ? m+1 : n; \
602 RECORD_COPY(srclen); \
603 if (is_overlap(dst_orig, src_orig, n, srclen)) \
604 RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \
605 while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \
606 \
607 return dst_orig; \
608 }
609
610 #if defined(VGO_linux)
611 STRNCPY(VG_Z_LIBC_SONAME, strncpy)
612 STRNCPY(VG_Z_LIBC_SONAME, __GI_strncpy)
613 STRNCPY(VG_Z_LIBC_SONAME, __strncpy_sse2)
614 STRNCPY(VG_Z_LIBC_SONAME, __strncpy_sse2_unaligned)
615
616 #elif defined(VGO_freebsd)
617 STRNCPY(VG_Z_LIBC_SONAME, strncpy)
618 STRNCPY(VG_Z_LD_ELF_SO_1, strncpy)
619 STRNCPY(VG_Z_LD_ELF32_SO_1, strncpy)
620
621 #elif defined(VGO_darwin)
622 STRNCPY(VG_Z_LIBC_SONAME, strncpy)
623 # if DARWIN_VERS >= DARWIN_10_9
624 STRNCPY(libsystemZucZddylib, strncpy)
625 # endif
626
627 #elif defined(VGO_solaris)
628 STRNCPY(VG_Z_LIBC_SONAME, strncpy)
629 STRNCPY(VG_Z_LD_SO_1, strncpy)
630
631 #endif
632
633
634 /*---------------------- strlcpy ----------------------*/
635
636 /* Copy up to n-1 bytes from src to dst. Then nul-terminate dst if n > 0.
637 Returns strlen(src). Does not zero-fill the remainder of dst. */
638 #define STRLCPY(soname, fnname) \
639 SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \
640 ( char* dst, const char* src, SizeT n ); \
641 SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \
642 ( char* dst, const char* src, SizeT n ) \
643 { \
644 const HChar* src_orig = src; \
645 HChar* dst_orig = dst; \
646 SizeT m = 0; \
647 \
648 STRLCPY_CHECK_FOR_DSTSIZE_ZERO \
649 \
650 while (m+1 < n && *src) { m++; *dst++ = *src++; } \
651 /* m non-nul bytes have now been copied, and m <= n-1. */ \
652 /* Check for overlap after copying; all n bytes of dst are relevant, */ \
653 /* but only m+1 bytes of src if terminator was found */ \
654 SizeT srclen = (m < n) ? m+1 : n; \
655 RECORD_COPY(srclen); \
656 if (is_overlap(dst_orig, src_orig, n, srclen)) \
657 RECORD_OVERLAP_ERROR("strlcpy", dst, src, n); \
658 /* Nul-terminate dst. */ \
659 if (n > 0) *dst = 0; \
660 /* Finish counting strlen(src). */ \
661 while (*src) src++; \
662 return src - src_orig; \
663 }
664
665 #if defined(VGO_linux)
666
667 #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
668 STRLCPY(VG_Z_LIBC_SONAME, strlcpy);
669
670 #elif defined(VGO_freebsd)
671 #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
672 STRLCPY(VG_Z_LD_ELF_SO_1, strlcpy)
673 STRLCPY(VG_Z_LD_ELF32_SO_1, strlcpy)
674 STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
675
676 #elif defined(VGO_darwin)
677 #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
678 //STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
679 //STRLCPY(VG_Z_DYLD, strlcpy)
680 STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
681
682 #elif defined(VGO_solaris)
683 /* special case for n == 0 which is undocumented but heavily used */
684 #define STRLCPY_CHECK_FOR_DSTSIZE_ZERO \
685 if (n == 0) { \
686 while (*src) src++; \
687 return src - src_orig; \
688 }
689
690 STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
691
692 #endif
693
694
695 /*---------------------- strncmp ----------------------*/
696
697 #define STRNCMP(soname, fnname) \
698 int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \
699 ( const char* s1, const char* s2, SizeT nmax ); \
700 int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \
701 ( const char* s1, const char* s2, SizeT nmax ) \
702 { \
703 SizeT n = 0; \
704 while (True) { \
705 if (n >= nmax) return 0; \
706 if (*s1 == 0 && *s2 == 0) return 0; \
707 if (*s1 == 0) return -1; \
708 if (*s2 == 0) return 1; \
709 \
710 if (*(const UChar*)s1 < *(const UChar*)s2) return -1; \
711 if (*(const UChar*)s1 > *(const UChar*)s2) return 1; \
712 \
713 s1++; s2++; n++; \
714 } \
715 }
716
717 #if defined(VGO_linux)
718 STRNCMP(VG_Z_LIBC_SONAME, strncmp)
719 STRNCMP(VG_Z_LIBC_SONAME, __GI_strncmp)
720 STRNCMP(VG_Z_LIBC_SONAME, __strncmp_sse2)
721 STRNCMP(VG_Z_LIBC_SONAME, __strncmp_sse42)
722 STRNCMP(VG_Z_LD_LINUX_SO_2, strncmp)
723 STRNCMP(VG_Z_LD_LINUX_X86_64_SO_2, strncmp)
724
725 #elif defined(VGO_freebsd)
726 STRNCMP(VG_Z_LIBC_SONAME, strncmp)
727 STRNCMP(VG_Z_LD_ELF_SO_1, strncmp)
728 STRNCMP(VG_Z_LD_ELF32_SO_1, strncmp)
729
730 #elif defined(VGO_darwin)
731 STRNCMP(VG_Z_LIBC_SONAME, strncmp)
732 # if DARWIN_VERS >= DARWIN_10_9
733 STRNCMP(libsystemZuplatformZddylib, _platform_strncmp)
734 # endif
735
736 #elif defined(VGO_solaris)
737 STRNCMP(VG_Z_LIBC_SONAME, strncmp)
738
739 #endif
740
741
742 /*---------------------- strcasecmp ----------------------*/
743
744 #define STRCASECMP(soname, fnname) \
745 int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \
746 ( const char* s1, const char* s2 ); \
747 int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \
748 ( const char* s1, const char* s2 ) \
749 { \
750 extern int tolower(int); \
751 register UChar c1; \
752 register UChar c2; \
753 while (True) { \
754 c1 = tolower(*(const UChar *)s1); \
755 c2 = tolower(*(const UChar *)s2); \
756 if (c1 != c2) break; \
757 if (c1 == 0) break; \
758 s1++; s2++; \
759 } \
760 if ((UChar)c1 < (UChar)c2) return -1; \
761 if ((UChar)c1 > (UChar)c2) return 1; \
762 return 0; \
763 }
764
765 #if defined(VGO_linux)
766 # if !defined(VGPV_arm_linux_android) \
767 && !defined(VGPV_x86_linux_android) \
768 && !defined(VGPV_mips32_linux_android) \
769 && !defined(VGPV_arm64_linux_android)
770 STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
771 STRCASECMP(VG_Z_LIBC_SONAME, __GI_strcasecmp)
772 # endif
773
774 #elif defined(VGO_freebsd)
775 STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
776 STRNCMP(VG_Z_LD_ELF_SO_1, strcasecmp)
777 STRNCMP(VG_Z_LD_ELF32_SO_1, strcasecmp)
778
779 #elif defined(VGO_darwin)
780 //STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
781
782 #elif defined(VGO_solaris)
783 STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
784
785 #endif
786
787
788 /*---------------------- strncasecmp ----------------------*/
789
790 #define STRNCASECMP(soname, fnname) \
791 int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \
792 ( const char* s1, const char* s2, SizeT nmax ); \
793 int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \
794 ( const char* s1, const char* s2, SizeT nmax ) \
795 { \
796 extern int tolower(int); \
797 SizeT n = 0; \
798 while (True) { \
799 if (n >= nmax) return 0; \
800 if (*s1 == 0 && *s2 == 0) return 0; \
801 if (*s1 == 0) return -1; \
802 if (*s2 == 0) return 1; \
803 \
804 if (tolower(*(const UChar *)s1) \
805 < tolower(*(const UChar*)s2)) return -1; \
806 if (tolower(*(const UChar *)s1) \
807 > tolower(*(const UChar *)s2)) return 1; \
808 \
809 s1++; s2++; n++; \
810 } \
811 }
812
813 #if defined(VGO_linux)
814 # if !defined(VGPV_arm_linux_android) \
815 && !defined(VGPV_x86_linux_android) \
816 && !defined(VGPV_mips32_linux_android) \
817 && !defined(VGPV_arm64_linux_android)
818 STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
819 STRNCASECMP(VG_Z_LIBC_SONAME, __GI_strncasecmp)
820 # endif
821
822 #elif defined(VGO_freebsd)
823 STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
824 STRNCASECMP(VG_Z_LD_ELF_SO_1, strncasecmp)
825 STRNCASECMP(VG_Z_LD_ELF32_SO_1, strncasecmp)
826
827 #elif defined(VGO_darwin)
828 //STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
829 //STRNCASECMP(VG_Z_DYLD, strncasecmp)
830
831 #elif defined(VGO_solaris)
832 STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
833
834 #endif
835
836
837 /*---------------------- strcasecmp_l ----------------------*/
838
839 #define STRCASECMP_L(soname, fnname) \
840 int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \
841 ( const char* s1, const char* s2, void* locale ); \
842 int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \
843 ( const char* s1, const char* s2, void* locale ) \
844 { \
845 extern int tolower_l(int, void*) __attribute__((weak)); \
846 register UChar c1; \
847 register UChar c2; \
848 while (True) { \
849 c1 = tolower_l(*(const UChar *)s1, locale); \
850 c2 = tolower_l(*(const UChar *)s2, locale); \
851 if (c1 != c2) break; \
852 if (c1 == 0) break; \
853 s1++; s2++; \
854 } \
855 if ((UChar)c1 < (UChar)c2) return -1; \
856 if ((UChar)c1 > (UChar)c2) return 1; \
857 return 0; \
858 }
859
860 #if defined(VGO_linux)
861 STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l)
862 STRCASECMP_L(VG_Z_LIBC_SONAME, __GI_strcasecmp_l)
863 STRCASECMP_L(VG_Z_LIBC_SONAME, __GI___strcasecmp_l)
864
865 #elif defined(VGO_freebsd)
866 STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l)
867
868 #elif defined(VGO_darwin)
869 //STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l)
870
871 #elif defined(VGO_solaris)
872
873 #endif
874
875
876 /*---------------------- strncasecmp_l ----------------------*/
877
878 #define STRNCASECMP_L(soname, fnname) \
879 int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \
880 ( const char* s1, const char* s2, SizeT nmax, void* locale ); \
881 int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \
882 ( const char* s1, const char* s2, SizeT nmax, void* locale ) \
883 { \
884 extern int tolower_l(int, void*) __attribute__((weak)); \
885 SizeT n = 0; \
886 while (True) { \
887 if (n >= nmax) return 0; \
888 if (*s1 == 0 && *s2 == 0) return 0; \
889 if (*s1 == 0) return -1; \
890 if (*s2 == 0) return 1; \
891 \
892 if (tolower_l(*(const UChar *)s1, locale) \
893 < tolower_l(*(const UChar *)s2, locale)) return -1; \
894 if (tolower_l(*(const UChar *)s1, locale) \
895 > tolower_l(*(const UChar *)s2, locale)) return 1; \
896 \
897 s1++; s2++; n++; \
898 } \
899 }
900
901 #if defined(VGO_linux)
902 STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l)
903 STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI_strncasecmp_l)
904 STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI___strncasecmp_l)
905
906 #elif defined(VGO_freebsd)
907 STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l)
908
909 #elif defined(VGO_darwin)
910 //STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l)
911 //STRNCASECMP_L(VG_Z_DYLD, strncasecmp_l)
912
913 #elif defined(VGO_solaris)
914
915 #endif
916
917
918 /*---------------------- strcmp ----------------------*/
919
920 #define STRCMP(soname, fnname) \
921 int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \
922 ( const char* s1, const char* s2 ); \
923 int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \
924 ( const char* s1, const char* s2 ) \
925 { \
926 register UChar c1; \
927 register UChar c2; \
928 while (True) { \
929 c1 = *(const UChar *)s1; \
930 c2 = *(const UChar *)s2; \
931 if (c1 != c2) break; \
932 if (c1 == 0) break; \
933 s1++; s2++; \
934 } \
935 if ((UChar)c1 < (UChar)c2) return -1; \
936 if ((UChar)c1 > (UChar)c2) return 1; \
937 return 0; \
938 }
939
940 #if defined(VGO_linux)
941 STRCMP(VG_Z_LIBC_SONAME, strcmp)
942 STRCMP(VG_Z_LIBC_SONAME, __GI_strcmp)
943 STRCMP(VG_Z_LIBC_SONAME, __strcmp_sse2)
944 STRCMP(VG_Z_LIBC_SONAME, __strcmp_sse42)
945 STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp)
946 STRCMP(VG_Z_LD64_SO_1, strcmp)
947 # if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
948 || defined(VGPV_mips32_linux_android)
949 STRCMP(NONE, __dl_strcmp); /* in /system/bin/linker */
950 # endif
951
952 #elif defined(VGO_freebsd)
953 STRCMP(VG_Z_LIBC_SONAME, strcmp)
954 STRCMP(VG_Z_LD_ELF_SO_1, strcmp)
955 STRCMP(VG_Z_LD_ELF32_SO_1, strcmp)
956
957 #elif defined(VGO_darwin)
958 STRCMP(VG_Z_LIBC_SONAME, strcmp)
959 # if DARWIN_VERS >= DARWIN_10_9
960 STRCMP(libsystemZuplatformZddylib, _platform_strcmp)
961 # endif
962
963 #elif defined(VGO_solaris)
964 STRCMP(VG_Z_LIBC_SONAME, strcmp)
965 STRCMP(VG_Z_LD_SO_1, strcmp)
966
967 #endif
968
969
970 /*---------------------- memchr ----------------------*/
971
972 #define MEMCHR(soname, fnname) \
973 void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \
974 (const void *s, int c, SizeT n); \
975 void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \
976 (const void *s, int c, SizeT n) \
977 { \
978 SizeT i; \
979 UChar c0 = (UChar)c; \
980 const UChar* p = s; \
981 for (i = 0; i < n; i++) \
982 if (p[i] == c0) return CONST_CAST(void *,&p[i]); \
983 return NULL; \
984 }
985
986 #if defined(VGO_linux)
987 MEMCHR(VG_Z_LIBC_SONAME, memchr)
988 MEMCHR(VG_Z_LIBC_SONAME, __GI_memchr)
989
990 #elif defined(VGO_freebsd)
991 MEMCHR(VG_Z_LIBC_SONAME, memchr)
992
993 #elif defined(VGO_darwin)
994 # if DARWIN_VERS == DARWIN_10_9
995 MEMCHR(VG_Z_DYLD, memchr)
996 MEMCHR(libsystemZuplatformZddylib, _platform_memchr)
997 # endif
998 # if DARWIN_VERS >= DARWIN_10_10
999 MEMCHR(VG_Z_DYLD, memchr)
1000 /* _platform_memchr$VARIANT$Generic */
1001 MEMCHR(libsystemZuplatformZddylib, _platform_memchr$VARIANT$Generic)
1002 /* _platform_memchr$VARIANT$Haswell */
1003 MEMCHR(libsystemZuplatformZddylib, _platform_memchr$VARIANT$Haswell)
1004 # endif
1005 # if DARWIN_VERS >= DARWIN_10_12
1006 /* _platform_memchr$VARIANT$Base */
1007 MEMCHR(libsystemZuplatformZddylib, _platform_memchr$VARIANT$Base)
1008 #endif
1009
1010 #elif defined(VGO_solaris)
1011 MEMCHR(VG_Z_LIBC_SONAME, memchr)
1012
1013 #endif
1014
1015
1016 /*---------------------- memrchr ----------------------*/
1017
1018 #define MEMRCHR(soname, fnname) \
1019 void* VG_REPLACE_FUNCTION_EZU(20360,soname,fnname) \
1020 (const void *s, int c, SizeT n); \
1021 void* VG_REPLACE_FUNCTION_EZU(20360,soname,fnname) \
1022 (const void *s, int c, SizeT n) \
1023 { \
1024 SizeT i; \
1025 UChar c0 = (UChar)c; \
1026 const UChar* p = s; \
1027 for (i = 0; i < n; i++) \
1028 if (p[n-1-i] == c0) return CONST_CAST(void *,&p[n-1-i]); \
1029 return NULL; \
1030 }
1031
1032 #if defined(VGO_linux)
1033 MEMRCHR(VG_Z_LIBC_SONAME, memrchr)
1034
1035 #elif defined(VGO_freebsd)
1036 MEMRCHR(VG_Z_LIBC_SONAME, memrchr)
1037
1038 #elif defined(VGO_darwin)
1039 //MEMRCHR(VG_Z_LIBC_SONAME, memrchr)
1040 //MEMRCHR(VG_Z_DYLD, memrchr)
1041
1042 #elif defined(VGO_solaris)
1043
1044 #endif
1045
1046
1047 /*---------------------- memcpy ----------------------*/
1048
1049 #define MEMMOVE_OR_MEMCPY(becTag, soname, fnname, do_ol_check) \
1050 void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \
1051 ( void *dst, const void *src, SizeT len ); \
1052 void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \
1053 ( void *dst, const void *src, SizeT len ) \
1054 { \
1055 RECORD_COPY(len); \
1056 if (do_ol_check && is_overlap(dst, src, len, len)) \
1057 RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \
1058 \
1059 const Addr WS = sizeof(UWord); /* 8 or 4 */ \
1060 const Addr WM = WS - 1; /* 7 or 3 */ \
1061 \
1062 if (len > 0) { \
1063 if (dst < src || !is_overlap(dst, src, len, len)) { \
1064 \
1065 /* Copying backwards. */ \
1066 SizeT n = len; \
1067 Addr d = (Addr)dst; \
1068 Addr s = (Addr)src; \
1069 \
1070 if (((s^d) & WM) == 0) { \
1071 /* s and d have same UWord alignment. */ \
1072 /* Pull up to a UWord boundary. */ \
1073 while ((s & WM) != 0 && n >= 1) \
1074 { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
1075 /* Copy UWords. */ \
1076 while (n >= WS * 4) \
1077 { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; \
1078 *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; \
1079 *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; \
1080 *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \
1081 while (n >= WS) \
1082 { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \
1083 if (n == 0) \
1084 return dst; \
1085 } \
1086 if (((s|d) & 1) == 0) { \
1087 /* Both are 16-aligned; copy what we can thusly. */ \
1088 while (n >= 2) \
1089 { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \
1090 } \
1091 /* Copy leftovers, or everything if misaligned. */ \
1092 while (n >= 1) \
1093 { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
1094 \
1095 } else if (dst > src) { \
1096 \
1097 SizeT n = len; \
1098 Addr d = ((Addr)dst) + n; \
1099 Addr s = ((Addr)src) + n; \
1100 \
1101 /* Copying forwards. */ \
1102 if (((s^d) & WM) == 0) { \
1103 /* s and d have same UWord alignment. */ \
1104 /* Back down to a UWord boundary. */ \
1105 while ((s & WM) != 0 && n >= 1) \
1106 { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
1107 /* Copy UWords. */ \
1108 while (n >= WS * 4) \
1109 { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; \
1110 s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; \
1111 s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; \
1112 s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \
1113 while (n >= WS) \
1114 { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \
1115 if (n == 0) \
1116 return dst; \
1117 } \
1118 if (((s|d) & 1) == 0) { \
1119 /* Both are 16-aligned; copy what we can thusly. */ \
1120 while (n >= 2) \
1121 { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \
1122 } \
1123 /* Copy leftovers, or everything if misaligned. */ \
1124 while (n >= 1) \
1125 { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
1126 \
1127 } \
1128 } \
1129 \
1130 return dst; \
1131 }
1132
1133 #define MEMMOVE(soname, fnname) \
1134 MEMMOVE_OR_MEMCPY(20181, soname, fnname, 0)
1135
1136 /* See https://bugs.kde.org/show_bug.cgi?id=402833
1137 why we disable the overlap check on x86_64. */
1138 #if defined(VGP_amd64_linux) || defined(VGP_arm64_freebsd)
1139 #define MEMCPY(soname, fnname) \
1140 MEMMOVE_OR_MEMCPY(20180, soname, fnname, 0)
1141 #else
1142 #define MEMCPY(soname, fnname) \
1143 MEMMOVE_OR_MEMCPY(20180, soname, fnname, 1)
1144 #endif
1145
1146 #if defined(VGO_linux)
1147 /* For older memcpy we have to use memmove-like semantics and skip
1148 the overlap check; sigh; see #275284. */
1149 MEMMOVE(VG_Z_LIBC_SONAME, memcpyZAGLIBCZu2Zd2Zd5) /* memcpy@GLIBC_2.2.5 */
1150 MEMCPY(VG_Z_LIBC_SONAME, memcpyZAZAGLIBCZu2Zd14) /* memcpy@@GLIBC_2.14 */
1151 MEMCPY(VG_Z_LIBC_SONAME, memcpy) /* fallback case */
1152 MEMCPY(VG_Z_LIBC_SONAME, __GI_memcpy)
1153 MEMCPY(VG_Z_LIBC_SONAME, __memcpy_sse2)
1154 MEMCPY(VG_Z_LIBC_SONAME, __memcpy_avx_unaligned_erms)
1155 MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */
1156 MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */
1157 /* icc9 blats these around all over the place. Not only in the main
1158 executable but various .so's. They are highly tuned and read
1159 memory beyond the source boundary (although work correctly and
1160 never go across page boundaries), so give errors when run
1161 natively, at least for misaligned source arg. Just intercepting
1162 in the exe only until we understand more about the problem. See
1163 http://bugs.kde.org/show_bug.cgi?id=139776
1164 */
1165 MEMCPY(NONE, ZuintelZufastZumemcpy)
1166
1167 #elif defined(VGO_freebsd)
1168 MEMCPY(VG_Z_LIBC_SONAME, memcpy)
1169 MEMCPY(VG_Z_LD_ELF_SO_1, memcpy)
1170 MEMCPY(VG_Z_LD_ELF32_SO_1, memcpy)
1171
1172 #elif defined(VGO_darwin)
1173 # if DARWIN_VERS <= DARWIN_10_6
1174 MEMCPY(VG_Z_LIBC_SONAME, memcpy)
1175 # endif
1176 MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse3x) /* memcpy$VARIANT$sse3x */
1177 MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse42) /* memcpy$VARIANT$sse42 */
1178
1179 #elif defined(VGO_solaris)
1180 MEMCPY(VG_Z_LIBC_SONAME, memcpy)
1181 MEMCPY(VG_Z_LIBC_SONAME, memcpyZPZa)
1182 MEMCPY(VG_Z_LD_SO_1, memcpy)
1183
1184 #endif
1185
1186
1187 /*---------------------- memcmp ----------------------*/
1188
1189 #define MEMCMP(soname, fnname) \
1190 int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \
1191 ( const void *s1V, const void *s2V, SizeT n ); \
1192 int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \
1193 ( const void *s1V, const void *s2V, SizeT n ) \
1194 { \
1195 const SizeT WS = sizeof(UWord); /* 8 or 4 */ \
1196 const SizeT WM = WS - 1; /* 7 or 3 */ \
1197 Addr s1A = (Addr)s1V; \
1198 Addr s2A = (Addr)s2V; \
1199 \
1200 if (((s1A | s2A) & WM) == 0) { \
1201 /* Both areas are word aligned. Skip over the */ \
1202 /* equal prefix as fast as possible. */ \
1203 while (n >= WS) { \
1204 UWord w1 = *(UWord*)s1A; \
1205 UWord w2 = *(UWord*)s2A; \
1206 if (w1 != w2) break; \
1207 s1A += WS; \
1208 s2A += WS; \
1209 n -= WS; \
1210 } \
1211 } \
1212 \
1213 const UChar* s1 = (const UChar*) s1A; \
1214 const UChar* s2 = (const UChar*) s2A; \
1215 \
1216 while (n != 0) { \
1217 UChar a0 = s1[0]; \
1218 UChar b0 = s2[0]; \
1219 s1 += 1; \
1220 s2 += 1; \
1221 int res = ((int)a0) - ((int)b0); \
1222 if (res != 0) \
1223 return res; \
1224 n -= 1; \
1225 } \
1226 return 0; \
1227 }
1228
1229 #if defined(VGO_linux)
1230 MEMCMP(VG_Z_LIBC_SONAME, memcmp)
1231 MEMCMP(VG_Z_LIBC_SONAME, __GI_memcmp)
1232 MEMCMP(VG_Z_LIBC_SONAME, __memcmp_sse2)
1233 MEMCMP(VG_Z_LIBC_SONAME, __memcmp_sse4_1)
1234 MEMCMP(VG_Z_LIBC_SONAME, bcmp)
1235 MEMCMP(VG_Z_LD_SO_1, bcmp)
1236
1237 #elif defined(VGO_freebsd)
1238 MEMCMP(VG_Z_LIBC_SONAME, memcmp)
1239 MEMCMP(VG_Z_LIBC_SONAME, bcmp)
1240 MEMCMP(VG_Z_LIBC_SONAME, timingsafe_memcmp)
1241 MEMCMP(VG_Z_LIBC_SONAME, timingsafe_bcmp)
1242
1243 #elif defined(VGO_darwin)
1244 # if DARWIN_VERS >= DARWIN_10_9
1245 MEMCMP(libsystemZuplatformZddylib, _platform_memcmp)
1246 # endif
1247
1248 #elif defined(VGO_solaris)
1249 MEMCMP(VG_Z_LIBC_SONAME, memcmp)
1250 MEMCMP(VG_Z_LIBC_SONAME, bcmp)
1251 MEMCMP(VG_Z_LD_SO_1, memcmp)
1252
1253 #endif
1254
1255
1256 /*---------------------- stpcpy ----------------------*/
1257
1258 /* Copy SRC to DEST, returning the address of the terminating '\0' in
1259 DEST. (minor variant of strcpy) */
1260 #define STPCPY(soname, fnname) \
1261 char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \
1262 ( char* dst, const char* src ); \
1263 char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \
1264 ( char* dst, const char* src ) \
1265 { \
1266 const HChar* src_orig = src; \
1267 HChar* dst_orig = dst; \
1268 \
1269 while (*src) *dst++ = *src++; \
1270 *dst = 0; \
1271 \
1272 /* This checks for overlap after copying, unavoidable without */ \
1273 /* pre-counting length... should be ok */ \
1274 SizeT srclen = (Addr)src-(Addr)src_orig+1; \
1275 RECORD_COPY(srclen); \
1276 if (is_overlap(dst_orig, \
1277 src_orig, \
1278 (Addr)dst-(Addr)dst_orig+1, \
1279 srclen)) \
1280 RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \
1281 \
1282 return dst; \
1283 }
1284
1285 #if defined(VGO_linux)
1286 STPCPY(VG_Z_LIBC_SONAME, stpcpy)
1287 STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy)
1288 STPCPY(VG_Z_LIBC_SONAME, __stpcpy_sse2)
1289 STPCPY(VG_Z_LIBC_SONAME, __stpcpy_sse2_unaligned)
1290 STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy)
1291 STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy)
1292 STPCPY(VG_Z_LD_LINUX_AARCH64_SO_1,stpcpy)
1293
1294 #elif defined(VGO_freebsd)
1295 STPCPY(VG_Z_LD_ELF_SO_1, stpcpy)
1296 STPCPY(VG_Z_LD_ELF32_SO_1, stpcpy)
1297 STPCPY(VG_Z_LIBC_SONAME, stpcpy)
1298
1299 #elif defined(VGO_freebsd)
1300 STPCPY(VG_Z_LD_ELF_SO_1, stpcpy)
1301 STPCPY(VG_Z_LD_ELF32_SO_1, stpcpy)
1302 STPCPY(VG_Z_LIBC_SONAME, stpcpy)
1303
1304 #elif defined(VGO_darwin)
1305 //STPCPY(VG_Z_LIBC_SONAME, stpcpy)
1306 //STPCPY(VG_Z_DYLD, stpcpy)
1307
1308 #elif defined(VGO_solaris)
1309 STPCPY(VG_Z_LIBC_SONAME, stpcpy)
1310
1311 #endif
1312
1313
1314 /*---------------------- stpncpy ----------------------*/
1315
1316 #define STPNCPY(soname, fnname) \
1317 char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
1318 ( char* dst, const char* src, SizeT n ); \
1319 char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
1320 ( char* dst, const char* src, SizeT n ) \
1321 { \
1322 const HChar* src_orig = src; \
1323 HChar* dst_str = dst; \
1324 SizeT m = 0; \
1325 \
1326 while (m < n && *src) { m++; *dst++ = *src++; } \
1327 /* Check for overlap after copying; all n bytes of dst are relevant, */ \
1328 /* but only m+1 bytes of src if terminator was found */ \
1329 SizeT srclen = (m < n) ? m+1 : n; \
1330 RECORD_COPY(srclen); \
1331 if (is_overlap(dst_str, src_orig, n, srclen)) \
1332 RECORD_OVERLAP_ERROR("stpncpy", dst, src, n); \
1333 dst_str = dst; \
1334 while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \
1335 \
1336 return dst_str; \
1337 }
1338
1339 #if defined(VGO_linux) || defined(VGO_freebsd)
1340 STPNCPY(VG_Z_LIBC_SONAME, stpncpy)
1341 #endif
1342
1343
1344 /*---------------------- memset ----------------------*/
1345
1346 #define MEMSET(soname, fnname) \
1347 void* VG_REPLACE_FUNCTION_EZZ(20210,soname,fnname) \
1348 (void *s, Int c, SizeT n); \
1349 void* VG_REPLACE_FUNCTION_EZZ(20210,soname,fnname) \
1350 (void *s, Int c, SizeT n) \
1351 { \
1352 if (sizeof(void*) == 8) { \
1353 Addr a = (Addr)s; \
1354 ULong c8 = (c & 0xFF); \
1355 c8 = (c8 << 8) | c8; \
1356 c8 = (c8 << 16) | c8; \
1357 c8 = (c8 << 32) | c8; \
1358 while ((a & 7) != 0 && n >= 1) \
1359 { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
1360 while (n >= 32) \
1361 { *(ULong*)a = c8; a += 8; n -= 8; \
1362 *(ULong*)a = c8; a += 8; n -= 8; \
1363 *(ULong*)a = c8; a += 8; n -= 8; \
1364 *(ULong*)a = c8; a += 8; n -= 8; } \
1365 while (n >= 8) \
1366 { *(ULong*)a = c8; a += 8; n -= 8; } \
1367 while (n >= 1) \
1368 { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
1369 return s; \
1370 } else { \
1371 Addr a = (Addr)s; \
1372 UInt c4 = (c & 0xFF); \
1373 c4 = (c4 << 8) | c4; \
1374 c4 = (c4 << 16) | c4; \
1375 while ((a & 3) != 0 && n >= 1) \
1376 { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
1377 while (n >= 16) \
1378 { *(UInt*)a = c4; a += 4; n -= 4; \
1379 *(UInt*)a = c4; a += 4; n -= 4; \
1380 *(UInt*)a = c4; a += 4; n -= 4; \
1381 *(UInt*)a = c4; a += 4; n -= 4; } \
1382 while (n >= 4) \
1383 { *(UInt*)a = c4; a += 4; n -= 4; } \
1384 while (n >= 1) \
1385 { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
1386 return s; \
1387 } \
1388 }
1389
1390 #if defined(VGO_linux)
1391 MEMSET(VG_Z_LIBC_SONAME, memset)
1392
1393 #elif defined(VGO_freebsd)
1394 MEMSET(VG_Z_LIBC_SONAME, memset)
1395 MEMSET(VG_Z_LD_ELF_SO_1, memset)
1396 MEMSET(VG_Z_LD_ELF32_SO_1, memset)
1397
1398 #elif defined(VGO_darwin)
1399 //MEMSET(VG_Z_LIBC_SONAME, memset)
1400 //MEMSET(VG_Z_DYLD, memset)
1401 MEMSET(VG_Z_LIBC_SONAME, memset)
1402
1403 #elif defined(VGO_solaris)
1404 MEMSET(VG_Z_LIBC_SONAME, memset)
1405 MEMSET(VG_Z_LIBC_SONAME, memsetZPZa)
1406
1407 #endif
1408
1409
1410 /*---------------------- memmove ----------------------*/
1411
1412 /* memmove -- use the MEMMOVE defn above. */
1413
1414 #if defined(VGO_linux)
1415 MEMMOVE(VG_Z_LIBC_SONAME, memmove)
1416 MEMMOVE(VG_Z_LIBC_SONAME, __GI_memmove)
1417 /* See bug #349828 Override for ld64.so.1 like memcpy, because for some
1418 arches MEMCPY_OK_FOR_FORWARD_MEMMOVE is set, which might cause memmove
1419 to call memcpy. */
1420 MEMMOVE(VG_Z_LD64_SO_1, memmove)
1421
1422 #elif defined(VGO_freebsd)
1423 MEMMOVE(VG_Z_LD_ELF_SO_1, memmove)
1424 MEMMOVE(VG_Z_LD_ELF32_SO_1, memmove)
1425 MEMMOVE(VG_Z_LIBC_SONAME, memmove)
1426
1427 #elif defined(VGO_darwin)
1428 # if DARWIN_VERS <= DARWIN_10_6
1429 MEMMOVE(VG_Z_LIBC_SONAME, memmove)
1430 # endif
1431 MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse3x) /* memmove$VARIANT$sse3x */
1432 MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse42) /* memmove$VARIANT$sse42 */
1433 # if DARWIN_VERS >= DARWIN_10_9
1434 /* _platform_memmove$VARIANT$Ivybridge */
1435 MEMMOVE(libsystemZuplatformZddylib, ZuplatformZumemmoveZDVARIANTZDIvybridge)
1436 # endif
1437
1438 #elif defined(VGO_solaris)
1439 MEMMOVE(VG_Z_LIBC_SONAME, memmove)
1440 MEMMOVE(VG_Z_LIBC_SONAME, memmoveZPZa)
1441 MEMMOVE(VG_Z_LD_SO_1, memmove)
1442
1443 #endif
1444
1445
1446 /*---------------------- bcopy ----------------------*/
1447
1448 #define BCOPY(soname, fnname) \
1449 void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \
1450 (const void *srcV, void *dstV, SizeT n); \
1451 void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \
1452 (const void *srcV, void *dstV, SizeT n) \
1453 { \
1454 RECORD_COPY(n); \
1455 SizeT i; \
1456 HChar* dst = dstV; \
1457 const HChar* src = srcV; \
1458 if (dst < src) { \
1459 for (i = 0; i < n; i++) \
1460 dst[i] = src[i]; \
1461 } \
1462 else \
1463 if (dst > src) { \
1464 for (i = 0; i < n; i++) \
1465 dst[n-i-1] = src[n-i-1]; \
1466 } \
1467 }
1468
1469 #if defined(VGO_linux)
1470 BCOPY(VG_Z_LIBC_SONAME, bcopy)
1471
1472 #elif defined(VGO_freebsd)
1473 BCOPY(VG_Z_LIBC_SONAME, bcopy)
1474 BCOPY(VG_Z_LD_ELF_SO_1, bcopy)
1475 BCOPY(VG_Z_LD_ELF32_SO_1, bcopy)
1476
1477 #elif defined(VGO_darwin)
1478 //BCOPY(VG_Z_LIBC_SONAME, bcopy)
1479 //BCOPY(VG_Z_DYLD, bcopy)
1480
1481 #elif defined(VGO_darwin)
1482 BCOPY(VG_Z_LIBC_SONAME, bcopy)
1483
1484 #endif
1485
1486
1487 /*-------------------- memmove_chk --------------------*/
1488
1489 /* glibc 2.5 variant of memmove which checks the dest is big enough.
1490 There is no specific part of glibc that this is copied from. */
1491 #define GLIBC25___MEMMOVE_CHK(soname, fnname) \
1492 void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \
1493 (void *dstV, const void *srcV, SizeT n, SizeT destlen); \
1494 void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \
1495 (void *dstV, const void *srcV, SizeT n, SizeT destlen) \
1496 { \
1497 RECORD_COPY(n); \
1498 SizeT i; \
1499 HChar* dst = dstV; \
1500 const HChar* src = srcV; \
1501 if (destlen < n) \
1502 goto badness; \
1503 if (dst < src) { \
1504 for (i = 0; i < n; i++) \
1505 dst[i] = src[i]; \
1506 } \
1507 else \
1508 if (dst > src) { \
1509 for (i = 0; i < n; i++) \
1510 dst[n-i-1] = src[n-i-1]; \
1511 } \
1512 return dst; \
1513 badness: \
1514 VALGRIND_PRINTF_BACKTRACE( \
1515 "*** memmove_chk: buffer overflow detected ***: " \
1516 "program terminated\n"); \
1517 my_exit(1); \
1518 /*NOTREACHED*/ \
1519 return NULL; \
1520 }
1521
1522 #if defined(VGO_linux)
1523 GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk)
1524
1525 #elif defined(VGO_darwin)
1526
1527 #elif defined(VGO_solaris)
1528
1529 #endif
1530
1531
1532 /*-------------------- strchrnul --------------------*/
1533
1534 /* Find the first occurrence of C in S or the final NUL byte. */
1535 #define GLIBC232_STRCHRNUL(soname, fnname) \
1536 char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \
1537 (const char* s, int c_in); \
1538 char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \
1539 (const char* s, int c_in) \
1540 { \
1541 HChar c = (HChar) c_in; \
1542 const HChar* char_ptr = s; \
1543 while (1) { \
1544 if (*char_ptr == 0) return CONST_CAST(HChar *,char_ptr); \
1545 if (*char_ptr == c) return CONST_CAST(HChar *,char_ptr); \
1546 char_ptr++; \
1547 } \
1548 }
1549
1550 #if defined(VGO_linux)
1551 GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul)
1552
1553 #elif defined(VGO_freebsd)
1554 GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul)
1555
1556 #elif defined(VGO_darwin)
1557
1558 #elif defined(VGO_solaris)
1559
1560 #endif
1561
1562
1563 /*---------------------- rawmemchr ----------------------*/
1564
1565 /* Find the first occurrence of C in S. */
1566 #define GLIBC232_RAWMEMCHR(soname, fnname) \
1567 void* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \
1568 (const void* s, int c_in); \
1569 void* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \
1570 (const void* s, int c_in) \
1571 { \
1572 UChar c = (UChar) c_in; \
1573 const UChar* char_ptr = s; \
1574 while (1) { \
1575 if (*char_ptr == c) return CONST_CAST(void *,char_ptr); \
1576 char_ptr++; \
1577 } \
1578 }
1579
1580 #if defined (VGO_linux)
1581 GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr)
1582 GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr)
1583
1584 #elif defined(VGO_darwin)
1585
1586 #elif defined(VGO_solaris)
1587
1588 #endif
1589
1590
1591 /*---------------------- strcpy_chk ----------------------*/
1592
1593 /* glibc variant of strcpy that checks the dest is big enough.
1594 Copied from glibc-2.5/debug/test-strcpy_chk.c. */
1595 #define GLIBC25___STRCPY_CHK(soname,fnname) \
1596 char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \
1597 (char* dst, const char* src, SizeT len); \
1598 char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \
1599 (char* dst, const char* src, SizeT len) \
1600 { \
1601 FOR_COPY(const HChar* src_orig = src); \
1602 HChar* ret = dst; \
1603 if (! len) \
1604 goto badness; \
1605 while ((*dst++ = *src++) != '\0') \
1606 if (--len == 0) \
1607 goto badness; \
1608 RECORD_COPY((Addr)src-(Addr)src_orig); \
1609 return ret; \
1610 badness: \
1611 VALGRIND_PRINTF_BACKTRACE( \
1612 "*** strcpy_chk: buffer overflow detected ***: " \
1613 "program terminated\n"); \
1614 my_exit(1); \
1615 /*NOTREACHED*/ \
1616 return NULL; \
1617 }
1618
1619 #if defined(VGO_linux)
1620 GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk)
1621
1622 #elif defined(VGO_darwin)
1623
1624 #elif defined(VGO_solaris)
1625
1626 #endif
1627
1628
1629 /*---------------------- stpcpy_chk ----------------------*/
1630
1631 /* glibc variant of stpcpy that checks the dest is big enough.
1632 Copied from glibc-2.5/debug/test-stpcpy_chk.c. */
1633 #define GLIBC25___STPCPY_CHK(soname,fnname) \
1634 char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \
1635 (char* dst, const char* src, SizeT len); \
1636 char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \
1637 (char* dst, const char* src, SizeT len) \
1638 { \
1639 FOR_COPY(const HChar* src_orig = src); \
1640 if (! len) \
1641 goto badness; \
1642 while ((*dst++ = *src++) != '\0') \
1643 if (--len == 0) \
1644 goto badness; \
1645 RECORD_COPY((Addr)src-(Addr)src_orig); \
1646 return dst - 1; \
1647 badness: \
1648 VALGRIND_PRINTF_BACKTRACE( \
1649 "*** stpcpy_chk: buffer overflow detected ***: " \
1650 "program terminated\n"); \
1651 my_exit(1); \
1652 /*NOTREACHED*/ \
1653 return NULL; \
1654 }
1655
1656 #if defined(VGO_linux)
1657 GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk)
1658
1659 #elif defined(VGO_darwin)
1660
1661 #elif defined(VGO_solaris)
1662
1663 #endif
1664
1665
1666 /*---------------------- mempcpy ----------------------*/
1667
1668 /* mempcpy */
1669 #define GLIBC25_MEMPCPY(soname, fnname) \
1670 void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \
1671 ( void *dst, const void *src, SizeT len ); \
1672 void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \
1673 ( void *dst, const void *src, SizeT len ) \
1674 { \
1675 RECORD_COPY(len); \
1676 SizeT len_saved = len; \
1677 \
1678 if (len == 0) \
1679 return dst; \
1680 \
1681 if (is_overlap(dst, src, len, len)) \
1682 RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \
1683 \
1684 if ( dst > src ) { \
1685 register HChar *d = (char *)dst + len - 1; \
1686 register const HChar *s = (const char *)src + len - 1; \
1687 while ( len-- ) { \
1688 *d-- = *s--; \
1689 } \
1690 } else if ( dst < src ) { \
1691 register HChar *d = dst; \
1692 register const HChar *s = src; \
1693 while ( len-- ) { \
1694 *d++ = *s++; \
1695 } \
1696 } \
1697 return (void*)( ((char*)dst) + len_saved ); \
1698 }
1699
1700 #if defined(VGO_linux)
1701 GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy)
1702 GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, __GI_mempcpy)
1703 GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */
1704 GLIBC25_MEMPCPY(VG_Z_LD_LINUX_SO_3, mempcpy) /* ld-linux.so.3 */
1705 GLIBC25_MEMPCPY(VG_Z_LD_LINUX_X86_64_SO_2, mempcpy) /* ld-linux-x86-64.so.2 */
1706
1707 #elif defined(VGO_freebsd)
1708 GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy)
1709 #elif defined(VGO_darwin)
1710 //GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy)
1711
1712 #elif defined(VGO_solaris)
1713
1714 #endif
1715
1716
1717 /*-------------------- memcpy_chk --------------------*/
1718
1719 /* See https://bugs.kde.org/show_bug.cgi?id=402833
1720 why we disable the overlap check on x86_64. */
1721 #if defined(VGP_amd64_linux)
1722 #define CHECK_OVERLAP 0
1723 #else
1724 #define CHECK_OVERLAP 1
1725 #endif
1726
1727 #define GLIBC26___MEMCPY_CHK(soname, fnname) \
1728 void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \
1729 (void* dst, const void* src, SizeT len, SizeT dstlen ); \
1730 void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \
1731 (void* dst, const void* src, SizeT len, SizeT dstlen ) \
1732 { \
1733 register HChar *d; \
1734 register const HChar *s; \
1735 if (dstlen < len) \
1736 goto badness; \
1737 RECORD_COPY(len); \
1738 if (len == 0) \
1739 return dst; \
1740 if (CHECK_OVERLAP && is_overlap(dst, src, len, len)) \
1741 RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \
1742 if ( dst > src ) { \
1743 d = (HChar *)dst + len - 1; \
1744 s = (const HChar *)src + len - 1; \
1745 while ( len-- ) { \
1746 *d-- = *s--; \
1747 } \
1748 } else if ( dst < src ) { \
1749 d = (HChar *)dst; \
1750 s = (const HChar *)src; \
1751 while ( len-- ) { \
1752 *d++ = *s++; \
1753 } \
1754 } \
1755 return dst; \
1756 badness: \
1757 VALGRIND_PRINTF_BACKTRACE( \
1758 "*** memcpy_chk: buffer overflow detected ***: " \
1759 "program terminated\n"); \
1760 my_exit(1); \
1761 /*NOTREACHED*/ \
1762 return NULL; \
1763 }
1764
1765 #if defined(VGO_linux)
1766 GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk)
1767
1768 #elif defined(VGO_darwin)
1769
1770 #elif defined(VGO_solaris)
1771
1772 #endif
1773
1774
1775 /*---------------------- strstr ----------------------*/
1776
1777 #define STRSTR(soname, fnname) \
1778 char* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \
1779 (const char* haystack, const char* needle); \
1780 char* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \
1781 (const char* haystack, const char* needle) \
1782 { \
1783 const HChar* h = haystack; \
1784 const HChar* n = needle; \
1785 \
1786 /* find the length of n, not including terminating zero */ \
1787 UWord nlen = 0; \
1788 while (n[nlen]) nlen++; \
1789 \
1790 /* if n is the empty string, match immediately. */ \
1791 if (nlen == 0) return CONST_CAST(HChar *,h); \
1792 \
1793 /* assert(nlen >= 1); */ \
1794 HChar n0 = n[0]; \
1795 \
1796 while (1) { \
1797 const HChar hh = *h; \
1798 if (hh == 0) return NULL; \
1799 if (hh != n0) { h++; continue; } \
1800 \
1801 UWord i; \
1802 for (i = 0; i < nlen; i++) { \
1803 if (n[i] != h[i]) \
1804 break; \
1805 } \
1806 /* assert(i >= 0 && i <= nlen); */ \
1807 if (i == nlen) \
1808 return CONST_CAST(HChar *,h); \
1809 \
1810 h++; \
1811 } \
1812 }
1813
1814 #if defined(VGO_linux)
1815 STRSTR(VG_Z_LIBC_SONAME, strstr)
1816 STRSTR(VG_Z_LIBC_SONAME, __strstr_sse2)
1817 STRSTR(VG_Z_LIBC_SONAME, __strstr_sse42)
1818
1819 #elif defined(VGO_freebsd)
1820 STRSTR(VG_Z_LIBC_SONAME, strstr)
1821
1822 #elif defined(VGO_darwin)
1823
1824 #elif defined(VGO_solaris)
1825 STRSTR(VG_Z_LIBC_SONAME, strstr)
1826
1827 #endif
1828
1829 /*---------------------- memmem ----------------------*/
1830
1831 #define MEMMEM(soname, fnname) \
1832 void* VG_REPLACE_FUNCTION_EZU(20460,soname,fnname) \
1833 (const void* haystack, SizeT hlen, const void* needle, SizeT nlen); \
1834 void* VG_REPLACE_FUNCTION_EZU(20460,soname,fnname) \
1835 (const void* haystack, SizeT hlen, const void* needle, SizeT nlen) \
1836 { \
1837 const HChar* h = haystack; \
1838 const HChar* n = needle; \
1839 \
1840 /* If the needle is the empty string, match immediately. */ \
1841 if (nlen == 0) return CONST_CAST(void *,h); \
1842 \
1843 HChar n0 = n[0]; \
1844 \
1845 for (; hlen >= nlen; hlen--, h++) { \
1846 if (h[0] != n0) continue; \
1847 \
1848 UWord i; \
1849 for (i = 1; i < nlen; i++) { \
1850 if (n[i] != h[i]) \
1851 break; \
1852 } \
1853 if (i == nlen) \
1854 return CONST_CAST(HChar *,h); \
1855 \
1856 } \
1857 return NULL; \
1858 }
1859
1860 #if defined(VGP_s390x_linux)
1861 MEMMEM(VG_Z_LIBC_SONAME, memmem)
1862 #endif
1863
1864
1865 /*---------------------- strpbrk ----------------------*/
1866
1867 #define STRPBRK(soname, fnname) \
1868 char* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \
1869 (const char* sV, const char* acceptV); \
1870 char* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \
1871 (const char* sV, const char* acceptV) \
1872 { \
1873 const HChar* s = sV; \
1874 const HChar* accept = acceptV; \
1875 \
1876 /* find the length of 'accept', not including terminating zero */ \
1877 UWord nacc = 0; \
1878 while (accept[nacc]) nacc++; \
1879 \
1880 /* if n is the empty string, fail immediately. */ \
1881 if (nacc == 0) return NULL; \
1882 \
1883 /* assert(nacc >= 1); */ \
1884 while (1) { \
1885 UWord i; \
1886 HChar sc = *s; \
1887 if (sc == 0) \
1888 break; \
1889 for (i = 0; i < nacc; i++) { \
1890 if (sc == accept[i]) \
1891 return CONST_CAST(HChar *,s); \
1892 } \
1893 s++; \
1894 } \
1895 \
1896 return NULL; \
1897 }
1898
1899 #if defined(VGO_linux)
1900 STRPBRK(VG_Z_LIBC_SONAME, strpbrk)
1901
1902 #elif defined(VGO_freebsd)
1903 STRPBRK(VG_Z_LIBC_SONAME, strpbrk)
1904
1905 #elif defined(VGO_darwin)
1906
1907 #elif defined(VGO_solaris)
1908 STRPBRK(VG_Z_LIBC_SONAME, strpbrk)
1909
1910 #endif
1911
1912
1913 /*---------------------- strcspn ----------------------*/
1914
1915 #define STRCSPN(soname, fnname) \
1916 SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \
1917 (const char* sV, const char* rejectV); \
1918 SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \
1919 (const char* sV, const char* rejectV) \
1920 { \
1921 const HChar* s = sV; \
1922 const HChar* reject = rejectV; \
1923 \
1924 /* find the length of 'reject', not including terminating zero */ \
1925 UWord nrej = 0; \
1926 while (reject[nrej]) nrej++; \
1927 \
1928 UWord len = 0; \
1929 while (1) { \
1930 UWord i; \
1931 HChar sc = *s; \
1932 if (sc == 0) \
1933 break; \
1934 for (i = 0; i < nrej; i++) { \
1935 if (sc == reject[i]) \
1936 break; \
1937 } \
1938 /* assert(i >= 0 && i <= nrej); */ \
1939 if (i < nrej) \
1940 break; \
1941 s++; \
1942 len++; \
1943 } \
1944 \
1945 return len; \
1946 }
1947
1948 #if defined(VGO_linux)
1949 STRCSPN(VG_Z_LIBC_SONAME, strcspn)
1950 STRCSPN(VG_Z_LIBC_SONAME, __GI_strcspn)
1951
1952 #elif defined(VGO_freebsd)
1953 STRCSPN(VG_Z_LIBC_SONAME, strcspn)
1954
1955 #elif defined(VGO_darwin)
1956
1957 #elif defined(VGO_solaris)
1958 STRCSPN(VG_Z_LIBC_SONAME, strcspn)
1959
1960 #endif
1961
1962
1963 /*---------------------- strspn ----------------------*/
1964
1965 #define STRSPN(soname, fnname) \
1966 SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \
1967 (const char* sV, const char* acceptV); \
1968 SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \
1969 (const char* sV, const char* acceptV) \
1970 { \
1971 const UChar* s = (const UChar *)sV; \
1972 const UChar* accept = (const UChar *)acceptV; \
1973 \
1974 /* find the length of 'accept', not including terminating zero */ \
1975 UWord nacc = 0; \
1976 while (accept[nacc]) nacc++; \
1977 if (nacc == 0) return 0; \
1978 \
1979 UWord len = 0; \
1980 while (1) { \
1981 UWord i; \
1982 UChar sc = *s; \
1983 if (sc == 0) \
1984 break; \
1985 for (i = 0; i < nacc; i++) { \
1986 if (sc == accept[i]) \
1987 break; \
1988 } \
1989 /* assert(i >= 0 && i <= nacc); */ \
1990 if (i == nacc) \
1991 break; \
1992 s++; \
1993 len++; \
1994 } \
1995 \
1996 return len; \
1997 }
1998
1999 #if defined(VGO_linux)
2000 STRSPN(VG_Z_LIBC_SONAME, strspn)
2001
2002 #elif defined(VGO_freebsd)
2003 STRSPN(VG_Z_LIBC_SONAME, strspn)
2004
2005 #elif defined(VGO_darwin)
2006
2007 #elif defined(VGO_solaris)
2008 STRSPN(VG_Z_LIBC_SONAME, strspn)
2009
2010 #endif
2011
2012
2013 /*---------------------- strcasestr ----------------------*/
2014
2015 #define STRCASESTR(soname, fnname) \
2016 char* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \
2017 (const char* haystack, const char* needle); \
2018 char* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \
2019 (const char* haystack, const char* needle) \
2020 { \
2021 extern int tolower(int); \
2022 const HChar* h = haystack; \
2023 const HChar* n = needle; \
2024 \
2025 /* find the length of n, not including terminating zero */ \
2026 UWord nlen = 0; \
2027 while (n[nlen]) nlen++; \
2028 \
2029 /* if n is the empty string, match immediately. */ \
2030 if (nlen == 0) return CONST_CAST(HChar *,h); \
2031 \
2032 /* assert(nlen >= 1); */ \
2033 UChar n0 = tolower(n[0]); \
2034 \
2035 while (1) { \
2036 UChar hh = tolower(*h); \
2037 if (hh == 0) return NULL; \
2038 if (hh != n0) { h++; continue; } \
2039 \
2040 UWord i; \
2041 for (i = 0; i < nlen; i++) { \
2042 if (tolower(n[i]) != tolower(h[i])) \
2043 break; \
2044 } \
2045 /* assert(i >= 0 && i <= nlen); */ \
2046 if (i == nlen) \
2047 return CONST_CAST(HChar *,h); \
2048 \
2049 h++; \
2050 } \
2051 }
2052
2053 #if defined(VGO_linux)
2054 # if !defined(VGPV_arm_linux_android) \
2055 && !defined(VGPV_x86_linux_android) \
2056 && !defined(VGPV_mips32_linux_android) \
2057 && !defined(VGPV_arm64_linux_android)
2058 STRCASESTR(VG_Z_LIBC_SONAME, strcasestr)
2059 # endif
2060
2061 #elif defined(VGO_freebsd)
2062 STRCASESTR(VG_Z_LIBC_SONAME, strcasestr)
2063
2064 #elif defined(VGO_darwin)
2065
2066 #elif defined(VGO_solaris)
2067 STRCASESTR(VG_Z_LIBC_SONAME, strcasestr)
2068
2069 #endif
2070
2071
2072 /*---------------------- wcslen ----------------------*/
2073
2074 // This is a wchar_t equivalent to strlen. Unfortunately
2075 // we don't have wchar_t available here, but it looks like
2076 // a 32 bit int on Linux. I don't know if that is also
2077 // valid on MacOSX.
2078
2079 #define WCSLEN(soname, fnname) \
2080 SizeT VG_REPLACE_FUNCTION_EZU(20370,soname,fnname) \
2081 ( const Int* str ); \
2082 SizeT VG_REPLACE_FUNCTION_EZU(20370,soname,fnname) \
2083 ( const Int* str ) \
2084 { \
2085 SizeT i = 0; \
2086 while (str[i] != 0) i++; \
2087 return i; \
2088 }
2089
2090 #if defined(VGO_linux) || defined(VGO_freebsd) || defined(VGO_solaris)
2091 WCSLEN(VG_Z_LIBC_SONAME, wcslen)
2092
2093 #endif
2094
2095 /*---------------------- wcsnlen ----------------------*/
2096
2097 #define WCSNLEN(soname, fnname) \
2098 SizeT VG_REPLACE_FUNCTION_EZU(20440,soname,fnname) \
2099 ( const Int *s, SizeT n ); \
2100 SizeT VG_REPLACE_FUNCTION_EZU(20440,soname,fnname) \
2101 ( const Int *s, SizeT n ) \
2102 { \
2103 SizeT i = 0; \
2104 const Int* p = s; \
2105 while (i < n && *p != 0) { \
2106 i++; \
2107 p++; \
2108 } \
2109 return i; \
2110 }
2111
2112 #if defined(VGO_linux) || defined(VGO_freebsd)
2113 WCSNLEN(VG_Z_LIBC_SONAME, wcsnlen)
2114 WCSNLEN(VG_Z_LIBC_SONAME, __GI_wcsnlen)
2115 #endif
2116
2117 /*---------------------- wcscmp ----------------------*/
2118
2119 // This is a wchar_t equivalent to strcmp. We don't
2120 // have wchar_t available here, but in the GNU C Library
2121 // wchar_t is always 32 bits wide and wcscmp uses signed
2122 // comparison, not unsigned as in strcmp function.
2123
2124 #define WCSCMP(soname, fnname) \
2125 int VG_REPLACE_FUNCTION_EZU(20380,soname,fnname) \
2126 ( const Int* s1, const Int* s2 ); \
2127 int VG_REPLACE_FUNCTION_EZU(20380,soname,fnname) \
2128 ( const Int* s1, const Int* s2 ) \
2129 { \
2130 register Int c1; \
2131 register Int c2; \
2132 while (True) { \
2133 c1 = *s1; \
2134 c2 = *s2; \
2135 if (c1 != c2) break; \
2136 if (c1 == 0) break; \
2137 s1++; s2++; \
2138 } \
2139 if (c1 < c2) return -1; \
2140 if (c1 > c2) return 1; \
2141 return 0; \
2142 }
2143
2144 #if defined(VGO_linux) || defined(VGO_freebsd)
2145 WCSCMP(VG_Z_LIBC_SONAME, wcscmp)
2146 #endif
2147
2148 /*---------------------- wcsncmp ----------------------*/
2149
2150 // This is a wchar_t equivalent to strncmp. We don't
2151 // have wchar_t available here, but in the GNU C Library
2152 // wchar_t is always 32 bits wide and wcsncmp uses signed
2153 // comparison, not unsigned as in strncmp function.
2154
2155 #define WCSNCMP(soname, fnname) \
2156 int VG_REPLACE_FUNCTION_EZU(20450,soname,fnname) \
2157 ( const Int* s1, const Int* s2, SizeT nmax ); \
2158 int VG_REPLACE_FUNCTION_EZU(20450,soname,fnname) \
2159 ( const Int* s1, const Int* s2, SizeT nmax ) \
2160 { \
2161 SizeT n = 0; \
2162 while (True) { \
2163 if (n >= nmax) return 0; \
2164 if (*s1 == 0 && *s2 == 0) return 0; \
2165 if (*s1 == 0) return -1; \
2166 if (*s2 == 0) return 1; \
2167 \
2168 if (*s1 < *s2) return -1; \
2169 if (*s1 > *s2) return 1; \
2170 \
2171 s1++; s2++; n++; \
2172 } \
2173 }
2174 #if defined(VGO_linux) || defined(VGO_freebsd)
2175 WCSNCMP(VG_Z_LIBC_SONAME, wcsncmp)
2176 #endif
2177
2178 /*---------------------- wcscpy ----------------------*/
2179
2180 // This is a wchar_t equivalent to strcpy. We don't
2181 // have wchar_t available here, but in the GNU C Library
2182 // wchar_t is always 32 bits wide.
2183
2184 #define WCSCPY(soname, fnname) \
2185 Int* VG_REPLACE_FUNCTION_EZU(20390,soname,fnname) \
2186 ( Int* dst, const Int* src ); \
2187 Int* VG_REPLACE_FUNCTION_EZU(20390,soname,fnname) \
2188 ( Int* dst, const Int* src ) \
2189 { \
2190 const Int* src_orig = src; \
2191 Int* dst_orig = dst; \
2192 \
2193 while (*src) *dst++ = *src++; \
2194 *dst = 0; \
2195 \
2196 /* This checks for overlap after copying, unavoidable without */ \
2197 /* pre-counting length... should be ok */ \
2198 /* +4 because sizeof(wchar_t) == 4 */ \
2199 SizeT srclen = (Addr)src-(Addr)src_orig+4; \
2200 RECORD_COPY(srclen); \
2201 if (is_overlap(dst_orig, \
2202 src_orig, \
2203 /* +4 because sizeof(wchar_t) == 4 */ \
2204 (Addr)dst-(Addr)dst_orig+4, \
2205 srclen)) \
2206 RECORD_OVERLAP_ERROR("wcscpy", dst_orig, src_orig, 0); \
2207 \
2208 return dst_orig; \
2209 }
2210
2211 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
2212 WCSCPY(VG_Z_LIBC_SONAME, wcscpy)
2213 #endif
2214
2215
2216 /*---------------------- wcschr ----------------------*/
2217
2218 // This is a wchar_t equivalent to strchr. We don't
2219 // have wchar_t available here, but in the GNU C Library
2220 // wchar_t is always 32 bits wide.
2221
2222 #define WCSCHR(soname, fnname) \
2223 Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ); \
2224 Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ) \
2225 { \
2226 const Int* p = s; \
2227 while (True) { \
2228 if (*p == c) return CONST_CAST(Int *,p); \
2229 if (*p == 0) return NULL; \
2230 p++; \
2231 } \
2232 }
2233
2234 #if defined(VGO_linux) || defined(VGO_freebsd)
2235 WCSCHR(VG_Z_LIBC_SONAME, wcschr)
2236 #endif
2237 /*---------------------- wcsrchr ----------------------*/
2238
2239 // This is a wchar_t equivalent to strrchr. We don't
2240 // have wchar_t available here, but in the GNU C Library
2241 // wchar_t is always 32 bits wide.
2242
2243 #define WCSRCHR(soname, fnname) \
2244 Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ); \
2245 Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ) \
2246 { \
2247 const Int* p = s; \
2248 const Int* last = NULL; \
2249 while (True) { \
2250 if (*p == c) last = p; \
2251 if (*p == 0) return CONST_CAST(Int *,last); \
2252 p++; \
2253 } \
2254 }
2255
2256 #if defined(VGO_linux) || defined(VGO_freebsd)
2257 WCSRCHR(VG_Z_LIBC_SONAME, wcsrchr)
2258 #endif
2259
2260 /*---------------------- wmemchr ----------------------*/
2261
2262 // This is a wchar_t equivalent to memchr. We don't
2263 // have wchar_t available here, but in the GNU C Library
2264 // wchar_t is always 32 bits wide.
2265
2266 #define WMEMCHR(soname, fnname) \
2267 Int* VG_REPLACE_FUNCTION_EZU(20430,soname,fnname) \
2268 (const Int *s, Int c, SizeT n); \
2269 Int* VG_REPLACE_FUNCTION_EZU(20430,soname,fnname) \
2270 (const Int *s, Int c, SizeT n) \
2271 { \
2272 SizeT i; \
2273 const Int* p = s; \
2274 for (i = 0; i < n; i++) { \
2275 if (*p == c) return CONST_CAST(Int *,p); \
2276 p++; \
2277 } \
2278 return NULL; \
2279 }
2280
2281 #if defined(VGO_linux)
2282 WMEMCHR(VG_Z_LIBC_SONAME, wmemchr)
2283 WMEMCHR(VG_Z_LIBC_SONAME, __GI_wmemchr)
2284 #endif
2285
2286 #if defined(VGO_freebsd)
2287 WMEMCHR(VG_Z_LIBC_SONAME, wmemchr)
2288 #endif
2289
2290 /*---------------------- wmemcmp ----------------------*/
2291
2292 #define WMEMCMP(soname, fnname) \
2293 int VG_REPLACE_FUNCTION_EZU(20470,soname,fnname) \
2294 ( const Int *b1, const Int *b2, SizeT n ); \
2295 int VG_REPLACE_FUNCTION_EZU(20470,soname,fnname) \
2296 ( const Int *b1, const Int *b2, SizeT n ) \
2297 { \
2298 for (SizeT i = 0U; i < n; ++i) { \
2299 if (b1[i] != b2[i]) \
2300 return b1[i] > b2[i] ? 1 : -1; \
2301 } \
2302 return 0; \
2303 }
2304
2305 #if defined(VGO_linux) || defined(VGO_freebsd)
2306 WMEMCMP(VG_Z_LIBC_SONAME, wmemcmp)
2307 #endif
2308
2309 /*---------------------- wcsncpy ----------------------*/
2310
2311 // This is a wchar_t equivalent to strncpy. We don't
2312 // have wchar_t available here, but in the GNU C Library
2313 // wchar_t is always 32 bits wide.
2314
2315 #define WCSNCPY(soname, fnname) \
2316 Int* VG_REPLACE_FUNCTION_EZU(20480,soname,fnname) \
2317 ( Int* dst, const Int* src, SizeT n ); \
2318 Int* VG_REPLACE_FUNCTION_EZU(20480,soname,fnname) \
2319 ( Int* dst, const Int* src, SizeT n ) \
2320 { \
2321 const Int* src_orig = src; \
2322 Int* dst_orig = dst; \
2323 SizeT m = 0; \
2324 \
2325 while (m < n && *src) { \
2326 m++; \
2327 *dst++ = *src++; \
2328 } \
2329 \
2330 /* This checks for overlap after copying, unavoidable without */ \
2331 /* pre-counting length... should be ok */ \
2332 /* *4 because sizeof(wchar_t) == 4 */ \
2333 SizeT srclen = ((m < n) ? m+1 : n)*4; \
2334 RECORD_COPY(srclen); \
2335 if (is_overlap(dst_orig, \
2336 src_orig, \
2337 n*4, \
2338 srclen)) \
2339 RECORD_OVERLAP_ERROR("wcsncpy", dst_orig, src_orig, 0); \
2340 \
2341 while (m++ < n) { \
2342 *dst++ = 0; \
2343 } \
2344 \
2345 return dst_orig; \
2346 }
2347
2348 #if defined(VGO_linux) || defined(VGO_freebsd)
2349 WCSNCPY(VG_Z_LIBC_SONAME, wcsncpy)
2350 #endif
2351
2352 /*---------------------- memccpy ----------------------*/
2353
2354 /* memccpy, mostly based on GNU libc source */
2355 #define MEMCCPY(soname, fnname) \
2356 void* VG_REPLACE_FUNCTION_EZU(20490,soname,fnname) \
2357 ( void *dst, const void *src, Int c, SizeT len ); \
2358 void* VG_REPLACE_FUNCTION_EZU(20490,soname,fnname) \
2359 ( void *dst, const void *src, Int c, SizeT len ) \
2360 { \
2361 const char *s = src; \
2362 char *d = dst; \
2363 const char x = c; \
2364 SizeT i = len; \
2365 \
2366 while (i-- > 0) \
2367 if ((*d++ = *s++) == x) { \
2368 SizeT srclen = len - i; \
2369 RECORD_COPY(srclen); \
2370 if (is_overlap(dst, src, len, srclen)) \
2371 RECORD_OVERLAP_ERROR("memccpy", dst, src, len); \
2372 return d; \
2373 } \
2374 \
2375 if (len) { \
2376 RECORD_COPY(len); \
2377 if (is_overlap(dst, src, len, len)) \
2378 RECORD_OVERLAP_ERROR("memccpy", dst, src, len); \
2379 } \
2380 return NULL; \
2381 }
2382
2383 #if defined(VGO_linux) || defined(VGO_freebsd) || defined(VGO_darwin) || defined(VGO_solaris)
2384 MEMCCPY(VG_Z_LIBC_SONAME, memccpy)
2385 #endif
2386
2387 /*---------------------- wcpncpy ----------------------*/
2388
2389 // This is a wchar_t equivalent to strncpy. We don't
2390 // have wchar_t available here, but in the GNU C Library
2391 // wchar_t is always 32 bits wide.
2392
2393 #define WCPNCPY(soname, fnname) \
2394 Int* VG_REPLACE_FUNCTION_EZU(20500,soname,fnname) \
2395 ( Int* dst, const Int* src, SizeT n ); \
2396 Int* VG_REPLACE_FUNCTION_EZU(20500,soname,fnname) \
2397 ( Int* dst, const Int* src, SizeT n ) \
2398 { \
2399 const Int* src_orig = src; \
2400 Int* dst_orig = dst; \
2401 SizeT m = 0; \
2402 \
2403 while (m < n && *src) { \
2404 m++; \
2405 *dst++ = *src++; \
2406 } \
2407 \
2408 /* This checks for overlap after copying, unavoidable without */ \
2409 /* pre-counting length... should be ok */ \
2410 /* *4 because sizeof(wchar_t) == 4 */ \
2411 SizeT srclen = ((m < n) ? m+1 : n)*4; \
2412 RECORD_COPY(srclen); \
2413 if (is_overlap(dst_orig, \
2414 src_orig, \
2415 n*4, \
2416 srclen)) \
2417 RECORD_OVERLAP_ERROR("wcpncpy", dst_orig, src_orig, 0); \
2418 \
2419 while (m++ < n) { \
2420 *dst++ = 0; \
2421 } \
2422 \
2423 return dst_orig + (src - src_orig); \
2424 }
2425
2426 #if defined(VGO_linux) || defined(VGO_freebsd) || defined(VGO_solaris)
2427 WCPNCPY(VG_Z_LIBC_SONAME, wcpncpy)
2428 #endif
2429
2430 /*----------------------- wcscat ----------------------*/
2431
2432 #define WCSCAT(soname, fnname) \
2433 Int* VG_REPLACE_FUNCTION_EZU(20510,soname,fnname) \
2434 ( Int *restrict dest, const Int *restrict src ); \
2435 Int* VG_REPLACE_FUNCTION_EZU(20510,soname,fnname) \
2436 ( Int *restrict dest, const Int *restrict src ) \
2437 { \
2438 const Int* src_orig = src; \
2439 Int* dest_orig = dest; \
2440 while (*dest) dest++; \
2441 while (*src) *dest++ = *src++; \
2442 *dest = 0; \
2443 \
2444 /* This is a bit redundant, I think; any overlap and the wcscat will */ \
2445 /* go forever... or until a seg fault occurs. */ \
2446 if (is_overlap(dest_orig, \
2447 src_orig, \
2448 (Addr)dest-(Addr)dest_orig+1, \
2449 (Addr)src-(Addr)src_orig+1)) \
2450 RECORD_OVERLAP_ERROR("wcscat", dest_orig, src_orig, 0); \
2451 \
2452 return dest_orig; \
2453 }
2454
2455 #if defined(VGO_linux)
2456 WCSCAT(VG_Z_LIBC_SONAME, __wcscat_avx2)
2457 #endif
2458
2459 /*------------------------------------------------------------*/
2460 /*--- Improve definedness checking of process environment ---*/
2461 /*------------------------------------------------------------*/
2462
2463 #if defined(VGO_linux) || defined(VGO_freebsd)
2464
2465 /* If these wind up getting generated via a macro, so that multiple
2466 versions of each function exist (as above), use the _EZU variants
2467 to assign equivalance class tags. */
2468
2469 /*---------------------- putenv ----------------------*/
2470
2471 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string);
2472 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string)
2473 {
2474 OrigFn fn;
2475 Word result;
2476 const HChar* p = string;
2477 VALGRIND_GET_ORIG_FN(fn);
2478 /* Now by walking over the string we magically produce
2479 traces when hitting undefined memory. */
2480 if (p)
2481 while (*p++)
2482 __asm__ __volatile__("" ::: "memory");
2483 CALL_FN_W_W(result, fn, string);
2484 return result;
2485 }
2486
2487
2488 /*---------------------- unsetenv ----------------------*/
2489
2490 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name);
2491 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name)
2492 {
2493 OrigFn fn;
2494 Word result;
2495 const HChar* p = name;
2496 VALGRIND_GET_ORIG_FN(fn);
2497 /* Now by walking over the string we magically produce
2498 traces when hitting undefined memory. */
2499 if (p)
2500 while (*p++)
2501 __asm__ __volatile__("" ::: "memory");
2502 CALL_FN_W_W(result, fn, name);
2503 return result;
2504 }
2505
2506
2507 /*---------------------- setenv ----------------------*/
2508
2509 /* setenv */
2510 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv)
2511 (const char* name, const char* value, int overwrite);
2512 int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv)
2513 (const char* name, const char* value, int overwrite)
2514 {
2515 OrigFn fn;
2516 Word result;
2517 const HChar* p;
2518 VALGRIND_GET_ORIG_FN(fn);
2519 /* Now by walking over the string we magically produce
2520 traces when hitting undefined memory. */
2521 if (name)
2522 for (p = name; *p; p++)
2523 __asm__ __volatile__("" ::: "memory");
2524 if (value)
2525 for (p = value; *p; p++)
2526 __asm__ __volatile__("" ::: "memory");
2527 (void) VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite);
2528 CALL_FN_W_WWW(result, fn, name, value, overwrite);
2529 return result;
2530 }
2531
2532 #endif /* defined(VGO_linux) */
2533
2534 /*--------------------------------------------------------------------*/
2535 /*--- end ---*/
2536 /*--------------------------------------------------------------------*/