Instrumenting malloc & free

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my application (ATMega1284p, Atmel Studio) I have functions I call to wrap "malloc" and "free" so that I can turn on some memory verification when needed to detect stack/heap collisions, buffer overrun, etc.

 

This has all worked well in the past but I recently started using some stl library functions that call malloc and free themselves which bypasses my wrapper functions causing the verification functions to fail.

 

Even though I have access to the malloc source I don't want to modify it if at all possible. 

 

Using linker commands, etc. is it possible to have the linker direct all "malloc" references to "mymalloc" except for the references from "mymalloc" itself?  (i.e.: allow "mymalloc" to intercept all calls to "malloc" while still allowing "mymalloc" to call the library "malloc")

 

Thanks in advance ...

 

Chuck Hackett

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bad idea to use dynamic allocation on a limited resource micro. I suppose 1284 has 16KB but even that will eventually fragment.

 

Anyway if you insist can we assume from "Atmel Studio" you mean "avr-gcc". If so I think you may enjoy this page in the user manual:

 

https://www.gnu.org/savannah-che...

 

(ie someone already thought you might want to do this!)

 

PS more info:

 

http://man7.org/linux/man-pages/...

Last Edited: Mon. Feb 12, 2018 - 01:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
can we assume from "Atmel Studio" you mean "avr-gcc". If so I think you may enjoy this page in the user manual:
AVR-Libc has no implementation for these hooks.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just what I need.

 

Yup, I'm aware of the pitfalls of dynamic allocation and have taken steps tailored to my application to minimize the danger.  The application has been running for about 5 years now so I'm good ... so far ... :-)

 

While searching the issue I also came across things like "malloc_ss" which (quick read) looks interesting for detecting corruption, leaks, etc.

 

http://csweb.cs.wfu.edu/~torgerse/Kokua/More_SGI/007-2579-009/sgi_html/ch09.html

 

Thanks again,

 

Cheers,

 

Chuck Hackett

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR-Libc has no implementation for these hooks.

I was afraid of that ... sigh ...

 

What do people use in AVR-Libc land to instrument malloc? (I should probably switch this to the compiler forum ...)

 

Chuck Hackett

Last Edited: Mon. Feb 12, 2018 - 01:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You can use the --wrap=malloc feature from GNU Binutils (ld). Each call to malloc will be performed to __wrap_malloc instead, and the original malloc symbol will be named __real_malloc. Thus you can write a wrapper named __wrap_malloc, implement what you need together with a call to __real_malloc.
 
This requires that the compiler does not (partially) inline the call the the respective function. For built-in functions you want to compile with -fno-builtin-malloc. For own functions you want attributes noinline,noclone,no_icf,externally_visible and the function to have external linkage.
 
http://sourceware.org/binutils/d...

avrfreaks does not support Opera. Profile inactive.

Last Edited: Mon. Feb 12, 2018 - 01:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh rats it was actually the wrap thing SprinterSB mentions that I was trying to remember as that's actually the one I used in the distant past. (on "big iron")

Last Edited: Mon. Feb 12, 2018 - 02:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SprinterSB wrote:
You can use the --wrap=malloc feature from GNU Binutils (ld). Each call to malloc will be performed to __wrap_malloc instead, and the original malloc symbol will be named __real_malloc. Thus you can write a wrapper named __wrap_malloc, implement what you need together with a call to __real_malloc. ....

 

Thanks, I'll check it out.

 

Regards,

 

Chuck Hackett