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