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