Returning data from a exported function...

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

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Returning data from a exported function...

Post by ne0h »

Hi,
I'm trying to return some data from a function exported from a kernel prx and imported to a user pbp, but it crash every time!
What's the problem?
This is the exported function:

Code: Select all

char* GetBarraInf(void)
{
    char* data="fdsfdsfds";
    return data;
}
And here I try to use it...

Code: Select all

printf("%s",GetBarraInf());
It doesn't work, no one character is printed to screen!
Crash every time!
What I'm trying to do is export a variable ( unsigned char ) that contains a image data ( builted with bin2o )!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

make sure you allocate pointers.
Also I always need pspSdkSetK1 when exporting functions otherwise it does not work for me
Image
Upgrade your PSP
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

mmm, is always needed to set K1 to 0? Is not only for "special" operations, or to remove some security protections?
Thanks!
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Doesn't work whit this! ?_?

Code: Select all

char* GetBarraInf(void)
{
    return "asd";
}
EDIT:
Get this exception:
Exception - Address load/inst fetch
EPC - 0889A07C
Location - ra.text + 0009607C
Cause - 10000010
Status - 20008613
BadVAddr - 88217A40

zr:00000000 at:2008FF00 v0:88217A41 v1:0889CB38
a0:88217A40 a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:0BBBFE58 t5:00003E0C t6:0880A5FC t7:20008600
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088AB9E6 s7:08930000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939230 sp:0BBBF5D0 fp:88217A40 ra:0889D7C4
Badboy_
Posts: 4
Joined: Sat Aug 09, 2008 2:39 am

Post by Badboy_ »

Code: Select all

char* GetBarraInf(void)
{
    char* data= (char*) malloc(20);
    strcpy(data, "Hello, world!");
    return data;
}
does this work?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

*sigh*, noobish question again...

do you think making a local string in a function is making this string to be... local to this function, don't you ?

didn't it ever occur to you that adding "static" before the local string would turn it into a permanent static string you can return from your function ?

how many times do you think you need to master such a trivial error ?

well, don't answer.

EDIT:

a0:88217A40 <-- hernel address doesn't work with user code. Hint : #define USER_ADDR(x) ({ (typeof(x))(((int)(x))&~0x80000000); }) ==> return USER_ADDR("blahblah");
Last edited by hlide on Sun Nov 09, 2008 2:31 am, edited 2 times in total.
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Doesn't work!
Got the same exception:

Code: Select all

Exception - Address load/inst fetch
EPC       - 0889A07C
Location  - ra.text + 0009607C
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr&#58;00000000 at&#58;00000000 v0&#58;8002013B v1&#58;0889CB38
a0&#58;8002013A a1&#58;0BBBFE44 a2&#58;00000073 a3&#58;0BBBFE40
t0&#58;00000068 t1&#58;0000002B t2&#58;00000020 t3&#58;88065244
t4&#58;A8028454 t5&#58;00003E0C t6&#58;000000FC t7&#58;880161C8
s0&#58;00000000 s1&#58;FFFFFFFF s2&#58;0BBBF63C s3&#58;0BBBF600
s4&#58;08930000 s5&#58;08930000 s6&#58;088ABA06 s7&#58;08930000
t8&#58;00000000 t9&#58;00000000 k0&#58;0BBBFF00 k1&#58;00000000
gp&#58;08939250 sp&#58;0BBBF5D0 fp&#58;8002013A ra&#58;0889D7C4
EDIT:
hlide, thanks, I'll try!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

just look at registers, some registers have kernel addresses when you are running a user code...
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

replace 0x40000000 into 0x80000000 of course...
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Tryied with this:

Code: Select all

static char data&#91;20&#93;;

char* GetBarraInf&#40;void&#41;
&#123;
    k1 = pspSdkSetK1&#40;0&#41;;
    strcpy&#40;data, "Hello world!"&#41;;
    pspSdkSetK1&#40;k1&#41;;
    return data;
&#125;
And it doesn't work, crash!
I've also tryied to make it a user prx but it crash!
Get the same exception:

Code: Select all

Exception - Address load/inst fetch
EPC       - 0889A07C
Location  - ra.text + 0009607C
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr&#58;00000000 at&#58;00000000 v0&#58;8002013B v1&#58;0889CB38
a0&#58;8002013A a1&#58;0BBBFE44 a2&#58;00000073 a3&#58;0BBBFE40
t0&#58;00000068 t1&#58;0000002B t2&#58;00000020 t3&#58;88065244
t4&#58;A8028454 t5&#58;00003E0C t6&#58;000000FC t7&#58;880161C8
s0&#58;00000000 s1&#58;FFFFFFFF s2&#58;0BBBF63C s3&#58;0BBBF600
s4&#58;08930000 s5&#58;08930000 s6&#58;088ABA06 s7&#58;08930000
t8&#58;00000000 t9&#58;00000000 k0&#58;0BBBFF00 k1&#58;00000000
gp&#58;08939250 sp&#58;0BBBF5D0 fp&#58;8002013A ra&#58;0889D7C4
just look at registers, some registers have kernel addresses when you are running a user code...
Sorry, doesn't know what mean the content of registers!
Never studied it! ( Have you a tuto about the content of this? )
So kernel address are 0x80000000 like, and user 0x40000000 like this, right?
replace 0x40000000 into 0x80000000 of course...
Sorry, but I've to change the a0 reg value?
EDIT:

nop, maybe I've understand!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Code: Select all

char const * GetBarraInf&#40;void&#41;
&#123;
    return &#40;char const *&#41;&#40;&#40;&#40;int&#41;"blahblah"&#41;&~0x80000000&#41;&#41;;
&#125;
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

No, I haven't undestrand!
I've writed this:

Code: Select all

#define USER_ADDR&#40;x&#41; &#40;&#123; &#40;typeof&#40;x&#41;&#41;&#40;&#40;&#40;int&#41;&#40;x&#41;&#41;&~0x80000000&#41;; &#125;&#41;
    
char* GetBarraInf&#40;void&#41;;
intraFontPrintf&#40;ltn8, 20, 20, "%s", USER_ADDR&#40;GetBarraInf&#40;&#41;&#41;&#41;;
But it crash! Get Bus data error...

Code: Select all

Exception - Bus error &#40;data&#41;
EPC       - 0889A084
Location  - ra.text + 00096084
Cause     - 1000001C
Status    - 20008613
BadVAddr  - 01044400

zr&#58;00000000 at&#58;00000000 v0&#58;0002013B v1&#58;0889CB40
a0&#58;0002013A a1&#58;0BBBFE44 a2&#58;00000073 a3&#58;0BBBFE40
t0&#58;00000068 t1&#58;0000002B t2&#58;00000020 t3&#58;88065244
t4&#58;A8028454 t5&#58;00003E0C t6&#58;000000FC t7&#58;880161C8
s0&#58;00000000 s1&#58;FFFFFFFF s2&#58;0BBBF63C s3&#58;0BBBF600
s4&#58;08930000 s5&#58;08930000 s6&#58;088ABA16 s7&#58;08C80000
t8&#58;00000000 t9&#58;00000000 k0&#58;0BBBFF00 k1&#58;00000000
gp&#58;08939260 sp&#58;0BBBF5D0 fp&#58;0002013A ra&#58;0889D7CC
Now I haven't a kernel address, right?
Anyway I've another problem...
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

oh my god! I got another kernel address! It crash...

Code: Select all

Exception - Address load/inst fetch
EPC       - 0889A068
Location  - ra.text + 00096068
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr&#58;00000000 at&#58;00000000 v0&#58;8002013B v1&#58;0889CB24
a0&#58;8002013A a1&#58;0BBBFE44 a2&#58;00000073 a3&#58;0BBBFE40
t0&#58;00000068 t1&#58;0000002B t2&#58;00000020 t3&#58;88065244
t4&#58;A8028454 t5&#58;00003E0C t6&#58;000000FC t7&#58;880161C8
s0&#58;00000000 s1&#58;FFFFFFFF s2&#58;0BBBF63C s3&#58;0BBBF600
s4&#58;08930000 s5&#58;08930000 s6&#58;088AB9E6 s7&#58;08930000
t8&#58;00000000 t9&#58;00000000 k0&#58;0BBBFF00 k1&#58;00000000
gp&#58;08939230 sp&#58;0BBBF5D0 fp&#58;8002013A ra&#58;0889D7B0
I'm sorry...

This is the code now:

Code: Select all

static char data&#91;20&#93;;

char const * GetBarraInf&#40;void&#41;
&#123;
    strcpy&#40;data, "Hello world!"&#41;;
    return &#40;char const *&#41;&#40;&#40;&#40;int&#41;data&#41;&~0x80000000&#41;;
&#125;
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Look at BadVAddr values they are abnormal...
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

hlide, a question:
EPC, Cause, Status and BadVAddr what means?
To work I've to change the variable addres?
But if I change the address the pointed memory is the same?
Or the kernel mode has a different mode to point a specified memory than the user mode?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Wow... Finally resolved!

Code: Select all

static char* data = NULL;

char const * GetBarraInf&#40;void&#41;
&#123;
    data = &#40;char*&#41;malloc&#40;20&#41;;
    strcpy&#40;data, "Hello World!"&#41;;
    return &#40;char const *&#41;&#40;&#40;&#40;int&#41;data&#41;&~0x80000000&#41;; 
&#125;
And before I've commented the loadmodule function, sorry! :(
But the 8002013A of
BadVAddr is == SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED ?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

no wonder.

you don't need the malloc stuff. Just be sure the pointer is a user address.
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

heh, im too late (please ignore)

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

EPC, Cause and Status what means?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

hilde, if I doesn't use malloc it crash!
But the address is a user one...
Maybe I've do something wrong...

Code: Select all

static char* data = "hellooo!!";

char* GetBarraInf&#40;void&#41;
&#123;
    return &#40;char*&#41;&#40;&#40;&#40;int&#41;data&#41;&~0x80000000&#41;; // USER_ADDR&#40;data&#41;;
&#125;
And in the main programm:

Code: Select all

char* GetBarraInf&#40;void&#41;;
Init&#40;&#41;; // pspDebugScreenInit
printf&#40;"%s", GetBarraInf&#40;&#41;&#41;; // pspDebugScreenPrintf
sleep&#40;2000000&#41;; // sceKernelDelayThread
The exception:

Code: Select all

Exception - Bus error &#40;data&#41;
EPC       - 0889A064
Location  - ra.text + 00096064
Cause     - 1000001C
Status    - 20008613
BadVAddr  - 01044410

zr&#58;00000000 at&#58;2008FF00 v0&#58;0824E205 v1&#58;0889CB20
a0&#58;0824E204 a1&#58;0BBBFE48 a2&#58;00000073 a3&#58;0BBBFE44
t0&#58;00000068 t1&#58;0000002B t2&#58;00000020 t3&#58;880E57F0
t4&#58;0BBBFE60 t5&#58;00001E04 t6&#58;0880A4AC t7&#58;20008600
s0&#58;00000000 s1&#58;FFFFFFFF s2&#58;0BBBEF4C s3&#58;0BBBEF10
s4&#58;DEADBEEF s5&#58;DEADBEEF s6&#58;088AB9E6 s7&#58;DEADBEEF
t8&#58;880E5818 t9&#58;00000028 k0&#58;0BBBFF00 k1&#58;00000000
gp&#58;08939230 sp&#58;0BBBEEE0 fp&#58;0824E204 ra&#58;0889D7AC
The problem is not the address I think!
But 0x0824E204 is a strange address, right?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Work only with this:

Code: Select all

static char* data;

char* GetBarraInf&#40;void&#41;
&#123;
    data = &#40;char*&#41;malloc&#40;20&#41;;
    strcpy&#40;data, "Hello World!!!"&#41;;
    return &#40;char*&#41;&#40;&#40;&#40;int&#41;data&#41;&~0x80000000&#41;; // USER_ADDR&#40;data&#41;;
&#125;
But I can't do that, I've to return a unsiged char array...
( unsigned char data[]; <-- builted with bin2o )
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Resolved with this... but I have to use malloc! Without that it crash!
But I haven't found the malloc function in the kernel libc, where I can find it?
I'll built the prx with the kernel libs and libc to reduce its size!
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

search these forums for a malloc function made by HellCat I think using the sce memory functions.

He made it exactly because kernel libc does not have it
Image
Upgrade your PSP
Post Reply