Skip to content

Commit

Permalink
Fix issue5504: ctypes does now work with systems where mmap can't be
Browse files Browse the repository at this point in the history
PROT_WRITE and PROT_EXEC.
  • Loading branch information
Thomas Heller committed Aug 8, 2010
1 parent 93d768d commit 864cc67
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Core and Builtins
Extensions
----------

- Issue #5504 - ctypes should now work with systems where mmap can't
be PROT_WRITE and PROT_EXEC.

- Issue #9507: Named tuple repr will now automatically display the right
name in a tuple subclass.

Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3353,7 +3353,7 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->callable = callable;

self->thunk = thunk;
*(void **)self->b_ptr = (void *)thunk->pcl;
*(void **)self->b_ptr = (void *)thunk->pcl_exec;

Py_INCREF((PyObject *)thunk); /* for KeepRef */
if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
Expand Down
16 changes: 10 additions & 6 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ CThunkObject_dealloc(PyObject *_self)
Py_XDECREF(self->converters);
Py_XDECREF(self->callable);
Py_XDECREF(self->restype);
if (self->pcl)
_ctypes_free_closure(self->pcl);
if (self->pcl_write)
ffi_closure_free(self->pcl_write);
PyObject_GC_Del(self);
}

Expand Down Expand Up @@ -338,7 +338,8 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
return NULL;
}

p->pcl = NULL;
p->pcl_exec = NULL;
p->pcl_write = NULL;
memset(&p->cif, 0, sizeof(p->cif));
p->converters = NULL;
p->callable = NULL;
Expand Down Expand Up @@ -368,8 +369,9 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,

assert(CThunk_CheckExact((PyObject *)p));

p->pcl = _ctypes_alloc_closure();
if (p->pcl == NULL) {
p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
&p->pcl_exec);
if (p->pcl_write == NULL) {
PyErr_NoMemory();
goto error;
}
Expand Down Expand Up @@ -414,7 +416,9 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
"ffi_prep_cif failed with %d", result);
goto error;
}
result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
p,
p->pcl_exec);
if (result != FFI_OK) {
PyErr_Format(PyExc_RuntimeError,
"ffi_prep_closure failed with %d", result);
Expand Down
3 changes: 2 additions & 1 deletion Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ struct tagCDataObject {

typedef struct {
PyObject_VAR_HEAD
ffi_closure *pcl; /* the C callable */
ffi_closure *pcl_write; /* the C callable, writeable */
void *pcl_exec; /* the C callable, executable */
ffi_cif cif;
int flags;
PyObject *converters;
Expand Down
2 changes: 2 additions & 0 deletions Modules/_ctypes/libffi/fficonfig.py.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ffi_sources = """
src/prep_cif.c
src/closures.c
src/dlmalloc.c
""".split()

ffi_platforms = {
Expand Down
10 changes: 5 additions & 5 deletions Modules/_ctypes/libffi_msvc/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
extern void ffi_closure_OUTER();

ffi_status
ffi_prep_closure (ffi_closure* closure,
ffi_cif* cif,
void (*fun)(ffi_cif*,void*,void**,void*),
void *user_data)
ffi_prep_closure_loc (ffi_closure* closure,
ffi_cif* cif,
void (*fun)(ffi_cif*,void*,void**,void*),
void *user_data,
void *codeloc)
{
short bytes;
char *tramp;
Expand Down Expand Up @@ -452,6 +453,5 @@ ffi_prep_closure (ffi_closure* closure,
closure->cif = cif;
closure->user_data = user_data;
closure->fun = fun;

return FFI_OK;
}
8 changes: 6 additions & 2 deletions Modules/_ctypes/libffi_msvc/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,15 @@ typedef struct {
void *user_data;
} ffi_closure;

void ffi_closure_free(void *);
void *ffi_closure_alloc (size_t size, void **code);

ffi_status
ffi_prep_closure (ffi_closure*,
ffi_prep_closure_loc (ffi_closure*,
ffi_cif *,
void (*fun)(ffi_cif*,void*,void**,void*),
void *user_data);
void *user_data,
void *codeloc);

typedef struct {
char tramp[FFI_TRAMPOLINE_SIZE];
Expand Down
8 changes: 5 additions & 3 deletions Modules/_ctypes/malloc_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ static void more_core(void)
/******************************************************************/

/* put the item back into the free list */
void _ctypes_free_closure(void *p)
void ffi_closure_free(void *p)
{
ITEM *item = (ITEM *)p;
item->next = free_list;
free_list = item;
}

/* return one item from the free list, allocating more if needed */
void *_ctypes_alloc_closure(void)
void *ffi_closure_alloc(size_t ignored, void** codeloc)
{
ITEM *item;
if (!free_list)
Expand All @@ -106,5 +106,7 @@ void *_ctypes_alloc_closure(void)
return NULL;
item = free_list;
free_list = item->next;
return item;
*codeloc = (void *)item;
return (void *)item;
}

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,7 @@ def detect_ctypes(self, inc_dirs, lib_dirs):
'_ctypes/callbacks.c',
'_ctypes/callproc.c',
'_ctypes/stgdict.c',
'_ctypes/cfield.c',
'_ctypes/malloc_closure.c']
'_ctypes/cfield.c']
depends = ['_ctypes/ctypes.h']

if sys.platform == 'darwin':
Expand Down

0 comments on commit 864cc67

Please sign in to comment.