Skip to content

Commit c959860

Browse files
committed
unix/alloc: Add option to use uPy's alloc-exec implementation even for libffi.
When built for Linux, libffi includes very bloated and workaround exec-alloc implementation required to work around SELinux and other "sekuritee" features which real people don't use. MicroPython has own alloc-exec implementation, used to alloc memory for @micropython.native code. With this option enabled, uPy's implementation will override libffi's. This saves 11K on x86_64 (and that accounts for more than half of the libffi code size). TODO: Possibly, we want to refactor this option to allow either use uPy's implementation even for libffi, or allow to use libffi's implementation even for uPy.
1 parent 9b43a7d commit c959860

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

unix/alloc.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "py/mpstate.h"
3434
#include "py/gc.h"
3535

36-
#if MICROPY_EMIT_NATIVE
36+
#if MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)
3737

3838
#if defined(__OpenBSD__) || defined(__MACH__)
3939
#define MAP_ANONYMOUS MAP_ANON
@@ -85,4 +85,16 @@ void mp_unix_mark_exec(void) {
8585
}
8686
}
8787

88-
#endif // MICROPY_EMIT_NATIVE
88+
#if MICROPY_FORCE_PLAT_ALLOC_EXEC
89+
void *ffi_closure_alloc(size_t size, void **code) {
90+
mp_uint_t dummy;
91+
mp_unix_alloc_exec(size, code, &dummy);
92+
return *code;
93+
}
94+
95+
void ffi_closure_free(void *ptr) {
96+
// TODO
97+
}
98+
#endif
99+
100+
#endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)

unix/mpconfigport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ void mp_unix_free_exec(void *ptr, mp_uint_t size);
234234
void mp_unix_mark_exec(void);
235235
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) mp_unix_alloc_exec(min_size, ptr, size)
236236
#define MP_PLAT_FREE_EXEC(ptr, size) mp_unix_free_exec(ptr, size)
237+
#ifndef MICROPY_FORCE_PLAT_ALLOC_EXEC
238+
// Use MP_PLAT_ALLOC_EXEC for any executable memory allocation, including for FFI
239+
// (overriding libffi own implementation)
240+
#define MICROPY_FORCE_PLAT_ALLOC_EXEC (1)
241+
#endif
237242

238243
#if MICROPY_PY_OS_DUPTERM
239244
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)

0 commit comments

Comments
 (0)