Skip to content

Commit fa1f099

Browse files
Add tests for function name length
1 parent 6ffab61 commit fa1f099

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

profiling/src/php_ffi.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ uintptr_t *ddog_test_php_prof_function_run_time_cache(zend_function const *func)
390390
}
391391
#endif
392392

393-
#if CFG_STACK_WALKING_TESTS
393+
#if CFG_STACK_WALKING_TESTS || defined(CFG_TEST)
394394
static int (*og_snprintf)(char *, size_t, const char *, ...);
395395

396396
// "weak" let's us polyfill, needed by zend_string_init(..., persistent: 1).
@@ -463,7 +463,33 @@ void ddog_php_test_free_fake_zend_execute_data(zend_execute_data *execute_data)
463463

464464
free(execute_data);
465465
}
466-
#endif
466+
467+
zend_function *ddog_php_test_create_fake_zend_function_with_name_len(size_t len) {
468+
zend_op_array *op_array = calloc(1, sizeof(zend_function));
469+
if (!op_array) return NULL;
470+
471+
op_array->type = ZEND_USER_FUNCTION;
472+
473+
if (len > 0) {
474+
op_array->function_name = zend_string_alloc(len, true);
475+
if (!op_array->function_name) {
476+
free(op_array);
477+
return NULL;
478+
}
479+
memset(ZSTR_VAL(op_array->function_name), 'x', len);
480+
ZSTR_VAL(op_array->function_name)[len] = '\0';
481+
}
482+
483+
return (zend_function *)op_array;
484+
}
485+
486+
void ddog_php_test_free_fake_zend_function(zend_function *func) {
487+
if (!func) return;
488+
489+
free(func->common.function_name);
490+
free(func);
491+
}
492+
#endif // CFG_STACK_WALKING_TESTS || CFG_TEST
467493

468494
void *opcache_handle = NULL;
469495

profiling/src/profiling/stack_walking.rs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,14 +528,20 @@ mod detail {
528528

529529
pub use detail::*;
530530

531-
// todo: this should be feature = "stack_walking_tests" but it seemed to
532-
// cause a failure in CI to migrate it.
533-
#[cfg(all(test, stack_walking_tests))]
531+
#[cfg(test)]
534532
mod tests {
535533
use super::*;
536534
use crate::bindings as zend;
537535

536+
extern "C" {
537+
fn ddog_php_test_create_fake_zend_function_with_name_len(
538+
len: libc::size_t,
539+
) -> *mut zend::zend_function;
540+
fn ddog_php_test_free_fake_zend_function(func: *mut zend::zend_function);
541+
}
542+
538543
#[test]
544+
#[cfg(stack_walking_tests)]
539545
fn test_collect_stack_sample() {
540546
unsafe {
541547
let fake_execute_data = zend::ddog_php_test_create_fake_zend_execute_data(3);
@@ -560,4 +566,57 @@ mod tests {
560566
zend::ddog_php_test_free_fake_zend_execute_data(fake_execute_data);
561567
}
562568
}
569+
570+
#[test]
571+
fn test_extract_function_name_short_string() {
572+
unsafe {
573+
let func = ddog_php_test_create_fake_zend_function_with_name_len(10);
574+
assert!(!func.is_null());
575+
576+
let name = extract_function_name(&*func).expect("should extract name");
577+
assert_eq!(name, "xxxxxxxxxx");
578+
579+
ddog_php_test_free_fake_zend_function(func);
580+
}
581+
}
582+
583+
#[test]
584+
fn test_extract_function_name_at_limit_minus_one() {
585+
unsafe {
586+
let func = ddog_php_test_create_fake_zend_function_with_name_len(STR_LEN_LIMIT - 1);
587+
assert!(!func.is_null());
588+
589+
let name = extract_function_name(&*func).expect("should extract name");
590+
assert_eq!(name.len(), STR_LEN_LIMIT - 1);
591+
assert_ne!(name, COW_LARGE_STRING);
592+
593+
ddog_php_test_free_fake_zend_function(func);
594+
}
595+
}
596+
597+
#[test]
598+
fn test_extract_function_name_at_limit() {
599+
unsafe {
600+
let func = ddog_php_test_create_fake_zend_function_with_name_len(STR_LEN_LIMIT);
601+
assert!(!func.is_null());
602+
603+
let name = extract_function_name(&*func).expect("should return large string marker");
604+
assert_eq!(name, COW_LARGE_STRING);
605+
606+
ddog_php_test_free_fake_zend_function(func);
607+
}
608+
}
609+
610+
#[test]
611+
fn test_extract_function_name_over_limit() {
612+
unsafe {
613+
let func = ddog_php_test_create_fake_zend_function_with_name_len(STR_LEN_LIMIT + 1000);
614+
assert!(!func.is_null());
615+
616+
let name = extract_function_name(&*func).expect("should return large string marker");
617+
assert_eq!(name, COW_LARGE_STRING);
618+
619+
ddog_php_test_free_fake_zend_function(func);
620+
}
621+
}
563622
}

0 commit comments

Comments
 (0)