[issue5504] ctypes should work with systems where mmap can't be PROT_WRITE and PROT_EXEC

Łukasz Langa report at bugs.python.org
Sun Sep 5 15:46:50 CEST 2010


Łukasz Langa <lukasz at langa.pl> added the comment:

Please find attached a "fix" for #9662. Some explanation is in order:

- the Windows, Linux and OS X implementations of FFI included in the SVN are different in terms of maturity
- Thomas originally when fixing #5504 used a bit of functionality regarding closures that wasn't there in the Windows and OS X versions
- he did some dirty hacking on the Windows version to make it work: changed the ffi_prep_closure function header to ffi_prep_closure_loc (leaving the old implementation) and introduced names for ffi_closure_alloc and ffi_closure_free
- he didn't touch the OS X version so it failed

I did more-less the same thing but in case of OS X we have many ffi_prep_closure implementations for different archs so I simply did an #ifdef in the place ffi_prep_closure is used.

I think a better solution ultimately would be to update all implementation to FFI 3.0.9 and be done with it. Still, the current patch works for me.

----------
Added file: http://bugs.python.org/file18759/issue9662.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5504>
_______________________________________
-------------- next part --------------
Index: setup.py
===================================================================
--- setup.py	(revision 84528)
+++ setup.py	(working copy)
@@ -1657,6 +1657,7 @@
         depends = ['_ctypes/ctypes.h']
 
         if sys.platform == 'darwin':
+            sources.append('_ctypes/malloc_closure.c')
             sources.append('_ctypes/darwin/dlfcn_simple.c')
             extra_compile_args.append('-DMACOSX')
             include_dirs.append('_ctypes/darwin')
Index: Modules/_ctypes/libffi_osx/include/ffi.h
===================================================================
--- Modules/_ctypes/libffi_osx/include/ffi.h	(revision 84528)
+++ Modules/_ctypes/libffi_osx/include/ffi.h	(working copy)
@@ -264,6 +264,9 @@
 	void			(*fun)(ffi_cif*,void*,void**,void*),
 	void*			user_data);
 
+void ffi_closure_free(void *);
+void *ffi_closure_alloc (size_t size, void **code);
+
 typedef struct ffi_raw_closure {
 	char		tramp[FFI_TRAMPOLINE_SIZE];
 	ffi_cif*	cif;
@@ -349,4 +352,4 @@
 }
 #endif
 
-#endif	// #ifndef LIBFFI_H
\ No newline at end of file
+#endif	// #ifndef LIBFFI_H
Index: Modules/_ctypes/malloc_closure.c
===================================================================
--- Modules/_ctypes/malloc_closure.c	(revision 84528)
+++ Modules/_ctypes/malloc_closure.c	(working copy)
@@ -106,7 +106,6 @@
         return NULL;
     item = free_list;
     free_list = item->next;
-	*codeloc = (void *)item;
+    *codeloc = (void *)item;
     return (void *)item;
 }
-
Index: Modules/_ctypes/callbacks.c
===================================================================
--- Modules/_ctypes/callbacks.c	(revision 84528)
+++ Modules/_ctypes/callbacks.c	(working copy)
@@ -416,9 +416,13 @@
                      "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);
+#else
     result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
 				  p,
 				  p->pcl_exec);
+#endif
     if (result != FFI_OK) {
         PyErr_Format(PyExc_RuntimeError,
                      "ffi_prep_closure failed with %d", result);


More information about the Python-bugs-list mailing list