Skip to content

[NativeAOT] Fix Lock's usage of NativeRuntimeEventSource.Log to account for recursive accesses during its own class construction #94873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static bool TryInitializeStatics()
s_minSpinCount = DetermineMinSpinCount();

// Also initialize some types that are used later to prevent potential class construction cycles
NativeRuntimeEventSource.Log.IsEnabled();
_ = NativeRuntimeEventSource.Log;
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId)

Wait:
bool areContentionEventsEnabled =
NativeRuntimeEventSource.Log.IsEnabled(
NativeRuntimeEventSource.Log?.IsEnabled(
EventLevel.Informational,
NativeRuntimeEventSource.Keywords.ContentionKeyword);
NativeRuntimeEventSource.Keywords.ContentionKeyword) ?? false;
AutoResetEvent waitEvent = _waitEvent ?? CreateWaitEvent(areContentionEventsEnabled);
if (State.TryLockBeforeWait(this))
{
Expand All @@ -463,7 +463,7 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId)
long waitStartTimeTicks = 0;
if (areContentionEventsEnabled)
{
NativeRuntimeEventSource.Log.ContentionStart(this);
NativeRuntimeEventSource.Log!.ContentionStart(this);
waitStartTimeTicks = Stopwatch.GetTimestamp();
}

Expand Down Expand Up @@ -535,7 +535,7 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId)
{
double waitDurationNs =
(Stopwatch.GetTimestamp() - waitStartTimeTicks) * 1_000_000_000.0 / Stopwatch.Frequency;
NativeRuntimeEventSource.Log.ContentionStop(waitDurationNs);
NativeRuntimeEventSource.Log!.ContentionStop(waitDurationNs);
}

return currentThreadId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ internal bool WaitOneNoCheck(
#if !CORECLR // CoreCLR sends the wait events from the native side
bool sendWaitEvents =
millisecondsTimeout != 0 &&
#if NATIVEAOT
// A null check is necessary in NativeAOT due to the possibility of reentrance during class
// construction, as this path can be reached through Lock. See
// https://github.com/dotnet/runtime/issues/94728 for a call stack.
NativeRuntimeEventSource.Log != null &&
#endif
NativeRuntimeEventSource.Log.IsEnabled(
EventLevel.Verbose,
NativeRuntimeEventSource.Keywords.WaitHandleKeyword);
Expand Down