flush
[m6w6/ext-psi] / src / context.c
index d7eabb56e19e535be2265d106c118defe4bd599f..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"
@@ -127,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);
@@ -156,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;
@@ -227,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;
@@ -235,21 +331,19 @@ 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->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
-       if (!decl->call.sym) {
-               size_t i;
-
-               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;
-                       }
-               }
+               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());
@@ -276,23 +370,27 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
        }
        return 1;
 }
+static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) {
+       size_t i;
 
-static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
-       if (s->args) {
-               size_t i;
-
-               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 var->arg = 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];
@@ -316,10 +414,11 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r
        EMPTY_SWITCH_DEFAULT_CASE();
        }
 
-       if (strcmp(set_var->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;
+               }
        }
-       ZEND_ASSERT(!set_var->arg || set_var->arg == ref);
        set_var->arg = ref;
 
        if (set->count && (set->func->type != PSI_T_TO_ARRAY || (ref_type->type != PSI_T_STRUCT && !ref->var->arg->var->pointer_level))) {
@@ -328,24 +427,32 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r
        }
 
        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)) {
+                               if (!validate_set_value(data, set->inner[i], sub_ref, ref_type->strct->args)) {
                                        return 0;
                                }
+                               set->inner[i]->outer.set = set;
                        }
                }
        } 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 = sub_var->arg;
+               decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list);
 
                if (sub_ref) {
-                       if (!validate_set_value(data, set->inner[0], 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");
@@ -393,7 +500,7 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
                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;
        }
 
@@ -491,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;