Replace `SetErrorMode` with `SetThreadErrorMode`. (#90122)
authorAaron Robinson <arobins@microsoft.com>
Tue, 8 Aug 2023 02:00:24 +0000 (19:00 -0700)
committerGitHub <noreply@github.com>
Tue, 8 Aug 2023 02:00:24 +0000 (19:00 -0700)
* Replace SetErrorMode with SetThreadErrorMode.

Replaces all possible locations.
Removed SetErrorMode from PAL.

12 files changed:
src/coreclr/dlls/mscordac/mscordac_unixexports.src
src/coreclr/inc/holder.h
src/coreclr/pal/inc/pal.h
src/coreclr/pal/src/misc/error.cpp
src/coreclr/pal/tests/palsuite/CMakeLists.txt
src/coreclr/pal/tests/palsuite/compilableTests.txt
src/coreclr/pal/tests/palsuite/paltestlist.txt
src/coreclr/pal/tests/palsuite/threading/SetErrorMode/test1/test1.cpp [deleted file]
src/coreclr/vm/ceeload.cpp
src/coreclr/vm/peimage.cpp
src/coreclr/vm/peimagelayout.cpp
src/coreclr/vm/util.cpp

index 9f7d7c8..6372f19 100644 (file)
@@ -151,7 +151,6 @@ nativeStringResourceTable_mscorrc
 #SetEvent
 #SetFilePointer
 #SetLastError
-#SetErrorMode
 #SetThreadDescription
 #Sleep
 #SleepEx
index 325036b..16551b1 100644 (file)
@@ -1187,13 +1187,24 @@ FORCEINLINE void RegKeyRelease(HKEY k) {RegCloseKey(k);};
 typedef Wrapper<HKEY,DoNothing,RegKeyRelease> RegKeyHolder;
 #endif // HOST_WINDOWS
 
-class ErrorModeHolder
+class ErrorModeHolder final
 {
-    UINT m_oldMode;
+#ifdef HOST_WINDOWS
+    BOOL m_revert;
+    DWORD m_oldMode;
 public:
-    ErrorModeHolder(UINT newMode){m_oldMode=SetErrorMode(newMode);};
-    ~ErrorModeHolder(){SetErrorMode(m_oldMode);};
-    UINT OldMode() {return m_oldMode;};
+    ErrorModeHolder()
+        : m_revert{ FALSE }
+    {
+        DWORD newMode = SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS;
+        m_revert = ::SetThreadErrorMode(newMode, &m_oldMode);
+    }
+    ~ErrorModeHolder() noexcept
+    {
+        if (m_revert != FALSE)
+            (void)::SetThreadErrorMode(m_oldMode, NULL);
+    }
+#endif // HOST_WINDOWS
 };
 
 #ifdef HOST_WINDOWS
index 92455bc..d554162 100644 (file)
@@ -405,7 +405,7 @@ PALIMPORT
 VOID
 PALAPI
 PAL_SetCreateDumpCallback(
-    IN PCREATEDUMP_CALLBACK callback); 
+    IN PCREATEDUMP_CALLBACK callback);
 
 PALIMPORT
 BOOL
@@ -2760,15 +2760,6 @@ PALIMPORT VOID PALAPI LeaveCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalS
 PALIMPORT VOID PALAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection);
 PALIMPORT VOID PALAPI DeleteCriticalSection(IN OUT LPCRITICAL_SECTION lpCriticalSection);
 
-#define SEM_FAILCRITICALERRORS          0x0001
-#define SEM_NOOPENFILEERRORBOX          0x8000
-
-PALIMPORT
-UINT
-PALAPI
-SetErrorMode(
-         IN UINT uMode);
-
 #define PAGE_NOACCESS                   0x01
 #define PAGE_READONLY                   0x02
 #define PAGE_READWRITE                  0x04
index 3abe031..6b67bf7 100644 (file)
@@ -28,45 +28,6 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC);
 
 /*++
 Function:
-  SetErrorMode
-
-The SetErrorMode function controls whether the system will handle the
-specified types of serious errors, or whether the process will handle
-them.
-
-Parameters
-
-uMode
-       [in] Specifies the process error mode. This parameter can be one or more of the following values.
-
-        Value                     Action
-        0                         Use the system default, which is to display all error dialog boxes.
-        SEM_FAILCRITICALERRORS    The system does not display the critical-error-handler message box. Instead,
-                                  the system sends the error to the calling process.
-        SEM_NOOPENFILEERRORBOX    The system does not display a message box when it fails to find a file. Instead,
-                                  the error is returned to the calling process.
-
-Return Values
-
-The return value is the previous state of the error-mode bit flags.
-
---*/
-UINT
-PALAPI
-SetErrorMode(
-         IN UINT uMode)
-{
-  PERF_ENTRY(SetErrorMode);
-  ENTRY("SetErrorMode (uMode=%#x)\n", uMode);
-
-  LOGEXIT("SetErrorMode returns UINT 0\n");
-  PERF_EXIT(SetErrorMode);
-  return 0;
-}
-
-
-/*++
-Function:
   GetLastError
 
 GetLastError
index 91d9e35..e3698bf 100644 (file)
@@ -604,7 +604,6 @@ add_executable_clr(paltests
   threading/ResetEvent/test3/test3.cpp
   threading/ResetEvent/test4/test4.cpp
   threading/ResumeThread/test1/test1.cpp
-  threading/SetErrorMode/test1/test1.cpp
   threading/SetEvent/test1/test1.cpp
   threading/SetEvent/test2/test2.cpp
   threading/SetEvent/test3/test3.cpp
index 851ccd5..3719161 100644 (file)
@@ -490,7 +490,6 @@ threading/ResetEvent/test2/paltest_resetevent_test2
 threading/ResetEvent/test3/paltest_resetevent_test3
 threading/ResetEvent/test4/paltest_resetevent_test4
 threading/ResumeThread/test1/paltest_resumethread_test1
-threading/SetErrorMode/test1/paltest_seterrormode_test1
 threading/SetEvent/test1/paltest_setevent_test1
 threading/SetEvent/test2/paltest_setevent_test2
 threading/SetEvent/test3/paltest_setevent_test3
index 6f064d9..e2fdbdd 100644 (file)
@@ -415,7 +415,6 @@ threading/ResetEvent/test2/paltest_resetevent_test2
 threading/ResetEvent/test3/paltest_resetevent_test3
 threading/ResetEvent/test4/paltest_resetevent_test4
 threading/ResumeThread/test1/paltest_resumethread_test1
-threading/SetErrorMode/test1/paltest_seterrormode_test1
 threading/SetEvent/test1/paltest_setevent_test1
 threading/SetEvent/test2/paltest_setevent_test2
 threading/SetEvent/test3/paltest_setevent_test3
diff --git a/src/coreclr/pal/tests/palsuite/threading/SetErrorMode/test1/test1.cpp b/src/coreclr/pal/tests/palsuite/threading/SetErrorMode/test1/test1.cpp
deleted file mode 100644 (file)
index 6774312..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-/*=====================================================================
-**
-** Source:  test1.c (SetErrorMode)
-**
-** Purpose: Tests the PAL implementation of the SetErrorMode function.
-**          This test will set the error mode and then read the error
-**          set with GetLastError().
-**
-**
-**===================================================================*/
-
-#include <palsuite.h>
-
-PALTEST(threading_SetErrorMode_test1_paltest_seterrormode_test1, "threading/SetErrorMode/test1/paltest_seterrormode_test1")
-{   
-    DWORD dErrorReturn;
-    UINT  dErrorModes[] = {SEM_NOOPENFILEERRORBOX, SEM_FAILCRITICALERRORS, 0};
-    int   i;
-    
-    /*
-     *  Initialize the Pal
-     */
-    if ((PAL_Initialize(argc,argv)) != 0)
-    {
-        return (FAIL);
-    }
-
-    /*
-     *  Loop through the supported Error Modes and verify
-     *  that GetLastError() returns the correct Error Mode
-     */
-    for (i=0; i < (sizeof(dErrorModes) / sizeof(UINT)); i++)
-    {
-        SetLastError(dErrorModes[i]);
-        if ((dErrorReturn = GetLastError()) != dErrorModes[i])
-        {   
-            Fail("ERROR: SetLastError was set to 0x%4.4x but,"
-                    " GetLastError returned 0x%4.4x\n", 
-                    dErrorModes[i],
-                    dErrorReturn);
-        }
-    }
-        
-    PAL_Terminate();
-    return (PASS);
-}
index fc704bc..709ec1e 100644 (file)
@@ -2221,8 +2221,6 @@ ISymUnmanagedReader *Module::GetISymUnmanagedReader(void)
     // AddRef which we take inside the lock at the bottom of this method.
     CrstHolder holder(&m_ISymUnmanagedReaderCrst);
 
-    UINT lastErrorMode = 0;
-
     // If we haven't created a reader yet, do so now
     if (m_pISymUnmanagedReader == NULL)
     {
@@ -2278,7 +2276,7 @@ ISymUnmanagedReader *Module::GetISymUnmanagedReader(void)
 
         // Note: we change the error mode here so we don't get any popups as the PDB symbol reader attempts to search the
         // hard disk for files.
-        lastErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
+        ErrorModeHolder errorMode{};
 
         SafeComHolder<ISymUnmanagedReader> pReader;
 
@@ -2325,8 +2323,6 @@ ISymUnmanagedReader *Module::GetISymUnmanagedReader(void)
                 hr = pBinder->GetReaderForFile(pUnk, path, NULL, &pReader);
         }
 
-        SetErrorMode(lastErrorMode);
-
         if (SUCCEEDED(hr))
         {
             m_pISymUnmanagedReader = pReader.Extract();
index 0f235ad..55fc458 100644 (file)
@@ -872,7 +872,7 @@ HRESULT PEImage::TryOpenFile(bool takeLock)
     if (m_hFile!=INVALID_HANDLE_VALUE)
         return S_OK;
 
-    ErrorModeHolder mode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
+    ErrorModeHolder mode{};
     m_hFile=WszCreateFile((LPCWSTR)GetPathToLoad(),
                           GENERIC_READ
 #if TARGET_WINDOWS
index dcbe0f9..c756c45 100644 (file)
@@ -1190,7 +1190,6 @@ NativeImageLayout::NativeImageLayout(LPCWSTR fullPath)
     PVOID loadedImage;
 #if TARGET_UNIX
     {
-        ErrorModeHolder mode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
         HANDLE fileHandle = WszCreateFile(
             fullPath,
             GENERIC_READ,
index ad6f906..03c9568 100644 (file)
@@ -912,13 +912,12 @@ static HMODULE CLRLoadLibraryWorker(LPCWSTR lpLibFileName, DWORD *pLastError)
     STATIC_CONTRACT_FAULT;
 
     HMODULE hMod;
-    UINT last = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
+    ErrorModeHolder errorMode{};
     {
         INDEBUG(PEDecoder::ForceRelocForDLL(lpLibFileName));
         hMod = WszLoadLibrary(lpLibFileName);
         *pLastError = GetLastError();
     }
-    SetErrorMode(last);
     return hMod;
 }
 
@@ -949,13 +948,12 @@ static HMODULE CLRLoadLibraryExWorker(LPCWSTR lpLibFileName, HANDLE hFile, DWORD
     STATIC_CONTRACT_FAULT;
 
     HMODULE hMod;
-    UINT last = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
+    ErrorModeHolder errorMode{};
     {
         INDEBUG(PEDecoder::ForceRelocForDLL(lpLibFileName));
         hMod = WszLoadLibraryEx(lpLibFileName, hFile, dwFlags);
         *pLastError = GetLastError();
     }
-    SetErrorMode(last);
     return hMod;
 }