| 1 | |
|---|
| 2 | #include "xcache.h" |
|---|
| 3 | #include "stack.h" |
|---|
| 4 | #include "xcache_globals.h" |
|---|
| 5 | #include "utils.h" |
|---|
| 6 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 7 | #include "zend_vm.h" |
|---|
| 8 | #endif |
|---|
| 9 | #include "opcode_spec.h" |
|---|
| 10 | #undef NDEBUG |
|---|
| 11 | #include "assert.h" |
|---|
| 12 | |
|---|
| 13 | #ifndef max |
|---|
| 14 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | #ifndef ZEND_VM_SET_OPCODE_HANDLER |
|---|
| 18 | # define ZEND_VM_SET_OPCODE_HANDLER(opline) do { } while (0) |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | #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 |
|---|
| 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 | { |
|---|
| 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); |
|---|
| 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 | |
|---|
| 140 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 141 | if (!(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) { |
|---|
| 142 | return 0; |
|---|
| 143 | } |
|---|
| 144 | #else |
|---|
| 145 | if (!op_array->done_pass_two) { |
|---|
| 146 | return 0; |
|---|
| 147 | } |
|---|
| 148 | #endif |
|---|
| 149 | |
|---|
| 150 | opline = op_array->opcodes; |
|---|
| 151 | end = opline + op_array->last; |
|---|
| 152 | while (opline < end) { |
|---|
| 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 | |
|---|
| 162 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 163 | switch (opline->opcode) { |
|---|
| 164 | #ifdef ZEND_GOTO |
|---|
| 165 | case ZEND_GOTO: |
|---|
| 166 | #endif |
|---|
| 167 | case ZEND_JMP: |
|---|
| 168 | assert(Z_OP(opline->op1).jmp_addr >= op_array->opcodes && Z_OP(opline->op1).jmp_addr - op_array->opcodes < op_array->last); |
|---|
| 169 | Z_OP(opline->op1).opline_num = Z_OP(opline->op1).jmp_addr - op_array->opcodes; |
|---|
| 170 | break; |
|---|
| 171 | case ZEND_JMPZ: |
|---|
| 172 | case ZEND_JMPNZ: |
|---|
| 173 | case ZEND_JMPZ_EX: |
|---|
| 174 | case ZEND_JMPNZ_EX: |
|---|
| 175 | #ifdef ZEND_JMP_SET |
|---|
| 176 | case ZEND_JMP_SET: |
|---|
| 177 | #endif |
|---|
| 178 | assert(Z_OP(opline->op2).jmp_addr >= op_array->opcodes && Z_OP(opline->op2).jmp_addr - op_array->opcodes < op_array->last); |
|---|
| 179 | Z_OP(opline->op2).opline_num = Z_OP(opline->op2).jmp_addr - op_array->opcodes; |
|---|
| 180 | break; |
|---|
| 181 | } |
|---|
| 182 | #endif |
|---|
| 183 | opline++; |
|---|
| 184 | } |
|---|
| 185 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 186 | op_array->fn_flags &= ~ZEND_ACC_DONE_PASS_TWO; |
|---|
| 187 | #else |
|---|
| 188 | op_array->done_pass_two = 0; |
|---|
| 189 | #endif |
|---|
| 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; |
|---|
| 197 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 198 | zend_literal *literal = op_array->literals; |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 202 | if ((op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) { |
|---|
| 203 | return 0; |
|---|
| 204 | } |
|---|
| 205 | #else |
|---|
| 206 | if (op_array->done_pass_two) { |
|---|
| 207 | return 0; |
|---|
| 208 | } |
|---|
| 209 | #endif |
|---|
| 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 | */ |
|---|
| 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 |
|---|
| 225 | |
|---|
| 226 | opline = op_array->opcodes; |
|---|
| 227 | end = opline + op_array->last; |
|---|
| 228 | while (opline < end) { |
|---|
| 229 | #ifndef ZEND_ENGINE_2_4 |
|---|
| 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 */ |
|---|
| 233 | } |
|---|
| 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); |
|---|
| 237 | } |
|---|
| 238 | #endif |
|---|
| 239 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 240 | switch (opline->opcode) { |
|---|
| 241 | #ifdef ZEND_GOTO |
|---|
| 242 | case ZEND_GOTO: |
|---|
| 243 | #endif |
|---|
| 244 | case ZEND_JMP: |
|---|
| 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; |
|---|
| 247 | break; |
|---|
| 248 | case ZEND_JMPZ: |
|---|
| 249 | case ZEND_JMPNZ: |
|---|
| 250 | case ZEND_JMPZ_EX: |
|---|
| 251 | case ZEND_JMPNZ_EX: |
|---|
| 252 | #ifdef ZEND_JMP_SET |
|---|
| 253 | case ZEND_JMP_SET: |
|---|
| 254 | #endif |
|---|
| 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; |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
|---|
| 260 | #endif |
|---|
| 261 | opline++; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 265 | op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO; |
|---|
| 266 | #else |
|---|
| 267 | op_array->done_pass_two = 1; |
|---|
| 268 | #endif |
|---|
| 269 | return 0; |
|---|
| 270 | } |
|---|
| 271 | /* }}} */ |
|---|
| 272 | |
|---|
| 273 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| 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) /* {{{ */ |
|---|
| 275 | { |
|---|
| 276 | #ifdef ZEND_ENGINE_2 |
|---|
| 277 | if ((*op_type != IS_UNUSED && (spec == OPSPEC_UCLASS || spec == OPSPEC_CLASS)) || |
|---|
| 278 | spec == OPSPEC_FETCH) { |
|---|
| 279 | if (tofix) { |
|---|
| 280 | switch (*op_type) { |
|---|
| 281 | case IS_VAR: |
|---|
| 282 | case IS_TMP_VAR: |
|---|
| 283 | break; |
|---|
| 284 | |
|---|
| 285 | default: |
|---|
| 286 | /* TODO: data lost, find a way to keep it */ |
|---|
| 287 | /* assert(*op_type == IS_CONST); */ |
|---|
| 288 | *op_type = IS_TMP_VAR; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | switch (*op_type) { |
|---|
| 293 | case IS_TMP_VAR: |
|---|
| 294 | case IS_VAR: |
|---|
| 295 | if (tofix) { |
|---|
| 296 | Z_OP(*op).var /= sizeof(temp_variable); |
|---|
| 297 | } |
|---|
| 298 | else { |
|---|
| 299 | Z_OP(*op).var *= sizeof(temp_variable); |
|---|
| 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; |
|---|
| 309 | zend_uint i; |
|---|
| 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 | |
|---|
| 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); |
|---|
| 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 | /* }}} */ |
|---|
| 338 | #endif |
|---|
| 339 | |
|---|
| 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) { |
|---|
| 348 | #ifdef ZEND_GOTO |
|---|
| 349 | case ZEND_GOTO: |
|---|
| 350 | #endif |
|---|
| 351 | case ZEND_JMP: |
|---|
| 352 | next = begin + Z_OP(opline->op1).opline_num; |
|---|
| 353 | break; |
|---|
| 354 | |
|---|
| 355 | case ZEND_JMPZNZ: |
|---|
| 356 | next = begin + max(Z_OP(opline->op2).opline_num, opline->extended_value); |
|---|
| 357 | break; |
|---|
| 358 | |
|---|
| 359 | case ZEND_JMPZ: |
|---|
| 360 | case ZEND_JMPNZ: |
|---|
| 361 | case ZEND_JMPZ_EX: |
|---|
| 362 | case ZEND_JMPNZ_EX: |
|---|
| 363 | #ifdef ZEND_JMP_SET |
|---|
| 364 | case ZEND_JMP_SET: |
|---|
| 365 | #endif |
|---|
| 366 | next = begin + Z_OP(opline->op2).opline_num; |
|---|
| 367 | break; |
|---|
| 368 | |
|---|
| 369 | case ZEND_RETURN: |
|---|
| 370 | opline = end; |
|---|
| 371 | break; |
|---|
| 372 | |
|---|
| 373 | #ifdef ZEND_ENGINE_2 |
|---|
| 374 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 375 | callback(opline, opline - begin, data TSRMLS_CC); |
|---|
| 376 | break; |
|---|
| 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 |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | if (opline < next) { |
|---|
| 387 | opline = next; |
|---|
| 388 | } |
|---|
| 389 | else { |
|---|
| 390 | opline ++; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | return SUCCESS; |
|---|
| 394 | } |
|---|
| 395 | /* }}} */ |
|---|
| 396 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 397 | static int xc_do_early_binding(zend_op_array *op_array, HashTable *class_table, int oplineno TSRMLS_DC) /* {{{ */ |
|---|
| 398 | { |
|---|
| 399 | zend_op *opline; |
|---|
| 400 | |
|---|
| 401 | TRACE("binding %d", oplineno); |
|---|
| 402 | assert(oplineno >= 0); |
|---|
| 403 | |
|---|
| 404 | /* do early binding */ |
|---|
| 405 | opline = &(op_array->opcodes[oplineno]); |
|---|
| 406 | |
|---|
| 407 | switch (opline->opcode) { |
|---|
| 408 | #ifdef ZEND_ENGINE_2 |
|---|
| 409 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 410 | { |
|---|
| 411 | zval *parent_name; |
|---|
| 412 | zend_class_entry **pce; |
|---|
| 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 | |
|---|
| 419 | parent_name = &(Z_OP_CONSTANT((opline - 1)->op2)); |
|---|
| 420 | TRACE("binding with parent %s", Z_STRVAL_P(parent_name)); |
|---|
| 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 | |
|---|
| 435 | TRACE("%s %p", Z_STRVAL(Z_OP_CONSTANT(fetch_class_opline->op2)), Z_STRVAL(Z_OP_CONSTANT(fetch_class_opline->op2))); |
|---|
| 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 | } |
|---|
| 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 |
|---|
| 458 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
|---|
| 459 | if (do_bind_function_or_class(opline, NULL, class_table, 1) == FAILURE) { |
|---|
| 460 | return FAILURE; |
|---|
| 461 | } |
|---|
| 462 | #endif |
|---|
| 463 | break; |
|---|
| 464 | |
|---|
| 465 | default: |
|---|
| 466 | return FAILURE; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | zend_hash_del(class_table, Z_OP_CONSTANT(opline->op1).value.str.val, Z_OP_CONSTANT(opline->op1).value.str.len); |
|---|
| 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 | /* }}} */ |
|---|
| 481 | #endif |
|---|
| 482 | |
|---|
| 483 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 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) /* {{{ */ |
|---|
| 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; |
|---|
| 491 | #ifdef IS_UNICODE |
|---|
| 492 | zend_error(E_NOTICE, "Constant %R already defined", type, key); |
|---|
| 493 | #else |
|---|
| 494 | zend_error(E_NOTICE, "Constant %s already defined", key); |
|---|
| 495 | #endif |
|---|
| 496 | free(ZSTR_V(constant->name)); |
|---|
| 497 | if (!(constant->flags & CONST_PERSISTENT)) { |
|---|
| 498 | zval_dtor(&constant->value); |
|---|
| 499 | } |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | /* }}} */ |
|---|
| 503 | #endif |
|---|
| 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) /* {{{ */ |
|---|
| 505 | { |
|---|
| 506 | zend_bool istmpkey; |
|---|
| 507 | |
|---|
| 508 | if (func->type == ZEND_USER_FUNCTION) { |
|---|
| 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) { |
|---|
| 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, |
|---|
| 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); |
|---|
| 525 | #ifdef IS_UNICODE |
|---|
| 526 | zend_error(E_ERROR, "Cannot redeclare %R()", type, key); |
|---|
| 527 | #else |
|---|
| 528 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
|---|
| 529 | #endif |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | } |
|---|
| 533 | /* }}} */ |
|---|
| 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) /* {{{ */ |
|---|
| 535 | { |
|---|
| 536 | zend_bool istmpkey; |
|---|
| 537 | zend_class_entry *cep = CestToCePtr(*cest); |
|---|
| 538 | ZESW(void *stored_ce_ptr, NOTHING); |
|---|
| 539 | |
|---|
| 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) { |
|---|
| 546 | zend_u_hash_quick_update(CG(class_table), type, key, len, h, |
|---|
| 547 | cest, sizeof(xc_cest_t), |
|---|
| 548 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 549 | ); |
|---|
| 550 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 551 | if (oplineno != -1) { |
|---|
| 552 | xc_do_early_binding(CG(active_op_array), CG(class_table), oplineno TSRMLS_CC); |
|---|
| 553 | } |
|---|
| 554 | #endif |
|---|
| 555 | } |
|---|
| 556 | else if (zend_u_hash_quick_add(CG(class_table), type, key, len, h, |
|---|
| 557 | cest, sizeof(xc_cest_t), |
|---|
| 558 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 559 | ) == FAILURE) { |
|---|
| 560 | CG(zend_lineno) = ZESW(0, Z_CLASS_INFO(*cep).line_start); |
|---|
| 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 |
|---|
| 566 | assert(oplineno == -1); |
|---|
| 567 | } |
|---|
| 568 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
|---|
| 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 | /* }}} */ |
|---|
| 578 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 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); |
|---|
| 587 | switch (type) { |
|---|
| 588 | #ifdef E_STRICT |
|---|
| 589 | case E_STRICT: |
|---|
| 590 | #endif |
|---|
| 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: { |
|---|
| 611 | /* give up, and user handler is not supported in this case */ |
|---|
| 612 | zend_uint i; |
|---|
| 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; |
|---|
| 619 | zend_error(compilererror->type, "%s", compilererror->error); |
|---|
| 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); |
|---|
| 625 | break; |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | } |
|---|
| 629 | /* }}} */ |
|---|
| 630 | #endif |
|---|
| 631 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 632 | static zend_bool xc_auto_global_callback(ZEND_24(const) char *name, uint name_len TSRMLS_DC) /* {{{ */ |
|---|
| 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 |
|---|
| 650 | |
|---|
| 651 | void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker) /* {{{ */ |
|---|
| 652 | { |
|---|
| 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) { |
|---|
| 665 | zend_u_hash_quick_update(target, p->key.type, ZSTR(BUCKET_KEY_S(p)), p->nKeyLength, p->h, p->pData, size, &new_entry); |
|---|
| 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)); |
|---|
| 689 | ZSTR_U(c->name) = UNISW(zend_strndup, zend_ustrndup)(ZSTR_U(c->name), c->name_len - 1); |
|---|
| 690 | } |
|---|
| 691 | /* }}} */ |
|---|
| 692 | void xc_zend_constant_dtor(zend_constant *c) /* {{{ */ |
|---|
| 693 | { |
|---|
| 694 | free(ZSTR_V(c->name)); |
|---|
| 695 | } |
|---|
| 696 | /* }}} */ |
|---|
| 697 | static void xc_free_zend_constant(zend_constant *c) /* {{{ */ |
|---|
| 698 | { |
|---|
| 699 | if (!(c->flags & CONST_PERSISTENT)) { |
|---|
| 700 | zval_dtor(&c->value); |
|---|
| 701 | } |
|---|
| 702 | free(ZSTR_V(c->name)); |
|---|
| 703 | } |
|---|
| 704 | /* }}} */ |
|---|
| 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 |
|---|
| 712 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, ZEND_24(const) char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 713 | { |
|---|
| 714 | HashTable *h; |
|---|
| 715 | |
|---|
| 716 | if (sandbox) { |
|---|
| 717 | memset(sandbox, 0, sizeof(sandbox[0])); |
|---|
| 718 | } |
|---|
| 719 | else { |
|---|
| 720 | ECALLOC_ONE(sandbox); |
|---|
| 721 | sandbox->alloc = 1; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | memcpy(&OG(included_files), &EG(included_files), sizeof(EG(included_files))); |
|---|
| 725 | |
|---|
| 726 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 727 | OG(zend_constants) = EG(zend_constants); |
|---|
| 728 | EG(zend_constants) = &TG(zend_constants); |
|---|
| 729 | #endif |
|---|
| 730 | |
|---|
| 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 | |
|---|
| 738 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 739 | OG(auto_globals) = CG(auto_globals); |
|---|
| 740 | CG(auto_globals) = &TG(auto_globals); |
|---|
| 741 | #endif |
|---|
| 742 | |
|---|
| 743 | TG(included_files) = &EG(included_files); |
|---|
| 744 | |
|---|
| 745 | zend_hash_init_ex(TG(included_files), 5, NULL, NULL, 0, 1); |
|---|
| 746 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 747 | h = OG(zend_constants); |
|---|
| 748 | zend_hash_init_ex(&TG(zend_constants), 20, NULL, (dtor_func_t) xc_free_zend_constant, h->persistent, h->bApplyProtection); |
|---|
| 749 | xc_copy_internal_zend_constants(&TG(zend_constants), &XG(internal_constant_table)); |
|---|
| 750 | TG(internal_constant_tail) = TG(zend_constants).pListTail; |
|---|
| 751 | #endif |
|---|
| 752 | h = OG(function_table); |
|---|
| 753 | zend_hash_init_ex(&TG(function_table), 128, NULL, ZEND_FUNCTION_DTOR, h->persistent, h->bApplyProtection); |
|---|
| 754 | { |
|---|
| 755 | zend_function tmp_func; |
|---|
| 756 | zend_hash_copy(&TG(function_table), &XG(internal_function_table), NULL, (void *) &tmp_func, sizeof(tmp_func)); |
|---|
| 757 | } |
|---|
| 758 | TG(internal_function_tail) = TG(function_table).pListTail; |
|---|
| 759 | |
|---|
| 760 | h = OG(class_table); |
|---|
| 761 | zend_hash_init_ex(&TG(class_table), 16, NULL, ZEND_CLASS_DTOR, h->persistent, h->bApplyProtection); |
|---|
| 762 | #if 0 && TODO |
|---|
| 763 | { |
|---|
| 764 | xc_cest_t tmp_cest; |
|---|
| 765 | zend_hash_copy(&TG(class_table), &XG(internal_class_table), NULL, (void *) &tmp_cest, sizeof(tmp_cest)); |
|---|
| 766 | } |
|---|
| 767 | #endif |
|---|
| 768 | TG(internal_class_tail) = TG(class_table).pListTail; |
|---|
| 769 | |
|---|
| 770 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 771 | /* shallow copy, don't destruct */ |
|---|
| 772 | h = OG(auto_globals); |
|---|
| 773 | zend_hash_init_ex(&TG(auto_globals), 8, NULL, NULL, h->persistent, h->bApplyProtection); |
|---|
| 774 | { |
|---|
| 775 | zend_auto_global tmp_autoglobal; |
|---|
| 776 | |
|---|
| 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 | |
|---|
| 782 | sandbox->filename = filename; |
|---|
| 783 | |
|---|
| 784 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 785 | sandbox->orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting); |
|---|
| 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; |
|---|
| 792 | #endif |
|---|
| 793 | |
|---|
| 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 | */ |
|---|
| 798 | CG(compiler_options) |= ZEND_COMPILE_IGNORE_INTERNAL_CLASSES | ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_DELAYED_BINDING; |
|---|
| 799 | #endif |
|---|
| 800 | |
|---|
| 801 | XG(sandbox) = (void *) sandbox; |
|---|
| 802 | return sandbox; |
|---|
| 803 | } |
|---|
| 804 | /* }}} */ |
|---|
| 805 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 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 | /* }}} */ |
|---|
| 812 | #endif |
|---|
| 813 | static void xc_sandbox_install(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
|---|
| 814 | { |
|---|
| 815 | zend_uint i; |
|---|
| 816 | Bucket *b; |
|---|
| 817 | |
|---|
| 818 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 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 | |
|---|
| 824 | b = TG(internal_constant_tail) ? TG(internal_constant_tail)->pListNext : TG(zend_constants).pListHead; |
|---|
| 825 | /* install constants */ |
|---|
| 826 | while (b != NULL) { |
|---|
| 827 | zend_constant *c = (zend_constant*) b->pData; |
|---|
| 828 | xc_install_constant(sandbox->filename, c, |
|---|
| 829 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
|---|
| 830 | b = b->pListNext; |
|---|
| 831 | } |
|---|
| 832 | #endif |
|---|
| 833 | |
|---|
| 834 | b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; |
|---|
| 835 | /* install function */ |
|---|
| 836 | while (b != NULL) { |
|---|
| 837 | zend_function *func = (zend_function*) b->pData; |
|---|
| 838 | xc_install_function(sandbox->filename, func, |
|---|
| 839 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
|---|
| 840 | b = b->pListNext; |
|---|
| 841 | } |
|---|
| 842 | |
|---|
| 843 | b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; |
|---|
| 844 | /* install class */ |
|---|
| 845 | while (b != NULL) { |
|---|
| 846 | xc_install_class(sandbox->filename, (xc_cest_t*) b->pData, -1, |
|---|
| 847 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength, b->h TSRMLS_CC); |
|---|
| 848 | b = b->pListNext; |
|---|
| 849 | } |
|---|
| 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 | |
|---|
| 862 | if (install != XC_InstallNoBinding) { |
|---|
| 863 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 864 | zend_do_delayed_early_binding(CG(active_op_array) TSRMLS_CC); |
|---|
| 865 | #else |
|---|
| 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); |
|---|
| 869 | #endif |
|---|
| 870 | } |
|---|
| 871 | |
|---|
| 872 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 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; |
|---|
| 877 | zend_error(error->type, "%s", error->error); |
|---|
| 878 | } |
|---|
| 879 | CG(zend_lineno) = 0; |
|---|
| 880 | #endif |
|---|
| 881 | |
|---|
| 882 | i = 1; |
|---|
| 883 | zend_hash_add(&OG(included_files), sandbox->filename, strlen(sandbox->filename) + 1, (void *)&i, sizeof(int), NULL); |
|---|
| 884 | } |
|---|
| 885 | /* }}} */ |
|---|
| 886 | void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
|---|
| 887 | { |
|---|
| 888 | XG(sandbox) = NULL; |
|---|
| 889 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 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 | |
|---|
| 894 | /* restore first first install function/class */ |
|---|
| 895 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 896 | EG(zend_constants) = OG(zend_constants); |
|---|
| 897 | #endif |
|---|
| 898 | CG(function_table) = OG(function_table); |
|---|
| 899 | CG(class_table) = OG(class_table); |
|---|
| 900 | EG(class_table) = CG(class_table); |
|---|
| 901 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 902 | CG(auto_globals) = OG(auto_globals); |
|---|
| 903 | #endif |
|---|
| 904 | |
|---|
| 905 | if (install != XC_NoInstall) { |
|---|
| 906 | CG(in_compilation) = 1; |
|---|
| 907 | CG(compiled_filename) = sandbox->filename; |
|---|
| 908 | CG(zend_lineno) = 0; |
|---|
| 909 | xc_sandbox_install(sandbox, install TSRMLS_CC); |
|---|
| 910 | CG(in_compilation) = 0; |
|---|
| 911 | CG(compiled_filename) = NULL; |
|---|
| 912 | |
|---|
| 913 | /* no free as it's installed */ |
|---|
| 914 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 915 | TG(zend_constants).pDestructor = NULL; |
|---|
| 916 | #endif |
|---|
| 917 | TG(function_table).pDestructor = NULL; |
|---|
| 918 | TG(class_table).pDestructor = NULL; |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | /* destroy all the tmp */ |
|---|
| 922 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 923 | zend_hash_destroy(&TG(zend_constants)); |
|---|
| 924 | #endif |
|---|
| 925 | zend_hash_destroy(&TG(function_table)); |
|---|
| 926 | zend_hash_destroy(&TG(class_table)); |
|---|
| 927 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 928 | zend_hash_destroy(&TG(auto_globals)); |
|---|
| 929 | #endif |
|---|
| 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 | |
|---|
| 935 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 936 | if (sandbox->compilererrors) { |
|---|
| 937 | zend_uint i; |
|---|
| 938 | for (i = 0; i < sandbox->compilererror_cnt; i ++) { |
|---|
| 939 | efree(sandbox->compilererrors[i].error); |
|---|
| 940 | } |
|---|
| 941 | efree(sandbox->compilererrors); |
|---|
| 942 | } |
|---|
| 943 | #endif |
|---|
| 944 | |
|---|
| 945 | #ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES |
|---|
| 946 | CG(compiler_options) = sandbox->orig_compiler_options; |
|---|
| 947 | #endif |
|---|
| 948 | |
|---|
| 949 | if (sandbox->alloc) { |
|---|
| 950 | efree(sandbox); |
|---|
| 951 | } |
|---|
| 952 | } |
|---|
| 953 | /* }}} */ |
|---|
| 954 | int xc_vtrace(const char *fmt, va_list args) /* {{{ */ |
|---|
| 955 | { |
|---|
| 956 | return vfprintf(stderr, fmt, args); |
|---|
| 957 | } |
|---|
| 958 | /* }}} */ |
|---|
| 959 | int xc_trace(const char *fmt, ...) /* {{{ */ |
|---|
| 960 | { |
|---|
| 961 | va_list args; |
|---|
| 962 | int ret; |
|---|
| 963 | |
|---|
| 964 | va_start(args, fmt); |
|---|
| 965 | ret = xc_vtrace(fmt, args); |
|---|
| 966 | va_end(args); |
|---|
| 967 | return ret; |
|---|
| 968 | } |
|---|
| 969 | /* }}} */ |
|---|
| 970 | |
|---|
| 971 | #ifndef ZEND_ENGINE_2_3 |
|---|
| 972 | #include "ext/standard/php_string.h" |
|---|
| 973 | size_t xc_dirname(char *path, size_t len) /* {{{ */ |
|---|
| 974 | { |
|---|
| 975 | #ifdef ZEND_ENGINE_2 |
|---|
| 976 | return php_dirname(path, len); |
|---|
| 977 | #else |
|---|
| 978 | php_dirname(path, len); |
|---|
| 979 | return strlen(path); |
|---|
| 980 | #endif |
|---|
| 981 | } |
|---|
| 982 | /* }}} */ |
|---|
| 983 | |
|---|
| 984 | long xc_atol(const char *str, int str_len) /* {{{ */ |
|---|
| 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 |
|---|