include/begin_code.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 May 2013 00:43:22 -0700
changeset 7180 3733e68edbc3
parent 7054 9a9d05664124
child 7191 75360622e65f
child 8460 f483e8bdae3c
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 
    22 /**
    23  *  \file begin_code.h
    24  *
    25  *  This file sets things up for C dynamic library function definitions,
    26  *  static inlined functions, and structures aligned at 4-byte alignment.
    27  *  If you don't like ugly C preprocessor code, don't look at this file. :)
    28  */
    29 
    30 /* This shouldn't be nested -- included it around code only. */
    31 #ifdef _begin_code_h
    32 #error Nested inclusion of begin_code.h
    33 #endif
    34 #define _begin_code_h
    35 
    36 /* Some compilers use a special export keyword */
    37 #ifndef DECLSPEC
    38 # if defined(__BEOS__) || defined(__HAIKU__)
    39 #  if defined(__GNUC__)
    40 #   define DECLSPEC	__declspec(dllexport)
    41 #  else
    42 #   define DECLSPEC	__declspec(export)
    43 #  endif
    44 # elif defined(__WIN32__)
    45 #  ifdef __BORLANDC__
    46 #   ifdef BUILD_SDL
    47 #    define DECLSPEC
    48 #   else
    49 #    define DECLSPEC	__declspec(dllimport)
    50 #   endif
    51 #  else
    52 #   define DECLSPEC	__declspec(dllexport)
    53 #  endif
    54 # else
    55 #  if defined(__GNUC__) && __GNUC__ >= 4
    56 #   define DECLSPEC	__attribute__ ((visibility("default")))
    57 #  else
    58 #   define DECLSPEC
    59 #  endif
    60 # endif
    61 #endif
    62 
    63 /* By default SDL uses the C calling convention */
    64 #ifndef SDLCALL
    65 #if defined(__WIN32__) && !defined(__GNUC__)
    66 #define SDLCALL __cdecl
    67 #else
    68 #define SDLCALL
    69 #endif
    70 #endif /* SDLCALL */
    71 
    72 /* Removed DECLSPEC on Symbian OS because SDL cannot be a DLL in EPOC */
    73 #ifdef __SYMBIAN32__
    74 #undef DECLSPEC
    75 #define DECLSPEC
    76 #endif /* __SYMBIAN32__ */
    77 
    78 /* Force structure packing at 4 byte alignment.
    79    This is necessary if the header is included in code which has structure
    80    packing set to an alternate value, say for loading structures from disk.
    81    The packing is reset to the previous value in close_code.h
    82  */
    83 #if defined(_MSC_VER) || defined(__MWERKS__) || defined(__BORLANDC__)
    84 #ifdef _MSC_VER
    85 #pragma warning(disable: 4103)
    86 #endif
    87 #ifdef __BORLANDC__
    88 #pragma nopackwarning
    89 #endif
    90 #ifdef _M_X64
    91 /* Use 8-byte alignment on 64-bit architectures, so pointers are aligned */
    92 #pragma pack(push,8)
    93 #else
    94 #pragma pack(push,4)
    95 #endif
    96 #endif /* Compiler needs structure packing set */
    97 
    98 /* Set up compiler-specific options for inlining functions */
    99 #ifndef SDL_INLINE_OKAY
   100 #ifdef __GNUC__
   101 #define SDL_INLINE_OKAY
   102 #else
   103 /* Add any special compiler-specific cases here */
   104 #if defined(_MSC_VER) || defined(__BORLANDC__) || \
   105     defined(__DMC__) || defined(__SC__) || \
   106     defined(__WATCOMC__) || defined(__LCC__) || \
   107     defined(__DECC)
   108 #ifndef __inline__
   109 #define __inline__	__inline
   110 #endif
   111 #define SDL_INLINE_OKAY
   112 #else
   113 #if !defined(__MRC__) && !defined(_SGI_SOURCE)
   114 #ifndef __inline__
   115 #define __inline__ inline
   116 #endif
   117 #define SDL_INLINE_OKAY
   118 #endif /* Not a funky compiler */
   119 #endif /* Visual C++ */
   120 #endif /* GNU C */
   121 #endif /* SDL_INLINE_OKAY */
   122 
   123 /* If inlining isn't supported, remove "__inline__", turning static
   124    inlined functions into static functions (resulting in code bloat
   125    in all files which include the offending header files)
   126 */
   127 #ifndef SDL_INLINE_OKAY
   128 #define __inline__
   129 #endif
   130 
   131 #ifndef SDL_FORCE_INLINE
   132 #if defined(_MSC_VER)
   133 #define SDL_FORCE_INLINE __forceinline
   134 #elif ( (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) )
   135 #define SDL_FORCE_INLINE __attribute__((always_inline)) static inline
   136 #else
   137 #define SDL_FORCE_INLINE static __inline__
   138 #endif
   139 #endif
   140 
   141 /* Apparently this is needed by several Windows compilers */
   142 #if !defined(__MACH__)
   143 #ifndef NULL
   144 #ifdef __cplusplus
   145 #define NULL 0
   146 #else
   147 #define NULL ((void *)0)
   148 #endif
   149 #endif /* NULL */
   150 #endif /* ! Mac OS X - breaks precompiled headers */