Accept zend_string instead of zval in zend_compile_string

This commit is contained in:
Nikita Popov 2020-09-07 11:42:21 +02:00
parent 85b5dc4711
commit f5dbebd82e
9 changed files with 27 additions and 44 deletions

View file

@ -79,7 +79,7 @@ static inline uint32_t zend_alloc_cache_slot(void) {
} }
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type); ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename); ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
#ifndef ZTS #ifndef ZTS
ZEND_API zend_compiler_globals compiler_globals; ZEND_API zend_compiler_globals compiler_globals;

View file

@ -732,7 +732,7 @@ void zend_file_context_begin(zend_file_context *prev_context);
void zend_file_context_end(zend_file_context *prev_context); void zend_file_context_end(zend_file_context *prev_context);
extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type); extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
extern ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename); extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem); ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
void startup_scanner(void); void startup_scanner(void);
@ -792,7 +792,7 @@ ZEND_API void function_add_ref(zend_function *function);
struct _zend_arena; struct _zend_arena;
ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type); ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
ZEND_API zend_op_array *compile_string(zval *source_string, const char *filename); ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename);
ZEND_API zend_op_array *compile_filename(int type, zval *filename); ZEND_API zend_op_array *compile_filename(int type, zval *filename);
ZEND_API zend_ast *zend_compile_string_to_ast( ZEND_API zend_ast *zend_compile_string_to_ast(
zend_string *code, struct _zend_arena **ast_arena, const char *filename); zend_string *code, struct _zend_arena **ast_arena, const char *filename);

View file

@ -4217,7 +4217,9 @@ already_compiled:
break; break;
case ZEND_EVAL: { case ZEND_EVAL: {
char *eval_desc = zend_make_compiled_string_description("eval()'d code"); char *eval_desc = zend_make_compiled_string_description("eval()'d code");
new_op_array = zend_compile_string(inc_filename, eval_desc); zend_string *code = zval_get_string(inc_filename);
new_op_array = zend_compile_string(code, eval_desc);
zend_string_release(code);
efree(eval_desc); efree(eval_desc);
} }
break; break;

View file

@ -1143,26 +1143,23 @@ ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex) /* {{{ */
ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *retval_ptr, const char *string_name) /* {{{ */ ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *retval_ptr, const char *string_name) /* {{{ */
{ {
zval pv;
zend_op_array *new_op_array; zend_op_array *new_op_array;
uint32_t original_compiler_options; uint32_t original_compiler_options;
zend_result retval; zend_result retval;
zend_string *code_str;
if (retval_ptr) { if (retval_ptr) {
ZVAL_NEW_STR(&pv, zend_string_alloc(str_len + sizeof("return ;")-1, 0)); code_str = zend_string_concat3(
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1); "return ", sizeof("return ")-1, str, str_len, ";", sizeof(";")-1);
memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
} else { } else {
ZVAL_STRINGL(&pv, str, str_len); code_str = zend_string_init(str, str_len, 0);
} }
/*printf("Evaluating '%s'\n", pv.value.str.val);*/ /*printf("Evaluating '%s'\n", pv.value.str.val);*/
original_compiler_options = CG(compiler_options); original_compiler_options = CG(compiler_options);
CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL; CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
new_op_array = zend_compile_string(&pv, string_name); new_op_array = zend_compile_string(code_str, string_name);
CG(compiler_options) = original_compiler_options; CG(compiler_options) = original_compiler_options;
if (new_op_array) { if (new_op_array) {
@ -1200,7 +1197,7 @@ ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *re
} else { } else {
retval = FAILURE; retval = FAILURE;
} }
zval_ptr_dtor_str(&pv); zend_string_release(code_str);
return retval; return retval;
} }
/* }}} */ /* }}} */

View file

@ -804,23 +804,18 @@ ZEND_API size_t zend_get_scanned_file_offset(void)
return offset; return offset;
} }
zend_op_array *compile_string(zval *source_string, const char *filename) zend_op_array *compile_string(zend_string *source_string, const char *filename)
{ {
zend_lex_state original_lex_state; zend_lex_state original_lex_state;
zend_op_array *op_array = NULL; zend_op_array *op_array = NULL;
zval tmp; zval tmp;
if (UNEXPECTED(Z_TYPE_P(source_string) != IS_STRING)) { if (ZSTR_LEN(source_string) == 0) {
ZVAL_STR(&tmp, zval_get_string_func(source_string));
} else {
ZVAL_COPY(&tmp, source_string);
}
if (Z_STRLEN(tmp)==0) {
zval_ptr_dtor(&tmp);
return NULL; return NULL;
} }
ZVAL_STR_COPY(&tmp, source_string);
zend_save_lexical_state(&original_lex_state); zend_save_lexical_state(&original_lex_state);
zend_prepare_string_for_scanning(&tmp, filename); zend_prepare_string_for_scanning(&tmp, filename);
BEGIN(ST_IN_SCRIPTING); BEGIN(ST_IN_SCRIPTING);

View file

@ -275,7 +275,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type); zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type);
zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type); zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type);
zend_op_array *(*compile_string)(zval *source_string, const char *filename); zend_op_array *(*compile_string)(zend_string *source_string, const char *filename);
HashTable file_sources; HashTable file_sources;
FILE *oplog; /* opline log */ FILE *oplog; /* opline log */

View file

@ -828,7 +828,7 @@ static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, co
{ {
phpdbg_breakcond_t new_break; phpdbg_breakcond_t new_break;
uint32_t cops = CG(compiler_options); uint32_t cops = CG(compiler_options);
zval pv; zend_string *bp_code;
switch (param->type) { switch (param->type) {
case STR_PARAM: case STR_PARAM:
@ -877,16 +877,10 @@ static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, co
new_break.code = estrndup(expr, expr_len); new_break.code = estrndup(expr, expr_len);
new_break.code_len = expr_len; new_break.code_len = expr_len;
Z_STR(pv) = zend_string_alloc(expr_len + sizeof("return ;") - 1, 0); bp_code = zend_string_concat3(
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1); "return ", sizeof("return ")-1, expr, expr_len, ";", sizeof(";")-1);
memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, expr, expr_len); new_break.ops = zend_compile_string(bp_code, "Conditional Breakpoint Code");
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';'; zend_string_release(bp_code);
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
Z_TYPE_INFO(pv) = IS_STRING;
new_break.ops = zend_compile_string(&pv, "Conditional Breakpoint Code");
zval_ptr_dtor_str(&pv);
if (new_break.ops) { if (new_break.ops) {
brake = zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash, &new_break, sizeof(phpdbg_breakcond_t)); brake = zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash, &new_break, sizeof(phpdbg_breakcond_t));

View file

@ -316,7 +316,7 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
return op_array; return op_array;
} }
zend_op_array *phpdbg_compile_string(zval *source_string, const char *filename) { zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *filename) {
zend_string *fake_name; zend_string *fake_name;
zend_op_array *op_array; zend_op_array *op_array;
phpdbg_file_source *dataptr; phpdbg_file_source *dataptr;
@ -327,9 +327,9 @@ zend_op_array *phpdbg_compile_string(zval *source_string, const char *filename)
return PHPDBG_G(compile_string)(source_string, filename); return PHPDBG_G(compile_string)(source_string, filename);
} }
dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * Z_STRLEN_P(source_string)); dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * ZSTR_LEN(source_string));
dataptr->buf = estrndup(Z_STRVAL_P(source_string), Z_STRLEN_P(source_string)); dataptr->buf = estrndup(ZSTR_VAL(source_string), ZSTR_LEN(source_string));
dataptr->len = Z_STRLEN_P(source_string); dataptr->len = ZSTR_LEN(source_string);
dataptr->line[0] = 0; dataptr->line[0] = 0;
for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) { for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
if (*bufptr == '\n') { if (*bufptr == '\n') {

View file

@ -519,12 +519,7 @@ exec_code:
} /* }}} */ } /* }}} */
int phpdbg_compile_stdin(zend_string *code) { int phpdbg_compile_stdin(zend_string *code) {
zval zv; PHPDBG_G(ops) = zend_compile_string(code, "Standard input code");
ZVAL_STR(&zv, code);
PHPDBG_G(ops) = zend_compile_string(&zv, "Standard input code");
zend_string_release(code); zend_string_release(code);
if (EG(exception)) { if (EG(exception)) {