Skip to content

Commit ffd9ca2

Browse files
committed
fix
1 parent f078838 commit ffd9ca2

File tree

5 files changed

+131
-24
lines changed

5 files changed

+131
-24
lines changed

agent/lib_aws_sdk_php.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ void nr_lib_aws_sdk_php_lambda_handle(nr_segment_t* auto_segment,
321321
return;
322322
}
323323

324-
if (NULL == retval_ptr) {
324+
if (NULL == *retval_ptr) {
325325
/* Do not instrument when an exception has happened */
326326
return;
327327
}
@@ -362,23 +362,38 @@ void nr_lib_aws_sdk_php_lambda_handle(nr_segment_t* auto_segment,
362362
nr_segment_traces_add_cloud_attributes(external_segment, &cloud_attrs);
363363
nr_segment_external_params_t external_params = {.library = "aws_sdk"};
364364
zval* data = nr_php_get_zval_object_property(*retval_ptr, "data");
365-
if (NULL != data && IS_ARRAY == Z_TYPE_P(data)) {
365+
if (nr_php_is_zval_valid_array(data)) {
366366
zval* status_code = nr_php_zend_hash_find(Z_ARRVAL_P(data), "StatusCode");
367-
if (NULL != status_code && IS_LONG == Z_TYPE_P(status_code)) {
367+
if (nr_php_is_zval_valid_integer(status_code)) {
368368
external_params.status = Z_LVAL_P(status_code);
369369
}
370370
zval* metadata = nr_php_zend_hash_find(Z_ARRVAL_P(data), "@metadata");
371-
if (NULL != metadata && IS_REFERENCE == Z_TYPE_P(metadata)
372-
&& IS_ARRAY == Z_TYPE_P(Z_REFVAL_P(metadata))) {
373-
zval* uri = nr_php_zend_hash_find(Z_ARRVAL_P(Z_REFVAL_P(metadata)), "effectiveUri");
374-
if (NULL != uri && IS_STRING == Z_TYPE_P(uri)) {
375-
external_params.uri = Z_STRVAL_P(uri);
371+
if (NULL != metadata && IS_REFERENCE == Z_TYPE_P(metadata)) {
372+
metadata = Z_REFVAL_P(metadata);
373+
if (IS_ARRAY == Z_TYPE_P(metadata)) {
374+
zval* uri = nr_php_zend_hash_find(Z_ARRVAL_P(metadata), "effectiveUri");
375+
if (nr_php_is_zval_valid_string(uri)) {
376+
external_params.uri = Z_STRVAL_P(uri);
377+
}
376378
}
377379
}
378380
}
379381
nr_segment_external_end(&auto_segment, &external_params);
380382
}
381383

384+
/* This stores the compiled regex to parse AWS ARNs. The compilation happens when
385+
* it is first needed and is destroyed in mshutdown
386+
*/
387+
static nr_regex_t* aws_arn_regex;
388+
389+
static void nr_aws_sdk_compile_regex(void) {
390+
aws_arn_regex = nr_regex_create(AWS_ARN_REGEX, 0, 0);
391+
}
392+
393+
void nr_aws_sdk_mshutdown(void) {
394+
nr_regex_destroy(&aws_arn_regex);
395+
}
396+
382397
void nr_aws_lambda_invoke(NR_EXECUTE_PROTO, nr_segment_cloud_attrs_t* cloud_attrs) {
383398
zval* call_args = nr_php_get_user_func_arg(2, NR_EXECUTE_ORIG_ARGS);
384399
zval* this_obj = NR_PHP_USER_FN_THIS();
@@ -391,26 +406,26 @@ void nr_aws_lambda_invoke(NR_EXECUTE_PROTO, nr_segment_cloud_attrs_t* cloud_attr
391406
char* accountID = NULL;
392407

393408
/* verify arguments */
394-
if (NULL == call_args || IS_ARRAY != Z_TYPE_P(call_args)) {
409+
if (!nr_php_is_zval_valid_array(call_args)) {
395410
return;
396411
}
397412
zval* lambda_args = nr_php_zend_hash_index_find(Z_ARRVAL_P(call_args), 0);
398-
if (NULL == lambda_args || IS_ARRAY != Z_TYPE_P(lambda_args)) {
413+
if (!nr_php_is_zval_valid_array(lambda_args)) {
399414
return;
400415
}
401416
zval* lambda_name = nr_php_zend_hash_find(Z_ARRVAL_P(lambda_args), "FunctionName");
402-
if (NULL == lambda_name || IS_STRING != Z_TYPE_P(lambda_name)) {
417+
if (!nr_php_is_zval_valid_string(lambda_name)) {
403418
return;
404419
}
405420

406-
/* Compile the regex */
407-
if (NULL == NRPRG(aws_arn_regex)) {
408-
NRPRG(aws_arn_regex) = nr_regex_create(AWS_ARN_REGEX, 0, 0);
421+
/* Ensure regex exists */
422+
if (NULL == aws_arn_regex) {
423+
nr_aws_sdk_compile_regex();
409424
}
410425

411426
/* Extract all information possible from the passed lambda name via regex */
412427
nr_regex_substrings_t* matches =
413-
nr_regex_match_capture(NRPRG(aws_arn_regex),
428+
nr_regex_match_capture(aws_arn_regex,
414429
Z_STRVAL_P(lambda_name),
415430
Z_STRLEN_P(lambda_name));
416431
function_name = nr_regex_substrings_get_named(matches, "functionName");
@@ -429,10 +444,10 @@ void nr_aws_lambda_invoke(NR_EXECUTE_PROTO, nr_segment_cloud_attrs_t* cloud_attr
429444
}
430445
if (nr_strempty(accountID)) {
431446
accountID = NRINI(aws_account_id);
432-
accountID = "012345";
433447
}
434448
if (nr_strempty(region)) {
435-
region_zval = nr_php_call(this_obj, "getRegion");
449+
region_zval
450+
= nr_php_get_zval_object_property(this_obj, "region");
436451
if (nr_php_is_zval_valid_string(region_zval)) {
437452
region = Z_STRVAL_P(region_zval);
438453
}

agent/lib_aws_sdk_php.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ extern void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment,
8686
*/
8787
void nr_aws_lambda_invoke(NR_EXECUTE_PROTO, nr_segment_cloud_attrs_t* cloud_attrs);
8888

89+
/*
90+
* Purpose : Handles regex destruction during mshutdown
91+
*/
92+
void nr_aws_sdk_mshutdown(void);
93+
8994
/*
9095
* Purpose : The second argument to the Aws/AwsClient::__call function should be
9196
* an array, the first element of which is itself an array of arguments that

agent/php_minit.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,6 @@ PHP_MINIT_FUNCTION(newrelic) {
716716
nr_guzzle4_minit(TSRMLS_C);
717717
nr_guzzle6_minit(TSRMLS_C);
718718
nr_laravel_minit(TSRMLS_C);
719-
nr_wordpress_minit();
720719
nr_php_set_opcode_handlers();
721720

722721
nrl_debug(NRL_INIT, "MINIT processing done");

agent/php_mshutdown.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ PHP_MSHUTDOWN_FUNCTION(newrelic) {
4242

4343
nr_wordpress_mshutdown();
4444

45+
#if ZEND_MODULE_API_NO >= ZEND_8_1_X_API_NO /* PHP 8.1+ */
46+
nr_aws_sdk_mshutdown();
47+
#endif
48+
4549
/* restore header handler */
4650
sapi_module.header_handler = NR_PHP_PROCESS_GLOBALS(orig_header_handler);
4751
NR_PHP_PROCESS_GLOBALS(orig_header_handler) = NULL;

agent/tests/test_lib_aws_sdk_php.c

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,23 @@ NR_PHP_WRAPPER_END
6363

6464
NR_PHP_WRAPPER(aws_lambda_invoke_wrapper) {
6565
nr_segment_cloud_attrs_t cloud_attrs = {0};
66+
/*
67+
* Because argument 1 is not used in instrumentation, we will use it
68+
* to pass in the expected value
69+
*/
6670
zval* expected = nr_php_get_user_func_arg(1, NR_EXECUTE_ORIG_ARGS);
6771
nr_aws_lambda_invoke(NR_EXECUTE_ORIG_ARGS, &cloud_attrs);
6872
(void)wraprec;
6973

70-
tlib_pass_if_str_equal("Expected should match reconstructed arn",
71-
Z_STRVAL_P(expected),
72-
cloud_attrs.cloud_resource_id);
74+
if (nr_php_is_zval_valid_string(expected)) {
75+
tlib_pass_if_str_equal("Expected should match reconstructed arn",
76+
Z_STRVAL_P(expected),
77+
cloud_attrs.cloud_resource_id);
78+
} else {
79+
tlib_pass_if_str_equal("Expected should match reconstructed arn",
80+
NULL,
81+
cloud_attrs.cloud_resource_id);
82+
}
7383
NR_PHP_WRAPPER_CALL;
7484
}
7585
NR_PHP_WRAPPER_END
@@ -471,28 +481,102 @@ static void test_nr_lib_aws_sdk_php_handle_version(void) {
471481
tlib_php_request_end();
472482
}
473483

484+
#if ZEND_MODULE_API_NO >= ZEND_8_1_X_API_NO
474485
static void test_nr_lib_aws_sdk_php_lambda_invoke() {
475-
tlib_php_engine_create("");
486+
tlib_php_engine_create("newrelic.cloud.aws.account_id=\"test_account\"");
476487
tlib_php_request_start();
477488

478489
tlib_php_request_eval("function lambda_invoke($a, $b) { return; }");
479490
nr_php_wrap_user_function(NR_PSTR("lambda_invoke"), aws_lambda_invoke_wrapper);
480491

492+
/* Test full-info run */
481493
char* args
482494
= "array("
483495
" 0 => array("
484-
" 'FunctionName' => 'us-east-2:012345678901:function'"
496+
" 'FunctionName' => 'us-east-2:012345678901:function:my-function'"
485497
" )"
486498
")";
487499
zval* array_arg = tlib_php_request_eval_expr(args);
488-
char* expect = "'arn:aws:lamba:us-east-2:012345678901:function'";
500+
char* expect = "'arn:aws:lambda:us-east-2:012345678901:function:my-function'";
489501
zval* expect_arg = tlib_php_request_eval_expr(expect);
490502
zval* expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
491503
tlib_pass_if_not_null("Expression should evaluate.", expr);
504+
nr_php_zval_free(&expr);
505+
nr_php_zval_free(&expect_arg);
506+
nr_php_zval_free(&array_arg);
507+
508+
/* Test alias full-info run */
509+
args
510+
= "array("
511+
" 0 => array("
512+
" 'FunctionName' => 'us-east-2:012345678901:function:my-function:v1'"
513+
" )"
514+
")";
515+
array_arg = tlib_php_request_eval_expr(args);
516+
expect = "'arn:aws:lambda:us-east-2:012345678901:function:my-function:v1'";
517+
expect_arg = tlib_php_request_eval_expr(expect);
518+
expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
519+
tlib_pass_if_not_null("Expression should evaluate.", expr);
520+
nr_php_zval_free(&expr);
521+
nr_php_zval_free(&expect_arg);
522+
nr_php_zval_free(&array_arg);
523+
524+
/* Test INI extract */
525+
args
526+
= "array("
527+
" 0 => array("
528+
" 'FunctionName' => 'us-east-2:my-function'"
529+
" )"
530+
")";
531+
array_arg = tlib_php_request_eval_expr(args);
532+
expect = "'arn:aws:lambda:us-east-2:test_account:function:my-function'";
533+
expect_arg = tlib_php_request_eval_expr(expect);
534+
expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
535+
tlib_pass_if_not_null("Expression should evaluate.", expr);
536+
nr_php_zval_free(&expr);
537+
nr_php_zval_free(&expect_arg);
538+
nr_php_zval_free(&array_arg);
539+
540+
/* Test invalid arg 1 */
541+
args
542+
= "array("
543+
" 0 => array("
544+
" 'FunctionName' => 123"
545+
" )"
546+
")";
547+
array_arg = tlib_php_request_eval_expr(args);
548+
expect = "NULL";
549+
expect_arg = tlib_php_request_eval_expr(expect);
550+
expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
551+
tlib_pass_if_not_null("Expression should evaluate.", expr);
552+
nr_php_zval_free(&expr);
553+
nr_php_zval_free(&array_arg);
554+
555+
/* Test invalid arg 2 */
556+
args
557+
= "array("
558+
" 0 => array("
559+
" )"
560+
")";
561+
array_arg = tlib_php_request_eval_expr(args);
562+
expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
563+
tlib_pass_if_not_null("Expression should evaluate.", expr);
564+
nr_php_zval_free(&expr);
565+
nr_php_zval_free(&array_arg);
566+
567+
/* Test invalid arg 3 */
568+
args = "array()";
569+
array_arg = tlib_php_request_eval_expr(args);
570+
expr = nr_php_call(NULL, "lambda_invoke", expect_arg, array_arg);
571+
tlib_pass_if_not_null("Expression should evaluate.", expr);
572+
nr_php_zval_free(&expr);
573+
nr_php_zval_free(&expect_arg);
574+
nr_php_zval_free(&array_arg);
492575

493576
tlib_php_request_end();
494577
tlib_php_engine_destroy();
495578
}
579+
#endif /* PHP 8.1+ */
496580

497581
void test_main(void* p NRUNUSED) {
498582
tlib_php_engine_create("");

0 commit comments

Comments
 (0)