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