0

For an analysis I'm doing I want to be able to "catch" specific malloc calls. I therefore created a function wrapper to malloc, named malloc_wrapper:

void *malloc_wrap(size_t size) {
   return malloc(size);
}

All left is just slightly modify the source code by switching some malloc calls with malloc_wrap. I then use Intel Pin to capture what I need.

Unfortunately, it didn't work. I didn't see malloc_wrap being called in the assembly code, so it was probably inlined. Quick search, and I added this to the function header:

__attribute__ ((noinline))

Great, now I'm able to spot the function entry, but not the exit. I can't see any ret call at the end of the function. How can I force the compiler to compile my wrapper function regularly?

Gilsho
  • 63
  • 7
  • 1
    How do you compile the code (command line arguments)? Any optimizations enabled? I think [MCVE](http://stackoverflow.com/help/mcve) with both C and assembly code would be very helpful. – yeputons Jun 09 '17 at 13:39
  • I do not mind sharing the command line arguments, but I'm going to try this wrapper trick on a range of applications, that each have a different makefile. Therefore I'm looking for a solution that is independent on the command line arguments. – Gilsho Jun 09 '17 at 13:43
  • Probably it's optimized in a way it is doing just a jump (not call) to malloc, then ret from malloc happens to address left on the stack by malloc_wrap caller. Disable all optimization. At least with -O0. If it won't help, try [this](https://stackoverflow.com/a/33281940/2989411). – ArturFH Jun 09 '17 at 14:03
  • What you outline in the question should work. You say it doesn't. We can't reproduce the problem. You, therefore, need to show us enough more context to show us what isn't working — why you can't see the return. I'm not sure whether that needs to be assembler generated from the code or what. If, when you run the code, the surrogate `malloc()` does in fact return, then the problem is likely in your interpretation of what you're seeing. But, with the information in the question, we cannot help you — there isn't a problem we can reproduce. Hence the request for an MCVE ([MCVE]). – Jonathan Leffler Jun 09 '17 at 14:03
  • I will modify my post with additional information. Meanwhile, I made it work by making my wrapper a shared library. – Gilsho Jun 09 '17 at 14:05
  • @jonathan: i suppose it's been TCO'd. – rici Jun 09 '17 at 17:08

0 Answers0