X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=301995e5903d9bc377c3d2630f1be48fed9e0513;hp=eb5b2b518aa800bf8f9922d50cdd1d66e833785c;hb=d3171526ab7658114cac4ebe1098af4b038e576e;hpb=f6b2422d4a9cb0424412a4077241cfd5bcb9ebeb diff --git a/src/libffi.c b/src/libffi.c index eb5b2b5..301995e 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -1,6 +1,21 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "php.h" + +#ifdef HAVE_LIBFFI + #include "php_psi.h" #include "libffi.h" +#include "engine.h" + +#undef PACKAGE +#undef PACKAGE_BUGREPORT +#undef PACKAGE_NAME +#undef PACKAGE_STRING +#undef PACKAGE_TARNAME +#undef PACKAGE_VERSION #include @@ -10,6 +25,9 @@ # endif # if HAVE_SYS_MMAN_H # include +# ifndef MAP_ANONYMOUS +# define MAP_ANONYMOUS MAP_ANON +# endif # endif #endif @@ -38,41 +56,39 @@ static void psi_ffi_closure_free(void *c) #endif } -static void handler(ffi_cif *signature, void *_result, void **_args, void *_data); +static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data); +static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg); static inline ffi_abi psi_ffi_abi(const char *convention) { return FFI_DEFAULT_ABI; } -static inline ffi_type *psi_ffi_type(token_t t) { +static inline ffi_type *psi_ffi_token_type(token_t t) { switch (t) { default: ZEND_ASSERT(0); /* no break */ case PSI_T_VOID: return &ffi_type_void; - case PSI_T_SINT8: + case PSI_T_INT8: return &ffi_type_sint8; case PSI_T_UINT8: return &ffi_type_uint8; - case PSI_T_SINT16: + case PSI_T_INT16: return &ffi_type_sint16; case PSI_T_UINT16: return &ffi_type_uint16; - case PSI_T_SINT32: + case PSI_T_INT32: return &ffi_type_sint32; case PSI_T_UINT32: return &ffi_type_uint32; - case PSI_T_SINT64: + case PSI_T_INT64: return &ffi_type_sint64; case PSI_T_UINT64: return &ffi_type_uint64; case PSI_T_BOOL: return &ffi_type_uchar; - case PSI_T_CHAR: - return &ffi_type_schar; - case PSI_T_SHORT: - return &ffi_type_sshort; case PSI_T_INT: + case PSI_T_ENUM: return &ffi_type_sint; case PSI_T_LONG: return &ffi_type_slong; @@ -80,10 +96,123 @@ static inline ffi_type *psi_ffi_type(token_t t) { return &ffi_type_float; case PSI_T_DOUBLE: return &ffi_type_double; +#ifdef HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + return &ffi_type_longdouble; +#endif + case PSI_T_POINTER: + return &ffi_type_pointer; + } +} +static inline ffi_type *psi_ffi_impl_type(token_t impl_type) { + switch (impl_type) { + case PSI_T_BOOL: + return &ffi_type_sint8; + case PSI_T_INT: + return &ffi_type_sint64; + case PSI_T_STRING: + return &ffi_type_pointer; + case PSI_T_FLOAT: + case PSI_T_DOUBLE: + return &ffi_type_double; + EMPTY_SWITCH_DEFAULT_CASE(); + } + return NULL; +} +static void psi_ffi_struct_type_dtor(void *type) { + ffi_type *strct = type; + + if (strct->elements) { + ffi_type **ptr; + + for (ptr = strct->elements; *ptr; ++ptr) { + free(*ptr); + } + free(strct->elements); + } + free(strct); +} + +static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) { + size_t i; + + for (i = 0; i < padding; ++i) { + ffi_type *pad = malloc(sizeof(*pad)); + + memcpy(pad, &ffi_type_schar, sizeof(*pad)); + *els++ = pad; } + + return padding; +} + +static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { + size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0; + ffi_type **els = calloc(argc + 1, sizeof(*els)); + + for (i = 0; i < strct->args->count; ++i) { + decl_arg *darg = strct->args->args[i]; + ffi_type *type = malloc(sizeof(*type)); + size_t padding; + + memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type)); + + ZEND_ASSERT(type->size == darg->layout->len); + + if (type->alignment > maxalign) { + maxalign = type->alignment; + } + + if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) { + if (nels + padding + 1 > argc) { + argc += padding; + els = realloc(els, (argc + 1) * sizeof(*els)); + els[argc] = NULL; + } + psi_ffi_struct_type_pad(&els[nels], padding); + nels += padding; + offset += padding; + } + ZEND_ASSERT(offset == darg->layout->pos); + + offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1); + els[nels++] = type; + } + + /* apply struct alignment padding */ + offset = (offset + maxalign - 1) & ~(maxalign - 1); + + ZEND_ASSERT(offset <= strct->size); + if (offset < strct->size) { + psi_ffi_struct_type_pad(&els[nels], strct->size - offset); + } + + return els; } static inline ffi_type *psi_ffi_decl_type(decl_type *type) { - return psi_ffi_type(real_decl_type(type)->type); + decl_type *real = real_decl_type(type); + + switch (real->type) { + case PSI_T_STRUCT: + if (!real->strct->engine.type) { + ffi_type *strct = calloc(1, sizeof(ffi_type)); + + strct->type = FFI_TYPE_STRUCT; + strct->size = 0; + strct->elements = psi_ffi_struct_type_elements(real->strct); + + real->strct->engine.type = strct; + real->strct->engine.dtor = psi_ffi_struct_type_dtor; + } + + return real->strct->engine.type; + + case PSI_T_UNION: + return psi_ffi_decl_arg_type(real->unn->args->args[0]); + + default: + return psi_ffi_token_type(real->type); + } } static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { if (darg->var->pointer_level) { @@ -96,70 +225,65 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { typedef struct PSI_LibffiContext { ffi_cif signature; ffi_type *params[2]; - struct { - struct PSI_LibffiData **list; - size_t count; - } data; } PSI_LibffiContext; -typedef struct PSI_LibffiData { - PSI_LibffiContext *context; - impl *impl; - zend_internal_arg_info *arginfo; +typedef struct PSI_LibffiCall { void *code; ffi_closure *closure; ffi_cif signature; - ffi_type *params[1]; -} PSI_LibffiData; + void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ +} PSI_LibffiCall; -static inline PSI_LibffiData *PSI_LibffiDataAlloc(PSI_LibffiContext *context, impl *impl) { - ffi_status rc; - size_t i, c = impl->decl->args->count; - PSI_LibffiData *data = malloc(sizeof(*data) + c * sizeof(ffi_type *)); +static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { + int rc; + size_t i, c = decl->args ? decl->args->count : 0; + PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); - data->context = context; - data->impl = impl; - data->arginfo = psi_internal_arginfo(impl); for (i = 0; i < c; ++i) { - data->params[i] = psi_ffi_decl_arg_type(impl->decl->args->args[i]); + call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]); } - data->params[c] = NULL; - - rc = ffi_prep_cif( - &data->signature, - psi_ffi_abi(data->impl->decl->abi->convention), - c, - psi_ffi_decl_arg_type(data->impl->decl->func), - data->params); + call->params[c] = NULL; + + decl->call.info = call; + decl->call.rval = &decl->func->ptr; + decl->call.argc = c; + decl->call.args = (void **) &call->params[c+1]; + + rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), + c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params); ZEND_ASSERT(FFI_OK == rc); - data->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &data->code); - ZEND_ASSERT(data->closure != NULL); + return call; +} + +static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { + PSI_LibffiContext *context = C->context; + int rc; + + call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code); + ZEND_ASSERT(call->closure != NULL); + #if PSI_HAVE_FFI_PREP_CLOSURE_LOC rc = ffi_prep_closure_loc( - data->closure, + call->closure, &context->signature, - handler, - data, - data->code); - ZEND_ASSERT(FFI_OK == rc); + psi_ffi_handler, + impl, + call->code); + #elif PSI_HAVE_FFI_PREP_CLOSURE - rc = ffi_prep_closure(data->code, &context->signature, handler, data); - ZEND_ASSERT(FFI_OK == rc); + rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl); #else # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available" #endif - - context->data.list = realloc(context->data.list, ++context->data.count * sizeof(*context->data.list)); - context->data.list[context->data.count-1] = data; - - return data; + ZEND_ASSERT(FFI_OK == rc); } -static inline void PSI_LibffiDataFree(PSI_LibffiData *data) { - psi_ffi_closure_free(data->closure); - free(data->arginfo); - free(data); +static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { + if (call->closure) { + psi_ffi_closure_free(call->closure); + } + free(call); } static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { @@ -178,118 +302,118 @@ static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { return L; } -static inline void PSI_LibffiContextDtor(PSI_LibffiContext *L) { - size_t i; - - for (i = 0; i < L->data.count; ++i) { - PSI_LibffiDataFree(L->data.list[i]); - } - if (L->data.list) { - free(L->data.list); - } +static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data) +{ + psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data); } -static inline void PSI_LibffiContextFree(PSI_LibffiContext **L) { - if (*L) { - PSI_LibffiContextDtor(*L); - free(*L); - *L = NULL; - } +static void psi_ffi_init(PSI_Context *C) +{ + C->context = PSI_LibffiContextInit(NULL); } -static void handler(ffi_cif *_sig, void *_result, void **_args, void *_data) +static void psi_ffi_dtor(PSI_Context *C) { - PSI_LibffiData *data = _data; - size_t i; - void **arg_ptr = NULL, **arg_prm = NULL; - impl_val ret_val; - - if (SUCCESS != psi_parse_args(*(zend_execute_data **)_args[0], data->impl)) { - return; - } - - if (data->impl->decl->args->count) { - arg_ptr = malloc(data->impl->decl->args->count * sizeof(*arg_ptr)); - arg_prm = malloc(data->impl->decl->args->count * sizeof(*arg_prm)); + if (C->decls) { + size_t i; - for (i = 0; i < data->impl->decl->args->count; ++i) { - decl_arg *darg = data->impl->decl->args->args[i]; + for (i = 0; i < C->decls->count; ++i) { + decl *decl = C->decls->list[i]; - arg_ptr[i] = psi_do_let(darg); - arg_prm[i] = darg->let->val->is_reference ? &arg_ptr[i] : arg_ptr[i]; - - darg->let->ptr = arg_ptr[i]; + if (decl->call.info) { + PSI_LibffiCallFree(decl->call.info); + } } } + free(C->context); +} - ffi_call(&data->signature, FFI_FN(data->impl->decl->dlptr), &ret_val, arg_prm); +static zend_function_entry *psi_ffi_compile(PSI_Context *C) +{ + size_t i, j = 0; + zend_function_entry *zfe; - psi_do_return(data->impl, &ret_val, *(zval **)_args[1]); + if (!C->impls) { + return NULL; + } - for (i = 0; i < data->impl->stmts->set.count; ++i) { - set_stmt *set = data->impl->stmts->set.list[i]; + zfe = calloc(C->impls->count + 1, sizeof(*zfe)); + for (i = 0; i < C->impls->count; ++i) { + zend_function_entry *zf = &zfe[j]; + PSI_LibffiCall *call; + impl *impl = C->impls->list[i]; - psi_do_set(set->arg->_zv, set->val->func, set->val->vars); - } + if (!impl->decl) { + continue; + } - for (i = 0; i < data->impl->stmts->fre.count; ++i) { - free_stmt *fre = data->impl->stmts->fre.list[i]; + call = PSI_LibffiCallAlloc(C, impl->decl); + PSI_LibffiCallInitClosure(C, call, impl); - psi_do_free(fre); + zf->fname = impl->func->name + (impl->func->name[0] == '\\'); + zf->num_args = impl->func->args->count; + zf->handler = call->code; + zf->arg_info = psi_internal_arginfo(impl); + ++j; } - psi_do_clean(data->impl); + for (i = 0; i < C->decls->count; ++i) { + decl *decl = C->decls->list[i]; - if (arg_ptr) { - free(arg_ptr); - } - if (arg_prm) { - free(arg_prm); - } -} + if (decl->impl) { + continue; + } -static void init(PSI_Context *C) -{ - C->context = PSI_LibffiContextInit(NULL); -} + PSI_LibffiCallAlloc(C, decl); + } -static void dtor(PSI_Context *C) -{ - PSI_LibffiContextFree((void *) &C->context); + return zfe; } -static zend_function_entry *compile(PSI_Context *C, PSI_Data *D) -{ - size_t i, j = 0; - zend_function_entry *zfe = calloc(D->impls->count + 1, sizeof(*zfe)); - PSI_LibffiContext *ctx = C->context; +static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) { + PSI_LibffiCall *call = decl_call->info; - for (i = 0; i < D->impls->count; ++i) { - zend_function_entry *zf = &zfe[j]; - PSI_LibffiData *data; + if (va) { + ffi_status rc; + ffi_cif signature; + size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count; + void **params = calloc(2 * ntotalargs + 2, sizeof(void *)); - if (!D->impls->list[i]->decl) { - continue; + for (i = 0; i < nfixedargs; ++i) { + params[i] = call->params[i]; + params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1]; } - - data = PSI_LibffiDataAlloc(ctx, D->impls->list[i]); - zf->fname = D->impls->list[i]->func->name + (D->impls->list[i]->func->name[0] == '\\'); - zf->num_args = D->impls->list[i]->func->args->count; - zf->handler = data->code; - zf->arg_info = data->arginfo; - ++j; + for (i = 0; i < va->args->count; ++i) { + params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]); + params[nfixedargs + i + ntotalargs + 1] = &va->values[i]; + } +#ifdef PSI_HAVE_FFI_PREP_CIF_VAR + rc = ffi_prep_cif_var(&signature, call->signature.abi, + nfixedargs, ntotalargs, + call->signature.rtype, (ffi_type **) params); +#else + /* FIXME: test in config.m4; assume we can just call anyway */ + rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs, + call->signature.rtype, (ffi_type **) params); +#endif + ZEND_ASSERT(FFI_OK == rc); + ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, ¶ms[ntotalargs + 1]); + free(params); + } else { + ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args); } - - return zfe; } static PSI_ContextOps ops = { - init, - dtor, - compile, + psi_ffi_init, + psi_ffi_dtor, + psi_ffi_compile, + psi_ffi_call, }; PSI_ContextOps *PSI_Libffi(void) { return &ops; } + +#endif /* HAVE_LIBFFI */