Skip to content

Commit a57fd9a

Browse files
committed
feat: first-class hybrid extensions
1 parent 031b4c6 commit a57fd9a

File tree

10 files changed

+217
-169
lines changed

10 files changed

+217
-169
lines changed

Zend/zend_extensions.c

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ zend_result zend_load_extension(const char *path)
6060
#endif
6161
}
6262

63-
zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path)
63+
zend_result zend_load_extension_handle_ex(DL_HANDLE handle, const char *path, bool unload_on_failure, bool silent)
6464
{
6565
#if ZEND_EXTENSIONS_SUPPORT
6666
zend_extension *new_extension;
@@ -74,63 +74,83 @@ zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path)
7474
new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
7575
}
7676
if (!extension_version_info || !new_extension) {
77-
fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
77+
if (!silent) {
78+
fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
79+
}
7880
/* See http://support.microsoft.com/kb/190351 */
7981
#ifdef ZEND_WIN32
8082
fflush(stderr);
8183
#endif
82-
DL_UNLOAD(handle);
84+
if (unload_on_failure) {
85+
DL_UNLOAD(handle);
86+
}
8387
return FAILURE;
8488
}
8589

8690
/* allow extension to proclaim compatibility with any Zend version */
8791
if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) {
8892
if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
89-
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
90-
"The Zend Engine API version %d which is installed, is outdated.\n\n",
91-
new_extension->name,
92-
extension_version_info->zend_extension_api_no,
93-
ZEND_EXTENSION_API_NO);
93+
if (!silent) {
94+
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
95+
"The Zend Engine API version %d which is installed, is outdated.\n\n",
96+
new_extension->name,
97+
extension_version_info->zend_extension_api_no,
98+
ZEND_EXTENSION_API_NO);
99+
}
94100
/* See http://support.microsoft.com/kb/190351 */
95101
#ifdef ZEND_WIN32
96102
fflush(stderr);
97103
#endif
98-
DL_UNLOAD(handle);
104+
if (unload_on_failure) {
105+
DL_UNLOAD(handle);
106+
}
99107
return FAILURE;
100108
} else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
101-
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
102-
"The Zend Engine API version %d which is installed, is newer.\n"
103-
"Contact %s at %s for a later version of %s.\n\n",
104-
new_extension->name,
105-
extension_version_info->zend_extension_api_no,
106-
ZEND_EXTENSION_API_NO,
107-
new_extension->author,
108-
new_extension->URL,
109-
new_extension->name);
109+
if (!silent) {
110+
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
111+
"The Zend Engine API version %d which is installed, is newer.\n"
112+
"Contact %s at %s for a later version of %s.\n\n",
113+
new_extension->name,
114+
extension_version_info->zend_extension_api_no,
115+
ZEND_EXTENSION_API_NO,
116+
new_extension->author,
117+
new_extension->URL,
118+
new_extension->name);
119+
}
110120
/* See http://support.microsoft.com/kb/190351 */
111121
#ifdef ZEND_WIN32
112122
fflush(stderr);
113123
#endif
114-
DL_UNLOAD(handle);
124+
if (unload_on_failure) {
125+
DL_UNLOAD(handle);
126+
}
115127
return FAILURE;
116128
}
117129
} else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) &&
118130
(!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
119-
fprintf(stderr, "Cannot load %s - it was built with configuration %s, whereas running engine is %s\n",
120-
new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
131+
if (!silent) {
132+
fprintf(stderr, "Cannot load %s - it was built with configuration %s, whereas running engine is %s\n",
133+
new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
134+
}
121135
/* See http://support.microsoft.com/kb/190351 */
122136
#ifdef ZEND_WIN32
123137
fflush(stderr);
124138
#endif
125-
DL_UNLOAD(handle);
139+
if (unload_on_failure) {
140+
DL_UNLOAD(handle);
141+
}
126142
return FAILURE;
127143
} else if (zend_get_extension(new_extension->name)) {
128-
fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
144+
if (!silent) {
145+
fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
146+
}
129147
/* See http://support.microsoft.com/kb/190351 */
130148
#ifdef ZEND_WIN32
131149
fflush(stderr);
132150
#endif
133-
DL_UNLOAD(handle);
151+
if (unload_on_failure) {
152+
DL_UNLOAD(handle);
153+
}
134154
return FAILURE;
135155
}
136156

@@ -146,6 +166,11 @@ zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path)
146166
#endif
147167
}
148168

169+
zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path)
170+
{
171+
return zend_load_extension_handle_ex(handle, path, true, false);
172+
}
173+
149174

150175
void zend_register_extension(zend_extension *new_extension, DL_HANDLE handle)
151176
{

Zend/zend_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ ZEND_API void zend_reset_internal_run_time_cache(void);
153153

154154
BEGIN_EXTERN_C()
155155
ZEND_API zend_result zend_load_extension(const char *path);
156+
ZEND_API zend_result zend_load_extension_handle_ex(DL_HANDLE handle, const char *path, bool unload_on_failure, bool silent);
156157
ZEND_API zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path);
157158
ZEND_API void zend_register_extension(zend_extension *new_extension, DL_HANDLE handle);
158159
ZEND_API zend_extension *zend_get_extension(const char *extension_name);

ext/opcache/ZendAccelerator.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ typedef int gid_t;
102102

103103
#include "zend_simd.h"
104104

105-
static zend_extension opcache_extension_entry;
106-
107105
#ifndef ZTS
108106
zend_accel_globals accel_globals;
109107
#else
@@ -2986,6 +2984,16 @@ static void accel_globals_dtor(zend_accel_globals *accel_globals)
29862984
}
29872985
}
29882986

2987+
void zend_accel_init_globals(void)
2988+
{
2989+
#ifdef ZTS
2990+
ts_allocate_fast_id(&accel_globals_id, &accel_globals_offset, sizeof(zend_accel_globals),
2991+
(ts_allocate_ctor) accel_globals_ctor, (ts_allocate_dtor) accel_globals_dtor);
2992+
#else
2993+
accel_globals_ctor(&accel_globals);
2994+
#endif
2995+
}
2996+
29892997
#ifdef HAVE_HUGE_CODE_PAGES
29902998
# ifndef _WIN32
29912999
# include <sys/mman.h>
@@ -3188,19 +3196,8 @@ static void accel_move_code_to_huge_pages(void)
31883196
# endif /* defined(MAP_HUGETLB) || defined(MADV_HUGEPAGE) */
31893197
#endif /* HAVE_HUGE_CODE_PAGES */
31903198

3191-
void start_accel_extension(void)
3192-
{
3193-
zend_register_extension(&opcache_extension_entry, NULL);
3194-
}
3195-
31963199
static int accel_startup(zend_extension *extension)
31973200
{
3198-
#ifdef ZTS
3199-
accel_globals_id = ts_allocate_fast_id(&accel_globals_id, &accel_globals_offset, sizeof(zend_accel_globals), (ts_allocate_ctor) accel_globals_ctor, (ts_allocate_dtor) accel_globals_dtor);
3200-
#else
3201-
accel_globals_ctor(&accel_globals);
3202-
#endif
3203-
32043201
#ifdef HAVE_JIT
32053202
zend_jit_init();
32063203
#endif
@@ -3211,8 +3208,6 @@ static int accel_startup(zend_extension *extension)
32113208
# endif
32123209
#endif
32133210

3214-
zend_accel_register_ini_entries();
3215-
32163211
#ifdef ZEND_WIN32
32173212
if (UNEXPECTED(accel_gen_uname_id() == FAILURE)) {
32183213
zps_startup_failure("Unable to get user name", NULL, accelerator_remove_cb);
@@ -5194,3 +5189,8 @@ static zend_extension opcache_extension_entry = {
51945189
NULL, /* op_array dtor */
51955190
STANDARD_ZEND_EXTENSION_PROPERTIES
51965191
};
5192+
5193+
zend_extension *zend_accel_get_extension_entry(void)
5194+
{
5195+
return &opcache_extension_entry;
5196+
}

ext/opcache/ZendAccelerator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ extern const char *zps_api_failure_reason;
313313

314314
BEGIN_EXTERN_C()
315315

316-
void start_accel_extension(void);
317316
void accel_shutdown(void);
317+
void zend_accel_init_globals(void);
318318
ZEND_RINIT_FUNCTION(zend_accelerator);
319319
zend_result accel_post_deactivate(void);
320320
void zend_accel_schedule_restart(zend_accel_restart_reason reason);
@@ -325,6 +325,7 @@ zend_result validate_timestamp_and_record_ex(zend_persistent_script *persistent_
325325
zend_result zend_accel_invalidate(zend_string *filename, bool force);
326326
zend_result accelerator_shm_read_lock(void);
327327
void accelerator_shm_read_unlock(void);
328+
zend_extension *zend_accel_get_extension_entry(void);
328329

329330
zend_string *accel_make_persistent_key(zend_string *path);
330331
zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);

ext/opcache/zend_accelerator_module.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,9 @@ static ZEND_NAMED_FUNCTION(accel_is_readable)
445445

446446
static ZEND_MINIT_FUNCTION(zend_accelerator)
447447
{
448-
start_accel_extension();
449-
450-
return SUCCESS;
451-
}
452-
453-
void zend_accel_register_ini_entries(void)
454-
{
455-
zend_module_entry *module = zend_hash_str_find_ptr_lc(&module_registry,
456-
ACCELERATOR_PRODUCT_NAME, strlen(ACCELERATOR_PRODUCT_NAME));
457-
458-
zend_register_ini_entries_ex(ini_entries, module->module_number, module->type);
448+
(void)type;
449+
zend_accel_init_globals();
450+
return REGISTER_INI_ENTRIES();
459451
}
460452

461453
void zend_accel_override_file_functions(void)

ext/opcache/zend_accelerator_module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#define phpext_opcache_ptr &opcache_module_entry
2828
extern zend_module_entry opcache_module_entry;
2929

30-
void zend_accel_register_ini_entries(void);
31-
3230
void zend_accel_override_file_functions(void);
3331

3432
#endif /* _ZEND_ACCELERATOR_MODULE_H */

0 commit comments

Comments
 (0)