xref: /php-src/Zend/zend_extensions.c (revision 213248a0)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "zend_extensions.h"
21 #include "zend_system_id.h"
22 
23 ZEND_API zend_llist zend_extensions;
24 ZEND_API uint32_t zend_extension_flags = 0;
25 ZEND_API int zend_op_array_extension_handles = 0;
26 static int last_resource_number;
27 
zend_load_extension(const char * path)28 zend_result zend_load_extension(const char *path)
29 {
30 #if ZEND_EXTENSIONS_SUPPORT
31 	DL_HANDLE handle;
32 
33 	handle = DL_LOAD(path);
34 	if (!handle) {
35 #ifndef ZEND_WIN32
36 		fprintf(stderr, "Failed loading %s:  %s\n", path, DL_ERROR());
37 #else
38 		fprintf(stderr, "Failed loading %s\n", path);
39 		/* See http://support.microsoft.com/kb/190351 */
40 		fflush(stderr);
41 #endif
42 		return FAILURE;
43 	}
44 #ifdef ZEND_WIN32
45 	char *err;
46 	if (!php_win32_image_compatible(handle, &err)) {
47 		zend_error(E_CORE_WARNING, err);
48 		return FAILURE;
49 	}
50 #endif
51 	return zend_load_extension_handle(handle, path);
52 #else
53 	fprintf(stderr, "Extensions are not supported on this platform.\n");
54 /* See http://support.microsoft.com/kb/190351 */
55 #ifdef ZEND_WIN32
56 	fflush(stderr);
57 #endif
58 	return FAILURE;
59 #endif
60 }
61 
zend_load_extension_handle(DL_HANDLE handle,const char * path)62 zend_result zend_load_extension_handle(DL_HANDLE handle, const char *path)
63 {
64 #if ZEND_EXTENSIONS_SUPPORT
65 	zend_extension *new_extension;
66 
67 	const zend_extension_version_info *extension_version_info = (const zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
68 	if (!extension_version_info) {
69 		extension_version_info = (const zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
70 	}
71 	new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
72 	if (!new_extension) {
73 		new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
74 	}
75 	if (!extension_version_info || !new_extension) {
76 		fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
77 /* See http://support.microsoft.com/kb/190351 */
78 #ifdef ZEND_WIN32
79 		fflush(stderr);
80 #endif
81 		DL_UNLOAD(handle);
82 		return FAILURE;
83 	}
84 
85 	/* allow extension to proclaim compatibility with any Zend version */
86 	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)) {
87 		if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
88 			fprintf(stderr, "%s requires Zend Engine API version %d.\n"
89 					"The Zend Engine API version %d which is installed, is outdated.\n\n",
90 					new_extension->name,
91 					extension_version_info->zend_extension_api_no,
92 					ZEND_EXTENSION_API_NO);
93 /* See http://support.microsoft.com/kb/190351 */
94 #ifdef ZEND_WIN32
95 			fflush(stderr);
96 #endif
97 			DL_UNLOAD(handle);
98 			return FAILURE;
99 		} else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
100 			fprintf(stderr, "%s requires Zend Engine API version %d.\n"
101 					"The Zend Engine API version %d which is installed, is newer.\n"
102 					"Contact %s at %s for a later version of %s.\n\n",
103 					new_extension->name,
104 					extension_version_info->zend_extension_api_no,
105 					ZEND_EXTENSION_API_NO,
106 					new_extension->author,
107 					new_extension->URL,
108 					new_extension->name);
109 /* See http://support.microsoft.com/kb/190351 */
110 #ifdef ZEND_WIN32
111 			fflush(stderr);
112 #endif
113 			DL_UNLOAD(handle);
114 			return FAILURE;
115 		}
116 	} else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) &&
117 	           (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
118 		fprintf(stderr, "Cannot load %s - it was built with configuration %s, whereas running engine is %s\n",
119 					new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
120 /* See http://support.microsoft.com/kb/190351 */
121 #ifdef ZEND_WIN32
122 		fflush(stderr);
123 #endif
124 		DL_UNLOAD(handle);
125 		return FAILURE;
126 	} else if (zend_get_extension(new_extension->name)) {
127 		fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
128 /* See http://support.microsoft.com/kb/190351 */
129 #ifdef ZEND_WIN32
130 		fflush(stderr);
131 #endif
132 		DL_UNLOAD(handle);
133 		return FAILURE;
134 	}
135 
136 	zend_register_extension(new_extension, handle);
137 	return SUCCESS;
138 #else
139 	fprintf(stderr, "Extensions are not supported on this platform.\n");
140 /* See http://support.microsoft.com/kb/190351 */
141 #ifdef ZEND_WIN32
142 	fflush(stderr);
143 #endif
144 	return FAILURE;
145 #endif
146 }
147 
148 
zend_register_extension(zend_extension * new_extension,DL_HANDLE handle)149 void zend_register_extension(zend_extension *new_extension, DL_HANDLE handle)
150 {
151 #if ZEND_EXTENSIONS_SUPPORT
152 	zend_extension extension;
153 
154 	extension = *new_extension;
155 	extension.handle = handle;
156 
157 	zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension);
158 
159 	zend_llist_add_element(&zend_extensions, &extension);
160 
161 	if (extension.op_array_ctor) {
162 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_CTOR;
163 	}
164 	if (extension.op_array_dtor) {
165 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR;
166 	}
167 	if (extension.op_array_handler) {
168 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER;
169 	}
170 	if (extension.op_array_persist_calc) {
171 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC;
172 	}
173 	if (extension.op_array_persist) {
174 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST;
175 	}
176 	/*fprintf(stderr, "Loaded %s, version %s\n", extension.name, extension.version);*/
177 #endif
178 }
179 
180 
zend_extension_shutdown(zend_extension * extension)181 static void zend_extension_shutdown(zend_extension *extension)
182 {
183 #if ZEND_EXTENSIONS_SUPPORT
184 	if (extension->shutdown) {
185 		extension->shutdown(extension);
186 	}
187 #endif
188 }
189 
190 /* int return due to zend linked list API */
zend_extension_startup(zend_extension * extension)191 static int zend_extension_startup(zend_extension *extension)
192 {
193 #if ZEND_EXTENSIONS_SUPPORT
194 	if (extension->startup) {
195 		if (extension->startup(extension)!=SUCCESS) {
196 			return 1;
197 		}
198 		zend_append_version_info(extension);
199 	}
200 #endif
201 	return 0;
202 }
203 
204 
zend_startup_extensions_mechanism(void)205 void zend_startup_extensions_mechanism(void)
206 {
207 	/* Startup extensions mechanism */
208 	zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);
209 	zend_op_array_extension_handles = 0;
210 	last_resource_number = 0;
211 }
212 
213 
zend_startup_extensions(void)214 void zend_startup_extensions(void)
215 {
216 	zend_llist_apply_with_del(&zend_extensions, (int (*)(void *)) zend_extension_startup);
217 }
218 
219 
zend_shutdown_extensions(void)220 void zend_shutdown_extensions(void)
221 {
222 	zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_shutdown);
223 	zend_llist_destroy(&zend_extensions);
224 }
225 
226 
zend_extension_dtor(zend_extension * extension)227 void zend_extension_dtor(zend_extension *extension)
228 {
229 #if ZEND_EXTENSIONS_SUPPORT && !ZEND_DEBUG
230 	if (extension->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
231 		DL_UNLOAD(extension->handle);
232 	}
233 #endif
234 }
235 
236 
zend_extension_message_dispatcher(const zend_extension * extension,int num_args,va_list args)237 static void zend_extension_message_dispatcher(const zend_extension *extension, int num_args, va_list args)
238 {
239 	int message;
240 	void *arg;
241 
242 	if (!extension->message_handler || num_args!=2) {
243 		return;
244 	}
245 	message = va_arg(args, int);
246 	arg = va_arg(args, void *);
247 	extension->message_handler(message, arg);
248 }
249 
250 
zend_extension_dispatch_message(int message,void * arg)251 ZEND_API void zend_extension_dispatch_message(int message, void *arg)
252 {
253 	zend_llist_apply_with_arguments(&zend_extensions, (llist_apply_with_args_func_t) zend_extension_message_dispatcher, 2, message, arg);
254 }
255 
256 
zend_get_resource_handle(const char * module_name)257 ZEND_API int zend_get_resource_handle(const char *module_name)
258 {
259 	if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
260 		zend_add_system_entropy(module_name, "zend_get_resource_handle", &last_resource_number, sizeof(int));
261 		return last_resource_number++;
262 	} else {
263 		return -1;
264 	}
265 }
266 
267 /**
268  * The handle returned by this function can be used with
269  * `ZEND_OP_ARRAY_EXTENSION(op_array, handle)`.
270  *
271  * The extension slot has been available since PHP 7.4 on user functions and
272  * has been available since PHP 8.2 on internal functions.
273  *
274  * # Safety
275  * The extension slot made available by calling this function is initialized on
276  * the first call made to the function in that request. If you need to
277  * initialize it before this point, call `zend_init_func_run_time_cache`.
278  *
279  * The function cache slots are not available if the function is a trampoline,
280  * which can be checked with something like:
281  *
282  *     if (fbc->type == ZEND_USER_FUNCTION
283  *         && !(fbc->op_array.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)
284  *     ) {
285  *         // Use ZEND_OP_ARRAY_EXTENSION somehow
286  *     }
287  */
zend_get_op_array_extension_handle(const char * module_name)288 ZEND_API int zend_get_op_array_extension_handle(const char *module_name)
289 {
290 	int handle = zend_op_array_extension_handles++;
291 	zend_add_system_entropy(module_name, "zend_get_op_array_extension_handle", &zend_op_array_extension_handles, sizeof(int));
292 	return handle;
293 }
294 
295 /** See zend_get_op_array_extension_handle for important usage information. */
zend_get_op_array_extension_handles(const char * module_name,int handles)296 ZEND_API int zend_get_op_array_extension_handles(const char *module_name, int handles)
297 {
298 	int handle = zend_op_array_extension_handles;
299 	zend_op_array_extension_handles += handles;
300 	zend_add_system_entropy(module_name, "zend_get_op_array_extension_handle", &zend_op_array_extension_handles, sizeof(int));
301 	return handle;
302 }
303 
zend_internal_run_time_cache_reserved_size(void)304 ZEND_API size_t zend_internal_run_time_cache_reserved_size(void) {
305 	return zend_op_array_extension_handles * sizeof(void *);
306 }
307 
zend_init_internal_run_time_cache(void)308 ZEND_API void zend_init_internal_run_time_cache(void) {
309 	size_t rt_size = zend_internal_run_time_cache_reserved_size();
310 	if (rt_size) {
311 		size_t functions = zend_hash_num_elements(CG(function_table));
312 		zend_class_entry *ce;
313 		ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
314 			functions += zend_hash_num_elements(&ce->function_table);
315 		} ZEND_HASH_FOREACH_END();
316 
317 		char *ptr = zend_arena_calloc(&CG(arena), functions, rt_size);
318 		zend_internal_function *zif;
319 		ZEND_HASH_MAP_FOREACH_PTR(CG(function_table), zif) {
320 			if (!ZEND_USER_CODE(zif->type) && ZEND_MAP_PTR_GET(zif->run_time_cache) == NULL)
321 			{
322 				ZEND_MAP_PTR_SET(zif->run_time_cache, (void *)ptr);
323 				ptr += rt_size;
324 			}
325 		} ZEND_HASH_FOREACH_END();
326 		ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
327 			ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, zif) {
328 				if (!ZEND_USER_CODE(zif->type) && ZEND_MAP_PTR_GET(zif->run_time_cache) == NULL)
329 				{
330 					ZEND_MAP_PTR_SET(zif->run_time_cache, (void *)ptr);
331 					ptr += rt_size;
332 				}
333 			} ZEND_HASH_FOREACH_END();
334 		} ZEND_HASH_FOREACH_END();
335 	}
336 }
337 
zend_get_extension(const char * extension_name)338 ZEND_API zend_extension *zend_get_extension(const char *extension_name)
339 {
340 	zend_llist_element *element;
341 
342 	for (element = zend_extensions.head; element; element = element->next) {
343 		zend_extension *extension = (zend_extension *) element->data;
344 
345 		if (!strcmp(extension->name, extension_name)) {
346 			return extension;
347 		}
348 	}
349 	return NULL;
350 }
351 
352 typedef struct _zend_extension_persist_data {
353 	zend_op_array *op_array;
354 	size_t         size;
355 	char          *mem;
356 } zend_extension_persist_data;
357 
zend_extension_op_array_persist_calc_handler(zend_extension * extension,zend_extension_persist_data * data)358 static void zend_extension_op_array_persist_calc_handler(zend_extension *extension, zend_extension_persist_data *data)
359 {
360 	if (extension->op_array_persist_calc) {
361 		data->size += extension->op_array_persist_calc(data->op_array);
362 	}
363 }
364 
zend_extension_op_array_persist_handler(zend_extension * extension,zend_extension_persist_data * data)365 static void zend_extension_op_array_persist_handler(zend_extension *extension, zend_extension_persist_data *data)
366 {
367 	if (extension->op_array_persist) {
368 		size_t size = extension->op_array_persist(data->op_array, data->mem);
369 		if (size) {
370 			data->mem = (void*)((char*)data->mem + size);
371 			data->size += size;
372 		}
373 	}
374 }
375 
zend_extensions_op_array_persist_calc(zend_op_array * op_array)376 ZEND_API size_t zend_extensions_op_array_persist_calc(zend_op_array *op_array)
377 {
378 	if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC) {
379 		zend_extension_persist_data data;
380 
381 		data.op_array = op_array;
382 		data.size = 0;
383 		data.mem  = NULL;
384 		zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_calc_handler, &data);
385 		return data.size;
386 	}
387 	return 0;
388 }
389 
zend_extensions_op_array_persist(zend_op_array * op_array,void * mem)390 ZEND_API size_t zend_extensions_op_array_persist(zend_op_array *op_array, void *mem)
391 {
392 	if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST) {
393 		zend_extension_persist_data data;
394 
395 		data.op_array = op_array;
396 		data.size = 0;
397 		data.mem  = mem;
398 		zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_handler, &data);
399 		return data.size;
400 	}
401 	return 0;
402 }
403