From: Aaron Robinson Date: Tue, 8 Aug 2023 02:00:24 +0000 (-0700) Subject: Replace `SetErrorMode` with `SetThreadErrorMode`. (#90122) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~462 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e2c4b102ef02b14f57ec2c772913c6f4c611127b;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Replace `SetErrorMode` with `SetThreadErrorMode`. (#90122) * Replace SetErrorMode with SetThreadErrorMode. Replaces all possible locations. Removed SetErrorMode from PAL. --- diff --git a/src/coreclr/dlls/mscordac/mscordac_unixexports.src b/src/coreclr/dlls/mscordac/mscordac_unixexports.src index 9f7d7c8..6372f19 100644 --- a/src/coreclr/dlls/mscordac/mscordac_unixexports.src +++ b/src/coreclr/dlls/mscordac/mscordac_unixexports.src @@ -151,7 +151,6 @@ nativeStringResourceTable_mscorrc #SetEvent #SetFilePointer #SetLastError -#SetErrorMode #SetThreadDescription #Sleep #SleepEx diff --git a/src/coreclr/inc/holder.h b/src/coreclr/inc/holder.h index 325036b..16551b1 100644 --- a/src/coreclr/inc/holder.h +++ b/src/coreclr/inc/holder.h @@ -1187,13 +1187,24 @@ FORCEINLINE void RegKeyRelease(HKEY k) {RegCloseKey(k);}; typedef Wrapper 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 diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 92455bc..d554162 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -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 diff --git a/src/coreclr/pal/src/misc/error.cpp b/src/coreclr/pal/src/misc/error.cpp index 3abe031..6b67bf7 100644 --- a/src/coreclr/pal/src/misc/error.cpp +++ b/src/coreclr/pal/src/misc/error.cpp @@ -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 diff --git a/src/coreclr/pal/tests/palsuite/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/CMakeLists.txt index 91d9e35..e3698bf 100644 --- a/src/coreclr/pal/tests/palsuite/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/CMakeLists.txt @@ -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 diff --git a/src/coreclr/pal/tests/palsuite/compilableTests.txt b/src/coreclr/pal/tests/palsuite/compilableTests.txt index 851ccd5..3719161 100644 --- a/src/coreclr/pal/tests/palsuite/compilableTests.txt +++ b/src/coreclr/pal/tests/palsuite/compilableTests.txt @@ -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 diff --git a/src/coreclr/pal/tests/palsuite/paltestlist.txt b/src/coreclr/pal/tests/palsuite/paltestlist.txt index 6f064d9..e2fdbdd 100644 --- a/src/coreclr/pal/tests/palsuite/paltestlist.txt +++ b/src/coreclr/pal/tests/palsuite/paltestlist.txt @@ -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 index 6774312..0000000 --- a/src/coreclr/pal/tests/palsuite/threading/SetErrorMode/test1/test1.cpp +++ /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 - -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); -} diff --git a/src/coreclr/vm/ceeload.cpp b/src/coreclr/vm/ceeload.cpp index fc704bc..709ec1e 100644 --- a/src/coreclr/vm/ceeload.cpp +++ b/src/coreclr/vm/ceeload.cpp @@ -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 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(); diff --git a/src/coreclr/vm/peimage.cpp b/src/coreclr/vm/peimage.cpp index 0f235ad..55fc458 100644 --- a/src/coreclr/vm/peimage.cpp +++ b/src/coreclr/vm/peimage.cpp @@ -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 diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index dcbe0f9..c756c45 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -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, diff --git a/src/coreclr/vm/util.cpp b/src/coreclr/vm/util.cpp index ad6f906..03c9568 100644 --- a/src/coreclr/vm/util.cpp +++ b/src/coreclr/vm/util.cpp @@ -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; }