Possible bug in libc

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Possible bug in libc

Post by J.F. »

Me and someone else have been working on a prorgam that takes a lot of memory, however, we've been having problems setting the memory via PSP_HEAP_SIZE_KB and PSP_HEAP_SIZE_MAX. I went to look at the code for PSP_HEAP_SIZE_MAX because I knew it was new.

First look at where it is applied to the module info:

Code: Select all

/* Declare the size of the heap (in KB) that the program wants to allocate from. */
#define PSP_HEAP_SIZE_KB(size_kb) \
	unsigned int sce_newlib_heap_kb_size = (size_kb)

/* Declare to allocate maximum heap area */
#define PSP_HEAP_SIZE_MAX() \
	PSP_HEAP_SIZE_KB(-1)
As you can see, for MAX, sce_newlib_heap_kb is set to -1. Now look at the relavent code in libcglue.c:

Code: Select all

                if (&sce_newlib_heap_kb_size != NULL) {
                        heap_size = sce_newlib_heap_kb_size * 1024;
                } else {
                        if (&__pspsdk_is_prx != NULL) {
                                heap_size = DEFAULT_PRX_HEAP_SIZE_KB * 1024;
                        } else {
                                heap_size = sceKernelMaxFreeMemSize();
                        }
                }
-1 is not NULL, so heap_size appears to be set to -1024 in this case when it should be sceKernelMaxFreeMemSize().

Looks like whatever check there was for MAX didn't make it into libc like it was supposed to.

EDIT: Looking a bit more, the check is in libcglue.c for newlib, but not in the psplibc.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Not saying you have or haven't found a bug - but it's checking the address of the variable == NULL, not the value. I assume our linker is setting undefined symbols to 0?

Jim
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Oh, yeah. Missed the & there. But it still doesn't seem right. It certainly doesn't match the code in newlib which clearly looks for -1. I think it would probably be best simply to cut the code from there and paste it in the other.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Undefined symbols are set to 0 when they're weakly linked with __attribute__((weak)), and they aren't defined at runtime, hence the NULL checks.

JF, you're correct, Tyranid added the check to newlib but not libc (rev 2320). I dunno if many people use psplibc, but I just added his changes there too (rev 2327). I'm too lazy to even compile it so please check that it works and provide a patch if I screwed up. :)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

That seems to have done the trick. I wouldn't have noticed it myself if I hadn't switched from newlib to psplibc while checking if the libc was causing a problem. :)
Post Reply