1 | |
---|
2 | #include "xcache.h" |
---|
3 | #include "stack.h" |
---|
4 | #include "xcache_globals.h" |
---|
5 | #include "utils.h" |
---|
6 | #ifdef ZEND_ENGINE_2_1 |
---|
7 | #include "zend_vm.h" |
---|
8 | #endif |
---|
9 | #include "opcode_spec.h" |
---|
10 | #undef NDEBUG |
---|
11 | #include "assert.h" |
---|
12 | |
---|
13 | #ifndef max |
---|
14 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef ZEND_VM_SET_OPCODE_HANDLER |
---|
18 | # define ZEND_VM_SET_OPCODE_HANDLER(opline) do { } while (0) |
---|
19 | #endif |
---|
20 | |
---|
21 | #define OP_ZVAL_DTOR(op) do { \ |
---|
22 | Z_UNSET_ISREF((op).u.constant); \ |
---|
23 | zval_dtor(&(op).u.constant); \ |
---|
24 | } while(0) |
---|
25 | xc_compile_result_t *xc_compile_result_init(xc_compile_result_t *cr, /* {{{ */ |
---|
26 | zend_op_array *op_array, |
---|
27 | HashTable *function_table, |
---|
28 | HashTable *class_table) |
---|
29 | { |
---|
30 | if (cr) { |
---|
31 | cr->alloc = 0; |
---|
32 | } |
---|
33 | else { |
---|
34 | cr = emalloc(sizeof(xc_compile_result_t)); |
---|
35 | cr->alloc = 1; |
---|
36 | } |
---|
37 | cr->op_array = op_array; |
---|
38 | cr->function_table = function_table; |
---|
39 | cr->class_table = class_table; |
---|
40 | return cr; |
---|
41 | } |
---|
42 | /* }}} */ |
---|
43 | xc_compile_result_t *xc_compile_result_init_cur(xc_compile_result_t *cr, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
44 | { |
---|
45 | return xc_compile_result_init(cr, op_array, CG(function_table), CG(class_table)); |
---|
46 | } |
---|
47 | /* }}} */ |
---|
48 | void xc_compile_result_free(xc_compile_result_t *cr) /* {{{ */ |
---|
49 | { |
---|
50 | if (cr->alloc) { |
---|
51 | efree(cr); |
---|
52 | } |
---|
53 | } |
---|
54 | /* }}} */ |
---|
55 | |
---|
56 | int xc_apply_function(zend_function *zf, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
57 | { |
---|
58 | switch (zf->type) { |
---|
59 | case ZEND_USER_FUNCTION: |
---|
60 | case ZEND_EVAL_CODE: |
---|
61 | return applyer(&zf->op_array TSRMLS_CC); |
---|
62 | break; |
---|
63 | |
---|
64 | case ZEND_INTERNAL_FUNCTION: |
---|
65 | case ZEND_OVERLOADED_FUNCTION: |
---|
66 | break; |
---|
67 | |
---|
68 | EMPTY_SWITCH_DEFAULT_CASE(); |
---|
69 | } |
---|
70 | return 0; |
---|
71 | } |
---|
72 | /* }}} */ |
---|
73 | typedef struct { |
---|
74 | apply_func_t applyer; |
---|
75 | zend_class_entry *ce; |
---|
76 | } xc_apply_method_info; |
---|
77 | int xc_apply_method(zend_function *zf, xc_apply_method_info *mi TSRMLS_DC) /* {{{ */ |
---|
78 | { |
---|
79 | /* avoid duplicate apply for shadowed method */ |
---|
80 | #ifdef ZEND_ENGINE_2 |
---|
81 | if (mi->ce != zf->common.scope) { |
---|
82 | /* fprintf(stderr, "avoided duplicate %s\n", zf->common.function_name); */ |
---|
83 | return 0; |
---|
84 | } |
---|
85 | #else |
---|
86 | char *name = zf->common.function_name; |
---|
87 | int name_s = strlen(name) + 1; |
---|
88 | zend_class_entry *ce; |
---|
89 | zend_function *ptr; |
---|
90 | |
---|
91 | for (ce = mi->ce->parent; ce; ce = ce->parent) { |
---|
92 | if (zend_hash_find(&ce->function_table, name, name_s, (void **) &ptr) == SUCCESS) { |
---|
93 | if (ptr->op_array.refcount == zf->op_array.refcount) { |
---|
94 | return 0; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | #endif |
---|
99 | return xc_apply_function(zf, mi->applyer TSRMLS_CC); |
---|
100 | } |
---|
101 | /* }}} */ |
---|
102 | #if 0 |
---|
103 | int xc_apply_class(zend_class_entry *ce, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
104 | { |
---|
105 | xc_apply_method_info mi; |
---|
106 | |
---|
107 | mi.applyer = applyer; |
---|
108 | mi.ce = ce; |
---|
109 | zend_hash_apply_with_argument(&(ce->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
---|
110 | return 0; |
---|
111 | } |
---|
112 | /* }}} */ |
---|
113 | #endif |
---|
114 | static int xc_apply_cest(xc_cest_t *cest, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
115 | { |
---|
116 | xc_apply_method_info mi; |
---|
117 | |
---|
118 | mi.applyer = applyer; |
---|
119 | mi.ce = CestToCePtr(*cest); |
---|
120 | zend_hash_apply_with_argument(&(CestToCePtr(*cest)->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
---|
121 | return 0; |
---|
122 | } |
---|
123 | /* }}} */ |
---|
124 | int xc_apply_op_array(xc_compile_result_t *cr, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
125 | { |
---|
126 | zend_hash_apply_with_argument(cr->function_table, (apply_func_arg_t) xc_apply_function, (void *) applyer TSRMLS_CC); |
---|
127 | zend_hash_apply_with_argument(cr->class_table, (apply_func_arg_t) xc_apply_cest, (void *) applyer TSRMLS_CC); |
---|
128 | |
---|
129 | return applyer(cr->op_array TSRMLS_CC); |
---|
130 | } |
---|
131 | /* }}} */ |
---|
132 | |
---|
133 | int xc_undo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
134 | { |
---|
135 | zend_op *opline, *end; |
---|
136 | |
---|
137 | if (!op_array->done_pass_two) { |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | opline = op_array->opcodes; |
---|
142 | end = opline + op_array->last; |
---|
143 | while (opline < end) { |
---|
144 | #ifdef ZEND_ENGINE_2_1 |
---|
145 | switch (opline->opcode) { |
---|
146 | #ifdef ZEND_GOTO |
---|
147 | case ZEND_GOTO: |
---|
148 | #endif |
---|
149 | case ZEND_JMP: |
---|
150 | opline->op1.u.opline_num = opline->op1.u.jmp_addr - op_array->opcodes; |
---|
151 | assert(opline->op1.u.opline_num < op_array->last); |
---|
152 | break; |
---|
153 | case ZEND_JMPZ: |
---|
154 | case ZEND_JMPNZ: |
---|
155 | case ZEND_JMPZ_EX: |
---|
156 | case ZEND_JMPNZ_EX: |
---|
157 | #ifdef ZEND_JMP_SET |
---|
158 | case ZEND_JMP_SET: |
---|
159 | #endif |
---|
160 | opline->op2.u.opline_num = opline->op2.u.jmp_addr - op_array->opcodes; |
---|
161 | assert(opline->op2.u.opline_num < op_array->last); |
---|
162 | break; |
---|
163 | } |
---|
164 | #endif |
---|
165 | opline++; |
---|
166 | } |
---|
167 | op_array->done_pass_two = 0; |
---|
168 | |
---|
169 | return 0; |
---|
170 | } |
---|
171 | /* }}} */ |
---|
172 | int xc_redo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
173 | { |
---|
174 | zend_op *opline, *end; |
---|
175 | |
---|
176 | if (op_array->done_pass_two) { |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | /* |
---|
181 | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last); |
---|
182 | op_array->size = op_array->last; |
---|
183 | */ |
---|
184 | |
---|
185 | opline = op_array->opcodes; |
---|
186 | end = opline + op_array->last; |
---|
187 | while (opline < end) { |
---|
188 | if (opline->op1.op_type == IS_CONST) { |
---|
189 | Z_SET_ISREF(opline->op1.u.constant); |
---|
190 | Z_SET_REFCOUNT(opline->op1.u.constant, 2); /* Make sure is_ref won't be reset */ |
---|
191 | |
---|
192 | } |
---|
193 | if (opline->op2.op_type == IS_CONST) { |
---|
194 | Z_SET_ISREF(opline->op2.u.constant); |
---|
195 | Z_SET_REFCOUNT(opline->op2.u.constant, 2); |
---|
196 | } |
---|
197 | #ifdef ZEND_ENGINE_2_1 |
---|
198 | switch (opline->opcode) { |
---|
199 | #ifdef ZEND_GOTO |
---|
200 | case ZEND_GOTO: |
---|
201 | #endif |
---|
202 | case ZEND_JMP: |
---|
203 | assert(opline->op1.u.opline_num < op_array->last); |
---|
204 | opline->op1.u.jmp_addr = op_array->opcodes + opline->op1.u.opline_num; |
---|
205 | break; |
---|
206 | case ZEND_JMPZ: |
---|
207 | case ZEND_JMPNZ: |
---|
208 | case ZEND_JMPZ_EX: |
---|
209 | case ZEND_JMPNZ_EX: |
---|
210 | #ifdef ZEND_JMP_SET |
---|
211 | case ZEND_JMP_SET: |
---|
212 | #endif |
---|
213 | assert(opline->op2.u.opline_num < op_array->last); |
---|
214 | opline->op2.u.jmp_addr = op_array->opcodes + opline->op2.u.opline_num; |
---|
215 | break; |
---|
216 | } |
---|
217 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
---|
218 | #endif |
---|
219 | opline++; |
---|
220 | } |
---|
221 | |
---|
222 | op_array->done_pass_two = 1; |
---|
223 | return 0; |
---|
224 | } |
---|
225 | /* }}} */ |
---|
226 | |
---|
227 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
---|
228 | static void xc_fix_opcode_ex_znode(int tofix, xc_op_spec_t spec, znode *znode, int type TSRMLS_DC) /* {{{ */ |
---|
229 | { |
---|
230 | #ifdef ZEND_ENGINE_2 |
---|
231 | if ((znode->op_type != IS_UNUSED && (spec == OPSPEC_UCLASS || spec == OPSPEC_CLASS)) || |
---|
232 | spec == OPSPEC_FETCH) { |
---|
233 | if (tofix) { |
---|
234 | switch (znode->op_type) { |
---|
235 | case IS_VAR: |
---|
236 | case IS_TMP_VAR: |
---|
237 | break; |
---|
238 | |
---|
239 | default: |
---|
240 | /* TODO: data lost, find a way to keep it */ |
---|
241 | /* assert(znode->op_type == IS_CONST); */ |
---|
242 | znode->op_type = IS_TMP_VAR; |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | switch (znode->op_type) { |
---|
247 | case IS_TMP_VAR: |
---|
248 | case IS_VAR: |
---|
249 | if (tofix) { |
---|
250 | znode->u.var /= sizeof(temp_variable); |
---|
251 | } |
---|
252 | else { |
---|
253 | znode->u.var *= sizeof(temp_variable); |
---|
254 | } |
---|
255 | } |
---|
256 | #endif |
---|
257 | } |
---|
258 | /* }}} */ |
---|
259 | |
---|
260 | static void xc_fix_opcode_ex(zend_op_array *op_array, int tofix TSRMLS_DC) /* {{{ */ |
---|
261 | { |
---|
262 | zend_op *opline; |
---|
263 | zend_uint i; |
---|
264 | |
---|
265 | opline = op_array->opcodes; |
---|
266 | for (i = 0; i < op_array->last; i ++, opline ++) { |
---|
267 | /* 3rd optimizer may have ... */ |
---|
268 | if (opline->opcode < xc_get_opcode_spec_count()) { |
---|
269 | const xc_opcode_spec_t *spec; |
---|
270 | spec = xc_get_opcode_spec(opline->opcode); |
---|
271 | |
---|
272 | xc_fix_opcode_ex_znode(tofix, spec->op1, &opline->op1, 0 TSRMLS_CC); |
---|
273 | xc_fix_opcode_ex_znode(tofix, spec->op2, &opline->op2, 1 TSRMLS_CC); |
---|
274 | xc_fix_opcode_ex_znode(tofix, spec->res, &opline->result, 2 TSRMLS_CC); |
---|
275 | } |
---|
276 | } |
---|
277 | } |
---|
278 | /* }}} */ |
---|
279 | int xc_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
280 | { |
---|
281 | xc_fix_opcode_ex(op_array, 1 TSRMLS_CC); |
---|
282 | return 0; |
---|
283 | } |
---|
284 | /* }}} */ |
---|
285 | int xc_undo_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
286 | { |
---|
287 | xc_fix_opcode_ex(op_array, 0 TSRMLS_CC); |
---|
288 | |
---|
289 | return 0; |
---|
290 | } |
---|
291 | /* }}} */ |
---|
292 | #endif |
---|
293 | |
---|
294 | int xc_foreach_early_binding_class(zend_op_array *op_array, void (*callback)(zend_op *opline, int oplineno, void *data TSRMLS_DC), void *data TSRMLS_DC) /* {{{ */ |
---|
295 | { |
---|
296 | zend_op *opline, *begin, *end, *next = NULL; |
---|
297 | |
---|
298 | opline = begin = op_array->opcodes; |
---|
299 | end = opline + op_array->last; |
---|
300 | while (opline < end) { |
---|
301 | switch (opline->opcode) { |
---|
302 | #ifdef ZEND_GOTO |
---|
303 | case ZEND_GOTO: |
---|
304 | #endif |
---|
305 | case ZEND_JMP: |
---|
306 | next = begin + opline->op1.u.opline_num; |
---|
307 | break; |
---|
308 | |
---|
309 | case ZEND_JMPZNZ: |
---|
310 | next = begin + max(opline->op2.u.opline_num, opline->extended_value); |
---|
311 | break; |
---|
312 | |
---|
313 | case ZEND_JMPZ: |
---|
314 | case ZEND_JMPNZ: |
---|
315 | case ZEND_JMPZ_EX: |
---|
316 | case ZEND_JMPNZ_EX: |
---|
317 | #ifdef ZEND_JMP_SET |
---|
318 | case ZEND_JMP_SET: |
---|
319 | #endif |
---|
320 | next = begin + opline->op2.u.opline_num; |
---|
321 | break; |
---|
322 | |
---|
323 | case ZEND_RETURN: |
---|
324 | opline = end; |
---|
325 | break; |
---|
326 | |
---|
327 | #ifdef ZEND_ENGINE_2 |
---|
328 | case ZEND_DECLARE_INHERITED_CLASS: |
---|
329 | callback(opline, opline - begin, data TSRMLS_CC); |
---|
330 | break; |
---|
331 | #else |
---|
332 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
---|
333 | if (opline->extended_value == ZEND_DECLARE_INHERITED_CLASS) { |
---|
334 | callback(opline, opline - begin, data TSRMLS_CC); |
---|
335 | } |
---|
336 | break; |
---|
337 | #endif |
---|
338 | } |
---|
339 | |
---|
340 | if (opline < next) { |
---|
341 | opline = next; |
---|
342 | } |
---|
343 | else { |
---|
344 | opline ++; |
---|
345 | } |
---|
346 | } |
---|
347 | return SUCCESS; |
---|
348 | } |
---|
349 | /* }}} */ |
---|
350 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
351 | static int xc_do_early_binding(zend_op_array *op_array, HashTable *class_table, int oplineno TSRMLS_DC) /* {{{ */ |
---|
352 | { |
---|
353 | zend_op *opline; |
---|
354 | |
---|
355 | TRACE("binding %d", oplineno); |
---|
356 | assert(oplineno >= 0); |
---|
357 | |
---|
358 | /* do early binding */ |
---|
359 | opline = &(op_array->opcodes[oplineno]); |
---|
360 | |
---|
361 | switch (opline->opcode) { |
---|
362 | #ifdef ZEND_ENGINE_2 |
---|
363 | case ZEND_DECLARE_INHERITED_CLASS: |
---|
364 | { |
---|
365 | zval *parent_name; |
---|
366 | zend_class_entry **pce; |
---|
367 | |
---|
368 | /* don't early-bind classes that implement interfaces */ |
---|
369 | if ((opline + 1)->opcode == ZEND_FETCH_CLASS && (opline + 2)->opcode == ZEND_ADD_INTERFACE) { |
---|
370 | return FAILURE; |
---|
371 | } |
---|
372 | |
---|
373 | parent_name = &(opline - 1)->op2.u.constant; |
---|
374 | TRACE("binding with parent %s", Z_STRVAL_P(parent_name)); |
---|
375 | if (zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) { |
---|
376 | return FAILURE; |
---|
377 | } |
---|
378 | |
---|
379 | if (do_bind_inherited_class(opline, class_table, *pce, 1 TSRMLS_CC) == NULL) { |
---|
380 | return FAILURE; |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | /* clear unnecessary ZEND_FETCH_CLASS opcode */ |
---|
385 | if (opline > op_array->opcodes |
---|
386 | && (opline - 1)->opcode == ZEND_FETCH_CLASS) { |
---|
387 | zend_op *fetch_class_opline = opline - 1; |
---|
388 | |
---|
389 | TRACE("%s %p", Z_STRVAL(fetch_class_opline->op2.u.constant), Z_STRVAL(fetch_class_opline->op2.u.constant)); |
---|
390 | OP_ZVAL_DTOR(fetch_class_opline->op2); |
---|
391 | fetch_class_opline->opcode = ZEND_NOP; |
---|
392 | ZEND_VM_SET_OPCODE_HANDLER(fetch_class_opline); |
---|
393 | memset(&fetch_class_opline->op1, 0, sizeof(znode)); |
---|
394 | memset(&fetch_class_opline->op2, 0, sizeof(znode)); |
---|
395 | SET_UNUSED(fetch_class_opline->op1); |
---|
396 | SET_UNUSED(fetch_class_opline->op2); |
---|
397 | SET_UNUSED(fetch_class_opline->result); |
---|
398 | } |
---|
399 | |
---|
400 | /* clear unnecessary ZEND_VERIFY_ABSTRACT_CLASS opcode */ |
---|
401 | if ((opline + 1)->opcode == ZEND_VERIFY_ABSTRACT_CLASS) { |
---|
402 | zend_op *abstract_op = opline + 1; |
---|
403 | memset(abstract_op, 0, sizeof(abstract_op[0])); |
---|
404 | abstract_op->lineno = 0; |
---|
405 | SET_UNUSED(abstract_op->op1); |
---|
406 | SET_UNUSED(abstract_op->op2); |
---|
407 | SET_UNUSED(abstract_op->result); |
---|
408 | abstract_op->opcode = ZEND_NOP; |
---|
409 | ZEND_VM_SET_OPCODE_HANDLER(abstract_op); |
---|
410 | } |
---|
411 | #else |
---|
412 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
---|
413 | if (do_bind_function_or_class(opline, NULL, class_table, 1) == FAILURE) { |
---|
414 | return FAILURE; |
---|
415 | } |
---|
416 | #endif |
---|
417 | break; |
---|
418 | |
---|
419 | default: |
---|
420 | return FAILURE; |
---|
421 | } |
---|
422 | |
---|
423 | zend_hash_del(class_table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len); |
---|
424 | OP_ZVAL_DTOR(opline->op1); |
---|
425 | OP_ZVAL_DTOR(opline->op2); |
---|
426 | opline->opcode = ZEND_NOP; |
---|
427 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
---|
428 | memset(&opline->op1, 0, sizeof(znode)); |
---|
429 | memset(&opline->op2, 0, sizeof(znode)); |
---|
430 | SET_UNUSED(opline->op1); |
---|
431 | SET_UNUSED(opline->op2); |
---|
432 | return SUCCESS; |
---|
433 | } |
---|
434 | /* }}} */ |
---|
435 | #endif |
---|
436 | |
---|
437 | #ifdef HAVE_XCACHE_CONSTANT |
---|
438 | void xc_install_constant(char *filename, zend_constant *constant, zend_uchar type, zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
---|
439 | { |
---|
440 | if (zend_u_hash_add(EG(zend_constants), type, key, len, |
---|
441 | constant, sizeof(zend_constant), |
---|
442 | NULL |
---|
443 | ) == FAILURE) { |
---|
444 | CG(zend_lineno) = 0; |
---|
445 | #ifdef IS_UNICODE |
---|
446 | zend_error(E_NOTICE, "Constant %R already defined", type, key); |
---|
447 | #else |
---|
448 | zend_error(E_NOTICE, "Constant %s already defined", key); |
---|
449 | #endif |
---|
450 | free(ZSTR_V(constant->name)); |
---|
451 | if (!(constant->flags & CONST_PERSISTENT)) { |
---|
452 | zval_dtor(&constant->value); |
---|
453 | } |
---|
454 | } |
---|
455 | } |
---|
456 | /* }}} */ |
---|
457 | #endif |
---|
458 | void xc_install_function(char *filename, zend_function *func, zend_uchar type, zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
---|
459 | { |
---|
460 | zend_bool istmpkey; |
---|
461 | |
---|
462 | if (func->type == ZEND_USER_FUNCTION) { |
---|
463 | #ifdef IS_UNICODE |
---|
464 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
---|
465 | #else |
---|
466 | istmpkey = ZSTR_S(key)[0] == 0; |
---|
467 | #endif |
---|
468 | if (istmpkey) { |
---|
469 | zend_u_hash_update(CG(function_table), type, key, len, |
---|
470 | func, sizeof(zend_op_array), |
---|
471 | NULL |
---|
472 | ); |
---|
473 | } |
---|
474 | else if (zend_u_hash_add(CG(function_table), type, key, len, |
---|
475 | func, sizeof(zend_op_array), |
---|
476 | NULL |
---|
477 | ) == FAILURE) { |
---|
478 | CG(zend_lineno) = ZESW(func->op_array.opcodes[0].lineno, func->op_array.line_start); |
---|
479 | #ifdef IS_UNICODE |
---|
480 | zend_error(E_ERROR, "Cannot redeclare %R()", type, key); |
---|
481 | #else |
---|
482 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
---|
483 | #endif |
---|
484 | } |
---|
485 | } |
---|
486 | } |
---|
487 | /* }}} */ |
---|
488 | ZESW(xc_cest_t *, void) xc_install_class(char *filename, xc_cest_t *cest, int oplineno, zend_uchar type, zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
---|
489 | { |
---|
490 | zend_bool istmpkey; |
---|
491 | zend_class_entry *cep = CestToCePtr(*cest); |
---|
492 | ZESW(void *stored_ce_ptr, NOTHING); |
---|
493 | |
---|
494 | #ifdef IS_UNICODE |
---|
495 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
---|
496 | #else |
---|
497 | istmpkey = ZSTR_S(key)[0] == 0; |
---|
498 | #endif |
---|
499 | if (istmpkey) { |
---|
500 | zend_u_hash_quick_update(CG(class_table), type, key, len, h, |
---|
501 | cest, sizeof(xc_cest_t), |
---|
502 | ZESW(&stored_ce_ptr, NULL) |
---|
503 | ); |
---|
504 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
505 | if (oplineno != -1) { |
---|
506 | xc_do_early_binding(CG(active_op_array), CG(class_table), oplineno TSRMLS_CC); |
---|
507 | } |
---|
508 | #endif |
---|
509 | } |
---|
510 | else if (zend_u_hash_quick_add(CG(class_table), type, key, len, h, |
---|
511 | cest, sizeof(xc_cest_t), |
---|
512 | ZESW(&stored_ce_ptr, NULL) |
---|
513 | ) == FAILURE) { |
---|
514 | CG(zend_lineno) = ZESW(0, cep->line_start); |
---|
515 | #ifdef IS_UNICODE |
---|
516 | zend_error(E_ERROR, "Cannot redeclare class %R", type, cep->name); |
---|
517 | #else |
---|
518 | zend_error(E_ERROR, "Cannot redeclare class %s", cep->name); |
---|
519 | #endif |
---|
520 | assert(oplineno == -1); |
---|
521 | } |
---|
522 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
---|
523 | } |
---|
524 | /* }}} */ |
---|
525 | |
---|
526 | /* sandbox {{{ */ |
---|
527 | #undef TG |
---|
528 | #undef OG |
---|
529 | #define TG(x) (sandbox->tmp_##x) |
---|
530 | #define OG(x) (sandbox->orig_##x) |
---|
531 | /* }}} */ |
---|
532 | #if defined(E_STRICT) || defined(E_DEPRECATED) |
---|
533 | static void xc_sandbox_error_cb(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args) /* {{{ */ |
---|
534 | { |
---|
535 | xc_compilererror_t *compilererror; |
---|
536 | xc_sandbox_t *sandbox; |
---|
537 | TSRMLS_FETCH(); |
---|
538 | |
---|
539 | sandbox = (xc_sandbox_t *) XG(sandbox); |
---|
540 | assert(sandbox != NULL); |
---|
541 | switch (type) { |
---|
542 | #ifdef E_STRICT: |
---|
543 | case E_STRICT: |
---|
544 | #endif |
---|
545 | #ifdef E_DEPRECATED |
---|
546 | case E_DEPRECATED: |
---|
547 | #endif |
---|
548 | if (sandbox->compilererror_cnt <= sandbox->compilererror_size) { |
---|
549 | if (sandbox->compilererror_size) { |
---|
550 | sandbox->compilererror_size += 16; |
---|
551 | sandbox->compilererrors = erealloc(sandbox->compilererrors, sandbox->compilererror_size * sizeof(sandbox->compilererrors)); |
---|
552 | } |
---|
553 | else { |
---|
554 | sandbox->compilererror_size = 16; |
---|
555 | sandbox->compilererrors = emalloc(sandbox->compilererror_size * sizeof(sandbox->compilererrors)); |
---|
556 | } |
---|
557 | } |
---|
558 | compilererror = &sandbox->compilererrors[sandbox->compilererror_cnt++]; |
---|
559 | compilererror->type = type; |
---|
560 | compilererror->lineno = error_lineno; |
---|
561 | compilererror->error_len = vspprintf(&compilererror->error, 0, format, args); |
---|
562 | break; |
---|
563 | |
---|
564 | default: { |
---|
565 | /* give up, and user handler is not supported in this case */ |
---|
566 | zend_uint i; |
---|
567 | zend_uint orig_lineno = CG(zend_lineno); |
---|
568 | zend_error_cb = sandbox->orig_zend_error_cb; |
---|
569 | |
---|
570 | for (i = 0; i < sandbox->compilererror_cnt; i ++) { |
---|
571 | compilererror = &sandbox->compilererrors[i]; |
---|
572 | CG(zend_lineno) = compilererror->lineno; |
---|
573 | zend_error(type, "%s", compilererror->error); |
---|
574 | } |
---|
575 | CG(zend_lineno) = orig_lineno; |
---|
576 | sandbox->compilererror_cnt = 0; |
---|
577 | |
---|
578 | sandbox->orig_zend_error_cb(type, error_filename, error_lineno, format, args); |
---|
579 | break; |
---|
580 | } |
---|
581 | } |
---|
582 | } |
---|
583 | /* }}} */ |
---|
584 | #endif |
---|
585 | #ifdef ZEND_ENGINE_2_1 |
---|
586 | static zend_bool xc_auto_global_callback(char *name, uint name_len TSRMLS_DC) /* {{{ */ |
---|
587 | { |
---|
588 | zend_auto_global *auto_global; |
---|
589 | if (zend_u_hash_find(CG(auto_globals), UG(unicode) ? IS_UNICODE : IS_STRING, ZSTR(name), name_len + 1, (void **) &auto_global) == FAILURE) { |
---|
590 | return 1; |
---|
591 | } |
---|
592 | return 0; |
---|
593 | } |
---|
594 | /* }}} */ |
---|
595 | static int xc_auto_global_arm(zend_auto_global *auto_global TSRMLS_DC) /* {{{ */ |
---|
596 | { |
---|
597 | if (auto_global->auto_global_callback) { |
---|
598 | auto_global->armed = 1; |
---|
599 | auto_global->auto_global_callback = xc_auto_global_callback; |
---|
600 | } |
---|
601 | else { |
---|
602 | auto_global->armed = 0; |
---|
603 | } |
---|
604 | return ZEND_HASH_APPLY_KEEP; |
---|
605 | } |
---|
606 | /* }}} */ |
---|
607 | #endif |
---|
608 | |
---|
609 | void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker) /* {{{ */ |
---|
610 | { |
---|
611 | Bucket *p; |
---|
612 | void *new_entry; |
---|
613 | zend_bool setTargetPointer; |
---|
614 | |
---|
615 | setTargetPointer = !target->pInternalPointer; |
---|
616 | p = source->pListHead; |
---|
617 | while (p) { |
---|
618 | if (checker(p->pData)) { |
---|
619 | if (setTargetPointer && source->pInternalPointer == p) { |
---|
620 | target->pInternalPointer = NULL; |
---|
621 | } |
---|
622 | if (p->nKeyLength) { |
---|
623 | zend_u_hash_quick_update(target, p->key.type, ZSTR(BUCKET_KEY_S(p)), p->nKeyLength, p->h, p->pData, size, &new_entry); |
---|
624 | } else { |
---|
625 | zend_hash_index_update(target, p->h, p->pData, size, &new_entry); |
---|
626 | } |
---|
627 | if (pCopyConstructor) { |
---|
628 | pCopyConstructor(new_entry); |
---|
629 | } |
---|
630 | } |
---|
631 | p = p->pListNext; |
---|
632 | } |
---|
633 | if (!target->pInternalPointer) { |
---|
634 | target->pInternalPointer = target->pListHead; |
---|
635 | } |
---|
636 | } |
---|
637 | /* }}} */ |
---|
638 | #ifdef HAVE_XCACHE_CONSTANT |
---|
639 | static zend_bool xc_is_internal_zend_constant(zend_constant *c) /* {{{ */ |
---|
640 | { |
---|
641 | return (c->flags & CONST_PERSISTENT) ? 1 : 0; |
---|
642 | } |
---|
643 | /* }}} */ |
---|
644 | void xc_zend_constant_ctor(zend_constant *c) /* {{{ */ |
---|
645 | { |
---|
646 | assert((c->flags & CONST_PERSISTENT)); |
---|
647 | #ifdef IS_UNICODE |
---|
648 | c->name.u = zend_ustrndup(c->name.u, c->name_len - 1); |
---|
649 | #else |
---|
650 | c->name = zend_strndup(c->name, c->name_len - 1); |
---|
651 | #endif |
---|
652 | } |
---|
653 | /* }}} */ |
---|
654 | void xc_zend_constant_dtor(zend_constant *c) /* {{{ */ |
---|
655 | { |
---|
656 | free(ZSTR_U(c->name)); |
---|
657 | } |
---|
658 | /* }}} */ |
---|
659 | void xc_copy_internal_zend_constants(HashTable *target, HashTable *source) /* {{{ */ |
---|
660 | { |
---|
661 | zend_constant tmp_const; |
---|
662 | xc_hash_copy_if(target, source, (copy_ctor_func_t) xc_zend_constant_ctor, (void *) &tmp_const, sizeof(zend_constant), (xc_if_func_t) xc_is_internal_zend_constant); |
---|
663 | } |
---|
664 | /* }}} */ |
---|
665 | #endif |
---|
666 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC) /* {{{ */ |
---|
667 | { |
---|
668 | HashTable *h; |
---|
669 | |
---|
670 | if (sandbox) { |
---|
671 | memset(sandbox, 0, sizeof(sandbox[0])); |
---|
672 | } |
---|
673 | else { |
---|
674 | ECALLOC_ONE(sandbox); |
---|
675 | sandbox->alloc = 1; |
---|
676 | } |
---|
677 | |
---|
678 | memcpy(&OG(included_files), &EG(included_files), sizeof(EG(included_files))); |
---|
679 | |
---|
680 | #ifdef HAVE_XCACHE_CONSTANT |
---|
681 | OG(zend_constants) = EG(zend_constants); |
---|
682 | EG(zend_constants) = &TG(zend_constants); |
---|
683 | #endif |
---|
684 | |
---|
685 | OG(function_table) = CG(function_table); |
---|
686 | CG(function_table) = &TG(function_table); |
---|
687 | |
---|
688 | OG(class_table) = CG(class_table); |
---|
689 | CG(class_table) = &TG(class_table); |
---|
690 | EG(class_table) = CG(class_table); |
---|
691 | |
---|
692 | #ifdef ZEND_ENGINE_2_1 |
---|
693 | OG(auto_globals) = CG(auto_globals); |
---|
694 | CG(auto_globals) = &TG(auto_globals); |
---|
695 | #endif |
---|
696 | |
---|
697 | TG(included_files) = &EG(included_files); |
---|
698 | |
---|
699 | zend_hash_init_ex(TG(included_files), 5, NULL, NULL, 0, 1); |
---|
700 | #ifdef HAVE_XCACHE_CONSTANT |
---|
701 | h = OG(zend_constants); |
---|
702 | zend_hash_init_ex(&TG(zend_constants), 20, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
703 | xc_copy_internal_zend_constants(&TG(zend_constants), &XG(internal_constant_table)); |
---|
704 | { |
---|
705 | zend_constant tmp_const; |
---|
706 | zend_hash_copy(&TG(zend_constants), &XG(internal_constant_table), (copy_ctor_func_t) xc_zend_constant_ctor, (void *) &tmp_const, sizeof(tmp_const)); |
---|
707 | } |
---|
708 | TG(internal_constant_tail) = TG(zend_constants).pListTail; |
---|
709 | #endif |
---|
710 | h = OG(function_table); |
---|
711 | zend_hash_init_ex(&TG(function_table), 128, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
712 | { |
---|
713 | zend_function tmp_func; |
---|
714 | zend_hash_copy(&TG(function_table), &XG(internal_function_table), NULL, (void *) &tmp_func, sizeof(tmp_func)); |
---|
715 | } |
---|
716 | TG(internal_function_tail) = TG(function_table).pListTail; |
---|
717 | |
---|
718 | h = OG(class_table); |
---|
719 | zend_hash_init_ex(&TG(class_table), 16, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
720 | #if 0 && TODO |
---|
721 | { |
---|
722 | xc_cest_t tmp_cest; |
---|
723 | zend_hash_copy(&TG(class_table), &XG(internal_class_table), NULL, (void *) &tmp_cest, sizeof(tmp_cest)); |
---|
724 | } |
---|
725 | #endif |
---|
726 | TG(internal_class_tail) = TG(class_table).pListTail; |
---|
727 | |
---|
728 | #ifdef ZEND_ENGINE_2_1 |
---|
729 | /* shallow copy, don't destruct */ |
---|
730 | h = OG(auto_globals); |
---|
731 | zend_hash_init_ex(&TG(auto_globals), 8, NULL, NULL, h->persistent, h->bApplyProtection); |
---|
732 | { |
---|
733 | zend_auto_global tmp_autoglobal; |
---|
734 | |
---|
735 | zend_hash_copy(&TG(auto_globals), OG(auto_globals), NULL, (void *) &tmp_autoglobal, sizeof(tmp_autoglobal)); |
---|
736 | zend_hash_apply(&TG(auto_globals), (apply_func_t) xc_auto_global_arm TSRMLS_CC); |
---|
737 | } |
---|
738 | #endif |
---|
739 | |
---|
740 | sandbox->filename = filename; |
---|
741 | |
---|
742 | #ifdef E_STRICT |
---|
743 | sandbox->orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting); |
---|
744 | EG(user_error_handler_error_reporting) = 0; |
---|
745 | |
---|
746 | sandbox->compilererror_cnt = 0; |
---|
747 | sandbox->compilererror_size = 0; |
---|
748 | sandbox->orig_zend_error_cb = zend_error_cb; |
---|
749 | zend_error_cb = xc_sandbox_error_cb; |
---|
750 | #endif |
---|
751 | |
---|
752 | #ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES |
---|
753 | sandbox->orig_compiler_options = CG(compiler_options); |
---|
754 | /* Using ZEND_COMPILE_IGNORE_INTERNAL_CLASSES for ZEND_FETCH_CLASS_RT_NS_CHECK |
---|
755 | */ |
---|
756 | CG(compiler_options) |= ZEND_COMPILE_IGNORE_INTERNAL_CLASSES | ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_DELAYED_BINDING; |
---|
757 | #endif |
---|
758 | |
---|
759 | XG(sandbox) = (void *) sandbox; |
---|
760 | return sandbox; |
---|
761 | } |
---|
762 | /* }}} */ |
---|
763 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
764 | static void xc_early_binding_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
---|
765 | { |
---|
766 | xc_sandbox_t *sandbox = (xc_sandbox_t *) data; |
---|
767 | xc_do_early_binding(CG(active_op_array), OG(class_table), oplineno TSRMLS_CC); |
---|
768 | } |
---|
769 | /* }}} */ |
---|
770 | #endif |
---|
771 | static void xc_sandbox_install(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
---|
772 | { |
---|
773 | zend_uint i; |
---|
774 | Bucket *b; |
---|
775 | |
---|
776 | #ifdef HAVE_XCACHE_CONSTANT |
---|
777 | b = TG(internal_constant_tail) ? TG(internal_constant_tail)->pListNext : TG(zend_constants).pListHead; |
---|
778 | /* install constants */ |
---|
779 | while (b != NULL) { |
---|
780 | zend_constant *c = (zend_constant*) b->pData; |
---|
781 | xc_install_constant(sandbox->filename, c, |
---|
782 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
---|
783 | b = b->pListNext; |
---|
784 | } |
---|
785 | #endif |
---|
786 | |
---|
787 | b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; |
---|
788 | /* install function */ |
---|
789 | while (b != NULL) { |
---|
790 | zend_function *func = (zend_function*) b->pData; |
---|
791 | xc_install_function(sandbox->filename, func, |
---|
792 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
---|
793 | b = b->pListNext; |
---|
794 | } |
---|
795 | |
---|
796 | b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; |
---|
797 | /* install class */ |
---|
798 | while (b != NULL) { |
---|
799 | xc_install_class(sandbox->filename, (xc_cest_t*) b->pData, -1, |
---|
800 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
---|
801 | b = b->pListNext; |
---|
802 | } |
---|
803 | |
---|
804 | #ifdef ZEND_ENGINE_2_1 |
---|
805 | /* trigger auto_globals jit */ |
---|
806 | for (b = TG(auto_globals).pListHead; b != NULL; b = b->pListNext) { |
---|
807 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
808 | /* check if actived */ |
---|
809 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
810 | zend_u_is_auto_global(BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), auto_global->name_len TSRMLS_CC); |
---|
811 | } |
---|
812 | } |
---|
813 | #endif |
---|
814 | |
---|
815 | if (install != XC_InstallNoBinding) { |
---|
816 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
---|
817 | zend_do_delayed_early_binding(CG(active_op_array) TSRMLS_CC); |
---|
818 | #else |
---|
819 | xc_undo_pass_two(CG(active_op_array) TSRMLS_CC); |
---|
820 | xc_foreach_early_binding_class(CG(active_op_array), xc_early_binding_cb, (void *) sandbox TSRMLS_CC); |
---|
821 | xc_redo_pass_two(CG(active_op_array) TSRMLS_CC); |
---|
822 | #endif |
---|
823 | } |
---|
824 | |
---|
825 | #ifdef E_STRICT |
---|
826 | /* restore trigger errors */ |
---|
827 | for (i = 0; i < sandbox->compilererror_cnt; i ++) { |
---|
828 | xc_compilererror_t *error = &sandbox->compilererrors[i]; |
---|
829 | CG(zend_lineno) = error->lineno; |
---|
830 | zend_error(error->type, "%s", error->error); |
---|
831 | } |
---|
832 | CG(zend_lineno) = 0; |
---|
833 | #endif |
---|
834 | |
---|
835 | i = 1; |
---|
836 | zend_hash_add(&OG(included_files), sandbox->filename, strlen(sandbox->filename) + 1, (void *)&i, sizeof(int), NULL); |
---|
837 | } |
---|
838 | /* }}} */ |
---|
839 | void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
---|
840 | { |
---|
841 | XG(sandbox) = NULL; |
---|
842 | #ifdef E_STRICT |
---|
843 | EG(user_error_handler_error_reporting) = sandbox->orig_user_error_handler_error_reporting; |
---|
844 | zend_error_cb = sandbox->orig_zend_error_cb; |
---|
845 | #endif |
---|
846 | |
---|
847 | /* restore first first install function/class */ |
---|
848 | #ifdef HAVE_XCACHE_CONSTANT |
---|
849 | EG(zend_constants) = OG(zend_constants); |
---|
850 | #endif |
---|
851 | CG(function_table) = OG(function_table); |
---|
852 | CG(class_table) = OG(class_table); |
---|
853 | EG(class_table) = CG(class_table); |
---|
854 | #ifdef ZEND_ENGINE_2_1 |
---|
855 | CG(auto_globals) = OG(auto_globals); |
---|
856 | #endif |
---|
857 | |
---|
858 | if (install != XC_NoInstall) { |
---|
859 | CG(in_compilation) = 1; |
---|
860 | CG(compiled_filename) = sandbox->filename; |
---|
861 | CG(zend_lineno) = 0; |
---|
862 | xc_sandbox_install(sandbox, install TSRMLS_CC); |
---|
863 | CG(in_compilation) = 0; |
---|
864 | CG(compiled_filename) = NULL; |
---|
865 | |
---|
866 | /* no free as it's installed */ |
---|
867 | #ifdef HAVE_XCACHE_CONSTANT |
---|
868 | TG(zend_constants).pDestructor = NULL; |
---|
869 | #endif |
---|
870 | TG(function_table).pDestructor = NULL; |
---|
871 | TG(class_table).pDestructor = NULL; |
---|
872 | } |
---|
873 | |
---|
874 | /* destroy all the tmp */ |
---|
875 | #ifdef HAVE_XCACHE_CONSTANT |
---|
876 | zend_hash_destroy(&TG(zend_constants)); |
---|
877 | #endif |
---|
878 | zend_hash_destroy(&TG(function_table)); |
---|
879 | zend_hash_destroy(&TG(class_table)); |
---|
880 | #ifdef ZEND_ENGINE_2_1 |
---|
881 | zend_hash_destroy(&TG(auto_globals)); |
---|
882 | #endif |
---|
883 | zend_hash_destroy(TG(included_files)); |
---|
884 | |
---|
885 | /* restore orig here, as EG/CG holded tmp before */ |
---|
886 | memcpy(&EG(included_files), &OG(included_files), sizeof(EG(included_files))); |
---|
887 | |
---|
888 | #ifdef E_STRICT |
---|
889 | if (sandbox->compilererrors) { |
---|
890 | zend_uint i; |
---|
891 | for (i = 0; i < sandbox->compilererror_cnt; i ++) { |
---|
892 | efree(sandbox->compilererrors[i].error); |
---|
893 | } |
---|
894 | efree(sandbox->compilererrors); |
---|
895 | } |
---|
896 | #endif |
---|
897 | |
---|
898 | #ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES |
---|
899 | CG(compiler_options) = sandbox->orig_compiler_options; |
---|
900 | #endif |
---|
901 | |
---|
902 | if (sandbox->alloc) { |
---|
903 | efree(sandbox); |
---|
904 | } |
---|
905 | } |
---|
906 | /* }}} */ |
---|
907 | int xc_vtrace(const char *fmt, va_list args) /* {{{ */ |
---|
908 | { |
---|
909 | return vfprintf(stderr, fmt, args); |
---|
910 | } |
---|
911 | /* }}} */ |
---|
912 | int xc_trace(const char *fmt, ...) /* {{{ */ |
---|
913 | { |
---|
914 | va_list args; |
---|
915 | int ret; |
---|
916 | |
---|
917 | va_start(args, fmt); |
---|
918 | ret = xc_vtrace(fmt, args); |
---|
919 | va_end(args); |
---|
920 | return ret; |
---|
921 | } |
---|
922 | /* }}} */ |
---|