| 1 | |
|---|
| 2 | #include "xcache.h" |
|---|
| 3 | #include "xcache_globals.h" |
|---|
| 4 | #include "xc_utils.h" |
|---|
| 5 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 6 | #include "zend_vm.h" |
|---|
| 7 | #endif |
|---|
| 8 | #include "xc_opcode_spec.h" |
|---|
| 9 | #include "../util/xc_trace.h" |
|---|
| 10 | |
|---|
| 11 | #ifndef max |
|---|
| 12 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
|---|
| 13 | #endif |
|---|
| 14 | |
|---|
| 15 | #ifndef ZEND_VM_SET_OPCODE_HANDLER |
|---|
| 16 | # define ZEND_VM_SET_OPCODE_HANDLER(opline) do { } while (0) |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 20 | # define OP_ZVAL_DTOR(op) do { } while(0) |
|---|
| 21 | #else |
|---|
| 22 | # define OP_ZVAL_DTOR(op) do { \ |
|---|
| 23 | Z_UNSET_ISREF(Z_OP_CONSTANT(op)); \ |
|---|
| 24 | zval_dtor(&Z_OP_CONSTANT(op)); \ |
|---|
| 25 | } while(0) |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | /* }}} */ |
|---|
| 29 | |
|---|
| 30 | xc_compile_result_t *xc_compile_result_init(xc_compile_result_t *cr, /* {{{ */ |
|---|
| 31 | zend_op_array *op_array, |
|---|
| 32 | HashTable *function_table, |
|---|
| 33 | HashTable *class_table) |
|---|
| 34 | { |
|---|
| 35 | assert(cr); |
|---|
| 36 | cr->op_array = op_array; |
|---|
| 37 | cr->function_table = function_table; |
|---|
| 38 | cr->class_table = class_table; |
|---|
| 39 | return cr; |
|---|
| 40 | } |
|---|
| 41 | /* }}} */ |
|---|
| 42 | xc_compile_result_t *xc_compile_result_init_cur(xc_compile_result_t *cr, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 43 | { |
|---|
| 44 | assert(cr); |
|---|
| 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 | } |
|---|
| 51 | /* }}} */ |
|---|
| 52 | |
|---|
| 53 | int xc_apply_function(zend_function *zf, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 54 | { |
|---|
| 55 | switch (zf->type) { |
|---|
| 56 | case ZEND_USER_FUNCTION: |
|---|
| 57 | case ZEND_EVAL_CODE: |
|---|
| 58 | return applyer(&zf->op_array TSRMLS_CC); |
|---|
| 59 | break; |
|---|
| 60 | |
|---|
| 61 | case ZEND_INTERNAL_FUNCTION: |
|---|
| 62 | case ZEND_OVERLOADED_FUNCTION: |
|---|
| 63 | break; |
|---|
| 64 | |
|---|
| 65 | EMPTY_SWITCH_DEFAULT_CASE(); |
|---|
| 66 | } |
|---|
| 67 | return 0; |
|---|
| 68 | } |
|---|
| 69 | /* }}} */ |
|---|
| 70 | typedef struct { |
|---|
| 71 | apply_func_t applyer; |
|---|
| 72 | zend_class_entry *ce; |
|---|
| 73 | } xc_apply_method_info; |
|---|
| 74 | int xc_apply_method(zend_function *zf, xc_apply_method_info *mi TSRMLS_DC) /* {{{ */ |
|---|
| 75 | { |
|---|
| 76 | /* avoid duplicate apply for shadowed method */ |
|---|
| 77 | #ifdef ZEND_ENGINE_2 |
|---|
| 78 | if (mi->ce != zf->common.scope) { |
|---|
| 79 | /* fprintf(stderr, "avoided duplicate %s\n", zf->common.function_name); */ |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|
| 82 | #else |
|---|
| 83 | char *name = zf->common.function_name; |
|---|
| 84 | int name_s = strlen(name) + 1; |
|---|
| 85 | zend_class_entry *ce; |
|---|
| 86 | zend_function *ptr; |
|---|
| 87 | |
|---|
| 88 | for (ce = mi->ce->parent; ce; ce = ce->parent) { |
|---|
| 89 | if (zend_hash_find(&ce->function_table, name, name_s, (void **) &ptr) == SUCCESS) { |
|---|
| 90 | if (ptr->op_array.refcount == zf->op_array.refcount) { |
|---|
| 91 | return 0; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | #endif |
|---|
| 96 | return xc_apply_function(zf, mi->applyer TSRMLS_CC); |
|---|
| 97 | } |
|---|
| 98 | /* }}} */ |
|---|
| 99 | #if 0 |
|---|
| 100 | int xc_apply_class(zend_class_entry *ce, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 101 | { |
|---|
| 102 | xc_apply_method_info mi; |
|---|
| 103 | |
|---|
| 104 | mi.applyer = applyer; |
|---|
| 105 | mi.ce = ce; |
|---|
| 106 | zend_hash_apply_with_argument(&(ce->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
|---|
| 107 | return 0; |
|---|
| 108 | } |
|---|
| 109 | /* }}} */ |
|---|
| 110 | #endif |
|---|
| 111 | static int xc_apply_cest(xc_cest_t *cest, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 112 | { |
|---|
| 113 | xc_apply_method_info mi; |
|---|
| 114 | |
|---|
| 115 | mi.applyer = applyer; |
|---|
| 116 | mi.ce = CestToCePtr(*cest); |
|---|
| 117 | zend_hash_apply_with_argument(&(CestToCePtr(*cest)->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
|---|
| 118 | return 0; |
|---|
| 119 | } |
|---|
| 120 | /* }}} */ |
|---|
| 121 | int xc_apply_op_array(xc_compile_result_t *cr, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 122 | { |
|---|
| 123 | zend_hash_apply_with_argument(cr->function_table, (apply_func_arg_t) xc_apply_function, (void *) applyer TSRMLS_CC); |
|---|
| 124 | zend_hash_apply_with_argument(cr->class_table, (apply_func_arg_t) xc_apply_cest, (void *) applyer TSRMLS_CC); |
|---|
| 125 | |
|---|
| 126 | return applyer(cr->op_array TSRMLS_CC); |
|---|
| 127 | } |
|---|
| 128 | /* }}} */ |
|---|
| 129 | int xc_undo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 130 | { |
|---|
| 131 | zend_op *opline, *opline_end; |
|---|
| 132 | |
|---|
| 133 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 134 | if (!(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) { |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | #else |
|---|
| 138 | if (!op_array->done_pass_two) { |
|---|
| 139 | return 0; |
|---|
| 140 | } |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | opline = op_array->opcodes; |
|---|
| 144 | opline_end = opline + op_array->last; |
|---|
| 145 | while (opline < opline_end) { |
|---|
| 146 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 147 | if (opline->op1_type == IS_CONST) { |
|---|
| 148 | opline->op1.constant = opline->op1.literal - op_array->literals; |
|---|
| 149 | } |
|---|
| 150 | if (opline->op2_type == IS_CONST) { |
|---|
| 151 | opline->op2.constant = opline->op2.literal - op_array->literals; |
|---|
| 152 | } |
|---|
| 153 | #endif |
|---|
| 154 | |
|---|
| 155 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 156 | switch (opline->opcode) { |
|---|
| 157 | #ifdef ZEND_GOTO |
|---|
| 158 | case ZEND_GOTO: |
|---|
| 159 | #endif |
|---|
| 160 | case ZEND_JMP: |
|---|
| 161 | assert(Z_OP(opline->op1).jmp_addr >= op_array->opcodes && (zend_uint) (Z_OP(opline->op1).jmp_addr - op_array->opcodes) < op_array->last); |
|---|
| 162 | Z_OP(opline->op1).opline_num = Z_OP(opline->op1).jmp_addr - op_array->opcodes; |
|---|
| 163 | break; |
|---|
| 164 | case ZEND_JMPZ: |
|---|
| 165 | case ZEND_JMPNZ: |
|---|
| 166 | case ZEND_JMPZ_EX: |
|---|
| 167 | case ZEND_JMPNZ_EX: |
|---|
| 168 | #ifdef ZEND_JMP_SET |
|---|
| 169 | case ZEND_JMP_SET: |
|---|
| 170 | #endif |
|---|
| 171 | #ifdef ZEND_JMP_SET_VAR |
|---|
| 172 | case ZEND_JMP_SET_VAR: |
|---|
| 173 | #endif |
|---|
| 174 | assert(Z_OP(opline->op2).jmp_addr >= op_array->opcodes && (zend_uint) (Z_OP(opline->op2).jmp_addr - op_array->opcodes) < op_array->last); |
|---|
| 175 | Z_OP(opline->op2).opline_num = Z_OP(opline->op2).jmp_addr - op_array->opcodes; |
|---|
| 176 | break; |
|---|
| 177 | } |
|---|
| 178 | #endif |
|---|
| 179 | opline++; |
|---|
| 180 | } |
|---|
| 181 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 182 | op_array->fn_flags &= ~ZEND_ACC_DONE_PASS_TWO; |
|---|
| 183 | #else |
|---|
| 184 | op_array->done_pass_two = 0; |
|---|
| 185 | #endif |
|---|
| 186 | |
|---|
| 187 | return 0; |
|---|
| 188 | } |
|---|
| 189 | /* }}} */ |
|---|
| 190 | int xc_redo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 191 | { |
|---|
| 192 | zend_op *opline, *opline_end; |
|---|
| 193 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 194 | zend_literal *literal = op_array->literals; |
|---|
| 195 | #endif |
|---|
| 196 | |
|---|
| 197 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 198 | if ((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) { |
|---|
| 199 | return 0; |
|---|
| 200 | } |
|---|
| 201 | #else |
|---|
| 202 | if (op_array->done_pass_two) { |
|---|
| 203 | return 0; |
|---|
| 204 | } |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | /* |
|---|
| 208 | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last); |
|---|
| 209 | op_array->size = op_array->last; |
|---|
| 210 | */ |
|---|
| 211 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 212 | if (literal) { |
|---|
| 213 | zend_literal *literal_end = literal + op_array->last_literal; |
|---|
| 214 | while (literal < literal_end) { |
|---|
| 215 | Z_SET_ISREF(literal->constant); |
|---|
| 216 | Z_SET_REFCOUNT(literal->constant, 2); /* Make sure is_ref won't be reset */ |
|---|
| 217 | literal++; |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | #endif |
|---|
| 221 | |
|---|
| 222 | opline = op_array->opcodes; |
|---|
| 223 | opline_end = opline + op_array->last; |
|---|
| 224 | while (opline < opline_end) { |
|---|
| 225 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 226 | if (opline->op1_type == IS_CONST) { |
|---|
| 227 | opline->op1.literal = op_array->literals + opline->op1.constant; |
|---|
| 228 | } |
|---|
| 229 | if (opline->op2_type == IS_CONST) { |
|---|
| 230 | opline->op2.literal = op_array->literals + opline->op2.constant; |
|---|
| 231 | } |
|---|
| 232 | #else |
|---|
| 233 | if (Z_OP_TYPE(opline->op1) == IS_CONST) { |
|---|
| 234 | Z_SET_ISREF(Z_OP_CONSTANT(opline->op1)); |
|---|
| 235 | Z_SET_REFCOUNT(Z_OP_CONSTANT(opline->op1), 2); /* Make sure is_ref won't be reset */ |
|---|
| 236 | } |
|---|
| 237 | if (Z_OP_TYPE(opline->op2) == IS_CONST) { |
|---|
| 238 | Z_SET_ISREF(Z_OP_CONSTANT(opline->op2)); |
|---|
| 239 | Z_SET_REFCOUNT(Z_OP_CONSTANT(opline->op2), 2); |
|---|
| 240 | } |
|---|
| 241 | #endif |
|---|
| 242 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 243 | switch (opline->opcode) { |
|---|
| 244 | #ifdef ZEND_GOTO |
|---|
| 245 | case ZEND_GOTO: |
|---|
| 246 | #endif |
|---|
| 247 | case ZEND_JMP: |
|---|
| 248 | assert(Z_OP(opline->op1).opline_num < op_array->last); |
|---|
| 249 | Z_OP(opline->op1).jmp_addr = op_array->opcodes + Z_OP(opline->op1).opline_num; |
|---|
| 250 | break; |
|---|
| 251 | case ZEND_JMPZ: |
|---|
| 252 | case ZEND_JMPNZ: |
|---|
| 253 | case ZEND_JMPZ_EX: |
|---|
| 254 | case ZEND_JMPNZ_EX: |
|---|
| 255 | #ifdef ZEND_JMP_SET |
|---|
| 256 | case ZEND_JMP_SET: |
|---|
| 257 | #endif |
|---|
| 258 | assert(Z_OP(opline->op2).opline_num < op_array->last); |
|---|
| 259 | Z_OP(opline->op2).jmp_addr = op_array->opcodes + Z_OP(opline->op2).opline_num; |
|---|
| 260 | break; |
|---|
| 261 | } |
|---|
| 262 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
|---|
| 263 | #endif |
|---|
| 264 | opline++; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 268 | op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO; |
|---|
| 269 | #else |
|---|
| 270 | op_array->done_pass_two = 1; |
|---|
| 271 | #endif |
|---|
| 272 | return 0; |
|---|
| 273 | } |
|---|
| 274 | /* }}} */ |
|---|
| 275 | |
|---|
| 276 | static void xc_fix_opcode_ex_znode(int tofix, xc_op_spec_t spec, Z_OP_TYPEOF_TYPE *op_type, znode_op *op, int type TSRMLS_DC) /* {{{ */ |
|---|
| 277 | { |
|---|
| 278 | #ifdef ZEND_ENGINE_2 |
|---|
| 279 | if ((*op_type != IS_UNUSED && (spec == OPSPEC_UCLASS || spec == OPSPEC_CLASS)) || |
|---|
| 280 | spec == OPSPEC_FETCH) { |
|---|
| 281 | if (tofix) { |
|---|
| 282 | switch (*op_type) { |
|---|
| 283 | case IS_VAR: |
|---|
| 284 | case IS_TMP_VAR: |
|---|
| 285 | break; |
|---|
| 286 | |
|---|
| 287 | default: |
|---|
| 288 | /* TODO: data lost, find a way to keep it */ |
|---|
| 289 | /* assert(*op_type == IS_CONST); */ |
|---|
| 290 | *op_type = IS_TMP_VAR; |
|---|
| 291 | } |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | switch (*op_type) { |
|---|
| 295 | case IS_TMP_VAR: |
|---|
| 296 | case IS_VAR: |
|---|
| 297 | if (tofix) { |
|---|
| 298 | Z_OP(*op).var /= sizeof(temp_variable); |
|---|
| 299 | } |
|---|
| 300 | else { |
|---|
| 301 | Z_OP(*op).var *= sizeof(temp_variable); |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | #endif |
|---|
| 305 | } |
|---|
| 306 | /* }}} */ |
|---|
| 307 | |
|---|
| 308 | static void xc_fix_opcode_ex(zend_op_array *op_array, int tofix TSRMLS_DC) /* {{{ */ |
|---|
| 309 | { |
|---|
| 310 | zend_op *opline; |
|---|
| 311 | zend_uint i; |
|---|
| 312 | |
|---|
| 313 | opline = op_array->opcodes; |
|---|
| 314 | for (i = 0; i < op_array->last; i ++, opline ++) { |
|---|
| 315 | /* 3rd optimizer may have ... */ |
|---|
| 316 | if (opline->opcode < xc_get_opcode_spec_count()) { |
|---|
| 317 | const xc_opcode_spec_t *spec; |
|---|
| 318 | spec = xc_get_opcode_spec(opline->opcode); |
|---|
| 319 | |
|---|
| 320 | xc_fix_opcode_ex_znode(tofix, spec->op1, &Z_OP_TYPE(opline->op1), &opline->op1, 0 TSRMLS_CC); |
|---|
| 321 | xc_fix_opcode_ex_znode(tofix, spec->op2, &Z_OP_TYPE(opline->op2), &opline->op2, 1 TSRMLS_CC); |
|---|
| 322 | xc_fix_opcode_ex_znode(tofix, spec->res, &Z_OP_TYPE(opline->result), &opline->result, 2 TSRMLS_CC); |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | /* }}} */ |
|---|
| 327 | int xc_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 328 | { |
|---|
| 329 | xc_fix_opcode_ex(op_array, 1 TSRMLS_CC); |
|---|
| 330 | return 0; |
|---|
| 331 | } |
|---|
| 332 | /* }}} */ |
|---|
| 333 | int xc_undo_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 334 | { |
|---|
| 335 | xc_fix_opcode_ex(op_array, 0 TSRMLS_CC); |
|---|
| 336 | |
|---|
| 337 | return 0; |
|---|
| 338 | } |
|---|
| 339 | /* }}} */ |
|---|
| 340 | |
|---|
| 341 | 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) /* {{{ */ |
|---|
| 342 | { |
|---|
| 343 | zend_op *opline, *begin, *opline_end, *next = NULL; |
|---|
| 344 | |
|---|
| 345 | opline = begin = op_array->opcodes; |
|---|
| 346 | opline_end = opline + op_array->last; |
|---|
| 347 | while (opline < opline_end) { |
|---|
| 348 | switch (opline->opcode) { |
|---|
| 349 | #ifdef ZEND_GOTO |
|---|
| 350 | case ZEND_GOTO: |
|---|
| 351 | #endif |
|---|
| 352 | case ZEND_JMP: |
|---|
| 353 | next = begin + Z_OP(opline->op1).opline_num; |
|---|
| 354 | break; |
|---|
| 355 | |
|---|
| 356 | case ZEND_JMPZNZ: |
|---|
| 357 | next = begin + max(Z_OP(opline->op2).opline_num, opline->extended_value); |
|---|
| 358 | break; |
|---|
| 359 | |
|---|
| 360 | case ZEND_JMPZ: |
|---|
| 361 | case ZEND_JMPNZ: |
|---|
| 362 | case ZEND_JMPZ_EX: |
|---|
| 363 | case ZEND_JMPNZ_EX: |
|---|
| 364 | #ifdef ZEND_JMP_SET |
|---|
| 365 | case ZEND_JMP_SET: |
|---|
| 366 | #endif |
|---|
| 367 | next = begin + Z_OP(opline->op2).opline_num; |
|---|
| 368 | break; |
|---|
| 369 | |
|---|
| 370 | case ZEND_RETURN: |
|---|
| 371 | opline = opline_end; |
|---|
| 372 | break; |
|---|
| 373 | |
|---|
| 374 | #ifdef ZEND_ENGINE_2 |
|---|
| 375 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 376 | callback(opline, opline - begin, data TSRMLS_CC); |
|---|
| 377 | break; |
|---|
| 378 | #else |
|---|
| 379 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
|---|
| 380 | if (opline->extended_value == ZEND_DECLARE_INHERITED_CLASS) { |
|---|
| 381 | callback(opline, opline - begin, data TSRMLS_CC); |
|---|
| 382 | } |
|---|
| 383 | break; |
|---|
| 384 | #endif |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | if (opline < next) { |
|---|
| 388 | opline = next; |
|---|
| 389 | } |
|---|
| 390 | else { |
|---|
| 391 | opline ++; |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | return SUCCESS; |
|---|
| 395 | } |
|---|
| 396 | /* }}} */ |
|---|
| 397 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 398 | int xc_do_early_binding(zend_op_array *op_array, HashTable *class_table, int oplineno TSRMLS_DC) /* {{{ */ |
|---|
| 399 | { |
|---|
| 400 | zend_op *opline; |
|---|
| 401 | |
|---|
| 402 | TRACE("binding %d", oplineno); |
|---|
| 403 | assert(oplineno >= 0); |
|---|
| 404 | |
|---|
| 405 | /* do early binding */ |
|---|
| 406 | opline = &(op_array->opcodes[oplineno]); |
|---|
| 407 | |
|---|
| 408 | switch (opline->opcode) { |
|---|
| 409 | #ifdef ZEND_ENGINE_2 |
|---|
| 410 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 411 | { |
|---|
| 412 | zval *parent_name; |
|---|
| 413 | zend_class_entry **pce; |
|---|
| 414 | |
|---|
| 415 | /* don't early-bind classes that implement interfaces */ |
|---|
| 416 | if ((opline + 1)->opcode == ZEND_FETCH_CLASS && (opline + 2)->opcode == ZEND_ADD_INTERFACE) { |
|---|
| 417 | return FAILURE; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | parent_name = &(Z_OP_CONSTANT((opline - 1)->op2)); |
|---|
| 421 | TRACE("binding with parent %s", Z_STRVAL_P(parent_name)); |
|---|
| 422 | if (zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) { |
|---|
| 423 | return FAILURE; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | if (do_bind_inherited_class(opline, class_table, *pce, 1 TSRMLS_CC) == NULL) { |
|---|
| 427 | return FAILURE; |
|---|
| 428 | } |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | /* clear unnecessary ZEND_FETCH_CLASS opcode */ |
|---|
| 432 | if (opline > op_array->opcodes |
|---|
| 433 | && (opline - 1)->opcode == ZEND_FETCH_CLASS) { |
|---|
| 434 | zend_op *fetch_class_opline = opline - 1; |
|---|
| 435 | |
|---|
| 436 | TRACE("%s %p", Z_STRVAL(Z_OP_CONSTANT(fetch_class_opline->op2)), Z_STRVAL(Z_OP_CONSTANT(fetch_class_opline->op2))); |
|---|
| 437 | OP_ZVAL_DTOR(fetch_class_opline->op2); |
|---|
| 438 | fetch_class_opline->opcode = ZEND_NOP; |
|---|
| 439 | ZEND_VM_SET_OPCODE_HANDLER(fetch_class_opline); |
|---|
| 440 | memset(&fetch_class_opline->op1, 0, sizeof(znode)); |
|---|
| 441 | memset(&fetch_class_opline->op2, 0, sizeof(znode)); |
|---|
| 442 | SET_UNUSED(fetch_class_opline->op1); |
|---|
| 443 | SET_UNUSED(fetch_class_opline->op2); |
|---|
| 444 | SET_UNUSED(fetch_class_opline->result); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | /* clear unnecessary ZEND_VERIFY_ABSTRACT_CLASS opcode */ |
|---|
| 448 | if ((opline + 1)->opcode == ZEND_VERIFY_ABSTRACT_CLASS) { |
|---|
| 449 | zend_op *abstract_op = opline + 1; |
|---|
| 450 | memset(abstract_op, 0, sizeof(abstract_op[0])); |
|---|
| 451 | abstract_op->lineno = 0; |
|---|
| 452 | SET_UNUSED(abstract_op->op1); |
|---|
| 453 | SET_UNUSED(abstract_op->op2); |
|---|
| 454 | SET_UNUSED(abstract_op->result); |
|---|
| 455 | abstract_op->opcode = ZEND_NOP; |
|---|
| 456 | ZEND_VM_SET_OPCODE_HANDLER(abstract_op); |
|---|
| 457 | } |
|---|
| 458 | #else |
|---|
| 459 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
|---|
| 460 | if (do_bind_function_or_class(opline, NULL, class_table, 1) == FAILURE) { |
|---|
| 461 | return FAILURE; |
|---|
| 462 | } |
|---|
| 463 | #endif |
|---|
| 464 | break; |
|---|
| 465 | |
|---|
| 466 | default: |
|---|
| 467 | return FAILURE; |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | zend_hash_del(class_table, Z_OP_CONSTANT(opline->op1).value.str.val, Z_OP_CONSTANT(opline->op1).value.str.len); |
|---|
| 471 | OP_ZVAL_DTOR(opline->op1); |
|---|
| 472 | OP_ZVAL_DTOR(opline->op2); |
|---|
| 473 | opline->opcode = ZEND_NOP; |
|---|
| 474 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
|---|
| 475 | memset(&opline->op1, 0, sizeof(znode)); |
|---|
| 476 | memset(&opline->op2, 0, sizeof(znode)); |
|---|
| 477 | SET_UNUSED(opline->op1); |
|---|
| 478 | SET_UNUSED(opline->op2); |
|---|
| 479 | return SUCCESS; |
|---|
| 480 | } |
|---|
| 481 | /* }}} */ |
|---|
| 482 | #endif |
|---|
| 483 | |
|---|
| 484 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 485 | void xc_install_constant(ZEND_24(NOTHING, const) char *filename, zend_constant *constant, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
|---|
| 486 | { |
|---|
| 487 | if (zend_u_hash_add(EG(zend_constants), type, key, len, |
|---|
| 488 | constant, sizeof(zend_constant), |
|---|
| 489 | NULL |
|---|
| 490 | ) == FAILURE) { |
|---|
| 491 | CG(zend_lineno) = 0; |
|---|
| 492 | #ifdef IS_UNICODE |
|---|
| 493 | zend_error(E_NOTICE, "Constant %R already defined", type, key); |
|---|
| 494 | #else |
|---|
| 495 | zend_error(E_NOTICE, "Constant %s already defined", key); |
|---|
| 496 | #endif |
|---|
| 497 | free(ZSTR_V(constant->name)); |
|---|
| 498 | if (!(constant->flags & CONST_PERSISTENT)) { |
|---|
| 499 | zval_dtor(&constant->value); |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | /* }}} */ |
|---|
| 504 | #endif |
|---|
| 505 | void xc_install_function(ZEND_24(NOTHING, const) char *filename, zend_function *func, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
|---|
| 506 | { |
|---|
| 507 | zend_bool istmpkey; |
|---|
| 508 | |
|---|
| 509 | if (func->type == ZEND_USER_FUNCTION) { |
|---|
| 510 | #ifdef IS_UNICODE |
|---|
| 511 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
|---|
| 512 | #else |
|---|
| 513 | istmpkey = ZSTR_S(key)[0] == 0; |
|---|
| 514 | #endif |
|---|
| 515 | if (istmpkey) { |
|---|
| 516 | zend_u_hash_update(CG(function_table), type, key, len, |
|---|
| 517 | func, sizeof(zend_op_array), |
|---|
| 518 | NULL |
|---|
| 519 | ); |
|---|
| 520 | } |
|---|
| 521 | else if (zend_u_hash_add(CG(function_table), type, key, len, |
|---|
| 522 | func, sizeof(zend_op_array), |
|---|
| 523 | NULL |
|---|
| 524 | ) == FAILURE) { |
|---|
| 525 | CG(zend_lineno) = ZESW(func->op_array.opcodes[0].lineno, func->op_array.line_start); |
|---|
| 526 | #ifdef IS_UNICODE |
|---|
| 527 | zend_error(E_ERROR, "Cannot redeclare %R()", type, key); |
|---|
| 528 | #else |
|---|
| 529 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
|---|
| 530 | #endif |
|---|
| 531 | } |
|---|
| 532 | } |
|---|
| 533 | } |
|---|
| 534 | /* }}} */ |
|---|
| 535 | ZESW(xc_cest_t *, void) xc_install_class(ZEND_24(NOTHING, const) char *filename, xc_cest_t *cest, int oplineno, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC) /* {{{ */ |
|---|
| 536 | { |
|---|
| 537 | zend_bool istmpkey; |
|---|
| 538 | zend_class_entry *cep = CestToCePtr(*cest); |
|---|
| 539 | ZESW(void *stored_ce_ptr, NOTHING); |
|---|
| 540 | |
|---|
| 541 | #ifdef IS_UNICODE |
|---|
| 542 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
|---|
| 543 | #else |
|---|
| 544 | istmpkey = ZSTR_S(key)[0] == 0; |
|---|
| 545 | #endif |
|---|
| 546 | if (istmpkey) { |
|---|
| 547 | zend_u_hash_quick_update(CG(class_table), type, key, len, h, |
|---|
| 548 | cest, sizeof(xc_cest_t), |
|---|
| 549 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 550 | ); |
|---|
| 551 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 552 | if (oplineno != -1) { |
|---|
| 553 | xc_do_early_binding(CG(active_op_array), CG(class_table), oplineno TSRMLS_CC); |
|---|
| 554 | } |
|---|
| 555 | #endif |
|---|
| 556 | } |
|---|
| 557 | else if (zend_u_hash_quick_add(CG(class_table), type, key, len, h, |
|---|
| 558 | cest, sizeof(xc_cest_t), |
|---|
| 559 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 560 | ) == FAILURE) { |
|---|
| 561 | CG(zend_lineno) = ZESW(0, Z_CLASS_INFO(*cep).line_start); |
|---|
| 562 | #ifdef IS_UNICODE |
|---|
| 563 | zend_error(E_ERROR, "Cannot redeclare class %R", type, cep->name); |
|---|
| 564 | #else |
|---|
| 565 | zend_error(E_ERROR, "Cannot redeclare class %s", cep->name); |
|---|
| 566 | #endif |
|---|
| 567 | assert(oplineno == -1); |
|---|
| 568 | } |
|---|
| 569 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
|---|
| 570 | } |
|---|
| 571 | /* }}} */ |
|---|
| 572 | |
|---|
| 573 | void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker) /* {{{ */ |
|---|
| 574 | { |
|---|
| 575 | Bucket *p; |
|---|
| 576 | void *new_entry; |
|---|
| 577 | zend_bool setTargetPointer; |
|---|
| 578 | |
|---|
| 579 | setTargetPointer = !target->pInternalPointer; |
|---|
| 580 | p = source->pListHead; |
|---|
| 581 | while (p) { |
|---|
| 582 | if (checker(p->pData)) { |
|---|
| 583 | if (setTargetPointer && source->pInternalPointer == p) { |
|---|
| 584 | target->pInternalPointer = NULL; |
|---|
| 585 | } |
|---|
| 586 | if (p->nKeyLength) { |
|---|
| 587 | zend_u_hash_quick_update(target, p->key.type, ZSTR(BUCKET_KEY_S(p)), p->nKeyLength, p->h, p->pData, size, &new_entry); |
|---|
| 588 | } else { |
|---|
| 589 | zend_hash_index_update(target, p->h, p->pData, size, &new_entry); |
|---|
| 590 | } |
|---|
| 591 | if (pCopyConstructor) { |
|---|
| 592 | pCopyConstructor(new_entry); |
|---|
| 593 | } |
|---|
| 594 | } |
|---|
| 595 | p = p->pListNext; |
|---|
| 596 | } |
|---|
| 597 | if (!target->pInternalPointer) { |
|---|
| 598 | target->pInternalPointer = target->pListHead; |
|---|
| 599 | } |
|---|
| 600 | } |
|---|
| 601 | /* }}} */ |
|---|
| 602 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 603 | static zend_bool xc_is_internal_zend_constant(zend_constant *c) /* {{{ */ |
|---|
| 604 | { |
|---|
| 605 | return (c->flags & CONST_PERSISTENT) ? 1 : 0; |
|---|
| 606 | } |
|---|
| 607 | /* }}} */ |
|---|
| 608 | void xc_zend_constant_ctor(zend_constant *c) /* {{{ */ |
|---|
| 609 | { |
|---|
| 610 | assert((c->flags & CONST_PERSISTENT)); |
|---|
| 611 | ZSTR_U(c->name) = UNISW(zend_strndup, zend_ustrndup)(ZSTR_U(c->name), c->name_len - 1); |
|---|
| 612 | } |
|---|
| 613 | /* }}} */ |
|---|
| 614 | void xc_zend_constant_dtor(zend_constant *c) /* {{{ */ |
|---|
| 615 | { |
|---|
| 616 | free(ZSTR_V(c->name)); |
|---|
| 617 | } |
|---|
| 618 | /* }}} */ |
|---|
| 619 | void xc_copy_internal_zend_constants(HashTable *target, HashTable *source) /* {{{ */ |
|---|
| 620 | { |
|---|
| 621 | zend_constant tmp_const; |
|---|
| 622 | 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); |
|---|
| 623 | } |
|---|
| 624 | /* }}} */ |
|---|
| 625 | #endif |
|---|