summaryrefslogtreecommitdiffstats
path: root/Modules/_ctypes
diff options
context:
space:
mode:
authorRonald Oussoren <ronaldoussoren@mac.com>2020-11-22 10:18:40 (GMT)
committerGitHub <noreply@github.com>2020-11-22 10:18:40 (GMT)
commite8b1c038b14b5fc8120aab62c9bf5fb840274cb6 (patch)
treee4b3ac9e3a17883c78d284c2f1552820ae098106 /Modules/_ctypes
parent0aab3522b259c40abf1f070c71aa7b914c1239b5 (diff)
downloadcpython-e8b1c038b14b5fc8120aab62c9bf5fb840274cb6.zip
cpython-e8b1c038b14b5fc8120aab62c9bf5fb840274cb6.tar.gz
cpython-e8b1c038b14b5fc8120aab62c9bf5fb840274cb6.tar.bz2
[3.9] bpo-41100: Support macOS 11 and Apple Silicon (GH-22855) (GH-23295)
* [3.9] bpo-41100: Support macOS 11 and Apple Silicon (GH-22855) Co-authored-by: Lawrence D’Anna <lawrence_danna@apple.com> * Add support for macOS 11 and Apple Silicon (aka arm64) As a side effect of this work use the system copy of libffi on macOS, and remove the vendored copy * Support building on recent versions of macOS while deploying to older versions This allows building installers on macOS 11 while still supporting macOS 10.9.. (cherry picked from commit 41761933c1c30bb6003b65eef1ba23a83db4eae4) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com> * Back port of changes to _decimal to support arm64 * temp_dir is in test.support in 3.9
Diffstat (limited to 'Modules/_ctypes')
-rw-r--r--Modules/_ctypes/callbacks.c39
-rw-r--r--Modules/_ctypes/callproc.c127
-rw-r--r--Modules/_ctypes/ctypes.h8
-rw-r--r--Modules/_ctypes/malloc_closure.c15
4 files changed, 167 insertions, 22 deletions
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 2abfa67..39cace5 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -1,6 +1,8 @@
#include "Python.h"
#include "frameobject.h"
+#include <stdbool.h>
+
#include <ffi.h>
#ifdef MS_WIN32
#include <windows.h>
@@ -18,7 +20,7 @@ CThunkObject_dealloc(PyObject *myself)
Py_XDECREF(self->callable);
Py_XDECREF(self->restype);
if (self->pcl_write)
- ffi_closure_free(self->pcl_write);
+ Py_ffi_closure_free(self->pcl_write);
PyObject_GC_Del(self);
}
@@ -361,8 +363,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
assert(CThunk_CheckExact((PyObject *)p));
- p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
- &p->pcl_exec);
+ p->pcl_write = Py_ffi_closure_alloc(sizeof(ffi_closure), &p->pcl_exec);
if (p->pcl_write == NULL) {
PyErr_NoMemory();
goto error;
@@ -408,13 +409,35 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
"ffi_prep_cif failed with %d", result);
goto error;
}
-#if defined(X86_DARWIN) || defined(POWERPC_DARWIN)
- result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p);
+#if HAVE_FFI_PREP_CLOSURE_LOC
+# if USING_APPLE_OS_LIBFFI
+# define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
+# else
+# define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME 1
+# endif
+ if (HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME) {
+ result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
+ p,
+ p->pcl_exec);
+ } else
+#endif
+ {
+#if USING_APPLE_OS_LIBFFI && defined(__arm64__)
+ PyErr_Format(PyExc_NotImplementedError, "ffi_prep_closure_loc() is missing");
+ goto error;
#else
- result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
- p,
- p->pcl_exec);
+#ifdef MACOSX
+ #pragma clang diagnostic push
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
+ result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p);
+
+#ifdef MACOSX
+ #pragma clang diagnostic pop
+#endif
+
+#endif
+ }
if (result != FFI_OK) {
PyErr_Format(PyExc_RuntimeError,
"ffi_prep_closure failed with %d", result);
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 6030cc3..b0a36a3 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -57,6 +57,8 @@
#include "Python.h"
#include "structmember.h" // PyMemberDef
+#include <stdbool.h>
+
#ifdef MS_WIN32
#include <windows.h>
#include <tchar.h>
@@ -64,6 +66,10 @@
#include "ctypes_dlfcn.h"
#endif
+#ifdef __APPLE__
+#include <mach-o/dyld.h>
+#endif
+
#ifdef MS_WIN32
#include <malloc.h>
#endif
@@ -812,7 +818,8 @@ static int _call_function_pointer(int flags,
ffi_type **atypes,
ffi_type *restype,
void *resmem,
- int argcount)
+ int argcount,
+ int argtypecount)
{
PyThreadState *_save = NULL; /* For Py_BLOCK_THREADS and Py_UNBLOCK_THREADS */
PyObject *error_object = NULL;
@@ -835,14 +842,70 @@ static int _call_function_pointer(int flags,
if ((flags & FUNCFLAG_CDECL) == 0)
cc = FFI_STDCALL;
#endif
- if (FFI_OK != ffi_prep_cif(&cif,
- cc,
- argcount,
- restype,
- atypes)) {
- PyErr_SetString(PyExc_RuntimeError,
- "ffi_prep_cif failed");
- return -1;
+
+# if USING_APPLE_OS_LIBFFI
+# define HAVE_FFI_PREP_CIF_VAR_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
+# elif HAVE_FFI_PREP_CIF_VAR
+# define HAVE_FFI_PREP_CIF_VAR_RUNTIME true
+# else
+# define HAVE_FFI_PREP_CIF_VAR_RUNTIME false
+# endif
+
+ /* Even on Apple-arm64 the calling convention for variadic functions conincides
+ * with the standard calling convention in the case that the function called
+ * only with its fixed arguments. Thus, we do not need a special flag to be
+ * set on variadic functions. We treat a function as variadic if it is called
+ * with a nonzero number of variadic arguments */
+ bool is_variadic = (argtypecount != 0 && argcount > argtypecount);
+ (void) is_variadic;
+
+#if defined(__APPLE__) && defined(__arm64__)
+ if (is_variadic) {
+ if (HAVE_FFI_PREP_CIF_VAR_RUNTIME) {
+ } else {
+ PyErr_SetString(PyExc_NotImplementedError, "ffi_prep_cif_var() is missing");
+ return -1;
+ }
+ }
+#endif
+
+#if HAVE_FFI_PREP_CIF_VAR
+ if (is_variadic) {
+ if (HAVE_FFI_PREP_CIF_VAR_RUNTIME) {
+ if (FFI_OK != ffi_prep_cif_var(&cif,
+ cc,
+ argtypecount,
+ argcount,
+ restype,
+ atypes)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "ffi_prep_cif_var failed");
+ return -1;
+ }
+ } else {
+ if (FFI_OK != ffi_prep_cif(&cif,
+ cc,
+ argcount,
+ restype,
+ atypes)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "ffi_prep_cif failed");
+ return -1;
+ }
+ }
+ } else
+#endif
+
+ {
+ if (FFI_OK != ffi_prep_cif(&cif,
+ cc,
+ argcount,
+ restype,
+ atypes)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "ffi_prep_cif failed");
+ return -1;
+ }
}
if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
@@ -1212,9 +1275,8 @@ PyObject *_ctypes_callproc(PPROC pProc,
if (-1 == _call_function_pointer(flags, pProc, avalues, atypes,
rtype, resbuf,
- Py_SAFE_DOWNCAST(argcount,
- Py_ssize_t,
- int)))
+ Py_SAFE_DOWNCAST(argcount, Py_ssize_t, int),
+ Py_SAFE_DOWNCAST(argtype_count, Py_ssize_t, int)))
goto cleanup;
#ifdef WORDS_BIGENDIAN
@@ -1398,6 +1460,42 @@ copy_com_pointer(PyObject *self, PyObject *args)
}
#else
+#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
+static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
+{
+ PyObject *name, *name2;
+ char *name_str;
+
+ if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
+ int r;
+
+ if (!PyArg_ParseTuple(args, "O", &name))
+ return NULL;
+
+ if (name == Py_None)
+ Py_RETURN_FALSE;
+
+ if (PyUnicode_FSConverter(name, &name2) == 0)
+ return NULL;
+ name_str = PyBytes_AS_STRING(name2);
+
+ r = _dyld_shared_cache_contains_path(name_str);
+ Py_DECREF(name2);
+
+ if (r) {
+ Py_RETURN_TRUE;
+ } else {
+ Py_RETURN_FALSE;
+ }
+
+ } else {
+ PyErr_SetString(PyExc_NotImplementedError, "_dyld_shared_cache_contains_path symbol is missing");
+ return NULL;
+ }
+
+ }
+#endif
+
static PyObject *py_dl_open(PyObject *self, PyObject *args)
{
PyObject *name, *name2;
@@ -1887,6 +1985,8 @@ buffer_info(PyObject *self, PyObject *arg)
return Py_BuildValue("siN", dict->format, dict->ndim, shape);
}
+
+
PyMethodDef _ctypes_module_methods[] = {
{"get_errno", get_errno, METH_NOARGS},
{"set_errno", set_errno, METH_VARARGS},
@@ -1909,6 +2009,9 @@ PyMethodDef _ctypes_module_methods[] = {
{"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
{"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
#endif
+#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
+ {"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
+#endif
{"alignment", align_func, METH_O, alignment_doc},
{"sizeof", sizeof_func, METH_O, sizeof_doc},
{"byref", byref, METH_VARARGS, byref_doc},
diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h
index 1effccf..3f20031 100644
--- a/Modules/_ctypes/ctypes.h
+++ b/Modules/_ctypes/ctypes.h
@@ -366,6 +366,14 @@ PyObject *_ctypes_get_errobj(int **pspace);
extern PyObject *ComError;
#endif
+#if USING_MALLOC_CLOSURE_DOT_C
+void Py_ffi_closure_free(void *p);
+void *Py_ffi_closure_alloc(size_t size, void** codeloc);
+#else
+#define Py_ffi_closure_free ffi_closure_free
+#define Py_ffi_closure_alloc ffi_closure_alloc
+#endif
+
/*
Local Variables:
compile-command: "python setup.py -q build install --home ~"
diff --git a/Modules/_ctypes/malloc_closure.c b/Modules/_ctypes/malloc_closure.c
index f9cdb33..4f220e4 100644
--- a/Modules/_ctypes/malloc_closure.c
+++ b/Modules/_ctypes/malloc_closure.c
@@ -89,16 +89,27 @@ static void more_core(void)
/******************************************************************/
/* put the item back into the free list */
-void ffi_closure_free(void *p)
+void Py_ffi_closure_free(void *p)
{
+#if USING_APPLE_OS_LIBFFI && HAVE_FFI_CLOSURE_ALLOC
+ if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) {
+ ffi_closure_free(p);
+ return;
+ }
+#endif
ITEM *item = (ITEM *)p;
item->next = free_list;
free_list = item;
}
/* return one item from the free list, allocating more if needed */
-void *ffi_closure_alloc(size_t ignored, void** codeloc)
+void *Py_ffi_closure_alloc(size_t size, void** codeloc)
{
+#if USING_APPLE_OS_LIBFFI && HAVE_FFI_CLOSURE_ALLOC
+ if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) {
+ return ffi_closure_alloc(size, codeloc);
+ }
+#endif
ITEM *item;
if (!free_list)
more_core();