Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

release/2.0: Fix lifetime handling of ReceiveMessageFromAsync buffer on Windows#22013

Merged
stephentoub merged 3 commits intodotnet:release/2.0.0from
stephentoub:port22012
Jul 10, 2017
Merged

release/2.0: Fix lifetime handling of ReceiveMessageFromAsync buffer on Windows#22013
stephentoub merged 3 commits intodotnet:release/2.0.0from
stephentoub:port22012

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Jul 9, 2017

The test currently only tests a single send/receive pair, which doesn't catch issues when trying to reuse a SocketAsyncEventArgs for multiple operations iteratively.  The test is also duplicated for IPv4 and IPv6.  Fix both issues.
When a ReceiveMessageFromAsync is first used with a SocketAsyncEventArgs instance, it initializes the _wsaMessageBuffer.  Then in order to use the buffer, it pins it, storing both a GCHandle and a pointer to the target object.  But if the SAEA's buffer is ever changed with, for example, a SetBuffer call, a routine is invoked on the SocketAsyncEventArgs that frees all of its pinned data, including these for the _wsaMessageBuffer.  Then the next time ReceiveMessageFromAsync is used, this handle ends up not getting recreated, and we end up dereferencing a null pointer.

The fix is to separate the creation of the buffer from the creation of the pinning handle, lazily initializing each independently.  If the pinning handle is freed due to SetBuffer, the next invocation will reinitialize it.
@dotnet dotnet deleted a comment from dotnet-bot Jul 9, 2017
@stephentoub stephentoub self-assigned this Jul 9, 2017
@geoffkizer
Copy link

LGTM

@stephentoub
Copy link
Member Author

@dotnet-bot test outerloop netcoreapp OSX10.12 Debug
@dotnet-bot test outerloop netcoreapp Windows_NT Debug
@dotnet-bot test OuterLoop netcoreapp PortableLinux Debug x64 please

@stephentoub stephentoub merged commit 5d82df4 into dotnet:release/2.0.0 Jul 10, 2017
@stephentoub stephentoub deleted the port22012 branch July 10, 2017 02:16
@karelz karelz modified the milestone: 2.0.0 Jul 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants