src/dynapi/SDL_dynapi.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 18 Feb 2019 07:50:33 -0800
changeset 12612 07c39cbbeacf
parent 12503 806492103856
child 12961 8dbdfc918c7a
permissions -rw-r--r--
Fixed bug 4500 - Heap-Buffer Overflow in Map1toN pertaining to SDL_pixels.c

Petr Pisar

The reproducer has these data in BITMAPINFOHEADER:

biSize = 40
biBitCount = 8
biClrUsed = 131075

SDL_LoadBMP_RW() function passes biBitCount as a color depth to SDL_CreateRGBSurface(), thus 256-color pallete is allocated. But then biClrUsed colors are read from a file and stored into the palette. SDL_LoadBMP_RW should report an error if biClrUsed is greater than 2^biBitCount.
icculus@8094
     1
/*
icculus@8094
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
icculus@8094
     4
icculus@8094
     5
  This software is provided 'as-is', without any express or implied
icculus@8094
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@8094
     7
  arising from the use of this software.
icculus@8094
     8
icculus@8094
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@8094
    10
  including commercial applications, and to alter it and redistribute it
icculus@8094
    11
  freely, subject to the following restrictions:
icculus@8094
    12
icculus@8094
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@8094
    14
     claim that you wrote the original software. If you use this software
icculus@8094
    15
     in a product, an acknowledgment in the product documentation would be
icculus@8094
    16
     appreciated but is not required.
icculus@8094
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@8094
    18
     misrepresented as being the original software.
icculus@8094
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@8094
    20
*/
icculus@8094
    21
icculus@8094
    22
#include "SDL_config.h"
icculus@8094
    23
#include "SDL_dynapi.h"
icculus@8094
    24
icculus@8094
    25
#if SDL_DYNAMIC_API
icculus@8094
    26
icculus@11348
    27
#if defined(__OS2__)
icculus@11348
    28
#define INCL_DOS
icculus@11348
    29
#define INCL_DOSERRORS
sezeroz@12327
    30
#include <os2.h>
icculus@11348
    31
#include <dos.h>
icculus@11348
    32
#endif
icculus@11348
    33
icculus@8094
    34
#include "SDL.h"
icculus@8094
    35
slouken@11376
    36
/* These headers have system specific definitions, so aren't included above */
icculus@8094
    37
#include "SDL_syswm.h"
slouken@11376
    38
#include "SDL_vulkan.h"
icculus@8094
    39
icculus@8094
    40
/* This is the version of the dynamic API. This doesn't match the SDL version
icculus@8094
    41
   and should not change until there's been a major revamp in API/ABI.
icculus@8094
    42
   So 2.0.5 adds functions over 2.0.4? This number doesn't change;
icculus@8094
    43
   the sizeof (jump_table) changes instead. But 2.1.0 changes how a function
icculus@8094
    44
   works in an incompatible way or removes a function? This number changes,
icculus@8094
    45
   since sizeof (jump_table) isn't sufficient anymore. It's likely
icculus@8094
    46
   we'll forget to bump every time we add a function, so this is the
icculus@8094
    47
   failsafe switch for major API change decisions. Respect it and use it
icculus@8094
    48
   sparingly. */
icculus@8094
    49
#define SDL_DYNAPI_VERSION 1
icculus@8094
    50
icculus@8094
    51
static void SDL_InitDynamicAPI(void);
icculus@8094
    52
icculus@8094
    53
icculus@8094
    54
/* BE CAREFUL CALLING ANY SDL CODE IN HERE, IT WILL BLOW UP.
icculus@8094
    55
   Even self-contained stuff might call SDL_Error and break everything. */
icculus@8094
    56
icculus@8094
    57
icculus@8094
    58
/* behold, the macro salsa! */
icculus@8094
    59
icculus@8094
    60
/* !!! FIXME: ...disabled...until we write it.  :) */
icculus@8094
    61
#define DISABLE_JUMP_MAGIC 1
icculus@8094
    62
icculus@8094
    63
#if DISABLE_JUMP_MAGIC
icculus@8094
    64
/* Can't use the macro for varargs nonsense. This is atrocious. */
icculus@8094
    65
#define SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, logname, prio) \
slouken@11360
    66
    _static void SDLCALL SDL_Log##logname##name(int category, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    67
        va_list ap; initcall; va_start(ap, fmt); \
icculus@8094
    68
        jump_table.SDL_LogMessageV(category, SDL_LOG_PRIORITY_##prio, fmt, ap); \
icculus@8094
    69
        va_end(ap); \
icculus@8094
    70
    }
icculus@8094
    71
icculus@8094
    72
#define SDL_DYNAPI_VARARGS(_static, name, initcall) \
slouken@11360
    73
    _static int SDLCALL SDL_SetError##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    74
        char buf[512]; /* !!! FIXME: dynamic allocation */ \
icculus@8094
    75
        va_list ap; initcall; va_start(ap, fmt); \
icculus@8094
    76
        jump_table.SDL_vsnprintf(buf, sizeof (buf), fmt, ap); \
icculus@8094
    77
        va_end(ap); \
icculus@8094
    78
        return jump_table.SDL_SetError("%s", buf); \
icculus@8094
    79
    } \
slouken@11360
    80
    _static int SDLCALL SDL_sscanf##name(const char *buf, SDL_SCANF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    81
        int retval; va_list ap; initcall; va_start(ap, fmt); \
icculus@8094
    82
        retval = jump_table.SDL_vsscanf(buf, fmt, ap); \
icculus@8094
    83
        va_end(ap); \
icculus@8094
    84
        return retval; \
icculus@8094
    85
    } \
slouken@11360
    86
    _static int SDLCALL SDL_snprintf##name(SDL_OUT_Z_CAP(maxlen) char *buf, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    87
        int retval; va_list ap; initcall; va_start(ap, fmt); \
slouken@8820
    88
        retval = jump_table.SDL_vsnprintf(buf, maxlen, fmt, ap); \
icculus@8094
    89
        va_end(ap); \
icculus@8094
    90
        return retval; \
icculus@8094
    91
    } \
slouken@11360
    92
    _static void SDLCALL SDL_Log##name(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    93
        va_list ap; initcall; va_start(ap, fmt); \
icculus@8094
    94
        jump_table.SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap); \
icculus@8094
    95
        va_end(ap); \
icculus@8094
    96
    } \
slouken@11360
    97
    _static void SDLCALL SDL_LogMessage##name(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) { \
icculus@8094
    98
        va_list ap; initcall; va_start(ap, fmt); \
icculus@8094
    99
        jump_table.SDL_LogMessageV(category, priority, fmt, ap); \
icculus@8094
   100
        va_end(ap); \
icculus@8094
   101
    } \
icculus@8094
   102
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Verbose, VERBOSE) \
icculus@8094
   103
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Debug, DEBUG) \
icculus@8094
   104
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Info, INFO) \
icculus@8094
   105
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Warn, WARN) \
icculus@8094
   106
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Error, ERROR) \
icculus@8094
   107
    SDL_DYNAPI_VARARGS_LOGFN(_static, name, initcall, Critical, CRITICAL)
icculus@8094
   108
#endif
icculus@8094
   109
icculus@8094
   110
icculus@8094
   111
/* Typedefs for function pointers for jump table, and predeclare funcs */
icculus@8094
   112
/* The DEFAULT funcs will init jump table and then call real function. */
icculus@8094
   113
/* The REAL funcs are the actual functions, name-mangled to not clash. */
icculus@8094
   114
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
slouken@11360
   115
    typedef rc (SDLCALL *SDL_DYNAPIFN_##fn) params; \
slouken@11360
   116
    static rc SDLCALL fn##_DEFAULT params; \
slouken@11360
   117
    extern rc SDLCALL fn##_REAL params;
icculus@8094
   118
#include "SDL_dynapi_procs.h"
icculus@8094
   119
#undef SDL_DYNAPI_PROC
icculus@8094
   120
icculus@8094
   121
/* The jump table! */
icculus@8094
   122
typedef struct {
icculus@8094
   123
    #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) SDL_DYNAPIFN_##fn fn;
icculus@8094
   124
    #include "SDL_dynapi_procs.h"
icculus@8094
   125
    #undef SDL_DYNAPI_PROC
icculus@8094
   126
} SDL_DYNAPI_jump_table;
icculus@8094
   127
icculus@8094
   128
/* Predeclare the default functions for initializing the jump table. */
slouken@11360
   129
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret) static rc SDLCALL fn##_DEFAULT params;
icculus@8094
   130
#include "SDL_dynapi_procs.h"
icculus@8094
   131
#undef SDL_DYNAPI_PROC
icculus@8094
   132
icculus@8094
   133
/* The actual jump table. */
icculus@8094
   134
static SDL_DYNAPI_jump_table jump_table = {
icculus@8094
   135
    #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) fn##_DEFAULT,
icculus@8094
   136
    #include "SDL_dynapi_procs.h"
icculus@8094
   137
    #undef SDL_DYNAPI_PROC
icculus@8094
   138
};
icculus@8094
   139
icculus@8094
   140
/* Default functions init the function table then call right thing. */
icculus@8094
   141
#if DISABLE_JUMP_MAGIC
icculus@8094
   142
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
slouken@11360
   143
    static rc SDLCALL fn##_DEFAULT params { \
icculus@8094
   144
        SDL_InitDynamicAPI(); \
icculus@8094
   145
        ret jump_table.fn args; \
icculus@8094
   146
    }
icculus@8094
   147
#define SDL_DYNAPI_PROC_NO_VARARGS 1
icculus@8094
   148
#include "SDL_dynapi_procs.h"
icculus@8094
   149
#undef SDL_DYNAPI_PROC
icculus@8094
   150
#undef SDL_DYNAPI_PROC_NO_VARARGS
icculus@8094
   151
SDL_DYNAPI_VARARGS(static, _DEFAULT, SDL_InitDynamicAPI())
icculus@8094
   152
#else
icculus@8094
   153
/* !!! FIXME: need the jump magic. */
icculus@8094
   154
#error Write me.
icculus@8094
   155
#endif
icculus@8094
   156
icculus@8094
   157
/* Public API functions to jump into the jump table. */
icculus@8094
   158
#if DISABLE_JUMP_MAGIC
icculus@8094
   159
#define SDL_DYNAPI_PROC(rc,fn,params,args,ret) \
slouken@11360
   160
    rc SDLCALL fn params { ret jump_table.fn args; }
icculus@8094
   161
#define SDL_DYNAPI_PROC_NO_VARARGS 1
icculus@8094
   162
#include "SDL_dynapi_procs.h"
icculus@8094
   163
#undef SDL_DYNAPI_PROC
icculus@8094
   164
#undef SDL_DYNAPI_PROC_NO_VARARGS
icculus@8094
   165
SDL_DYNAPI_VARARGS(,,)
icculus@8094
   166
#else
icculus@8094
   167
/* !!! FIXME: need the jump magic. */
icculus@8094
   168
#error Write me.
icculus@8094
   169
#endif
icculus@8094
   170
icculus@11982
   171
/* we make this a static function so we can call the correct one without the
icculus@11982
   172
   system's dynamic linker resolving to the wrong version of this. */
icculus@11982
   173
static Sint32
icculus@11982
   174
initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
icculus@8094
   175
{
icculus@8094
   176
    SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
icculus@8094
   177
icculus@8094
   178
    if (apiver != SDL_DYNAPI_VERSION) {
icculus@8094
   179
        /* !!! FIXME: can maybe handle older versions? */
icculus@8094
   180
        return -1;  /* not compatible. */
icculus@8094
   181
    } else if (tablesize > sizeof (jump_table)) {
icculus@8094
   182
        return -1;  /* newer version of SDL with functions we can't provide. */
icculus@8094
   183
    }
icculus@8094
   184
icculus@8094
   185
    /* Init our jump table first. */
icculus@8094
   186
    #define SDL_DYNAPI_PROC(rc,fn,params,args,ret) jump_table.fn = fn##_REAL;
icculus@8094
   187
    #include "SDL_dynapi_procs.h"
icculus@8094
   188
    #undef SDL_DYNAPI_PROC
icculus@8094
   189
icculus@8094
   190
    /* Then the external table... */
icculus@8094
   191
    if (output_jump_table != &jump_table) {
icculus@8094
   192
        jump_table.SDL_memcpy(output_jump_table, &jump_table, tablesize);
icculus@8094
   193
    }
icculus@8094
   194
icculus@8094
   195
    /* Safe to call SDL functions now; jump table is initialized! */
icculus@8094
   196
icculus@8094
   197
    return 0;  /* success! */
icculus@8094
   198
}
icculus@8094
   199
icculus@8094
   200
icculus@11982
   201
/* Here's the exported entry point that fills in the jump table. */
icculus@11982
   202
/*  Use specific types when an "int" might suffice to keep this sane. */
icculus@11982
   203
typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
icculus@11982
   204
extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
icculus@11982
   205
icculus@11982
   206
Sint32
icculus@11982
   207
SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
icculus@11982
   208
{
icculus@11982
   209
    return initialize_jumptable(apiver, table, tablesize);
icculus@11982
   210
}
icculus@11982
   211
icculus@11982
   212
icculus@8094
   213
/* Obviously we can't use SDL_LoadObject() to load SDL.  :)  */
icculus@8094
   214
/* Also obviously, we never close the loaded library. */
icculus@8101
   215
#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
icculus@8094
   216
#ifndef WIN32_LEAN_AND_MEAN
icculus@8094
   217
#define WIN32_LEAN_AND_MEAN 1
icculus@8094
   218
#endif
icculus@8094
   219
#include <windows.h>
icculus@8094
   220
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
icculus@8094
   221
{
icculus@8094
   222
    HANDLE lib = LoadLibraryA(fname);
icculus@9305
   223
    void *retval = NULL;
icculus@9305
   224
    if (lib) {
icculus@9305
   225
        retval = GetProcAddress(lib, sym);
icculus@9305
   226
        if (retval == NULL) {
icculus@9305
   227
            FreeLibrary(lib);
icculus@9305
   228
        }
icculus@9305
   229
    }
icculus@9305
   230
    return retval;
icculus@8094
   231
}
icculus@8094
   232
icculus@11119
   233
#elif defined(unix) || defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__QNX__)
icculus@8094
   234
#include <dlfcn.h>
icculus@8094
   235
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
icculus@8094
   236
{
icculus@8094
   237
    void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
icculus@9305
   238
    void *retval = NULL;
icculus@9305
   239
    if (lib != NULL) {
icculus@9305
   240
        retval = dlsym(lib, sym);
icculus@9305
   241
        if (retval == NULL) {
icculus@9305
   242
            dlclose(lib);
icculus@9305
   243
        }
icculus@9305
   244
    }
icculus@9305
   245
    return retval;
icculus@8094
   246
}
icculus@11345
   247
icculus@11345
   248
#elif defined(__OS2__)
icculus@11345
   249
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
icculus@11345
   250
{
icculus@11345
   251
    HMODULE hmodule;
icculus@11345
   252
    PFN retval = NULL;
icculus@11345
   253
    char error[256];
slouken@11359
   254
    if (DosLoadModule(&error, sizeof(error), fname, &hmodule) == NO_ERROR) {
slouken@11359
   255
        if (DosQueryProcAddr(hmodule, 0, sym, &retval) != NO_ERROR) {
icculus@11345
   256
            DosFreeModule(hmodule);
icculus@11345
   257
        }
icculus@11345
   258
    }
icculus@11345
   259
    return (void *) retval;
icculus@11345
   260
}
icculus@11345
   261
icculus@8094
   262
#else
icculus@8094
   263
#error Please define your platform.
icculus@8094
   264
#endif
icculus@8094
   265
icculus@8094
   266
icculus@8094
   267
static void
icculus@8094
   268
SDL_InitDynamicAPILocked(void)
icculus@8094
   269
{
icculus@8094
   270
    const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
icculus@11982
   271
    SDL_DYNAPI_ENTRYFN entry = NULL;  /* funcs from here by default. */
icculus@8094
   272
icculus@8094
   273
    if (libname) {
sezero@11872
   274
        entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
icculus@8094
   275
        if (!entry) {
icculus@8094
   276
            /* !!! FIXME: fail to startup here instead? */
icculus@8094
   277
            /* !!! FIXME: definitely warn user. */
icculus@8094
   278
            /* Just fill in the function pointers from this library. */
icculus@8094
   279
        }
icculus@8094
   280
    }
icculus@8094
   281
icculus@11982
   282
    if (!entry || (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0)) {
icculus@8094
   283
        /* !!! FIXME: fail to startup here instead? */
icculus@8094
   284
        /* !!! FIXME: definitely warn user. */
icculus@8094
   285
        /* Just fill in the function pointers from this library. */
icculus@11982
   286
        if (!entry) {
icculus@11982
   287
            if (!initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
icculus@8094
   288
                /* !!! FIXME: now we're screwed. Should definitely abort now. */
icculus@8094
   289
            }
icculus@8094
   290
        }
icculus@8094
   291
    }
icculus@8094
   292
icculus@8094
   293
    /* we intentionally never close the newly-loaded lib, of course. */
icculus@8094
   294
}
icculus@8094
   295
icculus@8094
   296
static void
icculus@8094
   297
SDL_InitDynamicAPI(void)
icculus@8094
   298
{
icculus@8094
   299
    /* So the theory is that every function in the jump table defaults to
icculus@8094
   300
     *  calling this function, and then replaces itself with a version that
icculus@8094
   301
     *  doesn't call this function anymore. But it's possible that, in an
icculus@8094
   302
     *  extreme corner case, you can have a second thread hit this function
icculus@8094
   303
     *  while the jump table is being initialized by the first.
icculus@8094
   304
     * In this case, a spinlock is really painful compared to what spinlocks
icculus@8094
   305
     *  _should_ be used for, but this would only happen once, and should be
icculus@8094
   306
     *  insanely rare, as you would have to spin a thread outside of SDL (as
icculus@8094
   307
     *  SDL_CreateThread() would also call this function before building the
icculus@8094
   308
     *  new thread).
icculus@8094
   309
     */
icculus@10003
   310
    static SDL_bool already_initialized = SDL_FALSE;
icculus@8094
   311
icculus@8094
   312
    /* SDL_AtomicLock calls SDL mutex functions to emulate if
icculus@8094
   313
       SDL_ATOMIC_DISABLED, which we can't do here, so in such a
icculus@8094
   314
       configuration, you're on your own. */
icculus@8094
   315
    #if !SDL_ATOMIC_DISABLED
icculus@8094
   316
    static SDL_SpinLock lock = 0;
icculus@8094
   317
    SDL_AtomicLock_REAL(&lock);
icculus@8094
   318
    #endif
icculus@8094
   319
icculus@8094
   320
    if (!already_initialized) {
icculus@8094
   321
        SDL_InitDynamicAPILocked();
icculus@8094
   322
        already_initialized = SDL_TRUE;
icculus@8094
   323
    }
icculus@8094
   324
icculus@8094
   325
    #if !SDL_ATOMIC_DISABLED
icculus@8094
   326
    SDL_AtomicUnlock_REAL(&lock);
icculus@8094
   327
    #endif
icculus@8094
   328
}
icculus@8094
   329
icculus@8094
   330
#endif  /* SDL_DYNAMIC_API */
icculus@8094
   331
icculus@8094
   332
/* vi: set ts=4 sw=4 expandtab: */