dotnet/corefx — pull request #200 by AArnott (2014-12-06 04:12:44+00:00)
Title: Fix thread-safety vulnerability in ImmutableArray<T>

Any member that references 'this' more than once is vulnerable to in-place replacements of the value of the struct from another thread. That pretty much defeats the thread-safety guarantee of ImmutableArray. So the fix is to make sure we dereference this (to get the array) exactly once per public entrypoint so that the array instance we access is the same every time.

An example of the problem is we can capture the length of the array, and then copy from the array. But in between those two steps another thread updated the field that contained this struct, so while our method is still running, the instance of the array actually *changed*, so the length of the array changed with it, and we throw an exception because we tried to read beyond the bounds of the array.

It turns out this demonstrates a very compelling reason to prefix *all* instance member access with "this." (not only fields, but properties and methods too!) because it made auditing and fixing this class much easier since I just had to look for any member that referenced 'this' more than once and could use search and replace to change them all to 'self'.

Fixes #191