Skip to content

Commit

Permalink
fix: guard against invalid internal handles too
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed Jan 7, 2025
1 parent 81c5f16 commit 3e1329c
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions profiling/src/php_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ zend_fiber* ddog_php_prof_get_active_fiber_test()
#endif

#if CFG_RUN_TIME_CACHE // defined by build.rs
static int ddog_php_prof_run_time_cache_handle = -1;
static int _user_run_time_cache_handle = -1;

// On PHP 8.4+, the internal cache slots need to be registered separately from
// the user ones.
#if PHP_VERSION_ID >= 80400
static int ddog_php_prof_internal_run_time_cache_handle = -1;
static int _internal_run_time_cache_handle = -1;
#endif

#endif
Expand All @@ -331,18 +331,18 @@ void ddog_php_prof_function_run_time_cache_init(const char *module_name) {
// Grab 1 slot for caching filename, as it turns out the utf-8 validity
// check is worth caching.
#if PHP_VERSION_ID < 80200
ddog_php_prof_run_time_cache_handle =
_user_run_time_cache_handle =
zend_get_op_array_extension_handle(module_name);
int second = zend_get_op_array_extension_handle(module_name);
ZEND_ASSERT(ddog_php_prof_run_time_cache_handle + 1 == second);
ZEND_ASSERT(_user_run_time_cache_handle + 1 == second);
#else
ddog_php_prof_run_time_cache_handle =
_user_run_time_cache_handle =
zend_get_op_array_extension_handles(module_name, 2);

#if PHP_VERSION_ID >= 80400
// On PHP 8.4+, the internal cache slots need to be registered separately
// from the user ones.
ddog_php_prof_internal_run_time_cache_handle =
_internal_run_time_cache_handle =
zend_get_op_array_extension_handles(module_name, 2);
#endif

Expand All @@ -361,7 +361,21 @@ void ddog_php_prof_function_run_time_cache_init(const char *module_name) {
// defined by build.rs
#if CFG_RUN_TIME_CACHE && !CFG_STACK_WALKING_TESTS
static bool has_invalid_run_time_cache(zend_function const *func) {
if (UNEXPECTED(_ignore_run_time_cache) || UNEXPECTED(ddog_php_prof_run_time_cache_handle < 0))
bool ignore_cache = _ignore_run_time_cache;
bool inv_user_handle = _user_run_time_cache_handle < 0;

// The bitwise-ors are intentional here. We don't expect any of these
// things to be true, except if we're on CLI and in that case it's okay
// to pessimize since it'll predict well after it gets it wrong the first
// time.
#if PHP_VERSION_ID < 80400
bool fast_skip = ignore_cache | inv_user_handle;
#else
bool inv_internal_handle = _internal_run_time_cache_handle < 0;
bool fast_skip = ignore_cache | inv_user_handle | inv_internal_handle;
#endif

if (UNEXPECTED(fast_skip))
return true;

// during an `include()`/`require()` with enabled OPcache, OPcache is
Expand Down Expand Up @@ -407,11 +421,11 @@ uintptr_t *ddog_php_prof_function_run_time_cache(zend_function const *func) {
ZEND_ASSERT(cache_addr);

#if PHP_VERSION_ID < 80400
int handle_offset = ddog_php_prof_run_time_cache_handle;
int handle_offset = _user_run_time_cache_handle;
#else
int handle_offset = func->type == ZEND_USER_FUNCTION
? ddog_php_prof_run_time_cache_handle
: ddog_php_prof_internal_run_time_cache_handle;
? _user_run_time_cache_handle
: _internal_run_time_cache_handle;
#endif
return cache_addr + handle_offset;

Expand Down

0 comments on commit 3e1329c

Please sign in to comment.