Remove use of Dangerous* Span APIs and use MemoryMarshal instead (#2228)
This commit is contained in:
parent
9cb1acdea0
commit
3a0a133a02
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web.Utf8;
|
using System.Text.Encodings.Web.Utf8;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
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
|
// .NET 451 doesn't have pointer overloads for Encoding.GetString so we
|
||||||
// copy to an array
|
// copy to an array
|
||||||
fixed (byte* pointer = &path.DangerousGetPinnableReference())
|
fixed (byte* pointer = &MemoryMarshal.GetReference(path))
|
||||||
{
|
{
|
||||||
return Encoding.UTF8.GetString(pointer, path.Length);
|
return Encoding.UTF8.GetString(pointer, path.Length);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
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
|
// Fix and parse the span
|
||||||
fixed (byte* data = &span.DangerousGetPinnableReference())
|
fixed (byte* data = &MemoryMarshal.GetReference(span))
|
||||||
{
|
{
|
||||||
ParseRequestLine(handler, data, span.Length);
|
ParseRequestLine(handler, data, span.Length);
|
||||||
}
|
}
|
||||||
|
|
@ -204,7 +205,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
var span = reader.Span;
|
var span = reader.Span;
|
||||||
var remaining = span.Length - reader.Index;
|
var remaining = span.Length - reader.Index;
|
||||||
|
|
||||||
fixed (byte* pBuffer = &span.DangerousGetPinnableReference())
|
fixed (byte* pBuffer = &MemoryMarshal.GetReference(span))
|
||||||
{
|
{
|
||||||
while (remaining > 0)
|
while (remaining > 0)
|
||||||
{
|
{
|
||||||
|
|
@ -289,7 +290,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
var headerSpan = buffer.Slice(current, lineEnd).ToSpan();
|
var headerSpan = buffer.Slice(current, lineEnd).ToSpan();
|
||||||
length = headerSpan.Length;
|
length = headerSpan.Length;
|
||||||
|
|
||||||
fixed (byte* pHeader = &headerSpan.DangerousGetPinnableReference())
|
fixed (byte* pHeader = &MemoryMarshal.GetReference(headerSpan))
|
||||||
{
|
{
|
||||||
TakeSingleHeader(pHeader, length, handler);
|
TakeSingleHeader(pHeader, length, handler);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
@ -32,7 +33,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
|
|
||||||
public unsafe void Append(Span<byte> name, string value)
|
public unsafe void Append(Span<byte> name, string value)
|
||||||
{
|
{
|
||||||
fixed (byte* namePtr = &name.DangerousGetPinnableReference())
|
fixed (byte* namePtr = &MemoryMarshal.GetReference(name))
|
||||||
{
|
{
|
||||||
Append(namePtr, name.Length, value);
|
Append(namePtr, name.Length, value);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
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
|
// In-place implementation of the algorithm from https://tools.ietf.org/html/rfc3986#section-5.2.4
|
||||||
public static unsafe int RemoveDotSegments(Span<byte> input)
|
public static unsafe int RemoveDotSegments(Span<byte> input)
|
||||||
{
|
{
|
||||||
fixed (byte* start = &input.DangerousGetPinnableReference())
|
fixed (byte* start = &MemoryMarshal.GetReference(input))
|
||||||
{
|
{
|
||||||
var end = start + input.Length;
|
var end = start + input.Length;
|
||||||
return RemoveDotSegments(start, end);
|
return RemoveDotSegments(start, end);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
{
|
{
|
||||||
|
|
@ -49,7 +50,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
if (sourceLength <= destLength)
|
if (sourceLength <= destLength)
|
||||||
{
|
{
|
||||||
fixed (char* input = data)
|
fixed (char* input = data)
|
||||||
fixed (byte* output = &dest.DangerousGetPinnableReference())
|
fixed (byte* output = &MemoryMarshal.GetReference(dest))
|
||||||
{
|
{
|
||||||
EncodeAsciiCharsToBytes(input, output, sourceLength);
|
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
|
// Fast path, try copying to the available memory directly
|
||||||
var simpleWrite = true;
|
var simpleWrite = true;
|
||||||
fixed (byte* output = &span.DangerousGetPinnableReference())
|
fixed (byte* output = &MemoryMarshal.GetReference(span))
|
||||||
{
|
{
|
||||||
var start = output;
|
var start = output;
|
||||||
if (number < 10 && bytesLeftInBlock >= 1)
|
if (number < 10 && bytesLeftInBlock >= 1)
|
||||||
|
|
@ -152,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
fixed (byte* output = &buffer.Span.DangerousGetPinnableReference())
|
fixed (byte* output = &MemoryMarshal.GetReference(buffer.Span))
|
||||||
{
|
{
|
||||||
EncodeAsciiCharsToBytes(inputSlice, output, writable);
|
EncodeAsciiCharsToBytes(inputSlice, output, writable);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
|
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);
|
var asciiString = new string('\0', span.Length);
|
||||||
|
|
||||||
fixed (char* output = asciiString)
|
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
|
// This version if AsciiUtilities returns null if there are any null (0 byte) characters
|
||||||
// in the string
|
// in the string
|
||||||
|
|
@ -136,7 +137,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static unsafe bool GetKnownMethod(this Span<byte> span, out HttpMethod method, out int length)
|
public static unsafe bool GetKnownMethod(this Span<byte> 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);
|
method = GetKnownMethod(data, span.Length, out length);
|
||||||
return method != HttpMethod.Custom;
|
return method != HttpMethod.Custom;
|
||||||
|
|
@ -190,7 +191,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static unsafe bool GetKnownVersion(this Span<byte> span, out HttpVersion knownVersion, out byte length)
|
public static unsafe bool GetKnownVersion(this Span<byte> span, out HttpVersion knownVersion, out byte length)
|
||||||
{
|
{
|
||||||
fixed (byte* data = &span.DangerousGetPinnableReference())
|
fixed (byte* data = &MemoryMarshal.GetReference(span))
|
||||||
{
|
{
|
||||||
knownVersion = GetKnownVersion(data, span.Length);
|
knownVersion = GetKnownVersion(data, span.Length);
|
||||||
if (knownVersion != HttpVersion.Unknown)
|
if (knownVersion != HttpVersion.Unknown)
|
||||||
|
|
@ -249,7 +250,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static unsafe bool GetKnownHttpScheme(this Span<byte> span, out HttpScheme knownScheme)
|
public static unsafe bool GetKnownHttpScheme(this Span<byte> span, out HttpScheme knownScheme)
|
||||||
{
|
{
|
||||||
fixed (byte* data = &span.DangerousGetPinnableReference())
|
fixed (byte* data = &MemoryMarshal.GetReference(span))
|
||||||
{
|
{
|
||||||
return GetKnownHttpScheme(data, span.Length, out knownScheme);
|
return GetKnownHttpScheme(data, span.Length, out knownScheme);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue