Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure MetadataReader fast path works with every FileStream on every OS #50367

Merged
merged 4 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefaultLanguage>en-US</DefaultLanguage>
Expand All @@ -15,9 +15,11 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
Link="Common\Interop\Windows\Interop.Libraries.cs"
Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs" />
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
Expand Down Expand Up @@ -102,8 +104,9 @@
<Compile Include="System\Reflection\Internal\Utilities\EmptyArray.cs" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.cs" Condition="$(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('net4'))" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard2.0.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net461'" />
<Compile Include="System\Reflection\Internal\Utilities\Hash.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableByteArrayInterop.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableMemoryStream.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ internal static unsafe NativeHeapMemoryBlock ReadMemoryBlockNoLock(Stream stream
{
stream.Seek(start, SeekOrigin.Begin);

if (!isFileStream || !FileStreamReadLightUp.TryReadFile(stream, block.Pointer, start, size))
int bytesRead = 0;

if (!isFileStream || (bytesRead = FileStreamReadLightUp.ReadFile(stream, block.Pointer, size)) != size)
{
stream.CopyTo(block.Pointer, size);
stream.CopyTo(block.Pointer + bytesRead, size - bytesRead);
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
}

fault = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;

namespace System.Reflection.Internal
{
internal static class FileStreamReadLightUp
{
internal static bool IsFileStream(Stream stream) => stream is FileStream;

internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
=> stream.Read(new Span<byte>(buffer, size));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,29 @@ internal static SafeHandle GetSafeFileHandle(Stream stream)
return handle;
}

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
{
if (readFileNotAvailable)
{
return false;
return 0;
}

SafeHandle handle = GetSafeFileHandle(stream);
if (handle == null)
{
return false;
return 0;
}

int result;
int bytesRead;

try
{
result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);
return result == 0 ? 0 : bytesRead;
}
catch
{
readFileNotAvailable = true;
return false;
return 0;
}

if (result == 0 || bytesRead != size)
{
// We used to throw here, but this is where we land if the FileStream was
// opened with useAsync: true, which is currently the default on .NET Core.
// https://github.com/dotnet/corefx/pull/987 filed to look in to how best to
// handle this, but in the meantime, we'll fall back to the slower code path
// just as in the case where the native API is unavailable in the current platform.
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace System.Reflection.Internal
{
internal static class FileStreamReadLightUp
{
private static bool IsReadFileAvailable =>
#if NETCOREAPP
OperatingSystem.IsWindows();
#else
Path.DirectorySeparatorChar == '\\';
#endif
private static bool IsReadFileAvailable => Path.DirectorySeparatorChar == '\\';

internal static bool IsFileStream(Stream stream) => stream is FileStream;

Expand Down Expand Up @@ -42,32 +36,21 @@ internal static class FileStreamReadLightUp
return handle;
}

internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
{
if (!IsReadFileAvailable)
{
return false;
return 0;
}

SafeHandle? handle = GetSafeFileHandle(stream);
if (handle == null)
{
return false;
return 0;
}

int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);

if (result == 0 || bytesRead != size)
{
// We used to throw here, but this is where we land if the FileStream was
// opened with useAsync: true, which is currently the default on .NET Core.
// https://github.com/dotnet/corefx/pull/987 filed to look in to how best to
// handle this, but in the meantime, we'll fall back to the slower code path
// just as in the case where the native API is unavailable in the current platform.
return false;
}

return true;
return result == 0 ? 0 : bytesRead;
}
}
}