Date: Sun, 11 May 2003 19:59:06 +0300
authorSam Lantinga <slouken@libsdl.org>
Sat, 28 Jun 2003 17:27:33 +0000
changeset 638b0108e9dea53
parent 637 6862d4294870
child 639 6961e1c84e76
Date: Sun, 11 May 2003 19:59:06 +0300
From: Pasi K?rkk?inen
Subject: [PATCH] fix SDL OpenGL segfault with DRI/Mesa drivers and Glew

Hello!

The attached patch fixes a bug in SDL which causes SDL to crash in
X11_GL_Shutdown() if you are using DRI/Mesa drivers AND glew
(http://glew.sf.net).

The bug is caused by a namespace collision affecting dlsym() to fetch wrong
pointer for glXReleaseBuffersMESA() (uninitialized pointer from glew because
the extension is NOT supported by the driver) and then SDL calling it in
X11_GL_Shutdown().

SDL should check if the glXReleaseBuffersMESA() is really supported by the
driver (from the extensions string) before calling it.

Attached patch adds extension string parsing to check if
glXReleaseBuffersMESA() is really supported (and this way
prevents the segfault).

Availability of the extensions should be _always_ checked from the
extensions string rather than using dlsym()!

Please add it to the next version of SDL.

Thanks to gltron and author of glew to help fixing this.
src/video/x11/SDL_x11gl.c
src/video/x11/SDL_x11gl_c.h
     1.1 --- a/src/video/x11/SDL_x11gl.c	Sat Jun 28 17:16:52 2003 +0000
     1.2 +++ b/src/video/x11/SDL_x11gl.c	Sat Jun 28 17:27:33 2003 +0000
     1.3 @@ -229,11 +229,45 @@
     1.4  
     1.5  #ifdef HAVE_OPENGL
     1.6  
     1.7 +static int ExtensionSupported(const char *extension, const char *all_extensions)
     1.8 +{
     1.9 +	const GLubyte *extensions = NULL;
    1.10 +	const GLubyte *start;
    1.11 +	GLubyte *where, *terminator;
    1.12 +
    1.13 +	/* Extension names should not have spaces. */
    1.14 +	where = (GLubyte *) strchr(extension, ' ');
    1.15 +	if (where || *extension == '\0')
    1.16 +	      return 0;
    1.17 +	
    1.18 +	extensions = glGetString(GL_EXTENSIONS);
    1.19 +	/* It takes a bit of care to be fool-proof about parsing the
    1.20 +	 *      OpenGL extensions string. Don't be fooled by sub-strings,
    1.21 +	 *           etc. */
    1.22 +	
    1.23 +	start = extensions;
    1.24 +	
    1.25 +	for (;;)
    1.26 +	{
    1.27 +		where = (GLubyte *) strstr((const char *) start, extension);
    1.28 +		if (!where) break;
    1.29 +		
    1.30 +		terminator = where + strlen(extension);
    1.31 +		if (where == start || *(where - 1) == ' ')
    1.32 +	        if (*terminator == ' ' || *terminator == '\0') return 1;
    1.33 +						  
    1.34 +		start = terminator;
    1.35 +	}
    1.36 +	
    1.37 +	return 0;
    1.38 +}
    1.39 +
    1.40  /* Make the current context active */
    1.41  int X11_GL_MakeCurrent(_THIS)
    1.42  {
    1.43  	int retval;
    1.44 -
    1.45 +	const char *glx_extensions;
    1.46 +	
    1.47  	retval = 0;
    1.48  	if ( ! this->gl_data->glXMakeCurrent(GFX_Display,
    1.49  	                                     SDL_Window, glx_context) ) {
    1.50 @@ -242,6 +276,30 @@
    1.51  	}
    1.52  	XSync( GFX_Display, False );
    1.53  
    1.54 +	
    1.55 +	/* 
    1.56 +	 * The context is now current, check for glXReleaseBuffersMESA() 
    1.57 +	 * extension. If extension is _not_ supported, destroy the pointer 
    1.58 +	 * (to make sure it will not be called in X11_GL_Shutdown() ).
    1.59 +	 * 
    1.60 +	 * DRI/Mesa drivers include glXReleaseBuffersMESA() in the libGL.so, 
    1.61 +	 * but there's no need to call it (is is only needed for some old 
    1.62 +	 * non-DRI drivers).
    1.63 +	 * 
    1.64 +	 * When using for example glew (http://glew.sf.net), dlsym() for
    1.65 +	 * glXReleaseBuffersMESA() returns the pointer from the glew library
    1.66 +	 * (namespace conflict).
    1.67 +	 *
    1.68 +	 * The glXReleaseBuffersMESA() pointer in the glew is NULL, if the 
    1.69 +	 * driver doesn't support this extension. So blindly calling it will
    1.70 +	 * cause segfault with DRI/Mesa drivers!
    1.71 +	 * 
    1.72 +	 */
    1.73 +	
    1.74 +	glx_extensions = this->gl_data->glXQueryExtensionsString(GFX_Display, SDL_Screen);
    1.75 +	if (!ExtensionSupported("glXReleaseBuffersMESA", glx_extensions)) this->gl_data->glXReleaseBuffersMESA = NULL;
    1.76 +	
    1.77 +	
    1.78  	/* More Voodoo X server workarounds... Grr... */
    1.79  	SDL_Lock_EventThread();
    1.80  	X11_CheckDGAMouse(this);
    1.81 @@ -382,16 +440,21 @@
    1.82  		(void (*)(Display *, GLXDrawable)) dlsym(handle, "glXSwapBuffers");
    1.83  	this->gl_data->glXGetConfig =
    1.84  		(int (*)(Display *, XVisualInfo *, int, int *)) dlsym(handle, "glXGetConfig");
    1.85 +	this->gl_data->glXQueryExtensionsString =
    1.86 +		(const char (*)(Display *, int)) dlsym(handle, "glXQueryExtensionsString");
    1.87 +	
    1.88  	/* We don't compare below for this in case we're not using Mesa. */
    1.89  	this->gl_data->glXReleaseBuffersMESA =
    1.90  		(void (*)(Display *, GLXDrawable)) dlsym( handle, "glXReleaseBuffersMESA" );
    1.91 -
    1.92 +	
    1.93 +	
    1.94  	if ( (this->gl_data->glXChooseVisual == NULL) || 
    1.95  	     (this->gl_data->glXCreateContext == NULL) ||
    1.96  	     (this->gl_data->glXDestroyContext == NULL) ||
    1.97  	     (this->gl_data->glXMakeCurrent == NULL) ||
    1.98  	     (this->gl_data->glXSwapBuffers == NULL) ||
    1.99 -	     (this->gl_data->glXGetConfig == NULL) ) {
   1.100 +	     (this->gl_data->glXGetConfig == NULL) ||
   1.101 +	     (this->gl_data->glXQueryExtensionsString == NULL)) {
   1.102  		SDL_SetError("Could not retrieve OpenGL functions");
   1.103  		return -1;
   1.104  	}
     2.1 --- a/src/video/x11/SDL_x11gl_c.h	Sat Jun 28 17:16:52 2003 +0000
     2.2 +++ b/src/video/x11/SDL_x11gl_c.h	Sat Jun 28 17:27:33 2003 +0000
     2.3 @@ -77,6 +77,11 @@
     2.4  	 ( Display* dpy,
     2.5  	   GLXDrawable drawable );
     2.6  
     2.7 +    const char (*glXQueryExtensionsString)
     2.8 +	    ( Display* dpy,
     2.9 +	      int screen);
    2.10 +
    2.11 +    
    2.12  #endif /* HAVE_OPENGL */
    2.13  };
    2.14