lift single lib statement restriction
[m6w6/ext-psi] / src / types / decl.c
index eda230398533c9d251cdcadf0aebfc9698bb9db6..300f5bdd36b6e1a782285e54423dbfbb3e2064bf 100644 (file)
@@ -5,11 +5,11 @@
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
 
    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*******************************************************************************/
+ *******************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#else
-# include "php_config.h"
-#endif
+#include "php_psi_stdinc.h"
 
-#include "php_psi_macros.h"
-#include "php_psi_redirs.h"
+#include "php_psi.h"
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <sys/param.h>
 #include <dlfcn.h>
+#include <fnmatch.h>
 
 #include "data.h"
 
-decl *init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
-       decl *d = calloc(1, sizeof(*d));
-       d->abi = abi;
+#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->func = func;
        d->args = args;
+
        return d;
 }
 
-void free_decl(decl *d) {
-       free_decl_abi(d->abi);
-       free_decl_arg(d->func);
-       if (d->args) {
-               free_decl_args(d->args);
+void psi_decl_free(struct psi_decl **d_ptr)
+{
+       if (*d_ptr) {
+               struct psi_decl *d = *d_ptr;
+
+               *d_ptr = NULL;
+
+               psi_decl_abi_free(&d->abi);
+               psi_decl_arg_free(&d->func);
+               if (d->args) {
+                       psi_plist_free(d->args);
+               }
+               if (d->redir) {
+                       free(d->redir);
+               }
+               free(d);
        }
-       free(d);
 }
 
-void dump_decl(int fd, decl *decl) {
-       dump_decl_abi(fd, decl->abi);
+void psi_decl_dump(int fd, struct psi_decl *decl)
+{
+       psi_decl_abi_dump(fd, decl->abi);
        dprintf(fd, " ");
-       dump_decl_arg(fd, decl->func, 0);
+       /* FIXME: functions returning arrays */
+       psi_decl_arg_dump(fd, decl->func, 0);
        dprintf(fd, "(");
        if (decl->args) {
-               dump_decl_args(fd, decl->args, 0);
+               size_t i;
+               struct psi_decl_arg *arg;
+
+               for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+                       if (i) {
+                               dprintf(fd, ", ");
+                       }
+                       psi_decl_arg_dump(fd, arg, 0);
+               }
+               if (decl->varargs) {
+                       dprintf(fd, ", ...");
+               }
+       }
+       if (decl->redir) {
+               dprintf(fd, ") __asm__ (\"%s\");", decl->redir);
+       } else {
+               dprintf(fd, ");");
        }
-       dprintf(fd, ");");
 }
 
-static inline int validate_decl_func(struct psi_data *data, void *dl, decl *decl, decl_arg *func)
+static inline bool psi_decl_validate_func(struct psi_data *data,
+               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!)");
-               return 0;
+       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)) {
-                       decl->call.sym = redir->func;
+                       decl->sym = redir->func;
                }
        }
-       if (!decl->call.sym) {
+       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 ?: func->var->name);
+               }
+       }
+       if (!decl->sym) {
 #ifndef RTLD_NEXT
 # define RTLD_NEXT ((void *) -1l)
 #endif
-               decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
-               if (!decl->call.sym) {
+#ifndef RTLD_DEFAULT
+# define RTLD_DEFAULT ((void *) 0)
+#endif
+               decl->sym = dlsym(RTLD_DEFAULT, decl->redir ?: func->var->name);
+               if (!decl->sym) {
                        data->error(data, func->token, PSI_WARNING,
-                               "Failed to locate symbol '%s': %s",
-                               func->var->name, dlerror() ?: "not found");
+                                       "Failed to locate symbol '%s(%s)': %s",
+                                       func->var->name, decl->redir ?: "",
+                                       dlerror() ?: "not found");
+                       return false;
                }
        }
-       return 1;
+       return true;
 }
 
-int validate_decl(struct psi_data *data, void *dl, decl *decl) {
-       if (!validate_decl_nodl(data, decl)) {
-               return 0;
+bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl,
+               struct psi_validate_scope *scope)
+{
+       if (!psi_decl_validate_nodl(data, decl, scope)) {
+               return false;
        }
-       if (!validate_decl_func(data, dl, decl, decl->func)) {
-               return 0;
+       if (!psi_decl_validate_func(data, decl, decl->func)) {
+               return false;
        }
-       return 1;
+
+       return true;
 }
 
-int validate_decl_nodl(struct psi_data *data, decl *decl) {
-       if (!validate_decl_abi(data, decl->abi)) {
+bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl,
+               struct psi_validate_scope *scope)
+{
+       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 0;
+               return false;
        }
-       if (!validate_decl_arg(data, decl->func)) {
-               return 0;
+       if (!psi_decl_arg_validate(data, decl->func, scope)) {
+               return false;
        }
        if (decl->args) {
-               size_t i;
-
-               for (i = 0; i < decl->args->count; ++i) {
-                       if (!validate_decl_arg(data, decl->args->args[i])) {
-                               return 0;
+               size_t i = 0;
+               struct psi_decl_arg *arg;
+
+               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);
                        }
+                       if (!psi_decl_arg_validate(data, arg, scope)) {
+                               return false;
+                       }
+               }
+       }
+
+       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 1;
+       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);
 }
+