X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=1741513b02df21a3d8409e441bf920a5ae9504d4;hp=aea1cfa15faabccbcbe4d25055aae457a5639231;hb=fbd71a516e68ea8b106261f450c7f215349b24c4;hpb=9bcb1df0786a8193d65949c857baaba2f4296e84 diff --git a/src/libffi.c b/src/libffi.c index aea1cfa..1741513 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -69,7 +69,7 @@ static void *psi_ffi_closure_alloc(size_t s, void **code) static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) { *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code); - ZEND_ASSERT(*closure != NULL); + assert(*closure != NULL); #if PSI_HAVE_FFI_PREP_CLOSURE_LOC return ffi_prep_closure_loc(*closure, sig, handler, data, *code); @@ -102,7 +102,7 @@ static void psi_ffi_prep_va(ffi_cif *base, ffi_cif *signature, size_t argc, size rc = ffi_prep_cif(signature, base->abi, argc + va_count, base->rtype, param_types); #endif - ZEND_ASSERT(FFI_OK == rc); + assert(FFI_OK == rc); } static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg); @@ -156,6 +156,18 @@ static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data } static inline ffi_abi psi_ffi_abi(const char *convention) { + if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) { +#ifdef HAVE_FFI_STDCALL + if (!strcasecmp(convention, "stdcall")) { + return FFI_STDCALL; + } +#endif +#ifdef HAVE_FFI_FASTCALL + if (!strcasecmp(convention, "fastcall")) { + return FFI_FASTCALL; + } +#endif + } return FFI_DEFAULT_ABI; } @@ -175,7 +187,7 @@ static inline struct psi_ffi_call *psi_ffi_call_alloc(struct psi_context *C, str rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), c, psi_ffi_decl_arg_type(decl->func), call->params); - ZEND_ASSERT(FFI_OK == rc); + assert(FFI_OK == rc); return call; } @@ -205,7 +217,7 @@ static inline void psi_ffi_call_free(struct psi_ffi_call *call) { static inline ffi_type *psi_ffi_token_type(token_t t) { switch (t) { default: - ZEND_ASSERT(0); + assert(0); /* no break */ case PSI_T_VOID: return &ffi_type_void; @@ -288,14 +300,24 @@ static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) { } static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) { - size_t i = 0, argc = psi_plist_count(strct->args), nels = 0, offset = 0, maxalign = 0; - ffi_type **els = calloc(argc + 1, sizeof(*els)); + size_t i = 0, argc, nels = 0, offset = 0, maxalign = 0, last_arg_pos = -1; + ffi_type **tmp, **els; struct psi_decl_arg *darg; + argc = psi_plist_count(strct->args); + els = calloc(argc + 1, sizeof(*els)); + while (psi_plist_get(strct->args, i++, &darg)) { - ffi_type *type = malloc(sizeof(*type)); + ffi_type *type; size_t padding; + if (darg->layout->pos == last_arg_pos) { + /* skip bit fields */ + continue; + } + last_arg_pos = darg->layout->pos; + + type = malloc(sizeof(*type)); *type = *psi_ffi_decl_arg_type(darg); if (type->alignment > maxalign) { @@ -306,7 +328,13 @@ static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) { 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)); + tmp = realloc(els, (argc + 1) * sizeof(*els)); + if (tmp) { + els = tmp; + } else { + free(els); + return NULL; + } els[argc] = NULL; } psi_ffi_struct_type_pad(&els[nels], padding); @@ -324,7 +352,17 @@ static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) { assert(offset <= strct->size); if (offset < strct->size) { - psi_ffi_struct_type_pad(&els[nels], strct->size - offset); + size_t padding = strct->size - offset; + + tmp = realloc(els, (padding + argc + 1) * sizeof(*els)); + if (tmp) { + els = tmp; + } else { + free(els); + return NULL; + } + psi_ffi_struct_type_pad(&els[nels], padding); + els[argc + padding] = NULL; } return els; @@ -378,7 +416,7 @@ static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_contex L->params[0] = &ffi_type_pointer; L->params[1] = &ffi_type_pointer; rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params); - ZEND_ASSERT(rc == FFI_OK); + assert(rc == FFI_OK); return L; }