From 203caffc3e92072074fd7f2aa666aca801c2e069 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 4 Dec 2018 11:26:15 +0100 Subject: [PATCH] fix printf with threaded parser; replace SUCCESS/FAILURE with bool --- src/call.c | 23 ++++++++--------- src/call.h | 7 +++--- src/context.c | 68 ++++++++++++--------------------------------------- src/context.h | 3 ++- src/data.c | 2 +- src/parser.c | 44 ++++++++++++++++++--------------- 6 files changed, 59 insertions(+), 88 deletions(-) diff --git a/src/call.c b/src/call.c index a46fa4e..704675c 100644 --- a/src/call.c +++ b/src/call.c @@ -28,10 +28,13 @@ #else # include "php_config.h" #endif + #include "context.h" #include "data.h" #include "call.h" +#include "php_psi.h" + #include "php.h" #include "zend_exceptions.h" @@ -259,7 +262,7 @@ struct psi_context *psi_call_frame_get_context(struct psi_call_frame *frame) { #else # define PARAM_PROLOGUE(separate) Z_PARAM_PROLOGUE(1, separate) #endif -ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, +bool psi_call_frame_parse_args(struct psi_call_frame *frame, zend_execute_data *execute_data) { size_t i, argc = psi_plist_count(frame->impl->func->args); zend_error_handling zeh; @@ -271,7 +274,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, rv = zend_parse_parameters_none(); zend_restore_error_handling(&zeh); - return rv; + return rv == SUCCESS; } ZEND_PARSE_PARAMETERS_START( @@ -337,7 +340,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, } ZEND_PARSE_PARAMETERS_END_EX( zend_restore_error_handling(&zeh); - return FAILURE; + return false; ); /* set up defaults */ @@ -352,7 +355,7 @@ ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, } zend_restore_error_handling(&zeh); - return SUCCESS; + return true; } void psi_call_frame_enter(struct psi_call_frame *frame) { @@ -374,7 +377,7 @@ void psi_call_frame_enter(struct psi_call_frame *frame) { frame->rpointer = rv_sym->ptr = rv_sym->ival_ptr; } -ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame) { +bool psi_call_frame_do_let(struct psi_call_frame *frame) { size_t i; struct psi_let_stmt *let; struct psi_decl_arg *arg; @@ -420,10 +423,10 @@ ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame) { } } - return SUCCESS; + return true; } -ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind) { +bool psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind) { size_t i = 0; struct psi_assert_stmt *ass; @@ -431,12 +434,12 @@ ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi if (ass->kind == kind) { if (!psi_assert_stmt_exec(ass, frame)) { psi_assert_stmt_throw(ass); - return FAILURE; + return false; } } } - return SUCCESS; + return true; } void psi_call_frame_do_call(struct psi_call_frame *frame) { @@ -536,8 +539,6 @@ static void psi_call_frame_local_auto_dtor(void *auto_list) efree(auto_list); } -#include "php_psi.h" - void psi_call_frame_free(struct psi_call_frame *frame) { zend_hash_destroy(&frame->arguments); zend_hash_destroy(&frame->symbols); diff --git a/src/call.h b/src/call.h index a6ad873..eb8361a 100644 --- a/src/call.h +++ b/src/call.h @@ -26,6 +26,7 @@ #ifndef PSI_CALL_H #define PSI_CALL_H +#include #include "Zend/zend_types.h" #include "data.h" @@ -71,7 +72,7 @@ struct psi_call_frame { struct psi_call_frame *psi_call_frame_init(struct psi_context *context, struct psi_decl *decl, struct psi_impl *impl); -ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame, zend_execute_data *execute_data); +bool psi_call_frame_parse_args(struct psi_call_frame *frame, zend_execute_data *execute_data); size_t psi_call_frame_num_var_args(struct psi_call_frame *frame); size_t psi_call_frame_num_fixed_args(struct psi_call_frame *frame); @@ -92,8 +93,8 @@ struct psi_impl *psi_call_frame_get_impl(struct psi_call_frame *frame); void **psi_call_frame_get_arg_pointers(struct psi_call_frame *frame); void *psi_call_frame_get_rpointer(struct psi_call_frame *frame); -ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame); -ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind); +bool psi_call_frame_do_let(struct psi_call_frame *frame); +bool psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind); void psi_call_frame_do_call(struct psi_call_frame *frame); void psi_call_frame_do_callback(struct psi_call_frame *frame, struct psi_call_frame_callback *cb); void psi_call_frame_do_return(struct psi_call_frame *frame, zval *return_value); diff --git a/src/context.c b/src/context.c index f480ebd..308e11b 100644 --- a/src/context.c +++ b/src/context.c @@ -154,10 +154,12 @@ static bool psi_context_add(struct psi_context *C, struct psi_parser *P) } struct psi_context_build_worker { +#if PSI_THREADED_PARSER pthread_t tid; +#endif struct psi_parser parser; struct psi_parser_input *input; - char psi_file[MAXPATHLEN]; + char psi_file[PATH_MAX]; }; static struct psi_context_build_worker *psi_context_build_worker_init( @@ -165,7 +167,7 @@ static struct psi_context_build_worker *psi_context_build_worker_init( { struct psi_context_build_worker *w = pecalloc(1, sizeof(*w), 1); - if (MAXPATHLEN <= slprintf(w->psi_file, MAXPATHLEN, "%s/%s", dir, file)) { + if (PATH_MAX <= slprintf(w->psi_file, PATH_MAX, "%s/%s", dir, file)) { C->error(PSI_DATA(C), NULL, PSI_WARNING, "Path to PSI file too long: %s/%s", dir, file); pefree(w, 1); @@ -370,7 +372,9 @@ void psi_context_build(struct psi_context *C, const char *paths) } } } + psi_plist_free(running); } + psi_plist_free(workers); psi_context_compile(C); } @@ -712,46 +716,6 @@ void **psi_context_composite_type_elements(struct psi_context *C, return psi_plist_eles(*eles); } -/* -void psi_context_decl_func_array_elements(struct psi_context *C, - struct psi_decl *fn, struct psi_plist **els) -{ - void *type; - size_t i; - - if (fn->func->var->pointer_level > 1) { - type = C->ops->typeof_decl(C, PSI_T_POINTER); - } else { - type = psi_context_decl_type(C, fn->func->type); - } - - for (i = 0; i < fn->func->var->array_size; ++i) { - void *copy = C->ops->copyof_type(C, type); - *els = psi_plist_add(*els, ©); - } -} - -void *psi_context_decl_func_type(struct psi_context *C, struct psi_decl *fn) -{ - struct psi_decl_arg *darg = fn->func; - - if (darg->engine.type) { - return darg->engine.type; - } - - if (darg->var->pointer_level) { - if (!darg->var->array_size) { - return C->ops->typeof_decl(C, PSI_T_POINTER); - } else { - C->ops->composite_init(C, darg); - return darg->engine.type; - } - } - - return psi_context_decl_type(C, darg->type); -} -*/ - void psi_context_compile(struct psi_context *C) { psi_context_consts_init(C); @@ -767,32 +731,32 @@ void psi_context_compile(struct psi_context *C) EG(current_module) = NULL; } -ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl) +bool psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl) { struct psi_call_frame *frame; frame = psi_call_frame_init(C, impl->decl, impl); - if (SUCCESS != psi_call_frame_parse_args(frame, execute_data)) { + if (!psi_call_frame_parse_args(frame, execute_data)) { psi_call_frame_free(frame); - return FAILURE; + return false; } psi_call_frame_enter(frame); - if (SUCCESS != psi_call_frame_do_let(frame)) { + if (!psi_call_frame_do_let(frame)) { psi_call_frame_do_return(frame, return_value); psi_call_frame_free(frame); - return FAILURE; + return false; } - if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_PRE)) { + if (!psi_call_frame_do_assert(frame, PSI_ASSERT_PRE)) { psi_call_frame_do_return(frame, return_value); psi_call_frame_free(frame); - return FAILURE; + return false; } if (psi_call_frame_num_var_args(frame)) { @@ -801,11 +765,11 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec C->ops->call(frame); } - if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) { + if (!psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) { psi_call_frame_do_return(frame, return_value); psi_call_frame_free(frame); - return FAILURE; + return false; } psi_call_frame_do_return(frame, return_value); @@ -813,7 +777,7 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec psi_call_frame_do_free(frame); psi_call_frame_free(frame); - return SUCCESS; + return true; } diff --git a/src/context.h b/src/context.h index 62d2a12..e2ee35d 100644 --- a/src/context.h +++ b/src/context.h @@ -26,6 +26,7 @@ #ifndef PSI_CONTEXT_H #define PSI_CONTEXT_H +#include #include "token.h" /* zend_function_entry */ @@ -99,7 +100,7 @@ void **psi_context_composite_type_elements(struct psi_context *C, void *psi_context_decl_arg_call_type(struct psi_context *C, struct psi_decl_arg *arg); void *psi_context_decl_arg_full_type(struct psi_context *C, struct psi_decl_arg *arg); -ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl); +bool psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl); void psi_context_dump(struct psi_dump *dump, struct psi_context *C); void psi_context_dtor(struct psi_context *C); void psi_context_free(struct psi_context **C); diff --git a/src/data.c b/src/data.c index 6ce20a7..2898ff1 100644 --- a/src/data.c +++ b/src/data.c @@ -40,7 +40,7 @@ #if PSI_THREADED_PARSER # include -pthread_mutex_t psi_string_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t psi_string_mutex = PTHREAD_MUTEX_INITIALIZER; zend_string *psi_string_init_interned(const char *buf, size_t len, int p) { diff --git a/src/parser.c b/src/parser.c index 91a47d8..9781ed1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -146,41 +146,45 @@ bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens, return true; } +#if PSI_THREADED_PARSER +static void psi_smart_str_printf(smart_str *ss, const char *fmt, ...) +{ + va_list argv; + char *buf; + int len; + + va_start(argv, fmt); + len = vasprintf(&buf, fmt, argv); + va_end(argv); + + if (len != -1) { + smart_str_appendl_ex(ss, buf, len, 1); + free(buf); + } +} +#else +# define psi_smart_str_printf smart_str_append_printf +#endif + static inline zend_string *macro_to_constant(struct psi_parser *parser, zend_string *name, struct psi_validate_scope *scope) { smart_str str = {0}; - size_t i = 0; struct psi_token *tok; - -#if HAVE_ASPRINTF +#if PSI_THREADED_PARSER int persistent = 1; - - smart_str_appendl_ex(&str, ZEND_STRL("const psi\\"), 1); - smart_str_append_ex(&str, name, 1); - smart_str_appendl_ex(&str, ZEND_STRL(" = "), 1); #else int persistent = 0; - - smart_str_append_printf(&str, "const psi\\%s = ", name->val); #endif + + psi_smart_str_printf(&str, "const psi\\%s = ", name->val); if (scope->macro->exp) { -#if HAVE_ASPRINTF - char *astr = NULL; - struct psi_dump dump = {{.hn = &astr}, - .fun = (psi_dump_cb) asprintf}; -#else struct psi_dump dump = {{.hn = &str}, - .fun = (psi_dump_cb) smart_str_append_printf}; -#endif + .fun = (psi_dump_cb) psi_smart_str_printf}; psi_num_exp_dump(&dump, scope->macro->exp); -#if HAVE_ASPRINTF - smart_str_appends_ex(&str, astr, 1); - free(astr); -#endif } else while (psi_plist_get(scope->macro->tokens, i++, &tok)) { if (tok->type == PSI_T_QUOTED_STRING) { smart_str_appendc_ex(&str, '"', persistent); -- 2.30.2