-
Notifications
You must be signed in to change notification settings - Fork 4.9k
[release/2.0.0] Ensure HttpListener request buffer is aligned as required by the host processor #25763
[release/2.0.0] Ensure HttpListener request buffer is aligned as required by the host processor #25763
Conversation
… processor (dotnet#25563) On Windows, the HttpRecieveHttpRequest function requires a buffer with a memory alignment greater than or equal to the required alignment of the HTTP_REQUEST struct. This fix ensures that alignment requirements are respected when allocating buffers in HttpListener RequestContextBase. Since HttpReceiveHttpRequest copies both the HTTP_REQUEST struct and the variable length request body into the buffer, we need to be able to allocate a buffer with variable size and with a set alignment. Since C# does not provide a method for specifying the alignment of byte arrays, I switched the underlying buffer to be unmanaged. This unmanaged buffer is allocated using Marshal.AllocHGlobal, which allocates memory at the maximum alignment required by the host processor.
// Zero out the contents of the buffer. | ||
for(int i = 0; i < size; ++i) | ||
{ | ||
Marshal.WriteByte(_backingBuffer + i, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marshal.WriteByte
is expensive API to call for something like this.
Instead, cast the _backingBuffer
to (byte*)
, and cache it in a local:
byte *buffer = (byte*)_backingBuffer;
for (int i = 0; i < size; i++) buffer[i] = 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. I'm glad to know there's a better way to do that.
Edit: This is done in the latest iteration.
@dotnet-bot test Outerloop Windows x64 Debug Build please |
@davidsh , We're open for checkins for the next release but I see the NO MERGE label on this one. Is there something left to do before we would want to merge? |
It was marked as NO MERGE when the branch wasn't opened for checkin. It was marked that way to prevent accidental merges. But it is ready to go now. So, I will merge it now after CI finishes (again). |
On Windows, the HttpRecieveHttpRequest function requires a buffer with a memory alignment greater than or equal to the required alignment of the HTTP_REQUEST struct.
This fix ensures that alignment requirements are respected when allocating buffers in HttpListener RequestContextBase. Since HttpReceiveHttpRequest copies both the HTTP_REQUEST struct and the variable length request body into the buffer, we need to be able to allocate a buffer with variable size and with a set alignment. Since C# does not provide a method for specifying the alignment of byte arrays, I switched the underlying buffer to be unmanaged. This unmanaged buffer is allocated using Marshal.AllocHGlobal, which allocates memory at the maximum alignment required by the host processor. This fix differs from the 2.1.0 fix in how it zeros out allocated memory, as 2.0.0 disables support for Span.
Fixes #25289