flush
[m6w6/ext-psi] / src / context.c
index 3ff510f2f8aa47a6002dcedafb9ce9b45890fb78..727388bcca095f9e521903ac5810bba2eddbd6bb 100644 (file)
-#include <sys/param.h>
-#include <dlfcn.h>
-#include <dirent.h>
-#include <fnmatch.h>
-#include <errno.h>
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#include "php.h"
+
+#include <stdio.h>
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif
+#ifdef HAVE_STRING_H
+# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
+#  include <memory.h>
+# endif
+# include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+# include <errno.h>
+#endif
+#ifdef HAVE_GLOB_H
+# include <glob.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+#ifdef HAVE_ARPA_NAMESER_H
+# include <arpa/nameser.h>
+#endif
+#ifdef HAVE_NETDB_H
+# include <netdb.h>
+#endif
+#ifdef HAVE_RESOLV_H
+# include <resolv.h>
+#endif
+#ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_TIMES_H
+# include <sys/times.h>
+#endif
+#ifdef HAVE_SYS_UIO_H
+# include <sys/uio.h>
+#endif
+#ifdef HAVE_SYS_UTSNAME_H
+# include <sys/utsname.h>
+#endif
+#ifdef HAVE_TIME_H
+# include <time.h>
+#endif
+#ifdef HAVE_WCHAR_H
+# include <wchar.h>
+#endif
+
+#ifdef HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen ((dirent)->d_name)
+#else
+# define dirent direct
+# define NAMLEN(dirent) ((dirent)->d_namlen)
+# ifdef HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+# endif
+# ifdef HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+# endif
+# ifdef HAVE_NDIR_H
+#  include <ndir.h>
+# endif
+#endif
+
+#include <sys/param.h>
+#include <dlfcn.h>
+#include <fnmatch.h>
+
 #include "php.h"
 #include "php_scandir.h"
 #include "php_psi.h"
 #include "context.h"
 #include "parser.h"
 
+#include "libjit.h"
+#include "libffi.h"
+
 #define psi_predef_count(of) ((sizeof(psi_predef ##of## s)/sizeof(psi_predef ##of))-1)
 typedef struct psi_predef_type {
        token_t type_tag;
@@ -57,6 +154,17 @@ static const psi_predef_struct psi_predef_structs[] = {
 };
 #define psi_predef_struct_count() psi_predef_count(_struct)
 
+PHP_PSI_MACROS
+
+typedef struct psi_predef_func {
+       const char *name;
+       void (*func)(void);
+} psi_predef_func;
+static psi_predef_func psi_predef_funcs[] = {
+       PHP_PSI_FUNCS{0}
+};
+#define psi_predef_func_count() psi_predef_count(_func)
+
 static int validate_lib(PSI_Data *data, void **dlopened) {
        char lib[MAXPATHLEN];
        const char *ptr = data->psi.file.ln;
@@ -66,11 +174,7 @@ static int validate_lib(PSI_Data *data, void **dlopened) {
                /* FIXME: assume stdlib */
                return 1;
        } else if (!strchr(ptr, '/')) {
-#ifdef DARWIN
-               len = snprintf(lib, MAXPATHLEN, "lib%s.dylib", ptr);
-#else
-               len = snprintf(lib, MAXPATHLEN, "lib%s.so", ptr);
-#endif
+               len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX);
                if (MAXPATHLEN == len) {
                        data->error(PSI_WARNING, "Library name too long: '%s'", ptr);
                }
@@ -117,7 +221,7 @@ static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type
 static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        switch (type->type) {
        case PSI_T_NAME:
-               if (!data->defs |!locate_decl_type_alias(data->defs, type)) {
+               if (!data->defs |!locate_decl_type_alias(data->defs, type)) {
                        return 0;
                }
                return validate_decl_type(data, type->real);
@@ -146,8 +250,8 @@ static inline int validate_constant(PSI_Data *data, constant *c) {
 
 static inline int validate_decl_arg(PSI_Data *data, decl_arg *arg) {
        if (!validate_decl_type(data, arg->type)) {
-               data->error(PSI_WARNING, "Cannot use '%s' as type for '%s'",
-                       arg->type->name, arg->var->name);
+               data->error(PSI_WARNING, "Cannot use '%s'(%d) as type for '%s'",
+                       arg->type->name, arg->type->type, arg->var->name);
                return 0;
        }
        return 1;
@@ -217,6 +321,8 @@ static inline int validate_decl_abi(PSI_Data *data, decl_abi *abi) {
 }
 static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_arg *func)
 {
+       size_t i;
+
        if (!strcmp(func->var->name, "dlsym")) {
                data->error(PSI_WARNING, "Cannot dlsym dlsym (sic!)");
                return 0;
@@ -225,13 +331,23 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
        if (!validate_decl_arg(data, func)) {
                return 0;
        }
+       for (i = 0; i < psi_predef_func_count(); ++i) {
+               psi_predef_func *pre = &psi_predef_funcs[i];
+
+               if (!strcmp(func->var->name, pre->name)) {
+                       decl->call.sym = pre->func;
+                       break;
+               }
+       }
+       if (!decl->call.sym) {
 #ifndef RTLD_NEXT
 # define RTLD_NEXT ((void *) -1l)
 #endif
-       decl->dlptr = dlsym(dl ?: RTLD_NEXT, func->var->name);
-       if (!decl->dlptr) {
-               data->error(PSI_WARNING, "Failed to locate symbol '%s': %s",
-                       func->var->name, dlerror());
+               decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
+               if (!decl->call.sym) {
+                       data->error(PSI_WARNING, "Failed to locate symbol '%s': %s",
+                               func->var->name, dlerror());
+               }
        }
        return 1;
 }
@@ -254,23 +370,30 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
        }
        return 1;
 }
-
-static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
+static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) {
        size_t i;
 
-       ZEND_ASSERT(s);
-       for (i = 0; i < s->args->count; ++i) {
-               decl_arg *darg = s->args->args[i];
+       for (i = 0; i < args->count; ++i) {
+               decl_arg *arg = args->args[i];
 
-               if (!strcmp(var->name, darg->var->name)) {
-                       return darg;
+               if (!strcmp(var->name, arg->var->name)) {
+                       return var->arg = arg;
                }
        }
 
        return NULL;
 }
-static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *ref) {
+static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
+       if (s->args) {
+               return locate_decl_var_arg(var, s->args);
+       }
+
+       return NULL;
+}
+static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *ref, decl_args *ref_list) {
        size_t i;
+       decl_type *ref_type = real_decl_type(ref->type);
+       decl_var *set_var = set->vars->vars[0];
 
        switch (set->func->type) {
        case PSI_T_TO_BOOL:
@@ -291,36 +414,65 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r
        EMPTY_SWITCH_DEFAULT_CASE();
        }
 
-       if (strcmp(set->vars->vars[0]->name, ref->var->name)) {
-               return 0;
+       for (i = 1; i < set->vars->count; ++i) {
+               if (!locate_decl_var_arg(set->vars->vars[i], ref_list)) {
+                       return 0;
+               }
        }
+       set_var->arg = ref;
 
-       if (set->count && (set->func->type != PSI_T_TO_ARRAY || real_decl_type(ref->type)->type != PSI_T_STRUCT)) {
-               data->error(E_WARNING, "Inner `set` statement casts only work with to_array() casts on structs");
+       if (set->count && (set->func->type != PSI_T_TO_ARRAY || (ref_type->type != PSI_T_STRUCT && !ref->var->arg->var->pointer_level))) {
+               data->error(E_WARNING, "Inner `set` statement casts only work with to_array() casts on structs or pointers");
                return 0;
        }
-       for (i = 0; i < set->count; ++i) {
-               decl_arg *sub_ref = locate_struct_member(real_decl_type(ref->type)->strct, set->inner[i]->vars->vars[0]);
 
-               if (!sub_ref) {
-                       return 0;
+       if (ref_type->type == PSI_T_STRUCT) {
+               /* to_array(struct, to_...) */
+               for (i = 0; i < set->count; ++i) {
+                       decl_var *sub_var = set->inner[i]->vars->vars[0];
+                       decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var);
+
+                       if (sub_ref) {
+                               if (!validate_set_value(data, set->inner[i], sub_ref, ref_type->strct->args)) {
+                                       return 0;
+                               }
+                               set->inner[i]->outer.set = set;
+                       }
                }
-               if (!validate_set_value(data, set->inner[i], sub_ref)) {
-                       return 0;
+       } else if (set->count == 1) {
+               /* to_array(ptr, to_string(*ptr)) */
+               decl_var *sub_var = set->inner[0]->vars->vars[0];
+               decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list);
+
+               if (sub_ref) {
+                       if (strcmp(sub_var->name, set_var->name)) {
+                               data->error(E_WARNING, "Inner `set` statement casts on pointers must reference the same variable");
+                               return 0;
+                       }
+                       if (!validate_set_value(data, set->inner[0], sub_ref, ref_list)) {
+                               return 0;
+                       }
+                       set->inner[0]->outer.set = set;
                }
+       } else if (set->count > 1) {
+               data->error(E_WARNING, "Inner `set` statement casts on pointers may only occur once");
+               return 0;
        }
 
        return 1;
 }
 static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) {
-       size_t i;
+       if (decls) {
+               size_t i;
 
-       for (i = 0; i < decls->count; ++i) {
-               if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) {
-                       ret->decl = decls->list[i]->func;
-                       return decls->list[i];
+               for (i = 0; i < decls->count; ++i) {
+                       if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) {
+                               ret->decl = decls->list[i]->func;
+                               return decls->list[i];
+                       }
                }
        }
+
        return NULL;
 }
 static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
@@ -347,10 +499,13 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
                                impl->func->name);
                return 0;
        }
-       if (!validate_set_value(data, ret->set, ret->decl)) {
+
+       if (!validate_set_value(data, ret->set, ret->decl, impl->decl->args)) {
                return 0;
        }
 
+       impl->decl->impl = impl;
+
        return 1;
 }
 static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
@@ -443,7 +598,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
 
                                if (!strcmp(set_var->name, set_arg->var->name)) {
                                        check = 1;
-                                       if (!validate_set_value(data, set->val, set_arg)) {
+                                       if (!validate_set_value(data, set->val, set_arg, impl->decl->args)) {
                                                return 0;
                                        }
                                        set_var->arg = set_arg;
@@ -461,34 +616,62 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
        }
        return 1;
 }
+static inline decl *locate_free_decl(decls *decls, free_call *f) {
+       if (decls)  {
+               size_t i;
+
+               for (i = 0; i < decls->count; ++i) {
+                       if (!strcmp(decls->list[i]->func->var->name, f->func)) {
+                               f->decl = decls->list[i];
+                               return decls->list[i];
+                       }
+               }
+       }
+
+       return NULL;
+}
 static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) {
-       size_t i, j, k;
+       size_t i, j, k, l;
        /* we can have any count of free stmts; freeing any out vars */
        for (i = 0; i < impl->stmts->fre.count; ++i) {
                free_stmt *fre = impl->stmts->fre.list[i];
 
-               for (j = 0; j < fre->vars->count; ++j) {
-                       decl_var *free_var = fre->vars->vars[j];
-                       int check = 0;
+               for (j = 0; j < fre->calls->count; ++j) {
+                       free_call *free_call = fre->calls->list[j];
 
-                       if (!strcmp(free_var->name, impl->decl->func->var->name)) {
-                               continue;
+                       /* first find the decl of the free func */
+                       if (!locate_free_decl(data->decls, free_call)) {
+                               data->error(PSI_WARNING, "Unknown function '%s' in `free` statement"
+                                               " of implementation '%s'", free_call->func, impl->func->name);
+                               return 0;
+                       }
+                       if (!impl->decl->args) {
+                               data->error(PSI_WARNING, "Declaration '%s' of implementation '%s'"
+                                               " does not have any arguments to free",
+                                               impl->decl->func->var->name, impl->func->name);
                        }
-                       if (impl->decl->args) for (k = 0; k < impl->decl->args->count; ++k) {
-                               decl_arg *free_arg = impl->decl->args->args[k];
 
-                               if (!strcmp(free_var->name, free_arg->var->name)) {
-                                       check = 1;
-                                       free_var->arg = free_arg;
-                                       break;
+                       /* now check for known vars */
+                       for (l = 0; l < free_call->vars->count; ++l) {
+                               int check = 0;
+                               decl_var *free_var = free_call->vars->vars[l];
+
+                               for (k = 0; k < impl->decl->args->count; ++k) {
+                                       decl_arg *free_arg = impl->decl->args->args[k];
+
+                                       if (!strcmp(free_var->name, free_arg->var->name)) {
+                                               check = 1;
+                                               free_var->arg = free_arg;
+                                               break;
+                                       }
                                }
-                       }
 
-                       if (!check) {
-                               data->error(PSI_WARNING, "Unknown variable '%s' of `free` statement"
-                                               " of implementation '%s'",
-                                               free_var->name, impl->func->name);
-                               return 0;
+                               if (!check) {
+                                       data->error(PSI_WARNING, "Unknown variable '%s' of `free` statement"
+                                                       " of implementation '%s'",
+                                                       free_var->name, impl->func->name);
+                                       return 0;
+                               }
                        }
                }
        }
@@ -530,7 +713,13 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
 
        C->error = error;
        C->ops = ops;
-       ops->init(C);
+
+       if (ops->init) {
+               ops->init(C);
+       }
+
+       ZEND_ASSERT(ops->call != NULL);
+       ZEND_ASSERT(ops->compile != NULL);
 
        /* build up predefs in a temporary PSI_Data for validation */
        memset(&T, 0, sizeof(T));
@@ -682,47 +871,62 @@ static int psi_select_dirent(const struct dirent *entry)
        return 0 == fnmatch("*.psi", entry->d_name, FNM_CASEFOLD);
 }
 
-void PSI_ContextBuild(PSI_Context *C, const char *path)
+void PSI_ContextBuild(PSI_Context *C, const char *paths)
 {
-       int i, n;
+       int i, n, flags = getenv("PSI_DEBUG") ? PSI_PARSER_DEBUG : 0;
+       char *sep = NULL, *cpy = strdup(paths), *ptr = cpy;
        struct dirent **entries = NULL;
 
-       n = php_scandir(path, &entries, psi_select_dirent, alphasort);
 
-       if (n < 0) {
-               return;
-       } else for (i = 0; i < n; ++i) {
-               char psi[MAXPATHLEN];
-               PSI_Parser P;
+       do {
+               sep = strchr(ptr, ':');
 
-               if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", path, entries[i]->d_name)) {
-                       C->error(PSI_WARNING, "Path to PSI file too long: %s/%s",
-                               path, entries[i]->d_name);
+               if (sep) {
+                       *sep = 0;
                }
-               if (!PSI_ParserInit(&P, psi, C->error, 0)) {
-                       C->error(PSI_WARNING, "Failed to init PSI parser (%s): %s",
-                               psi, strerror(errno));
-                       continue;
+
+               n = php_scandir(ptr, &entries, psi_select_dirent, alphasort);
+
+               if (n > 0) {
+                       for (i = 0; i < n; ++i) {
+                               char psi[MAXPATHLEN];
+                               PSI_Parser P;
+
+                               if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) {
+                                       C->error(PSI_WARNING, "Path to PSI file too long: %s/%s",
+                                               ptr, entries[i]->d_name);
+                               }
+                               if (!PSI_ParserInit(&P, psi, C->error, flags)) {
+                                       C->error(PSI_WARNING, "Failed to init PSI parser (%s): %s",
+                                               psi, strerror(errno));
+                                       continue;
+                               }
+
+                               while (-1 != PSI_ParserScan(&P)) {
+                                       PSI_ParserParse(&P, PSI_TokenAlloc(&P));
+                               };
+                               PSI_ParserParse(&P, NULL);
+                               PSI_ContextValidate(C, &P);
+                               PSI_ParserDtor(&P);
+                       }
                }
 
-               while (-1 != PSI_ParserScan(&P)) {
-                       PSI_ParserParse(&P, PSI_TokenAlloc(&P));
-               };
-               PSI_ParserParse(&P, NULL);
-               PSI_ContextValidate(C, &P);
-               PSI_ParserDtor(&P);
-       }
+               if (entries) {
+                       for (i = 0; i < n; ++i) {
+                               free(entries[i]);
+                       }
+                       free(entries);
+               }
+
+               ptr = sep + 1;
+       } while (sep);
+
 
        if (PSI_ContextCompile(C) && SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
                C->error(PSI_WARNING, "Failed to register functions!");
        }
 
-       if (entries) {
-               for (i = 0; i < n; ++i) {
-                       free(entries[i]);
-               }
-               free(entries);
-       }
+       free(cpy);
 
 }
 
@@ -757,24 +961,101 @@ zend_function_entry *PSI_ContextCompile(PSI_Context *C)
                }
        }
 
-
        return C->closures = C->ops->compile(C);
 }
 
+
+void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl)
+{
+       C->ops->call(C, ret_val, decl);
+}
+
+static inline void dump_decl_type(int fd, decl_type *t) {
+       const char *pre;
+
+       switch (t->type) {
+       case PSI_T_STRUCT:
+               pre = "struct ";
+               break;
+       default:
+               pre = "";
+       }
+       dprintf(fd, "%s%s", pre, t->name);
+}
+static inline void dump_decl_var(int fd, decl_var *v) {
+       dprintf(fd, "%.*s%s", v->pointer_level-!!v->array_size, "**********", v->name);
+       if (v->array_size) {
+               dprintf(fd, "[%u]", v->array_size);
+       }
+}
+static inline void dump_decl_arg(int fd, decl_arg *a) {
+       dump_decl_type(fd, a->type);
+       dprintf(fd, " ");
+       dump_decl_var(fd, a->var);
+}
+void PSI_ContextDump(PSI_Context *C, int fd)
+{
+       size_t i, j;
+
+#ifdef HAVE_LIBJIT
+       if (C->ops == PSI_Libjit()) {
+               dprintf(fd, "#PSI(libjit)\n");
+       }
+#endif
+#ifdef HAVE_LIBFFI
+       if (C->ops == PSI_Libffi()) {
+               dprintf(fd, "#PSI(libffi)\n");
+       }
+#endif
+
+       if (C->defs) for (i = 0; i < C->defs->count; ++i) {
+               decl_typedef *tdef = C->defs->list[i];
+
+               dprintf(fd, "typedef ");
+               dump_decl_type(fd, tdef->type);
+               dprintf(fd, " %s;\n", tdef->alias);
+       }
+       if (C->structs) for (i = 0; i < C->structs->count; ++i) {
+               decl_struct *strct = C->structs->list[i];
+               decl_arg *sarg = NULL;
+
+               dprintf(fd, "struct %s::(%zu) {\n", strct->name, strct->size);
+               for (j = 0; j < strct->args->count; ++j) {
+                       sarg = strct->args->args[j];
+                       dprintf(fd, "\t");
+                       dump_decl_arg(fd, sarg);
+                       dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len);
+               }
+               dprintf(fd, "}\n");
+       }
+
+       dprintf(fd, "\n");
+}
+
 void PSI_ContextDtor(PSI_Context *C)
 {
        size_t i;
+       zend_function_entry *zfe;
 
-       C->ops->dtor(C);
+       if (C->ops->dtor) {
+               C->ops->dtor(C);
+       }
 
        free_decl_libs(&C->psi.libs);
 
-       for (i = 0; i < C->count; ++i) {
-               PSI_DataDtor(&C->data[i]);
+       if (C->data) {
+               for (i = 0; i < C->count; ++i) {
+                       PSI_DataDtor(&C->data[i]);
+               }
+               free(C->data);
        }
 
-       free(C->data);
-       free(C->closures);
+       if (C->closures) {
+               for (zfe = C->closures; zfe->fname; ++zfe) {
+                       free((void *) zfe->arg_info);
+               }
+               free(C->closures);
+       }
 
        if (C->consts) {
                if (C->consts->list) {