xref: /PHP-7.3/Zend/zend_list.c (revision 9afce019)
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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* resource lists */
21 
22 #include "zend.h"
23 #include "zend_list.h"
24 #include "zend_API.h"
25 #include "zend_globals.h"
26 
27 ZEND_API int le_index_ptr;
28 
29 /* true global */
30 static HashTable list_destructors;
31 
zend_list_insert(void * ptr,int type)32 ZEND_API zval* ZEND_FASTCALL zend_list_insert(void *ptr, int type)
33 {
34 	int index;
35 	zval zv;
36 
37 	index = zend_hash_next_free_element(&EG(regular_list));
38 	if (index == 0) {
39 		index = 1;
40 	}
41 	ZVAL_NEW_RES(&zv, index, ptr, type);
42 	return zend_hash_index_add_new(&EG(regular_list), index, &zv);
43 }
44 
zend_list_delete(zend_resource * res)45 ZEND_API int ZEND_FASTCALL zend_list_delete(zend_resource *res)
46 {
47 	if (GC_DELREF(res) <= 0) {
48 		return zend_hash_index_del(&EG(regular_list), res->handle);
49 	} else {
50 		return SUCCESS;
51 	}
52 }
53 
zend_list_free(zend_resource * res)54 ZEND_API int ZEND_FASTCALL zend_list_free(zend_resource *res)
55 {
56 	if (GC_REFCOUNT(res) <= 0) {
57 		return zend_hash_index_del(&EG(regular_list), res->handle);
58 	} else {
59 		return SUCCESS;
60 	}
61 }
62 
zend_resource_dtor(zend_resource * res)63 static void zend_resource_dtor(zend_resource *res)
64 {
65 	zend_rsrc_list_dtors_entry *ld;
66 	zend_resource r = *res;
67 
68 	res->type = -1;
69 	res->ptr = NULL;
70 
71 	ld = zend_hash_index_find_ptr(&list_destructors, r.type);
72 	if (ld) {
73 		if (ld->list_dtor_ex) {
74 			ld->list_dtor_ex(&r);
75 		}
76 	} else {
77 		zend_error(E_WARNING, "Unknown list entry type (%d)", r.type);
78 	}
79 }
80 
81 
zend_list_close(zend_resource * res)82 ZEND_API int ZEND_FASTCALL zend_list_close(zend_resource *res)
83 {
84 	if (GC_REFCOUNT(res) <= 0) {
85 		return zend_list_free(res);
86 	} else if (res->type >= 0) {
87 		zend_resource_dtor(res);
88 	}
89 	return SUCCESS;
90 }
91 
zend_register_resource(void * rsrc_pointer,int rsrc_type)92 ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
93 {
94 	zval *zv;
95 
96 	zv = zend_list_insert(rsrc_pointer, rsrc_type);
97 
98 	return Z_RES_P(zv);
99 }
100 
zend_fetch_resource2(zend_resource * res,const char * resource_type_name,int resource_type1,int resource_type2)101 ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
102 {
103 	if (res) {
104 		if (resource_type1 == res->type) {
105 			return res->ptr;
106 		}
107 
108 		if (resource_type2 == res->type) {
109 			return res->ptr;
110 		}
111 	}
112 
113 	if (resource_type_name) {
114 		const char *space;
115 		const char *class_name = get_active_class_name(&space);
116 		zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
117 	}
118 
119 	return NULL;
120 }
121 
zend_fetch_resource(zend_resource * res,const char * resource_type_name,int resource_type)122 ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
123 {
124 	if (resource_type == res->type) {
125 		return res->ptr;
126 	}
127 
128 	if (resource_type_name) {
129 		const char *space;
130 		const char *class_name = get_active_class_name(&space);
131 		zend_error(E_WARNING, "%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
132 	}
133 
134 	return NULL;
135 }
136 
zend_fetch_resource_ex(zval * res,const char * resource_type_name,int resource_type)137 ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
138 {
139 	const char *space, *class_name;
140 	if (res == NULL) {
141 		if (resource_type_name) {
142 			class_name = get_active_class_name(&space);
143 			zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
144 		}
145 		return NULL;
146 	}
147 	if (Z_TYPE_P(res) != IS_RESOURCE) {
148 		if (resource_type_name) {
149 			class_name = get_active_class_name(&space);
150 			zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
151 		}
152 		return NULL;
153 	}
154 
155 	return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
156 }
157 
zend_fetch_resource2_ex(zval * res,const char * resource_type_name,int resource_type1,int resource_type2)158 ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
159 {
160 	const char *space, *class_name;
161 	if (res == NULL) {
162 		if (resource_type_name) {
163 			class_name = get_active_class_name(&space);
164 			zend_error(E_WARNING, "%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
165 		}
166 		return NULL;
167 	}
168 	if (Z_TYPE_P(res) != IS_RESOURCE) {
169 		if (resource_type_name) {
170 			class_name = get_active_class_name(&space);
171 			zend_error(E_WARNING, "%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
172 		}
173 		return NULL;
174 	}
175 
176 	return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
177 }
178 
list_entry_destructor(zval * zv)179 void list_entry_destructor(zval *zv)
180 {
181 	zend_resource *res = Z_RES_P(zv);
182 
183 	ZVAL_UNDEF(zv);
184 	if (res->type >= 0) {
185 		zend_resource_dtor(res);
186 	}
187 	efree_size(res, sizeof(zend_resource));
188 }
189 
plist_entry_destructor(zval * zv)190 void plist_entry_destructor(zval *zv)
191 {
192 	zend_resource *res = Z_RES_P(zv);
193 
194 	if (res->type >= 0) {
195 		zend_rsrc_list_dtors_entry *ld;
196 
197 		ld = zend_hash_index_find_ptr(&list_destructors, res->type);
198 		if (ld) {
199 			if (ld->plist_dtor_ex) {
200 				ld->plist_dtor_ex(res);
201 			}
202 		} else {
203 			zend_error(E_WARNING,"Unknown list entry type (%d)", res->type);
204 		}
205 	}
206 	free(res);
207 }
208 
zend_init_rsrc_list(void)209 int zend_init_rsrc_list(void)
210 {
211 	zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
212 	return SUCCESS;
213 }
214 
215 
zend_init_rsrc_plist(void)216 int zend_init_rsrc_plist(void)
217 {
218 	zend_hash_init_ex(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1, 0);
219 	return SUCCESS;
220 }
221 
222 
zend_close_rsrc(zval * zv)223 static int zend_close_rsrc(zval *zv)
224 {
225 	zend_resource *res = Z_PTR_P(zv);
226 
227 	if (res->type >= 0) {
228 		zend_resource_dtor(res);
229 	}
230 	return ZEND_HASH_APPLY_KEEP;
231 }
232 
233 
zend_close_rsrc_list(HashTable * ht)234 void zend_close_rsrc_list(HashTable *ht)
235 {
236 	zend_hash_reverse_apply(ht, zend_close_rsrc);
237 }
238 
239 
zend_destroy_rsrc_list(HashTable * ht)240 void zend_destroy_rsrc_list(HashTable *ht)
241 {
242 	zend_hash_graceful_reverse_destroy(ht);
243 }
244 
clean_module_resource(zval * zv,void * arg)245 static int clean_module_resource(zval *zv, void *arg)
246 {
247 	int resource_id = *(int *)arg;
248 	if (Z_RES_TYPE_P(zv) == resource_id) {
249 		return 1;
250 	} else {
251 		return 0;
252 	}
253 }
254 
255 
zend_clean_module_rsrc_dtors_cb(zval * zv,void * arg)256 static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
257 {
258 	zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
259 	int module_number = *(int *)arg;
260 	if (ld->module_number == module_number) {
261 		zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
262 		return 1;
263 	} else {
264 		return 0;
265 	}
266 }
267 
268 
zend_clean_module_rsrc_dtors(int module_number)269 void zend_clean_module_rsrc_dtors(int module_number)
270 {
271 	zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
272 }
273 
274 
zend_register_list_destructors_ex(rsrc_dtor_func_t ld,rsrc_dtor_func_t pld,const char * type_name,int module_number)275 ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
276 {
277 	zend_rsrc_list_dtors_entry *lde;
278 	zval zv;
279 
280 	lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
281 	lde->list_dtor_ex = ld;
282 	lde->plist_dtor_ex = pld;
283 	lde->module_number = module_number;
284 	lde->resource_id = list_destructors.nNextFreeElement;
285 	lde->type_name = type_name;
286 	ZVAL_PTR(&zv, lde);
287 
288 	if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
289 		return FAILURE;
290 	}
291 	return list_destructors.nNextFreeElement-1;
292 }
293 
zend_fetch_list_dtor_id(const char * type_name)294 ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
295 {
296 	zend_rsrc_list_dtors_entry *lde;
297 
298 	ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
299 		if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
300 			return lde->resource_id;
301 		}
302 	} ZEND_HASH_FOREACH_END();
303 
304 	return 0;
305 }
306 
list_destructors_dtor(zval * zv)307 static void list_destructors_dtor(zval *zv)
308 {
309 	free(Z_PTR_P(zv));
310 }
311 
zend_init_rsrc_list_dtors(void)312 int zend_init_rsrc_list_dtors(void)
313 {
314 	zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
315 	list_destructors.nNextFreeElement=1;	/* we don't want resource type 0 */
316 	return SUCCESS;
317 }
318 
319 
zend_destroy_rsrc_list_dtors(void)320 void zend_destroy_rsrc_list_dtors(void)
321 {
322 	zend_hash_destroy(&list_destructors);
323 }
324 
325 
zend_rsrc_list_get_rsrc_type(zend_resource * res)326 const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
327 {
328 	zend_rsrc_list_dtors_entry *lde;
329 
330 	lde = zend_hash_index_find_ptr(&list_destructors, res->type);
331 	if (lde) {
332 		return lde->type_name;
333 	} else {
334 		return NULL;
335 	}
336 }
337 
zend_register_persistent_resource_ex(zend_string * key,void * rsrc_pointer,int rsrc_type)338 ZEND_API zend_resource* zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
339 {
340 	zval *zv;
341 	zval tmp;
342 
343 	ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
344 	GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(tmp));
345 	GC_MAKE_PERSISTENT_LOCAL(key);
346 
347 	zv = zend_hash_update(&EG(persistent_list), key, &tmp);
348 
349 	return Z_RES_P(zv);
350 }
351 
zend_register_persistent_resource(const char * key,size_t key_len,void * rsrc_pointer,int rsrc_type)352 ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
353 {
354 	zend_string *str = zend_string_init(key, key_len, 1);
355 	zend_resource *ret  = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
356 
357 	zend_string_release_ex(str, 1);
358 	return ret;
359 }
360 
361 /*
362  * Local variables:
363  * tab-width: 4
364  * c-basic-offset: 4
365  * indent-tabs-mode: t
366  * End:
367  * vim600: sw=4 ts=4 fdm=marker
368  * vim<600: sw=4 ts=4
369  */
370