-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
copy.c
257 lines (209 loc) · 7.01 KB
/
copy.c
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
/*
+----------------------------------------------------------------------+
| uopz |
+----------------------------------------------------------------------+
| Copyright (c) Joe Watkins 2016 |
+----------------------------------------------------------------------+
| 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: |
| http://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: Joe Watkins <krakjoe@php.net> |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_UOPZ_COPY
#define HAVE_UOPZ_COPY
#include "php.h"
#include "uopz.h"
#include "util.h"
#include "copy.h"
/* {{{ */
static inline HashTable* uopz_copy_statics(HashTable *old) {
return zend_array_dup(old);
} /* }}} */
/* {{{ */
static inline zend_string** uopz_copy_variables(zend_string **old, int end) {
zend_string **variables = safe_emalloc(end, sizeof(zend_string*), 0);
int it = 0;
while (it < end) {
variables[it] = zend_string_copy(old[it]);
it++;
}
return variables;
} /* }}} */
/* {{{ */
static inline zend_try_catch_element* uopz_copy_try(zend_try_catch_element *old, int end) {
zend_try_catch_element *try_catch = safe_emalloc(end, sizeof(zend_try_catch_element), 0);
memcpy(
try_catch,
old,
sizeof(zend_try_catch_element) * end);
return try_catch;
} /* }}} */
#if PHP_VERSION_ID < 70100
/* {{{ */
static inline zend_brk_cont_element* uopz_copy_brk(zend_brk_cont_element *old, int end) {
zend_brk_cont_element *brk_cont = safe_emalloc(end, sizeof(zend_brk_cont_element), 0);
memcpy(
brk_cont,
old,
sizeof(zend_brk_cont_element) * end);
return brk_cont;
} /* }}} */
#else
static inline zend_live_range* uopz_copy_live(zend_live_range *old, int end) {
zend_live_range *range = safe_emalloc(end, sizeof(zend_live_range), 0);
memcpy(
range,
old,
sizeof(zend_live_range) * end);
return range;
}
#endif
/* {{{ */
static inline zval* uopz_copy_literals(zval *old, int end) {
zval *literals = (zval*) safe_emalloc(end, sizeof(zval), 0);
int it = 0;
memcpy(literals, old, sizeof(zval) * end);
while (it < end) {
zval_copy_ctor(&literals[it]);
it++;
}
return literals;
} /* }}} */
/* {{{ */
static inline zend_op* uopz_copy_opcodes(zend_op_array *op_array, zval *literals) {
zend_op *copy = safe_emalloc(
op_array->last, sizeof(zend_op), 0);
memcpy(copy, op_array->opcodes, sizeof(zend_op) * op_array->last);
#if ZEND_USE_ABS_CONST_ADDR || ZEND_USE_ABS_JMP_ADDR
zend_op *opline = copy;
zend_op *end = copy + op_array->last;
for (; opline < end; opline++) {
#if ZEND_USE_ABS_CONST_ADDR
if (opline->op1_type == IS_CONST)
opline->op1.zv = (zval*)((char*)opline->op1.zv + ((char*)op_array->literals - (char*)literals));
if (opline->op2_type == IS_CONST)
opline->op2.zv = (zval*)((char*)opline->op2.zv + ((char*)op_array->literals - (char*)literals));
#endif
#if ZEND_USE_ABS_JMP_ADDR
if ((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) != 0) {
switch (opline->opcode) {
case ZEND_JMP:
case ZEND_FAST_CALL:
case ZEND_DECLARE_ANON_CLASS:
case ZEND_DECLARE_ANON_INHERITED_CLASS:
opline->op1.jmp_addr = ©[opline->op1.jmp_addr - op_array->opcodes];
break;
case ZEND_JMPZNZ:
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
case ZEND_JMP_SET:
case ZEND_COALESCE:
case ZEND_NEW:
case ZEND_FE_RESET_R:
case ZEND_FE_RESET_RW:
case ZEND_ASSERT_CHECK:
opline->op2.jmp_addr = ©[opline->op2.jmp_addr - op_array->opcodes];
break;
}
}
#endif
}
#endif
return copy;
} /* }}} */
/* {{{ */
static inline zend_arg_info* uopz_copy_arginfo(zend_op_array *op_array, zend_arg_info *old, uint32_t end) {
zend_arg_info *info;
uint32_t it = 0;
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
old--;
end++;
}
if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
end++;
}
info = safe_emalloc
(end, sizeof(zend_arg_info), 0);
memcpy(info, old, sizeof(zend_arg_info) * end);
while (it < end) {
if (info[it].name)
info[it].name = zend_string_copy(old[it].name);
#if PHP_VERSION_ID >= 70200
if (ZEND_TYPE_IS_SET(old[it].type) && ZEND_TYPE_IS_CLASS(old[it].type)) {
info[it].type = ZEND_TYPE_ENCODE_CLASS(
zend_string_new(
ZEND_TYPE_NAME(info[it].type)),
ZEND_TYPE_ALLOW_NULL(info[it].type));
}
#else
if (info[it].class_name)
info[it].class_name = zend_string_new(old[it].class_name);
#endif
it++;
}
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
info++;
}
return info;
} /* }}} */
zend_function* uopz_copy_closure(zend_class_entry *scope, zend_function *function, zend_long flags) { /* {{{ */
zend_function *copy;
zend_op_array *op_array;
zend_string **variables;
zval *literals;
zend_arg_info *arg_info;
copy = (zend_function*)
zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(copy, function, sizeof(zend_op_array));
op_array = ©->op_array;
variables = op_array->vars;
literals = op_array->literals;
arg_info = op_array->arg_info;
op_array->function_name = zend_string_dup(op_array->function_name, 0);
op_array->refcount = emalloc(sizeof(uint32_t));
(*op_array->refcount) = 1;
op_array->fn_flags &= ~ ZEND_ACC_CLOSURE | ZEND_ACC_PPP_MASK;
op_array->fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
op_array->fn_flags |= flags;
op_array->scope = scope;
op_array->prototype = NULL;
op_array->run_time_cache = zend_arena_alloc(&CG(arena), op_array->cache_size);
if (op_array->doc_comment) {
op_array->doc_comment = zend_string_copy(op_array->doc_comment);
}
if (op_array->literals) {
op_array->literals = uopz_copy_literals (literals, op_array->last_literal);
}
op_array->opcodes = uopz_copy_opcodes(op_array, literals);
if (op_array->arg_info) {
op_array->arg_info = uopz_copy_arginfo(op_array, arg_info, op_array->num_args);
}
#if PHP_VERSION_ID < 70100
if (op_array->brk_cont_array) {
op_array->brk_cont_array = uopz_copy_brk(op_array->brk_cont_array, op_array->last_brk_cont);
}
#else
if (op_array->live_range) {
op_array->live_range = uopz_copy_live(op_array->live_range, op_array->last_live_range);
}
#endif
if (op_array->try_catch_array) {
op_array->try_catch_array = uopz_copy_try(op_array->try_catch_array, op_array->last_try_catch);
}
if (op_array->vars) {
op_array->vars = uopz_copy_variables(variables, op_array->last_var);
}
if (op_array->static_variables) {
op_array->static_variables = uopz_copy_statics(op_array->static_variables);
}
return copy;
} /* }}} */
#endif