blob: 49811606c52ac60c562af8acb47989d93782af0a [file] [log] [blame]
sewardjcbdddcf2005-03-10 23:23:45 +00001/*--------------------------------------------------------------------*/
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
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjcbdddcf2005-03-10 23:23:45 +000011 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*/
sewardjcbdddcf2005-03-10 23:23:45 +000032
njnc7561b92005-06-19 01:24:32 +000033#include "pub_core_basics.h"
njn88c51482005-06-25 20:49:33 +000034#include "pub_core_debuginfo.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000037#include "pub_core_libcprint.h"
njnaf1d7df2005-06-11 01:31:52 +000038#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000039#include "pub_core_options.h"
njnd1af0032005-05-29 17:01:48 +000040#include "pub_core_redir.h"
njnaf1d7df2005-06-11 01:31:52 +000041#include "pub_core_skiplist.h"
njna7598f62005-06-18 03:27:58 +000042#include "pub_core_trampoline.h"
njn8bddf582005-05-13 23:40:55 +000043#include "pub_core_transtab.h"
sewardj55f9d1a2005-04-25 11:11:44 +000044
sewardjcbdddcf2005-03-10 23:23:45 +000045/*------------------------------------------------------------*/
46/*--- General purpose redirection. ---*/
47/*------------------------------------------------------------*/
48
njnd9109c62005-06-26 04:49:25 +000049#define TRACE_REDIR(format, args...) \
50 if (VG_(clo_trace_redir)) { VG_(message)(Vg_DebugMsg, format, ## args); }
51
sewardjcbdddcf2005-03-10 23:23:45 +000052/*
53 wraps and redirections, indexed by from_addr
54
55 Redirection and wrapping are two distinct mechanisms which Valgrind
56 can use to change the client's control flow.
57
58 Redirection intercepts a call to a client function, and re-points it
59 to a new piece of code (presumably functionally equivalent). The
60 original code is never run.
61
62 Wrapping does call the client's original code, but calls "before"
63 and "after" functions which can inspect (and perhaps modify) the
64 function's arguments and return value.
65 */
66struct _CodeRedirect {
67 enum redir_type {
68 R_REDIRECT, /* plain redirection */
69 R_WRAPPER, /* wrap with valgrind-internal code */
70 R_CLIENT_WRAPPER, /* wrap with client-side code */
71 } type;
72
73 const Char *from_lib; /* library qualifier pattern */
74 const Char *from_sym; /* symbol */
75 Addr from_addr; /* old addr */
76
njnd9109c62005-06-26 04:49:25 +000077 Addr to_addr; /* used for redirection -- new addr */
78 const FuncWrapper *wrapper; /* used for wrapping */
sewardjcbdddcf2005-03-10 23:23:45 +000079
njnd9109c62005-06-26 04:49:25 +000080 CodeRedirect *next; /* next pointer on unresolved list */
sewardjcbdddcf2005-03-10 23:23:45 +000081};
82
83static Char *straddr(void *p)
84{
85 static Char buf[16];
sewardjcbdddcf2005-03-10 23:23:45 +000086 VG_(sprintf)(buf, "%p", *(Addr *)p);
sewardjcbdddcf2005-03-10 23:23:45 +000087 return buf;
88}
89
njnd9109c62005-06-26 04:49:25 +000090static SkipList sk_resolved_redirs =
njn41f8e4a2005-06-25 20:13:05 +000091 VG_SKIPLIST_INIT(CodeRedirect, from_addr, VG_(cmp_Addr),
92 straddr, VG_AR_SYMTAB);
93
njnd9109c62005-06-26 04:49:25 +000094static CodeRedirect *unresolved_redirs = NULL;
sewardjcbdddcf2005-03-10 23:23:45 +000095
njn4f612c22005-06-25 20:22:43 +000096static Bool soname_matches(const Char *pattern, const Char* soname)
sewardjcbdddcf2005-03-10 23:23:45 +000097{
njn41f8e4a2005-06-25 20:13:05 +000098 // pattern must start with "soname:"
99 vg_assert(NULL != pattern);
100 vg_assert(0 == VG_(strncmp)(pattern, "soname:", 7));
sewardjcbdddcf2005-03-10 23:23:45 +0000101
njn4f612c22005-06-25 20:22:43 +0000102 if (NULL == soname)
sewardjcbdddcf2005-03-10 23:23:45 +0000103 return False;
104
njn4f612c22005-06-25 20:22:43 +0000105 return VG_(string_match)(pattern + 7, soname);
sewardjcbdddcf2005-03-10 23:23:45 +0000106}
107
njnd9109c62005-06-26 04:49:25 +0000108Bool VG_(is_resolved)(const CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000109{
110 return redir->from_addr != 0;
111}
112
njnd9109c62005-06-26 04:49:25 +0000113// Prepends redir to the unresolved list.
114static void add_redir_to_unresolved_list(CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000115{
njnd9109c62005-06-26 04:49:25 +0000116 redir->next = unresolved_redirs;
117 unresolved_redirs = redir;
sewardjcbdddcf2005-03-10 23:23:45 +0000118}
119
sewardjf9a82d12005-07-23 09:52:21 +0000120static void add_redir_to_resolved_list(CodeRedirect *redir, Bool need_discard)
tom748a1312005-04-02 15:53:01 +0000121{
njnd9109c62005-06-26 04:49:25 +0000122 vg_assert(redir->from_addr);
123
124 switch (redir->type) {
125 case R_REDIRECT: {
126 CodeRedirect* r;
127
128 TRACE_REDIR(" redir resolved (%s:%s=%p -> %p)",
129 redir->from_lib, redir->from_sym, redir->from_addr,
130 redir->to_addr);
131
132 vg_assert(redir->to_addr != 0);
tom748a1312005-04-02 15:53:01 +0000133
sewardjf9a82d12005-07-23 09:52:21 +0000134 if (need_discard) {
tom748a1312005-04-02 15:53:01 +0000135 /* For some given (from, to) redir, the "from" function got
sewardjf9a82d12005-07-23 09:52:21 +0000136 loaded before the .so containing "to" became available so
137 we need to discard any existing translations involving
138 the "from" function.
tom748a1312005-04-02 15:53:01 +0000139
140 Note, we only really need to discard the first bb of the
141 old entry point, and so we avoid the problem of having to
142 figure out how big that bb was -- since it is at least 1
143 byte of original code, we can just pass 1 as the original
144 size to invalidate_translations() and it will indeed get
145 rid of the translation.
146
147 Note, this is potentially expensive -- discarding
148 translations causes complete unchaining.
149 */
sewardjf9a82d12005-07-23 09:52:21 +0000150 TRACE_REDIR("Discarding translation due to redirect of already loaded function" );
njnd9109c62005-06-26 04:49:25 +0000151 TRACE_REDIR(" %s:%s(%p) -> %p)", redir->from_lib, redir->from_sym,
152 redir->from_addr, redir->to_addr );
tom748a1312005-04-02 15:53:01 +0000153 VG_(discard_translations)((Addr64)redir->from_addr, 1);
154 }
155
njnd9109c62005-06-26 04:49:25 +0000156 r = VG_(SkipList_Find_Exact)(&sk_resolved_redirs, &redir->from_addr);
tom748a1312005-04-02 15:53:01 +0000157
njnd9109c62005-06-26 04:49:25 +0000158 if (r == NULL) {
159 VG_(SkipList_Insert)(&sk_resolved_redirs, redir);
160 } else {
161 /* XXX leak redir */
162 TRACE_REDIR(" redir %s:%s:%p->%p duplicated\n",
163 redir->from_lib, redir->from_sym, redir->from_addr,
164 redir->to_addr);
tom748a1312005-04-02 15:53:01 +0000165 }
166 break;
njnd9109c62005-06-26 04:49:25 +0000167 }
tom748a1312005-04-02 15:53:01 +0000168
169 case R_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000170 TRACE_REDIR(" wrapper resolved (%s:%s=%p -> wrapper)",
171 redir->from_lib, redir->from_sym, redir->from_addr);
172
173 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000174
175 /* XXX redir leaked */
176 //VG_(wrap_function)(redir->from_addr, redir->wrapper);
177 break;
178
179 case R_CLIENT_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000180 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000181 VG_(core_panic)("not implemented");
182 break;
183 }
184}
185
njnd9109c62005-06-26 04:49:25 +0000186// Resolve a redir using si if possible. Returns True if it succeeded.
187static Bool resolve_redir_with_seginfo(CodeRedirect *redir, const SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000188{
njnd9109c62005-06-26 04:49:25 +0000189 Bool ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000190
191 vg_assert(si != NULL);
njnd9109c62005-06-26 04:49:25 +0000192 vg_assert(redir->from_addr == 0 );
193 vg_assert(redir->from_sym != NULL);
sewardjcbdddcf2005-03-10 23:23:45 +0000194
njnd9109c62005-06-26 04:49:25 +0000195 // Resolved if the soname matches and we find the symbol.
196 ok = soname_matches(redir->from_lib, VG_(seginfo_soname)(si));
197 if (ok) {
njn748ace42005-06-21 03:36:01 +0000198 redir->from_addr = VG_(reverse_search_one_symtab)(si, redir->from_sym);
njnd9109c62005-06-26 04:49:25 +0000199 ok = ( redir->from_addr == 0 ? False : True );
sewardjcbdddcf2005-03-10 23:23:45 +0000200 }
njnd9109c62005-06-26 04:49:25 +0000201 return ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000202}
203
njn2025cf92005-06-26 20:44:48 +0000204// Resolve a redir using any SegInfo if possible. This is called whenever
205// a new sym-to-addr redir is created. It covers the case where a
206// replacement function is loaded after its replacee.
njnd9109c62005-06-26 04:49:25 +0000207static Bool resolve_redir_with_existing_seginfos(CodeRedirect *redir)
njnbf7ca332005-05-14 17:11:06 +0000208{
209 const SegInfo *si;
210
njnd9109c62005-06-26 04:49:25 +0000211 for (si = VG_(next_seginfo)(NULL);
212 si != NULL;
213 si = VG_(next_seginfo)(si))
njnbf7ca332005-05-14 17:11:06 +0000214 {
njnd9109c62005-06-26 04:49:25 +0000215 if (resolve_redir_with_seginfo(redir, si))
njnbf7ca332005-05-14 17:11:06 +0000216 return True;
217 }
218 return False;
219}
220
njnd9109c62005-06-26 04:49:25 +0000221// Resolve as many unresolved redirs as possible with this SegInfo. This
njn2025cf92005-06-26 20:44:48 +0000222// should be called when a new SegInfo symtab is loaded. It covers the case
223// where a replacee function is loaded after its replacement function.
njnd9109c62005-06-26 04:49:25 +0000224void VG_(resolve_existing_redirs_with_seginfo)(SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000225{
njnd9109c62005-06-26 04:49:25 +0000226 CodeRedirect **prevp = &unresolved_redirs;
sewardjcbdddcf2005-03-10 23:23:45 +0000227 CodeRedirect *redir, *next;
228
njnd9109c62005-06-26 04:49:25 +0000229 TRACE_REDIR("Just loaded %s (soname=%s),",
230 VG_(seginfo_filename)(si), VG_(seginfo_soname)(si));
231 TRACE_REDIR(" resolving any unresolved redirs with it");
sewardjcbdddcf2005-03-10 23:23:45 +0000232
njnd9109c62005-06-26 04:49:25 +0000233 // Visit each unresolved redir - if it becomes resolved, then
234 // move it from the unresolved list to the resolved list.
235 for (redir = unresolved_redirs; redir != NULL; redir = next) {
sewardjcbdddcf2005-03-10 23:23:45 +0000236 next = redir->next;
237
njnd9109c62005-06-26 04:49:25 +0000238 if (resolve_redir_with_seginfo(redir, si)) {
sewardjcbdddcf2005-03-10 23:23:45 +0000239 *prevp = next;
240 redir->next = NULL;
sewardjf9a82d12005-07-23 09:52:21 +0000241 add_redir_to_resolved_list(redir, False);
sewardjcbdddcf2005-03-10 23:23:45 +0000242 } else
243 prevp = &redir->next;
244 }
sewardjcbdddcf2005-03-10 23:23:45 +0000245
njnd9109c62005-06-26 04:49:25 +0000246 TRACE_REDIR(" Finished resolving");
sewardjcbdddcf2005-03-10 23:23:45 +0000247}
248
tom748a1312005-04-02 15:53:01 +0000249/* Redirect a function at from_addr to a function at to_addr */
njn16eeb4e2005-06-16 03:56:58 +0000250__attribute__((unused)) // It is used, but not on all platforms...
njnd9109c62005-06-26 04:49:25 +0000251static void add_redirect_addr_to_addr( Addr from_addr, Addr to_addr )
tom748a1312005-04-02 15:53:01 +0000252{
njnd9109c62005-06-26 04:49:25 +0000253 CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redirs);
254
255 vg_assert(0 != from_addr && 0 != to_addr);
256
257 redir->type = R_REDIRECT;
258
259 redir->from_lib = NULL;
260 redir->from_sym = NULL;
261 redir->from_addr = from_addr;
262
263 redir->to_addr = to_addr;
264 redir->wrapper = 0;
265
266 TRACE_REDIR("REDIRECT addr to addr: %p to %p", from_addr, to_addr);
267
268 // This redirection is already resolved, put it straight in the list.
sewardjf9a82d12005-07-23 09:52:21 +0000269 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000270}
271
272/* Redirect a lib/symbol reference to a function at addr */
273static void add_redirect_sym_to_addr(
274 const Char *from_lib, const Char *from_sym, Addr to_addr
275)
276{
277 CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redirs);
278
279 vg_assert(from_lib && from_sym && 0 != to_addr);
280
281 redir->type = R_REDIRECT;
282 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
283 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
284 redir->from_addr = 0;
285 redir->to_addr = to_addr;
286 redir->wrapper = 0;
287
288 TRACE_REDIR("REDIR sym to addr: %s:%s to %p", from_lib, from_sym, to_addr);
289
290 // Check against all existing segments to see if this redirection
njn2025cf92005-06-26 20:44:48 +0000291 // can be resolved immediately (as will be the case when the replacement
292 // function is loaded after the replacee). Then add it to the
293 // appropriate list.
njnd9109c62005-06-26 04:49:25 +0000294 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000295 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000296 } else {
297 add_redir_to_unresolved_list(redir);
298 }
tom748a1312005-04-02 15:53:01 +0000299}
300
sewardjcbdddcf2005-03-10 23:23:45 +0000301CodeRedirect *VG_(add_wrapper)(const Char *from_lib, const Char *from_sym,
302 const FuncWrapper *wrapper)
303{
njnd9109c62005-06-26 04:49:25 +0000304 CodeRedirect *redir = VG_(SkipNode_Alloc)(&sk_resolved_redirs);
sewardjcbdddcf2005-03-10 23:23:45 +0000305
njnd9109c62005-06-26 04:49:25 +0000306 redir->type = R_WRAPPER;
njn136b83b2005-06-19 05:14:03 +0000307 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
308 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
sewardjcbdddcf2005-03-10 23:23:45 +0000309 redir->from_addr = 0;
njn136b83b2005-06-19 05:14:03 +0000310 redir->to_addr = 0;
311 redir->wrapper = wrapper;
sewardjcbdddcf2005-03-10 23:23:45 +0000312
njnd9109c62005-06-26 04:49:25 +0000313 TRACE_REDIR("REDIR sym to wrapper: %s:%s to (%p,%p)",
314 from_lib, from_sym, wrapper->before, wrapper->after);
315
316 // Check against all existing segments to see if this redirection
317 // can be resolved immediately. Then add it to the appropriate list.
318 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000319 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000320 } else {
321 add_redir_to_unresolved_list(redir);
sewardjcbdddcf2005-03-10 23:23:45 +0000322 }
323
324 return redir;
325}
326
sewardjcbdddcf2005-03-10 23:23:45 +0000327/* If address 'a' is being redirected, return the redirected-to
328 address. */
329Addr VG_(code_redirect)(Addr a)
330{
331 CodeRedirect* r;
332
njnd9109c62005-06-26 04:49:25 +0000333 r = VG_(SkipList_Find_Exact)(&sk_resolved_redirs, &a);
sewardjcbdddcf2005-03-10 23:23:45 +0000334 if (r == NULL)
335 return a;
336
337 vg_assert(r->to_addr != 0);
338
339 return r->to_addr;
340}
341
342void VG_(setup_code_redirect_table) ( void )
343{
njnd1af0032005-05-29 17:01:48 +0000344#if defined(VGP_x86_linux)
345 /* Redirect _dl_sysinfo_int80, which is glibc's default system call
sewardjb9bce632005-06-21 01:41:34 +0000346 routine, to our copy so that the special sysinfo unwind hack in
347 m_stacktrace.c will kick in. */
348 add_redirect_sym_to_addr(
349 "soname:ld-linux.so.2", "_dl_sysinfo_int80",
350 (Addr)&VG_(x86_linux_REDIR_FOR__dl_sysinfo_int80)
351 );
352
njnd1af0032005-05-29 17:01:48 +0000353#elif defined(VGP_amd64_linux)
sewardjb9bce632005-06-21 01:41:34 +0000354
njnd1af0032005-05-29 17:01:48 +0000355 /* Redirect vsyscalls to local versions */
sewardjb9bce632005-06-21 01:41:34 +0000356 add_redirect_addr_to_addr(
357 0xFFFFFFFFFF600000ULL,
358 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
359 );
360
361 add_redirect_addr_to_addr(
tomf5db3b62005-06-21 13:26:17 +0000362 0xFFFFFFFFFF600400ULL,
sewardjb9bce632005-06-21 01:41:34 +0000363 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
364 );
365
cerion85665ca2005-06-20 15:51:07 +0000366#elif defined(VGP_ppc32_linux)
sewardjb9bce632005-06-21 01:41:34 +0000367
368 //CAB: TODO
369
njnd1af0032005-05-29 17:01:48 +0000370#else
371# error Unknown platform
372#endif
373}
sewardjcbdddcf2005-03-10 23:23:45 +0000374
njn16eeb4e2005-06-16 03:56:58 +0000375/* Z-decode a symbol into library:func form, eg
376
377 _vgi_libcZdsoZd6__ZdlPv --> libc.so.6:_ZdlPv
378
379 Uses the Z-encoding scheme described in pub_core_redir.h.
380 Returns True if demangle OK, False otherwise.
381*/
382static Bool Z_decode(const Char* symbol, Char* result, Int nbytes)
383{
384# define EMIT(ch) \
385 do { \
386 if (j >= nbytes) \
387 result[j-1] = 0; \
388 else \
389 result[j++] = ch; \
390 } while (0)
391
392 Bool error = False;
393 Int i, j = 0;
394 Int len = VG_(strlen)(symbol);
395 if (0) VG_(printf)("idm: %s\n", symbol);
396
397 i = VG_REPLACE_FUNCTION_PREFIX_LEN;
398
399 /* Chew though the Z-encoded soname part. */
400 while (True) {
401
402 if (i >= len)
403 break;
404
405 if (symbol[i] == '_')
406 /* We found the underscore following the Z-encoded soname.
407 Just copy the rest literally. */
408 break;
409
410 if (symbol[i] != 'Z') {
411 EMIT(symbol[i]);
412 i++;
413 continue;
414 }
415
416 /* We've got a Z-escape. Act accordingly. */
417 i++;
418 if (i >= len) {
419 /* Hmm, Z right at the end. Something's wrong. */
420 error = True;
421 EMIT('Z');
422 break;
423 }
424 switch (symbol[i]) {
425 case 'a': EMIT('*'); break;
426 case 'p': EMIT('+'); break;
427 case 'c': EMIT(':'); break;
428 case 'd': EMIT('.'); break;
429 case 'u': EMIT('_'); break;
430 case 'h': EMIT('-'); break;
431 case 's': EMIT(' '); break;
432 case 'Z': EMIT('Z'); break;
433 default: error = True; EMIT('Z'); EMIT(symbol[i]); break;
434 }
435 i++;
436 }
437
438 if (error || i >= len || symbol[i] != '_') {
439 /* Something's wrong. Give up. */
440 VG_(message)(Vg_UserMsg, "intercept: error demangling: %s", symbol);
441 EMIT(0);
442 return False;
443 }
444
445 /* Copy the rest of the string verbatim. */
446 i++;
447 EMIT(':');
448 while (True) {
449 if (i >= len)
450 break;
451 EMIT(symbol[i]);
452 i++;
453 }
454
455 EMIT(0);
456 if (0) VG_(printf)("%s\n", result);
457 return True;
458
459# undef EMIT
460}
461
462// Nb: this can change the string pointed to by 'symbol'.
463static void handle_replacement_function( Char* symbol, Addr addr )
464{
465 Bool ok;
466 Int len = VG_(strlen)(symbol) + 1 - VG_REPLACE_FUNCTION_PREFIX_LEN;
467 Char *lib = VG_(arena_malloc)(VG_AR_SYMTAB, len+8);
468 Char *func;
469
470 // Put "soname:" at the start of lib
471 VG_(strcpy)(lib, "soname:");
472
473 ok = Z_decode(symbol, lib+7, len);
474 if (ok) {
475 // lib is "soname:<libname>:<fnname>". Split the string at the 2nd ':'.
476 func = lib + VG_(strlen)(lib)-1;
477 while(*func != ':') func--;
478 *func = '\0';
479 func++; // Move past the '\0'
480
481 // Now lib is "soname:<libname>" and func is "<fnname>".
482 if (0) VG_(printf)("lib A%sZ, func A%sZ\n", lib, func);
483 add_redirect_sym_to_addr(lib, func, addr);
484
485 // Overwrite the given Z-encoded name with just the fnname.
486 VG_(strcpy)(symbol, func);
487 }
488
489 VG_(arena_free)(VG_AR_SYMTAB, lib);
490}
491
njnbc6d84d2005-06-19 18:58:03 +0000492static Addr __libc_freeres_wrapper = 0;
493
494Addr VG_(get_libc_freeres_wrapper)(void)
495{
496 return __libc_freeres_wrapper;
497}
498
njn16eeb4e2005-06-16 03:56:58 +0000499// This is specifically for stringifying VG_(x) function names. We
500// need to do two macroexpansions to get the VG_ macro expanded before
501// stringifying.
502#define _STR(x) #x
503#define STR(x) _STR(x)
504
505static void handle_load_notifier( Char* symbol, Addr addr )
506{
507 if (VG_(strcmp)(symbol, STR(VG_NOTIFY_ON_LOAD(freeres))) == 0)
njnbc6d84d2005-06-19 18:58:03 +0000508 __libc_freeres_wrapper = addr;
njn16eeb4e2005-06-16 03:56:58 +0000509// else if (VG_(strcmp)(symbol, STR(VG_WRAPPER(pthread_startfunc_wrapper))) == 0)
510// VG_(pthread_startfunc_wrapper)((Addr)(si->offset + sym->st_value));
511 else
512 vg_assert2(0, "unrecognised load notification function: %s", symbol);
513}
514
515static Bool is_replacement_function(Char* s)
516{
517 return (0 == VG_(strncmp)(s,
518 VG_REPLACE_FUNCTION_PREFIX,
519 VG_REPLACE_FUNCTION_PREFIX_LEN));
520}
521
522static Bool is_load_notifier(Char* s)
523{
524 return (0 == VG_(strncmp)(s,
525 VG_NOTIFY_ON_LOAD_PREFIX,
526 VG_NOTIFY_ON_LOAD_PREFIX_LEN));
527}
528
529// Call this for each symbol loaded. It determines if we need to do
530// anything special with it. It can modify 'symbol' in-place.
531void VG_(maybe_redir_or_notify) ( Char* symbol, Addr addr )
532{
533 if (is_replacement_function(symbol))
534 handle_replacement_function(symbol, addr);
535 else
536 if (is_load_notifier(symbol))
537 handle_load_notifier(symbol, addr);
538}
539
540
sewardjcbdddcf2005-03-10 23:23:45 +0000541//:: /*------------------------------------------------------------*/
542//:: /*--- General function wrapping. ---*/
543//:: /*------------------------------------------------------------*/
544//::
545//:: /*
546//:: TODO:
547//:: - hook into the symtab machinery
548//:: - client-side wrappers?
549//:: - better interfaces for before() functions to get to arguments
550//:: - handle munmap of code (dlclose())
551//:: - handle thread exit
552//:: - handle longjmp
553//:: */
554//:: struct callkey {
555//:: ThreadId tid; /* calling thread */
556//:: Addr esp; /* address of args on stack */
557//:: Addr eip; /* return address */
558//:: };
559//::
560//:: struct call_instance {
561//:: struct callkey key;
562//::
563//:: const FuncWrapper *wrapper;
564//:: void *nonce;
565//:: };
566//::
567//:: static inline Addr addrcmp(Addr a, Addr b)
568//:: {
569//:: if (a < b)
570//:: return -1;
571//:: else if (a > b)
572//:: return 1;
573//:: else
574//:: return 0;
575//:: }
576//::
577//:: static inline Int cmp(UInt a, UInt b)
578//:: {
579//:: if (a < b)
580//:: return -1;
581//:: else if (a > b)
582//:: return 1;
583//:: else
584//:: return 0;
585//:: }
586//::
587//:: static Int keycmp(const void *pa, const void *pb)
588//:: {
589//:: const struct callkey *a = (const struct callkey *)pa;
590//:: const struct callkey *b = (const struct callkey *)pb;
591//:: Int ret;
592//::
593//:: if ((ret = cmp(a->tid, b->tid)))
594//:: return ret;
595//::
596//:: if ((ret = addrcmp(a->esp, b->esp)))
597//:: return ret;
598//::
599//:: return addrcmp(a->eip, b->eip);
600//:: }
601//::
602//:: /* List of wrapped call invocations which are currently active */
njnbe91aae2005-03-27 01:42:41 +0000603//:: static SkipList wrapped_frames = VG_SKIPLIST_INIT(struct call_instance, key, keycmp,
sewardjcbdddcf2005-03-10 23:23:45 +0000604//:: NULL, VG_AR_SYMTAB);
605//::
606//:: static struct call_instance *find_call(Addr retaddr, Addr argsp, ThreadId tid)
607//:: {
608//:: struct callkey key = { tid, argsp, retaddr };
609//::
610//:: return VG_(SkipList_Find_Exact)(&wrapped_frames, &key);
611//:: }
612//::
613//:: static void wrapper_return(Addr retaddr);
614//::
615//:: /* Called from generated code via helper */
616//:: void VG_(wrap_before)(ThreadState *tst, const FuncWrapper *wrapper)
617//:: {
njnaf839f52005-06-23 03:27:57 +0000618//:: Addr retaddr = VG_RETADDR(tst->arch);
619//:: Addr argp = (Addr)&VG_FUNC_ARG(tst->arch, 0);
sewardjcbdddcf2005-03-10 23:23:45 +0000620//:: void *nonce = NULL;
621//:: Bool mf = VG_(my_fault);
622//:: VG_(my_fault) = True;
623//::
624//:: if (wrapper->before) {
njnaf839f52005-06-23 03:27:57 +0000625//:: va_list args = VG_VA_LIST(tst->arch);
sewardjcbdddcf2005-03-10 23:23:45 +0000626//:: nonce = (*wrapper->before)(args);
627//:: }
628//::
629//:: if (wrapper->after) {
630//:: /* If there's an after function, make sure it gets called */
631//:: struct call_instance *call;
632//::
633//:: call = find_call(retaddr, argp, tst->tid);
634//::
635//:: if (call != NULL) {
636//:: /* Found a stale outstanding call; clean it up and recycle
637//:: the structure */
638//:: if (call->wrapper->after)
639//:: (*call->wrapper->after)(call->nonce, RT_LONGJMP, 0);
640//:: } else {
641//:: call = VG_(SkipNode_Alloc)(&wrapped_frames);
642//::
643//:: call->key.tid = tst->tid;
644//:: call->key.esp = argp;
645//:: call->key.eip = retaddr;
646//::
647//:: VG_(SkipList_Insert)(&wrapped_frames, call);
648//::
649//:: wrapper_return(retaddr);
650//:: }
651//::
652//:: call->wrapper = wrapper;
653//:: call->nonce = nonce;
654//:: } else
655//:: vg_assert(nonce == NULL);
656//::
657//:: VG_(my_fault) = mf;
658//:: }
659//::
660//:: /* Called from generated code via helper */
661//:: void VG_(wrap_after)(ThreadState *tst)
662//:: {
njnaf839f52005-06-23 03:27:57 +0000663//:: Addr EIP = VG_INSTR_PTR(tst->arch); /* instruction after call */
664//:: Addr ESP = VG_STACK_PTR(tst->arch); /* pointer to args */
665//:: Word ret = VG_RETVAL(tst->arch); /* return value */
sewardjcbdddcf2005-03-10 23:23:45 +0000666//:: struct call_instance *call;
667//:: Bool mf = VG_(my_fault);
668//::
669//:: VG_(my_fault) = True;
670//:: call = find_call(EIP, ESP, tst->tid);
671//::
672//:: if (0)
673//:: VG_(printf)("wrap_after(%p,%p,%d) -> %p\n", EIP, ESP, tst->tid, call);
674//::
675//:: if (call != NULL) {
676//:: if (call->wrapper->after)
677//:: (*call->wrapper->after)(call->nonce, RT_RETURN, ret);
678//::
679//:: VG_(SkipList_Remove)(&wrapped_frames, &call->key);
680//:: VG_(SkipNode_Free)(&wrapped_frames, call);
681//:: }
682//:: VG_(my_fault) = mf;
683//:: }
684//::
685//::
686//:: struct wrapped_function {
687//:: Addr eip; /* eip of function entrypoint */
688//:: const FuncWrapper *wrapper;
689//:: };
690//::
691//:: struct wrapper_return {
692//:: Addr eip; /* return address */
693//:: };
694//::
695//:: /* A mapping from eip of wrapped function entrypoints to actual wrappers */
njnbe91aae2005-03-27 01:42:41 +0000696//:: static SkipList wrapped_functions = VG_SKIPLIST_INIT(struct wrapped_function, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000697//:: NULL, VG_AR_SYMTAB);
698//::
699//:: /* A set of EIPs which are return addresses for wrapped functions */
njnbe91aae2005-03-27 01:42:41 +0000700//:: static SkipList wrapper_returns = VG_SKIPLIST_INIT(struct wrapper_return, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000701//:: NULL, VG_AR_SYMTAB);
702//::
703//:: /* Wrap function starting at eip */
704//:: void VG_(wrap_function)(Addr eip, const FuncWrapper *wrapper)
705//:: {
706//:: struct wrapped_function *func;
707//::
708//:: if (0)
709//:: VG_(printf)("wrapping %p with (%p,%p)\n", eip, wrapper->before, wrapper->after);
710//::
711//:: func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
712//::
713//:: if (func == NULL) {
714//:: func = VG_(SkipNode_Alloc)(&wrapped_functions);
715//:: VG_(invalidate_translations)(eip, 1, True);
716//::
717//:: func->eip = eip;
718//:: VG_(SkipList_Insert)(&wrapped_functions, func);
719//:: }
720//::
721//:: func->wrapper = wrapper;
722//:: }
723//::
724//:: const FuncWrapper *VG_(is_wrapped)(Addr eip)
725//:: {
726//:: struct wrapped_function *func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
727//::
728//:: if (func)
729//:: return func->wrapper;
730//:: return NULL;
731//:: }
732//::
733//:: Bool VG_(is_wrapper_return)(Addr eip)
734//:: {
735//:: struct wrapper_return *ret = VG_(SkipList_Find_Exact)(&wrapper_returns, &eip);
736//::
737//:: return ret != NULL;
738//:: }
739//::
740//:: /* Mark eip as being the return address of a wrapper, so that the
741//:: codegen will generate the appropriate call. */
742//:: void wrapper_return(Addr eip)
743//:: {
744//:: struct wrapper_return *ret;
745//::
746//:: if (VG_(is_wrapper_return)(eip))
747//:: return;
748//::
749//:: VG_(invalidate_translations)(eip, 1, True);
750//::
751//:: ret = VG_(SkipNode_Alloc)(&wrapper_returns);
752//:: ret->eip = eip;
753//::
754//:: VG_(SkipList_Insert)(&wrapper_returns, ret);
755//:: }