Skip to content

Conversation

@satyam102006
Copy link
Contributor

Summary
This PR resolves a race condition in the OpenGL loader reported in issue #27965. When multiple threads called Switch_DeleteShader simultaneously, the global function pointer assignment was not synchronized, leading to potential crashes or undefined behavior.

Changes Made
Modified modules/core/src/gl_core_3_1.cpp.

Included the header.

Implemented std::once_flag and std::call_once inside Switch_DeleteShader to ensure the function pointer is initialized exactly once in a thread-safe manner.

DeleteShader = (PFNDELETESHADERPROC)IntGetProcAddress("glDeleteShader");
static std::once_flag flag;
std::call_once(flag, []() {
DeleteShader = (PFNDELETESHADERPROC)IntGetProcAddress("glDeleteShader");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use static DeleteShader?

Copy link
Contributor Author

@satyam102006 satyam102006 Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will think on it

Copy link
Contributor Author

@satyam102006 satyam102006 Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use static DeleteShader?

static local variable to solve this, because it breaks the "Self-Patching" optimization that this code relies on. Initial State: The global pointer DeleteShader points to the helper function Switch_DeleteShader.

First Call: You call DeleteShader(id). It goes to Switch_DeleteShader.

The Patch: Switch_DeleteShader finds the real driver address and overwrites the global DeleteShader variable.

Future Calls: The next time you call DeleteShader(id), the global variable now points directly to the driver. It skips Switch_DeleteShader entirely.

If change the code to use a static local variable

C++

// BAD APPROACH for this specific architecture
static void Switch_DeleteShader(GLuint shader)
{
// This is thread-safe (C++11 magic statics), BUT...
static PFNDELETESHADERPROC real_func = (PFNDELETESHADERPROC)IntGetProcAddress("glDeleteShader");

real_func(shader); 

}
The Problem: This code never updates the global DeleteShader pointer.

Result: gl::DeleteShader will always point to Switch_DeleteShader.

@asmorkalov
Copy link
Contributor

Also the same approach with static local variable should be applied to all OpenGL functions in the file.

@satyam102006
Copy link
Contributor Author

Also the same approach with static local variable should be applied to all OpenGL functions in the file.

i will try

@satyam102006
Copy link
Contributor Author

satyam102006 commented Nov 22, 2025

Also the same approach with static local variable should be applied to all OpenGL functions in the file.

i edited all 245 fuctions

please check my changed function and also give review where i have to improve

@satyam102006
Copy link
Contributor Author

@asmorkalov can you review it , thank you for your time

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants