Skip to content

Commit 5c77893

Browse files
committed
Make FFI callbacks work if using glasgowPLI libffi
1 parent 2019516 commit 5c77893

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

ports/unix/alloc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#define MAP_ANONYMOUS MAP_ANON
4040
#endif
4141

42+
#ifdef __CHERI_PURE_CAPABILITY__
43+
#include <cheriintrin.h>
44+
#endif
45+
4246
// The memory allocated here is not on the GC heap (and it may contain pointers
4347
// that need to be GC'd) so we must somehow trace this memory. We do it by
4448
// keeping a linked list of all mmap'd regions, and tracing them explicitly.
@@ -95,7 +99,13 @@ void ffi_closure_free(void *ptr);
9599
void *ffi_closure_alloc(size_t size, void **code) {
96100
size_t dummy;
97101
mp_unix_alloc_exec(size, code, &dummy);
102+
#ifdef __CHERI_PURE_CAPABILITY__
103+
void * ret = *code;
104+
*code = cheri_sentry_create((char*)*code + 1);
105+
return ret;
106+
#else
98107
return *code;
108+
#endif
99109
}
100110

101111
void ffi_closure_free(void *ptr) {

ports/unix/modffi.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ typedef struct _mp_obj_ffifunc_t {
9797
ffi_type *params[];
9898
} mp_obj_ffifunc_t;
9999

100-
#ifndef __CHERI_PURE_CAPABILITY__
101100
typedef struct _mp_obj_fficallback_t {
102101
mp_obj_base_t base;
103102
void *func;
@@ -107,14 +106,11 @@ typedef struct _mp_obj_fficallback_t {
107106
ffi_cif cif;
108107
ffi_type *params[];
109108
} mp_obj_fficallback_t;
110-
#endif
111109

112110
// STATIC const mp_obj_type_t opaque_type;
113111
STATIC const mp_obj_type_t ffimod_type;
114112
STATIC const mp_obj_type_t ffifunc_type;
115-
#ifndef __CHERI_PURE_CAPABILITY__
116113
STATIC const mp_obj_type_t fficallback_type;
117-
#endif
118114
STATIC const mp_obj_type_t ffivar_type;
119115

120116
#ifdef __CHERI_PURE_CAPABILITY__
@@ -285,14 +281,15 @@ STATIC mp_obj_t mod_ffi_func(mp_obj_t rettype, mp_obj_t addr_in, mp_obj_t argtyp
285281
}
286282
MP_DEFINE_CONST_FUN_OBJ_3(mod_ffi_func_obj, mod_ffi_func);
287283

288-
#ifndef __CHERI_PURE_CAPABILITY__
289284
STATIC void call_py_func(ffi_cif *cif, void *ret, void **args, void *user_data) {
290285
mp_obj_t pyargs[cif->nargs];
291286
mp_obj_fficallback_t *o = user_data;
292287
mp_obj_t pyfunc = o->pyfunc;
293288

294289
for (uint i = 0; i < cif->nargs; i++) {
295-
pyargs[i] = mp_obj_new_int(*(mp_int_t *)args[i]);
290+
pyargs[i] = (cif->arg_types[i] == &ffi_type_pointer)
291+
? mp_obj_new_cap(*(void **)args[i])
292+
: mp_obj_new_int(*(mp_int_t *)args[i]);
296293
}
297294
mp_obj_t res = mp_call_function_n_kw(pyfunc, cif->nargs, 0, pyargs);
298295

@@ -381,7 +378,6 @@ STATIC mp_obj_t mod_ffi_callback(size_t n_args, const mp_obj_t *pos_args, mp_map
381378
return MP_OBJ_FROM_PTR(o);
382379
}
383380
MP_DEFINE_CONST_FUN_OBJ_KW(mod_ffi_callback_obj, 3, mod_ffi_callback);
384-
#endif
385381

386382
STATIC mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symname_in) {
387383
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(self_in);
@@ -540,11 +536,9 @@ STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
540536
goto error;
541537
}
542538
values[i].ffi = (ffi_arg)(intptr_t)bufinfo.buf;
543-
#ifndef __CHERI_PURE_CAPABILITY__
544539
} else if (mp_obj_is_type(a, &fficallback_type)) {
545540
mp_obj_fficallback_t *p = MP_OBJ_TO_PTR(a);
546541
values[i].ffi = (ffi_arg)(intptr_t)p->func;
547-
#endif
548542
} else {
549543
goto error;
550544
}
@@ -567,7 +561,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
567561
call, ffifunc_call
568562
);
569563

570-
#ifndef __CHERI_PURE_CAPABILITY__
571564
// FFI callback for Python function
572565

573566
STATIC void fficallback_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -594,7 +587,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
594587
print, fficallback_print,
595588
locals_dict, &fficallback_locals_dict
596589
);
597-
#endif
598590
// FFI variable
599591

600592
STATIC void ffivar_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -660,9 +652,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
660652
STATIC const mp_rom_map_elem_t mp_module_ffi_globals_table[] = {
661653
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ffi) },
662654
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_ffi_open_obj) },
663-
#ifndef __CHERI_PURE_CAPABILITY__
664655
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&mod_ffi_callback_obj) },
665-
#endif
666656
{ MP_ROM_QSTR(MP_QSTR_func), MP_ROM_PTR(&mod_ffi_func_obj) },
667657
{ MP_ROM_QSTR(MP_QSTR_as_bytearray), MP_ROM_PTR(&mod_ffi_as_bytearray_obj) },
668658
};

0 commit comments

Comments
 (0)