Skip to content

Add mode for verifying internal function param defaults #5366

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,44 @@ ZEND_API void ZEND_FASTCALL zend_free_extra_named_params(zend_array *extra_named
zend_array_release(extra_named_params);
}

/* Replace optional parameters that weren't passed with their declared default values,
* which allows us to check that this does not change the behavior of the function. */
#define ZEND_VERIFY_INTERNAL_PARAM_DEFAULTS 1
#if ZEND_VERIFY_INTERNAL_PARAM_DEFAULTS
static void zend_verify_internal_param_defaults(zend_execute_data **call_ptr) {
zend_function *fbc = (*call_ptr)->func;
uint32_t num_passed_args = ZEND_CALL_NUM_ARGS(*call_ptr);
if (num_passed_args < fbc->common.required_num_args) {
/* This is an error anyway. */
return;
}

uint32_t num_declared_args = fbc->common.num_args;
while (num_passed_args < num_declared_args) {
zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[num_passed_args];
zval default_value;
if (zend_get_default_from_internal_arg_info(&default_value, arg_info) == FAILURE) {
/* Default value not available, so we can't pass any further defaults either. */
return;
}

if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
zval_update_constant_ex(&default_value, fbc->common.scope);
}

zend_vm_stack_extend_call_frame(call_ptr, num_passed_args, 1);
zval *arg = ZEND_CALL_VAR_NUM(*call_ptr, num_passed_args);
ZVAL_COPY_VALUE(arg, &default_value);
if (ARG_SHOULD_BE_SENT_BY_REF(fbc, num_passed_args + 1)) {
ZVAL_MAKE_REF(arg);
}

num_passed_args++;
ZEND_CALL_NUM_ARGS(*call_ptr)++;
}
}
#endif

#if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
/* Special versions of functions that sets EX(opline) before calling zend_vm_stack_extend() */
static zend_always_inline zend_execute_data *_zend_vm_stack_push_call_frame_ex(uint32_t used_stack, uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope) /* {{{ */
Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3886,8 +3886,11 @@ ZEND_VM_HOT_HANDLER(129, ZEND_DO_ICALL, ANY, ANY, SPEC(RETVAL))
zval retval;

SAVE_OPLINE();
EX(call) = call->prev_execute_data;
#if ZEND_VERIFY_INTERNAL_PARAM_DEFAULTS
zend_verify_internal_param_defaults(&call);
#endif

EX(call) = call->prev_execute_data;
call->prev_execute_data = execute_data;
EG(current_execute_data) = call;

Expand Down
10 changes: 8 additions & 2 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,11 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DO_ICALL_SPEC_RETV
zval retval;

SAVE_OPLINE();
EX(call) = call->prev_execute_data;
#if ZEND_VERIFY_INTERNAL_PARAM_DEFAULTS
zend_verify_internal_param_defaults(&call);
#endif

EX(call) = call->prev_execute_data;
call->prev_execute_data = execute_data;
EG(current_execute_data) = call;

Expand Down Expand Up @@ -1280,8 +1283,11 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DO_ICALL_SPEC_RETV
zval retval;

SAVE_OPLINE();
EX(call) = call->prev_execute_data;
#if ZEND_VERIFY_INTERNAL_PARAM_DEFAULTS
zend_verify_internal_param_defaults(&call);
#endif

EX(call) = call->prev_execute_data;
call->prev_execute_data = execute_data;
EG(current_execute_data) = call;

Expand Down