From 6624d0c80deb0b29ffda50452ae2dcd6dbefb910 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Tue, 16 May 2017 08:20:39 -0700 Subject: [PATCH] Change CoreFX to use SetThreadErrorMode (dotnet/corefx#19801) * Change CoreFX to use SetThreadErrorMode See dotnet/corefx#19738. SetErrorMode isn't thread safe- we use SetThreadErrorMode in NetFX. * Add P/Invoke exceptions Commit migrated from https://github.com/dotnet/corefx/commit/ac3d3ba3b4097a5626055447b39535e4ee67d9cb --- ...p.cs => Interop.SetThreadErrorMode.Uap.cs} | 5 +-- ...rMode.cs => Interop.SetThreadErrorMode.cs} | 4 +-- .../PinvokeAnalyzerExceptionList.analyzerdata | 2 +- .../src/System.IO.FileSystem.DriveInfo.csproj | 4 +-- .../src/System/IO/DriveInfo.Windows.cs | 35 ++++++++++++------- .../PinvokeAnalyzerExceptionList.analyzerdata | 2 +- .../src/System.IO.FileSystem.csproj | 8 ++--- .../src/System/IO/Win32FileSystem.cs | 12 ++++--- .../System/IO/Win32FileSystemEnumerable.cs | 9 +++-- 9 files changed, 51 insertions(+), 30 deletions(-) rename src/libraries/Common/src/Interop/Windows/kernel32/{Interop.SetErrorMode.Uap.cs => Interop.SetThreadErrorMode.Uap.cs} (74%) rename src/libraries/Common/src/Interop/Windows/kernel32/{Interop.SetErrorMode.cs => Interop.SetThreadErrorMode.cs} (64%) diff --git a/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.Uap.cs b/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.Uap.cs similarity index 74% rename from src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.Uap.cs rename to src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.Uap.cs index d76d2b710b5..2b772075b35 100644 --- a/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.Uap.cs +++ b/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.Uap.cs @@ -8,10 +8,11 @@ internal partial class Interop { internal partial class Kernel32 { - internal static uint SetErrorMode(uint uMode) + internal static bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode) { // Prompting behavior no longer occurs in all platforms supported - return 0; + lpOldMode = 0; + return true; } } } diff --git a/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.cs b/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.cs similarity index 64% rename from src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.cs rename to src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.cs index caa2ce5bfae..ecf0c044dc0 100644 --- a/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetErrorMode.cs +++ b/src/libraries/Common/src/Interop/Windows/kernel32/Interop.SetThreadErrorMode.cs @@ -8,7 +8,7 @@ internal partial class Interop { internal partial class Kernel32 { - [DllImport(Libraries.Kernel32, SetLastError = false, EntryPoint = "SetErrorMode", ExactSpelling = true)] - internal static extern uint SetErrorMode(uint newMode); + [DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)] + internal static extern bool SetThreadErrorMode(uint dwNewMode, out uint lpOldMode); } } diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata b/src/libraries/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata index 885626aff71..4a86b6a2a78 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/PinvokeAnalyzerExceptionList.analyzerdata @@ -1,5 +1,5 @@ kernel32.dll!GetDriveTypeW kernel32.dll!GetLogicalDrives kernel32.dll!GetVolumeInformationW -kernel32.dll!SetErrorMode +kernel32.dll!SetThreadErrorMode kernel32.dll!SetVolumeLabelW diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj index d9bfdecee0b..975c560b38f 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj @@ -49,8 +49,8 @@ Common\Interop\Windows\Interop.SetVolumeLabel.cs - - Common\Interop\Windows\Interop.SetErrorMode.cs + + Common\Interop\Windows\Interop.SetThreadErrorMode.cs Common\Interop\Windows\Interop.SecurityOptions.cs diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs index 03d5e1933b5..f329edaf528 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs @@ -67,7 +67,8 @@ namespace System.IO StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen); int serialNumber, maxFileNameLen, fileSystemFlags; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen); @@ -78,7 +79,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return fileSystemName.ToString(); } @@ -90,7 +92,8 @@ namespace System.IO get { long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -99,7 +102,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return userBytes; } @@ -111,7 +115,8 @@ namespace System.IO get { long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -120,7 +125,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return freeBytes; } @@ -134,7 +140,8 @@ namespace System.IO // Don't cache this, to handle variable sized floppy drives // or other various removable media drives. long userBytes, totalBytes, freeBytes; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetDiskFreeSpaceEx(Name, out userBytes, out totalBytes, out freeBytes); @@ -143,7 +150,7 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return totalBytes; } @@ -174,7 +181,8 @@ namespace System.IO StringBuilder fileSystemName = new StringBuilder(fileSystemNameLen); int serialNumber, maxFileNameLen, fileSystemFlags; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.GetVolumeInformation(Name, volumeName, volNameLen, out serialNumber, out maxFileNameLen, out fileSystemFlags, fileSystemName, fileSystemNameLen); @@ -190,14 +198,16 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } return volumeName.ToString(); } [System.Security.SecuritySafeCritical] // auto-generated set { - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool r = Interop.Kernel32.SetVolumeLabel(Name, value); @@ -212,7 +222,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } } } diff --git a/src/libraries/System.IO.FileSystem/src/PinvokeAnalyzerExceptionList.analyzerdata b/src/libraries/System.IO.FileSystem/src/PinvokeAnalyzerExceptionList.analyzerdata index ca3eb2e1d63..4b690b75047 100644 --- a/src/libraries/System.IO.FileSystem/src/PinvokeAnalyzerExceptionList.analyzerdata +++ b/src/libraries/System.IO.FileSystem/src/PinvokeAnalyzerExceptionList.analyzerdata @@ -2,5 +2,5 @@ kernel32.dll!CopyFileExW kernel32.dll!CreateFileW kernel32.dll!DeleteVolumeMountPointW kernel32.dll!GetLogicalDrives -kernel32.dll!SetErrorMode +kernel32.dll!SetThreadErrorMode diff --git a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj index 45930d49e72..852c0fc29ee 100644 --- a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj +++ b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj @@ -214,8 +214,8 @@ Common\Interop\Windows\Interop.DeleteVolumeMountPoint.cs - - Common\Interop\Windows\Interop.SetErrorMode.cs + + Common\Interop\Windows\Interop.SetThreadErrorMode.cs @@ -229,8 +229,8 @@ Common\Interop\Windows\Interop.DeleteVolumeMountPoint.Uap.cs - - Common\Interop\Windows\Interop.SetErrorMode.Uap.cs + + Common\Interop\Windows\Interop.SetThreadErrorMode.Uap.cs diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs b/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs index c03b28a1807..11c27349d87 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystem.cs @@ -238,7 +238,8 @@ namespace System.IO // there is no disk in drive A:, please insert one. We don't want that. // SetErrorMode will let us disable this, but we should set the error // mode back, since this may have wide-ranging effects. - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool success = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { bool error = false; @@ -283,7 +284,8 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (success) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } // Copy the information to data @@ -296,14 +298,16 @@ namespace System.IO // SetErrorMode will let us disable this, but we should set the error // mode back, since this may have wide-ranging effects. bool success = false; - uint oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + uint oldMode; + bool errorModeSuccess = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out oldMode); try { success = Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data); } finally { - Interop.Kernel32.SetErrorMode(oldMode); + if (errorModeSuccess) + Interop.Kernel32.SetThreadErrorMode(oldMode, out oldMode); } if (!success) diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystemEnumerable.cs b/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystemEnumerable.cs index cf63de91c8d..5d305718f8d 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystemEnumerable.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/Win32FileSystemEnumerable.cs @@ -112,6 +112,7 @@ namespace System.IO private readonly string _fullPath; private readonly string _normalizedSearchPath; private readonly uint _oldMode; + private readonly bool _setBackOldMode; [SecuritySafeCritical] internal Win32FileSystemEnumerableIterator(string path, string originalUserPath, string searchPattern, SearchOption searchOption, SearchResultHandler resultHandler) @@ -122,7 +123,7 @@ namespace System.IO Debug.Assert(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly); Debug.Assert(resultHandler != null); - _oldMode = Interop.Kernel32.SetErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS); + _setBackOldMode = Interop.Kernel32.SetThreadErrorMode(Interop.Kernel32.SEM_FAILCRITICALERRORS, out _oldMode); string normalizedSearchPattern = PathHelpers.NormalizeSearchPattern(searchPattern); @@ -251,7 +252,11 @@ namespace System.IO } finally { - Interop.Kernel32.SetErrorMode(_oldMode); + if (_setBackOldMode) + { + uint _ignore; + Interop.Kernel32.SetThreadErrorMode(_oldMode, out _ignore); + } base.Dispose(disposing); } }