src/SDL_fatal.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 May 2013 00:43:22 -0700
changeset 7180 3733e68edbc3
parent 6885 700f1b25f77f
child 7336 b0ef4d043c7b
permissions -rw-r--r--
Fixed bug 1846 - _allmul implementation in SDL_stdlib.c doesn't clean up the stack

Colin Barrett

I see this manifest itself (VS2012 x86) as:

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

in the first call to SDL_GetTicks in my application. The disassembly at the problem line is:

hires_now.QuadPart *= 1000;
00AD0792 push 0
00AD0794 push 3E8h
00AD0799 mov eax,dword ptr [ebp-10h]
00AD079C push eax
00AD079D mov ecx,dword ptr [hires_now]
00AD07A0 push ecx
00AD07A1 call _allmul (0AE7D40h)
00AD07A6 mov dword ptr [hires_now],eax
00AD07A9 mov dword ptr [ebp-10h],edx

Apparently _allmul should be popping the stack but isn't (other similar functions in SDL_stdlib.c - _alldiv and whatnot - DO pop the stack).

A 'ret 10h' at the end of _allmul appears to do the trick
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* General fatal signal handling code for SDL */
    24 
    25 #ifdef HAVE_SIGNAL_H
    26 
    27 #include <signal.h>
    28 
    29 #include "SDL.h"
    30 #include "SDL_fatal.h"
    31 
    32 /* This installs some signal handlers for the more common fatal signals,
    33    so that if the programmer is lazy, the app doesn't die so horribly if
    34    the program crashes.
    35 */
    36 
    37 static void
    38 SDL_Parachute(int sig)
    39 {
    40     signal(sig, SIG_DFL);
    41     SDL_Quit();
    42     raise(sig);
    43 }
    44 
    45 static const int SDL_fatal_signals[] = {
    46     SIGSEGV,
    47 #ifdef SIGBUS
    48     SIGBUS,
    49 #endif
    50 #ifdef SIGFPE
    51     SIGFPE,
    52 #endif
    53 #ifdef SIGQUIT
    54     SIGQUIT,
    55 #endif
    56     0
    57 };
    58 
    59 void
    60 SDL_InstallParachute(void)
    61 {
    62     /* Set a handler for any fatal signal not already handled */
    63     int i;
    64 #ifdef HAVE_SIGACTION
    65     struct sigaction action;
    66 
    67     for (i = 0; SDL_fatal_signals[i]; ++i) {
    68         sigaction(SDL_fatal_signals[i], NULL, &action);
    69         if (action.sa_handler == SIG_DFL) {
    70             action.sa_handler = SDL_Parachute;
    71             sigaction(SDL_fatal_signals[i], &action, NULL);
    72         }
    73     }
    74 #ifdef SIGALRM
    75     /* Set SIGALRM to be ignored -- necessary on Solaris */
    76     sigaction(SIGALRM, NULL, &action);
    77     if (action.sa_handler == SIG_DFL) {
    78         action.sa_handler = SIG_IGN;
    79         sigaction(SIGALRM, &action, NULL);
    80     }
    81 #endif
    82 #else
    83     void (*ohandler) (int);
    84 
    85     for (i = 0; SDL_fatal_signals[i]; ++i) {
    86         ohandler = signal(SDL_fatal_signals[i], SDL_Parachute);
    87         if (ohandler != SIG_DFL) {
    88             signal(SDL_fatal_signals[i], ohandler);
    89         }
    90     }
    91 #endif /* HAVE_SIGACTION */
    92     return;
    93 }
    94 
    95 void
    96 SDL_UninstallParachute(void)
    97 {
    98     /* Remove a handler for any fatal signal handled */
    99     int i;
   100 #ifdef HAVE_SIGACTION
   101     struct sigaction action;
   102 
   103     for (i = 0; SDL_fatal_signals[i]; ++i) {
   104         sigaction(SDL_fatal_signals[i], NULL, &action);
   105         if (action.sa_handler == SDL_Parachute) {
   106             action.sa_handler = SIG_DFL;
   107             sigaction(SDL_fatal_signals[i], &action, NULL);
   108         }
   109     }
   110 #else
   111     void (*ohandler) (int);
   112 
   113     for (i = 0; SDL_fatal_signals[i]; ++i) {
   114         ohandler = signal(SDL_fatal_signals[i], SIG_DFL);
   115         if (ohandler != SDL_Parachute) {
   116             signal(SDL_fatal_signals[i], ohandler);
   117         }
   118     }
   119 #endif /* HAVE_SIGACTION */
   120 }
   121 
   122 #else
   123 
   124 /* No signals on this platform, nothing to do.. */
   125 
   126 void
   127 SDL_InstallParachute(void)
   128 {
   129     return;
   130 }
   131 
   132 void
   133 SDL_UninstallParachute(void)
   134 {
   135     return;
   136 }
   137 
   138 #endif /* HAVE_SIGNAL_H */
   139 /* vi: set ts=4 sw=4 expandtab: */