libffi v3.1 - ffi_closure_alloc on Android ARM

Grégory Pakosz gpakosz@myscript.com
Thu Oct 30 20:40:00 GMT 2014


Hello,

When building libffi for Android ARM, invoking an ffi closure just crashes with SIGSEGV / SEGV_ACCERR (code 2).


It happens malloc on Android doesn't set PROT_EXEC contrary to Linux. As a consequence, to make closures work on Android, I had to tweak fficonfig.h by adding the following preprocessor  definitions:


#define FFI_MMAP_EXEC_WRIT       1
#define FFI_MMAP_EXEC_SELINUX 0

Otherwise, compiled ffi_closure_alloc is the one that just calls return *code = malloc (size);

Is defining those two preprocessor tokens the recommended way? If not, is there a better way?


Thank you,
Gregory.


PS: Here are the steps I used to build libffi

$ /opt/android-ndk/build/tools/make-standalone-toolchain.sh --arch=arm --instal-dir=/tmp/android-arm

$ export PATH=/tmp/android-arm/bin:$PATH

$ cd ~/libffi
$ git checkout v3.1
$ mkdir build-android-arm
$ cd build-android-arm
$ ../configure --host=arm-linux-androideabi

And here are the defines in the generated fficonfig.h

#define EH_FRAME_FLAGS "aw"
#define HAVE_ALLOCA 1
#define HAVE_ALLOCA_H 1
#define HAVE_AS_CFI_PSEUDO_OP 1
#define HAVE_DLFCN_H 1
#define HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 1
#define HAVE_INTTYPES_H 1
#define HAVE_MEMCPY 1
#define HAVE_MEMORY_H 1
#define HAVE_MMAP 1
#define HAVE_MMAP_ANON 1
#define HAVE_MMAP_DEV_ZERO 1
#define HAVE_MMAP_FILE 1
#define HAVE_STDINT_H 1
#define HAVE_STDLIB_H 1
#define HAVE_STRINGS_H 1
#define HAVE_STRING_H 1
#define HAVE_SYS_MMAN_H 1
#define HAVE_SYS_STAT_H 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_UNISTD_H 1
#define SIZEOF_DOUBLE 8
#define SIZEOF_LONG_DOUBLE 8
#define SIZEOF_SIZE_T 4
#define STDC_HEADERS 1​


PS2: for the record, Android 2.2 or 2.3's malloc set PROT_EXEC. I'm not sure if behavior changed with Honeycomb (3.0–3.2.6), Ice Cream Sandwich (4.0–4.0.4) or Jelly Bean (4.1–4.3.​1).​


More information about the Libffi-discuss mailing list