Skip to content

Commit 3c4f753

Browse files
committed
gh-152433: Windows: fix ctypes MemoryError in UWP build
UWP does not allow allocating memory with code execution permissions.
1 parent b86a41c commit 3c4f753

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Modules/_ctypes/malloc_closure.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,24 @@ 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 points 2. 3. in the appropriate places */
84+
item = (ITEM*)VirtualAllocFromApp(NULL,
85+
count * sizeof(ITEM),
86+
MEM_COMMIT | MEM_RESERVE,
87+
PAGE_READWRITE);
88+
#endif // !MS_WINDOWS_DESKTOP
7589
if (item == NULL)
7690
return;
7791
#else

0 commit comments

Comments
 (0)