Skip to content

Commit

Permalink
Fix memory load ordering bug (dotnet#66952)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Robinson <arobins@microsoft.com>
  • Loading branch information
2 people authored and radekdoulik committed Mar 30, 2022
1 parent 0262b86 commit fc1da91
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/native/libs/System.Native/pal_dynamicload.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
return (void*)RTLD_DEFAULT;
}
#else
static void* g_defaultSearchOrderPseudoHandle = NULL;
static void* volatile g_defaultSearchOrderPseudoHandle = NULL;
void* SystemNative_GetDefaultSearchOrderPseudoHandle(void)
{
if (g_defaultSearchOrderPseudoHandle == NULL)
// Read the value once from the volatile static to avoid reading from memory twice.
void* defaultSearchOrderPseudoHandle = (void*)g_defaultSearchOrderPseudoHandle;
if (defaultSearchOrderPseudoHandle == NULL)
{
g_defaultSearchOrderPseudoHandle = dlopen(NULL, RTLD_LAZY);
// Assign back to the static as well as the local here.
// We don't need to check for a race between two threads as the value returned by
// dlopen here will always be the same in a given environment.
g_defaultSearchOrderPseudoHandle = defaultSearchOrderPseudoHandle = dlopen(NULL, RTLD_LAZY);
}
return g_defaultSearchOrderPseudoHandle;
return defaultSearchOrderPseudoHandle;
}
#endif

0 comments on commit fc1da91

Please sign in to comment.