Skip to content

Disable opcache per script using "declare(cache=0)" #3678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5245,6 +5245,29 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */
CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
}

} else if (zend_string_equals_literal_ci(name, "cache")) {
zval value_zv;

if (FAILURE == zend_declare_is_first_statement(ast)) {
zend_error_noreturn(E_COMPILE_ERROR, "cache declaration must be "
"the very first statement in the script");
Copy link
Contributor

@Majkl578 Majkl578 Nov 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is a bit misleading, when cache=0 is used together with (and after) strict_types.

}

if (ast->child[1] != NULL) {
zend_error_noreturn(E_COMPILE_ERROR, "cache declaration must not "
"use block mode");
}

zend_const_expr_to_zval(&value_zv, value_ast);

if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
zend_error_noreturn(E_COMPILE_ERROR, "cache declaration must have 0 or 1 as its value");
}

if (Z_LVAL(value_zv) == 0) {
CG(active_op_array)->fn_flags |= ZEND_ACC_NEVER_CACHE;
}

} else {
zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ typedef struct _zend_oparray_context {
#define ZEND_ACC_CALL_VIA_TRAMPOLINE (1 << 17) /* | X | | */
/* | | | */
/* disable inline caching | | | */
/* or disable caching in opcache for main op_array | | | */
#define ZEND_ACC_NEVER_CACHE (1 << 18) /* | X | | */
/* | | | */
/* Closure related | | | */
Expand Down
6 changes: 5 additions & 1 deletion ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,10 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl
return NULL;
}

if (op_array->fn_flags & ZEND_ACC_NEVER_CACHE) {
return NULL;
}

/* Build the persistent_script structure.
Here we aren't sure we would store it, but we will need it
further anyway.
Expand Down Expand Up @@ -3169,7 +3173,7 @@ static zend_op_array *preload_compile_file(zend_file_handle *file_handle, int ty
{
zend_op_array *op_array = preload_orig_compile_file(file_handle, type);

if (op_array && op_array->refcount) {
if (op_array && op_array->refcount && !(op_array->fn_flags & ZEND_ACC_NEVER_CACHE)) {
zend_persistent_script *script;

script = create_persistent_script();
Expand Down