flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > [solved] why "call SDL_Quit" segfault?

Author
Thread Post new topic Reply to topic
ginere



Joined: 15 Oct 2017
Posts: 12
hi, i'm trying to make a game using fasm + sdl, so i have to link the c library with gcc..
i'm having a weird problem, i have a cleanup routine that frees up the memory, and i call it from my main routine with
Code:
    call    cleanup
    


and cleanup is
Code:
cleanup:
    push    rax
    call       SDL_Quit
    pop      rax
    ret
    


now, this segfaults immediately. but, when i remove push and pop, and only leave it like this:
Code:
cleanup:
    call    SDL_Quit
    ret
    


it works.

can someone explain to my why the first example segfaults? it is like that first push somehow changes the return address of SDL_Quit.
Post 23 Jan 2019, 11:15
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16734
Location: In your JS exploiting you and your system
Does the function "SDL_Quit" expect any parameters passed into it on the stack?
Post 23 Jan 2019, 11:22
View user's profile Send private message Visit poster's website Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
no, sdl_quit is defined like this in sdl source:

void SDL_Quit(void)
Post 23 Jan 2019, 11:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16734
Location: In your JS exploiting you and your system
Where is the segfault? In the SDL library, or in other code?
Post 23 Jan 2019, 11:52
View user's profile Send private message Visit poster's website Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
if i put print functions above and below the sdl_quit, the bottom print does not execute, so it means it crashes inside SDL_Quit, I dont know how to use gdb, but it says this:
Code:
Thread 1 "b" received signal SIGSEGV, Segmentation fault.
0x00007ffff729d7a7 in ?? () from /usr/lib/libxcb.so.1
(gdb) backtrace
#0  0x00007ffff729d7a7 in ?? () from /usr/lib/libxcb.so.1
#1  0x00007ffff729e338 in ?? () from /usr/lib/libxcb.so.1
#2  0x00007ffff72f999f in ?? () from /usr/lib/libX11.so.6
#3  0x00007ffff72f9b21 in ?? () from /usr/lib/libX11.so.6
#4  0x00007ffff72f9e1d in _XEventsQueued () from /usr/lib/libX11.so.6
#5  0x00007ffff72fcc06 in _XGetRequest () from /usr/lib/libX11.so.6
#6  0x00007ffff72e094b in _XGetWindowAttributes () from /usr/lib/libX11.so.6
#7  0x00007ffff72e0b3a in XGetWindowAttributes () from /usr/lib/libX11.so.6
#8  0x00007ffff7f400c7 in ?? () from /usr/lib/libSDL2-2.0.so.0
#9  0x00007ffff7f0579d in ?? () from /usr/lib/libSDL2-2.0.so.0
#10 0x00007ffff7f087c8 in ?? () from /usr/lib/libSDL2-2.0.so.0
#11 0x00007ffff7f08b95 in ?? () from /usr/lib/libSDL2-2.0.so.0
#12 0x00007ffff7e85525 in ?? () from /usr/lib/libSDL2-2.0.so.0
#13 0x00007ffff7e856e8 in ?? () from /usr/lib/libSDL2-2.0.so.0
#14 0x00005555555554c2 in main ()
(gdb)

    
Post 23 Jan 2019, 12:18
View user's profile Send private message Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
the instruction at 0x00005555555554c2 is "pop rax" just below the SDL_Quit
Post 23 Jan 2019, 12:28
View user's profile Send private message Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
i miss those dos days.. if was so easy to do gui in assembly... this linking with various c libs to draw basic graphic on screen is complex and so hard..
Post 23 Jan 2019, 12:30
View user's profile Send private message Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
i will post the minimal code that shows this problem..
Post 23 Jan 2019, 12:31
View user's profile Send private message Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
ok it seems to work now, in every procedure that calls sdl functions i added
Code:
push    rsp
and     rsp, -$10
    


and
Code:
pop     rsp
    

at the end

it seems those sdl functions require aligned stack?
Post 23 Jan 2019, 13:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16734
Location: In your JS exploiting you and your system
ginere wrote:
ok it seems to work now, in every procedure that calls sdl functions i added
Code:
push    rsp
and     rsp, -$10
    


and
Code:
pop     rsp
    

at the end
You can't pop rsp like that unless the "and rsp,-$10" has no effect.
Post 23 Jan 2019, 14:56
View user's profile Send private message Visit poster's website Reply with quote
ginere



Joined: 15 Oct 2017
Posts: 12
ah yes.. sorry.. brain melt.. will save it another reg...

replaced it basically with stack frame:
Code:
push   rbp
mov    rbp, rsp
and    rsp, -$10
    


and at the end
Code:
mov   rsp, rbp
pop   rbp
    
Post 23 Jan 2019, 16:16
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.