sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Management of function redirection and wrapping. ---*/ |
| 3 | /*--- vg_redir.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
| 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 Julian Seward |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | Copyright (C) 2003-2005 Jeremy Fitzhardinge |
| 13 | jeremy@goop.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 | */ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_basics.h" |
njn | 899ce73 | 2005-06-21 00:28:11 +0000 | [diff] [blame^] | 34 | #include "pub_core_debuginfo.h" // Needed for pub_core_aspacemgr :( |
| 35 | #include "pub_core_aspacemgr.h" // Needed for pub_core_redir.h :( |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 36 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_core_libcprint.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 39 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 40 | #include "pub_core_options.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 41 | #include "pub_core_redir.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 42 | #include "pub_core_skiplist.h" |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 43 | #include "pub_core_trampoline.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 44 | #include "pub_core_transtab.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 45 | #include "m_debuginfo/priv_symtab.h" // XXX: bad! For SegInfo internals |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 46 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 47 | /*------------------------------------------------------------*/ |
| 48 | /*--- General purpose redirection. ---*/ |
| 49 | /*------------------------------------------------------------*/ |
| 50 | |
| 51 | /* |
| 52 | wraps and redirections, indexed by from_addr |
| 53 | |
| 54 | Redirection and wrapping are two distinct mechanisms which Valgrind |
| 55 | can use to change the client's control flow. |
| 56 | |
| 57 | Redirection intercepts a call to a client function, and re-points it |
| 58 | to a new piece of code (presumably functionally equivalent). The |
| 59 | original code is never run. |
| 60 | |
| 61 | Wrapping does call the client's original code, but calls "before" |
| 62 | and "after" functions which can inspect (and perhaps modify) the |
| 63 | function's arguments and return value. |
| 64 | */ |
| 65 | struct _CodeRedirect { |
| 66 | enum redir_type { |
| 67 | R_REDIRECT, /* plain redirection */ |
| 68 | R_WRAPPER, /* wrap with valgrind-internal code */ |
| 69 | R_CLIENT_WRAPPER, /* wrap with client-side code */ |
| 70 | } type; |
| 71 | |
| 72 | const Char *from_lib; /* library qualifier pattern */ |
| 73 | const Char *from_sym; /* symbol */ |
| 74 | Addr from_addr; /* old addr */ |
| 75 | |
| 76 | /* used for redirection */ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 77 | Addr to_addr; /* new addr */ |
| 78 | |
| 79 | /* used for wrapping */ |
| 80 | const FuncWrapper *wrapper; |
| 81 | |
| 82 | CodeRedirect *next; /* next pointer on unresolved list */ |
| 83 | }; |
| 84 | |
| 85 | static Char *straddr(void *p) |
| 86 | { |
| 87 | static Char buf[16]; |
| 88 | |
| 89 | VG_(sprintf)(buf, "%p", *(Addr *)p); |
| 90 | |
| 91 | return buf; |
| 92 | } |
| 93 | |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 94 | static SkipList sk_resolved_redir = VG_SKIPLIST_INIT(CodeRedirect, from_addr, |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 95 | VG_(cmp_Addr), straddr, VG_AR_SYMTAB); |
| 96 | static CodeRedirect *unresolved_redir = NULL; |
| 97 | |
| 98 | static Bool match_lib(const Char *pattern, const SegInfo *si) |
| 99 | { |
| 100 | /* pattern == NULL matches everything, otherwise use globbing |
| 101 | |
| 102 | If the pattern starts with: |
| 103 | file:, then match filename |
| 104 | soname:, then match soname |
| 105 | something else, match filename |
| 106 | */ |
| 107 | const Char *name = si->filename; |
| 108 | |
| 109 | if (pattern == NULL) |
| 110 | return True; |
| 111 | |
| 112 | if (VG_(strncmp)(pattern, "file:", 5) == 0) { |
| 113 | pattern += 5; |
| 114 | name = si->filename; |
| 115 | } |
| 116 | if (VG_(strncmp)(pattern, "soname:", 7) == 0) { |
| 117 | pattern += 7; |
| 118 | name = si->soname; |
| 119 | } |
| 120 | |
| 121 | if (name == NULL) |
| 122 | return False; |
| 123 | |
| 124 | return VG_(string_match)(pattern, name); |
| 125 | } |
| 126 | |
| 127 | static inline Bool from_resolved(const CodeRedirect *redir) |
| 128 | { |
| 129 | return redir->from_addr != 0; |
| 130 | } |
| 131 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 132 | Bool VG_(is_resolved)(const CodeRedirect *redir) |
| 133 | { |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 134 | return from_resolved(redir); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 135 | } |
| 136 | |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 137 | static void add_resolved(CodeRedirect *redir) |
| 138 | { |
| 139 | switch(redir->type) { |
| 140 | case R_REDIRECT: |
| 141 | if (VG_(clo_trace_redir)) { |
| 142 | VG_(message)(Vg_DebugMsg, " redir resolved (%s:%s=%p -> ", |
| 143 | redir->from_lib, redir->from_sym, redir->from_addr); |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 144 | VG_(message)(Vg_DebugMsg, " %p)", redir->to_addr); |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (VG_(search_transtab)(NULL, (Addr64)redir->from_addr, False)) { |
| 148 | /* For some given (from, to) redir, the "from" function got |
| 149 | called before the .so containing "to" became available. We |
| 150 | know this because there is already a translation for the |
| 151 | entry point of the original "from". So the redirect will |
| 152 | never actually take effect unless that translation is |
| 153 | discarded. |
| 154 | |
| 155 | Note, we only really need to discard the first bb of the |
| 156 | old entry point, and so we avoid the problem of having to |
| 157 | figure out how big that bb was -- since it is at least 1 |
| 158 | byte of original code, we can just pass 1 as the original |
| 159 | size to invalidate_translations() and it will indeed get |
| 160 | rid of the translation. |
| 161 | |
| 162 | Note, this is potentially expensive -- discarding |
| 163 | translations causes complete unchaining. |
| 164 | */ |
| 165 | if (VG_(clo_verbosity) > 2 && VG_(clo_trace_redir)) { |
| 166 | VG_(message)(Vg_UserMsg, |
| 167 | "Discarding translation due to redirect of already called function" ); |
| 168 | VG_(message)(Vg_UserMsg, |
| 169 | " %s (%p -> %p)", |
| 170 | redir->from_sym, redir->from_addr, redir->to_addr ); |
| 171 | } |
| 172 | VG_(discard_translations)((Addr64)redir->from_addr, 1); |
| 173 | } |
| 174 | |
| 175 | { |
| 176 | CodeRedirect *r = VG_(SkipList_Find_Exact)(&sk_resolved_redir, &redir->from_addr); |
| 177 | |
| 178 | if (r == NULL) |
| 179 | VG_(SkipList_Insert)(&sk_resolved_redir, redir); |
| 180 | else { |
| 181 | /* XXX leak redir */ |
| 182 | if (VG_(clo_trace_redir)) |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 183 | VG_(message)(Vg_DebugMsg, " redir %s:%s:%p->%p duplicated\n", |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 184 | redir->from_lib, redir->from_sym, redir->from_addr, |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 185 | redir->to_addr); |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | break; |
| 189 | |
| 190 | case R_WRAPPER: |
| 191 | if (VG_(clo_trace_redir)) { |
| 192 | VG_(message)(Vg_DebugMsg, " wrapper resolved (%s:%s=%p -> wrapper)", |
| 193 | redir->from_lib, redir->from_sym, redir->from_addr); |
| 194 | } |
| 195 | |
| 196 | /* XXX redir leaked */ |
| 197 | //VG_(wrap_function)(redir->from_addr, redir->wrapper); |
| 198 | break; |
| 199 | |
| 200 | case R_CLIENT_WRAPPER: |
| 201 | VG_(core_panic)("not implemented"); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 206 | /* Resolve a redir using si if possible, and add it to the resolved |
| 207 | list */ |
njn | 1725012 | 2005-05-14 17:18:12 +0000 | [diff] [blame] | 208 | static Bool resolve_redir(CodeRedirect *redir, const SegInfo *si) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 209 | { |
| 210 | Bool resolved; |
| 211 | |
| 212 | vg_assert(si != NULL); |
| 213 | vg_assert(si->seg != NULL); |
| 214 | |
| 215 | /* no redirection from Valgrind segments */ |
| 216 | if (si->seg->flags & SF_VALGRIND) |
| 217 | return False; |
| 218 | |
| 219 | resolved = VG_(is_resolved)(redir); |
| 220 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 221 | vg_assert(!resolved); |
| 222 | |
| 223 | if (!from_resolved(redir)) { |
| 224 | vg_assert(redir->from_sym != NULL); |
| 225 | |
| 226 | if (match_lib(redir->from_lib, si)) { |
| 227 | redir->from_addr = VG_(reverse_search_one_symtab)(si, redir->from_sym); |
| 228 | if (VG_(clo_trace_redir) && redir->from_addr != 0) |
| 229 | VG_(printf)(" bind FROM: %p = %s:%s\n", |
| 230 | redir->from_addr,redir->from_lib, redir->from_sym ); |
| 231 | } |
| 232 | } |
| 233 | |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 234 | resolved = from_resolved(redir); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 235 | |
| 236 | if (0 && VG_(clo_trace_redir)) |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 237 | VG_(printf)("resolve_redir: %s:%s from=%p to=%p\n", |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 238 | redir->from_lib, redir->from_sym, redir->from_addr, |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 239 | redir->to_addr); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 240 | |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 241 | if (resolved) add_resolved(redir); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 242 | |
| 243 | return resolved; |
| 244 | } |
| 245 | |
njn | bf7ca33 | 2005-05-14 17:11:06 +0000 | [diff] [blame] | 246 | static Bool resolve_redir_allsegs(CodeRedirect *redir) |
| 247 | { |
| 248 | const SegInfo *si; |
| 249 | |
| 250 | for(si = VG_(next_seginfo)(NULL); |
| 251 | si != NULL; |
| 252 | si = VG_(next_seginfo)(si)) |
| 253 | { |
njn | 1725012 | 2005-05-14 17:18:12 +0000 | [diff] [blame] | 254 | if (resolve_redir(redir, si)) |
njn | bf7ca33 | 2005-05-14 17:11:06 +0000 | [diff] [blame] | 255 | return True; |
| 256 | } |
| 257 | return False; |
| 258 | } |
| 259 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 260 | /* Go through the complete redir list, resolving as much as possible with this SegInfo. |
| 261 | |
| 262 | This should be called when a new SegInfo symtab is loaded. |
| 263 | */ |
| 264 | void VG_(resolve_seg_redirs)(SegInfo *si) |
| 265 | { |
| 266 | CodeRedirect **prevp = &unresolved_redir; |
| 267 | CodeRedirect *redir, *next; |
| 268 | |
| 269 | if (VG_(clo_trace_redir)) |
| 270 | VG_(printf)("Considering redirs to/from %s(soname=%s)\n", |
| 271 | si->filename, si->soname); |
| 272 | |
| 273 | /* visit each unresolved redir - if it becomes resolved, then |
| 274 | remove it from the unresolved list */ |
| 275 | for(redir = unresolved_redir; redir != NULL; redir = next) { |
| 276 | next = redir->next; |
| 277 | |
njn | 1725012 | 2005-05-14 17:18:12 +0000 | [diff] [blame] | 278 | if (resolve_redir(redir, si)) { |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 279 | *prevp = next; |
| 280 | redir->next = NULL; |
| 281 | } else |
| 282 | prevp = &redir->next; |
| 283 | } |
| 284 | } |
| 285 | |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 286 | static void add_redirect_X_to_addr( |
| 287 | const Char *from_lib, const Char *from_sym, Addr from_addr, Addr to_addr |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 288 | ) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 289 | { |
| 290 | CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redir); |
| 291 | |
| 292 | redir->type = R_REDIRECT; |
| 293 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 294 | if (from_lib) redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib); |
tom | 3c9b866 | 2005-06-16 09:20:43 +0000 | [diff] [blame] | 295 | else redir->from_lib = NULL; |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 296 | if (from_sym) redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym); |
tom | 3c9b866 | 2005-06-16 09:20:43 +0000 | [diff] [blame] | 297 | else redir->from_sym = NULL; |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 298 | redir->from_addr = from_addr; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 299 | |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 300 | vg_assert(0 != to_addr); |
| 301 | redir->to_addr = to_addr; |
| 302 | redir->wrapper = 0; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 303 | |
| 304 | if (VG_(clo_verbosity) >= 2 && VG_(clo_trace_redir)) |
| 305 | VG_(message)(Vg_UserMsg, |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 306 | "REDIRECT %s:%s(%p) to %p", |
| 307 | from_lib, from_sym, from_addr, to_addr); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 308 | |
| 309 | /* Check against all existing segments to see if this redirection |
| 310 | can be resolved immediately */ |
tom | 3c9b866 | 2005-06-16 09:20:43 +0000 | [diff] [blame] | 311 | if (VG_(is_resolved)(redir)) { |
| 312 | add_resolved(redir); |
| 313 | } |
| 314 | else if (!resolve_redir_allsegs(redir)) { |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 315 | /* nope, add to list */ |
| 316 | redir->next = unresolved_redir; |
| 317 | unresolved_redir = redir; |
| 318 | } |
| 319 | } |
| 320 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 321 | /* Redirect a lib/symbol reference to a function at addr */ |
| 322 | static void add_redirect_sym_to_addr(const Char *from_lib, const Char *from_sym, |
| 323 | Addr to_addr) |
| 324 | { |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 325 | add_redirect_X_to_addr(from_lib, from_sym, 0, to_addr); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 328 | /* Redirect a function at from_addr to a function at to_addr */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 329 | __attribute__((unused)) // It is used, but not on all platforms... |
| 330 | static void add_redirect_addr_to_addr(Addr from_addr, Addr to_addr) |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 331 | { |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 332 | add_redirect_X_to_addr(NULL, NULL, from_addr, to_addr); |
tom | 748a131 | 2005-04-02 15:53:01 +0000 | [diff] [blame] | 333 | } |
| 334 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 335 | CodeRedirect *VG_(add_wrapper)(const Char *from_lib, const Char *from_sym, |
| 336 | const FuncWrapper *wrapper) |
| 337 | { |
| 338 | CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redir); |
| 339 | |
| 340 | if (0) |
| 341 | VG_(printf)("adding wrapper for %s:%s -> (%p,%p)\n", |
| 342 | from_lib, from_sym, wrapper->before, wrapper->after); |
| 343 | |
| 344 | redir->type = R_WRAPPER; |
| 345 | |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 346 | redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib); |
| 347 | redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 348 | redir->from_addr = 0; |
njn | 136b83b | 2005-06-19 05:14:03 +0000 | [diff] [blame] | 349 | redir->to_addr = 0; |
| 350 | redir->wrapper = wrapper; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 351 | |
| 352 | /* Check against all existing segments to see if this redirection |
| 353 | can be resolved immediately */ |
njn | bf7ca33 | 2005-05-14 17:11:06 +0000 | [diff] [blame] | 354 | if (!resolve_redir_allsegs(redir)) { |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 355 | /* nope, add to list */ |
| 356 | redir->next = unresolved_redir; |
| 357 | unresolved_redir = redir; |
| 358 | } |
| 359 | |
| 360 | return redir; |
| 361 | } |
| 362 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 363 | /* If address 'a' is being redirected, return the redirected-to |
| 364 | address. */ |
| 365 | Addr VG_(code_redirect)(Addr a) |
| 366 | { |
| 367 | CodeRedirect* r; |
| 368 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 369 | r = VG_(SkipList_Find_Exact)(&sk_resolved_redir, &a); |
| 370 | if (r == NULL) |
| 371 | return a; |
| 372 | |
| 373 | vg_assert(r->to_addr != 0); |
| 374 | |
| 375 | return r->to_addr; |
| 376 | } |
| 377 | |
| 378 | void VG_(setup_code_redirect_table) ( void ) |
| 379 | { |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 380 | #if defined(VGP_x86_linux) |
| 381 | /* Redirect _dl_sysinfo_int80, which is glibc's default system call |
| 382 | routine, to the routine in our trampoline page so that the |
| 383 | special sysinfo unwind hack in m_stacktrace.c will kick in. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 384 | add_redirect_sym_to_addr("soname:ld-linux.so.2", "_dl_sysinfo_int80", |
| 385 | VG_(client_trampoline_code)+VG_(tramp_syscall_offset)); |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 386 | #elif defined(VGP_amd64_linux) |
| 387 | /* Redirect vsyscalls to local versions */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 388 | add_redirect_addr_to_addr(0xFFFFFFFFFF600000ULL, |
| 389 | VG_(client_trampoline_code)+VG_(tramp_gettimeofday_offset)); |
| 390 | add_redirect_addr_to_addr(0xFFFFFFFFFF600400ULL, |
| 391 | VG_(client_trampoline_code)+VG_(tramp_time_offset)); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 392 | #elif defined(VGP_ppc32_linux) |
| 393 | //CAB: TODO |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 394 | #else |
| 395 | # error Unknown platform |
| 396 | #endif |
| 397 | } |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 398 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 399 | /* Z-decode a symbol into library:func form, eg |
| 400 | |
| 401 | _vgi_libcZdsoZd6__ZdlPv --> libc.so.6:_ZdlPv |
| 402 | |
| 403 | Uses the Z-encoding scheme described in pub_core_redir.h. |
| 404 | Returns True if demangle OK, False otherwise. |
| 405 | */ |
| 406 | static Bool Z_decode(const Char* symbol, Char* result, Int nbytes) |
| 407 | { |
| 408 | # define EMIT(ch) \ |
| 409 | do { \ |
| 410 | if (j >= nbytes) \ |
| 411 | result[j-1] = 0; \ |
| 412 | else \ |
| 413 | result[j++] = ch; \ |
| 414 | } while (0) |
| 415 | |
| 416 | Bool error = False; |
| 417 | Int i, j = 0; |
| 418 | Int len = VG_(strlen)(symbol); |
| 419 | if (0) VG_(printf)("idm: %s\n", symbol); |
| 420 | |
| 421 | i = VG_REPLACE_FUNCTION_PREFIX_LEN; |
| 422 | |
| 423 | /* Chew though the Z-encoded soname part. */ |
| 424 | while (True) { |
| 425 | |
| 426 | if (i >= len) |
| 427 | break; |
| 428 | |
| 429 | if (symbol[i] == '_') |
| 430 | /* We found the underscore following the Z-encoded soname. |
| 431 | Just copy the rest literally. */ |
| 432 | break; |
| 433 | |
| 434 | if (symbol[i] != 'Z') { |
| 435 | EMIT(symbol[i]); |
| 436 | i++; |
| 437 | continue; |
| 438 | } |
| 439 | |
| 440 | /* We've got a Z-escape. Act accordingly. */ |
| 441 | i++; |
| 442 | if (i >= len) { |
| 443 | /* Hmm, Z right at the end. Something's wrong. */ |
| 444 | error = True; |
| 445 | EMIT('Z'); |
| 446 | break; |
| 447 | } |
| 448 | switch (symbol[i]) { |
| 449 | case 'a': EMIT('*'); break; |
| 450 | case 'p': EMIT('+'); break; |
| 451 | case 'c': EMIT(':'); break; |
| 452 | case 'd': EMIT('.'); break; |
| 453 | case 'u': EMIT('_'); break; |
| 454 | case 'h': EMIT('-'); break; |
| 455 | case 's': EMIT(' '); break; |
| 456 | case 'Z': EMIT('Z'); break; |
| 457 | default: error = True; EMIT('Z'); EMIT(symbol[i]); break; |
| 458 | } |
| 459 | i++; |
| 460 | } |
| 461 | |
| 462 | if (error || i >= len || symbol[i] != '_') { |
| 463 | /* Something's wrong. Give up. */ |
| 464 | VG_(message)(Vg_UserMsg, "intercept: error demangling: %s", symbol); |
| 465 | EMIT(0); |
| 466 | return False; |
| 467 | } |
| 468 | |
| 469 | /* Copy the rest of the string verbatim. */ |
| 470 | i++; |
| 471 | EMIT(':'); |
| 472 | while (True) { |
| 473 | if (i >= len) |
| 474 | break; |
| 475 | EMIT(symbol[i]); |
| 476 | i++; |
| 477 | } |
| 478 | |
| 479 | EMIT(0); |
| 480 | if (0) VG_(printf)("%s\n", result); |
| 481 | return True; |
| 482 | |
| 483 | # undef EMIT |
| 484 | } |
| 485 | |
| 486 | // Nb: this can change the string pointed to by 'symbol'. |
| 487 | static void handle_replacement_function( Char* symbol, Addr addr ) |
| 488 | { |
| 489 | Bool ok; |
| 490 | Int len = VG_(strlen)(symbol) + 1 - VG_REPLACE_FUNCTION_PREFIX_LEN; |
| 491 | Char *lib = VG_(arena_malloc)(VG_AR_SYMTAB, len+8); |
| 492 | Char *func; |
| 493 | |
| 494 | // Put "soname:" at the start of lib |
| 495 | VG_(strcpy)(lib, "soname:"); |
| 496 | |
| 497 | ok = Z_decode(symbol, lib+7, len); |
| 498 | if (ok) { |
| 499 | // lib is "soname:<libname>:<fnname>". Split the string at the 2nd ':'. |
| 500 | func = lib + VG_(strlen)(lib)-1; |
| 501 | while(*func != ':') func--; |
| 502 | *func = '\0'; |
| 503 | func++; // Move past the '\0' |
| 504 | |
| 505 | // Now lib is "soname:<libname>" and func is "<fnname>". |
| 506 | if (0) VG_(printf)("lib A%sZ, func A%sZ\n", lib, func); |
| 507 | add_redirect_sym_to_addr(lib, func, addr); |
| 508 | |
| 509 | // Overwrite the given Z-encoded name with just the fnname. |
| 510 | VG_(strcpy)(symbol, func); |
| 511 | } |
| 512 | |
| 513 | VG_(arena_free)(VG_AR_SYMTAB, lib); |
| 514 | } |
| 515 | |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 516 | static Addr __libc_freeres_wrapper = 0; |
| 517 | |
| 518 | Addr VG_(get_libc_freeres_wrapper)(void) |
| 519 | { |
| 520 | return __libc_freeres_wrapper; |
| 521 | } |
| 522 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 523 | // This is specifically for stringifying VG_(x) function names. We |
| 524 | // need to do two macroexpansions to get the VG_ macro expanded before |
| 525 | // stringifying. |
| 526 | #define _STR(x) #x |
| 527 | #define STR(x) _STR(x) |
| 528 | |
| 529 | static void handle_load_notifier( Char* symbol, Addr addr ) |
| 530 | { |
| 531 | if (VG_(strcmp)(symbol, STR(VG_NOTIFY_ON_LOAD(freeres))) == 0) |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 532 | __libc_freeres_wrapper = addr; |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 533 | // else if (VG_(strcmp)(symbol, STR(VG_WRAPPER(pthread_startfunc_wrapper))) == 0) |
| 534 | // VG_(pthread_startfunc_wrapper)((Addr)(si->offset + sym->st_value)); |
| 535 | else |
| 536 | vg_assert2(0, "unrecognised load notification function: %s", symbol); |
| 537 | } |
| 538 | |
| 539 | static Bool is_replacement_function(Char* s) |
| 540 | { |
| 541 | return (0 == VG_(strncmp)(s, |
| 542 | VG_REPLACE_FUNCTION_PREFIX, |
| 543 | VG_REPLACE_FUNCTION_PREFIX_LEN)); |
| 544 | } |
| 545 | |
| 546 | static Bool is_load_notifier(Char* s) |
| 547 | { |
| 548 | return (0 == VG_(strncmp)(s, |
| 549 | VG_NOTIFY_ON_LOAD_PREFIX, |
| 550 | VG_NOTIFY_ON_LOAD_PREFIX_LEN)); |
| 551 | } |
| 552 | |
| 553 | // Call this for each symbol loaded. It determines if we need to do |
| 554 | // anything special with it. It can modify 'symbol' in-place. |
| 555 | void VG_(maybe_redir_or_notify) ( Char* symbol, Addr addr ) |
| 556 | { |
| 557 | if (is_replacement_function(symbol)) |
| 558 | handle_replacement_function(symbol, addr); |
| 559 | else |
| 560 | if (is_load_notifier(symbol)) |
| 561 | handle_load_notifier(symbol, addr); |
| 562 | } |
| 563 | |
| 564 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 565 | //:: /*------------------------------------------------------------*/ |
| 566 | //:: /*--- General function wrapping. ---*/ |
| 567 | //:: /*------------------------------------------------------------*/ |
| 568 | //:: |
| 569 | //:: /* |
| 570 | //:: TODO: |
| 571 | //:: - hook into the symtab machinery |
| 572 | //:: - client-side wrappers? |
| 573 | //:: - better interfaces for before() functions to get to arguments |
| 574 | //:: - handle munmap of code (dlclose()) |
| 575 | //:: - handle thread exit |
| 576 | //:: - handle longjmp |
| 577 | //:: */ |
| 578 | //:: struct callkey { |
| 579 | //:: ThreadId tid; /* calling thread */ |
| 580 | //:: Addr esp; /* address of args on stack */ |
| 581 | //:: Addr eip; /* return address */ |
| 582 | //:: }; |
| 583 | //:: |
| 584 | //:: struct call_instance { |
| 585 | //:: struct callkey key; |
| 586 | //:: |
| 587 | //:: const FuncWrapper *wrapper; |
| 588 | //:: void *nonce; |
| 589 | //:: }; |
| 590 | //:: |
| 591 | //:: static inline Addr addrcmp(Addr a, Addr b) |
| 592 | //:: { |
| 593 | //:: if (a < b) |
| 594 | //:: return -1; |
| 595 | //:: else if (a > b) |
| 596 | //:: return 1; |
| 597 | //:: else |
| 598 | //:: return 0; |
| 599 | //:: } |
| 600 | //:: |
| 601 | //:: static inline Int cmp(UInt a, UInt b) |
| 602 | //:: { |
| 603 | //:: if (a < b) |
| 604 | //:: return -1; |
| 605 | //:: else if (a > b) |
| 606 | //:: return 1; |
| 607 | //:: else |
| 608 | //:: return 0; |
| 609 | //:: } |
| 610 | //:: |
| 611 | //:: static Int keycmp(const void *pa, const void *pb) |
| 612 | //:: { |
| 613 | //:: const struct callkey *a = (const struct callkey *)pa; |
| 614 | //:: const struct callkey *b = (const struct callkey *)pb; |
| 615 | //:: Int ret; |
| 616 | //:: |
| 617 | //:: if ((ret = cmp(a->tid, b->tid))) |
| 618 | //:: return ret; |
| 619 | //:: |
| 620 | //:: if ((ret = addrcmp(a->esp, b->esp))) |
| 621 | //:: return ret; |
| 622 | //:: |
| 623 | //:: return addrcmp(a->eip, b->eip); |
| 624 | //:: } |
| 625 | //:: |
| 626 | //:: /* List of wrapped call invocations which are currently active */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 627 | //:: static SkipList wrapped_frames = VG_SKIPLIST_INIT(struct call_instance, key, keycmp, |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 628 | //:: NULL, VG_AR_SYMTAB); |
| 629 | //:: |
| 630 | //:: static struct call_instance *find_call(Addr retaddr, Addr argsp, ThreadId tid) |
| 631 | //:: { |
| 632 | //:: struct callkey key = { tid, argsp, retaddr }; |
| 633 | //:: |
| 634 | //:: return VG_(SkipList_Find_Exact)(&wrapped_frames, &key); |
| 635 | //:: } |
| 636 | //:: |
| 637 | //:: static void wrapper_return(Addr retaddr); |
| 638 | //:: |
| 639 | //:: /* Called from generated code via helper */ |
| 640 | //:: void VG_(wrap_before)(ThreadState *tst, const FuncWrapper *wrapper) |
| 641 | //:: { |
njn | db9b773 | 2005-03-26 00:32:29 +0000 | [diff] [blame] | 642 | //:: Addr retaddr = VGA_RETADDR(tst->arch); |
| 643 | //:: Addr argp = (Addr)&VGA_FUNC_ARG(tst->arch, 0); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 644 | //:: void *nonce = NULL; |
| 645 | //:: Bool mf = VG_(my_fault); |
| 646 | //:: VG_(my_fault) = True; |
| 647 | //:: |
| 648 | //:: if (wrapper->before) { |
njn | db9b773 | 2005-03-26 00:32:29 +0000 | [diff] [blame] | 649 | //:: va_list args = VGA_VA_LIST(tst->arch); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 650 | //:: nonce = (*wrapper->before)(args); |
| 651 | //:: } |
| 652 | //:: |
| 653 | //:: if (wrapper->after) { |
| 654 | //:: /* If there's an after function, make sure it gets called */ |
| 655 | //:: struct call_instance *call; |
| 656 | //:: |
| 657 | //:: call = find_call(retaddr, argp, tst->tid); |
| 658 | //:: |
| 659 | //:: if (call != NULL) { |
| 660 | //:: /* Found a stale outstanding call; clean it up and recycle |
| 661 | //:: the structure */ |
| 662 | //:: if (call->wrapper->after) |
| 663 | //:: (*call->wrapper->after)(call->nonce, RT_LONGJMP, 0); |
| 664 | //:: } else { |
| 665 | //:: call = VG_(SkipNode_Alloc)(&wrapped_frames); |
| 666 | //:: |
| 667 | //:: call->key.tid = tst->tid; |
| 668 | //:: call->key.esp = argp; |
| 669 | //:: call->key.eip = retaddr; |
| 670 | //:: |
| 671 | //:: VG_(SkipList_Insert)(&wrapped_frames, call); |
| 672 | //:: |
| 673 | //:: wrapper_return(retaddr); |
| 674 | //:: } |
| 675 | //:: |
| 676 | //:: call->wrapper = wrapper; |
| 677 | //:: call->nonce = nonce; |
| 678 | //:: } else |
| 679 | //:: vg_assert(nonce == NULL); |
| 680 | //:: |
| 681 | //:: VG_(my_fault) = mf; |
| 682 | //:: } |
| 683 | //:: |
| 684 | //:: /* Called from generated code via helper */ |
| 685 | //:: void VG_(wrap_after)(ThreadState *tst) |
| 686 | //:: { |
njn | 35172bc | 2005-03-26 00:04:03 +0000 | [diff] [blame] | 687 | //:: Addr EIP = VGA_INSTR_PTR(tst->arch); /* instruction after call */ |
| 688 | //:: Addr ESP = VGA_STACK_PTR(tst->arch); /* pointer to args */ |
| 689 | //:: Word ret = VGA_RETVAL(tst->arch); /* return value */ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 690 | //:: struct call_instance *call; |
| 691 | //:: Bool mf = VG_(my_fault); |
| 692 | //:: |
| 693 | //:: VG_(my_fault) = True; |
| 694 | //:: call = find_call(EIP, ESP, tst->tid); |
| 695 | //:: |
| 696 | //:: if (0) |
| 697 | //:: VG_(printf)("wrap_after(%p,%p,%d) -> %p\n", EIP, ESP, tst->tid, call); |
| 698 | //:: |
| 699 | //:: if (call != NULL) { |
| 700 | //:: if (call->wrapper->after) |
| 701 | //:: (*call->wrapper->after)(call->nonce, RT_RETURN, ret); |
| 702 | //:: |
| 703 | //:: VG_(SkipList_Remove)(&wrapped_frames, &call->key); |
| 704 | //:: VG_(SkipNode_Free)(&wrapped_frames, call); |
| 705 | //:: } |
| 706 | //:: VG_(my_fault) = mf; |
| 707 | //:: } |
| 708 | //:: |
| 709 | //:: |
| 710 | //:: struct wrapped_function { |
| 711 | //:: Addr eip; /* eip of function entrypoint */ |
| 712 | //:: const FuncWrapper *wrapper; |
| 713 | //:: }; |
| 714 | //:: |
| 715 | //:: struct wrapper_return { |
| 716 | //:: Addr eip; /* return address */ |
| 717 | //:: }; |
| 718 | //:: |
| 719 | //:: /* A mapping from eip of wrapped function entrypoints to actual wrappers */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 720 | //:: static SkipList wrapped_functions = VG_SKIPLIST_INIT(struct wrapped_function, eip, VG_(cmp_Addr), |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 721 | //:: NULL, VG_AR_SYMTAB); |
| 722 | //:: |
| 723 | //:: /* A set of EIPs which are return addresses for wrapped functions */ |
njn | be91aae | 2005-03-27 01:42:41 +0000 | [diff] [blame] | 724 | //:: static SkipList wrapper_returns = VG_SKIPLIST_INIT(struct wrapper_return, eip, VG_(cmp_Addr), |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 725 | //:: NULL, VG_AR_SYMTAB); |
| 726 | //:: |
| 727 | //:: /* Wrap function starting at eip */ |
| 728 | //:: void VG_(wrap_function)(Addr eip, const FuncWrapper *wrapper) |
| 729 | //:: { |
| 730 | //:: struct wrapped_function *func; |
| 731 | //:: |
| 732 | //:: if (0) |
| 733 | //:: VG_(printf)("wrapping %p with (%p,%p)\n", eip, wrapper->before, wrapper->after); |
| 734 | //:: |
| 735 | //:: func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip); |
| 736 | //:: |
| 737 | //:: if (func == NULL) { |
| 738 | //:: func = VG_(SkipNode_Alloc)(&wrapped_functions); |
| 739 | //:: VG_(invalidate_translations)(eip, 1, True); |
| 740 | //:: |
| 741 | //:: func->eip = eip; |
| 742 | //:: VG_(SkipList_Insert)(&wrapped_functions, func); |
| 743 | //:: } |
| 744 | //:: |
| 745 | //:: func->wrapper = wrapper; |
| 746 | //:: } |
| 747 | //:: |
| 748 | //:: const FuncWrapper *VG_(is_wrapped)(Addr eip) |
| 749 | //:: { |
| 750 | //:: struct wrapped_function *func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip); |
| 751 | //:: |
| 752 | //:: if (func) |
| 753 | //:: return func->wrapper; |
| 754 | //:: return NULL; |
| 755 | //:: } |
| 756 | //:: |
| 757 | //:: Bool VG_(is_wrapper_return)(Addr eip) |
| 758 | //:: { |
| 759 | //:: struct wrapper_return *ret = VG_(SkipList_Find_Exact)(&wrapper_returns, &eip); |
| 760 | //:: |
| 761 | //:: return ret != NULL; |
| 762 | //:: } |
| 763 | //:: |
| 764 | //:: /* Mark eip as being the return address of a wrapper, so that the |
| 765 | //:: codegen will generate the appropriate call. */ |
| 766 | //:: void wrapper_return(Addr eip) |
| 767 | //:: { |
| 768 | //:: struct wrapper_return *ret; |
| 769 | //:: |
| 770 | //:: if (VG_(is_wrapper_return)(eip)) |
| 771 | //:: return; |
| 772 | //:: |
| 773 | //:: VG_(invalidate_translations)(eip, 1, True); |
| 774 | //:: |
| 775 | //:: ret = VG_(SkipNode_Alloc)(&wrapper_returns); |
| 776 | //:: ret->eip = eip; |
| 777 | //:: |
| 778 | //:: VG_(SkipList_Insert)(&wrapper_returns, ret); |
| 779 | //:: } |