SameBoy icon indicating copy to clipboard operation
SameBoy copied to clipboard

SIGSEV in run_gui (is_running=false) at SDL/gui.c:994

Open g4jc opened this issue 5 years ago • 2 comments

I'm building on Gentoo musl, and the program crashes in the following ways immediately when launched:

Built with -01 CFLAG: '''' Thread 1 "sameboy" received signal SIGFPE, Arithmetic exception. 0x0000000100023a3a in update_viewport () at SDL/gui.c:141 141 SDL/gui.c: No such file or directory. (gdb) bt full #0 0x0000000100023a3a in update_viewport () at SDL/gui.c:141 win_height = -8488 win_width = 32767 logical_height = 0 logical_width = x_factor = y_factor = new_width = new_height = #1 0x0000000100025f0c in main (argc=, argv=) at SDL/main.c:701 fullscreen = prefs_file = major = gl_context = minor = 0 '''' Appears to be a divide by zero, which can happen per SDL Wiki, especially if HDPI is enabled.

Built without optimizations -O0: '''' Thread 1 "sameboy" received signal SIGSEGV, Segmentation fault. 0x000000010003a51d in run_gui (is_running=false) at SDL/gui.c:994 994 SDL/gui.c: No such file or directory. (gdb) bt full #0 0x000000010003a51d in run_gui (is_running=false) at SDL/gui.c:994 background = 0x0 converted_background = 0x0 axis_active = {false, false} width = 32640 height = 2147464924 x_offset = 63 y_offset = 1088421824 __vla_expr0 = 13888969309415276544 event = {type = 249648, common = {type = 249648, timestamp = 1}, display = {type = 249648, timestamp = 1, display = 4154893744, event = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', data1 = -8320}, window = {type = 249648, timestamp = 1, windowID = 4154893744, event = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', data1 = -8320, data2 = 32767}, key = {type = 249648, timestamp = 1, windowID = 4154893744, state = 255 '\377', repeat = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', keysym = {scancode = 4294958976, sym = 32767, mod = 39694, unused = 1}}, edit = {type = 249648, timestamp = 1, windowID = 4154893744, text = "\377\177\000\000\200\337\377\377\377\177\000\000\016\233\003\000\001\000\000\000\340Q\364\367\377\177\000\000\000\000\000", start = 1080655872, length = 969892}, text = {type = 249648, timestamp = 1, windowID = 4154893744, text = "\377\177\000\000\200\337\377\377\377\177\000\000\016\233\003\000\001\000\000\000\340Q\364\367\377\177\000\000\000\000\000"}, motion = {type = 249648, timestamp = 1, windowID = 4154893744, which = 32767, state = 4294958976, x = 32767, y = 236302, xrel = 1, yrel = -134983200}, button = {type = 249648, timestamp = 1, windowID = 4154893744, which = 32767, button = 128 '\200', state = 223 '\337', clicks = 255 '\377', padding1 = 255 '\377', x = 32767, y = 236302}, wheel = {type = 249648, timestamp = 1, windowID = 4154893744, which = 32767, x = -8320, y = 32767, direction = 236302}, jaxis = {type = 249648, timestamp = 1, which = -140073552, axis = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', value = -8320, padding4 = 65535}, jball = {type = 249648, timestamp = 1, which = -140073552, ball = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', xrel = -8320, yrel = -1}, jhat = {type = 249648, timestamp = 1, which = -140073552, hat = 255 '\377', value = 127 '\177', padding1 = 0 '\000', padding2 = 0 '\000'}, jbutton = {type = 249648, timestamp = 1, which = -140073552, button = 255 '\377', state = 127 '\177', padding1 = 0 '\000', padding2 = 0 '\000'}, jdevice = { type = 249648, timestamp = 1, which = -140073552}, caxis = {type = 249648, timestamp = 1, which = -140073552, axis = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000', value = -8320, padding4 = 65535}, cbutton = {type = 249648, timestamp = 1, which = -140073552, button = 255 '\377', state = 127 '\177', padding1 = 0 '\000', padding2 = 0 '\000'}, cdevice = {type = 249648, timestamp = 1, which = -140073552}, adevice = {type = 249648, timestamp = 1, which = 4154893744, iscapture = 255 '\377', padding1 = 127 '\177', padding2 = 0 '\000', padding3 = 0 '\000'}, sensor = {type = 249648, timestamp = 1, which = -140073552, data = {4.59163468e-41, -nan(0x7fdf80), 4.59163468e-41, 3.3112963e-40, 1.40129846e-45, -9.9107895e+33}}, quit = { type = 249648, timestamp = 1}, user = {type = 249648, timestamp = 1, windowID = 4154893744, code = 32767, data1 = 0x7fffffffdf80, data2 = 0x100039b0e <update_viewport+494>}, syswm = {type = 249648, timestamp = 1, msg = 0x7ffff7a6a5b0}, tfinger = {type = 249648, timestamp = 1, touchId = 140737348281776, fingerId = 140737488347008, x = 3.3112963e-40, y = 1.40129846e-45, dx = -9.9107895e+33, dy = 4.59163468e-41, pressure = 0, windowID = 1080655872}, mgesture = {type = 249648, timestamp = 1, touchId = 140737348281776, dTheta = -nan(0x7fdf80), dDist = 4.59163468e-41, x = 3.3112963e-40, y = 1.40129846e-45, numFingers = 20960, padding = 63476}, dgesture = {type = 249648, timestamp = 1, touchId = 140737348281776, gestureId = 140737488347008, numFingers = 236302, error = 1.40129846e-45, x = -9.9107895e+33, y = 4.59163468e-41}, drop = {type = 249648, timestamp = 1, file = 0x7ffff7a6a5b0 "\270", windowID = 4294958976}, padding = "0\317\003\000\001\000\000\000\260\245\246\367\377\177\000\000\200\337\377\377\377\177\000\000\016\233\003\000\001\000\000\000\340Q\364\367\377\177\000\000\000\000\000\000\000\200i@\244\314\016\000\001\000\000"} should_render = false pixels = #1 0x000000010003d3fa in main (argc=1, argv=0x7fffffffe078) at SDL/main.c:704 fullscreen = false prefs_file = 0x0 gl_context = 0x0 major = 0 minor = 0 ''''

g4jc avatar Oct 21 '20 16:10 g4jc

The SDL wiki says it may differ from the size in pixels, not that it can be zero. The fact that the crash differs between optimization levels, all related to SDL, makes me suspect there's something more unusual going on. Can you see if Valgrind finds anything suspicious? Also, are you using the SameBoy build system as-is, or have modified it or overwrote variables via the command line?

Additionally, a built binary that corresponds to this crash log will be helpful.

LIJI32 avatar Oct 21 '20 16:10 LIJI32

Thanks for the quick reply! I have built it in the local folder with no custom variables.

I had someone using Void Linux give this and test (as they also use musl) and they said it does compile and runs properly. So it is not an issue in musl. Valgrind also found nothing of interest.

This lead me to believe something was wrong with my dependencies, so I decided to add some additional checks which confirmed this.

I used the following hacks during testing:

diff --git a/SDL/gui.c b/SDL/gui.c
index 62656e8d..b7ca98d5 100644
--- a/SDL/gui.c
+++ b/SDL/gui.c
@@ -989,17 +989,29 @@ void run_gui(bool is_running)
     
     /* Draw the background screen */
     static SDL_Surface *converted_background = NULL;
-    if (!converted_background) {
+
         SDL_Surface *background = SDL_LoadBMP(resource_path("background.bmp"));
-        SDL_SetPaletteColors(background->format->palette, gui_palette, 0, 4);
-        converted_background = SDL_ConvertSurface(background, pixel_format, 0);
-        SDL_LockSurface(converted_background);
-        SDL_FreeSurface(background);
+        if( background == NULL )
+        {
+               	printf( "Unable to load image! SDL Error: %s\n", SDL_GetError() );
+        }
+	else
+        {
+               	//Convert surface to screen format
+		SDL_SetPaletteColors(background->format->palette, gui_palette, 0, 4);
+                //converted_background = SDL_ConvertSurfaceFormat( background, SDL_PIXELFORMAT_RGBA8888, 0 );
+		converted_background = SDL_ConvertSurface(background, pixel_format, 0);
+                if( converted_background == NULL )
+                {
+                        printf( "Unable to optimize image! SDL Error: %s\n", SDL_GetError() );
+                }
+               	//Get rid of old loaded surface
+                SDL_FreeSurface( background );
+        }
         
         for (unsigned i = 4; i--; ) {
             gui_palette_native[i] = SDL_MapRGB(pixel_format, gui_palette[i].r, gui_palette[i].g, gui_palette[i].b);
         }
-    }
 
     unsigned width = GB_get_screen_width(&gb);
     unsigned height = GB_get_screen_height(&gb);
@@ -1329,7 +1341,7 @@ void run_gui(bool is_running)
                     memcpy(pixels + x_offset + width * (y + y_offset), ((uint32_t *)converted_background->pixels) + 160 * y, 160 * 4);
                 }
             }
-            
+
             switch (gui_state) {
                 case SHOWING_DROP_MESSAGE:
                     draw_text_centered(pixels, width, height, 8 + y_offset, "Press ESC for menu", gui_palette_native[3], gui_palette_native[0], false);
diff --git a/SDL/main.c b/SDL/main.c
index e79d0b33..87bfff09 100644
--- a/SDL/main.c
+++ b/SDL/main.c
@@ -665,6 +665,10 @@ int main(int argc, char **argv)
 
     window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                               160 * configuration.default_scale, 144 * configuration.default_scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
+    if( window == NULL )
+    {
+            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
+    }
     SDL_SetWindowMinimumSize(window, 160, 144);
     
     if (fullscreen) {

So as to the actual crash... SDL_PIXELFORMAT returns NULL, causing SDL_ConvertSurface(background, pixel_format, 0) to be NULL, resulting in the null pointer being carried down to SDL_LockSurface(converted_background);. Ultimately this results in surface=0x0 and a null pointer crash in libsdl2. Removing the LockSurface causes it to crash further down on the memcpy function which also calls converted_background.

The two printf errors I stubbed in threw the following alerts:

Window could not be created! SDL Error: GLX is not supported followed by Unable to optimize image! SDL Error: Unknown pixel format

I think the core problem here is my system's graphics driver has busted OpenGL (GLX) and there was no error check to bail out before crashing in the previously mentioned strange way.

Adding the two error checks may be helpful if anyone else comes across this problem, otherwise I think it's safe to close this as invalid. Sorry about that!

g4jc avatar Oct 22 '20 20:10 g4jc