validate: check anonymous decl
[m6w6/ext-psi] / src / types / decl.c
index 9c1b0c558b0ece04f22dc72c690824b2f56e332e..51dd733350b49073fbf4befc921426e1718c5633 100644 (file)
 
 #include "php_psi_stdinc.h"
 
-#if __GNUC__ >= 5
-# pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
-#endif
-#include "php_psi_macros.h"
-#include "php_psi_redirs.h"
-#if __GNUC__ >= 5
-# pragma GCC diagnostic pop
-#endif
-
 #include <dlfcn.h>
-
 #include "data.h"
 
-struct psi_decl *psi_decl_init(struct psi_decl_abi *abi,
-               struct psi_decl_arg *func, struct psi_plist *args)
+#define PSI_FUNC_REDIRS
+#include "php_psi_posix.h"
+
+struct psi_decl *psi_decl_init(struct psi_decl_arg *func, struct psi_plist *args)
 {
        struct psi_decl *d = calloc(1, sizeof(*d));
-       d->abi = abi;
+
        d->func = func;
        d->args = args;
+
        return d;
 }
 
@@ -92,6 +85,10 @@ static inline bool psi_decl_validate_func(struct psi_data *data,
 {
        struct psi_func_redir *redir;
 
+       if (!func->var->name) {
+               data->error(data, func->token, PSI_WARNING, "Anonymous decl!");
+               return false;
+       }
        if (!strcmp(func->var->name, "dlsym")) {
                data->error(data, func->token, PSI_WARNING,
                                "Cannot dlsym dlsym (sic!)");
@@ -107,7 +104,10 @@ static inline bool psi_decl_validate_func(struct psi_data *data,
 #ifndef RTLD_NEXT
 # define RTLD_NEXT ((void *) -1l)
 #endif
-               decl->sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
+#ifndef RTLD_DEFAULT
+# define RTLD_DEFAULT ((void *) 0)
+#endif
+               decl->sym = dlsym(dl ?: RTLD_DEFAULT, func->var->name);
                if (!decl->sym) {
                        data->error(data, func->token, PSI_WARNING,
                                        "Failed to locate symbol '%s': %s", func->var->name,
@@ -118,9 +118,10 @@ 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)
+bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl,
+               struct psi_validate_stack *type_stack)
 {
-       if (!psi_decl_validate_nodl(data, decl)) {
+       if (!psi_decl_validate_nodl(data, decl, type_stack)) {
                return false;
        }
        if (!psi_decl_validate_func(data, decl, decl->func, dl)) {
@@ -130,14 +131,17 @@ bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl)
        return true;
 }
 
-bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl)
+bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl,
+               struct psi_validate_stack *type_stack)
 {
-       if (!psi_decl_abi_validate(data, decl->abi)) {
+       if (!decl->abi) {
+               decl->abi = psi_decl_abi_init("default");
+       } 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);
                return false;
        }
-       if (!psi_decl_arg_validate(data, decl->func)) {
+       if (!psi_decl_arg_validate(data, decl->func, type_stack)) {
                return false;
        }
        if (decl->args) {
@@ -145,7 +149,12 @@ bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl)
                struct psi_decl_arg *arg;
 
                while (psi_plist_get(decl->args, i++, &arg)) {
-                       if (!psi_decl_arg_validate(data, 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);
+                       }
+                       if (!psi_decl_arg_validate(data, arg, type_stack)) {
                                return false;
                        }
                }