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