-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzend_common.c
More file actions
299 lines (247 loc) · 7.89 KB
/
zend_common.c
File metadata and controls
299 lines (247 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
#include "zend_common.h"
#include <zend_weakrefs.h>
#include "exceptions.h"
static zend_function *create_fn = NULL;
static zend_function *get_fn = NULL;
void zend_exception_to_warning(const char *format, const bool clean)
{
if (EG(exception) == NULL) {
return;
}
if (instanceof_function(EG(exception)->ce, async_ce_cancellation_exception)) {
// Ignore the exception if it is a cancellation exception
if (clean) {
zend_clear_exception();
}
return;
}
zval rv;
const zval *message =
zend_read_property_ex(EG(exception)->ce, EG(exception), zend_known_strings[ZEND_STR_MESSAGE], 0, &rv);
if (message == NULL) {
async_warning(format, "No message");
} else {
async_warning(format, Z_STRVAL_P(message));
}
if (clean) {
zend_clear_exception();
}
}
zend_string *zend_current_exception_get_message(const bool clean)
{
if (EG(exception) == NULL) {
return NULL;
}
zval rv;
const zval *message =
zend_read_property_ex(EG(exception)->ce, EG(exception), zend_known_strings[ZEND_STR_MESSAGE], 0, &rv);
if (clean) {
zend_clear_exception();
}
if (message != NULL && Z_TYPE_P(message) == IS_STRING) {
return Z_STR_P(message);
} else {
return NULL;
}
}
zend_string *zend_current_exception_get_file(void)
{
if (EG(exception) == NULL) {
return NULL;
}
zval rv;
const zval *file =
zend_read_property_ex(EG(exception)->ce, EG(exception), zend_known_strings[ZEND_STR_FILE], 0, &rv);
if (file != NULL && Z_TYPE_P(file) == IS_STRING) {
return Z_STR_P(file);
} else {
return NULL;
}
}
uint32_t zend_current_exception_get_line(void)
{
if (EG(exception) == NULL) {
return 0;
}
zval rv;
const zval *line =
zend_read_property_ex(EG(exception)->ce, EG(exception), zend_known_strings[ZEND_STR_LINE], 0, &rv);
if (line != NULL && Z_TYPE_P(line) == IS_LONG) {
return Z_LVAL_P(line);
} else {
return 0;
}
}
zend_object *zend_exception_merge(zend_object *exception, bool to_previous, bool transfer_error)
{
zend_object **exception_ptr = &EG(exception);
zend_object *prev_exception = NULL;
zend_object **prev_exception_ptr = &prev_exception;
zend_exception_save_fast(exception_ptr, prev_exception_ptr);
zend_exception_restore_fast(exception_ptr, prev_exception_ptr);
if (exception == NULL) {
exception = *exception_ptr;
*exception_ptr = NULL;
return exception;
}
if (*exception_ptr == NULL) {
return exception;
}
if (exception == *exception_ptr) {
*exception_ptr = NULL;
return exception;
}
if (to_previous) {
// The zend_exception_set_previous method requires ownership of the object to be transferred to it,
// so if ownership was not passed, we must increment the reference count by 1.
if (false == transfer_error) {
GC_ADDREF(exception);
}
zend_exception_set_previous(*exception_ptr, exception);
exception = *exception_ptr;
*exception_ptr = NULL;
} else {
zend_exception_set_previous(exception, *exception_ptr);
*exception_ptr = NULL;
}
return exception;
}
void async_warning(const char *format, ...)
{
va_list args;
va_start(args, format);
zend_string *message = zend_vstrpprintf(0, format, args);
va_end(args);
zend_error(E_CORE_WARNING, "%s", message->val);
zend_string_release(message);
}
void zend_new_weak_reference_from(const zval *referent, zval *retval)
{
if (UNEXPECTED(EG(exception))) {
ZVAL_UNDEF(retval);
zend_exception_to_warning("Unexpected exception in zend_new_weak_reference_from: %s", false);
return;
}
if (!create_fn) {
create_fn = zend_hash_str_find_ptr_lc(&zend_ce_weakref->function_table, ZEND_STRL("create"));
if (UNEXPECTED(create_fn == NULL)) {
zend_error_noreturn(E_CORE_ERROR, "Couldn't find implementation for method WeakReference::create");
}
}
ZVAL_UNDEF(retval);
zend_call_known_function(create_fn, NULL, zend_ce_weakref, retval, 1, (zval *) referent, NULL);
if (UNEXPECTED(Z_TYPE_P(retval) == IS_NULL || Z_ISUNDEF_P(retval))) {
async_warning("Failed to invoke WeakReference::create");
}
}
/**
* The method is used to resolve the weak reference.
*
* The method returns the resolved object.
*
* @warning The method may return a ZVAL with the type NULL!
* @warning You must call the dtor if the result is no longer needed!
*/
void zend_resolve_weak_reference(zval *weak_reference, zval *retval)
{
if (!get_fn) {
get_fn = zend_hash_str_find_ptr_lc(&zend_ce_weakref->function_table, ZEND_STRL("get"));
if (UNEXPECTED(get_fn == NULL)) {
zend_error_noreturn(E_CORE_ERROR, "Couldn't find implementation for method WeakReference::get");
}
}
zend_call_known_function(get_fn, Z_OBJ_P(weak_reference), zend_ce_weakref, retval, 0, NULL, NULL);
}
zif_handler zend_hook_php_function(const char *name, const size_t len, zif_handler new_function)
{
zend_function *original = zend_hash_str_find_ptr(CG(function_table), name, len);
if (original == NULL) {
return NULL;
}
zif_handler original_handler = original->internal_function.handler;
original->internal_function.handler = new_function;
return original_handler;
}
zif_handler zend_replace_method(zend_object *object, const char *method, const size_t len, const zif_handler handler)
{
zif_handler original_handler = NULL;
zend_function *func = zend_hash_str_find_ptr(&object->ce->function_table, method, len);
if (func == NULL) {
return original_handler;
}
original_handler = func->internal_function.handler;
func->internal_function.handler = handler;
return original_handler;
}
void zend_get_function_name_by_fci(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_string **name)
{
if (fci_cache != NULL && fci_cache->function_handler != NULL) {
*name = fci_cache->function_handler->common.function_name;
} else if (fci != NULL && Z_TYPE(fci->function_name) != IS_UNDEF) {
*name = Z_STR(fci->function_name);
} else {
*name = NULL;
}
}
void zend_free_fci(zend_fcall_info *fci, zend_fcall_info_cache *fcc)
{
if (fci != NULL) {
if (fci->params != NULL) {
for (uint32_t i = 0; i < fci->param_count; i++) {
zval_ptr_dtor(&fci->params[i]);
}
efree(fci->params);
}
if (!Z_ISUNDEF(fci->function_name)) {
zval_ptr_dtor(&fci->function_name);
}
if (fci->named_params != NULL) {
GC_DELREF(fci->named_params);
}
efree(fci);
}
if (fcc != NULL) {
efree(fcc);
}
}
void zend_copy_fci(zend_fcall_info *dest_fci,
zend_fcall_info_cache *dest_fcc,
zend_fcall_info *src_fci,
zend_fcall_info_cache *src_fcc)
{
// Copy FCI
*dest_fci = *src_fci;
// Copy function name with proper refcount
ZVAL_COPY(&dest_fci->function_name, &src_fci->function_name);
// Copy parameters if any
if (src_fci->param_count > 0 && src_fci->params != NULL) {
dest_fci->params = safe_emalloc(src_fci->param_count, sizeof(zval), 0);
for (uint32_t i = 0; i < src_fci->param_count; i++) {
ZVAL_COPY(&dest_fci->params[i], &src_fci->params[i]);
}
} else {
dest_fci->params = NULL;
}
// Copy named params if any
if (src_fci->named_params != NULL) {
dest_fci->named_params = src_fci->named_params;
GC_ADDREF(src_fci->named_params);
}
// Copy FCC
*dest_fcc = *src_fcc;
}