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