| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Replacements for strcpy(), memcpy() et al, which run on the ---*/ |
| 4 | /*--- simulated CPU. ---*/ |
| njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5 | /*--- mc_replace_strmem.c ---*/ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
| nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 9 | This file is part of MemCheck, a heavyweight Valgrind tool for |
| njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 10 | detecting memory errors. |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 11 | |
| sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2008 Julian Seward |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
| njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_tool_basics.h" |
| njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 34 | #include "pub_tool_hashtable.h" |
| njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 35 | #include "pub_tool_redir.h" |
| 36 | #include "pub_tool_tooliface.h" |
| 37 | #include "valgrind.h" |
| 38 | |
| njn | 34419c1 | 2003-05-02 17:24:29 +0000 | [diff] [blame] | 39 | #include "mc_include.h" |
| fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 40 | #include "memcheck.h" |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 41 | |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 42 | /* --------------------------------------------------------------------- |
| njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 43 | We have our own versions of these functions for two reasons: |
| 44 | (a) it allows us to do overlap checking |
| 45 | (b) some of the normal versions are hyper-optimised, which fools |
| 46 | Memcheck and cause spurious value warnings. Our versions are |
| 47 | simpler. |
| 48 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 49 | Note that overenthusiastic use of PLT bypassing by the glibc people also |
| 50 | means that we need to patch multiple versions of some of the functions to |
| 51 | our own implementations. |
| 52 | |
| njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 53 | THEY RUN ON THE SIMD CPU! |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 54 | ------------------------------------------------------------------ */ |
| 55 | |
| sewardj | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 56 | /* Figure out if [dst .. dst+dstlen-1] overlaps with |
| 57 | [src .. src+srclen-1]. |
| 58 | We assume that the address ranges do not wrap around |
| 59 | (which is safe since on Linux addresses >= 0xC0000000 |
| 60 | are not accessible and the program will segfault in this |
| 61 | circumstance, presumably). |
| 62 | */ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 63 | static __inline__ |
| njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 64 | Bool is_overlap ( void* dst, const void* src, SizeT dstlen, SizeT srclen ) |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 65 | { |
| sewardj | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 66 | Addr loS, hiS, loD, hiD; |
| 67 | |
| 68 | if (dstlen == 0 || srclen == 0) |
| 69 | return False; |
| 70 | |
| 71 | loS = (Addr)src; |
| 72 | loD = (Addr)dst; |
| 73 | hiS = loS + srclen - 1; |
| 74 | hiD = loD + dstlen - 1; |
| 75 | |
| 76 | /* So figure out if [loS .. hiS] overlaps with [loD .. hiD]. */ |
| 77 | if (loS < loD) { |
| 78 | return !(hiS < loD); |
| 79 | } |
| 80 | else if (loD < loS) { |
| 81 | return !(hiD < loS); |
| 82 | } |
| 83 | else { |
| 84 | /* They start at same place. Since we know neither of them has |
| 85 | zero length, they must overlap. */ |
| 86 | return True; |
| 87 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 90 | // This is a macro rather than a function because we don't want to have an |
| 91 | // extra function in the stack trace. |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 92 | #define RECORD_OVERLAP_ERROR(s, src, dst, len) \ |
| njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 93 | { \ |
| 94 | Word unused_res; \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 95 | VALGRIND_DO_CLIENT_REQUEST(unused_res, 0, \ |
| 96 | _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR, \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 97 | s, src, dst, len, 0); \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 100 | |
| 101 | #define STRRCHR(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 102 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ); \ |
| 103 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 104 | { \ |
| 105 | UChar ch = (UChar)((UInt)c); \ |
| 106 | UChar* p = (UChar*)s; \ |
| 107 | UChar* last = NULL; \ |
| 108 | while (True) { \ |
| 109 | if (*p == ch) last = p; \ |
| 110 | if (*p == 0) return last; \ |
| 111 | p++; \ |
| 112 | } \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 113 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 114 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 115 | // Apparently rindex() is the same thing as strrchr() |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 116 | STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
| 117 | STRRCHR(VG_Z_LIBC_SONAME, rindex) |
| 118 | STRRCHR(VG_Z_LD_LINUX_SO_2, rindex) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | #define STRCHR(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 122 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ); \ |
| 123 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 124 | { \ |
| 125 | UChar ch = (UChar)((UInt)c); \ |
| 126 | UChar* p = (UChar*)s; \ |
| 127 | while (True) { \ |
| 128 | if (*p == ch) return p; \ |
| 129 | if (*p == 0) return NULL; \ |
| 130 | p++; \ |
| 131 | } \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 132 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 133 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 134 | // Apparently index() is the same thing as strchr() |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 135 | STRCHR(VG_Z_LIBC_SONAME, strchr) |
| 136 | STRCHR(VG_Z_LD_LINUX_SO_2, strchr) |
| 137 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr) |
| 138 | STRCHR(VG_Z_LIBC_SONAME, index) |
| 139 | STRCHR(VG_Z_LD_LINUX_SO_2, index) |
| 140 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index) |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 141 | |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 142 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 143 | #define STRCAT(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 144 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \ |
| 145 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 146 | { \ |
| 147 | const Char* src_orig = src; \ |
| 148 | Char* dst_orig = dst; \ |
| 149 | while (*dst) dst++; \ |
| 150 | while (*src) *dst++ = *src++; \ |
| 151 | *dst = 0; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 152 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 153 | /* This is a bit redundant, I think; any overlap and the strcat will */ \ |
| 154 | /* go forever... or until a seg fault occurs. */ \ |
| 155 | if (is_overlap(dst_orig, \ |
| 156 | src_orig, \ |
| 157 | (Addr)dst-(Addr)dst_orig+1, \ |
| 158 | (Addr)src-(Addr)src_orig+1)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 159 | RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 160 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 161 | return dst_orig; \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 162 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 163 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 164 | STRCAT(VG_Z_LIBC_SONAME, strcat) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | #define STRNCAT(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 168 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 169 | ( char* dst, const char* src, SizeT n ); \ |
| 170 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 171 | ( char* dst, const char* src, SizeT n ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 172 | { \ |
| 173 | const Char* src_orig = src; \ |
| 174 | Char* dst_orig = dst; \ |
| 175 | SizeT m = 0; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 176 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 177 | while (*dst) dst++; \ |
| 178 | while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \ |
| 179 | *dst = 0; /* always add null */ \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 180 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 181 | /* This checks for overlap after copying, unavoidable without */ \ |
| 182 | /* pre-counting lengths... should be ok */ \ |
| 183 | if (is_overlap(dst_orig, \ |
| 184 | src_orig, \ |
| 185 | (Addr)dst-(Addr)dst_orig+1, \ |
| 186 | (Addr)src-(Addr)src_orig+1)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 187 | RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 188 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 189 | return dst_orig; \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 190 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 191 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 192 | STRNCAT(VG_Z_LIBC_SONAME, strncat) |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 193 | |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 194 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 195 | #define STRNLEN(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 196 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ); \ |
| 197 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 198 | { \ |
| 199 | SizeT i = 0; \ |
| 200 | while (i < n && str[i] != 0) i++; \ |
| 201 | return i; \ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 202 | } |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 203 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 204 | STRNLEN(VG_Z_LIBC_SONAME, strnlen) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 205 | |
| sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 206 | |
| njn | 5ec15ed | 2005-08-24 19:55:51 +0000 | [diff] [blame] | 207 | // Note that this replacement often doesn't get used because gcc inlines |
| 208 | // calls to strlen() with its own built-in version. This can be very |
| 209 | // confusing if you aren't expecting it. Other small functions in this file |
| 210 | // may also be inline by gcc. |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 211 | #define STRLEN(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 212 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ); \ |
| 213 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 214 | { \ |
| 215 | SizeT i = 0; \ |
| 216 | while (str[i] != 0) i++; \ |
| 217 | return i; \ |
| sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 218 | } |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 219 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 220 | STRLEN(VG_Z_LIBC_SONAME, strlen) |
| 221 | STRLEN(VG_Z_LD_LINUX_SO_2, strlen) |
| 222 | STRLEN(VG_Z_LD_LINUX_X86_64_SO_2, strlen) |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 223 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 224 | |
| 225 | #define STRCPY(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 226 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ); \ |
| 227 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 228 | { \ |
| 229 | const Char* src_orig = src; \ |
| 230 | Char* dst_orig = dst; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 231 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 232 | while (*src) *dst++ = *src++; \ |
| 233 | *dst = 0; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 234 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 235 | /* This checks for overlap after copying, unavoidable without */ \ |
| 236 | /* pre-counting length... should be ok */ \ |
| 237 | if (is_overlap(dst_orig, \ |
| 238 | src_orig, \ |
| 239 | (Addr)dst-(Addr)dst_orig+1, \ |
| 240 | (Addr)src-(Addr)src_orig+1)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 241 | RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 242 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 243 | return dst_orig; \ |
| 244 | } |
| 245 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 246 | STRCPY(VG_Z_LIBC_SONAME, strcpy) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | #define STRNCPY(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 250 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 251 | ( char* dst, const char* src, SizeT n ); \ |
| 252 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 253 | ( char* dst, const char* src, SizeT n ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 254 | { \ |
| 255 | const Char* src_orig = src; \ |
| 256 | Char* dst_orig = dst; \ |
| 257 | SizeT m = 0; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 258 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 259 | while (m < n && *src) { m++; *dst++ = *src++; } \ |
| 260 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 261 | /* but only m+1 bytes of src if terminator was found */ \ |
| 262 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 263 | RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 264 | while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \ |
| 265 | \ |
| 266 | return dst_orig; \ |
| 267 | } |
| 268 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 269 | STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | #define STRNCMP(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 273 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 274 | ( const char* s1, const char* s2, SizeT nmax ); \ |
| 275 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 276 | ( const char* s1, const char* s2, SizeT nmax ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 277 | { \ |
| 278 | SizeT n = 0; \ |
| 279 | while (True) { \ |
| 280 | if (n >= nmax) return 0; \ |
| 281 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 282 | if (*s1 == 0) return -1; \ |
| 283 | if (*s2 == 0) return 1; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 284 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 285 | if (*(unsigned char*)s1 < *(unsigned char*)s2) return -1; \ |
| 286 | if (*(unsigned char*)s1 > *(unsigned char*)s2) return 1; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 287 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 288 | s1++; s2++; n++; \ |
| 289 | } \ |
| 290 | } |
| 291 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 292 | STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | #define STRCMP(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 296 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 297 | ( const char* s1, const char* s2 ); \ |
| 298 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 299 | ( const char* s1, const char* s2 ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 300 | { \ |
| 301 | register unsigned char c1; \ |
| 302 | register unsigned char c2; \ |
| 303 | while (True) { \ |
| 304 | c1 = *(unsigned char *)s1; \ |
| 305 | c2 = *(unsigned char *)s2; \ |
| 306 | if (c1 != c2) break; \ |
| 307 | if (c1 == 0) break; \ |
| 308 | s1++; s2++; \ |
| 309 | } \ |
| 310 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 311 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 312 | return 0; \ |
| 313 | } |
| 314 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 315 | STRCMP(VG_Z_LIBC_SONAME, strcmp) |
| 316 | STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp) |
| 317 | STRCMP(VG_Z_LD64_SO_1, strcmp) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 318 | |
| 319 | |
| 320 | #define MEMCHR(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 321 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void *s, int c, SizeT n); \ |
| 322 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void *s, int c, SizeT n) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 323 | { \ |
| 324 | SizeT i; \ |
| 325 | UChar c0 = (UChar)c; \ |
| 326 | UChar* p = (UChar*)s; \ |
| 327 | for (i = 0; i < n; i++) \ |
| 328 | if (p[i] == c0) return (void*)(&p[i]); \ |
| 329 | return NULL; \ |
| 330 | } |
| 331 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 332 | MEMCHR(VG_Z_LIBC_SONAME, memchr) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | #define MEMCPY(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 336 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 337 | ( void *dst, const void *src, SizeT len ); \ |
| 338 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 339 | ( void *dst, const void *src, SizeT len ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 340 | { \ |
| 341 | register char *d; \ |
| 342 | register char *s; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 343 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 344 | if (len == 0) \ |
| 345 | return dst; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 346 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 347 | if (is_overlap(dst, src, len, len)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 348 | RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 349 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 350 | if ( dst > src ) { \ |
| 351 | d = (char *)dst + len - 1; \ |
| 352 | s = (char *)src + len - 1; \ |
| 353 | while ( len >= 4 ) { \ |
| 354 | *d-- = *s--; \ |
| 355 | *d-- = *s--; \ |
| 356 | *d-- = *s--; \ |
| 357 | *d-- = *s--; \ |
| 358 | len -= 4; \ |
| 359 | } \ |
| 360 | while ( len-- ) { \ |
| 361 | *d-- = *s--; \ |
| 362 | } \ |
| 363 | } else if ( dst < src ) { \ |
| 364 | d = (char *)dst; \ |
| 365 | s = (char *)src; \ |
| 366 | while ( len >= 4 ) { \ |
| 367 | *d++ = *s++; \ |
| 368 | *d++ = *s++; \ |
| 369 | *d++ = *s++; \ |
| 370 | *d++ = *s++; \ |
| 371 | len -= 4; \ |
| 372 | } \ |
| 373 | while ( len-- ) { \ |
| 374 | *d++ = *s++; \ |
| 375 | } \ |
| 376 | } \ |
| 377 | return dst; \ |
| 378 | } |
| 379 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 380 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) |
| 381 | MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */ |
| 382 | MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */ |
| sewardj | f0b3432 | 2007-01-16 21:42:28 +0000 | [diff] [blame] | 383 | /* icc9 blats these around all over the place. Not only in the main |
| 384 | executable but various .so's. They are highly tuned and read |
| 385 | memory beyond the source boundary (although work correctly and |
| 386 | never go across page boundaries), so give errors when run natively, |
| 387 | at least for misaligned source arg. Just intercepting in the exe |
| 388 | only until we understand more about the problem. See |
| 389 | http://bugs.kde.org/show_bug.cgi?id=139776 |
| 390 | */ |
| 391 | MEMCPY(NONE, _intel_fast_memcpy) |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 392 | |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 393 | |
| 394 | #define MEMCMP(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 395 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 396 | ( const void *s1V, const void *s2V, SizeT n ); \ |
| 397 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 398 | ( const void *s1V, const void *s2V, SizeT n ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 399 | { \ |
| 400 | int res; \ |
| 401 | unsigned char a0; \ |
| 402 | unsigned char b0; \ |
| 403 | unsigned char* s1 = (unsigned char*)s1V; \ |
| 404 | unsigned char* s2 = (unsigned char*)s2V; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 405 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 406 | while (n != 0) { \ |
| 407 | a0 = s1[0]; \ |
| 408 | b0 = s2[0]; \ |
| 409 | s1 += 1; \ |
| 410 | s2 += 1; \ |
| 411 | res = ((int)a0) - ((int)b0); \ |
| 412 | if (res != 0) \ |
| 413 | return res; \ |
| 414 | n -= 1; \ |
| 415 | } \ |
| 416 | return 0; \ |
| 417 | } |
| 418 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 419 | MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 420 | MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
| 421 | MEMCMP(VG_Z_LD_SO_1, bcmp) |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 422 | |
| jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 423 | |
| 424 | /* Copy SRC to DEST, returning the address of the terminating '\0' in |
| 425 | DEST. (minor variant of strcpy) */ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 426 | #define STPCPY(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 427 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \ |
| 428 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 429 | { \ |
| 430 | const Char* src_orig = src; \ |
| 431 | Char* dst_orig = dst; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 432 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 433 | while (*src) *dst++ = *src++; \ |
| 434 | *dst = 0; \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 435 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 436 | /* This checks for overlap after copying, unavoidable without */ \ |
| 437 | /* pre-counting length... should be ok */ \ |
| 438 | if (is_overlap(dst_orig, \ |
| 439 | src_orig, \ |
| 440 | (Addr)dst-(Addr)dst_orig+1, \ |
| 441 | (Addr)src-(Addr)src_orig+1)) \ |
| njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 442 | RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \ |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 443 | \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 444 | return dst; \ |
| sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 445 | } |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 446 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 447 | STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
| 448 | STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy) |
| 449 | STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 450 | |
| 451 | |
| 452 | #define MEMSET(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 453 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname)(void *s, Int c, SizeT n); \ |
| 454 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname)(void *s, Int c, SizeT n) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 455 | { \ |
| 456 | unsigned char *cp = s; \ |
| sewardj | 487cadb | 2007-08-25 23:25:00 +0000 | [diff] [blame] | 457 | while (n >= 4) { \ |
| 458 | cp[0] = c; \ |
| 459 | cp[1] = c; \ |
| 460 | cp[2] = c; \ |
| 461 | cp[3] = c; \ |
| 462 | cp += 4; \ |
| 463 | n -= 4; \ |
| 464 | } \ |
| 465 | while (n--) { \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 466 | *cp++ = c; \ |
| sewardj | 487cadb | 2007-08-25 23:25:00 +0000 | [diff] [blame] | 467 | } \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 468 | return s; \ |
| sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 469 | } |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 470 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 471 | MEMSET(VG_Z_LIBC_SONAME, memset) |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | #define MEMMOVE(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 475 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 476 | (void *dstV, const void *srcV, SizeT n); \ |
| 477 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 478 | (void *dstV, const void *srcV, SizeT n) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 479 | { \ |
| 480 | SizeT i; \ |
| 481 | Char* dst = (Char*)dstV; \ |
| 482 | Char* src = (Char*)srcV; \ |
| 483 | if (dst < src) { \ |
| 484 | for (i = 0; i < n; i++) \ |
| 485 | dst[i] = src[i]; \ |
| 486 | } \ |
| 487 | else \ |
| 488 | if (dst > src) { \ |
| 489 | for (i = 0; i < n; i++) \ |
| 490 | dst[n-i-1] = src[n-i-1]; \ |
| 491 | } \ |
| 492 | return dst; \ |
| 493 | } |
| 494 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 495 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
| sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 496 | |
| jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 497 | |
| sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 498 | /* glibc 2.5 variant of memmove which checks the dest is big enough. |
| 499 | There is no specific part of glibc that this is copied from. */ |
| 500 | #define GLIBC25___MEMMOVE_CHK(soname, fnname) \ |
| 501 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 502 | (void *dstV, const void *srcV, SizeT n, SizeT destlen); \ |
| 503 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 504 | (void *dstV, const void *srcV, SizeT n, SizeT destlen) \ |
| 505 | { \ |
| 506 | extern void _exit(int status); \ |
| 507 | SizeT i; \ |
| 508 | Char* dst = (Char*)dstV; \ |
| 509 | Char* src = (Char*)srcV; \ |
| 510 | if (destlen < n) \ |
| 511 | goto badness; \ |
| 512 | if (dst < src) { \ |
| 513 | for (i = 0; i < n; i++) \ |
| 514 | dst[i] = src[i]; \ |
| 515 | } \ |
| 516 | else \ |
| 517 | if (dst > src) { \ |
| 518 | for (i = 0; i < n; i++) \ |
| 519 | dst[n-i-1] = src[n-i-1]; \ |
| 520 | } \ |
| 521 | return dst; \ |
| 522 | badness: \ |
| 523 | VALGRIND_PRINTF_BACKTRACE( \ |
| 524 | "*** memmove_chk: buffer overflow detected ***: " \ |
| 525 | "program terminated"); \ |
| 526 | _exit(127); \ |
| sewardj | c271ec8 | 2007-02-27 22:36:14 +0000 | [diff] [blame] | 527 | /*NOTREACHED*/ \ |
| 528 | return NULL; \ |
| sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 531 | GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk) |
| sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 532 | |
| 533 | |
| sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 534 | /* Find the first occurrence of C in S or the final NUL byte. */ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 535 | #define GLIBC232_STRCHRNUL(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 536 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in); \ |
| 537 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 538 | { \ |
| 539 | unsigned char c = (unsigned char) c_in; \ |
| 540 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 541 | while (1) { \ |
| 542 | if (*char_ptr == 0) return char_ptr; \ |
| 543 | if (*char_ptr == c) return char_ptr; \ |
| 544 | char_ptr++; \ |
| 545 | } \ |
| sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 546 | } |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 547 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 548 | GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul) |
| sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 549 | |
| 550 | |
| 551 | /* Find the first occurrence of C in S. */ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 552 | #define GLIBC232_RAWMEMCHR(soname, fnname) \ |
| sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 553 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in); \ |
| 554 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in) \ |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 555 | { \ |
| 556 | unsigned char c = (unsigned char) c_in; \ |
| 557 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 558 | while (1) { \ |
| 559 | if (*char_ptr == c) return char_ptr; \ |
| 560 | char_ptr++; \ |
| 561 | } \ |
| sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 562 | } |
| njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 563 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 564 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr) |
| sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 565 | |
| 566 | |
| sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 567 | /* glibc variant of strcpy that checks the dest is big enough. |
| 568 | Copied from glibc-2.5/debug/test-strcpy_chk.c. */ |
| sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 569 | #define GLIBC25___STRCPY_CHK(soname,fnname) \ |
| 570 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 571 | (char* dst, const char* src, SizeT len); \ |
| 572 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 573 | (char* dst, const char* src, SizeT len) \ |
| 574 | { \ |
| 575 | extern void _exit(int status); \ |
| 576 | char* ret = dst; \ |
| 577 | if (! len) \ |
| 578 | goto badness; \ |
| 579 | while ((*dst++ = *src++) != '\0') \ |
| 580 | if (--len == 0) \ |
| 581 | goto badness; \ |
| 582 | return ret; \ |
| 583 | badness: \ |
| 584 | VALGRIND_PRINTF_BACKTRACE( \ |
| sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 585 | "*** strcpy_chk: buffer overflow detected ***: " \ |
| 586 | "program terminated"); \ |
| sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 587 | _exit(127); \ |
| 588 | /*NOTREACHED*/ \ |
| 589 | return NULL; \ |
| 590 | } |
| 591 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 592 | GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk) |
| sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 593 | |
| 594 | |
| sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 595 | /* glibc variant of stpcpy that checks the dest is big enough. |
| 596 | Copied from glibc-2.5/debug/test-stpcpy_chk.c. */ |
| sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 597 | #define GLIBC25___STPCPY_CHK(soname,fnname) \ |
| 598 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 599 | (char* dst, const char* src, SizeT len); \ |
| 600 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 601 | (char* dst, const char* src, SizeT len) \ |
| 602 | { \ |
| 603 | extern void _exit(int status); \ |
| sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 604 | if (! len) \ |
| 605 | goto badness; \ |
| 606 | while ((*dst++ = *src++) != '\0') \ |
| 607 | if (--len == 0) \ |
| sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 608 | goto badness; \ |
| sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 609 | return dst - 1; \ |
| 610 | badness: \ |
| 611 | VALGRIND_PRINTF_BACKTRACE( \ |
| sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 612 | "*** stpcpy_chk: buffer overflow detected ***: " \ |
| 613 | "program terminated"); \ |
| sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 614 | _exit(127); \ |
| 615 | /*NOTREACHED*/ \ |
| 616 | return NULL; \ |
| 617 | } |
| 618 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 619 | GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk) |
| sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 620 | |
| 621 | |
| sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 622 | /* mempcpy */ |
| 623 | #define GLIBC25_MEMPCPY(soname, fnname) \ |
| 624 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 625 | ( void *dst, const void *src, SizeT len ); \ |
| 626 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 627 | ( void *dst, const void *src, SizeT len ) \ |
| 628 | { \ |
| 629 | register char *d; \ |
| 630 | register char *s; \ |
| 631 | SizeT len_saved = len; \ |
| 632 | \ |
| 633 | if (len == 0) \ |
| 634 | return dst; \ |
| 635 | \ |
| 636 | if (is_overlap(dst, src, len, len)) \ |
| 637 | RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \ |
| 638 | \ |
| 639 | if ( dst > src ) { \ |
| 640 | d = (char *)dst + len - 1; \ |
| 641 | s = (char *)src + len - 1; \ |
| 642 | while ( len-- ) { \ |
| 643 | *d-- = *s--; \ |
| 644 | } \ |
| 645 | } else if ( dst < src ) { \ |
| 646 | d = (char *)dst; \ |
| 647 | s = (char *)src; \ |
| 648 | while ( len-- ) { \ |
| 649 | *d++ = *s++; \ |
| 650 | } \ |
| 651 | } \ |
| 652 | return (void*)( ((char*)dst) + len_saved ); \ |
| 653 | } |
| 654 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 655 | GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
| 656 | GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */ |
| sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 657 | |
| 658 | |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 659 | #define GLIBC26___MEMCPY_CHK(soname, fnname) \ |
| 660 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 661 | (void* dst, const void* src, SizeT len, SizeT dstlen ); \ |
| 662 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 663 | (void* dst, const void* src, SizeT len, SizeT dstlen ) \ |
| 664 | { \ |
| 665 | extern void _exit(int status); \ |
| 666 | register char *d; \ |
| 667 | register char *s; \ |
| 668 | \ |
| 669 | if (dstlen < len) goto badness; \ |
| 670 | \ |
| 671 | if (len == 0) \ |
| 672 | return dst; \ |
| 673 | \ |
| 674 | if (is_overlap(dst, src, len, len)) \ |
| 675 | RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \ |
| 676 | \ |
| 677 | if ( dst > src ) { \ |
| 678 | d = (char *)dst + len - 1; \ |
| 679 | s = (char *)src + len - 1; \ |
| 680 | while ( len-- ) { \ |
| 681 | *d-- = *s--; \ |
| 682 | } \ |
| 683 | } else if ( dst < src ) { \ |
| 684 | d = (char *)dst; \ |
| 685 | s = (char *)src; \ |
| 686 | while ( len-- ) { \ |
| 687 | *d++ = *s++; \ |
| 688 | } \ |
| 689 | } \ |
| 690 | return dst; \ |
| 691 | badness: \ |
| 692 | VALGRIND_PRINTF_BACKTRACE( \ |
| 693 | "*** memcpy_chk: buffer overflow detected ***: " \ |
| 694 | "program terminated"); \ |
| 695 | _exit(127); \ |
| 696 | /*NOTREACHED*/ \ |
| 697 | return NULL; \ |
| 698 | } |
| 699 | |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 700 | GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk) |
| sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 701 | |
| 702 | |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 703 | /*------------------------------------------------------------*/ |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 704 | /*--- Improve definedness checking of process environment ---*/ |
| 705 | /*------------------------------------------------------------*/ |
| 706 | |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 707 | #if defined(VGO_linux) |
| 708 | |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 709 | /* putenv */ |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 710 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string); |
| 711 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string) |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 712 | { |
| 713 | OrigFn fn; |
| 714 | Word result; |
| 715 | const char* p = string; |
| 716 | VALGRIND_GET_ORIG_FN(fn); |
| 717 | /* Now by walking over the string we magically produce |
| 718 | traces when hitting undefined memory. */ |
| 719 | if (p) |
| 720 | while (*p++) |
| 721 | ; |
| 722 | CALL_FN_W_W(result, fn, string); |
| 723 | return result; |
| 724 | } |
| 725 | |
| 726 | /* unsetenv */ |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 727 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name); |
| 728 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name) |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 729 | { |
| 730 | OrigFn fn; |
| 731 | Word result; |
| 732 | const char* p = name; |
| 733 | VALGRIND_GET_ORIG_FN(fn); |
| 734 | /* Now by walking over the string we magically produce |
| 735 | traces when hitting undefined memory. */ |
| 736 | if (p) |
| 737 | while (*p++) |
| 738 | ; |
| 739 | CALL_FN_W_W(result, fn, name); |
| 740 | return result; |
| 741 | } |
| 742 | |
| 743 | /* setenv */ |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 744 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 745 | (const char* name, const char* value, int overwrite); |
| njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 746 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 747 | (const char* name, const char* value, int overwrite) |
| 748 | { |
| 749 | OrigFn fn; |
| 750 | Word result; |
| 751 | const char* p; |
| 752 | VALGRIND_GET_ORIG_FN(fn); |
| 753 | /* Now by walking over the string we magically produce |
| 754 | traces when hitting undefined memory. */ |
| 755 | if (name) |
| 756 | for (p = name; *p; p++) |
| 757 | ; |
| 758 | if (value) |
| 759 | for (p = value; *p; p++) |
| 760 | ; |
| 761 | VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite); |
| 762 | CALL_FN_W_WWW(result, fn, name, value, overwrite); |
| 763 | return result; |
| 764 | } |
| 765 | |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 766 | #endif /* defined(VGO_linux) */ |
| 767 | |
| 768 | |
| dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 769 | /*------------------------------------------------------------*/ |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 770 | /*--- AIX stuff only after this point ---*/ |
| 771 | /*------------------------------------------------------------*/ |
| 772 | |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 773 | /* Generate replacements for strcat, strncat, strcpy, strncpy, strcmp |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 774 | in the given soname. */ |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 775 | #define Str5FNs(_soname) \ |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 776 | STRCAT(_soname, strcat) \ |
| 777 | STRNCAT(_soname, strncat) \ |
| 778 | STRCPY(_soname, strcpy) \ |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 779 | STRNCPY(_soname, strncpy) \ |
| 780 | STRCMP(_soname, strcmp) |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 781 | |
| 782 | #if defined(VGP_ppc32_aix5) |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 783 | Str5FNs(NONE) /* in main exe */ |
| 784 | Str5FNs(libCZdaZLshrcoreZdoZR) /* libC.a(shrcore.o) */ |
| 785 | Str5FNs(libX11ZdaZLshr4ZdoZR) /* libX11.a(shr4.o) */ |
| 786 | Str5FNs(libXmZdaZLshrZaZdoZR) /* libXm.a(shr*.o) */ |
| 787 | Str5FNs(libXtZdaZLshr4ZdoZR) /* libXt.a(shr4.o) */ |
| 788 | Str5FNs(libppeZurZdaZLdynamicZdoZR) /* libppe_r.a(dynamic.o) */ |
| 789 | Str5FNs(libodmZdaZLshrZdoZR) /* libodm.a(shr.o) */ |
| 790 | Str5FNs(libmpiZurZdaZLmpicoreZurZdoZR) /* libmpi_r.a(mpicore_r.o) */ |
| 791 | Str5FNs(libmpiZurZdaZLmpipoeZurZdoZR) /* libmpi_r.a(mpipoe_r.o) */ |
| 792 | Str5FNs(libmpiZurZdaZLmpciZurZdoZR) /* libmpi_r.a(mpci_r.o) */ |
| 793 | Str5FNs(libslurmZdso) /* libslurm.so */ |
| 794 | Str5FNs(libglibZdso) /* libglib.so */ |
| 795 | Str5FNs(libIMZdaZLshrZdoZR) /* libIM.a(shr.o) */ |
| 796 | Str5FNs(libiconvZdaZLshr4ZdoZR) /* libiconv.a(shr4.o) */ |
| 797 | Str5FNs(libGLZdaZLshrZdoZR) /* libGL.a(shr.o) */ |
| 798 | Str5FNs(libgdkZdso) /* libgdk.so */ |
| 799 | Str5FNs(libcursesZdaZLshr42ZdoZR) /* libcurses.a(shr42.o) */ |
| 800 | Str5FNs(libqtZda) /* libqt.a */ |
| sewardj | fd4b6f4 | 2007-11-29 03:08:32 +0000 | [diff] [blame] | 801 | Str5FNs(ZaZLlibglibZhZaZdsoZaZR) /* *(libglib-*.so*) */ |
| 802 | Str5FNs(ZaZLlibfontconfigZdsoZaZR) /* *(libfontconfig.so*) */ |
| 803 | Str5FNs(libQtZaa) /* libQt*.a */ |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 804 | #endif |
| 805 | #if defined(VGP_ppc64_aix5) |
| sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 806 | Str5FNs(NONE) /* in main exe */ |
| 807 | Str5FNs(libX11ZdaZLshrZu64ZdoZR) /* libX11.a(shr_64.o) */ |
| 808 | Str5FNs(libiconvZdaZLshr4Zu64ZdoZR) /* libiconv.a(shr4_64.o) */ |
| 809 | Str5FNs(libGLZdaZLshrZu64ZdoZR) /* libGL.a(shr_64.o) */ |
| 810 | Str5FNs(libppeZurZdaZLdynamic64ZdoZR) /* libppe_r.a(dynamic64.o) */ |
| 811 | Str5FNs(libodmZdaZLshrZu64ZdoZR) /* libodm.a(shr_64.o) */ |
| 812 | Str5FNs(libmpiZurZdaZLmpicore64ZurZdoZR) /* libmpi_r.a(mpicore64_r.o) */ |
| 813 | Str5FNs(libmpiZurZdaZLmpipoe64ZurZdoZR) /* libmpi_r.a(mpipoe64_r.o) */ |
| 814 | Str5FNs(libCZdaZLshrcoreZu64ZdoZR) /* libC.a(shrcore_64.o) */ |
| 815 | Str5FNs(libmpiZurZdaZLmpci64ZurZdoZR) /* libmpi_r.a(mpci64_r.o) */ |
| 816 | Str5FNs(libqtZda) /* libqt.a */ |
| sewardj | fd4b6f4 | 2007-11-29 03:08:32 +0000 | [diff] [blame] | 817 | Str5FNs(ZaZLlibglibZhZaZdsoZaZR) /* *(libglib-*.so*) */ |
| 818 | Str5FNs(ZaZLlibfontconfigZdsoZaZR) /* *(libfontconfig.so*) */ |
| 819 | Str5FNs(libQtZaa) /* libQt*.a */ |
| sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 820 | #endif |
| 821 | |
| 822 | |
| 823 | /* AIX's libm contains a sqrt implementation which does a nasty thing: |
| 824 | it loads the initial estimate of the root into a FP register, but |
| 825 | only the upper half of the number is initialised data. Hence the |
| 826 | least significant 32 mantissa bits are undefined, and it then uses |
| 827 | Newton-Raphson iteration to compute the final, defined result. |
| 828 | This fools memcheck completely; the only solution I can think of is |
| 829 | provide our own substitute. The _FAST variant is almost right |
| 830 | except the result is not correctly rounded. The _EXACT variant, |
| 831 | which is selected by default, is always right; but it's also pretty |
| 832 | darn slow. */ |
| 833 | |
| 834 | #if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 835 | #define SQRT_FAST(soname, fnname) \ |
| 836 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ); \ |
| 837 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ) \ |
| 838 | { \ |
| 839 | static UInt T1[32] = \ |
| 840 | { 0, 1024, 3062, 5746, 9193, 13348, \ |
| 841 | 18162, 23592, 29598, 36145, 43202, 50740, \ |
| 842 | 58733, 67158, 75992, 85215, 83599, 71378, \ |
| 843 | 60428, 50647, 41945, 34246, 27478, 21581, \ |
| 844 | 16499, 12183, 8588, 5674, 3403, 1742, \ |
| 845 | 661, 130 }; \ |
| 846 | UInt x0, x1, sign, expo, mant0, bIGENDIAN = 1; \ |
| 847 | union { UInt w[2]; double d; } u; \ |
| 848 | u.d = x; \ |
| 849 | x0 = u.w[1 - bIGENDIAN]; /* high half */ \ |
| 850 | x1 = u.w[bIGENDIAN]; /* low half */ \ |
| 851 | sign = x0 >> 31; \ |
| 852 | expo = (x0 >> 20) & 0x7FF; \ |
| 853 | mant0 = x0 & 0xFFFFF; \ |
| 854 | if ( (sign == 0 && expo >= 1 && expo <= 0x7FE) /* +normal */ \ |
| 855 | || (sign == 0 && expo == 0 \ |
| 856 | && (mant0 | x1) > 0) /* +denorm */) { \ |
| 857 | /* common case; do Newton-Raphson */ \ |
| 858 | /* technically k should be signed int32, but since we're \ |
| 859 | always entering here with x > 0, doesn't matter that it's \ |
| 860 | unsigned. */ \ |
| 861 | double y; \ |
| 862 | UInt k = (x0>>1) + 0x1ff80000; \ |
| 863 | u.w[1 - bIGENDIAN] = k - T1[31&(k>>15)]; \ |
| 864 | u.w[bIGENDIAN] = 0; \ |
| 865 | y = u.d; \ |
| 866 | y = (y+x/y)/2.0 ; \ |
| 867 | y = (y+x/y)/2.0 ; \ |
| 868 | y = y-(y-x/y)/2.0 ; \ |
| 869 | return y; \ |
| 870 | } \ |
| 871 | if ( (sign == 1 && expo >= 1 && expo <= 0x7FE) /* -normal */ \ |
| 872 | || (sign == 1 && expo == 0 \ |
| 873 | && (mant0 | x1) > 0) /* -denorm */) { \ |
| 874 | u.w[1 - bIGENDIAN] = 0xFFF00000; \ |
| 875 | u.w[bIGENDIAN] = 0x1; \ |
| 876 | return u.d; /* -Inf -> NaN */ \ |
| 877 | } \ |
| 878 | if ((expo | mant0 | x1) == 0) \ |
| 879 | return x; /* +/-zero -> self */ \ |
| 880 | if (expo == 0x7FF && (mant0 | x1) == 0) { \ |
| 881 | if (sign == 0) \ |
| 882 | return x; /* +Inf -> self */ \ |
| 883 | u.w[1 - bIGENDIAN] = 0xFFF00000; \ |
| 884 | u.w[bIGENDIAN] = 0x1; \ |
| 885 | return u.d; /* -Inf -> NaN */ \ |
| 886 | } \ |
| 887 | /* must be +/- NaN */ \ |
| 888 | return x; /* +/-NaN -> self */ \ |
| 889 | } |
| 890 | |
| 891 | #define SQRT_EXACT(soname, fnname) \ |
| 892 | /* \ |
| 893 | * ==================================================== \ |
| 894 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. \ |
| 895 | * \ |
| 896 | * Developed at SunPro, a Sun Microsystems, Inc. business. \ |
| 897 | * Permission to use, copy, modify, and distribute this \ |
| 898 | * software is freely granted, provided that this notice \ |
| 899 | * is preserved. \ |
| 900 | * ==================================================== \ |
| 901 | */ \ |
| 902 | /* \ |
| 903 | * Return correctly rounded sqrt. \ |
| 904 | * ------------------------------------------ \ |
| 905 | * | Use the hardware sqrt if you have one | \ |
| 906 | * ------------------------------------------ \ |
| 907 | * Method: \ |
| 908 | * Bit by bit method using integer arithmetic. (Slow, but portable) \ |
| 909 | * 1. Normalization \ |
| 910 | * Scale x to y in [1,4) with even powers of 2: \ |
| 911 | * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then \ |
| 912 | * sqrt(x) = 2^k * sqrt(y) \ |
| 913 | * 2. Bit by bit computation \ |
| 914 | * Let q = sqrt(y) truncated to i bit after binary point (q = 1), \ |
| 915 | * i 0 \ |
| 916 | * i+1 2 \ |
| 917 | * s = 2*q , and y = 2 * ( y - q ). (1) \ |
| 918 | * i i i i \ |
| 919 | * \ |
| 920 | * To compute q from q , one checks whether \ |
| 921 | * i+1 i \ |
| 922 | * \ |
| 923 | * -(i+1) 2 \ |
| 924 | * (q + 2 ) <= y. (2) \ |
| 925 | * i \ |
| 926 | * -(i+1) \ |
| 927 | * If (2) is false, then q = q ; otherwise q = q + 2 . \ |
| 928 | * i+1 i i+1 i \ |
| 929 | * \ |
| 930 | * With some algebric manipulation, it is not difficult to see \ |
| 931 | * that (2) is equivalent to \ |
| 932 | * -(i+1) \ |
| 933 | * s + 2 <= y (3) \ |
| 934 | * i i \ |
| 935 | * \ |
| 936 | * The advantage of (3) is that s and y can be computed by \ |
| 937 | * i i \ |
| 938 | * the following recurrence formula: \ |
| 939 | * if (3) is false \ |
| 940 | * \ |
| 941 | * s = s , y = y ; (4) \ |
| 942 | * i+1 i i+1 i \ |
| 943 | * \ |
| 944 | * otherwise, \ |
| 945 | * -i -(i+1) \ |
| 946 | * s = s + 2 , y = y - s - 2 (5) \ |
| 947 | * i+1 i i+1 i i \ |
| 948 | * \ |
| 949 | * \ |
| 950 | * One may easily use induction to prove (4) and (5). \ |
| 951 | * Note. Since the left hand side of (3) contain only i+2 bits, \ |
| 952 | * it does not necessary to do a full (53-bit) comparison \ |
| 953 | * in (3). \ |
| 954 | * 3. Final rounding \ |
| 955 | * After generating the 53 bits result, we compute one more bit. \ |
| 956 | * Together with the remainder, we can decide whether the \ |
| 957 | * result is exact, bigger than 1/2ulp, or less than 1/2ulp \ |
| 958 | * (it will never equal to 1/2ulp). \ |
| 959 | * The rounding mode can be detected by checking whether \ |
| 960 | * huge + tiny is equal to huge, and whether huge - tiny is \ |
| 961 | * equal to huge for some floating point number "huge" and "tiny". \ |
| 962 | * \ |
| 963 | * Special cases: \ |
| 964 | * sqrt(+-0) = +-0 ... exact \ |
| 965 | * sqrt(inf) = inf \ |
| 966 | * sqrt(-ve) = NaN ... with invalid signal \ |
| 967 | * sqrt(NaN) = NaN ... with invalid signal for signaling NaN \ |
| 968 | * \ |
| 969 | */ \ |
| 970 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ); \ |
| 971 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ) \ |
| 972 | { \ |
| 973 | const Int bIGENDIAN = 1; \ |
| 974 | const double one = 1.0, tiny=1.0e-300; \ |
| 975 | double z; \ |
| 976 | Int sign = (Int)0x80000000; \ |
| 977 | Int ix0,s0,q,m,t,i; \ |
| 978 | UInt r,t1,s1,ix1,q1; \ |
| 979 | union { UInt w[2]; double d; } u; \ |
| 980 | u.d = x; \ |
| 981 | ix0 = u.w[1-bIGENDIAN]; \ |
| 982 | ix1 = u.w[bIGENDIAN]; \ |
| 983 | \ |
| 984 | /* take care of Inf and NaN */ \ |
| 985 | if((ix0&0x7ff00000)==0x7ff00000) { \ |
| 986 | return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf \ |
| 987 | sqrt(-inf)=sNaN */ \ |
| 988 | } \ |
| 989 | /* take care of zero */ \ |
| 990 | if(ix0<=0) { \ |
| 991 | if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */ \ |
| 992 | else if(ix0<0) \ |
| 993 | return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ \ |
| 994 | } \ |
| 995 | /* normalize x */ \ |
| 996 | m = (ix0>>20); \ |
| 997 | if(m==0) { /* subnormal x */ \ |
| 998 | while(ix0==0) { \ |
| 999 | m -= 21; \ |
| 1000 | ix0 |= (ix1>>11); ix1 <<= 21; \ |
| 1001 | } \ |
| 1002 | for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1; \ |
| 1003 | m -= i-1; \ |
| 1004 | ix0 |= (ix1>>(32-i)); \ |
| 1005 | ix1 <<= i; \ |
| 1006 | } \ |
| 1007 | m -= 1023; /* unbias exponent */ \ |
| 1008 | ix0 = (ix0&0x000fffff)|0x00100000; \ |
| 1009 | if(m&1){ /* odd m, double x to make it even */ \ |
| 1010 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1011 | ix1 += ix1; \ |
| 1012 | } \ |
| 1013 | m >>= 1; /* m = [m/2] */ \ |
| 1014 | /* generate sqrt(x) bit by bit */ \ |
| 1015 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1016 | ix1 += ix1; \ |
| 1017 | q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ \ |
| 1018 | r = 0x00200000; /* r = moving bit from right to left */ \ |
| 1019 | while(r!=0) { \ |
| 1020 | t = s0+r; \ |
| 1021 | if(t<=ix0) { \ |
| 1022 | s0 = t+r; \ |
| 1023 | ix0 -= t; \ |
| 1024 | q += r; \ |
| 1025 | } \ |
| 1026 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1027 | ix1 += ix1; \ |
| 1028 | r>>=1; \ |
| 1029 | } \ |
| 1030 | r = sign; \ |
| 1031 | while(r!=0) { \ |
| 1032 | t1 = s1+r; \ |
| 1033 | t = s0; \ |
| 1034 | if((t<ix0)||((t==ix0)&&(t1<=ix1))) { \ |
| 1035 | s1 = t1+r; \ |
| 1036 | if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1; \ |
| 1037 | ix0 -= t; \ |
| 1038 | if (ix1 < t1) ix0 -= 1; \ |
| 1039 | ix1 -= t1; \ |
| 1040 | q1 += r; \ |
| 1041 | } \ |
| 1042 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1043 | ix1 += ix1; \ |
| 1044 | r>>=1; \ |
| 1045 | } \ |
| 1046 | /* use floating add to find out rounding direction */ \ |
| 1047 | if((ix0|ix1)!=0) { \ |
| 1048 | z = one-tiny; /* trigger inexact flag */ \ |
| 1049 | if (z>=one) { \ |
| 1050 | z = one+tiny; \ |
| 1051 | if (q1==(UInt)0xffffffff) { q1=0; q += 1;} \ |
| 1052 | else if (z>one) { \ |
| 1053 | if (q1==(UInt)0xfffffffe) q+=1; \ |
| 1054 | q1+=2; \ |
| 1055 | } else \ |
| 1056 | q1 += (q1&1); \ |
| 1057 | } \ |
| 1058 | } \ |
| 1059 | ix0 = (q>>1)+0x3fe00000; \ |
| 1060 | ix1 = q1>>1; \ |
| 1061 | if ((q&1)==1) ix1 |= sign; \ |
| 1062 | ix0 += (m <<20); \ |
| 1063 | ix0 = u.w[1-bIGENDIAN] = ix0; \ |
| 1064 | ix1 = u.w[bIGENDIAN] = ix1; \ |
| 1065 | z = u.d; \ |
| 1066 | return z; \ |
| 1067 | } |
| 1068 | |
| 1069 | #if 0 |
| 1070 | SQRT_FAST(NONE, sqrt) /* xlC generates these */ |
| 1071 | SQRT_FAST(NONE, _sqrt) /* xlf generates these */ |
| 1072 | #else |
| 1073 | SQRT_EXACT(NONE, sqrt) /* xlC generates these */ |
| 1074 | SQRT_EXACT(NONE, _sqrt) /* xlf generates these */ |
| 1075 | #endif |
| 1076 | |
| 1077 | #endif /* defined(VGP_ppc32_aix5) */ |
| 1078 | |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1079 | /*--------------------------------------------------------------------*/ |
| njn | 4627586 | 2005-03-24 04:00:03 +0000 | [diff] [blame] | 1080 | /*--- end ---*/ |
| njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1081 | /*--------------------------------------------------------------------*/ |