0

I have used dlsym() to invoke my version of malloc, instead of the default library malloc:

 lt_malloc = (void*(*)(size_t))dlsym(RTLD_NEXT, "malloc");
 TRACE((stderr, "initialize: lt_malloc=%p\n", lt_malloc));
 if (!lt_malloc) {
     fprintf(stderr, "LeakTracer: could not resolve 'malloc' in 'libc.so': %s\n", dlerror());
     exit(1);
 }

Now after certain time, may be a timer or so, i want to revert back to the original version of malloc (libc library malloc). How can i do that?

Thanks in advance.

Juliano
  • 39,173
  • 13
  • 67
  • 73
RajSanpui
  • 11,556
  • 32
  • 79
  • 146

2 Answers2

0

Does the above code mean 1) you have called your malloc also malloc? 2) or that you assign the libc malloc to lt_malloc?

The code above suggests that lt_malloc is the libc malloc so the "original" malloc. Well you than probably have some either some macros or a function

So then you switch as easily as my_malloc = libc_malloc my_malloc = my_debug_malloc

and you just call my_malloc

If that is not helpful please explain the posted code and what you expect at "ente codde here"

Friedrich
  • 5,916
  • 25
  • 45
  • @Friedrich: Please refer the man page for dlsym, especially the use of macro RTLD_NEXT. Yes, we have our own malloc code somewhere, in the program which will be called. – RajSanpui Mar 31 '11 at 05:38
  • I know what dlsym does and so lt_malloc is the libc malloc. So where's your malloc then? What's is name and how to you call it via a Macro or another functon pointer or how? – Friedrich Mar 31 '11 at 06:06
  • No, i think in that case, you need to know better about macro RTLD_NEXT. I will copy-paste here an excrept from man page: `There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find the first occurrence of the desired symbol using the default library search order. The latter will find the next occurrence of a function in the search order after the current library. This allows one to provide a wrapper around a function in another shared library.` – RajSanpui Mar 31 '11 at 06:09
  • The answer i guess is RTLD_DEFAULT, but what happens if my version of malloc is already loaded in memory? Does RTLD_DEFAULT overwrite it? – RajSanpui Mar 31 '11 at 06:13
  • Your malloc surely is not in libc.So where is it? – Friedrich Mar 31 '11 at 12:32
  • @Friedrich: I have a malloc defined in my file, which i haven't put here. lt_malloc will point to the original libc malloc – RajSanpui Mar 31 '11 at 13:34
  • I'm quite aware that lt_malloc is the "libc" malloc. And therefor I asked what's thename of your malloc? Is it also malloc or not? – Friedrich Apr 01 '11 at 08:22
  • @Friedrich: Ofcourse it will be "malloc", otherwise do you think we can put the wrapper? – RajSanpui Apr 01 '11 at 09:46
  • @Friedrich: If we carefully study the man page sometimes, hit and trial is not required. But anyways as you asked me, let me tell you: RTLD_DEFAULT will always hit my malloc, and will go to an infinite loop. (It's tested) – RajSanpui Apr 06 '11 at 05:58
0

You should rather use dlopen on the library containing your malloc function and use the handle from dlopen as a first argument to dlsym.

Alternatively, you can use LD_PRELOAD.

Community
  • 1
  • 1
Mathias Brossard
  • 3,668
  • 2
  • 26
  • 30