xref: /PHP-7.2/Zend/zend_extensions.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #include "zend_extensions.h"
23 
24 ZEND_API zend_llist zend_extensions;
25 ZEND_API uint32_t zend_extension_flags = 0;
26 static int last_resource_number;
27 
zend_load_extension(const char * path)28 int 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 	return zend_load_extension_handle(handle, path);
45 #else
46 	fprintf(stderr, "Extensions are not supported on this platform.\n");
47 /* See http://support.microsoft.com/kb/190351 */
48 #ifdef ZEND_WIN32
49 	fflush(stderr);
50 #endif
51 	return FAILURE;
52 #endif
53 }
54 
zend_load_extension_handle(DL_HANDLE handle,const char * path)55 int zend_load_extension_handle(DL_HANDLE handle, const char *path)
56 {
57 #if ZEND_EXTENSIONS_SUPPORT
58 	zend_extension *new_extension;
59 	zend_extension_version_info *extension_version_info;
60 
61 	extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
62 	if (!extension_version_info) {
63 		extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
64 	}
65 	new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
66 	if (!new_extension) {
67 		new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
68 	}
69 	if (!extension_version_info || !new_extension) {
70 		fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
71 /* See http://support.microsoft.com/kb/190351 */
72 #ifdef ZEND_WIN32
73 		fflush(stderr);
74 #endif
75 		DL_UNLOAD(handle);
76 		return FAILURE;
77 	}
78 
79 	/* allow extension to proclaim compatibility with any Zend version */
80 	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)) {
81 		if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
82 			fprintf(stderr, "%s requires Zend Engine API version %d.\n"
83 					"The Zend Engine API version %d which is installed, is outdated.\n\n",
84 					new_extension->name,
85 					extension_version_info->zend_extension_api_no,
86 					ZEND_EXTENSION_API_NO);
87 /* See http://support.microsoft.com/kb/190351 */
88 #ifdef ZEND_WIN32
89 			fflush(stderr);
90 #endif
91 			DL_UNLOAD(handle);
92 			return FAILURE;
93 		} else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
94 			fprintf(stderr, "%s requires Zend Engine API version %d.\n"
95 					"The Zend Engine API version %d which is installed, is newer.\n"
96 					"Contact %s at %s for a later version of %s.\n\n",
97 					new_extension->name,
98 					extension_version_info->zend_extension_api_no,
99 					ZEND_EXTENSION_API_NO,
100 					new_extension->author,
101 					new_extension->URL,
102 					new_extension->name);
103 /* See http://support.microsoft.com/kb/190351 */
104 #ifdef ZEND_WIN32
105 			fflush(stderr);
106 #endif
107 			DL_UNLOAD(handle);
108 			return FAILURE;
109 		}
110 	} else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) &&
111 	           (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
112 		fprintf(stderr, "Cannot load %s - it was built with configuration %s, whereas running engine is %s\n",
113 					new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
114 /* See http://support.microsoft.com/kb/190351 */
115 #ifdef ZEND_WIN32
116 		fflush(stderr);
117 #endif
118 		DL_UNLOAD(handle);
119 		return FAILURE;
120 	} else if (zend_get_extension(new_extension->name)) {
121 		fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
122 /* See http://support.microsoft.com/kb/190351 */
123 #ifdef ZEND_WIN32
124 		fflush(stderr);
125 #endif
126 		DL_UNLOAD(handle);
127 		return FAILURE;
128 	} else if (zend_get_extension(new_extension->name)) {
129 		fprintf(stderr, "Cannot load %s - extension already loaded\n", new_extension->name);
130 /* See http://support.microsoft.com/kb/190351 */
131 #ifdef PHP_WIN32
132 		fflush(stderr);
133 #endif
134 		DL_UNLOAD(handle);
135 		return FAILURE;
136 	}
137 
138 	return zend_register_extension(new_extension, handle);
139 #else
140 	fprintf(stderr, "Extensions are not supported on this platform.\n");
141 /* See http://support.microsoft.com/kb/190351 */
142 #ifdef ZEND_WIN32
143 	fflush(stderr);
144 #endif
145 	return FAILURE;
146 #endif
147 }
148 
149 
zend_register_extension(zend_extension * new_extension,DL_HANDLE handle)150 int zend_register_extension(zend_extension *new_extension, DL_HANDLE handle)
151 {
152 #if ZEND_EXTENSIONS_SUPPORT
153 	zend_extension extension;
154 
155 	extension = *new_extension;
156 	extension.handle = handle;
157 
158 	zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension);
159 
160 	zend_llist_add_element(&zend_extensions, &extension);
161 
162 	if (extension.op_array_ctor) {
163 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_CTOR;
164 	}
165 	if (extension.op_array_dtor) {
166 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR;
167 	}
168 	if (extension.op_array_handler) {
169 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER;
170 	}
171 	if (extension.op_array_persist_calc) {
172 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC;
173 	}
174 	if (extension.op_array_persist) {
175 		zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST;
176 	}
177 	/*fprintf(stderr, "Loaded %s, version %s\n", extension.name, extension.version);*/
178 #endif
179 
180 	return SUCCESS;
181 }
182 
183 
zend_extension_shutdown(zend_extension * extension)184 static void zend_extension_shutdown(zend_extension *extension)
185 {
186 #if ZEND_EXTENSIONS_SUPPORT
187 	if (extension->shutdown) {
188 		extension->shutdown(extension);
189 	}
190 #endif
191 }
192 
zend_extension_startup(zend_extension * extension)193 static int zend_extension_startup(zend_extension *extension)
194 {
195 #if ZEND_EXTENSIONS_SUPPORT
196 	if (extension->startup) {
197 		if (extension->startup(extension)!=SUCCESS) {
198 			return 1;
199 		}
200 		zend_append_version_info(extension);
201 	}
202 #endif
203 	return 0;
204 }
205 
206 
zend_startup_extensions_mechanism()207 int zend_startup_extensions_mechanism()
208 {
209 	/* Startup extensions mechanism */
210 	zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);
211 	last_resource_number = 0;
212 	return SUCCESS;
213 }
214 
215 
zend_startup_extensions()216 int zend_startup_extensions()
217 {
218 	zend_llist_apply_with_del(&zend_extensions, (int (*)(void *)) zend_extension_startup);
219 	return SUCCESS;
220 }
221 
222 
zend_shutdown_extensions(void)223 void zend_shutdown_extensions(void)
224 {
225 	zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_shutdown);
226 	zend_llist_destroy(&zend_extensions);
227 }
228 
229 
zend_extension_dtor(zend_extension * extension)230 void zend_extension_dtor(zend_extension *extension)
231 {
232 #if ZEND_EXTENSIONS_SUPPORT && !ZEND_DEBUG
233 	if (extension->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
234 		DL_UNLOAD(extension->handle);
235 	}
236 #endif
237 }
238 
239 
zend_extension_message_dispatcher(const zend_extension * extension,int num_args,va_list args)240 static void zend_extension_message_dispatcher(const zend_extension *extension, int num_args, va_list args)
241 {
242 	int message;
243 	void *arg;
244 
245 	if (!extension->message_handler || num_args!=2) {
246 		return;
247 	}
248 	message = va_arg(args, int);
249 	arg = va_arg(args, void *);
250 	extension->message_handler(message, arg);
251 }
252 
253 
zend_extension_dispatch_message(int message,void * arg)254 ZEND_API void zend_extension_dispatch_message(int message, void *arg)
255 {
256 	zend_llist_apply_with_arguments(&zend_extensions, (llist_apply_with_args_func_t) zend_extension_message_dispatcher, 2, message, arg);
257 }
258 
259 
zend_get_resource_handle(zend_extension * extension)260 ZEND_API int zend_get_resource_handle(zend_extension *extension)
261 {
262 	if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
263 		extension->resource_number = last_resource_number;
264 		return last_resource_number++;
265 	} else {
266 		return -1;
267 	}
268 }
269 
270 
zend_get_extension(const char * extension_name)271 ZEND_API zend_extension *zend_get_extension(const char *extension_name)
272 {
273 	zend_llist_element *element;
274 
275 	for (element = zend_extensions.head; element; element = element->next) {
276 		zend_extension *extension = (zend_extension *) element->data;
277 
278 		if (!strcmp(extension->name, extension_name)) {
279 			return extension;
280 		}
281 	}
282 	return NULL;
283 }
284 
285 typedef struct _zend_extension_persist_data {
286 	zend_op_array *op_array;
287 	size_t         size;
288 	char          *mem;
289 } zend_extension_persist_data;
290 
zend_extension_op_array_persist_calc_handler(zend_extension * extension,zend_extension_persist_data * data)291 static void zend_extension_op_array_persist_calc_handler(zend_extension *extension, zend_extension_persist_data *data)
292 {
293 	if (extension->op_array_persist_calc) {
294 		data->size += extension->op_array_persist_calc(data->op_array);
295 	}
296 }
297 
zend_extension_op_array_persist_handler(zend_extension * extension,zend_extension_persist_data * data)298 static void zend_extension_op_array_persist_handler(zend_extension *extension, zend_extension_persist_data *data)
299 {
300 	if (extension->op_array_persist) {
301 		size_t size = extension->op_array_persist(data->op_array, data->mem);
302 		if (size) {
303 			data->mem = (void*)((char*)data->mem + size);
304 			data->size += size;
305 		}
306 	}
307 }
308 
zend_extensions_op_array_persist_calc(zend_op_array * op_array)309 ZEND_API size_t zend_extensions_op_array_persist_calc(zend_op_array *op_array)
310 {
311 	if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC) {
312 		zend_extension_persist_data data;
313 
314 		data.op_array = op_array;
315 		data.size = 0;
316 		data.mem  = NULL;
317 		zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_calc_handler, &data);
318 		return data.size;
319 	}
320 	return 0;
321 }
322 
zend_extensions_op_array_persist(zend_op_array * op_array,void * mem)323 ZEND_API size_t zend_extensions_op_array_persist(zend_op_array *op_array, void *mem)
324 {
325 	if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST) {
326 		zend_extension_persist_data data;
327 
328 		data.op_array = op_array;
329 		data.size = 0;
330 		data.mem  = mem;
331 		zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_handler, &data);
332 		return data.size;
333 	}
334 	return 0;
335 }
336 
337 /*
338  * Local variables:
339  * tab-width: 4
340  * c-basic-offset: 4
341  * indent-tabs-mode: t
342  * End:
343  * vim600: sw=4 ts=4 fdm=marker
344  * vim<600: sw=4 ts=4
345  */
346