From 43426709c1807288dc93108ec6246b1f4efe8c52 Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Wed, 7 May 2025 13:39:44 +0200 Subject: [PATCH 1/5] disable invalid memory error reporting on unsupported WinAPI partitions --- Modules/mmapmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 6a385562845849..1cc10f4fea5b49 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -292,7 +292,7 @@ filter_page_exception_method(mmap_object *self, EXCEPTION_POINTERS *ptrs, } #endif -#if defined(MS_WINDOWS) && !defined(DONT_USE_SEH) +#if (defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) && !defined(DONT_USE_SEH) #define HANDLE_INVALID_MEM(sourcecode) \ do { \ EXCEPTION_RECORD record; \ @@ -320,7 +320,7 @@ do { \ } while (0) #endif -#if defined(MS_WINDOWS) && !defined(DONT_USE_SEH) +#if (defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) && !defined(DONT_USE_SEH) #define HANDLE_INVALID_MEM_METHOD(self, sourcecode) \ do { \ EXCEPTION_RECORD record; \ From dbd486c2e2591facdf9728dd78781581ee6a71d9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 7 May 2025 11:45:32 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst diff --git a/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst b/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst new file mode 100644 index 00000000000000..b8377b052b7e1a --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst @@ -0,0 +1 @@ +Disable error reporting for invalid memory access on unsupported WinAPI partitions. From 5c7ae18ef45711be290ae340d5e239cc82735ec7 Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Wed, 14 May 2025 14:25:02 +0200 Subject: [PATCH 3/5] apply code review --- Modules/mmapmodule.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 1cc10f4fea5b49..e3206b0d0eb490 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -60,6 +60,12 @@ my_getallocationgranularity (void) #endif +#if !defined(DONT_USE_SEH) && !(defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) +// Only the WINDOWS_DESKTOP and WINDOWS_SYSTEM API partitions support lsa handling we want to +// perform in there +#define DONT_USE_SEH +#endif + #ifdef UNIX #include #include @@ -292,7 +298,7 @@ filter_page_exception_method(mmap_object *self, EXCEPTION_POINTERS *ptrs, } #endif -#if (defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) && !defined(DONT_USE_SEH) +#if defined(MS_WINDOWS) && !defined(DONT_USE_SEH) #define HANDLE_INVALID_MEM(sourcecode) \ do { \ EXCEPTION_RECORD record; \ @@ -320,7 +326,7 @@ do { \ } while (0) #endif -#if (defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) && !defined(DONT_USE_SEH) +#if defined(MS_WINDOWS) && !defined(DONT_USE_SEH) #define HANDLE_INVALID_MEM_METHOD(self, sourcecode) \ do { \ EXCEPTION_RECORD record; \ From 0f9ff16ee4fe26a6dcb2aec9051c75c2efb122db Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Thu, 15 May 2025 11:32:38 +0200 Subject: [PATCH 4/5] cleanup patch --- Modules/mmapmodule.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index e3206b0d0eb490..7c4eb05488eb33 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -60,12 +60,6 @@ my_getallocationgranularity (void) #endif -#if !defined(DONT_USE_SEH) && !(defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)) -// Only the WINDOWS_DESKTOP and WINDOWS_SYSTEM API partitions support lsa handling we want to -// perform in there -#define DONT_USE_SEH -#endif - #ifdef UNIX #include #include @@ -296,6 +290,24 @@ filter_page_exception_method(mmap_object *self, EXCEPTION_POINTERS *ptrs, } return EXCEPTION_CONTINUE_SEARCH; } + +static void +_PyErr_SetFromNTSTATUS(ULONG status) +{ +#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM) + PyErr_SetFromWindowsErr(LsaNtStatusToWinError((NTSTATUS)status)); +#else + if (status & 0x80000000) { + // HRESULT-shaped codes are supported by PyErr_SetFromWindowsErr + PyErr_SetFromWindowsErr((int)status); + } + else { + // No mapping for NTSTATUS values, so just return it for diagnostic purposes + // If we provide it as winerror it could incorrectly change the type of the exception. + PyErr_Format(PyExc_OSError, "Operating system error NTSTATUS=0x%08lX", status); + } +#endif +} #endif #if defined(MS_WINDOWS) && !defined(DONT_USE_SEH) @@ -309,9 +321,7 @@ do { \ assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \ record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \ if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \ - NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \ - ULONG code = LsaNtStatusToWinError(status); \ - PyErr_SetFromWindowsErr(code); \ + _PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \ } \ else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \ PyErr_SetFromWindowsErr(ERROR_NOACCESS); \ @@ -338,9 +348,7 @@ do { \ assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \ record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \ if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \ - NTSTATUS status = (NTSTATUS) record.ExceptionInformation[2]; \ - ULONG code = LsaNtStatusToWinError(status); \ - PyErr_SetFromWindowsErr(code); \ + _PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \ } \ else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \ PyErr_SetFromWindowsErr(ERROR_NOACCESS); \ From 915c2987a677632c13e4e20026aed27adbf9babe Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Thu, 15 May 2025 11:37:05 +0200 Subject: [PATCH 5/5] update changelog --- .../next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst b/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst index b8377b052b7e1a..993eceab0b79c2 100644 --- a/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst +++ b/Misc/NEWS.d/next/Windows/2025-05-07-11-45-30.gh-issue-133572.Xc2zxH.rst @@ -1 +1 @@ -Disable error reporting for invalid memory access on unsupported WinAPI partitions. +Avoid LsaNtStatus to WinError conversion on unsupported WinAPI partitions.