Skip to content

Commit 0f00aef

Browse files
authored
gh-152433: Fix ctypes import error on Windows UWP builds (GH-154700)
This defers a closure-related limitation to time of use, rather than import time. Further work is necessary to offer full functionality on UWP builds.
1 parent 8759666 commit 0f00aef

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Modules/_ctypes/malloc_closure.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,25 @@ static void more_core(void)
6868

6969
/* allocate a memory block */
7070
#ifdef MS_WIN32
71+
#ifdef MS_WINDOWS_DESKTOP
7172
item = (ITEM *)VirtualAlloc(NULL,
7273
count * sizeof(ITEM),
7374
MEM_COMMIT,
7475
PAGE_EXECUTE_READWRITE);
76+
#else // UWP
77+
/* Due security restrictions, UWP not allows request Read-Write-Execute permissions at once.
78+
The correct flow in UWP for execute dynamic code in memmory is:
79+
1. Alloate as Read-Write (PAGE_READWRITE) and write dynamic code to memmory.
80+
2. Change to Executable (PAGE_EXECUTE_READ) with 'VirtualProtectFromApp'.
81+
3. Flush cache with 'FlushInstructionCache' to ensure CPU instruction cache coherency
82+
before executing the generated code.
83+
TODO: Implement 2 and 3 in the appropriate places. For now, this defers
84+
the error from import time to time of use (or never, if an app avoids it). */
85+
item = (ITEM*)VirtualAllocFromApp(NULL,
86+
count * sizeof(ITEM),
87+
MEM_COMMIT | MEM_RESERVE,
88+
PAGE_READWRITE);
89+
#endif // !MS_WINDOWS_DESKTOP
7590
if (item == NULL)
7691
return;
7792
#else

0 commit comments

Comments
 (0)