diff --git a/src/Kestrel.Core/Internal/Http/Http1Connection.cs b/src/Kestrel.Core/Internal/Http/Http1Connection.cs index 29b257208b..5168bf7d61 100644 --- a/src/Kestrel.Core/Internal/Http/Http1Connection.cs +++ b/src/Kestrel.Core/Internal/Http/Http1Connection.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; using System.IO.Pipelines; +using System.Runtime.InteropServices; using System.Text; using System.Text.Encodings.Web.Utf8; using Microsoft.AspNetCore.Http.Features; @@ -341,7 +342,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http { // .NET 451 doesn't have pointer overloads for Encoding.GetString so we // copy to an array - fixed (byte* pointer = &path.DangerousGetPinnableReference()) + fixed (byte* pointer = &MemoryMarshal.GetReference(path)) { return Encoding.UTF8.GetString(pointer, path.Length); } diff --git a/src/Kestrel.Core/Internal/Http/HttpParser.cs b/src/Kestrel.Core/Internal/Http/HttpParser.cs index cb2163a493..ab66b2337f 100644 --- a/src/Kestrel.Core/Internal/Http/HttpParser.cs +++ b/src/Kestrel.Core/Internal/Http/HttpParser.cs @@ -4,6 +4,7 @@ using System; using System.IO.Pipelines; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http @@ -60,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http } // Fix and parse the span - fixed (byte* data = &span.DangerousGetPinnableReference()) + fixed (byte* data = &MemoryMarshal.GetReference(span)) { ParseRequestLine(handler, data, span.Length); } @@ -204,7 +205,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http var span = reader.Span; var remaining = span.Length - reader.Index; - fixed (byte* pBuffer = &span.DangerousGetPinnableReference()) + fixed (byte* pBuffer = &MemoryMarshal.GetReference(span)) { while (remaining > 0) { @@ -289,7 +290,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http var headerSpan = buffer.Slice(current, lineEnd).ToSpan(); length = headerSpan.Length; - fixed (byte* pHeader = &headerSpan.DangerousGetPinnableReference()) + fixed (byte* pHeader = &MemoryMarshal.GetReference(headerSpan)) { TakeSingleHeader(pHeader, length, handler); } diff --git a/src/Kestrel.Core/Internal/Http/HttpRequestHeaders.cs b/src/Kestrel.Core/Internal/Http/HttpRequestHeaders.cs index fd0008e52b..f512df8fcf 100644 --- a/src/Kestrel.Core/Internal/Http/HttpRequestHeaders.cs +++ b/src/Kestrel.Core/Internal/Http/HttpRequestHeaders.cs @@ -5,6 +5,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; @@ -32,7 +33,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http public unsafe void Append(Span name, string value) { - fixed (byte* namePtr = &name.DangerousGetPinnableReference()) + fixed (byte* namePtr = &MemoryMarshal.GetReference(name)) { Append(namePtr, name.Length, value); } diff --git a/src/Kestrel.Core/Internal/Http/PathNormalizer.cs b/src/Kestrel.Core/Internal/Http/PathNormalizer.cs index de076e1849..68cdddf7ce 100644 --- a/src/Kestrel.Core/Internal/Http/PathNormalizer.cs +++ b/src/Kestrel.Core/Internal/Http/PathNormalizer.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Runtime.InteropServices; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http { @@ -14,7 +15,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http // In-place implementation of the algorithm from https://tools.ietf.org/html/rfc3986#section-5.2.4 public static unsafe int RemoveDotSegments(Span input) { - fixed (byte* start = &input.DangerousGetPinnableReference()) + fixed (byte* start = &MemoryMarshal.GetReference(input)) { var end = start + input.Length; return RemoveDotSegments(start, end); diff --git a/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs b/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs index e12b588bb4..6989d80aa3 100644 --- a/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs +++ b/src/Kestrel.Core/Internal/Http/PipelineExtensions.cs @@ -4,6 +4,7 @@ using System; using System.IO.Pipelines; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http { @@ -49,7 +50,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http if (sourceLength <= destLength) { fixed (char* input = data) - fixed (byte* output = &dest.DangerousGetPinnableReference()) + fixed (byte* output = &MemoryMarshal.GetReference(dest)) { EncodeAsciiCharsToBytes(input, output, sourceLength); } @@ -72,7 +73,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http // Fast path, try copying to the available memory directly var simpleWrite = true; - fixed (byte* output = &span.DangerousGetPinnableReference()) + fixed (byte* output = &MemoryMarshal.GetReference(span)) { var start = output; if (number < 10 && bytesLeftInBlock >= 1) @@ -152,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http continue; } - fixed (byte* output = &buffer.Span.DangerousGetPinnableReference()) + fixed (byte* output = &MemoryMarshal.GetReference(buffer.Span)) { EncodeAsciiCharsToBytes(inputSlice, output, writable); } diff --git a/src/Kestrel.Core/Internal/Infrastructure/HttpUtilities.cs b/src/Kestrel.Core/Internal/Infrastructure/HttpUtilities.cs index 7b08a068fd..28f73a9031 100644 --- a/src/Kestrel.Core/Internal/Infrastructure/HttpUtilities.cs +++ b/src/Kestrel.Core/Internal/Infrastructure/HttpUtilities.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; @@ -91,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure var asciiString = new string('\0', span.Length); fixed (char* output = asciiString) - fixed (byte* buffer = &span.DangerousGetPinnableReference()) + fixed (byte* buffer = &MemoryMarshal.GetReference(span)) { // This version if AsciiUtilities returns null if there are any null (0 byte) characters // in the string @@ -136,7 +137,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe bool GetKnownMethod(this Span span, out HttpMethod method, out int length) { - fixed (byte* data = &span.DangerousGetPinnableReference()) + fixed (byte* data = &MemoryMarshal.GetReference(span)) { method = GetKnownMethod(data, span.Length, out length); return method != HttpMethod.Custom; @@ -190,7 +191,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe bool GetKnownVersion(this Span span, out HttpVersion knownVersion, out byte length) { - fixed (byte* data = &span.DangerousGetPinnableReference()) + fixed (byte* data = &MemoryMarshal.GetReference(span)) { knownVersion = GetKnownVersion(data, span.Length); if (knownVersion != HttpVersion.Unknown) @@ -249,7 +250,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe bool GetKnownHttpScheme(this Span span, out HttpScheme knownScheme) { - fixed (byte* data = &span.DangerousGetPinnableReference()) + fixed (byte* data = &MemoryMarshal.GetReference(span)) { return GetKnownHttpScheme(data, span.Length, out knownScheme); }