We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4350

Summary: GL_CreateRenderer() crashes when using opengl32.dll
Product: SDL Reporter: Cameron Gutman <cameron.gutman>
Component: renderAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: sezeroz
Version: 2.0.9   
Hardware: All   
OS: Windows 10   
Attachments: Fix SDL_CreateRenderer() crash

Description Cameron Gutman 2018-11-01 03:24:58 UTC
Created attachment 3432 [details]
Fix SDL_CreateRenderer() crash

I received a bug report that a user was seeing my app crashing randomly. We narrowed it down to likely being a display driver crash (but the app crash was not expected). When this happens, the D3D device loss triggers us to try to create a new renderer. This ends up hitting GL_CreateRenderer() after D3D9 renderer creation fails, where it tries to look up OpenGL functions with GL_LoadFunctions().

Since the display driver is recovering, we don't have a working third party OGL implementation. The stock opengl32.dll lacks some of the functions that SDL looks for in GL_LoadFunctions(). The first one that's missing is glBlendEquation() which was introduced in OGL 1.2. When we hit this one, GL_LoadFunctions() fails, causing GL_DestroyRenderer() to be called. This eventually ends up calling a null pointer when SDL tries to invoke data->glViewport() inside GL_UpdateViewport().

The attached patch avoids caling GL_DestroyRenderer() until GL_LoadFunctions() has succeeded. I've also updated the GLES2 code which also had the same bug.

0:000> .excr
rax=0000000000000438 rbx=000002cae06b16b0 rcx=0000000000000000
rdx=0000000000000438 rsi=000002cae06b16b0 rdi=000002cae06b14c0
rip=0000000000000000 rsp=000000dc2ab06598 rbp=000002cae06b14c0
 r8=0000000000000000  r9=0000000000000000 r10=000002cae06a9ea8
r11=000000dc2ab06590 r12=0000000000000000 r13=00000000000002d0
r14=000002cae06b16b0 r15=0000000000000001
iopl=0         nv up ei pl nz na pe nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010202
00000000`00000000 ??              ???
0:000> k
  *** Stack trace for last set context - .thread/.cxr resets it
 # Child-SP          RetAddr           Call Site
00 000000dc`2ab06598 00007ffa`26d586d4 0x0
01 000000dc`2ab065a0 00007ffa`26d54019 SDL2!GL_UpdateViewport+0xa4 [c:\users\aicom\sdl\src\render\opengl\sdl_render_gl.c @ 1081] 
02 000000dc`2ab06620 00007ffa`26d55053 SDL2!GL_ActivateRenderer+0x69 [c:\users\aicom\sdl\src\render\opengl\sdl_render_gl.c @ 304] 
03 000000dc`2ab06650 00007ffa`26d53f52 SDL2!GL_DestroyRenderer+0x33 [c:\users\aicom\sdl\src\render\opengl\sdl_render_gl.c @ 1599] 
04 000000dc`2ab066c0 00007ffa`26d5ef68 SDL2!GL_CreateRenderer+0x572 [c:\users\aicom\sdl\src\render\opengl\sdl_render_gl.c @ 570] 
05 000000dc`2ab06720 00007ff7`1f086e51 SDL2!SDL_CreateRenderer_REAL+0x138 [c:\users\aicom\sdl\src\render\sdl_render.c @ 349] 
06 000000dc`2ab06790 00000000`00000000 Moonlight!SdlRenderer::initialize+0x51 [c:\moonlight-qt\app\streaming\video\ffmpeg-renderers\sdlvid.cpp @ 69] 

0:000> ?? data
struct GL_RenderData * 0x000002ca`e06b16b0
   +0x000 context          : 0x00000000`00040000 Void
   +0x008 debug_enabled    : 0 ( SDL_FALSE )
   +0x00c GL_ARB_debug_output_supported : 0 ( SDL_FALSE )
   +0x010 errors           : 0n0
   +0x018 error_messages   : (null) 
   +0x020 next_error_callback : (null) 
   +0x028 next_error_userparam : (null) 
   +0x030 GL_ARB_texture_non_power_of_two_supported : 0 ( SDL_FALSE )
   +0x034 GL_ARB_texture_rectangle_supported : 0 ( SDL_FALSE )
   +0x038 current          : <anonymous-tag>
   +0x044 GL_EXT_framebuffer_object_supported : 0 ( SDL_FALSE )
   +0x048 framebuffers     : (null) 
   +0x050 glBegin          : 0x00007ffa`37ef6470     void  opengl32!glBegin+0
   +0x058 glBindTexture    : 0x00007ffa`37ef6d60     void  opengl32!glBindTexture+0
   +0x060 glBlendEquation  : (null) 
   +0x068 glBlendFuncSeparate : (null) 
   +0x070 glClear          : (null) 
   +0x078 glClearColor     : (null) 
   +0x080 glColor3fv       : (null) 
   +0x088 glColor4f        : (null) 
   +0x090 glDeleteTextures : (null) 
   +0x098 glDepthFunc      : (null) 
   +0x0a0 glDisable        : (null) 
   +0x0a8 glDrawPixels     : (null) 
   +0x0b0 glEnable         : (null) 
   +0x0b8 glEnd            : (null) 
   +0x0c0 glGenTextures    : (null) 
   +0x0c8 glGetError       : (null) 
   +0x0d0 glGetIntegerv    : (null) 
   +0x0d8 glGetPointerv    : (null) 
   +0x0e0 glGetString      : (null) 
   +0x0e8 glLineWidth      : (null) 
   +0x0f0 glLoadIdentity   : (null) 
   +0x0f8 glMatrixMode     : (null) 
   +0x100 glOrtho          : (null) 
   +0x108 glPixelStorei    : (null) 
   +0x110 glPointSize      : (null) 
   +0x118 glPopMatrix      : (null) 
   +0x120 glPushMatrix     : (null) 
   +0x128 glRasterPos2i    : (null) 
   +0x130 glReadBuffer     : (null) 
   +0x138 glReadPixels     : (null) 
   +0x140 glRectf          : (null) 
   +0x148 glRotated        : (null) 
   +0x150 glRotatef        : (null) 
   +0x158 glScissor        : (null) 
   +0x160 glShadeModel     : (null) 
   +0x168 glTexCoord2f     : (null) 
   +0x170 glTexEnvf        : (null) 
   +0x178 glTexImage2D     : (null) 
   +0x180 glTexParameteri  : (null) 
   +0x188 glTexSubImage2D  : (null) 
   +0x190 glTranslatef     : (null) 
   +0x198 glVertex2f       : (null) 
   +0x1a0 glVertex3fv      : (null) 
   +0x1a8 glViewport       : (null) 
   +0x1b0 GL_ARB_multitexture_supported : 0 ( SDL_FALSE )
   +0x1b8 glActiveTextureARB : (null) 
   +0x1c0 num_texture_units : 0n0
   +0x1c8 glGenFramebuffersEXT : (null) 
   +0x1d0 glDeleteFramebuffersEXT : (null) 
   +0x1d8 glFramebufferTexture2DEXT : (null) 
   +0x1e0 glBindFramebufferEXT : (null) 
   +0x1e8 glCheckFramebufferStatusEXT : (null) 
   +0x1f0 shaders          : (null)
Comment 1 Ozkan Sezer 2018-11-01 09:27:06 UTC
Sam:  This lookes correct to me and applied it:
http://hg.libsdl.org/SDL/rev/84618d571795

If you agree, then please close this.
Comment 2 Sam Lantinga 2018-11-01 10:03:29 UTC
Looks good, thanks!