forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A bug in malloc() etc.

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
raipsu



Joined: 25 Oct 2004
Posts: 6

PostPosted: Mon Oct 25, 2004 7:15 am    Post subject: A bug in malloc() etc. Reply with quote

Hi!

I noticed that there are something wrong with malloc() etc. functions. Seems that sometimes free() does not free the memory.

I wrote this little program to demonstrate it:

Code:

#include <stdio.h>
#include <malloc.h>

int main () {
  int iter = 0;

  realloc(malloc(10), 8); /* 1 */
  realloc(malloc(10), 8); /* 2 */
  realloc(realloc(malloc(10), 20), 10);

  for (;;) {
    char *ptr;
    ptr = malloc(2048*1024);  /* This can be smaller also */
    printf ("%d: %p\n", iter++, ptr);
    if (ptr == NULL) {
      printf ("malloc() failed!\n");
      break;
    }
    free(ptr);
  }

  return 0;
}


I'm using a toolchain built a few days ago with oopo's toolchain.sh script.

I'll compile it like this:

ee-gcc -DPS2_EE -O3 -G0 -Wall -I$PS2SDK/common/include -I$PS2SDK/ee/include -c main.c
ee-gcc -nostartfiles -L$PS2SDK/ee/lib -T$PS2SDK/ee/startup/linkfile -o test.elf $PS2SDK/ee/startup/crt0.o main.o -lc -lkernel -lsyscall -lc

And then run it:

# ps2client execee host:test.elf
loadelf: fname host:test.elf secname all
Input ELF format filename = host:test.elf
0 00100000 000040b8 .
Loaded, host:test.elf
start address 0x100008
gp address 00000000
0: 1079296
1: 3176464
2: 5273632
3: 7370800
4: 9467968
5: 11565136
6: 13662304
7: 15759472
8: 17856640
9: 19953808
10: 22050976
11: 24148144
12: 26245312
13: 28342480
14: 30439648
15: 0
malloc() failed!

After 15 malloc() and free()'s it has filled up the whole memory and malloc() returns NULL. According my testings, realloc() is needed to make free() behave like this. If you'll remove the lines commented with 1 and 2, malloc() and free() works normally and the loop does never end. Any ideas how to fix it? :)
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Mon Oct 25, 2004 9:08 am    Post subject: Reply with quote

I thought %p was supposed to print a pointer, which is represented as a hex number. Why does it print out a decimal number?

(Sorry not to answer the original question).
_________________
"He was warned..."
Back to top
View user's profile Send private message
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Mon Oct 25, 2004 10:09 am    Post subject: Reply with quote

Fixed. Was a stupid realloc() bug: http://cvs.ps2dev.org/ps2sdk/ee/libc/src/alloc.c.diff?r1=1.3&r2=1.4

and since your realloc was shrinking the memory, the realloc function was munching its pointers located after the malloc()ed zone.


I also (uglily) fixed the %p part of xprintf.c. Still doesn't behave as it should though, since it should be translated into "0x%08X" and not "%X". Ho well...
_________________
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Back to top
View user's profile Send private message
raipsu



Joined: 25 Oct 2004
Posts: 6

PostPosted: Tue Nov 02, 2004 12:15 am    Post subject: Reply with quote

Thanks, the realloc() is working fine now.

Now memalign() is broken. :( Replace the malloc() and realloc() lines in start of previous test program with malloc(2); memalign(64, 10); and free() stops working again..
Back to top
View user's profile Send private message
raipsu



Joined: 25 Oct 2004
Posts: 6

PostPosted: Tue Nov 02, 2004 1:30 am    Post subject: Reply with quote

memalign() was broken because it did not update __alloc_heap_head and __alloc_heap_tail. This should fix it:

Code:

*** ../d/ps2sdk/ee/libc/src/alloc.c     Mon Oct 11 03:44:59 2004
--- ee/libc/src/alloc.c Mon Nov  1 17:22:39 2004
***************
*** 206,211 ****
--- 206,212 ----
  {
        heap_mem_header_t new_mem;
        heap_mem_header_t *cur_mem;
+       heap_mem_header_t *old_mem;
        void *ptr = NULL;

        if (align <= DEFAULT_ALIGNMENT)
        void *ptr = NULL;

        if (align <= DEFAULT_ALIGNMENT)
***************
*** 226,231 ****
--- 227,234 ----
        /* Otherwise, align the pointer and fixup our hearder accordingly.  */
        ptr = (void *)ALIGN((u32)ptr, align);

+       old_mem = cur_mem;
+
        /* Copy the heap_mem_header_t locally, before repositioning (to make
           sure we don't overwrite ourselves.  */
        memcpy(&new_mem, cur_mem, sizeof(heap_mem_header_t));
***************
*** 237,242 ****
--- 240,251 ----
        if (cur_mem->next)
                cur_mem->next->prev = cur_mem;

+       if (__alloc_heap_head == old_mem)
+               __alloc_heap_head = cur_mem;
+
+       if (__alloc_heap_tail == old_mem)
+               __alloc_heap_tail = cur_mem;
+
        cur_mem->ptr = ptr;
        return ptr;
  }
Back to top
View user's profile Send private message
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Tue Nov 02, 2004 3:33 am    Post subject: Reply with quote

Thanks -- commited.


Also, did you notice I changed realloc() code so it is a bit smarter ? (that is, won't re-malloc() if there is room in memory whatsoever).

I didn't do any extensive check however, so, could you be kind enough to run some of your previous code and check if things go right ? Thanks.
_________________
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group