Avoid Unsafe.As usage in ValueTask that can break type safety (#17471)
authorStephen Toub <stoub@microsoft.com>
Mon, 9 Apr 2018 23:52:25 +0000 (19:52 -0400)
committerGitHub <noreply@github.com>
Mon, 9 Apr 2018 23:52:25 +0000 (19:52 -0400)
commitf976f748723a874988bd63d1f790152baccf21dc
tree9fc92007c9fb528ea503a564fd959c10e5be0f7a
parentaf7903440ead38c3f5a8481c3343800d4a3e5dfe
Avoid Unsafe.As usage in ValueTask that can break type safety (#17471)

Unsafe.As yields a performance improvement over using a normal cast, and it's fine when ValueTask is used correctly, but if the ValueTask instance were to be stored into a field and multiple threads incorrectly raced to access it, a torn read/write could result in violating type safety due to ObjectIsTask reading the wrong value for the associated object. This commit changes the implementation to only use the single object field to determine which paths to take, rather than factoring in a second field that may not be in sync.
src/mscorlib/shared/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs
src/mscorlib/shared/System/Runtime/CompilerServices/ValueTaskAwaiter.cs
src/mscorlib/shared/System/Threading/Tasks/ValueTask.cs