[212] | 1 | |
---|
[1] | 2 | #include "xcache.h" |
---|
[344] | 3 | #include "stack.h" |
---|
| 4 | #include "xcache_globals.h" |
---|
[1] | 5 | #include "utils.h" |
---|
| 6 | #ifdef ZEND_ENGINE_2_1 |
---|
| 7 | #include "zend_vm.h" |
---|
| 8 | #endif |
---|
| 9 | #include "opcode_spec.h" |
---|
| 10 | #undef NDEBUG |
---|
| 11 | #include "assert.h" |
---|
| 12 | |
---|
[213] | 13 | #ifndef max |
---|
| 14 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
---|
| 15 | #endif |
---|
| 16 | |
---|
[212] | 17 | #ifndef ZEND_VM_SET_OPCODE_HANDLER |
---|
| 18 | # define ZEND_VM_SET_OPCODE_HANDLER(opline) do { } while (0) |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #define OP_ZVAL_DTOR(op) do { \ |
---|
| 22 | (op).u.constant.is_ref = 0; \ |
---|
| 23 | zval_dtor(&(op).u.constant); \ |
---|
| 24 | } while(0) |
---|
[1] | 25 | xc_compile_result_t *xc_compile_result_init(xc_compile_result_t *cr, /* {{{ */ |
---|
| 26 | zend_op_array *op_array, |
---|
| 27 | HashTable *function_table, |
---|
| 28 | HashTable *class_table) |
---|
| 29 | { |
---|
| 30 | if (cr) { |
---|
| 31 | cr->alloc = 0; |
---|
| 32 | } |
---|
| 33 | else { |
---|
| 34 | cr = emalloc(sizeof(xc_compile_result_t)); |
---|
| 35 | cr->alloc = 1; |
---|
| 36 | } |
---|
| 37 | cr->op_array = op_array; |
---|
| 38 | cr->function_table = function_table; |
---|
| 39 | cr->class_table = class_table; |
---|
| 40 | return cr; |
---|
| 41 | } |
---|
| 42 | /* }}} */ |
---|
| 43 | xc_compile_result_t *xc_compile_result_init_cur(xc_compile_result_t *cr, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
| 44 | { |
---|
| 45 | return xc_compile_result_init(cr, op_array, CG(function_table), CG(class_table)); |
---|
| 46 | } |
---|
| 47 | /* }}} */ |
---|
| 48 | void xc_compile_result_free(xc_compile_result_t *cr) /* {{{ */ |
---|
| 49 | { |
---|
| 50 | if (cr->alloc) { |
---|
| 51 | efree(cr); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | /* }}} */ |
---|
| 55 | |
---|
| 56 | int xc_apply_function(zend_function *zf, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
| 57 | { |
---|
| 58 | switch (zf->type) { |
---|
| 59 | case ZEND_USER_FUNCTION: |
---|
| 60 | case ZEND_EVAL_CODE: |
---|
| 61 | return applyer(&zf->op_array TSRMLS_CC); |
---|
| 62 | break; |
---|
| 63 | |
---|
| 64 | case ZEND_INTERNAL_FUNCTION: |
---|
| 65 | case ZEND_OVERLOADED_FUNCTION: |
---|
| 66 | break; |
---|
| 67 | |
---|
| 68 | EMPTY_SWITCH_DEFAULT_CASE(); |
---|
| 69 | } |
---|
| 70 | return 0; |
---|
| 71 | } |
---|
| 72 | /* }}} */ |
---|
| 73 | typedef struct { |
---|
| 74 | apply_func_t applyer; |
---|
| 75 | zend_class_entry *ce; |
---|
| 76 | } xc_apply_method_info; |
---|
| 77 | int xc_apply_method(zend_function *zf, xc_apply_method_info *mi TSRMLS_DC) /* {{{ */ |
---|
| 78 | { |
---|
| 79 | /* avoid duplicate apply for shadowed method */ |
---|
| 80 | #ifdef ZEND_ENGINE_2 |
---|
| 81 | if (mi->ce != zf->common.scope) { |
---|
| 82 | /* fprintf(stderr, "avoided duplicate %s\n", zf->common.function_name); */ |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
| 85 | #else |
---|
| 86 | char *name = zf->common.function_name; |
---|
| 87 | int name_s = strlen(name) + 1; |
---|
| 88 | zend_class_entry *ce; |
---|
| 89 | zend_function *ptr; |
---|
| 90 | |
---|
| 91 | for (ce = mi->ce->parent; ce; ce = ce->parent) { |
---|
| 92 | if (zend_hash_find(&ce->function_table, name, name_s, (void **) &ptr) == SUCCESS) { |
---|
| 93 | if (ptr->op_array.refcount == zf->op_array.refcount) { |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | #endif |
---|
| 99 | return xc_apply_function(zf, mi->applyer TSRMLS_CC); |
---|
| 100 | } |
---|
| 101 | /* }}} */ |
---|
| 102 | #if 0 |
---|
| 103 | int xc_apply_class(zend_class_entry *ce, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
| 104 | { |
---|
| 105 | xc_apply_method_info mi; |
---|
| 106 | |
---|
| 107 | mi.applyer = applyer; |
---|
| 108 | mi.ce = ce; |
---|
| 109 | zend_hash_apply_with_argument(&(ce->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
---|
| 110 | return 0; |
---|
| 111 | } |
---|
| 112 | /* }}} */ |
---|
| 113 | #endif |
---|
| 114 | static int xc_apply_cest(xc_cest_t *cest, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
| 115 | { |
---|
| 116 | xc_apply_method_info mi; |
---|
| 117 | |
---|
| 118 | mi.applyer = applyer; |
---|
| 119 | mi.ce = CestToCePtr(*cest); |
---|
| 120 | zend_hash_apply_with_argument(&(CestToCePtr(*cest)->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | /* }}} */ |
---|
| 124 | int xc_apply_op_array(xc_compile_result_t *cr, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
---|
| 125 | { |
---|
[294] | 126 | zend_hash_apply_with_argument(cr->function_table, (apply_func_arg_t) xc_apply_function, (void *) applyer TSRMLS_CC); |
---|
| 127 | zend_hash_apply_with_argument(cr->class_table, (apply_func_arg_t) xc_apply_cest, (void *) applyer TSRMLS_CC); |
---|
[1] | 128 | |
---|
| 129 | return applyer(cr->op_array TSRMLS_CC); |
---|
| 130 | } |
---|
| 131 | /* }}} */ |
---|
| 132 | |
---|
| 133 | int xc_undo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
| 134 | { |
---|
| 135 | zend_op *opline, *end; |
---|
| 136 | |
---|
| 137 | if (!op_array->done_pass_two) { |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | opline = op_array->opcodes; |
---|
| 142 | end = opline + op_array->last; |
---|
| 143 | while (opline < end) { |
---|
| 144 | #ifdef ZEND_ENGINE_2_1 |
---|
| 145 | switch (opline->opcode) { |
---|
| 146 | case ZEND_JMP: |
---|
| 147 | opline->op1.u.opline_num = opline->op1.u.jmp_addr - op_array->opcodes; |
---|
| 148 | assert(opline->op1.u.opline_num < op_array->last); |
---|
| 149 | break; |
---|
| 150 | case ZEND_JMPZ: |
---|
| 151 | case ZEND_JMPNZ: |
---|
| 152 | case ZEND_JMPZ_EX: |
---|
| 153 | case ZEND_JMPNZ_EX: |
---|
| 154 | opline->op2.u.opline_num = opline->op2.u.jmp_addr - op_array->opcodes; |
---|
| 155 | assert(opline->op2.u.opline_num < op_array->last); |
---|
| 156 | break; |
---|
| 157 | } |
---|
| 158 | #endif |
---|
| 159 | opline++; |
---|
| 160 | } |
---|
| 161 | op_array->done_pass_two = 0; |
---|
| 162 | |
---|
| 163 | return 0; |
---|
| 164 | } |
---|
| 165 | /* }}} */ |
---|
| 166 | int xc_redo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
| 167 | { |
---|
| 168 | zend_op *opline, *end; |
---|
| 169 | |
---|
| 170 | if (op_array->done_pass_two) { |
---|
| 171 | return 0; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* |
---|
| 175 | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last); |
---|
| 176 | op_array->size = op_array->last; |
---|
| 177 | */ |
---|
| 178 | |
---|
| 179 | opline = op_array->opcodes; |
---|
| 180 | end = opline + op_array->last; |
---|
| 181 | while (opline < end) { |
---|
| 182 | if (opline->op1.op_type == IS_CONST) { |
---|
| 183 | opline->op1.u.constant.is_ref = 1; |
---|
| 184 | opline->op1.u.constant.refcount = 2; /* Make sure is_ref won't be reset */ |
---|
| 185 | } |
---|
| 186 | if (opline->op2.op_type == IS_CONST) { |
---|
| 187 | opline->op2.u.constant.is_ref = 1; |
---|
| 188 | opline->op2.u.constant.refcount = 2; |
---|
| 189 | } |
---|
| 190 | #ifdef ZEND_ENGINE_2_1 |
---|
| 191 | switch (opline->opcode) { |
---|
| 192 | case ZEND_JMP: |
---|
| 193 | assert(opline->op1.u.opline_num < op_array->last); |
---|
| 194 | opline->op1.u.jmp_addr = op_array->opcodes + opline->op1.u.opline_num; |
---|
| 195 | break; |
---|
| 196 | case ZEND_JMPZ: |
---|
| 197 | case ZEND_JMPNZ: |
---|
| 198 | case ZEND_JMPZ_EX: |
---|
| 199 | case ZEND_JMPNZ_EX: |
---|
| 200 | assert(opline->op2.u.opline_num < op_array->last); |
---|
| 201 | opline->op2.u.jmp_addr = op_array->opcodes + opline->op2.u.opline_num; |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
---|
| 205 | #endif |
---|
| 206 | opline++; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | op_array->done_pass_two = 1; |
---|
| 210 | return 0; |
---|
| 211 | } |
---|
| 212 | /* }}} */ |
---|
| 213 | |
---|
[8] | 214 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
---|
[1] | 215 | static void xc_fix_opcode_ex_znode(int tofix, xc_op_spec_t spec, znode *znode, int type TSRMLS_DC) /* {{{ */ |
---|
| 216 | { |
---|
| 217 | #ifdef ZEND_ENGINE_2 |
---|
| 218 | if ((znode->op_type != IS_UNUSED && (spec == OPSPEC_UCLASS || spec == OPSPEC_CLASS)) || |
---|
| 219 | spec == OPSPEC_FETCH) { |
---|
| 220 | if (tofix) { |
---|
| 221 | switch (znode->op_type) { |
---|
| 222 | case IS_VAR: |
---|
| 223 | case IS_TMP_VAR: |
---|
| 224 | break; |
---|
| 225 | |
---|
| 226 | default: |
---|
| 227 | /* TODO: data lost, find a way to keep it */ |
---|
| 228 | /* assert(znode->op_type == IS_CONST); */ |
---|
| 229 | znode->op_type = IS_TMP_VAR; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | switch (znode->op_type) { |
---|
| 234 | case IS_TMP_VAR: |
---|
| 235 | case IS_VAR: |
---|
| 236 | if (tofix) { |
---|
| 237 | znode->u.var /= sizeof(temp_variable); |
---|
| 238 | } |
---|
| 239 | else { |
---|
| 240 | znode->u.var *= sizeof(temp_variable); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | #endif |
---|
| 244 | } |
---|
| 245 | /* }}} */ |
---|
| 246 | |
---|
| 247 | static void xc_fix_opcode_ex(zend_op_array *op_array, int tofix TSRMLS_DC) /* {{{ */ |
---|
| 248 | { |
---|
| 249 | zend_op *opline; |
---|
[11] | 250 | zend_uint i; |
---|
[1] | 251 | |
---|
| 252 | opline = op_array->opcodes; |
---|
| 253 | for (i = 0; i < op_array->last; i ++, opline ++) { |
---|
| 254 | /* 3rd optimizer may have ... */ |
---|
| 255 | if (opline->opcode < xc_get_opcode_spec_count()) { |
---|
| 256 | const xc_opcode_spec_t *spec; |
---|
| 257 | spec = xc_get_opcode_spec(opline->opcode); |
---|
| 258 | |
---|
| 259 | xc_fix_opcode_ex_znode(tofix, spec->op1, &opline->op1, 0 TSRMLS_CC); |
---|
| 260 | xc_fix_opcode_ex_znode(tofix, spec->op2, &opline->op2, 1 TSRMLS_CC); |
---|
| 261 | xc_fix_opcode_ex_znode(tofix, spec->res, &opline->result, 2 TSRMLS_CC); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | /* }}} */ |
---|
| 266 | int xc_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
| 267 | { |
---|
| 268 | xc_fix_opcode_ex(op_array, 1 TSRMLS_CC); |
---|
| 269 | return 0; |
---|
| 270 | } |
---|
| 271 | /* }}} */ |
---|
| 272 | int xc_undo_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
| 273 | { |
---|
| 274 | xc_fix_opcode_ex(op_array, 0 TSRMLS_CC); |
---|
| 275 | |
---|
| 276 | return 0; |
---|
| 277 | } |
---|
| 278 | /* }}} */ |
---|
[8] | 279 | #endif |
---|
[1] | 280 | |
---|
[212] | 281 | 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) /* {{{ */ |
---|
| 282 | { |
---|
| 283 | zend_op *opline, *begin, *end, *next = NULL; |
---|
| 284 | |
---|
| 285 | opline = begin = op_array->opcodes; |
---|
| 286 | end = opline + op_array->last; |
---|
| 287 | while (opline < end) { |
---|
| 288 | switch (opline->opcode) { |
---|
| 289 | case ZEND_JMP: |
---|
| 290 | next = begin + opline->op1.u.opline_num; |
---|
| 291 | break; |
---|
| 292 | |
---|
| 293 | case ZEND_JMPZNZ: |
---|
| 294 | next = begin + max(opline->op2.u.opline_num, opline->extended_value); |
---|
| 295 | break; |
---|
| 296 | |
---|
| 297 | case ZEND_JMPZ: |
---|
| 298 | case ZEND_JMPNZ: |
---|
| 299 | case ZEND_JMPZ_EX: |
---|
| 300 | case ZEND_JMPNZ_EX: |
---|
| 301 | next = begin + opline->op2.u.opline_num; |
---|
| 302 | break; |
---|
| 303 | |
---|
| 304 | case ZEND_RETURN: |
---|
| 305 | opline = end; |
---|
| 306 | break; |
---|
| 307 | |
---|
[226] | 308 | #ifdef ZEND_ENGINE_2 |
---|
[212] | 309 | case ZEND_DECLARE_INHERITED_CLASS: |
---|
| 310 | callback(opline, opline - begin, data TSRMLS_CC); |
---|
| 311 | break; |
---|
[226] | 312 | #else |
---|
| 313 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
---|
| 314 | if (opline->extended_value == ZEND_DECLARE_INHERITED_CLASS) { |
---|
| 315 | callback(opline, opline - begin, data TSRMLS_CC); |
---|
| 316 | } |
---|
| 317 | break; |
---|
| 318 | #endif |
---|
[212] | 319 | } |
---|
| 320 | |
---|
| 321 | if (opline < next) { |
---|
| 322 | opline = next; |
---|
| 323 | } |
---|
| 324 | else { |
---|
| 325 | opline ++; |
---|
| 326 | } |
---|
| 327 | } |
---|
[295] | 328 | return SUCCESS; |
---|
[212] | 329 | } |
---|
| 330 | /* }}} */ |
---|
[214] | 331 | static int xc_do_early_binding(zend_op_array *op_array, HashTable *class_table, int oplineno TSRMLS_DC) /* {{{ */ |
---|
[212] | 332 | { |
---|
[295] | 333 | zend_op *opline; |
---|
[212] | 334 | |
---|
[305] | 335 | TRACE("binding %d", oplineno); |
---|
[212] | 336 | assert(oplineno >= 0); |
---|
| 337 | |
---|
| 338 | /* do early binding */ |
---|
| 339 | opline = &(op_array->opcodes[oplineno]); |
---|
| 340 | |
---|
| 341 | switch (opline->opcode) { |
---|
[226] | 342 | #ifdef ZEND_ENGINE_2 |
---|
[212] | 343 | case ZEND_DECLARE_INHERITED_CLASS: |
---|
| 344 | { |
---|
| 345 | zval *parent_name; |
---|
| 346 | zend_class_entry **pce; |
---|
[214] | 347 | |
---|
| 348 | /* don't early-bind classes that implement interfaces */ |
---|
| 349 | if ((opline + 1)->opcode == ZEND_FETCH_CLASS && (opline + 2)->opcode == ZEND_ADD_INTERFACE) { |
---|
| 350 | return FAILURE; |
---|
| 351 | } |
---|
| 352 | |
---|
[212] | 353 | parent_name = &(opline - 1)->op2.u.constant; |
---|
[305] | 354 | TRACE("binding with parent %s", Z_STRVAL_P(parent_name)); |
---|
[212] | 355 | if (zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) { |
---|
| 356 | return FAILURE; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | if (do_bind_inherited_class(opline, class_table, *pce, 1 TSRMLS_CC) == NULL) { |
---|
| 360 | return FAILURE; |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /* clear unnecessary ZEND_FETCH_CLASS opcode */ |
---|
| 365 | if (opline > op_array->opcodes |
---|
| 366 | && (opline - 1)->opcode == ZEND_FETCH_CLASS) { |
---|
| 367 | zend_op *fetch_class_opline = opline - 1; |
---|
| 368 | |
---|
[305] | 369 | TRACE("%s %p", Z_STRVAL(fetch_class_opline->op2.u.constant), Z_STRVAL(fetch_class_opline->op2.u.constant)); |
---|
[212] | 370 | OP_ZVAL_DTOR(fetch_class_opline->op2); |
---|
| 371 | fetch_class_opline->opcode = ZEND_NOP; |
---|
| 372 | ZEND_VM_SET_OPCODE_HANDLER(fetch_class_opline); |
---|
| 373 | memset(&fetch_class_opline->op1, 0, sizeof(znode)); |
---|
| 374 | memset(&fetch_class_opline->op2, 0, sizeof(znode)); |
---|
| 375 | SET_UNUSED(fetch_class_opline->op1); |
---|
| 376 | SET_UNUSED(fetch_class_opline->op2); |
---|
| 377 | SET_UNUSED(fetch_class_opline->result); |
---|
| 378 | } |
---|
[214] | 379 | |
---|
| 380 | /* clear unnecessary ZEND_VERIFY_ABSTRACT_CLASS opcode */ |
---|
| 381 | if ((opline + 1)->opcode == ZEND_VERIFY_ABSTRACT_CLASS) { |
---|
| 382 | zend_op *abstract_op = opline + 1; |
---|
| 383 | memset(abstract_op, 0, sizeof(abstract_op[0])); |
---|
| 384 | abstract_op->lineno = 0; |
---|
| 385 | SET_UNUSED(abstract_op->op1); |
---|
| 386 | SET_UNUSED(abstract_op->op2); |
---|
| 387 | SET_UNUSED(abstract_op->result); |
---|
| 388 | abstract_op->opcode = ZEND_NOP; |
---|
| 389 | ZEND_VM_SET_OPCODE_HANDLER(abstract_op); |
---|
| 390 | } |
---|
| 391 | #else |
---|
[226] | 392 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
---|
[214] | 393 | if (do_bind_function_or_class(opline, NULL, class_table, 1) == FAILURE) { |
---|
| 394 | return FAILURE; |
---|
| 395 | } |
---|
[212] | 396 | #endif |
---|
| 397 | break; |
---|
| 398 | |
---|
| 399 | default: |
---|
| 400 | return FAILURE; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | zend_hash_del(class_table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len); |
---|
| 404 | OP_ZVAL_DTOR(opline->op1); |
---|
| 405 | OP_ZVAL_DTOR(opline->op2); |
---|
| 406 | opline->opcode = ZEND_NOP; |
---|
| 407 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
---|
| 408 | memset(&opline->op1, 0, sizeof(znode)); |
---|
| 409 | memset(&opline->op2, 0, sizeof(znode)); |
---|
| 410 | SET_UNUSED(opline->op1); |
---|
| 411 | SET_UNUSED(opline->op2); |
---|
| 412 | return SUCCESS; |
---|
| 413 | } |
---|
| 414 | /* }}} */ |
---|
| 415 | |
---|
[95] | 416 | #ifdef HAVE_XCACHE_CONSTANT |
---|
[103] | 417 | void xc_install_constant(char *filename, zend_constant *constant, zend_uchar type, zstr key, uint len TSRMLS_DC) /* {{{ */ |
---|
[95] | 418 | { |
---|
| 419 | if (zend_u_hash_add(EG(zend_constants), type, key, len, |
---|
| 420 | constant, sizeof(zend_constant), |
---|
| 421 | NULL |
---|
| 422 | ) == FAILURE) { |
---|
| 423 | CG(zend_lineno) = 0; |
---|
[103] | 424 | #ifdef IS_UNICODE |
---|
| 425 | zend_error(E_NOTICE, "Constant %R already defined", type, key); |
---|
| 426 | #else |
---|
[95] | 427 | zend_error(E_NOTICE, "Constant %s already defined", key); |
---|
[103] | 428 | #endif |
---|
| 429 | free(ZSTR_V(constant->name)); |
---|
[95] | 430 | if (!(constant->flags & CONST_PERSISTENT)) { |
---|
| 431 | zval_dtor(&constant->value); |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | } |
---|
| 435 | /* }}} */ |
---|
| 436 | #endif |
---|
[103] | 437 | void xc_install_function(char *filename, zend_function *func, zend_uchar type, zstr key, uint len TSRMLS_DC) /* {{{ */ |
---|
[1] | 438 | { |
---|
[103] | 439 | zend_bool istmpkey; |
---|
| 440 | |
---|
[1] | 441 | if (func->type == ZEND_USER_FUNCTION) { |
---|
[103] | 442 | #ifdef IS_UNICODE |
---|
| 443 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
---|
| 444 | #else |
---|
| 445 | istmpkey = ZSTR_S(key)[0] == 0; |
---|
| 446 | #endif |
---|
| 447 | if (istmpkey) { |
---|
[88] | 448 | zend_u_hash_update(CG(function_table), type, key, len, |
---|
| 449 | func, sizeof(zend_op_array), |
---|
| 450 | NULL |
---|
| 451 | ); |
---|
| 452 | } |
---|
| 453 | else if (zend_u_hash_add(CG(function_table), type, key, len, |
---|
[1] | 454 | func, sizeof(zend_op_array), |
---|
| 455 | NULL |
---|
| 456 | ) == FAILURE) { |
---|
| 457 | CG(zend_lineno) = ZESW(func->op_array.opcodes[0].lineno, func->op_array.line_start); |
---|
[103] | 458 | #ifdef IS_UNICODE |
---|
| 459 | zend_error(E_ERROR, "Cannot redeclare %R()", type, key); |
---|
| 460 | #else |
---|
[1] | 461 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
---|
[103] | 462 | #endif |
---|
[1] | 463 | } |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | /* }}} */ |
---|
[212] | 467 | ZESW(xc_cest_t *, void) xc_install_class(char *filename, xc_cest_t *cest, int oplineno, zend_uchar type, zstr key, uint len TSRMLS_DC) /* {{{ */ |
---|
[1] | 468 | { |
---|
[103] | 469 | zend_bool istmpkey; |
---|
[11] | 470 | zend_class_entry *cep = CestToCePtr(*cest); |
---|
[19] | 471 | ZESW(void *stored_ce_ptr, NOTHING); |
---|
[1] | 472 | |
---|
[103] | 473 | #ifdef IS_UNICODE |
---|
| 474 | istmpkey = (type == IS_STRING && ZSTR_S(key)[0] == 0) || ZSTR_U(key)[0] == 0; |
---|
| 475 | #else |
---|
| 476 | istmpkey = ZSTR_S(key)[0] == 0; |
---|
| 477 | #endif |
---|
| 478 | if (istmpkey) { |
---|
[88] | 479 | zend_u_hash_update(CG(class_table), type, key, len, |
---|
| 480 | cest, sizeof(xc_cest_t), |
---|
| 481 | ZESW(&stored_ce_ptr, NULL) |
---|
| 482 | ); |
---|
[212] | 483 | if (oplineno != -1) { |
---|
| 484 | xc_do_early_binding(CG(active_op_array), CG(class_table), oplineno TSRMLS_CC); |
---|
| 485 | } |
---|
[88] | 486 | } |
---|
| 487 | else if (zend_u_hash_add(CG(class_table), type, key, len, |
---|
[1] | 488 | cest, sizeof(xc_cest_t), |
---|
| 489 | ZESW(&stored_ce_ptr, NULL) |
---|
| 490 | ) == FAILURE) { |
---|
| 491 | CG(zend_lineno) = ZESW(0, cep->line_start); |
---|
[103] | 492 | #ifdef IS_UNICODE |
---|
| 493 | zend_error(E_ERROR, "Cannot redeclare class %R", type, cep->name); |
---|
| 494 | #else |
---|
| 495 | zend_error(E_ERROR, "Cannot redeclare class %s", cep->name); |
---|
| 496 | #endif |
---|
[212] | 497 | assert(oplineno == -1); |
---|
[1] | 498 | } |
---|
[19] | 499 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
---|
[1] | 500 | } |
---|
| 501 | /* }}} */ |
---|
| 502 | |
---|
| 503 | /* sandbox {{{ */ |
---|
| 504 | #undef TG |
---|
| 505 | #undef OG |
---|
| 506 | #define TG(x) (sandbox->tmp_##x) |
---|
| 507 | #define OG(x) (sandbox->orig_##x) |
---|
| 508 | /* }}} */ |
---|
[268] | 509 | #ifdef ZEND_ENGINE_2_1 |
---|
| 510 | static zend_bool xc_auto_global_callback(char *name, uint name_len TSRMLS_DC) /* {{{ */ |
---|
| 511 | { |
---|
| 512 | zend_auto_global *auto_global; |
---|
| 513 | if (zend_u_hash_find(CG(auto_globals), UG(unicode) ? IS_UNICODE : IS_STRING, ZSTR(name), name_len + 1, (void **) &auto_global) == FAILURE) { |
---|
| 514 | return 1; |
---|
| 515 | } |
---|
| 516 | return 0; |
---|
| 517 | } |
---|
| 518 | /* }}} */ |
---|
| 519 | static int xc_auto_global_arm(zend_auto_global *auto_global TSRMLS_DC) /* {{{ */ |
---|
| 520 | { |
---|
| 521 | if (auto_global->auto_global_callback) { |
---|
| 522 | auto_global->armed = 1; |
---|
| 523 | auto_global->auto_global_callback = xc_auto_global_callback; |
---|
| 524 | } |
---|
| 525 | else { |
---|
| 526 | auto_global->armed = 0; |
---|
| 527 | } |
---|
| 528 | return ZEND_HASH_APPLY_KEEP; |
---|
| 529 | } |
---|
| 530 | /* }}} */ |
---|
| 531 | #endif |
---|
[1] | 532 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC) /* {{{ */ |
---|
| 533 | { |
---|
[269] | 534 | HashTable *h; |
---|
[344] | 535 | zend_function tmp_func; |
---|
| 536 | xc_cest_t tmp_cest; |
---|
| 537 | |
---|
[1] | 538 | if (sandbox) { |
---|
| 539 | memset(sandbox, 0, sizeof(sandbox[0])); |
---|
| 540 | } |
---|
| 541 | else { |
---|
| 542 | ECALLOC_ONE(sandbox); |
---|
| 543 | sandbox->alloc = 1; |
---|
| 544 | } |
---|
[95] | 545 | |
---|
[1] | 546 | memcpy(&OG(included_files), &EG(included_files), sizeof(EG(included_files))); |
---|
| 547 | |
---|
[95] | 548 | #ifdef HAVE_XCACHE_CONSTANT |
---|
| 549 | OG(zend_constants) = EG(zend_constants); |
---|
| 550 | EG(zend_constants) = &TG(zend_constants); |
---|
| 551 | #endif |
---|
| 552 | |
---|
[1] | 553 | OG(function_table) = CG(function_table); |
---|
| 554 | CG(function_table) = &TG(function_table); |
---|
| 555 | |
---|
| 556 | OG(class_table) = CG(class_table); |
---|
| 557 | CG(class_table) = &TG(class_table); |
---|
| 558 | EG(class_table) = CG(class_table); |
---|
| 559 | |
---|
[268] | 560 | #ifdef ZEND_ENGINE_2_1 |
---|
| 561 | OG(auto_globals) = CG(auto_globals); |
---|
| 562 | CG(auto_globals) = &TG(auto_globals); |
---|
| 563 | #endif |
---|
| 564 | |
---|
[1] | 565 | TG(included_files) = &EG(included_files); |
---|
| 566 | |
---|
| 567 | zend_hash_init_ex(TG(included_files), 5, NULL, NULL, 0, 1); |
---|
[95] | 568 | #ifdef HAVE_XCACHE_CONSTANT |
---|
[269] | 569 | h = OG(zend_constants); |
---|
| 570 | zend_hash_init_ex(&TG(zend_constants), 20, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
[95] | 571 | #endif |
---|
[270] | 572 | h = OG(function_table); |
---|
[269] | 573 | zend_hash_init_ex(&TG(function_table), 128, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
[344] | 574 | zend_hash_copy(&TG(function_table), &XG(internal_function_table), NULL, (void *) &tmp_func, sizeof(tmp_func)); |
---|
| 575 | TG(internal_class_tail) = TG(function_table).pListTail; |
---|
| 576 | |
---|
[270] | 577 | h = OG(class_table); |
---|
[269] | 578 | zend_hash_init_ex(&TG(class_table), 16, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
---|
[344] | 579 | zend_hash_copy(&TG(class_table), &XG(internal_class_table), NULL, (void *) &tmp_cest, sizeof(tmp_cest)); |
---|
| 580 | TG(internal_class_tail) = TG(class_table).pListTail; |
---|
| 581 | |
---|
[268] | 582 | #ifdef ZEND_ENGINE_2_1 |
---|
[269] | 583 | /* shallow copy, don't destruct */ |
---|
[270] | 584 | h = OG(auto_globals); |
---|
[269] | 585 | zend_hash_init_ex(&TG(auto_globals), 8, NULL, NULL, h->persistent, h->bApplyProtection); |
---|
[268] | 586 | { |
---|
| 587 | zend_auto_global tmp_autoglobal; |
---|
[1] | 588 | |
---|
[268] | 589 | zend_hash_copy(&TG(auto_globals), OG(auto_globals), NULL, (void *) &tmp_autoglobal, sizeof(tmp_autoglobal)); |
---|
| 590 | zend_hash_apply(&TG(auto_globals), (apply_func_t) xc_auto_global_arm TSRMLS_CC); |
---|
| 591 | } |
---|
| 592 | #endif |
---|
| 593 | |
---|
[1] | 594 | sandbox->filename = filename; |
---|
| 595 | |
---|
[209] | 596 | #ifdef E_STRICT |
---|
| 597 | sandbox->orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting); |
---|
| 598 | EG(user_error_handler_error_reporting) &= ~E_STRICT; |
---|
| 599 | #endif |
---|
| 600 | |
---|
[1] | 601 | return sandbox; |
---|
| 602 | } |
---|
| 603 | /* }}} */ |
---|
[212] | 604 | static void xc_early_binding_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
---|
| 605 | { |
---|
| 606 | xc_sandbox_t *sandbox = (xc_sandbox_t *) data; |
---|
| 607 | xc_do_early_binding(CG(active_op_array), OG(class_table), oplineno TSRMLS_CC); |
---|
| 608 | } |
---|
| 609 | /* }}} */ |
---|
[1] | 610 | static void xc_sandbox_install(xc_sandbox_t *sandbox TSRMLS_DC) /* {{{ */ |
---|
| 611 | { |
---|
| 612 | int i; |
---|
| 613 | Bucket *b; |
---|
| 614 | |
---|
[95] | 615 | #ifdef HAVE_XCACHE_CONSTANT |
---|
| 616 | b = TG(zend_constants).pListHead; |
---|
| 617 | /* install constants */ |
---|
| 618 | while (b != NULL) { |
---|
| 619 | zend_constant *c = (zend_constant*) b->pData; |
---|
| 620 | xc_install_constant(sandbox->filename, c, |
---|
[200] | 621 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
---|
[95] | 622 | b = b->pListNext; |
---|
| 623 | } |
---|
| 624 | #endif |
---|
| 625 | |
---|
[344] | 626 | b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; |
---|
[1] | 627 | /* install function */ |
---|
| 628 | while (b != NULL) { |
---|
| 629 | zend_function *func = (zend_function*) b->pData; |
---|
| 630 | xc_install_function(sandbox->filename, func, |
---|
[200] | 631 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
---|
[1] | 632 | b = b->pListNext; |
---|
| 633 | } |
---|
| 634 | |
---|
[344] | 635 | b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; |
---|
[1] | 636 | /* install class */ |
---|
| 637 | while (b != NULL) { |
---|
[212] | 638 | xc_install_class(sandbox->filename, (xc_cest_t*) b->pData, -1, |
---|
[200] | 639 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
---|
[1] | 640 | b = b->pListNext; |
---|
| 641 | } |
---|
[268] | 642 | |
---|
| 643 | #ifdef ZEND_ENGINE_2_1 |
---|
| 644 | /* trigger auto_globals jit */ |
---|
| 645 | for (b = TG(auto_globals).pListHead; b != NULL; b = b->pListNext) { |
---|
| 646 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
| 647 | /* check if actived */ |
---|
| 648 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
| 649 | zend_u_is_auto_global(BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), auto_global->name_len TSRMLS_CC); |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | #endif |
---|
| 653 | |
---|
[212] | 654 | xc_undo_pass_two(CG(active_op_array) TSRMLS_CC); |
---|
| 655 | xc_foreach_early_binding_class(CG(active_op_array), xc_early_binding_cb, (void *) sandbox TSRMLS_CC); |
---|
| 656 | xc_redo_pass_two(CG(active_op_array) TSRMLS_CC); |
---|
[1] | 657 | |
---|
| 658 | i = 1; |
---|
| 659 | zend_hash_add(&OG(included_files), sandbox->filename, strlen(sandbox->filename) + 1, (void *)&i, sizeof(int), NULL); |
---|
| 660 | } |
---|
| 661 | /* }}} */ |
---|
| 662 | void xc_sandbox_free(xc_sandbox_t *sandbox, int install TSRMLS_DC) /* {{{ */ |
---|
| 663 | { |
---|
| 664 | /* restore first first install function/class */ |
---|
[95] | 665 | #ifdef HAVE_XCACHE_CONSTANT |
---|
| 666 | EG(zend_constants) = OG(zend_constants); |
---|
| 667 | #endif |
---|
[1] | 668 | CG(function_table) = OG(function_table); |
---|
| 669 | CG(class_table) = OG(class_table); |
---|
| 670 | EG(class_table) = CG(class_table); |
---|
[268] | 671 | #ifdef ZEND_ENGINE_2_1 |
---|
| 672 | CG(auto_globals) = OG(auto_globals); |
---|
| 673 | #endif |
---|
[1] | 674 | |
---|
| 675 | if (install) { |
---|
[235] | 676 | CG(in_compilation) = 1; |
---|
| 677 | CG(compiled_filename) = sandbox->filename; |
---|
| 678 | CG(zend_lineno) = 0; |
---|
[1] | 679 | xc_sandbox_install(sandbox TSRMLS_CC); |
---|
[235] | 680 | CG(in_compilation) = 0; |
---|
| 681 | CG(compiled_filename) = NULL; |
---|
[1] | 682 | |
---|
| 683 | /* no free as it's installed */ |
---|
[95] | 684 | #ifdef HAVE_XCACHE_CONSTANT |
---|
| 685 | TG(zend_constants).pDestructor = NULL; |
---|
| 686 | #endif |
---|
[1] | 687 | TG(function_table).pDestructor = NULL; |
---|
| 688 | TG(class_table).pDestructor = NULL; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | /* destroy all the tmp */ |
---|
[95] | 692 | #ifdef HAVE_XCACHE_CONSTANT |
---|
| 693 | zend_hash_destroy(&TG(zend_constants)); |
---|
| 694 | #endif |
---|
[1] | 695 | zend_hash_destroy(&TG(function_table)); |
---|
| 696 | zend_hash_destroy(&TG(class_table)); |
---|
[268] | 697 | #ifdef ZEND_ENGINE_2_1 |
---|
| 698 | zend_hash_destroy(&TG(auto_globals)); |
---|
| 699 | #endif |
---|
[1] | 700 | zend_hash_destroy(TG(included_files)); |
---|
| 701 | |
---|
| 702 | /* restore orig here, as EG/CG holded tmp before */ |
---|
| 703 | memcpy(&EG(included_files), &OG(included_files), sizeof(EG(included_files))); |
---|
| 704 | |
---|
[209] | 705 | #ifdef E_STRICT |
---|
| 706 | EG(user_error_handler_error_reporting) = sandbox->orig_user_error_handler_error_reporting; |
---|
| 707 | #endif |
---|
| 708 | |
---|
[1] | 709 | if (sandbox->alloc) { |
---|
| 710 | efree(sandbox); |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | /* }}} */ |
---|
[305] | 714 | int xc_vtrace(const char *fmt, va_list args) /* {{{ */ |
---|
| 715 | { |
---|
| 716 | vfprintf(stderr, fmt, args); |
---|
| 717 | return 0; |
---|
| 718 | } |
---|
| 719 | /* }}} */ |
---|
| 720 | int xc_trace(const char *fmt, ...) /* {{{ */ |
---|
| 721 | { |
---|
| 722 | va_list args; |
---|
| 723 | |
---|
| 724 | va_start(args, fmt); |
---|
| 725 | xc_vtrace(fmt, args); |
---|
| 726 | va_end(args); |
---|
| 727 | return 0; |
---|
| 728 | } |
---|
| 729 | /* }}} */ |
---|