Page 1 of 2
How can I make custom malloc / free?
Posted: Sun Jul 25, 2010 8:01 pm
by Dirbaio
So, basically I want to implement my custom malloc and free, overriding libc ones.
I've tried the following without success:
- Malloc hooks: they don't exist in devkitarm
- Defining my own malloc / free functions: Works, but some functions from libc (printf related) call _malloc_r and _free_r, which i'm not intercepting.
How could I do it?
Thanks in advance!
Re: How can I make custom malloc / free?
Posted: Mon Jul 26, 2010 9:32 pm
by WinterMute
I'm really not sure how easy this will be to do, I'll need to do some digging in newlib.
malloc and free call _malloc_r and _free_r respectively so intercepting those might work.
What's this for anyway?
Re: How can I make custom malloc / free?
Posted: Tue Jul 27, 2010 9:17 am
by Dirbaio
I need it for two reasons:
- For debugging: I have weird memory corruption issues
- For ROM Hacking (in particular NSMB Hacking): I've set up some linker scripts that allow me to write some asm/c/c++ code and insert it into the game. I need malloc/free to call the actuall malloc/free from the game, or else i can't use dynamic memory, libc, libfat and lots of cool things i'd like to use...
I did try to intercept _malloc_r and _free_r too. I just wasn't able because there was soooo much shit:
Code: Select all
#if __STD_C
Void_t* mALLOc(RARG size_t bytes)
#else
Void_t* mALLOc(RARG bytes) RDECL size_t bytes;
#endif
{
#ifdef MALLOC_PROVIDED
return malloc (bytes); // Make sure that the pointer returned by malloc is returned back.
#else
mchunkptr victim; /* inspected/selected chunk */
INTERNAL_SIZE_T victim_size; /* its size */
int idx; /* index for bin traversal */
mbinptr bin; /* associated bin */
mchunkptr remainder; /* remainder from a split */
long remainder_size; /* its size */
int remainder_index; /* its bin index */
unsigned long block; /* block traverser bit */
int startidx; /* first bin of a traversed block */
mchunkptr fwd; /* misc temp for linking */
mchunkptr bck; /* misc temp for linking */
(blah)
Right now, i've been able to get something working by copying the entire libc source code into my project, modifying malloc.c and compiling everything together.
However, that gives lots of problems. For example, when you just link to libc.a, the linker is smart and only links the .o's from libc.a that are needed. But if I put all the libc source code, everything is compiled and linked no matter if they are used or not
so i get more or less 100kb more of code. That's only a temporary solution...
So, I'm still interested in more "elegant" ways for doing this
Thanks!
Re: How can I make custom malloc / free?
Posted: Tue Jul 27, 2010 3:45 pm
by elhobbs
are you thinking that there are memory issues outside of your code - in libnds or newlib?
Re: How can I make custom malloc / free?
Posted: Tue Jul 27, 2010 7:01 pm
by Dirbaio
Nope
I dont want custom mallocs because I think that the default ones are bugged.
With custom mallocs you can do very good debugging.
For instance, what i have now is this:
http://dirbaio.pastebin.com/UaHqxWfa
What it does is allocate the memory from the beginning of RAM, and each allocated block has around it a reserved area filled with constant data. When you do a free(), it checks if the data is still there, so it can see if you're writing out of the bounds of an array or similar things.
It can catch silly errors like
u8* ptr = new u8[100];
delete[] ptr;
delete[] ptr; //Twice!!
and like
u8* ptr = new u8[10];
ptr[11]=1;
delete[] ptr;
Re: How can I make custom malloc / free?
Posted: Tue Jul 27, 2010 9:37 pm
by zeromus
if you just want to catch your own silly errors then only use new and delete and override them
Re: How can I make custom malloc / free?
Posted: Wed Jul 28, 2010 2:02 pm
by Izhido
Umm... could you please elaborate a little more on this thing about default malloc / free being "bugged"?
Re: How can I make custom malloc / free?
Posted: Wed Jul 28, 2010 2:32 pm
by vuurrobin
its not the default malloc/free that is bugged, but by overriding malloc and free, you can keep better track of memory allocation/freeing to find memory leaks and stuff.
Re: How can I make custom malloc / free?
Posted: Sun Aug 08, 2010 12:12 pm
by cdmac
The GNU linker allows you to override functions. It can be useful for globally disabling printf()s in crappy middleware and libraries in release builds.
Here's a forum post explaining how to use it with malloc. The same thing would apply for free/realloc.
http://www.linuxquestions.org/questions ... nt-790444/
Re: How can I make custom malloc / free?
Posted: Sun Aug 08, 2010 4:02 pm
by WinterMute
That's awesome, just tested with devkitARM, thanks cdmac.
Code: Select all
void *__real__malloc_r(struct _reent*r, size_t size);
void *__wrap__malloc_r(struct _reent*r, size_t size) {
iprintf("my malloc\n");
/* do this or write your own allocator */
return __real_malloc_r(r,size);
}
and add -Wl,-wrap,_malloc_r to your LDFLAGS in the makefile. The linker then prefixes the real function with __real_ and your override with __wrap_
Code: Select all
LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-wrap,_malloc_r -Wl,-Map,$(notdir $*.map)