Don't use SDL locking functions in our open()/close() hooks

Other shared library constructors can invoke open()/close() before
SDL2-compat's constructor runs to load SDL3 and populate the SDL3
function table. This causes SDL_AtomicLock()/SDL_AtomicUnlock()
to jump to 0.

See #1707
This commit is contained in:
Cameron Gutman
2025-10-09 20:03:19 -05:00
parent ae1c65805c
commit 490aa5082f
2 changed files with 15 additions and 10 deletions
+2
View File
@@ -9,7 +9,9 @@
// The specific kernel change required to run without root is: // The specific kernel change required to run without root is:
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=45bc3d26c95a8fc63a7d8668ca9e57ef0883351c // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=45bc3d26c95a8fc63a7d8668ca9e57ef0883351c
#ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#endif
// NOTE: This file MUST NOT include fcntl.h due to open() -> open64() // NOTE: This file MUST NOT include fcntl.h due to open() -> open64()
// redirection that happens when _FILE_OFFSET_BITS=64! // redirection that happens when _FILE_OFFSET_BITS=64!
+13 -10
View File
@@ -2,7 +2,9 @@
// which must be in a separate compilation unit due to fcntl.h doing // which must be in a separate compilation unit due to fcntl.h doing
// unwanted redirection of open() to open64(). // unwanted redirection of open() to open64().
#ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#endif
#include "SDL_compat.h" #include "SDL_compat.h"
#include <dlfcn.h> #include <dlfcn.h>
@@ -11,6 +13,7 @@
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <pthread.h>
#include <xf86drm.h> #include <xf86drm.h>
#include <xf86drmMode.h> #include <xf86drmMode.h>
@@ -33,7 +36,7 @@ extern struct stat g_DrmMasterStat;
#define MAX_SDL_FD_COUNT 8 #define MAX_SDL_FD_COUNT 8
int g_SdlDrmMasterFds[MAX_SDL_FD_COUNT]; int g_SdlDrmMasterFds[MAX_SDL_FD_COUNT];
int g_SdlDrmMasterFdCount = 0; int g_SdlDrmMasterFdCount = 0;
SDL_SpinLock g_FdTableLock = 0; pthread_mutex_t g_FdTableLock = PTHREAD_MUTEX_INITIALIZER;
// Caller must hold g_FdTableLock // Caller must hold g_FdTableLock
int getSdlFdEntryIndex(bool unused) int getSdlFdEntryIndex(bool unused)
@@ -55,7 +58,7 @@ int getSdlFdEntryIndex(bool unused)
// Returns true if the final SDL FD was removed // Returns true if the final SDL FD was removed
bool removeSdlFd(int fd) bool removeSdlFd(int fd)
{ {
SDL_AtomicLock(&g_FdTableLock); pthread_mutex_lock(&g_FdTableLock);
if (g_SdlDrmMasterFdCount != 0) { if (g_SdlDrmMasterFdCount != 0) {
// Clear the entry for this fd from the table // Clear the entry for this fd from the table
for (int i = 0; i < MAX_SDL_FD_COUNT; i++) { for (int i = 0; i < MAX_SDL_FD_COUNT; i++) {
@@ -67,11 +70,11 @@ bool removeSdlFd(int fd)
} }
if (g_SdlDrmMasterFdCount == 0) { if (g_SdlDrmMasterFdCount == 0) {
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
return true; return true;
} }
} }
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
return false; return false;
} }
@@ -82,12 +85,12 @@ int takeMasterFromSdlFd()
// Since all SDL FDs are actually dups of each other // Since all SDL FDs are actually dups of each other
// we can take master from any one of them. // we can take master from any one of them.
SDL_AtomicLock(&g_FdTableLock); pthread_mutex_lock(&g_FdTableLock);
int fdIndex = getSdlFdEntryIndex(false); int fdIndex = getSdlFdEntryIndex(false);
if (fdIndex != -1) { if (fdIndex != -1) {
fd = g_SdlDrmMasterFds[fdIndex]; fd = g_SdlDrmMasterFds[fdIndex];
} }
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
if (fd >= 0 && drmDropMaster(fd) == 0) { if (fd >= 0 && drmDropMaster(fd) == 0) {
return fd; return fd;
@@ -126,12 +129,12 @@ int openHook(const char *funcname, const char *pathname, int flags, va_list va)
int allocatedFdIndex; int allocatedFdIndex;
// It is our device. Time to do the magic! // It is our device. Time to do the magic!
SDL_AtomicLock(&g_FdTableLock); pthread_mutex_lock(&g_FdTableLock);
// Get a free index for us to put the new entry // Get a free index for us to put the new entry
freeFdIndex = getSdlFdEntryIndex(true); freeFdIndex = getSdlFdEntryIndex(true);
if (freeFdIndex < 0) { if (freeFdIndex < 0) {
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
SDL_assert(freeFdIndex >= 0); SDL_assert(freeFdIndex >= 0);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"No unused SDL FD table entries!"); "No unused SDL FD table entries!");
@@ -151,7 +154,7 @@ int openHook(const char *funcname, const char *pathname, int flags, va_list va)
else { else {
// Drop master on Qt's FD so we can pick it up for SDL. // Drop master on Qt's FD so we can pick it up for SDL.
if (drmDropMaster(g_QtDrmMasterFd) < 0) { if (drmDropMaster(g_QtDrmMasterFd) < 0) {
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Failed to drop master on Qt DRM FD: %d", "Failed to drop master on Qt DRM FD: %d",
errno); errno);
@@ -182,7 +185,7 @@ int openHook(const char *funcname, const char *pathname, int flags, va_list va)
g_SdlDrmMasterFdCount++; g_SdlDrmMasterFdCount++;
} }
SDL_AtomicUnlock(&g_FdTableLock); pthread_mutex_unlock(&g_FdTableLock);
} }
} }
} }