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