Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Modules/_ctypes/malloc_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ static void more_core(void)

/* allocate a memory block */
#ifdef MS_WIN32
#ifdef MS_WINDOWS_DESKTOP
item = (ITEM *)VirtualAlloc(NULL,
count * sizeof(ITEM),
MEM_COMMIT,
PAGE_EXECUTE_READWRITE);
#else
item = (ITEM*)VirtualAllocFromApp(NULL,
count * sizeof(ITEM),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this works without EXECUTE, then do we need EXECUTE in the alternative VirtualAlloc call?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More work is needed for ctypes to have full functionality in UWP.

The EXECUTE flag is required, but in UWP, it is not allowed alongside READWRITE. This PR addresses some of the issues that recently arose after adding this code:

_ctypes_mod_exec(PyObject *mod)
{
// See https://github.com/python/cpython/issues/128485
// This allocates some memory and then frees it to ensure that the
// the dlmalloc allocator initializes itself to avoid data races
// in free-threading.
void *codeloc = NULL;
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);

This causes errors immediately upon ctypes initialization (because reserves memory with execute permission).

But executing code in memory remains necessary for full ctypes functionality.

Let's say this PR only enables "limited" ctypes functionality on UWP but causes no harm on Windows Desktop.

#endif
if (item == NULL)
return;
#else
Expand Down
Loading