@@ -146,8 +146,16 @@ PFN_FPUTS real_fputs = nullptr;
146146PFN_FPUTC real_fputc = nullptr ;
147147#endif
148148
149+ // It would be nicer to use a lock_guard but due to the interposer, it isn't possible because these functions get called
150+ // before the shim_lock variable is finished initializing.
151+ #define LOCK_IF_NOT_IN_SETUP \
152+ std::unique_lock<std::recursive_mutex> lock{}; \
153+ if (platform_shim.is_finished_setup) { \
154+ lock = std::unique_lock<std::recursive_mutex>(shim_lock); \
155+ }
156+
149157FRAMEWORK_EXPORT DIR * OPENDIR_FUNC_NAME (const char * path_name) {
150- std::lock_guard lg (shim_lock);
158+ LOCK_IF_NOT_IN_SETUP
151159#if !defined(__APPLE__)
152160 if (!real_opendir) real_opendir = (PFN_OPENDIR )dlsym (RTLD_NEXT , " opendir" );
153161#endif
@@ -166,7 +174,7 @@ FRAMEWORK_EXPORT DIR* OPENDIR_FUNC_NAME(const char* path_name) {
166174}
167175
168176FRAMEWORK_EXPORT struct dirent * READDIR_FUNC_NAME (DIR * dir_stream) {
169- std::lock_guard lg (shim_lock);
177+ LOCK_IF_NOT_IN_SETUP
170178#if !defined(__APPLE__)
171179 if (!real_readdir) {
172180 if (sizeof (void *) == 8 ) {
@@ -227,7 +235,7 @@ FRAMEWORK_EXPORT struct dirent* READDIR_FUNC_NAME(DIR* dir_stream) {
227235}
228236
229237FRAMEWORK_EXPORT int CLOSEDIR_FUNC_NAME (DIR * dir_stream) {
230- std::lock_guard lg (shim_lock);
238+ LOCK_IF_NOT_IN_SETUP
231239#if !defined(__APPLE__)
232240 if (!real_closedir) real_closedir = (PFN_CLOSEDIR )dlsym (RTLD_NEXT , " closedir" );
233241#endif
@@ -245,7 +253,7 @@ FRAMEWORK_EXPORT int CLOSEDIR_FUNC_NAME(DIR* dir_stream) {
245253}
246254
247255FRAMEWORK_EXPORT int ACCESS_FUNC_NAME (const char * in_pathname, int mode) {
248- std::lock_guard lg (shim_lock);
256+ LOCK_IF_NOT_IN_SETUP
249257#if !defined(__APPLE__)
250258 if (!real_access) real_access = (PFN_ACCESS )dlsym (RTLD_NEXT , " access" );
251259#endif
@@ -268,7 +276,7 @@ FRAMEWORK_EXPORT int ACCESS_FUNC_NAME(const char* in_pathname, int mode) {
268276}
269277
270278FRAMEWORK_EXPORT FILE * FOPEN_FUNC_NAME (const char * in_filename, const char * mode) {
271- std::lock_guard lg (shim_lock);
279+ LOCK_IF_NOT_IN_SETUP
272280#if !defined(__APPLE__)
273281 if (!real_fopen) real_fopen = (PFN_FOPEN )dlsym (RTLD_NEXT , " fopen" );
274282#endif
@@ -307,7 +315,7 @@ FRAMEWORK_EXPORT FILE* FOPEN_FUNC_NAME(const char* in_filename, const char* mode
307315}
308316
309317FRAMEWORK_EXPORT void * DLOPEN_FUNC_NAME (const char * in_filename, int flags) {
310- std::lock_guard lg (shim_lock);
318+ LOCK_IF_NOT_IN_SETUP
311319#if !defined(__APPLE__)
312320 if (!real_dlopen) real_dlopen = (PFN_DLOPEN )dlsym (RTLD_NEXT , " dlopen" );
313321#endif
@@ -328,7 +336,7 @@ FRAMEWORK_EXPORT void* DLOPEN_FUNC_NAME(const char* in_filename, int flags) {
328336}
329337
330338FRAMEWORK_EXPORT uid_t GETEUID_FUNC_NAME (void ) {
331- std::lock_guard lg (shim_lock);
339+ LOCK_IF_NOT_IN_SETUP
332340#if !defined(__APPLE__)
333341 if (!real_geteuid) real_geteuid = (PFN_GETEUID )dlsym (RTLD_NEXT , " geteuid" );
334342#endif
@@ -341,7 +349,7 @@ FRAMEWORK_EXPORT uid_t GETEUID_FUNC_NAME(void) {
341349}
342350
343351FRAMEWORK_EXPORT gid_t GETEGID_FUNC_NAME (void ) {
344- std::lock_guard lg (shim_lock);
352+ LOCK_IF_NOT_IN_SETUP
345353#if !defined(__APPLE__)
346354 if (!real_getegid) real_getegid = (PFN_GETEGID )dlsym (RTLD_NEXT , " getegid" );
347355#endif
@@ -356,7 +364,7 @@ FRAMEWORK_EXPORT gid_t GETEGID_FUNC_NAME(void) {
356364#if !defined(TARGET_OS_IPHONE)
357365#if defined(HAVE_SECURE_GETENV)
358366FRAMEWORK_EXPORT char * SECURE_GETENV_FUNC_NAME (const char * name) {
359- std::lock_guard lg (shim_lock);
367+ LOCK_IF_NOT_IN_SETUP
360368#if !defined(__APPLE__)
361369 if (!real_secure_getenv) real_secure_getenv = (PFN_SEC_GETENV )dlsym (RTLD_NEXT , " secure_getenv" );
362370#endif
@@ -369,7 +377,7 @@ FRAMEWORK_EXPORT char* SECURE_GETENV_FUNC_NAME(const char* name) {
369377#endif
370378#if defined(HAVE___SECURE_GETENV)
371379FRAMEWORK_EXPORT char * __SECURE_GETENV_FUNC_NAME (const char * name) {
372- std::lock_guard lg (shim_lock);
380+ LOCK_IF_NOT_IN_SETUP
373381#if !defined(__APPLE__)
374382 if (!real__secure_getenv) real__secure_getenv = (PFN_SEC_GETENV )dlsym (RTLD_NEXT , " __secure_getenv" );
375383#endif
@@ -384,42 +392,42 @@ FRAMEWORK_EXPORT char* __SECURE_GETENV_FUNC_NAME(const char* name) {
384392#endif
385393
386394FRAMEWORK_EXPORT int FPUTS_FUNC_NAME (const char * str, FILE * stream) {
395+ LOCK_IF_NOT_IN_SETUP
387396#if !defined(__APPLE__)
388397 if (!real_fputs) real_fputs = (PFN_FPUTS )dlsym (RTLD_NEXT , " fputs" );
389398#endif
390399 if (stream == stderr) {
391- std::lock_guard lg (shim_lock);
392400 platform_shim.fputs_stderr_log += str;
393401 }
394402 return real_fputs (str, stream);
395403}
396404
397405FRAMEWORK_EXPORT int FPUTC_FUNC_NAME (int ch, FILE * stream) {
406+ LOCK_IF_NOT_IN_SETUP
398407#if !defined(__APPLE__)
399408 if (!real_fputc) real_fputc = (PFN_FPUTC )dlsym (RTLD_NEXT , " fputc" );
400409#endif
401410 if (stream == stderr) {
402- std::lock_guard lg (shim_lock);
403411 platform_shim.fputs_stderr_log += ch;
404412 }
405413 return real_fputc (ch, stream);
406414}
407415
408416#if defined(__APPLE__)
409417FRAMEWORK_EXPORT CFBundleRef my_CFBundleGetMainBundle () {
410- std::lock_guard lg (shim_lock);
418+ LOCK_IF_NOT_IN_SETUP
411419 static CFBundleRef global_bundle{};
412420 return reinterpret_cast <CFBundleRef>(&global_bundle);
413421}
414422FRAMEWORK_EXPORT CFURLRef my_CFBundleCopyResourcesDirectoryURL ([[maybe_unused]] CFBundleRef bundle) {
415- std::lock_guard lg (shim_lock);
423+ LOCK_IF_NOT_IN_SETUP
416424 static CFURLRef global_url{};
417425 return reinterpret_cast <CFURLRef>(&global_url);
418426}
419427FRAMEWORK_EXPORT Boolean my_CFURLGetFileSystemRepresentation ([[maybe_unused]] CFURLRef url,
420428 [[maybe_unused]] Boolean resolveAgainstBase, UInt8* buffer,
421429 CFIndex maxBufLen) {
422- std::lock_guard lg (shim_lock);
430+ LOCK_IF_NOT_IN_SETUP
423431 if (!platform_shim.bundle_contents .empty ()) {
424432 CFIndex copy_len = (CFIndex)platform_shim.bundle_contents .size ();
425433 if (copy_len > maxBufLen) {
0 commit comments