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 zend_extension_version_info *extension_version_info;
67
68 extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
69 if (!extension_version_info) {
70 extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
71 }
72 new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
73 if (!new_extension) {
74 new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
75 }
76 if (!extension_version_info || !new_extension) {
77 fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
78 /* See http://support.microsoft.com/kb/190351 */
79 #ifdef ZEND_WIN32
80 fflush(stderr);
81 #endif
82 DL_UNLOAD(handle);
83 return FAILURE;
84 }
85
86 /* allow extension to proclaim compatibility with any Zend version */
87 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)) {
88 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);
94 /* See http://support.microsoft.com/kb/190351 */
95 #ifdef ZEND_WIN32
96 fflush(stderr);
97 #endif
98 DL_UNLOAD(handle);
99 return FAILURE;
100 } 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);
110 /* See http://support.microsoft.com/kb/190351 */
111 #ifdef ZEND_WIN32
112 fflush(stderr);
113 #endif
114 DL_UNLOAD(handle);
115 return FAILURE;
116 }
117 } else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) &&
118 (!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);
121 /* See http://support.microsoft.com/kb/190351 */
122 #ifdef ZEND_WIN32
123 fflush(stderr);
124 #endif
125 DL_UNLOAD(handle);
126 return FAILURE;
127 } else if (zend_get_extension(new_extension->name)) {
128 fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
129 /* See http://support.microsoft.com/kb/190351 */
130 #ifdef ZEND_WIN32
131 fflush(stderr);
132 #endif
133 DL_UNLOAD(handle);
134 return FAILURE;
135 }
136
137 zend_register_extension(new_extension, handle);
138 return SUCCESS;
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 void 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
181
zend_extension_shutdown(zend_extension * extension)182 static void zend_extension_shutdown(zend_extension *extension)
183 {
184 #if ZEND_EXTENSIONS_SUPPORT
185 if (extension->shutdown) {
186 extension->shutdown(extension);
187 }
188 #endif
189 }
190
191 /* int return due to zend linked list API */
zend_extension_startup(zend_extension * extension)192 static int zend_extension_startup(zend_extension *extension)
193 {
194 #if ZEND_EXTENSIONS_SUPPORT
195 if (extension->startup) {
196 if (extension->startup(extension)!=SUCCESS) {
197 return 1;
198 }
199 zend_append_version_info(extension);
200 }
201 #endif
202 return 0;
203 }
204
205
zend_startup_extensions_mechanism(void)206 void zend_startup_extensions_mechanism(void)
207 {
208 /* Startup extensions mechanism */
209 zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);
210 zend_op_array_extension_handles = 0;
211 last_resource_number = 0;
212 }
213
214
zend_startup_extensions(void)215 void zend_startup_extensions(void)
216 {
217 zend_llist_apply_with_del(&zend_extensions, (int (*)(void *)) zend_extension_startup);
218 }
219
220
zend_shutdown_extensions(void)221 void zend_shutdown_extensions(void)
222 {
223 zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_shutdown);
224 zend_llist_destroy(&zend_extensions);
225 }
226
227
zend_extension_dtor(zend_extension * extension)228 void zend_extension_dtor(zend_extension *extension)
229 {
230 #if ZEND_EXTENSIONS_SUPPORT && !ZEND_DEBUG
231 if (extension->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
232 DL_UNLOAD(extension->handle);
233 }
234 #endif
235 }
236
237
zend_extension_message_dispatcher(const zend_extension * extension,int num_args,va_list args)238 static void zend_extension_message_dispatcher(const zend_extension *extension, int num_args, va_list args)
239 {
240 int message;
241 void *arg;
242
243 if (!extension->message_handler || num_args!=2) {
244 return;
245 }
246 message = va_arg(args, int);
247 arg = va_arg(args, void *);
248 extension->message_handler(message, arg);
249 }
250
251
zend_extension_dispatch_message(int message,void * arg)252 ZEND_API void zend_extension_dispatch_message(int message, void *arg)
253 {
254 zend_llist_apply_with_arguments(&zend_extensions, (llist_apply_with_args_func_t) zend_extension_message_dispatcher, 2, message, arg);
255 }
256
257
zend_get_resource_handle(const char * module_name)258 ZEND_API int zend_get_resource_handle(const char *module_name)
259 {
260 if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
261 zend_add_system_entropy(module_name, "zend_get_resource_handle", &last_resource_number, sizeof(int));
262 return last_resource_number++;
263 } else {
264 return -1;
265 }
266 }
267
zend_get_op_array_extension_handle(const char * module_name)268 ZEND_API int zend_get_op_array_extension_handle(const char *module_name)
269 {
270 int handle = zend_op_array_extension_handles++;
271 zend_add_system_entropy(module_name, "zend_get_op_array_extension_handle", &zend_op_array_extension_handles, sizeof(int));
272 return handle;
273 }
274
zend_get_op_array_extension_handles(const char * module_name,int handles)275 ZEND_API int zend_get_op_array_extension_handles(const char *module_name, int handles)
276 {
277 int handle = zend_op_array_extension_handles;
278 zend_op_array_extension_handles += handles;
279 zend_add_system_entropy(module_name, "zend_get_op_array_extension_handle", &zend_op_array_extension_handles, sizeof(int));
280 return handle;
281 }
282
zend_internal_run_time_cache_reserved_size(void)283 ZEND_API size_t zend_internal_run_time_cache_reserved_size(void) {
284 return zend_op_array_extension_handles * sizeof(void *);
285 }
286
zend_init_internal_run_time_cache(void)287 ZEND_API void zend_init_internal_run_time_cache(void) {
288 size_t rt_size = zend_internal_run_time_cache_reserved_size();
289 if (rt_size) {
290 size_t functions = zend_hash_num_elements(CG(function_table));
291 zend_class_entry *ce;
292 ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
293 functions += zend_hash_num_elements(&ce->function_table);
294 } ZEND_HASH_FOREACH_END();
295
296 char *ptr = zend_arena_calloc(&CG(arena), functions, rt_size);
297 zend_internal_function *zif;
298 ZEND_HASH_MAP_FOREACH_PTR(CG(function_table), zif) {
299 if (!ZEND_USER_CODE(zif->type) && ZEND_MAP_PTR_GET(zif->run_time_cache) == NULL)
300 {
301 ZEND_MAP_PTR_SET(zif->run_time_cache, (void *)ptr);
302 ptr += rt_size;
303 }
304 } ZEND_HASH_FOREACH_END();
305 ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
306 ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, zif) {
307 if (!ZEND_USER_CODE(zif->type) && ZEND_MAP_PTR_GET(zif->run_time_cache) == NULL)
308 {
309 ZEND_MAP_PTR_SET(zif->run_time_cache, (void *)ptr);
310 ptr += rt_size;
311 }
312 } ZEND_HASH_FOREACH_END();
313 } ZEND_HASH_FOREACH_END();
314 }
315 }
316
zend_get_extension(const char * extension_name)317 ZEND_API zend_extension *zend_get_extension(const char *extension_name)
318 {
319 zend_llist_element *element;
320
321 for (element = zend_extensions.head; element; element = element->next) {
322 zend_extension *extension = (zend_extension *) element->data;
323
324 if (!strcmp(extension->name, extension_name)) {
325 return extension;
326 }
327 }
328 return NULL;
329 }
330
331 typedef struct _zend_extension_persist_data {
332 zend_op_array *op_array;
333 size_t size;
334 char *mem;
335 } zend_extension_persist_data;
336
zend_extension_op_array_persist_calc_handler(zend_extension * extension,zend_extension_persist_data * data)337 static void zend_extension_op_array_persist_calc_handler(zend_extension *extension, zend_extension_persist_data *data)
338 {
339 if (extension->op_array_persist_calc) {
340 data->size += extension->op_array_persist_calc(data->op_array);
341 }
342 }
343
zend_extension_op_array_persist_handler(zend_extension * extension,zend_extension_persist_data * data)344 static void zend_extension_op_array_persist_handler(zend_extension *extension, zend_extension_persist_data *data)
345 {
346 if (extension->op_array_persist) {
347 size_t size = extension->op_array_persist(data->op_array, data->mem);
348 if (size) {
349 data->mem = (void*)((char*)data->mem + size);
350 data->size += size;
351 }
352 }
353 }
354
zend_extensions_op_array_persist_calc(zend_op_array * op_array)355 ZEND_API size_t zend_extensions_op_array_persist_calc(zend_op_array *op_array)
356 {
357 if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC) {
358 zend_extension_persist_data data;
359
360 data.op_array = op_array;
361 data.size = 0;
362 data.mem = NULL;
363 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_calc_handler, &data);
364 return data.size;
365 }
366 return 0;
367 }
368
zend_extensions_op_array_persist(zend_op_array * op_array,void * mem)369 ZEND_API size_t zend_extensions_op_array_persist(zend_op_array *op_array, void *mem)
370 {
371 if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST) {
372 zend_extension_persist_data data;
373
374 data.op_array = op_array;
375 data.size = 0;
376 data.mem = mem;
377 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_handler, &data);
378 return data.size;
379 }
380 return 0;
381 }
382