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