Skip to content

Commit

Permalink
tests(agent): fix test logic for 8.3 exceptions
Browse files Browse the repository at this point in the history
PHP 8.3 will invoke the user_exception_handler when an exception is
encountered (if one is registered). This results in an inversion of the
expectation from pre-8.3  of failing out of the zend_try/zend_catch
block of code that sets the zend_result to FAILURE and returns NULL to
the expr variable assigned to nr_php_call().
8.3 logic allows our newrelic_exception_handler to intercept the
exception, skip the zend_catch, and return SUCCESS in
nr_php_call_user_func() which returns a non-NULL value.
  • Loading branch information
bduranleau-nr committed Oct 2, 2023
1 parent 4a8b46b commit 969492f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
25 changes: 23 additions & 2 deletions agent/tests/test_php_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ static void test_stack_depth_after_exception() {
tlib_pass_if_int_equal(
"PHP stack depth tracking should be 3 after function call", 3,
NRPRG(php_cur_stack_depth));

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so expr should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null",
expr);
#endif

/*
* Trigger the unwind.
Expand Down Expand Up @@ -289,7 +296,14 @@ static void test_stack_depth_after_exception() {
tlib_pass_if_int_equal(
"PHP stack depth tracking should be 2 after function call", 2,
NRPRG(php_cur_stack_depth));
tlib_pass_if_null("Exception so expr should not be null.", expr);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so expr should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null",
expr);
#endif

/*
* Trigger the unwind.
Expand Down Expand Up @@ -319,7 +333,14 @@ static void test_stack_depth_after_exception() {
tlib_pass_if_int_equal(
"PHP stack depth tracking should be 2 after function call", 1,
NRPRG(php_cur_stack_depth));
tlib_pass_if_null("Exception so expr should not be null.", expr);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so expr should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null",
expr);
#endif

/*
* Trigger the unwind.
Expand Down
55 changes: 55 additions & 0 deletions agent/tests/test_php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,14 @@ static void test_before_after_clean() {
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "all_set", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so expr should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

/*
* Trigger the unwind.
*/
Expand Down Expand Up @@ -877,7 +884,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "before_after", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so does not evaluate.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

tlib_php_request_eval("newrelic_end_transaction(); ");
tlib_pass_if_int_equal("Clean callback should not set value", 10,
NRPRG(drupal_http_request_depth));
Expand Down Expand Up @@ -927,7 +942,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "before_clean", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so func does not evaluate.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

/*
* Trigger the unwind.
*/
Expand Down Expand Up @@ -972,7 +995,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "after_clean", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so returns null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

tlib_php_request_eval("newrelic_end_transaction(); ");
tlib_pass_if_int_equal(
"After callback should not be called and clean callback should set value",
Expand Down Expand Up @@ -1016,7 +1047,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "before_only", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so does not evaluate.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

tlib_php_request_eval("newrelic_end_transaction(); ");
tlib_pass_if_int_equal("Only before would set the value", 10,
NRPRG(drupal_http_request_depth));
Expand Down Expand Up @@ -1059,7 +1098,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "after_only", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

tlib_php_request_eval("newrelic_end_transaction(); ");
tlib_pass_if_int_equal("No callbacks triggered to set the value", 0,
NRPRG(drupal_http_request_depth));
Expand Down Expand Up @@ -1101,7 +1148,15 @@ static void test_before_after_clean() {
*/
arg = tlib_php_request_eval_expr("0" TSRMLS_CC);
expr = nr_php_call(NULL, "clean_only", arg);

#if ZEND_MODULE_API_NO < ZEND_8_3_X_API_NO
tlib_pass_if_null("Exception so should be null.", expr);
#else
tlib_pass_if_not_null(
"Exception caught by NR exception handler, expr should not be null.",
expr);
#endif

tlib_php_request_eval("newrelic_end_transaction(); ");
tlib_pass_if_int_equal("Only clean would set the value", 30,
NRPRG(drupal_http_request_depth));
Expand Down

0 comments on commit 969492f

Please sign in to comment.