src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 May 2013 00:43:22 -0700
changeset 7180 3733e68edbc3
parent 6885 700f1b25f77f
child 7191 75360622e65f
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 /* Get the name of the audio device we use for output */
    24 
    25 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    26 
    27 #include <fcntl.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include <unistd.h> /* For close() */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_audiodev_c.h"
    34 
    35 #ifndef _PATH_DEV_DSP
    36 #if defined(__NETBSD__) || defined(__OPENBSD__)
    37 #define _PATH_DEV_DSP  "/dev/audio"
    38 #else
    39 #define _PATH_DEV_DSP  "/dev/dsp"
    40 #endif
    41 #endif
    42 #ifndef _PATH_DEV_DSP24
    43 #define _PATH_DEV_DSP24	"/dev/sound/dsp"
    44 #endif
    45 #ifndef _PATH_DEV_AUDIO
    46 #define _PATH_DEV_AUDIO	"/dev/audio"
    47 #endif
    48 
    49 static inline void
    50 test_device(const char *fname, int flags, int (*test) (int fd),
    51             SDL_AddAudioDevice addfn)
    52 {
    53     struct stat sb;
    54     if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    55         const int audio_fd = open(fname, flags, 0);
    56         if (audio_fd >= 0) {
    57             if (test(audio_fd)) {
    58                 addfn(fname);
    59             }
    60             close(audio_fd);
    61         }
    62     }
    63 }
    64 
    65 static int
    66 test_stub(int fd)
    67 {
    68     return 1;
    69 }
    70 
    71 void
    72 SDL_EnumUnixAudioDevices(int iscapture, int classic, int (*test)(int fd),
    73                          SDL_AddAudioDevice addfn)
    74 {
    75     const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
    76     const char *audiodev;
    77     char audiopath[1024];
    78 
    79     if (test == NULL)
    80         test = test_stub;
    81 
    82     /* Figure out what our audio device is */
    83     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    84         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    85         if (classic) {
    86             audiodev = _PATH_DEV_AUDIO;
    87         } else {
    88             struct stat sb;
    89 
    90             /* Added support for /dev/sound/\* in Linux 2.4 */
    91             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    92                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    93                     && S_ISCHR(sb.st_mode))) {
    94                 audiodev = _PATH_DEV_DSP24;
    95             } else {
    96                 audiodev = _PATH_DEV_DSP;
    97             }
    98         }
    99     }
   100     test_device(audiodev, flags, test, addfn);
   101 
   102     if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
   103         int instance = 0;
   104         while (instance++ <= 64) {
   105             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   106                          "%s%d", audiodev, instance);
   107             test_device(audiopath, flags, test, addfn);
   108         }
   109     }
   110 }
   111 
   112 #endif /* Audio driver selection */
   113 /* vi: set ts=4 sw=4 expandtab: */