Files
tubestation/mozglue/misc/StackWalk_windows.h
Yannis Juglaret addbb1c6db Bug 1839299 - Use a finer-grained strategy to protect RtlLookupFunctionEntry against deadlocks. r=win-reviewers,rkraesig
On 64-bit Windows (x86_64, aarch64), stack walking relies on
RtlLookupFunctionEntry to navigate from one frame to the next. This
function acquires up to two ntdll internal locks when it is called.

The profiler and the background hang monitor both need to walk the
stacks of suspended threads. This can lead to deadlock situations,
which so far we have avoided with stack walk suppressions. We guard some
critical paths to mark them as suppressing stack walk, and we forbid
stack walking when any thread is currently on such path.

While stack walk suppression has helped remove most deadlock situations,
some can remain because it is hard to detect and manually annotate all
the paths that could lead to a deadlock situation. Another drawback is
that stack walk suppression disables stack walking for much larger
portions of code than required. For example, we disable stack walking
for LdrLoadDll, so we cannot collect stacks while we are loading a DLL.
Yet, the lock that could lead to a deadlock situation is only held
during a very small portion of the whole time spent in LdrLoadDll.

This patch addresses these two issues by implementing a finer-grained
strategy to avoid deadlock situations. We acquire the pointers to the
internel ntdll locks through a single-stepped execution of
RtlLookupFunctionEntry. This allows us to try to acquire the locks
non-blockingly so that we can guarantee safe stack walking with no
deadlock.

If we fail to collect pointers to the locks, we fall back to using stack
walk suppressions like before. This way we get the best of both worlds:
if we are confident that the situation is under control, we will use the
new strategy and get better profiler accuracy and no deadlock; in case
of doubt, we can still use the profiler thanks to stack walk
suppressions.

Differential Revision: https://phabricator.services.mozilla.com/D223498
2024-10-17 13:47:04 +00:00

53 lines
2.0 KiB
C

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_StackWalk_windows_h
#define mozilla_StackWalk_windows_h
#include "mozilla/Array.h"
#include "mozilla/Types.h"
#if defined(_M_AMD64) || defined(_M_ARM64)
/**
* This function enables strategy (1) for avoiding deadlocks between the stack
* walking thread and the suspended thread. In aStackWalkLocks the caller must
* provide pointers to the two ntdll-internal SRW locks acquired by
* RtlLookupFunctionEntry. These locks are LdrpInvertedFunctionTableSRWLock and
* RtlpDynamicFunctionTableLock -- we don't need to know which one is which.
* Until InitializeStackWalkLocks function is called, strategy (2) is used.
*
* See comment in StackWalk.cpp
*/
MFBT_API
void InitializeStackWalkLocks(const mozilla::Array<void*, 2>& aStackWalkLocks);
/**
* As part of strategy (2) for avoiding deadlocks between the stack walking
* thread and the suspended thread, we mark stack walk suppression paths by
* putting them under the scope of a AutoSuppressStackWalking object. Any code
* path that may do an exclusive acquire of LdrpInvertedFunctionTableSRWLock or
* RtlpDynamicFunctionTableLock should be marked this way, to ensure that
* strategy (2) can properly mitigate all deadlock scenarios.
*
* See comment in StackWalk.cpp
*/
struct MOZ_RAII AutoSuppressStackWalking {
MFBT_API AutoSuppressStackWalking();
MFBT_API ~AutoSuppressStackWalking();
};
# if defined(IMPL_MFBT)
void SuppressStackWalking();
void DesuppressStackWalking();
# endif // defined(IMPL_MFBT)
MFBT_API void RegisterJitCodeRegion(uint8_t* aStart, size_t size);
MFBT_API void UnregisterJitCodeRegion(uint8_t* aStart, size_t size);
#endif // _M_AMD64 || _M_ARM64
#endif // mozilla_StackWalk_windows_h