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 

[Solved] Pointer dereference crash while valid ptr (zziplib)

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



Joined: 26 Mar 2009
Posts: 33
Location: holland

PostPosted: Wed Apr 08, 2009 6:36 pm    Post subject: [Solved] Pointer dereference crash while valid ptr (zziplib) Reply with quote

Hi,

I got a weird zziplib crash and I don't really understand what is going wrong %)
It's in zzip.c, line 385 (I debugged with 'gcc -g' and psplink). A pointer representing a zip file entry is dereferenced, and the PSP crashes ("Program received signal SIGHUP, Hangup.").

I tried to decompose the code, and then rewrote it using bit shifts - and then it worked.
Do you know what went wrong here?

Here are my attempts:

Code:

   /* Original:
      hdr->d_off   = zzip_disk_entry_get_offset (d);
   */
   /* After macro processing:
      hdr->d_off   = (*(uint32_t*)((d)->z_offset));
   */
   /*
     Decompose:
     uint32_t* t_pointer = (uint32_t*)d->z_offset;
     uint32_t t_value = *t_pointer; // crashes here
     hdr->d_off = t_value;
   */
   /* Doesn't crash: */
   char* t_pointer2 = d->z_offset;
   unsigned char t_value0 = t_pointer2[0];
   unsigned char t_value1 = t_pointer2[1];
   unsigned char t_value2 = t_pointer2[2];
   unsigned char t_value3 = t_pointer2[3];
   hdr->d_off = (t_value3 << 24) | (t_value2 << 16) | (t_value1 << 8) | (t_value0);


Last edited by Beuc on Fri Apr 17, 2009 6:22 am; edited 1 time in total
Back to top
View user's profile Send private message
jbit
Site Admin


Joined: 28 May 2005
Posts: 293
Location: København, Danmark

PostPosted: Wed Apr 08, 2009 6:42 pm    Post subject: Reply with quote

My random guess is that d->z_offset isn't always four bytes aligned? Which is a real issue on most RISC machines ;)

The original is doing a 32bit load which needs the pointer to be aligned to 4bytes, and your "doesn't crash" code is doing 8bit loads which don't care.
Back to top
View user's profile Send private message Visit poster's website
Beuc



Joined: 26 Mar 2009
Posts: 33
Location: holland

PostPosted: Wed Apr 08, 2009 7:48 pm    Post subject: Reply with quote

This could well be the reason, the field is at offset 42 in the structure.
It didn't crash when I did it manually from GDB though!

I'm surprised it works for other devs. Admittedly my .zip is special as it's appened to EBOOT.PBP (with zip -A), in an attempt to create bundled resources, so it has a non-zero offset :) However the code that parses d->z_offset is always executed AFAICS.

I'm going to submit a patch for this; do you think my solution is fine, or is there a better way?
Back to top
View user's profile Send private message
Beuc



Joined: 26 Mar 2009
Posts: 33
Location: holland

PostPosted: Thu Apr 09, 2009 5:56 am    Post subject: Reply with quote

And here's the patch:
http://forums.ps2dev.org/viewtopic.php?t=11869

zziplib already has code similar to mine (__zzip_get32(c)), I made it use it in all little-endian cases.
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 -> PSP 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