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 /* 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 } else if (index == INT_MAX) {
41 zend_error_noreturn(E_ERROR, "Resource ID space overflow");
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 zend_result ZEND_FASTCALL zend_list_delete(zend_resource *res)
48 {
49 if (GC_DELREF(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 void ZEND_FASTCALL zend_list_free(zend_resource *res)
57 {
58 ZEND_ASSERT(GC_REFCOUNT(res) == 0);
59 zend_hash_index_del(&EG(regular_list), res->handle);
60 }
61
zend_resource_dtor(zend_resource * res)62 static void zend_resource_dtor(zend_resource *res)
63 {
64 zend_rsrc_list_dtors_entry *ld;
65 zend_resource r = *res;
66
67 res->type = -1;
68 res->ptr = NULL;
69
70 ld = zend_hash_index_find_ptr(&list_destructors, r.type);
71 ZEND_ASSERT(ld && "Unknown list entry type");
72
73 if (ld->list_dtor_ex) {
74 ld->list_dtor_ex(&r);
75 }
76 }
77
78
zend_list_close(zend_resource * res)79 ZEND_API void ZEND_FASTCALL zend_list_close(zend_resource *res)
80 {
81 if (GC_REFCOUNT(res) <= 0) {
82 zend_list_free(res);
83 } else if (res->type >= 0) {
84 zend_resource_dtor(res);
85 }
86 }
87
zend_register_resource(void * rsrc_pointer,int rsrc_type)88 ZEND_API zend_resource* zend_register_resource(void *rsrc_pointer, int rsrc_type)
89 {
90 zval *zv;
91
92 zv = zend_list_insert(rsrc_pointer, rsrc_type);
93
94 return Z_RES_P(zv);
95 }
96
zend_fetch_resource2(zend_resource * res,const char * resource_type_name,int resource_type1,int resource_type2)97 ZEND_API void *zend_fetch_resource2(zend_resource *res, const char *resource_type_name, int resource_type1, int resource_type2)
98 {
99 if (res) {
100 if (resource_type1 == res->type) {
101 return res->ptr;
102 }
103
104 if (resource_type2 == res->type) {
105 return res->ptr;
106 }
107 }
108
109 if (resource_type_name) {
110 const char *space;
111 const char *class_name = get_active_class_name(&space);
112 zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
113 }
114
115 return NULL;
116 }
117
zend_fetch_resource(zend_resource * res,const char * resource_type_name,int resource_type)118 ZEND_API void *zend_fetch_resource(zend_resource *res, const char *resource_type_name, int resource_type)
119 {
120 if (resource_type == res->type) {
121 return res->ptr;
122 }
123
124 if (resource_type_name) {
125 const char *space;
126 const char *class_name = get_active_class_name(&space);
127 zend_type_error("%s%s%s(): supplied resource is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
128 }
129
130 return NULL;
131 }
132
zend_fetch_resource_ex(zval * res,const char * resource_type_name,int resource_type)133 ZEND_API void *zend_fetch_resource_ex(zval *res, const char *resource_type_name, int resource_type)
134 {
135 const char *space, *class_name;
136 if (res == NULL) {
137 if (resource_type_name) {
138 class_name = get_active_class_name(&space);
139 zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
140 }
141 return NULL;
142 }
143 if (Z_TYPE_P(res) != IS_RESOURCE) {
144 if (resource_type_name) {
145 class_name = get_active_class_name(&space);
146 zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
147 }
148 return NULL;
149 }
150
151 return zend_fetch_resource(Z_RES_P(res), resource_type_name, resource_type);
152 }
153
zend_fetch_resource2_ex(zval * res,const char * resource_type_name,int resource_type1,int resource_type2)154 ZEND_API void *zend_fetch_resource2_ex(zval *res, const char *resource_type_name, int resource_type1, int resource_type2)
155 {
156 const char *space, *class_name;
157 if (res == NULL) {
158 if (resource_type_name) {
159 class_name = get_active_class_name(&space);
160 zend_type_error("%s%s%s(): no %s resource supplied", class_name, space, get_active_function_name(), resource_type_name);
161 }
162 return NULL;
163 }
164 if (Z_TYPE_P(res) != IS_RESOURCE) {
165 if (resource_type_name) {
166 class_name = get_active_class_name(&space);
167 zend_type_error("%s%s%s(): supplied argument is not a valid %s resource", class_name, space, get_active_function_name(), resource_type_name);
168 }
169 return NULL;
170 }
171
172 return zend_fetch_resource2(Z_RES_P(res), resource_type_name, resource_type1, resource_type2);
173 }
174
list_entry_destructor(zval * zv)175 void list_entry_destructor(zval *zv)
176 {
177 zend_resource *res = Z_RES_P(zv);
178
179 ZVAL_UNDEF(zv);
180 if (res->type >= 0) {
181 zend_resource_dtor(res);
182 }
183 efree_size(res, sizeof(zend_resource));
184 }
185
plist_entry_destructor(zval * zv)186 void plist_entry_destructor(zval *zv)
187 {
188 zend_resource *res = Z_RES_P(zv);
189
190 if (res->type >= 0) {
191 zend_rsrc_list_dtors_entry *ld;
192
193 ld = zend_hash_index_find_ptr(&list_destructors, res->type);
194 ZEND_ASSERT(ld && "Unknown list entry type");
195
196 if (ld->plist_dtor_ex) {
197 ld->plist_dtor_ex(res);
198 }
199 }
200 free(res);
201 }
202
zend_init_rsrc_list(void)203 ZEND_API void zend_init_rsrc_list(void)
204 {
205 zend_hash_init(&EG(regular_list), 8, NULL, list_entry_destructor, 0);
206 EG(regular_list).nNextFreeElement = 0;
207 }
208
209
zend_init_rsrc_plist(void)210 void zend_init_rsrc_plist(void)
211 {
212 zend_hash_init(&EG(persistent_list), 8, NULL, plist_entry_destructor, 1);
213 }
214
215
zend_close_rsrc_list(HashTable * ht)216 void zend_close_rsrc_list(HashTable *ht)
217 {
218 zend_resource *res;
219
220 ZEND_HASH_REVERSE_FOREACH_PTR(ht, res) {
221 if (res->type >= 0) {
222 zend_resource_dtor(res);
223 }
224 } ZEND_HASH_FOREACH_END();
225 }
226
227
zend_destroy_rsrc_list(HashTable * ht)228 void zend_destroy_rsrc_list(HashTable *ht)
229 {
230 zend_hash_graceful_reverse_destroy(ht);
231 }
232
233 /* int return due to HashTable API */
clean_module_resource(zval * zv,void * arg)234 static int clean_module_resource(zval *zv, void *arg)
235 {
236 int resource_id = *(int *)arg;
237
238 return Z_RES_TYPE_P(zv) == resource_id;
239 }
240
241 /* int return due to HashTable API */
zend_clean_module_rsrc_dtors_cb(zval * zv,void * arg)242 static int zend_clean_module_rsrc_dtors_cb(zval *zv, void *arg)
243 {
244 zend_rsrc_list_dtors_entry *ld = (zend_rsrc_list_dtors_entry *)Z_PTR_P(zv);
245 int module_number = *(int *)arg;
246 if (ld->module_number == module_number) {
247 zend_hash_apply_with_argument(&EG(persistent_list), clean_module_resource, (void *) &(ld->resource_id));
248 return 1;
249 } else {
250 return 0;
251 }
252 }
253
254
zend_clean_module_rsrc_dtors(int module_number)255 void zend_clean_module_rsrc_dtors(int module_number)
256 {
257 zend_hash_apply_with_argument(&list_destructors, zend_clean_module_rsrc_dtors_cb, (void *) &module_number);
258 }
259
260
zend_register_list_destructors_ex(rsrc_dtor_func_t ld,rsrc_dtor_func_t pld,const char * type_name,int module_number)261 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)
262 {
263 zend_rsrc_list_dtors_entry *lde;
264 zval zv;
265
266 lde = malloc(sizeof(zend_rsrc_list_dtors_entry));
267 lde->list_dtor_ex = ld;
268 lde->plist_dtor_ex = pld;
269 lde->module_number = module_number;
270 lde->resource_id = list_destructors.nNextFreeElement;
271 lde->type_name = type_name;
272 ZVAL_PTR(&zv, lde);
273
274 if (zend_hash_next_index_insert(&list_destructors, &zv) == NULL) {
275 return FAILURE;
276 }
277 return list_destructors.nNextFreeElement-1;
278 }
279
zend_fetch_list_dtor_id(const char * type_name)280 ZEND_API int zend_fetch_list_dtor_id(const char *type_name)
281 {
282 zend_rsrc_list_dtors_entry *lde;
283
284 ZEND_HASH_FOREACH_PTR(&list_destructors, lde) {
285 if (lde->type_name && (strcmp(type_name, lde->type_name) == 0)) {
286 return lde->resource_id;
287 }
288 } ZEND_HASH_FOREACH_END();
289
290 return 0;
291 }
292
list_destructors_dtor(zval * zv)293 static void list_destructors_dtor(zval *zv)
294 {
295 free(Z_PTR_P(zv));
296 }
297
zend_init_rsrc_list_dtors(void)298 void zend_init_rsrc_list_dtors(void)
299 {
300 zend_hash_init(&list_destructors, 64, NULL, list_destructors_dtor, 1);
301 list_destructors.nNextFreeElement=1; /* we don't want resource type 0 */
302 }
303
304
zend_destroy_rsrc_list_dtors(void)305 void zend_destroy_rsrc_list_dtors(void)
306 {
307 zend_hash_destroy(&list_destructors);
308 }
309
310
zend_rsrc_list_get_rsrc_type(zend_resource * res)311 const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
312 {
313 zend_rsrc_list_dtors_entry *lde;
314
315 lde = zend_hash_index_find_ptr(&list_destructors, res->type);
316 if (lde) {
317 return lde->type_name;
318 } else {
319 return NULL;
320 }
321 }
322
zend_register_persistent_resource_ex(zend_string * key,void * rsrc_pointer,int rsrc_type)323 ZEND_API zend_resource* zend_register_persistent_resource_ex(zend_string *key, void *rsrc_pointer, int rsrc_type)
324 {
325 zval *zv;
326 zval tmp;
327
328 ZVAL_NEW_PERSISTENT_RES(&tmp, -1, rsrc_pointer, rsrc_type);
329 GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(tmp));
330 GC_MAKE_PERSISTENT_LOCAL(key);
331
332 zv = zend_hash_update(&EG(persistent_list), key, &tmp);
333
334 return Z_RES_P(zv);
335 }
336
zend_register_persistent_resource(const char * key,size_t key_len,void * rsrc_pointer,int rsrc_type)337 ZEND_API zend_resource* zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
338 {
339 zend_string *str = zend_string_init(key, key_len, 1);
340 zend_resource *ret = zend_register_persistent_resource_ex(str, rsrc_pointer, rsrc_type);
341
342 zend_string_release_ex(str, 1);
343 return ret;
344 }
345