X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Ftypes%2Fdecl.c;h=581eadd95d01858261b846ea76b1c99b2aaef702;hp=95122ab1d9dad9112f172324f534abd1999b9ae2;hb=09529efcde471127419e141807b83b37077003a0;hpb=31cfe5e5e1bd44d408c5869e63fcc7419e81c294 diff --git a/src/types/decl.c b/src/types/decl.c index 95122ab..581eadd 100644 --- a/src/types/decl.c +++ b/src/types/decl.c @@ -24,8 +24,13 @@ *******************************************************************************/ #include "php_psi_stdinc.h" +#include "php_psi.h" #include +#include + +#include + #include "data.h" #define PSI_FUNC_REDIRS @@ -53,14 +58,20 @@ void psi_decl_free(struct psi_decl **d_ptr) if (d->args) { psi_plist_free(d->args); } + if (d->redir) { + zend_string_release(d->redir); + } free(d); } } void psi_decl_dump(int fd, struct psi_decl *decl) { - psi_decl_abi_dump(fd, decl->abi); + if (decl->abi) { + psi_decl_abi_dump(fd, decl->abi); + } dprintf(fd, " "); + /* FIXME: functions returning arrays */ psi_decl_arg_dump(fd, decl->func, 0); dprintf(fd, "("); if (decl->args) { @@ -77,25 +88,39 @@ void psi_decl_dump(int fd, struct psi_decl *decl) dprintf(fd, ", ..."); } } - dprintf(fd, ");"); + if (decl->func->var->array_size) { + dprintf(fd, ")[%u]", decl->func->var->array_size); + } + if (decl->redir) { + dprintf(fd, ") __asm__ (\"%s\");", decl->redir->val); + } else { + dprintf(fd, ");"); + } } static inline bool psi_decl_validate_func(struct psi_data *data, - struct psi_decl *decl, struct psi_decl_arg *func, void *dl) + struct psi_decl *decl, struct psi_decl_arg *func) { struct psi_func_redir *redir; - if (!strcmp(func->var->name, "dlsym")) { - data->error(data, func->token, PSI_WARNING, - "Cannot dlsym dlsym (sic!)"); + if (!func->var->name) { + data->error(data, func->token, PSI_WARNING, "Cannot load anonymous decl"); return false; } for (redir = &psi_func_redirs[0]; redir->name; ++redir) { - if (!strcmp(func->var->name, redir->name)) { + if (!strcmp(func->var->name->val, redir->name)) { decl->sym = redir->func; } } + if (!decl->sym) { + size_t i = 0; + void *dl; + + while (!decl->sym && psi_plist_get(data->file.dlopened, i++, &dl)) { + decl->sym = dlsym(dl, decl->redir ? decl->redir->val : func->var->name->val); + } + } if (!decl->sym) { #ifndef RTLD_NEXT # define RTLD_NEXT ((void *) -1l) @@ -103,10 +128,12 @@ static inline bool psi_decl_validate_func(struct psi_data *data, #ifndef RTLD_DEFAULT # define RTLD_DEFAULT ((void *) 0) #endif - decl->sym = dlsym(dl ?: RTLD_DEFAULT, func->var->name); + decl->sym = dlsym(RTLD_DEFAULT, decl->redir ? decl->redir->val : func->var->name->val); if (!decl->sym) { data->error(data, func->token, PSI_WARNING, - "Failed to locate symbol '%s': %s", func->var->name, + "Failed to locate symbol '%s(%s)': %s", + func->var->name->val, + decl->redir ? decl->redir->val : "", dlerror() ?: "not found"); return false; } @@ -114,13 +141,13 @@ static inline bool psi_decl_validate_func(struct psi_data *data, return true; } -bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl, - struct psi_validate_stack *type_stack) +bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, + struct psi_validate_scope *scope) { - if (!psi_decl_validate_nodl(data, decl, type_stack)) { + if (!psi_decl_validate_nodl(data, decl, scope)) { return false; } - if (!psi_decl_validate_func(data, decl, decl->func, dl)) { + if (!psi_decl_validate_func(data, decl, decl->func)) { return false; } @@ -128,16 +155,17 @@ bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl, } bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl, - struct psi_validate_stack *type_stack) + struct psi_validate_scope *scope) { if (!decl->abi) { - decl->abi = psi_decl_abi_init("default"); + decl->abi = psi_decl_abi_init(NULL); } else if (!psi_decl_abi_validate(data, decl->abi)) { data->error(data, decl->abi->token, PSI_WARNING, - "Invalid calling convention: '%s'", decl->abi->token->text); + "Invalid calling convention: '%s'", + decl->abi->token->text->val); return false; } - if (!psi_decl_arg_validate(data, decl->func, type_stack)) { + if (!psi_decl_arg_validate(data, decl->func, scope)) { return false; } if (decl->args) { @@ -146,11 +174,15 @@ bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl, while (psi_plist_get(decl->args, i++, &arg)) { if (!arg->var->name) { - arg->var->name = malloc(7); - snprintf(arg->var->name, 6, "arg%zu", i); - arg->var->fqn = strdup(arg->var->name); + smart_str name = {0}; + + smart_str_appendl_ex(&name, ZEND_STRL("arg"), 1); + smart_str_append_unsigned_ex(&name, i, 1); + + arg->var->name = smart_str_extract(&name); + arg->var->fqn = zend_string_copy(arg->var->name); } - if (!psi_decl_arg_validate(data, arg, type_stack)) { + if (!psi_decl_arg_validate(data, arg, scope)) { return false; } } @@ -158,3 +190,32 @@ bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl, return true; } + +bool psi_decl_is_blacklisted(const char *name) +{ + char *blacklisted; + size_t i = 0; + + while (psi_plist_get(PSI_G(blacklist).decls, i++, &blacklisted)) { + if (!fnmatch(blacklisted, name, 0)) { + return true; + } + } + return false; +} + +struct psi_decl_arg *psi_decl_get_arg(struct psi_decl *decl, struct psi_decl_var *var) { + if (var->arg) { + size_t i = 0; + struct psi_decl_arg *arg = decl->func; + + do { + if (var->arg == arg) { + return arg; + } + } while (psi_plist_get(decl->args, i++, &arg)); + } + + return psi_decl_arg_get_by_var(var, decl->args, decl->func); +} +