blob: 56a89e499d4db70c870a697318a73cbca63e1874 [file] [log] [blame]
njnc0ae7052005-08-25 22:55:19 +00001
sewardjcbdddcf2005-03-10 23:23:45 +00002/*--------------------------------------------------------------------*/
njnc0ae7052005-08-25 22:55:19 +00003/*--- Function replacement and wrapping. m_redir.c ---*/
sewardjcbdddcf2005-03-10 23:23:45 +00004/*--------------------------------------------------------------------*/
5
6/*
njnc0ae7052005-08-25 22:55:19 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjcbdddcf2005-03-10 23:23:45 +00009
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"
sewardj45f4e7c2005-09-27 19:20:21 +000034#include "pub_core_debuglog.h"
njn88c51482005-06-25 20:49:33 +000035#include "pub_core_debuginfo.h"
njn97405b22005-06-02 03:39:33 +000036#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000037#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_core_libcprint.h"
njnaf1d7df2005-06-11 01:31:52 +000039#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000040#include "pub_core_options.h"
njn8a96ec52005-10-15 15:48:52 +000041#include "pub_core_oset.h"
njnd1af0032005-05-29 17:01:48 +000042#include "pub_core_redir.h"
njna7598f62005-06-18 03:27:58 +000043#include "pub_core_trampoline.h"
njn8bddf582005-05-13 23:40:55 +000044#include "pub_core_transtab.h"
sewardj461c69d2005-11-17 20:15:04 +000045#include "pub_core_tooliface.h" // VG_(needs).malloc_replacement
46
sewardj55f9d1a2005-04-25 11:11:44 +000047
sewardjcbdddcf2005-03-10 23:23:45 +000048/*------------------------------------------------------------*/
49/*--- General purpose redirection. ---*/
50/*------------------------------------------------------------*/
51
njnd9109c62005-06-26 04:49:25 +000052#define TRACE_REDIR(format, args...) \
53 if (VG_(clo_trace_redir)) { VG_(message)(Vg_DebugMsg, format, ## args); }
54
sewardjcbdddcf2005-03-10 23:23:45 +000055/*
56 wraps and redirections, indexed by from_addr
57
58 Redirection and wrapping are two distinct mechanisms which Valgrind
59 can use to change the client's control flow.
60
61 Redirection intercepts a call to a client function, and re-points it
62 to a new piece of code (presumably functionally equivalent). The
63 original code is never run.
64
65 Wrapping does call the client's original code, but calls "before"
66 and "after" functions which can inspect (and perhaps modify) the
67 function's arguments and return value.
68 */
69struct _CodeRedirect {
njn8a96ec52005-10-15 15:48:52 +000070 Addr from_addr; /* old addr -- MUST BE THE FIRST WORD! */
71
sewardjcbdddcf2005-03-10 23:23:45 +000072 enum redir_type {
73 R_REDIRECT, /* plain redirection */
74 R_WRAPPER, /* wrap with valgrind-internal code */
75 R_CLIENT_WRAPPER, /* wrap with client-side code */
76 } type;
77
78 const Char *from_lib; /* library qualifier pattern */
79 const Char *from_sym; /* symbol */
sewardjcbdddcf2005-03-10 23:23:45 +000080
njnd9109c62005-06-26 04:49:25 +000081 Addr to_addr; /* used for redirection -- new addr */
82 const FuncWrapper *wrapper; /* used for wrapping */
sewardjcbdddcf2005-03-10 23:23:45 +000083
njnd9109c62005-06-26 04:49:25 +000084 CodeRedirect *next; /* next pointer on unresolved list */
sewardjcbdddcf2005-03-10 23:23:45 +000085};
86
njn8a96ec52005-10-15 15:48:52 +000087static OSet* resolved_redirs;
sewardjcbdddcf2005-03-10 23:23:45 +000088
njn8a96ec52005-10-15 15:48:52 +000089// We use a linked list here rather than an OSet, because we want to
90// traverse it and possibly remove elements as we look at them. OSet
91// doesn't support this very well.
njnd9109c62005-06-26 04:49:25 +000092static CodeRedirect *unresolved_redirs = NULL;
sewardjcbdddcf2005-03-10 23:23:45 +000093
njn4f612c22005-06-25 20:22:43 +000094static Bool soname_matches(const Char *pattern, const Char* soname)
sewardjcbdddcf2005-03-10 23:23:45 +000095{
njn41f8e4a2005-06-25 20:13:05 +000096 // pattern must start with "soname:"
97 vg_assert(NULL != pattern);
98 vg_assert(0 == VG_(strncmp)(pattern, "soname:", 7));
sewardjcbdddcf2005-03-10 23:23:45 +000099
njn4f612c22005-06-25 20:22:43 +0000100 if (NULL == soname)
sewardjcbdddcf2005-03-10 23:23:45 +0000101 return False;
102
njn4f612c22005-06-25 20:22:43 +0000103 return VG_(string_match)(pattern + 7, soname);
sewardjcbdddcf2005-03-10 23:23:45 +0000104}
105
njnd9109c62005-06-26 04:49:25 +0000106Bool VG_(is_resolved)(const CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000107{
108 return redir->from_addr != 0;
109}
110
njnd9109c62005-06-26 04:49:25 +0000111// Prepends redir to the unresolved list.
112static void add_redir_to_unresolved_list(CodeRedirect *redir)
sewardjcbdddcf2005-03-10 23:23:45 +0000113{
njnd9109c62005-06-26 04:49:25 +0000114 redir->next = unresolved_redirs;
115 unresolved_redirs = redir;
sewardjcbdddcf2005-03-10 23:23:45 +0000116}
117
sewardjf9a82d12005-07-23 09:52:21 +0000118static void add_redir_to_resolved_list(CodeRedirect *redir, Bool need_discard)
tom748a1312005-04-02 15:53:01 +0000119{
njnd9109c62005-06-26 04:49:25 +0000120 vg_assert(redir->from_addr);
121
122 switch (redir->type) {
123 case R_REDIRECT: {
njnd9109c62005-06-26 04:49:25 +0000124 TRACE_REDIR(" redir resolved (%s:%s=%p -> %p)",
125 redir->from_lib, redir->from_sym, redir->from_addr,
126 redir->to_addr);
127
128 vg_assert(redir->to_addr != 0);
tom748a1312005-04-02 15:53:01 +0000129
sewardjf9a82d12005-07-23 09:52:21 +0000130 if (need_discard) {
tom748a1312005-04-02 15:53:01 +0000131 /* For some given (from, to) redir, the "from" function got
sewardjf9a82d12005-07-23 09:52:21 +0000132 loaded before the .so containing "to" became available so
133 we need to discard any existing translations involving
134 the "from" function.
tom748a1312005-04-02 15:53:01 +0000135
136 Note, we only really need to discard the first bb of the
137 old entry point, and so we avoid the problem of having to
138 figure out how big that bb was -- since it is at least 1
139 byte of original code, we can just pass 1 as the original
140 size to invalidate_translations() and it will indeed get
141 rid of the translation.
142
143 Note, this is potentially expensive -- discarding
sewardjeccb2d82005-07-23 11:36:03 +0000144 translations requires a complete search through all of
145 them.
tom748a1312005-04-02 15:53:01 +0000146 */
sewardjf9a82d12005-07-23 09:52:21 +0000147 TRACE_REDIR("Discarding translation due to redirect of already loaded function" );
njnd9109c62005-06-26 04:49:25 +0000148 TRACE_REDIR(" %s:%s(%p) -> %p)", redir->from_lib, redir->from_sym,
149 redir->from_addr, redir->to_addr );
sewardj45f4e7c2005-09-27 19:20:21 +0000150 VG_(discard_translations)((Addr64)redir->from_addr, 1,
151 "add_redir_to_resolved_list");
tom748a1312005-04-02 15:53:01 +0000152 }
153
njn8a96ec52005-10-15 15:48:52 +0000154 // This entails a possible double OSet lookup -- one for Contains(),
155 // one for Insert(). If we had OSet_InsertIfNonDup() we could do it
156 // with one lookup.
157 if ( ! VG_(OSet_Contains)(resolved_redirs, &redir->from_addr) ) {
158 VG_(OSet_Insert)(resolved_redirs, redir);
njnd9109c62005-06-26 04:49:25 +0000159 } else {
njnd9109c62005-06-26 04:49:25 +0000160 TRACE_REDIR(" redir %s:%s:%p->%p duplicated\n",
161 redir->from_lib, redir->from_sym, redir->from_addr,
162 redir->to_addr);
sewardj6a443b22005-11-20 19:37:54 +0000163 // jrs 20 Nov 05: causes this: m_mallocfree.c:170
164 // (mk_plain_bszB): Assertion 'bszB != 0' failed.
165 // Perhaps it is an invalid free? Disable for now
166 // XXX leak?
167 //VG_(arena_free)(VG_AR_SYMTAB, redir);
tom748a1312005-04-02 15:53:01 +0000168 }
169 break;
njnd9109c62005-06-26 04:49:25 +0000170 }
tom748a1312005-04-02 15:53:01 +0000171
172 case R_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000173 TRACE_REDIR(" wrapper resolved (%s:%s=%p -> wrapper)",
174 redir->from_lib, redir->from_sym, redir->from_addr);
175
176 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000177
178 /* XXX redir leaked */
179 //VG_(wrap_function)(redir->from_addr, redir->wrapper);
180 break;
181
182 case R_CLIENT_WRAPPER:
njnd9109c62005-06-26 04:49:25 +0000183 vg_assert(redir->wrapper);
tom748a1312005-04-02 15:53:01 +0000184 VG_(core_panic)("not implemented");
185 break;
186 }
187}
188
njnd9109c62005-06-26 04:49:25 +0000189// Resolve a redir using si if possible. Returns True if it succeeded.
190static Bool resolve_redir_with_seginfo(CodeRedirect *redir, const SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000191{
njnd9109c62005-06-26 04:49:25 +0000192 Bool ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000193
194 vg_assert(si != NULL);
njnd9109c62005-06-26 04:49:25 +0000195 vg_assert(redir->from_addr == 0 );
196 vg_assert(redir->from_sym != NULL);
sewardjcbdddcf2005-03-10 23:23:45 +0000197
njnd9109c62005-06-26 04:49:25 +0000198 // Resolved if the soname matches and we find the symbol.
199 ok = soname_matches(redir->from_lib, VG_(seginfo_soname)(si));
200 if (ok) {
njn748ace42005-06-21 03:36:01 +0000201 redir->from_addr = VG_(reverse_search_one_symtab)(si, redir->from_sym);
njnd9109c62005-06-26 04:49:25 +0000202 ok = ( redir->from_addr == 0 ? False : True );
sewardjcbdddcf2005-03-10 23:23:45 +0000203 }
njnd9109c62005-06-26 04:49:25 +0000204 return ok;
sewardjcbdddcf2005-03-10 23:23:45 +0000205}
206
njn2025cf92005-06-26 20:44:48 +0000207// Resolve a redir using any SegInfo if possible. This is called whenever
208// a new sym-to-addr redir is created. It covers the case where a
209// replacement function is loaded after its replacee.
njnd9109c62005-06-26 04:49:25 +0000210static Bool resolve_redir_with_existing_seginfos(CodeRedirect *redir)
njnbf7ca332005-05-14 17:11:06 +0000211{
212 const SegInfo *si;
213
njnd9109c62005-06-26 04:49:25 +0000214 for (si = VG_(next_seginfo)(NULL);
215 si != NULL;
216 si = VG_(next_seginfo)(si))
njnbf7ca332005-05-14 17:11:06 +0000217 {
njnd9109c62005-06-26 04:49:25 +0000218 if (resolve_redir_with_seginfo(redir, si))
njnbf7ca332005-05-14 17:11:06 +0000219 return True;
220 }
221 return False;
222}
223
njnd9109c62005-06-26 04:49:25 +0000224// Resolve as many unresolved redirs as possible with this SegInfo. This
njn2025cf92005-06-26 20:44:48 +0000225// should be called when a new SegInfo symtab is loaded. It covers the case
226// where a replacee function is loaded after its replacement function.
njnd9109c62005-06-26 04:49:25 +0000227void VG_(resolve_existing_redirs_with_seginfo)(SegInfo *si)
sewardjcbdddcf2005-03-10 23:23:45 +0000228{
njnd9109c62005-06-26 04:49:25 +0000229 CodeRedirect **prevp = &unresolved_redirs;
sewardjcbdddcf2005-03-10 23:23:45 +0000230 CodeRedirect *redir, *next;
231
njnd9109c62005-06-26 04:49:25 +0000232 TRACE_REDIR("Just loaded %s (soname=%s),",
233 VG_(seginfo_filename)(si), VG_(seginfo_soname)(si));
234 TRACE_REDIR(" resolving any unresolved redirs with it");
sewardjcbdddcf2005-03-10 23:23:45 +0000235
njnd9109c62005-06-26 04:49:25 +0000236 // Visit each unresolved redir - if it becomes resolved, then
237 // move it from the unresolved list to the resolved list.
238 for (redir = unresolved_redirs; redir != NULL; redir = next) {
sewardjcbdddcf2005-03-10 23:23:45 +0000239 next = redir->next;
240
njnd9109c62005-06-26 04:49:25 +0000241 if (resolve_redir_with_seginfo(redir, si)) {
sewardjcbdddcf2005-03-10 23:23:45 +0000242 *prevp = next;
243 redir->next = NULL;
sewardjf9a82d12005-07-23 09:52:21 +0000244 add_redir_to_resolved_list(redir, False);
sewardjcbdddcf2005-03-10 23:23:45 +0000245 } else
246 prevp = &redir->next;
247 }
sewardjcbdddcf2005-03-10 23:23:45 +0000248
njnd9109c62005-06-26 04:49:25 +0000249 TRACE_REDIR(" Finished resolving");
sewardjcbdddcf2005-03-10 23:23:45 +0000250}
251
tom748a1312005-04-02 15:53:01 +0000252/* Redirect a function at from_addr to a function at to_addr */
njn16eeb4e2005-06-16 03:56:58 +0000253__attribute__((unused)) // It is used, but not on all platforms...
njnd9109c62005-06-26 04:49:25 +0000254static void add_redirect_addr_to_addr( Addr from_addr, Addr to_addr )
tom748a1312005-04-02 15:53:01 +0000255{
njn8a96ec52005-10-15 15:48:52 +0000256 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
257 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000258 vg_assert(0 != from_addr && 0 != to_addr);
259
260 redir->type = R_REDIRECT;
261
262 redir->from_lib = NULL;
263 redir->from_sym = NULL;
264 redir->from_addr = from_addr;
265
266 redir->to_addr = to_addr;
267 redir->wrapper = 0;
268
269 TRACE_REDIR("REDIRECT addr to addr: %p to %p", from_addr, to_addr);
270
271 // This redirection is already resolved, put it straight in the list.
sewardjf9a82d12005-07-23 09:52:21 +0000272 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000273}
274
275/* Redirect a lib/symbol reference to a function at addr */
276static void add_redirect_sym_to_addr(
277 const Char *from_lib, const Char *from_sym, Addr to_addr
278)
279{
njn8a96ec52005-10-15 15:48:52 +0000280 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
281 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000282 vg_assert(from_lib && from_sym && 0 != to_addr);
283
284 redir->type = R_REDIRECT;
285 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
286 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
287 redir->from_addr = 0;
288 redir->to_addr = to_addr;
289 redir->wrapper = 0;
290
291 TRACE_REDIR("REDIR sym to addr: %s:%s to %p", from_lib, from_sym, to_addr);
292
293 // Check against all existing segments to see if this redirection
njn2025cf92005-06-26 20:44:48 +0000294 // can be resolved immediately (as will be the case when the replacement
295 // function is loaded after the replacee). Then add it to the
296 // appropriate list.
njnd9109c62005-06-26 04:49:25 +0000297 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000298 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000299 } else {
300 add_redir_to_unresolved_list(redir);
301 }
tom748a1312005-04-02 15:53:01 +0000302}
303
sewardjcbdddcf2005-03-10 23:23:45 +0000304CodeRedirect *VG_(add_wrapper)(const Char *from_lib, const Char *from_sym,
305 const FuncWrapper *wrapper)
306{
njn8a96ec52005-10-15 15:48:52 +0000307 CodeRedirect* redir = VG_(OSet_AllocNode)(resolved_redirs,
308 sizeof(CodeRedirect));
njnd9109c62005-06-26 04:49:25 +0000309 redir->type = R_WRAPPER;
njn136b83b2005-06-19 05:14:03 +0000310 redir->from_lib = VG_(arena_strdup)(VG_AR_SYMTAB, from_lib);
311 redir->from_sym = VG_(arena_strdup)(VG_AR_SYMTAB, from_sym);
sewardjcbdddcf2005-03-10 23:23:45 +0000312 redir->from_addr = 0;
njn136b83b2005-06-19 05:14:03 +0000313 redir->to_addr = 0;
314 redir->wrapper = wrapper;
sewardjcbdddcf2005-03-10 23:23:45 +0000315
njnd9109c62005-06-26 04:49:25 +0000316 TRACE_REDIR("REDIR sym to wrapper: %s:%s to (%p,%p)",
317 from_lib, from_sym, wrapper->before, wrapper->after);
318
319 // Check against all existing segments to see if this redirection
320 // can be resolved immediately. Then add it to the appropriate list.
321 if (resolve_redir_with_existing_seginfos(redir)) {
sewardjf9a82d12005-07-23 09:52:21 +0000322 add_redir_to_resolved_list(redir, True);
njnd9109c62005-06-26 04:49:25 +0000323 } else {
324 add_redir_to_unresolved_list(redir);
sewardjcbdddcf2005-03-10 23:23:45 +0000325 }
326
327 return redir;
328}
329
sewardjcbdddcf2005-03-10 23:23:45 +0000330/* If address 'a' is being redirected, return the redirected-to
331 address. */
332Addr VG_(code_redirect)(Addr a)
333{
njn8a96ec52005-10-15 15:48:52 +0000334 CodeRedirect* r = VG_(OSet_Lookup)(resolved_redirs, &a);
sewardjcbdddcf2005-03-10 23:23:45 +0000335 if (r == NULL)
336 return a;
337
338 vg_assert(r->to_addr != 0);
339
340 return r->to_addr;
341}
342
njn8a96ec52005-10-15 15:48:52 +0000343static void* symtab_alloc(SizeT n)
344{
345 return VG_(arena_malloc)(VG_AR_SYMTAB, n);
346}
347
348static void symtab_free(void* p)
349{
350 return VG_(arena_free)(VG_AR_SYMTAB, p);
351}
352
sewardjcbdddcf2005-03-10 23:23:45 +0000353void VG_(setup_code_redirect_table) ( void )
354{
njn8a96ec52005-10-15 15:48:52 +0000355 // Initialise resolved_redirs list.
356 resolved_redirs = VG_(OSet_Create)(offsetof(CodeRedirect, from_addr),
357 NULL, // Use fast comparison
358 symtab_alloc,
359 symtab_free);
360
njnd1af0032005-05-29 17:01:48 +0000361#if defined(VGP_x86_linux)
362 /* Redirect _dl_sysinfo_int80, which is glibc's default system call
sewardjb9bce632005-06-21 01:41:34 +0000363 routine, to our copy so that the special sysinfo unwind hack in
364 m_stacktrace.c will kick in. */
365 add_redirect_sym_to_addr(
366 "soname:ld-linux.so.2", "_dl_sysinfo_int80",
367 (Addr)&VG_(x86_linux_REDIR_FOR__dl_sysinfo_int80)
368 );
sewardj6a443b22005-11-20 19:37:54 +0000369 /* If we're using memcheck, use this intercept right from the
370 start, otherwise ld.so (glibc-2.3.5) makes a lot of noise. */
371 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
372 add_redirect_sym_to_addr(
373 "soname:ld-linux.so.2", "index",
374 (Addr)&VG_(x86_linux_REDIR_FOR_index)
375 );
376 }
sewardjb9bce632005-06-21 01:41:34 +0000377
njnd1af0032005-05-29 17:01:48 +0000378#elif defined(VGP_amd64_linux)
sewardjb9bce632005-06-21 01:41:34 +0000379
njnd1af0032005-05-29 17:01:48 +0000380 /* Redirect vsyscalls to local versions */
sewardjb9bce632005-06-21 01:41:34 +0000381 add_redirect_addr_to_addr(
382 0xFFFFFFFFFF600000ULL,
383 (Addr)&VG_(amd64_linux_REDIR_FOR_vgettimeofday)
384 );
sewardjb9bce632005-06-21 01:41:34 +0000385 add_redirect_addr_to_addr(
tomf5db3b62005-06-21 13:26:17 +0000386 0xFFFFFFFFFF600400ULL,
sewardjb9bce632005-06-21 01:41:34 +0000387 (Addr)&VG_(amd64_linux_REDIR_FOR_vtime)
388 );
389
cerion85665ca2005-06-20 15:51:07 +0000390#elif defined(VGP_ppc32_linux)
sewardjb9bce632005-06-21 01:41:34 +0000391
sewardj96fcfc82005-11-18 21:12:52 +0000392 /* If we're using memcheck, use these intercepts right from
393 the start, otherwise ld.so makes a lot of noise. */
394 if (0==VG_(strcmp)("Memcheck", VG_(details).name)) {
395
396 add_redirect_sym_to_addr(
397 "soname:ld.so.1", "strlen",
398 (Addr)&VG_(ppc32_linux_REDIR_FOR_strlen)
399 );
400 add_redirect_sym_to_addr(
401 "soname:ld.so.1", "strcmp",
402 (Addr)&VG_(ppc32_linux_REDIR_FOR_strcmp)
403 );
404
405 }
sewardjb9bce632005-06-21 01:41:34 +0000406
njnd1af0032005-05-29 17:01:48 +0000407#else
408# error Unknown platform
409#endif
410}
sewardjcbdddcf2005-03-10 23:23:45 +0000411
njn16eeb4e2005-06-16 03:56:58 +0000412/* Z-decode a symbol into library:func form, eg
413
414 _vgi_libcZdsoZd6__ZdlPv --> libc.so.6:_ZdlPv
415
416 Uses the Z-encoding scheme described in pub_core_redir.h.
417 Returns True if demangle OK, False otherwise.
418*/
419static Bool Z_decode(const Char* symbol, Char* result, Int nbytes)
420{
421# define EMIT(ch) \
422 do { \
423 if (j >= nbytes) \
424 result[j-1] = 0; \
425 else \
426 result[j++] = ch; \
427 } while (0)
428
429 Bool error = False;
430 Int i, j = 0;
431 Int len = VG_(strlen)(symbol);
432 if (0) VG_(printf)("idm: %s\n", symbol);
433
434 i = VG_REPLACE_FUNCTION_PREFIX_LEN;
435
436 /* Chew though the Z-encoded soname part. */
437 while (True) {
438
439 if (i >= len)
440 break;
441
442 if (symbol[i] == '_')
443 /* We found the underscore following the Z-encoded soname.
444 Just copy the rest literally. */
445 break;
446
447 if (symbol[i] != 'Z') {
448 EMIT(symbol[i]);
449 i++;
450 continue;
451 }
452
453 /* We've got a Z-escape. Act accordingly. */
454 i++;
455 if (i >= len) {
456 /* Hmm, Z right at the end. Something's wrong. */
457 error = True;
458 EMIT('Z');
459 break;
460 }
461 switch (symbol[i]) {
462 case 'a': EMIT('*'); break;
463 case 'p': EMIT('+'); break;
464 case 'c': EMIT(':'); break;
465 case 'd': EMIT('.'); break;
466 case 'u': EMIT('_'); break;
467 case 'h': EMIT('-'); break;
468 case 's': EMIT(' '); break;
469 case 'Z': EMIT('Z'); break;
470 default: error = True; EMIT('Z'); EMIT(symbol[i]); break;
471 }
472 i++;
473 }
474
475 if (error || i >= len || symbol[i] != '_') {
476 /* Something's wrong. Give up. */
477 VG_(message)(Vg_UserMsg, "intercept: error demangling: %s", symbol);
478 EMIT(0);
479 return False;
480 }
481
482 /* Copy the rest of the string verbatim. */
483 i++;
484 EMIT(':');
485 while (True) {
486 if (i >= len)
487 break;
488 EMIT(symbol[i]);
489 i++;
490 }
491
492 EMIT(0);
493 if (0) VG_(printf)("%s\n", result);
494 return True;
495
496# undef EMIT
497}
498
499// Nb: this can change the string pointed to by 'symbol'.
500static void handle_replacement_function( Char* symbol, Addr addr )
501{
502 Bool ok;
503 Int len = VG_(strlen)(symbol) + 1 - VG_REPLACE_FUNCTION_PREFIX_LEN;
504 Char *lib = VG_(arena_malloc)(VG_AR_SYMTAB, len+8);
505 Char *func;
506
507 // Put "soname:" at the start of lib
508 VG_(strcpy)(lib, "soname:");
509
510 ok = Z_decode(symbol, lib+7, len);
511 if (ok) {
512 // lib is "soname:<libname>:<fnname>". Split the string at the 2nd ':'.
513 func = lib + VG_(strlen)(lib)-1;
514 while(*func != ':') func--;
515 *func = '\0';
516 func++; // Move past the '\0'
517
518 // Now lib is "soname:<libname>" and func is "<fnname>".
519 if (0) VG_(printf)("lib A%sZ, func A%sZ\n", lib, func);
520 add_redirect_sym_to_addr(lib, func, addr);
521
522 // Overwrite the given Z-encoded name with just the fnname.
523 VG_(strcpy)(symbol, func);
524 }
525
526 VG_(arena_free)(VG_AR_SYMTAB, lib);
527}
528
njnbc6d84d2005-06-19 18:58:03 +0000529static Addr __libc_freeres_wrapper = 0;
530
531Addr VG_(get_libc_freeres_wrapper)(void)
532{
533 return __libc_freeres_wrapper;
534}
535
njn16eeb4e2005-06-16 03:56:58 +0000536// This is specifically for stringifying VG_(x) function names. We
537// need to do two macroexpansions to get the VG_ macro expanded before
538// stringifying.
539#define _STR(x) #x
540#define STR(x) _STR(x)
541
542static void handle_load_notifier( Char* symbol, Addr addr )
543{
544 if (VG_(strcmp)(symbol, STR(VG_NOTIFY_ON_LOAD(freeres))) == 0)
njnbc6d84d2005-06-19 18:58:03 +0000545 __libc_freeres_wrapper = addr;
njn16eeb4e2005-06-16 03:56:58 +0000546// else if (VG_(strcmp)(symbol, STR(VG_WRAPPER(pthread_startfunc_wrapper))) == 0)
547// VG_(pthread_startfunc_wrapper)((Addr)(si->offset + sym->st_value));
548 else
549 vg_assert2(0, "unrecognised load notification function: %s", symbol);
550}
551
552static Bool is_replacement_function(Char* s)
553{
554 return (0 == VG_(strncmp)(s,
555 VG_REPLACE_FUNCTION_PREFIX,
556 VG_REPLACE_FUNCTION_PREFIX_LEN));
557}
558
559static Bool is_load_notifier(Char* s)
560{
561 return (0 == VG_(strncmp)(s,
562 VG_NOTIFY_ON_LOAD_PREFIX,
563 VG_NOTIFY_ON_LOAD_PREFIX_LEN));
564}
565
566// Call this for each symbol loaded. It determines if we need to do
567// anything special with it. It can modify 'symbol' in-place.
568void VG_(maybe_redir_or_notify) ( Char* symbol, Addr addr )
569{
570 if (is_replacement_function(symbol))
571 handle_replacement_function(symbol, addr);
572 else
573 if (is_load_notifier(symbol))
574 handle_load_notifier(symbol, addr);
575}
576
577
sewardjcbdddcf2005-03-10 23:23:45 +0000578//:: /*------------------------------------------------------------*/
579//:: /*--- General function wrapping. ---*/
580//:: /*------------------------------------------------------------*/
581//::
582//:: /*
583//:: TODO:
584//:: - hook into the symtab machinery
585//:: - client-side wrappers?
586//:: - better interfaces for before() functions to get to arguments
587//:: - handle munmap of code (dlclose())
588//:: - handle thread exit
589//:: - handle longjmp
590//:: */
591//:: struct callkey {
592//:: ThreadId tid; /* calling thread */
593//:: Addr esp; /* address of args on stack */
594//:: Addr eip; /* return address */
595//:: };
596//::
597//:: struct call_instance {
598//:: struct callkey key;
599//::
600//:: const FuncWrapper *wrapper;
601//:: void *nonce;
602//:: };
603//::
604//:: static inline Addr addrcmp(Addr a, Addr b)
605//:: {
606//:: if (a < b)
607//:: return -1;
608//:: else if (a > b)
609//:: return 1;
610//:: else
611//:: return 0;
612//:: }
613//::
614//:: static inline Int cmp(UInt a, UInt b)
615//:: {
616//:: if (a < b)
617//:: return -1;
618//:: else if (a > b)
619//:: return 1;
620//:: else
621//:: return 0;
622//:: }
623//::
624//:: static Int keycmp(const void *pa, const void *pb)
625//:: {
626//:: const struct callkey *a = (const struct callkey *)pa;
627//:: const struct callkey *b = (const struct callkey *)pb;
628//:: Int ret;
629//::
630//:: if ((ret = cmp(a->tid, b->tid)))
631//:: return ret;
632//::
633//:: if ((ret = addrcmp(a->esp, b->esp)))
634//:: return ret;
635//::
636//:: return addrcmp(a->eip, b->eip);
637//:: }
638//::
639//:: /* List of wrapped call invocations which are currently active */
njnbe91aae2005-03-27 01:42:41 +0000640//:: static SkipList wrapped_frames = VG_SKIPLIST_INIT(struct call_instance, key, keycmp,
sewardjcbdddcf2005-03-10 23:23:45 +0000641//:: NULL, VG_AR_SYMTAB);
642//::
643//:: static struct call_instance *find_call(Addr retaddr, Addr argsp, ThreadId tid)
644//:: {
645//:: struct callkey key = { tid, argsp, retaddr };
646//::
647//:: return VG_(SkipList_Find_Exact)(&wrapped_frames, &key);
648//:: }
649//::
650//:: static void wrapper_return(Addr retaddr);
651//::
652//:: /* Called from generated code via helper */
653//:: void VG_(wrap_before)(ThreadState *tst, const FuncWrapper *wrapper)
654//:: {
njnaf839f52005-06-23 03:27:57 +0000655//:: Addr retaddr = VG_RETADDR(tst->arch);
656//:: Addr argp = (Addr)&VG_FUNC_ARG(tst->arch, 0);
sewardjcbdddcf2005-03-10 23:23:45 +0000657//:: void *nonce = NULL;
658//:: Bool mf = VG_(my_fault);
659//:: VG_(my_fault) = True;
660//::
661//:: if (wrapper->before) {
njnaf839f52005-06-23 03:27:57 +0000662//:: va_list args = VG_VA_LIST(tst->arch);
sewardjcbdddcf2005-03-10 23:23:45 +0000663//:: nonce = (*wrapper->before)(args);
664//:: }
665//::
666//:: if (wrapper->after) {
667//:: /* If there's an after function, make sure it gets called */
668//:: struct call_instance *call;
669//::
670//:: call = find_call(retaddr, argp, tst->tid);
671//::
672//:: if (call != NULL) {
673//:: /* Found a stale outstanding call; clean it up and recycle
674//:: the structure */
675//:: if (call->wrapper->after)
676//:: (*call->wrapper->after)(call->nonce, RT_LONGJMP, 0);
677//:: } else {
678//:: call = VG_(SkipNode_Alloc)(&wrapped_frames);
679//::
680//:: call->key.tid = tst->tid;
681//:: call->key.esp = argp;
682//:: call->key.eip = retaddr;
683//::
684//:: VG_(SkipList_Insert)(&wrapped_frames, call);
685//::
686//:: wrapper_return(retaddr);
687//:: }
688//::
689//:: call->wrapper = wrapper;
690//:: call->nonce = nonce;
691//:: } else
692//:: vg_assert(nonce == NULL);
693//::
694//:: VG_(my_fault) = mf;
695//:: }
696//::
697//:: /* Called from generated code via helper */
698//:: void VG_(wrap_after)(ThreadState *tst)
699//:: {
njnaf839f52005-06-23 03:27:57 +0000700//:: Addr EIP = VG_INSTR_PTR(tst->arch); /* instruction after call */
701//:: Addr ESP = VG_STACK_PTR(tst->arch); /* pointer to args */
702//:: Word ret = VG_RETVAL(tst->arch); /* return value */
sewardjcbdddcf2005-03-10 23:23:45 +0000703//:: struct call_instance *call;
704//:: Bool mf = VG_(my_fault);
705//::
706//:: VG_(my_fault) = True;
707//:: call = find_call(EIP, ESP, tst->tid);
708//::
709//:: if (0)
710//:: VG_(printf)("wrap_after(%p,%p,%d) -> %p\n", EIP, ESP, tst->tid, call);
711//::
712//:: if (call != NULL) {
713//:: if (call->wrapper->after)
714//:: (*call->wrapper->after)(call->nonce, RT_RETURN, ret);
715//::
716//:: VG_(SkipList_Remove)(&wrapped_frames, &call->key);
717//:: VG_(SkipNode_Free)(&wrapped_frames, call);
718//:: }
719//:: VG_(my_fault) = mf;
720//:: }
721//::
722//::
723//:: struct wrapped_function {
724//:: Addr eip; /* eip of function entrypoint */
725//:: const FuncWrapper *wrapper;
726//:: };
727//::
728//:: struct wrapper_return {
729//:: Addr eip; /* return address */
730//:: };
731//::
732//:: /* A mapping from eip of wrapped function entrypoints to actual wrappers */
njnbe91aae2005-03-27 01:42:41 +0000733//:: static SkipList wrapped_functions = VG_SKIPLIST_INIT(struct wrapped_function, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000734//:: NULL, VG_AR_SYMTAB);
735//::
736//:: /* A set of EIPs which are return addresses for wrapped functions */
njnbe91aae2005-03-27 01:42:41 +0000737//:: static SkipList wrapper_returns = VG_SKIPLIST_INIT(struct wrapper_return, eip, VG_(cmp_Addr),
sewardjcbdddcf2005-03-10 23:23:45 +0000738//:: NULL, VG_AR_SYMTAB);
739//::
740//:: /* Wrap function starting at eip */
741//:: void VG_(wrap_function)(Addr eip, const FuncWrapper *wrapper)
742//:: {
743//:: struct wrapped_function *func;
744//::
745//:: if (0)
746//:: VG_(printf)("wrapping %p with (%p,%p)\n", eip, wrapper->before, wrapper->after);
747//::
748//:: func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
749//::
750//:: if (func == NULL) {
751//:: func = VG_(SkipNode_Alloc)(&wrapped_functions);
752//:: VG_(invalidate_translations)(eip, 1, True);
753//::
754//:: func->eip = eip;
755//:: VG_(SkipList_Insert)(&wrapped_functions, func);
756//:: }
757//::
758//:: func->wrapper = wrapper;
759//:: }
760//::
761//:: const FuncWrapper *VG_(is_wrapped)(Addr eip)
762//:: {
763//:: struct wrapped_function *func = VG_(SkipList_Find_Exact)(&wrapped_functions, &eip);
764//::
765//:: if (func)
766//:: return func->wrapper;
767//:: return NULL;
768//:: }
769//::
770//:: Bool VG_(is_wrapper_return)(Addr eip)
771//:: {
772//:: struct wrapper_return *ret = VG_(SkipList_Find_Exact)(&wrapper_returns, &eip);
773//::
774//:: return ret != NULL;
775//:: }
776//::
777//:: /* Mark eip as being the return address of a wrapper, so that the
778//:: codegen will generate the appropriate call. */
779//:: void wrapper_return(Addr eip)
780//:: {
781//:: struct wrapper_return *ret;
782//::
783//:: if (VG_(is_wrapper_return)(eip))
784//:: return;
785//::
786//:: VG_(invalidate_translations)(eip, 1, True);
787//::
788//:: ret = VG_(SkipNode_Alloc)(&wrapper_returns);
789//:: ret->eip = eip;
790//::
791//:: VG_(SkipList_Insert)(&wrapper_returns, ret);
792//:: }
njnc0ae7052005-08-25 22:55:19 +0000793
794/*--------------------------------------------------------------------*/
795/*--- end ---*/
796/*--------------------------------------------------------------------*/