From 56d93488ce83bf0c17d792833b68502d2b4ce46c Mon Sep 17 00:00:00 2001 From: Graham Sellers Date: Thu, 22 May 2014 16:57:16 -0400 Subject: [PATCH] Fix calling conventions for GL functions. The calling convention for all GL functions should be defined as GLAPIENTRY. On some platforms (esp. Linux), this is empty, resulting in whatever the default calling convention is for the compiler. On Windows, however, the calling convention should be __stdcall, which is _not_ the default of MSVC. Calling functions through pointers declared with the wrong convention results in stack corruption and ultimate doom, fire and brimstone. This change defines GLAPIENTRY in gl_types.h as __stdcall on WGL, and empty if it's not already defined. Throughout, I've used this define on anything that may be called through to GL or by GL (including things like the debug callback). --- src/voglcommon/gl_types.h | 7 +++++++ src/voglcommon/vogl_entrypoints.cpp | 4 ++-- src/voglcommon/vogl_entrypoints.h | 4 ++-- src/voglcommon/vogl_gl_replayer.h | 4 ++-- src/voglcommon/vogl_gl_utils.cpp | 2 +- src/voglcommon/vogl_gl_utils.h | 2 +- src/vogltrace/vogl_intercept.cpp | 4 ++-- 7 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/voglcommon/gl_types.h b/src/voglcommon/gl_types.h index fbfb3685..f168c25c 100644 --- a/src/voglcommon/gl_types.h +++ b/src/voglcommon/gl_types.h @@ -80,7 +80,14 @@ struct _cl_event; typedef GLintptr GLvdpauSurfaceNV; +#ifndef GLAPIENTRY +#if (VOGL_PLATFORM_HAS_WGL) +#define GLAPIENTRY __stdcall +#else #define GLAPIENTRY +#endif +#endif + typedef void(GLAPIENTRY *GLDEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *userParam); typedef void(GLAPIENTRY *GLDEBUGPROCARB)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *userParam); typedef void(GLAPIENTRY *GLDEBUGPROCAMD)(GLuint id, GLenum category, GLenum severity, GLsizei length, const GLchar *message, GLvoid *userParam); diff --git a/src/voglcommon/vogl_entrypoints.cpp b/src/voglcommon/vogl_entrypoints.cpp index efc68b9b..b5b8b415 100644 --- a/src/voglcommon/vogl_entrypoints.cpp +++ b/src/voglcommon/vogl_entrypoints.cpp @@ -49,7 +49,7 @@ static void *g_gl_func_epilog_func_user_data; // a user provided prolog/epilog funcs. This is where the *actual* GL/GLX driver funcs are called. //---------------------------------------------------------------------------------------------------------------------- #define DEF_PROTO(exported, category, ret, ret_type, num_params, func_name, args, params) \ - static ret VOGL_GLUER(vogl_direct_wrapper_, func_name) args \ + static ret GLAPIENTRY VOGL_GLUER(vogl_direct_wrapper_, func_name) args \ { \ void *pStack_data = NULL; \ if (g_gl_func_prolog_func_ptr) \ @@ -61,7 +61,7 @@ static void *g_gl_func_epilog_func_user_data; } #define DEF_PROTO_VOID(exported, category, ret, ret_type, num_params, func_name, args, params) \ - static void VOGL_GLUER(vogl_direct_wrapper_, func_name) args \ + static void GLAPIENTRY VOGL_GLUER(vogl_direct_wrapper_, func_name) args \ { \ void *pStack_data = NULL; \ if (g_gl_func_prolog_func_ptr) \ diff --git a/src/voglcommon/vogl_entrypoints.h b/src/voglcommon/vogl_entrypoints.h index 91f6a683..0799a6fc 100644 --- a/src/voglcommon/vogl_entrypoints.h +++ b/src/voglcommon/vogl_entrypoints.h @@ -33,8 +33,8 @@ // define GL/GLX function pointer typedefs, i.e. glXGetProcAddress_func_ptr_t //---------------------------------------------------------------------------------------------------------------------- -#define DEF_PROTO(exported, category, ret, ret_type, num_params, name, args, params) typedef ret(*name##_func_ptr_t) args; -#define DEF_PROTO_VOID(exported, category, ret, ret_type, num_params, name, args, params) typedef ret(*name##_func_ptr_t) args; +#define DEF_PROTO(exported, category, ret, ret_type, num_params, name, args, params) typedef ret(GLAPIENTRY *name##_func_ptr_t) args; +#define DEF_PROTO_VOID(exported, category, ret, ret_type, num_params, name, args, params) typedef ret(GLAPIENTRY *name##_func_ptr_t) args; #include "gl_glx_wgl_protos.inc" #undef DEF_PROTO #undef DEF_PROTO_VOID diff --git a/src/voglcommon/vogl_gl_replayer.h b/src/voglcommon/vogl_gl_replayer.h index a7c14e0b..d0cd7c33 100644 --- a/src/voglcommon/vogl_gl_replayer.h +++ b/src/voglcommon/vogl_gl_replayer.h @@ -1303,8 +1303,8 @@ class vogl_gl_replayer status_t switch_contexts(vogl_trace_context_ptr_value trace_context); // Loosely derived from http://www.altdevblogaday.com/2011/06/23/improving-opengl-error-messages/ - static void debug_callback_arb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); - static void debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); + static void GLAPIENTRY debug_callback_arb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); + static void GLAPIENTRY debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); bool is_extension_supported(const char *pExt); diff --git a/src/voglcommon/vogl_gl_utils.cpp b/src/voglcommon/vogl_gl_utils.cpp index fbb9c7d1..266bfd87 100644 --- a/src/voglcommon/vogl_gl_utils.cpp +++ b/src/voglcommon/vogl_gl_utils.cpp @@ -1954,7 +1954,7 @@ void vogl_format_debug_output_arb(char out_str[], size_t out_str_size, GLenum so //---------------------------------------------------------------------------------------------------------------------- // vogl_generic_arb_debug_callback //---------------------------------------------------------------------------------------------------------------------- -void vogl_generic_arb_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param) +void GLAPIENTRY vogl_generic_arb_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param) { VOGL_FUNC_TRACER diff --git a/src/voglcommon/vogl_gl_utils.h b/src/voglcommon/vogl_gl_utils.h index f8679947..bbd86b34 100644 --- a/src/voglcommon/vogl_gl_utils.h +++ b/src/voglcommon/vogl_gl_utils.h @@ -870,7 +870,7 @@ inline bool vogl_json_deserialize_vec4F(const json_node &src_node, vec4F &vec) // vogl_format_debug_output_arb //---------------------------------------------------------------------------------------------------------------------- void vogl_format_debug_output_arb(char out_str[], size_t out_str_size, GLenum source, GLenum type, GLuint id, GLenum severity, const char *msg); -void vogl_generic_arb_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); +void GLAPIENTRY vogl_generic_arb_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param); void vogl_enable_generic_context_debug_messages(); //---------------------------------------------------------------------------------------------------------------------- diff --git a/src/vogltrace/vogl_intercept.cpp b/src/vogltrace/vogl_intercept.cpp index bfcf8c61..e50ece2a 100644 --- a/src/vogltrace/vogl_intercept.cpp +++ b/src/vogltrace/vogl_intercept.cpp @@ -2725,7 +2725,7 @@ class vogl_context int m_current_display_list_handle; GLenum m_current_display_list_mode; - static void debug_callback_arb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param) + static void GLAPIENTRY debug_callback_arb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *pUser_param) { (void)length; @@ -4198,7 +4198,7 @@ static inline void vogl_write_packet_to_trace(vogl_trace_packet &packet) // gl/glx override functions //---------------------------------------------------------------------------------------------------------------------- template -static GLFuncType vogl_get_proc_address_helper_return_wrapper(GLFuncType (*realGetProcAddressFunc)(ProcNameType), ProcNameType procName) +static GLFuncType vogl_get_proc_address_helper_return_wrapper(GLFuncType (GLAPIENTRY *realGetProcAddressFunc)(ProcNameType), ProcNameType procName) { if (!procName) return NULL;