From 0b381be440984fd865852378604e2796be9a1133 Mon Sep 17 00:00:00 2001 From: Matthias Horn Date: Thu, 4 May 2017 10:50:53 +0100 Subject: [PATCH 1/2] Use executable copy of the FFI closure Modern CPUs restrict executing modifiable memory. Lib_ffi deals with this by mapping to copies of the closure object into memory. This change ensures that we use the executable copy for the function pointer. --- src/ctypes-foreign-base/ffi_call_stubs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ctypes-foreign-base/ffi_call_stubs.c b/src/ctypes-foreign-base/ffi_call_stubs.c index c26e4a1d..9b0561da 100644 --- a/src/ctypes-foreign-base/ffi_call_stubs.c +++ b/src/ctypes-foreign-base/ffi_call_stubs.c @@ -432,6 +432,7 @@ struct closure ffi_closure closure; intnat fnkey; struct call_context context; + void* fnptr; }; enum boxedfn_tags { Done, Fn }; @@ -589,6 +590,7 @@ value ctypes_make_function_pointer(value callspec_, value fnid) } else { closure->fnkey = Long_val(fnid); closure->context = callspec->context; + closure->fnptr = code_address; ffi_status status = ffi_prep_closure_loc ((ffi_closure *)closure, @@ -610,5 +612,5 @@ value ctypes_make_function_pointer(value callspec_, value fnid) /* Extract the raw address from a function pointer object */ value ctypes_raw_address_of_function_pointer(value closure) { - return CTYPES_FROM_PTR(*(struct closure **)Data_custom_val(closure)); + return CTYPES_FROM_PTR((*(struct closure **)Data_custom_val(closure))->fnptr); } From aeee034d03dc6553b1aefcf2941e424665662894 Mon Sep 17 00:00:00 2001 From: Jeremy Yallop Date: Fri, 5 May 2017 11:58:44 +0100 Subject: [PATCH 2/2] Update CHANGES.md and META for 0.11.4. --- CHANGES.md | 7 +++++++ META | 14 +++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index cbb73c03..fe15e3f7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,10 @@ +## ctypes 0.11.4 + +* Fix function pointer support on no-exec platforms + https://github.com/ocamllabs/ocaml-ctypes/pull/511 + +Thanks to Matthias Horn (@tiash) for contributing to this release. + ## ctypes 0.11.3 * Stub out some `long double` functions that are not supported on NetBSD and OpenBSD diff --git a/META b/META index b79eda3a..66645516 100644 --- a/META +++ b/META @@ -1,4 +1,4 @@ -version = "0.11.3" +version = "0.11.4" description = "Combinators for binding to C libraries without writing any C." requires = "unix bigarray str bytes" archive(byte) = "ctypes.cma" @@ -9,7 +9,7 @@ archive(native, plugin) = "ctypes.cmxs" exists_if = "ctypes.cma" package "top" ( - version = "0.11.3" + version = "0.11.4" description = "Toplevel printers for C types" requires = "ctypes" archive(byte) = "ctypes-top.cma" @@ -20,7 +20,7 @@ package "top" ( ) package "stubs" ( - version = "0.11.3" + version = "0.11.4" description = "Stub generation from C types" requires = "ctypes" archive(byte) = "cstubs.cma" @@ -32,13 +32,13 @@ package "stubs" ( ) package "foreign" ( - version = "0.11.3" + version = "0.11.4" description = "Dynamic linking of C functions" requires(-mt) = "ctypes.foreign.unthreaded" requires(mt) = "ctypes.foreign.threaded" package "base" ( - version = "0.11.3" + version = "0.11.4" description = "Dynamic linking of C functions (base package)" requires = "ctypes" archive(byte) = "ctypes-foreign-base.cma" @@ -49,7 +49,7 @@ package "foreign" ( ) package "threaded" ( - version = "0.11.3" + version = "0.11.4" description = "Dynamic linking of C functions (for use in threaded programs)" requires = "threads ctypes ctypes.foreign.base" archive(byte) = "ctypes-foreign-threaded.cma" @@ -60,7 +60,7 @@ package "foreign" ( ) package "unthreaded" ( - version = "0.11.3" + version = "0.11.4" description = "Dynamic linking of C functions (for use in unthreaded programs)" requires = "ctypes ctypes.foreign.base" archive(byte) = "ctypes-foreign-unthreaded.cma"