Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
6ed1863
use ZEND_OP_ARRAY_EXTENSION to store wraprec
lavarou Jan 31, 2025
88485df
verify wraprec belongs to process before using it
lavarou Jan 31, 2025
fd5c35b
don't store wraprec on op_array extension too soon
lavarou Jan 31, 2025
d2680ad
use op_array extension only for PHPs 8.0+
lavarou Jan 31, 2025
083bf39
fix use of op_array extension on PHP 8.4
lavarou Jan 31, 2025
d12eda5
re-use nr_php_show_exec in fcall_init
lavarou Feb 18, 2025
97b3c90
refactor wraprec storage and lookup [wip]
lavarou Mar 11, 2025
223ff67
first round of fixes after initial testing
lavarou Mar 11, 2025
4c9098b
second round of fixes
lavarou Mar 12, 2025
82477ae
copy key to wraprec only if new one was created
lavarou Mar 12, 2025
6be44b9
ensure runtime cache for function is initialized
lavarou Mar 12, 2025
3503ec6
don't forget to destroy transient wraprecs!!!
lavarou Mar 12, 2025
4c04db2
third round of fixes
lavarou Mar 13, 2025
cbbb74f
optimize library/framework detection for PHPs 8.0+
lavarou Mar 14, 2025
3ce9c4d
fourth round of fixes
lavarou Mar 14, 2025
1ee6486
remove nr_php_amqplib_ensure_class
lavarou Mar 14, 2025
998970b
fix unit tests build for PHPs < 8.0
lavarou Apr 1, 2025
952f119
fifth round of fixes
lavarou Apr 2, 2025
9d261da
fix laravel console application instrumentation
lavarou Apr 2, 2025
5131a2d
use `NR_OP_ARRAY` macro when accessing &execute_data->func->op_array
lavarou Apr 15, 2025
49bd6f9
sixth round of fixes
lavarou Apr 18, 2025
10416ff
seventh round of fixes
lavarou Apr 18, 2025
65515a8
Merge branch 'dev' into refactor/optimize-wraprec-lookup
lavarou Apr 21, 2025
7ae0675
ensure zend_function* passed to nr_php_wraprec_lookup_get is never NULL
lavarou Apr 22, 2025
986ba9d
add span events tests for different configurations of tt_details
lavarou Apr 23, 2025
2d812cd
fixup! 986ba9dd36f97694c7c7c11c91b261e9e6221047
lavarou Apr 24, 2025
ba95431
Revert 53d10e0870955520e2c020af1194a7733486932a
lavarou Apr 25, 2025
a724769
Revert 2d4eb21a6b1376481f478544c06999b508c53f00
lavarou Apr 25, 2025
cf07d9a
apply code review feedback and reduce code duplication
lavarou Apr 25, 2025
c494a45
ensure use of ZEND_OP_ARRAY_EXTENSION is allowed
lavarou Apr 25, 2025
09eca03
cleanup nr_php_fcall_register_handlers
lavarou Apr 29, 2025
b887845
add input validation when adding named custom tracer
lavarou Apr 29, 2025
64408b5
improve error handling in user function instrumentation initialization
lavarou Apr 29, 2025
575aa95
fixup! b887845010b262ecccd106da367f46b42e688d2b
lavarou Apr 30, 2025
c9563c2
apply code review feedback and add a NULL check
lavarou May 1, 2025
75e2253
simplify nr_php_show_exec
lavarou May 1, 2025
b457827
explain passing `-1` as `namestrlen` argument of nr_php_add_custom_tr…
lavarou May 1, 2025
d6eee1a
log debug message when reusing wraprec in nr_php_user_instrument_wrap…
lavarou May 1, 2025
55ea3c5
apply code review feedback and always initialize pointers to NULL
lavarou May 2, 2025
3cc2311
use a different mechanism to validate value retrieved from ZEND_OP_AR…
lavarou May 2, 2025
25a2df3
test add_custom_tracer with strange input
lavarou May 2, 2025
04ccbd1
test attribute instrumentation in PHP 8.0+
lavarou May 2, 2025
ac8497c
test trampoline functions to ensure stability
lavarou May 2, 2025
d74562e
skip tests for anonymous class's methods instrumentation in PHPs < 8.0
lavarou May 5, 2025
c883e84
make test deterministic
lavarou May 5, 2025
5383c94
use NRSAFESTR when printing strings that can be NULL
lavarou May 5, 2025
8abf1eb
simplify user instrumentation initialization handling
lavarou May 5, 2025
c6c50d9
apply code review feedback and remove redundant NULL checks
lavarou May 8, 2025
9748cf5
add add_custom_tracer_named test for unscoped global function
lavarou May 8, 2025
da65028
correct description in trampoline tests to improve clarity
lavarou May 8, 2025
952b2bc
NRPRG(pid) is only needed for PHPs < 7.4
lavarou May 8, 2025
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
13 changes: 10 additions & 3 deletions agent/php_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
* conditions wherever possible.
*/

static inline nruserfn_t* nr_php_get_wraprec_from_op_array_extension(const char* fn, zend_function* zf) {
nruserfn_t* wraprec = (nruserfn_t*)ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle));
nrl_verbosedebug(NRL_AGENT, "%s from %s, op_array_extension=%p, wraprec=%p, wraprec->pid=%d", __func__, fn, ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)), wraprec, wraprec->pid);
return wraprec;
}

static void nr_php_show_exec_return(NR_EXECUTE_PROTO TSRMLS_DC);
static int nr_php_show_exec_indentation(TSRMLS_D);
static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC);
Expand Down Expand Up @@ -598,6 +604,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
char argstr[NR_EXECUTE_DEBUG_STRBUFSZ];
const char* filename = nr_php_op_array_file_name(NR_OP_ARRAY);
const char* function_name = nr_php_op_array_function_name(NR_OP_ARRAY);
nruserfn_t* wr = nr_php_get_wraprec_from_op_array_extension(__func__, execute_data->func);

argstr[0] = '\0';

Expand All @@ -621,7 +628,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
nr_php_op_array_get_wraprec(NR_OP_ARRAY TSRMLS_CC) ? " *" : "",
#else
nr_php_get_wraprec(execute_data->func) ? " *" : "",
wr ? " *" : "",
#endif
NRP_FILENAME(filename), NR_OP_ARRAY->line_start);
} else if (NR_OP_ARRAY->function_name) {
Expand All @@ -642,7 +649,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
nr_php_op_array_get_wraprec(NR_OP_ARRAY TSRMLS_CC) ? " *" : "",
#else
nr_php_get_wraprec(execute_data->func) ? " *" : "",
wr ? " *" : "",
#endif
NRP_FILENAME(filename), NR_OP_ARRAY->line_start);
} else if (NR_OP_ARRAY->filename) {
Expand Down Expand Up @@ -1928,7 +1935,7 @@ static void nr_php_instrument_func_begin(NR_EXECUTE_PROTO) {
*/
nr_php_observer_attempt_call_cufa_handler(NR_EXECUTE_ORIG_ARGS);
}
wraprec = nr_php_get_wraprec(execute_data->func);
wraprec = nr_php_get_wraprec_from_op_array_extension(__func__, execute_data->func);

segment = nr_segment_start(NRPRG(txn), NULL, NULL);

Expand Down
4 changes: 1 addition & 3 deletions agent/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ typedef struct _nrphpglobals_t {
* variable with the key `NEW_RELIC_LABELS` */
#if ZEND_MODULE_API_NO >= ZEND_8_1_X_API_NO /* PHP 8.1+ */
zend_long zend_offset; /* Zend extension offset */
zend_long
zend_op_array_offset; /* Zend extension op_array to modify reserved */
#else
int zend_offset; /* Zend extension offset */
int zend_op_array_offset; /* Zend extension op_array to modify reserved */
#endif
int op_array_extension_handle; /* Zend op_array extension handle to attach agent's data to function */
int done_instrumentation; /* Set to true if we have installed instrumentation
handlers */
nrtime_t expensive_min; /* newrelic.special.expensive_node_min */
Expand Down
5 changes: 5 additions & 0 deletions agent/php_minit.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,11 @@ PHP_MINIT_FUNCTION(newrelic) {
nrl_debug(NRL_INIT, "MINIT processing done");
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP 7.4+ */
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(dummy);
#if ZEND_MODULE_API_NO >= ZEND_8_4_X_API_NO /* PHP 7.4+ */
NR_PHP_PROCESS_GLOBALS(op_array_extension_handle) = zend_get_internal_function_extension_handle("newrelic");
#else
NR_PHP_PROCESS_GLOBALS(op_array_extension_handle) = zend_get_op_array_extension_handle("newrelic");
#endif
#else
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(&dummy);
#endif
Expand Down
14 changes: 14 additions & 0 deletions agent/php_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,27 @@
static zend_observer_fcall_handlers nr_php_fcall_register_handlers(
zend_execute_data* execute_data) {
zend_observer_fcall_handlers handlers = {NULL, NULL};
nruserfn_t* wr;
if (NULL == execute_data) {
return handlers;
}
if ((NULL == execute_data->func)
|| (ZEND_INTERNAL_FUNCTION == execute_data->func->type)) {
return handlers;
}

if (!OP_ARRAY_IS_A_FILE(NR_OP_ARRAY)) {
nrl_verbosedebug(NRL_AGENT, "Registering Observer API handlers for user function %s",
NRSAFESTR(nr_php_op_array_function_name(NR_OP_ARRAY)));
} else {
nrl_verbosedebug(NRL_AGENT, "Registering Observer API handlers for file %s",
NRSAFESTR(nr_php_op_array_file_name(NR_OP_ARRAY)));
}

wr = nr_php_get_wraprec(execute_data->func);
// store the wraprec in the op_array extension for the duration of the request for later lookup
ZEND_OP_ARRAY_EXTENSION(&execute_data->func->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)) = wr;

handlers.begin = nr_php_observer_fcall_begin;
handlers.end = nr_php_observer_fcall_end;
return handlers;
Expand Down
8 changes: 7 additions & 1 deletion agent/php_user_instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ static nr_php_wraprec_hashmap_t* user_function_wrappers;
static inline void nr_php_wraprec_lookup_set(nruserfn_t* wr,
zend_function* zf) {
nr_php_wraprec_hashmap_update(user_function_wrappers, zf, wr);
// store the wraprec in the op_array extension for the duration of the request for later lookup
// for situation when wraprec is added after first execution of the function
ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)) = wr;

}
static inline nruserfn_t* nr_php_wraprec_lookup_get(zend_function* zf) {
nruserfn_t* wraprec = NULL;
Expand Down Expand Up @@ -291,7 +295,9 @@ static void nr_php_wrap_user_function_internal(nruserfn_t* wraprec TSRMLS_DC) {
}

static nruserfn_t* nr_php_user_wraprec_create(void) {
return (nruserfn_t*)nr_zalloc(sizeof(nruserfn_t));
nruserfn_t* wr = (nruserfn_t*)nr_zalloc(sizeof(nruserfn_t));
wr->pid = nr_getpid();
return wr;
}

static nruserfn_t* nr_php_user_wraprec_create_named(const char* full_name,
Expand Down
1 change: 1 addition & 0 deletions agent/php_user_instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef struct _nruserfn_t {
#if ZEND_MODULE_API_NO >= ZEND_7_4_X_API_NO
char* wordpress_plugin_theme;
#endif
int pid; /* pid of a process that created this wraprec */
} nruserfn_t;

extern nruserfn_t* nr_wrapped_user_functions; /* a singly linked list */
Expand Down