Skip to content

Fix 32-bit windows (use stdcall convention)#85

Open
bmmogensen wants to merge 1 commit into
JuliaGL:masterfrom
bmmogensen:bugfix/stdcall-win32
Open

Fix 32-bit windows (use stdcall convention)#85
bmmogensen wants to merge 1 commit into
JuliaGL:masterfrom
bmmogensen:bugfix/stdcall-win32

Conversation

@bmmogensen
Copy link
Copy Markdown

On 32-bit Windows, OpenGL functions use the stdcall calling convention (APIENTRY/WINAPI), but the @glfunc macro generates ccall without specifying a convention, which defaults to cdecl.

With cdecl, the caller adjusts the stack after the call. With stdcall, the callee pops the arguments before returning. When Julia calls a stdcall function assuming cdecl, both sides adjust the stack — corrupting it by the size of the arguments on every call. In a render loop with multiple GL calls per frame (glViewport, glClearColor, glClear, etc.), this quickly leads to EXCEPTION_ACCESS_VIOLATION crashes in unrelated Julia internals.

On 64-bit Windows (and all other platforms) there is only one calling convention, so this was never observed there.

The fix adds a stdcall convention to both ccall paths (static opengl32.dll symbols and dynamic getprocaddress pointers) when Sys.iswindows() && Sys.WORD_SIZE == 32.

On 32-bit Windows, OpenGL functions use the stdcall calling convention (APIENTRY/WINAPI), but the @glfunc macro generates ccall without specifying a convention, which defaults to cdecl.

With cdecl, the caller adjusts the stack after the call. With stdcall, the callee pops the arguments before returning. When Julia calls a stdcall function assuming cdecl, both sides adjust the stack — corrupting it by the size of the arguments on every call. In a render loop with multiple GL calls per frame (glViewport, glClearColor, glClear, etc.), this quickly leads to EXCEPTION_ACCESS_VIOLATION crashes in unrelated Julia internals.

On 64-bit Windows (and all other platforms) there is only one calling convention, so this was never observed there.

The fix adds a stdcall convention to both ccall paths (static opengl32.dll symbols and dynamic getprocaddress pointers) when Sys.iswindows() && Sys.WORD_SIZE == 32.
@SimonDanisch
Copy link
Copy Markdown
Member

Thank you! I'm wondering though, didnt Julia stop supporting win32?

@bmmogensen
Copy link
Copy Markdown
Author

https://julialang.org/downloads/support/ well it's still listed as a Tier 1 support. There are still many legacy enterprise applications that use 32-bit - believe it or not. That is also what lead me down this windy road :-)

@SimonDanisch
Copy link
Copy Markdown
Member

Fair enough 🤷 Well, I guess it wont hurt!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants