Fix watson buckets setting race condition (#46636)
authorJan Vorlicek <janvorli@microsoft.com>
Thu, 7 Jan 2021 16:44:04 +0000 (17:44 +0100)
committerGitHub <noreply@github.com>
Thu, 7 Jan 2021 16:44:04 +0000 (17:44 +0100)
commitf33dd09a8d4e908efd638bd3b1a20c78affb767e
treed51d8e7f83cb3315aa184bbdcef2d82fd9b77db5
parent9538bee0fc1fb24993217071e954d9597385485f
Fix watson buckets setting race condition (#46636)

* Fix watson buckets setting race condition

The code that updates watson buckets in an exception from its inner
exception has a race that can cause runtime crashes when many threads
are rethrowing the same exception using ExceptionDispatchInfo.Throw.
At several places, we check if the inner exception contains watson
buckets and if that is true, we copy them to the outer exception.
However, the ExceptionDispatchInfo.Throw on another thread can reset
the buckets to the captured value, which might be null. So the
copying then crashes with access violation.

This change fixes the issue by reading the buckets reference once and
then performing the checks / copying using that extracted value.

It also adds a regression test that asserts quickly in debug / checked
builds.

* Reflect feedback

* Make the VALIDATEOBJECTREF race-resilient
* Add license header to the test
* Fix indentation

* Fix wrong VALIDATEOBJECTREF change
src/coreclr/vm/excep.cpp
src/coreclr/vm/exstatecommon.h
src/coreclr/vm/vars.hpp
src/tests/Regressions/coreclr/GitHub_45929/test45929.cs [new file with mode: 0644]
src/tests/Regressions/coreclr/GitHub_45929/test45929.csproj [new file with mode: 0644]