Skip to content

Commit fd1eacc

Browse files
authored
Add assertions verifying that zend_ast_decl AST nodes are not treated as regular zend_ast nodes (#17390)
* zend_compile: Do not traverse children of ZEND_AST_CLOSURE in zend_compile_const_expr() * Add assertions verifying that zend_ast_decl AST nodes are not treated as regular zend_ast nodes
1 parent a091e52 commit fd1eacc

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

Zend/zend_ast.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,9 @@ static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
10881088
size += zend_ast_tree_size(list->child[i]);
10891089
}
10901090
}
1091+
} else if (zend_ast_is_decl(ast)) {
1092+
/* Not implemented. */
1093+
ZEND_UNREACHABLE();
10911094
} else {
10921095
uint32_t i, children = zend_ast_get_num_children(ast);
10931096

@@ -1141,6 +1144,9 @@ static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
11411144
ZVAL_COPY(&new->val, &((zend_ast_zval *) ast)->val);
11421145
Z_LINENO(new->val) = zend_ast_get_lineno(ast);
11431146
buf = (void*)((char*)buf + sizeof(zend_ast_zval));
1147+
} else if (zend_ast_is_decl(ast)) {
1148+
/* Not implemented. */
1149+
ZEND_UNREACHABLE();
11441150
} else {
11451151
uint32_t i, children = zend_ast_get_num_children(ast);
11461152
zend_ast *new = (zend_ast*)buf;
@@ -1206,7 +1212,7 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
12061212
zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
12071213
} else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) {
12081214
ZEND_ASSERT(!Z_REFCOUNTED(((zend_ast_zval*)(ast))->val));
1209-
} else if (EXPECTED(ast->kind >= ZEND_AST_FUNC_DECL)) {
1215+
} else if (EXPECTED(zend_ast_is_decl(ast))) {
12101216
zend_ast_decl *decl = (zend_ast_decl *) ast;
12111217

12121218
if (decl->name) {
@@ -1237,6 +1243,9 @@ ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn, void *contex
12371243
for (i = 0; i < list->children; ++i) {
12381244
fn(&list->child[i], context);
12391245
}
1246+
} else if (zend_ast_is_decl(ast)) {
1247+
/* Not implemented. */
1248+
ZEND_UNREACHABLE();
12401249
} else {
12411250
uint32_t i, children = zend_ast_get_num_children(ast);
12421251
for (i = 0; i < children; ++i) {

Zend/zend_ast.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ static zend_always_inline bool zend_ast_is_special(zend_ast *ast) {
331331
return (ast->kind >> ZEND_AST_SPECIAL_SHIFT) & 1;
332332
}
333333

334+
static zend_always_inline bool zend_ast_is_decl(zend_ast *ast) {
335+
return zend_ast_is_special(ast) && ast->kind >= ZEND_AST_FUNC_DECL;
336+
}
337+
334338
static zend_always_inline bool zend_ast_is_list(zend_ast *ast) {
335339
return (ast->kind >> ZEND_AST_IS_LIST_SHIFT) & 1;
336340
}
@@ -357,6 +361,8 @@ static zend_always_inline zend_string *zend_ast_get_constant_name(zend_ast *ast)
357361

358362
static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
359363
ZEND_ASSERT(!zend_ast_is_list(ast));
364+
ZEND_ASSERT(!zend_ast_is_special(ast));
365+
360366
return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
361367
}
362368
static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {

Zend/zend_compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11293,7 +11293,8 @@ static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
1129311293
break;
1129411294
case ZEND_AST_CLOSURE:
1129511295
zend_compile_const_expr_closure(ast_ptr);
11296-
break;
11296+
/* Return, because we do not want to traverse the children. */
11297+
return;
1129711298
}
1129811299

1129911300
zend_ast_apply(ast, zend_compile_const_expr, context);

ext/opcache/zend_file_cache.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ static void zend_file_cache_serialize_ast(zend_ast *ast,
366366
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
367367
/* The op_array itself will be serialized as part of the dynamic_func_defs. */
368368
SERIALIZE_PTR(Z_PTR(((zend_ast_zval*)ast)->val));
369+
} else if (zend_ast_is_decl(ast)) {
370+
/* Not implemented. */
371+
ZEND_UNREACHABLE();
369372
} else {
370373
uint32_t children = zend_ast_get_num_children(ast);
371374
for (i = 0; i < children; i++) {
@@ -1248,6 +1251,9 @@ static void zend_file_cache_unserialize_ast(zend_ast *ast,
12481251
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
12491252
/* The op_array itself will be unserialized as part of the dynamic_func_defs. */
12501253
UNSERIALIZE_PTR(Z_PTR(((zend_ast_zval*)ast)->val));
1254+
} else if (zend_ast_is_decl(ast)) {
1255+
/* Not implemented. */
1256+
ZEND_UNREACHABLE();
12511257
} else {
12521258
uint32_t children = zend_ast_get_num_children(ast);
12531259
for (i = 0; i < children; i++) {

ext/opcache/zend_persist.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ static zend_ast *zend_persist_ast(zend_ast *ast)
192192
zend_ast_zval *copy = zend_shared_memdup(ast, sizeof(zend_ast_zval));
193193
zend_persist_op_array(&copy->val);
194194
node = (zend_ast *) copy;
195+
} else if (zend_ast_is_decl(ast)) {
196+
/* Not implemented. */
197+
ZEND_UNREACHABLE();
195198
} else {
196199
uint32_t children = zend_ast_get_num_children(ast);
197200
node = zend_shared_memdup(ast, zend_ast_size(children));

ext/opcache/zend_persist_calc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ static void zend_persist_ast_calc(zend_ast *ast)
8989
} else if (ast->kind == ZEND_AST_OP_ARRAY) {
9090
ADD_SIZE(sizeof(zend_ast_zval));
9191
zend_persist_op_array_calc(&((zend_ast_zval*)(ast))->val);
92+
} else if (zend_ast_is_decl(ast)) {
93+
/* Not implemented. */
94+
ZEND_UNREACHABLE();
9295
} else {
9396
uint32_t children = zend_ast_get_num_children(ast);
9497
ADD_SIZE(zend_ast_size(children));

0 commit comments

Comments
 (0)