| 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 | #define OP_ZVAL_DTOR(op) do { \ |
|---|
| 22 | (op).u.constant.is_ref = 0; \ |
|---|
| 23 | zval_dtor(&(op).u.constant); \ |
|---|
| 24 | } while(0) |
|---|
| 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 | { |
|---|
| 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); |
|---|
| 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 | |
|---|
| 214 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| 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; |
|---|
| 250 | zend_uint i; |
|---|
| 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 | /* }}} */ |
|---|
| 279 | #endif |
|---|
| 280 | |
|---|
| 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 | |
|---|
| 308 | #ifdef ZEND_ENGINE_2 |
|---|
| 309 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 310 | callback(opline, opline - begin, data TSRMLS_CC); |
|---|
| 311 | break; |
|---|
| 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 |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | if (opline < next) { |
|---|
| 322 | opline = next; |
|---|
| 323 | } |
|---|
| 324 | else { |
|---|
| 325 | opline ++; |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | return SUCCESS; |
|---|
| 329 | } |
|---|
| 330 | /* }}} */ |
|---|
| 331 | static int xc_do_early_binding(zend_op_array *op_array, HashTable *class_table, int oplineno TSRMLS_DC) /* {{{ */ |
|---|
| 332 | { |
|---|
| 333 | zend_op *opline; |
|---|
| 334 | |
|---|
| 335 | TRACE("binding %d", oplineno); |
|---|
| 336 | assert(oplineno >= 0); |
|---|
| 337 | |
|---|
| 338 | /* do early binding */ |
|---|
| 339 | opline = &(op_array->opcodes[oplineno]); |
|---|
| 340 | |
|---|
| 341 | switch (opline->opcode) { |
|---|
| 342 | #ifdef ZEND_ENGINE_2 |
|---|
| 343 | case ZEND_DECLARE_INHERITED_CLASS: |
|---|
| 344 | { |
|---|
| 345 | zval *parent_name; |
|---|
| 346 | zend_class_entry **pce; |
|---|
| 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 | |
|---|
| 353 | parent_name = &(opline - 1)->op2.u.constant; |
|---|
| 354 | TRACE("binding with parent %s", Z_STRVAL_P(parent_name)); |
|---|
| 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 | |
|---|
| 369 | TRACE("%s %p", Z_STRVAL(fetch_class_opline->op2.u.constant), Z_STRVAL(fetch_class_opline->op2.u.constant)); |
|---|
| 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 | } |
|---|
| 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 |
|---|
| 392 | case ZEND_DECLARE_FUNCTION_OR_CLASS: |
|---|
| 393 | if (do_bind_function_or_class(opline, NULL, class_table, 1) == FAILURE) { |
|---|
| 394 | return FAILURE; |
|---|
| 395 | } |
|---|
| 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 | |
|---|
| 416 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 417 | void xc_install_constant(char *filename, zend_constant *constant, zend_uchar type, zstr key, uint len TSRMLS_DC) /* {{{ */ |
|---|
| 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; |
|---|
| 424 | #ifdef IS_UNICODE |
|---|
| 425 | zend_error(E_NOTICE, "Constant %R already defined", type, key); |
|---|
| 426 | #else |
|---|
| 427 | zend_error(E_NOTICE, "Constant %s already defined", key); |
|---|
| 428 | #endif |
|---|
| 429 | free(ZSTR_V(constant->name)); |
|---|
| 430 | if (!(constant->flags & CONST_PERSISTENT)) { |
|---|
| 431 | zval_dtor(&constant->value); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | /* }}} */ |
|---|
| 436 | #endif |
|---|
| 437 | void xc_install_function(char *filename, zend_function *func, zend_uchar type, zstr key, uint len TSRMLS_DC) /* {{{ */ |
|---|
| 438 | { |
|---|
| 439 | zend_bool istmpkey; |
|---|
| 440 | |
|---|
| 441 | if (func->type == ZEND_USER_FUNCTION) { |
|---|
| 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) { |
|---|
| 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, |
|---|
| 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); |
|---|
| 458 | #ifdef IS_UNICODE |
|---|
| 459 | zend_error(E_ERROR, "Cannot redeclare %R()", type, key); |
|---|
| 460 | #else |
|---|
| 461 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
|---|
| 462 | #endif |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | /* }}} */ |
|---|
| 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) /* {{{ */ |
|---|
| 468 | { |
|---|
| 469 | zend_bool istmpkey; |
|---|
| 470 | zend_class_entry *cep = CestToCePtr(*cest); |
|---|
| 471 | ZESW(void *stored_ce_ptr, NOTHING); |
|---|
| 472 | |
|---|
| 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) { |
|---|
| 479 | zend_u_hash_update(CG(class_table), type, key, len, |
|---|
| 480 | cest, sizeof(xc_cest_t), |
|---|
| 481 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 482 | ); |
|---|
| 483 | if (oplineno != -1) { |
|---|
| 484 | xc_do_early_binding(CG(active_op_array), CG(class_table), oplineno TSRMLS_CC); |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | else if (zend_u_hash_add(CG(class_table), type, key, len, |
|---|
| 488 | cest, sizeof(xc_cest_t), |
|---|
| 489 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 490 | ) == FAILURE) { |
|---|
| 491 | CG(zend_lineno) = ZESW(0, cep->line_start); |
|---|
| 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 |
|---|
| 497 | assert(oplineno == -1); |
|---|
| 498 | } |
|---|
| 499 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
|---|
| 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 | /* }}} */ |
|---|
| 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 |
|---|
| 532 | |
|---|
| 533 | void xc_zend_class_add_ref(zend_class_entry ZESW(*ce, **ce)) |
|---|
| 534 | { |
|---|
| 535 | #ifdef ZEND_ENGINE_2 |
|---|
| 536 | (*ce)->refcount++; |
|---|
| 537 | #else |
|---|
| 538 | (*ce->refcount)++; |
|---|
| 539 | #endif |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 543 | { |
|---|
| 544 | HashTable *h; |
|---|
| 545 | |
|---|
| 546 | if (sandbox) { |
|---|
| 547 | memset(sandbox, 0, sizeof(sandbox[0])); |
|---|
| 548 | } |
|---|
| 549 | else { |
|---|
| 550 | ECALLOC_ONE(sandbox); |
|---|
| 551 | sandbox->alloc = 1; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | memcpy(&OG(included_files), &EG(included_files), sizeof(EG(included_files))); |
|---|
| 555 | |
|---|
| 556 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 557 | OG(zend_constants) = EG(zend_constants); |
|---|
| 558 | EG(zend_constants) = &TG(zend_constants); |
|---|
| 559 | #endif |
|---|
| 560 | |
|---|
| 561 | OG(function_table) = CG(function_table); |
|---|
| 562 | CG(function_table) = &TG(function_table); |
|---|
| 563 | |
|---|
| 564 | OG(class_table) = CG(class_table); |
|---|
| 565 | CG(class_table) = &TG(class_table); |
|---|
| 566 | EG(class_table) = CG(class_table); |
|---|
| 567 | |
|---|
| 568 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 569 | OG(auto_globals) = CG(auto_globals); |
|---|
| 570 | CG(auto_globals) = &TG(auto_globals); |
|---|
| 571 | #endif |
|---|
| 572 | |
|---|
| 573 | TG(included_files) = &EG(included_files); |
|---|
| 574 | |
|---|
| 575 | zend_hash_init_ex(TG(included_files), 5, NULL, NULL, 0, 1); |
|---|
| 576 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 577 | h = OG(zend_constants); |
|---|
| 578 | zend_hash_init_ex(&TG(zend_constants), 20, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
|---|
| 579 | #endif |
|---|
| 580 | h = OG(function_table); |
|---|
| 581 | zend_hash_init_ex(&TG(function_table), 128, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
|---|
| 582 | { |
|---|
| 583 | zend_function tmp_func; |
|---|
| 584 | zend_hash_copy(&TG(function_table), &XG(internal_function_table), (copy_ctor_func_t) function_add_ref, (void *) &tmp_func, sizeof(tmp_func)); |
|---|
| 585 | } |
|---|
| 586 | TG(internal_function_tail) = TG(function_table).pListTail; |
|---|
| 587 | |
|---|
| 588 | h = OG(class_table); |
|---|
| 589 | zend_hash_init_ex(&TG(class_table), 16, NULL, h->pDestructor, h->persistent, h->bApplyProtection); |
|---|
| 590 | #if 0 && TODO |
|---|
| 591 | { |
|---|
| 592 | xc_cest_t tmp_cest; |
|---|
| 593 | zend_hash_copy(&TG(class_table), &XG(internal_class_table), (copy_ctor_func_t) xc_zend_class_add_ref, (void *) &tmp_cest, sizeof(tmp_cest)); |
|---|
| 594 | } |
|---|
| 595 | #endif |
|---|
| 596 | TG(internal_class_tail) = TG(class_table).pListTail; |
|---|
| 597 | |
|---|
| 598 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 599 | /* shallow copy, don't destruct */ |
|---|
| 600 | h = OG(auto_globals); |
|---|
| 601 | zend_hash_init_ex(&TG(auto_globals), 8, NULL, NULL, h->persistent, h->bApplyProtection); |
|---|
| 602 | { |
|---|
| 603 | zend_auto_global tmp_autoglobal; |
|---|
| 604 | |
|---|
| 605 | zend_hash_copy(&TG(auto_globals), OG(auto_globals), NULL, (void *) &tmp_autoglobal, sizeof(tmp_autoglobal)); |
|---|
| 606 | zend_hash_apply(&TG(auto_globals), (apply_func_t) xc_auto_global_arm TSRMLS_CC); |
|---|
| 607 | } |
|---|
| 608 | #endif |
|---|
| 609 | |
|---|
| 610 | sandbox->filename = filename; |
|---|
| 611 | |
|---|
| 612 | #ifdef E_STRICT |
|---|
| 613 | sandbox->orig_user_error_handler_error_reporting = EG(user_error_handler_error_reporting); |
|---|
| 614 | EG(user_error_handler_error_reporting) &= ~E_STRICT; |
|---|
| 615 | #endif |
|---|
| 616 | |
|---|
| 617 | return sandbox; |
|---|
| 618 | } |
|---|
| 619 | /* }}} */ |
|---|
| 620 | static void xc_early_binding_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
|---|
| 621 | { |
|---|
| 622 | xc_sandbox_t *sandbox = (xc_sandbox_t *) data; |
|---|
| 623 | xc_do_early_binding(CG(active_op_array), OG(class_table), oplineno TSRMLS_CC); |
|---|
| 624 | } |
|---|
| 625 | /* }}} */ |
|---|
| 626 | static void xc_sandbox_install(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
|---|
| 627 | { |
|---|
| 628 | int i; |
|---|
| 629 | Bucket *b; |
|---|
| 630 | |
|---|
| 631 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 632 | b = TG(zend_constants).pListHead; |
|---|
| 633 | /* install constants */ |
|---|
| 634 | while (b != NULL) { |
|---|
| 635 | zend_constant *c = (zend_constant*) b->pData; |
|---|
| 636 | xc_install_constant(sandbox->filename, c, |
|---|
| 637 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
|---|
| 638 | b = b->pListNext; |
|---|
| 639 | } |
|---|
| 640 | #endif |
|---|
| 641 | |
|---|
| 642 | b = TG(internal_function_tail) ? TG(internal_function_tail)->pListNext : TG(function_table).pListHead; |
|---|
| 643 | /* install function */ |
|---|
| 644 | while (b != NULL) { |
|---|
| 645 | zend_function *func = (zend_function*) b->pData; |
|---|
| 646 | xc_install_function(sandbox->filename, func, |
|---|
| 647 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
|---|
| 648 | b = b->pListNext; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | b = TG(internal_class_tail) ? TG(internal_class_tail)->pListNext : TG(class_table).pListHead; |
|---|
| 652 | /* install class */ |
|---|
| 653 | while (b != NULL) { |
|---|
| 654 | xc_install_class(sandbox->filename, (xc_cest_t*) b->pData, -1, |
|---|
| 655 | BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), b->nKeyLength TSRMLS_CC); |
|---|
| 656 | b = b->pListNext; |
|---|
| 657 | } |
|---|
| 658 | |
|---|
| 659 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 660 | /* trigger auto_globals jit */ |
|---|
| 661 | for (b = TG(auto_globals).pListHead; b != NULL; b = b->pListNext) { |
|---|
| 662 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
|---|
| 663 | /* check if actived */ |
|---|
| 664 | if (auto_global->auto_global_callback && !auto_global->armed) { |
|---|
| 665 | zend_u_is_auto_global(BUCKET_KEY_TYPE(b), ZSTR(BUCKET_KEY_S(b)), auto_global->name_len TSRMLS_CC); |
|---|
| 666 | } |
|---|
| 667 | } |
|---|
| 668 | #endif |
|---|
| 669 | |
|---|
| 670 | if (install != XC_InstallNoBinding) { |
|---|
| 671 | xc_undo_pass_two(CG(active_op_array) TSRMLS_CC); |
|---|
| 672 | xc_foreach_early_binding_class(CG(active_op_array), xc_early_binding_cb, (void *) sandbox TSRMLS_CC); |
|---|
| 673 | xc_redo_pass_two(CG(active_op_array) TSRMLS_CC); |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | i = 1; |
|---|
| 677 | zend_hash_add(&OG(included_files), sandbox->filename, strlen(sandbox->filename) + 1, (void *)&i, sizeof(int), NULL); |
|---|
| 678 | } |
|---|
| 679 | /* }}} */ |
|---|
| 680 | void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC) /* {{{ */ |
|---|
| 681 | { |
|---|
| 682 | /* restore first first install function/class */ |
|---|
| 683 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 684 | EG(zend_constants) = OG(zend_constants); |
|---|
| 685 | #endif |
|---|
| 686 | CG(function_table) = OG(function_table); |
|---|
| 687 | CG(class_table) = OG(class_table); |
|---|
| 688 | EG(class_table) = CG(class_table); |
|---|
| 689 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 690 | CG(auto_globals) = OG(auto_globals); |
|---|
| 691 | #endif |
|---|
| 692 | |
|---|
| 693 | if (install != XC_NoInstall) { |
|---|
| 694 | CG(in_compilation) = 1; |
|---|
| 695 | CG(compiled_filename) = sandbox->filename; |
|---|
| 696 | CG(zend_lineno) = 0; |
|---|
| 697 | xc_sandbox_install(sandbox, install TSRMLS_CC); |
|---|
| 698 | CG(in_compilation) = 0; |
|---|
| 699 | CG(compiled_filename) = NULL; |
|---|
| 700 | |
|---|
| 701 | /* no free as it's installed */ |
|---|
| 702 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 703 | TG(zend_constants).pDestructor = NULL; |
|---|
| 704 | #endif |
|---|
| 705 | TG(function_table).pDestructor = NULL; |
|---|
| 706 | TG(class_table).pDestructor = NULL; |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | /* destroy all the tmp */ |
|---|
| 710 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 711 | zend_hash_destroy(&TG(zend_constants)); |
|---|
| 712 | #endif |
|---|
| 713 | zend_hash_destroy(&TG(function_table)); |
|---|
| 714 | zend_hash_destroy(&TG(class_table)); |
|---|
| 715 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 716 | zend_hash_destroy(&TG(auto_globals)); |
|---|
| 717 | #endif |
|---|
| 718 | zend_hash_destroy(TG(included_files)); |
|---|
| 719 | |
|---|
| 720 | /* restore orig here, as EG/CG holded tmp before */ |
|---|
| 721 | memcpy(&EG(included_files), &OG(included_files), sizeof(EG(included_files))); |
|---|
| 722 | |
|---|
| 723 | #ifdef E_STRICT |
|---|
| 724 | EG(user_error_handler_error_reporting) = sandbox->orig_user_error_handler_error_reporting; |
|---|
| 725 | #endif |
|---|
| 726 | |
|---|
| 727 | if (sandbox->alloc) { |
|---|
| 728 | efree(sandbox); |
|---|
| 729 | } |
|---|
| 730 | } |
|---|
| 731 | /* }}} */ |
|---|
| 732 | int xc_vtrace(const char *fmt, va_list args) /* {{{ */ |
|---|
| 733 | { |
|---|
| 734 | return vfprintf(stderr, fmt, args); |
|---|
| 735 | } |
|---|
| 736 | /* }}} */ |
|---|
| 737 | int xc_trace(const char *fmt, ...) /* {{{ */ |
|---|
| 738 | { |
|---|
| 739 | va_list args; |
|---|
| 740 | int ret; |
|---|
| 741 | |
|---|
| 742 | va_start(args, fmt); |
|---|
| 743 | ret = xc_vtrace(fmt, args); |
|---|
| 744 | va_end(args); |
|---|
| 745 | return ret; |
|---|
| 746 | } |
|---|
| 747 | /* }}} */ |
|---|