Skip to content

chore(agent): remove obsolete PHP 7.0/7.1 ZEND_MODULE_API_NO usage #1092

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 2 commits into
base: remove-obsolete-code
Choose a base branch
from
Open
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
12 changes: 0 additions & 12 deletions agent/fw_laravel_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ NR_PHP_WRAPPER(nr_laravel_queue_worker_process) {
if (EG(exception)) {
zval* exception_zval = NULL;

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
/*
* On PHP 7, EG(exception) is stored as a zend_object, and is only wrapped
* in a zval when it actually needs to be. Unfortunately, our error handling
Expand All @@ -640,12 +639,6 @@ NR_PHP_WRAPPER(nr_laravel_queue_worker_process) {

ZVAL_OBJ(&exception, EG(exception));
exception_zval = &exception;
#else
/*
* On PHP 5, the exception is just a regular old zval.
*/
exception_zval = EG(exception);
#endif /* PHP7+ */

nr_php_error_record_exception(
NRPRG(txn), exception_zval, NR_PHP_ERROR_PRIORITY_UNCAUGHT_EXCEPTION,
Expand Down Expand Up @@ -835,13 +828,8 @@ NR_PHP_WRAPPER(nr_laravel_queue_queue_createpayload) {
/*
* Finally, we change the string in the return value to our new JSON.
*/
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
zend_string_free(Z_STR_P(*retval_ptr));
Z_STR_P(*retval_ptr) = zend_string_copy(Z_STR_P(json));
#else
efree(Z_STRVAL_PP(retval_ptr));
nr_php_zval_str_len(*retval_ptr, Z_STRVAL_P(json), Z_STRLEN_P(json));
#endif /* PHP7+ */

end:
nr_php_zval_free(&payload);
Expand Down
158 changes: 0 additions & 158 deletions agent/php_agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ static zval* nr_php_get_zval_object_property_with_class_internal(
zval* object,
zend_class_entry* ce,
const char* cname TSRMLS_DC) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
/*
* Although the below notes still apply in principle, PHP 7 additionally broke
* the API for zend_read_property by adding an rv parameter, which is used to
Expand All @@ -41,25 +40,6 @@ static zval* nr_php_get_zval_object_property_with_class_internal(
if (&EG(uninitialized_zval) != data) {
return data;
}
#else
/*
* This attempts to read uninitialized (or non existing) properties always
* return uninitialized_zval_ptr, even in the case where we read a property
* during pre-hook time on a constructor.
*/
zend_bool silent = 1; /* forces BP_VAR_IS semantics */
#if ZEND_MODULE_API_NO >= ZEND_5_4_X_API_NO
zval* data = zend_read_property(ce, object, cname, nr_strlen(cname),
silent TSRMLS_CC);
#else
zval* data = zend_read_property(ce, object, (char*)nr_remove_const(cname),
nr_strlen(cname), silent TSRMLS_CC);
#endif /* PHP >= 5.4 */

if (EG(uninitialized_zval_ptr) != data) {
return data;
}
#endif /* PHP7+ */

return NULL;
}
Expand Down Expand Up @@ -149,32 +129,12 @@ int nr_php_object_has_method(zval* object, const char* lcname TSRMLS_DC) {
return 0;
} else {
void* func;

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
zend_string* name_str = zend_string_init(vname, namelen, 0);

func = (void*)Z_OBJ_HT_P(object)->get_method(&Z_OBJ_P(object), name_str,
NULL TSRMLS_CC);

zend_string_release(name_str);
#elif ZEND_MODULE_API_NO >= ZEND_5_4_X_API_NO
/*
* This can leak if the object has a __call() method, as in that situation
* only, zend_std_get_method() will indirectly allocate a new
* zend_function in zend_get_user_call_function().
*
* We can't easily detect this, and the zend_function() is allocated via
* emalloc(), so we're just going to let this slide and let the Zend
* Engine clean it up at RSHUTDOWN. Note that this needs to be suppressed
* in Valgrind, though.
*/
func = (void*)Z_OBJ_HT_P(object)->get_method(
&object, vname, namelen,
NULL TSRMLS_CC); /* nr_php_object_has_method */
#else /* PHP < 5.4 */
func = (void*)Z_OBJ_HT_P(object)->get_method(
&object, vname, namelen TSRMLS_CC); /* nr_php_object_has_method */
#endif

if (NULL == func) {
return 0;
Expand Down Expand Up @@ -214,23 +174,11 @@ zend_function* nr_php_find_function(const char* name TSRMLS_DC) {
* whereas PHP 7 only uses a single level of indirection.
*/
zend_class_entry* nr_php_find_class(const char* name TSRMLS_DC) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
if (NULL == name) {
return NULL;
}

return (zend_class_entry*)nr_php_zend_hash_find_ptr(EG(class_table), name);
#else
zend_class_entry** ce_ptr = 0;

if (0 == name) {
return 0;
}

ce_ptr = (zend_class_entry**)nr_php_zend_hash_find_ptr(EG(class_table), name);

return ce_ptr ? *ce_ptr : NULL;
#endif /* PHP7+ */
}

zend_function* nr_php_find_class_method(const zend_class_entry* klass,
Expand Down Expand Up @@ -286,15 +234,9 @@ zend_function* nr_php_zval_to_function(zval* zv TSRMLS_DC) {
return NULL;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
if (zend_is_callable_ex(zv, NULL, 0, NULL, &fcc, NULL)) {
return fcc.function_handler;
}
#else
if (zend_is_callable_ex(zv, NULL, 0, NULL, NULL, &fcc, NULL TSRMLS_CC)) {
return fcc.function_handler;
}
#endif /* PHP7+ */

return NULL;
}
Expand Down Expand Up @@ -348,41 +290,20 @@ zval* nr_php_get_user_func_arg(size_t requested_arg_index,
return NULL;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
(void)arg_count_via_h;

if (requested_arg_index > ZEND_CALL_NUM_ARGS(execute_data)) {
return NULL;
}

arg_via_h = ZEND_CALL_ARG(execute_data, requested_arg_index);
#else
arg_via_h = nr_php_get_user_func_arg_via_h(
requested_arg_index, &arg_count_via_h, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
#endif /* PHP7+ */

return arg_via_h;
}

size_t nr_php_get_user_func_arg_count(NR_EXECUTE_PROTO TSRMLS_DC) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
NR_UNUSED_FUNC_RETURN_VALUE;
return (size_t)ZEND_CALL_NUM_ARGS(execute_data);
#else
int arg_count_via_h = -1;

if (NULL
== nr_php_get_user_func_arg_via_h(1, &arg_count_via_h,
NR_EXECUTE_ORIG_ARGS TSRMLS_CC)) {
return 0;
} else if (arg_count_via_h < 0) {
nrl_verbosedebug(NRL_AGENT, "%s: unexpected argument count %d", __func__,
arg_count_via_h);
return 0;
}

return (size_t)arg_count_via_h;
#endif /* PHP7+ */
}

zend_execute_data* nr_php_get_caller_execute_data(NR_EXECUTE_PROTO,
Expand Down Expand Up @@ -411,30 +332,18 @@ zend_execute_data* nr_php_get_caller_execute_data(NR_EXECUTE_PROTO,
ced = ced->prev_execute_data;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
if ((NULL == ced) || (NULL == ced->opline)) {
return NULL;
}
#else
if ((NULL == ced) || (NULL == ced->op_array)) {
return NULL;
}
#endif /* PHP7+ */

if ((ZEND_DO_FCALL != ced->opline->opcode)
&& (ZEND_DO_FCALL_BY_NAME != ced->opline->opcode)) {
return NULL;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
if (NULL == ced->func) {
return NULL;
}
#else
if (0 == ced->function_state.function) {
return NULL;
}
#endif /* PHP7+ */

return ced;
}
Expand All @@ -448,17 +357,12 @@ const zend_function* nr_php_get_caller(NR_EXECUTE_PROTO,
return NULL;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
return ped->func;
#else
return ped->function_state.function;
#endif /* PHP7+ */
}

zval* nr_php_get_active_php_variable(const char* name TSRMLS_DC) {
HashTable* table;

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
table = zend_rebuild_symbol_table();

/*
Expand All @@ -472,10 +376,6 @@ zval* nr_php_get_active_php_variable(const char* name TSRMLS_DC) {
* https://nikic.github.io/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html#indirect-zvals
*/
return nr_php_zval_direct(nr_php_zend_hash_find(table, name));
#else
table = EG(active_symbol_table);
return nr_php_zend_hash_find(table, name);
#endif /* PHP7+ */
}

int nr_php_silence_errors(TSRMLS_D) {
Expand All @@ -491,7 +391,6 @@ void nr_php_restore_errors(int error_reporting TSRMLS_DC) {
}

zval* nr_php_get_constant(const char* name TSRMLS_DC) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
zval* constant;
zval* copy = NULL;
zend_string* name_str;
Expand All @@ -515,31 +414,9 @@ zval* nr_php_get_constant(const char* name TSRMLS_DC) {
ZVAL_DUP(copy, constant);

return copy;
#else
nr_string_len_t len;
int rv;
zval* constant = NULL;

if (NULL == name) {
return NULL;
}

len = nr_strlen(name);
constant = nr_php_zval_alloc();

/* zend_get_constant returns 0 and 1 (not SUCCESS or FAILURE) */
rv = zend_get_constant(name, len, constant TSRMLS_CC);
if (0 == rv) {
nr_php_zval_free(&constant);
return NULL;
}

return constant;
#endif /* PHP7+ */
}

zval* nr_php_get_class_constant(const zend_class_entry* ce, const char* name) {
#if ZEND_MODULE_API_NO >= ZEND_7_1_X_API_NO
zend_class_constant* constant = NULL;
zval* copy = NULL;

Expand All @@ -555,29 +432,6 @@ zval* nr_php_get_class_constant(const zend_class_entry* ce, const char* name) {
}

return copy;
#else
zval* constant = NULL;
zval* copy = NULL;

if (NULL == ce) {
return NULL;
}

constant = nr_php_zend_hash_find(&(ce->constants_table), name);

if (constant) {
copy = nr_php_zval_alloc();

/*
* PHP 7.0 usually returns an IS_REF. We need to unwrap to ensure that we
* duplicate the concrete value, otherwise the caller will end up freeing a
* value that it doesn't own, and bad things will happen.
*/
ZVAL_DUP(copy, nr_php_zval_real_value(constant));
}

return copy;
#endif
}

char* nr_php_get_object_constant(zval* app, const char* name) {
Expand Down Expand Up @@ -649,17 +503,13 @@ int nr_php_is_zval_named_constant(const zval* zv, const char* name TSRMLS_DC) {
}

int nr_php_zend_is_auto_global(const char* name, size_t name_len TSRMLS_DC) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
zend_bool rv;
zend_string* zs = zend_string_init(name, name_len, 0);

rv = zend_is_auto_global(zs);

zend_string_free(zs);
return rv;
#else
return (int)zend_is_auto_global(name, (int)name_len TSRMLS_CC);
#endif /* PHP7+ */
}

const char* nr_php_use_license(const char* api_license TSRMLS_DC) {
Expand Down Expand Up @@ -687,11 +537,7 @@ char* nr_php_get_server_global(const char* name TSRMLS_DC) {
return NULL;
}

#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
global = &(PG(http_globals)[TRACK_VARS_SERVER]);
#else
global = PG(http_globals)[TRACK_VARS_SERVER];
#endif

if (!nr_php_is_zval_valid_array(global)) {
return NULL;
Expand Down Expand Up @@ -1014,11 +860,7 @@ char* nr_php_function_debug_name(const zend_function* func) {

if ((ZEND_USER_FUNCTION == func->type)
&& (func->common.fn_flags & ZEND_ACC_CLOSURE)) {
#if ZEND_MODULE_API_NO >= ZEND_7_0_X_API_NO /* PHP 7.0+ */
const char* filename = ZSTR_VAL(func->op_array.filename);
#else
const char* filename = func->op_array.filename;
#endif /* PHP7+ */
char* orig_name = name;

name = nr_formatf("%s declared at %s:%d", orig_name, NRSAFESTR(filename),
Expand Down
Loading