Skip to content

Commit 7b487bd

Browse files
authored
Fix compatilibity with PHP 5 (#2939)
Previous build could be loaded with php:5.6 docker image and I believe on centos 7 also. But on ubuntu:22.04, there were issues like "undefined symbol: __zend_malloc". This PR fixes this.
1 parent d47c723 commit 7b487bd

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

loader/compat_php.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#define PHP_70_71_72_IS_STR_INTERNED (1 << 1)
1010

1111
ZEND_API zval *ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *b, zend_string *key) __attribute__((weak));
12-
1312
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData) __attribute__((weak));
1413
ZEND_API zval *ZEND_FASTCALL _zend_hash_update(HashTable *ht, zend_string *key, zval *pData) __attribute__((weak));
14+
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len) __attribute__((weak));
15+
ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len) __attribute__((weak));
16+
ZEND_API void * __zend_malloc(size_t len) __attribute__((weak));
1517

1618
static bool ddloader_zstr_is_interned(int php_api_no, zend_string *key) {
1719
if (php_api_no <= 20170718) { // PHP 7.0 - 7.2
@@ -66,6 +68,29 @@ void ddloader_zend_string_release(int php_api_no, zend_string *s) {
6668
zend_string_release(s);
6769
}
6870

71+
void *ddloader_zend_hash_str_find_ptr(int php_api_no, const HashTable *ht, const char *str, size_t len) {
72+
UNUSED(php_api_no);
73+
zval *zv;
74+
75+
if (!zend_hash_str_find) {
76+
return NULL;
77+
}
78+
79+
zv = zend_hash_str_find(ht, str, len);
80+
if (zv) {
81+
return Z_PTR_P(zv);
82+
} else {
83+
return NULL;
84+
}
85+
}
86+
87+
void ddloader_zend_hash_str_del(int php_api_no, HashTable *ht, const char *str, size_t len) {
88+
UNUSED(php_api_no);
89+
if (zend_hash_str_del) {
90+
zend_hash_str_del(ht, str, len);
91+
}
92+
}
93+
6994
// This is an adaptation of zend_hash_set_bucket_key which is only available only starting from PHP 7.4
7095
// to be compatible with PHP 7.0+
7196
zval *ddloader_zend_hash_set_bucket_key(int php_api_no, HashTable *ht, Bucket *b, zend_string *key) {

loader/compat_php.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ zend_string *ddloader_zend_string_alloc(int php_api_no, size_t len, int persiste
77
zend_string *ddloader_zend_string_init(int php_api_no, const char *str, size_t len, bool persistent);
88
void ddloader_zend_string_release(int php_api_no, zend_string *s);
99
zval *ddloader_zend_hash_set_bucket_key(int php_api_no, HashTable *ht, Bucket *b, zend_string *key);
10+
void *ddloader_zend_hash_str_find_ptr(int php_api_no, const HashTable *ht, const char *str, size_t len);
11+
void ddloader_zend_hash_str_del(int php_api_no, HashTable *ht, const char *str, size_t len);
1012
void ddloader_replace_zend_error_cb(int php_api_no);
1113
void ddloader_restore_zend_error_cb();
1214
zval *ddloader_zend_hash_update(HashTable *ht, zend_string *key, zval *pData);

loader/dd_library_loader.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static char *ddtrace_pre_load_hook(void) {
6464
}
6565

6666
static bool ddloader_is_ext_loaded(const char *name) {
67-
return zend_hash_str_find_ptr(&module_registry, name, strlen(name))
67+
return ddloader_zend_hash_str_find_ptr(php_api_no, &module_registry, name, strlen(name))
6868
|| zend_get_extension(name)
6969
;
7070
}
@@ -390,14 +390,14 @@ static bool ddloader_check_deps(const zend_module_dep *deps) {
390390
}
391391

392392
static void ddloader_unregister_module(const char *name) {
393-
zend_module_entry *injected = zend_hash_str_find_ptr(&module_registry, name, strlen(name));
393+
zend_module_entry *injected = ddloader_zend_hash_str_find_ptr(php_api_no, &module_registry, name, strlen(name));
394394
if (!injected) {
395395
return;
396396
}
397397

398398
// Set the MSHUTDOWN function to NULL to avoid it being called by zend_hash_str_del
399399
injected->module_shutdown_func = NULL;
400-
zend_hash_str_del(&module_registry, name, strlen(name));
400+
ddloader_zend_hash_str_del(php_api_no, &module_registry, name, strlen(name));
401401
}
402402

403403
static PHP_MINIT_FUNCTION(ddloader_injected_extension_minit) {
@@ -414,7 +414,7 @@ static PHP_MINIT_FUNCTION(ddloader_injected_extension_minit) {
414414
return SUCCESS;
415415
}
416416

417-
zend_module_entry *module = zend_hash_str_find_ptr(&module_registry, config->ext_name, strlen(config->ext_name));
417+
zend_module_entry *module = ddloader_zend_hash_str_find_ptr(php_api_no, &module_registry, config->ext_name, strlen(config->ext_name));
418418
if (module) {
419419
LOG(INFO, "Extension '%s' is already loaded, unregister the injected extension", config->ext_name);
420420
ddloader_unregister_module(config->tmp_name);
@@ -446,7 +446,7 @@ static PHP_MINIT_FUNCTION(ddloader_injected_extension_minit) {
446446
ddloader_zend_hash_set_bucket_key(php_api_no, &module_registry, bucket, new_name);
447447
ddloader_zend_string_release(php_api_no, new_name);
448448

449-
module = zend_hash_str_find_ptr(&module_registry, config->ext_name, strlen(config->ext_name));
449+
module = ddloader_zend_hash_str_find_ptr(php_api_no, &module_registry, config->ext_name, strlen(config->ext_name));
450450
if (!module) {
451451
TELEMETRY(REASON_ERROR, "Extension '%s' not found after renaming. Something wrong happened", config->ext_name);
452452
return SUCCESS;

0 commit comments

Comments
 (0)