| 1 | #include "php.h" |
|---|
| 2 | #include "xcache.h" |
|---|
| 3 | #include "utils.h" |
|---|
| 4 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 5 | #include "zend_vm.h" |
|---|
| 6 | #endif |
|---|
| 7 | #include "opcode_spec.h" |
|---|
| 8 | #undef NDEBUG |
|---|
| 9 | #include "assert.h" |
|---|
| 10 | |
|---|
| 11 | xc_compile_result_t *xc_compile_result_init(xc_compile_result_t *cr, /* {{{ */ |
|---|
| 12 | zend_op_array *op_array, |
|---|
| 13 | HashTable *function_table, |
|---|
| 14 | HashTable *class_table) |
|---|
| 15 | { |
|---|
| 16 | if (cr) { |
|---|
| 17 | cr->alloc = 0; |
|---|
| 18 | } |
|---|
| 19 | else { |
|---|
| 20 | cr = emalloc(sizeof(xc_compile_result_t)); |
|---|
| 21 | cr->alloc = 1; |
|---|
| 22 | } |
|---|
| 23 | cr->op_array = op_array; |
|---|
| 24 | cr->function_table = function_table; |
|---|
| 25 | cr->class_table = class_table; |
|---|
| 26 | return cr; |
|---|
| 27 | } |
|---|
| 28 | /* }}} */ |
|---|
| 29 | xc_compile_result_t *xc_compile_result_init_cur(xc_compile_result_t *cr, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 30 | { |
|---|
| 31 | return xc_compile_result_init(cr, op_array, CG(function_table), CG(class_table)); |
|---|
| 32 | } |
|---|
| 33 | /* }}} */ |
|---|
| 34 | void xc_compile_result_free(xc_compile_result_t *cr) /* {{{ */ |
|---|
| 35 | { |
|---|
| 36 | if (cr->alloc) { |
|---|
| 37 | efree(cr); |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | /* }}} */ |
|---|
| 41 | |
|---|
| 42 | int xc_apply_function(zend_function *zf, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 43 | { |
|---|
| 44 | switch (zf->type) { |
|---|
| 45 | case ZEND_USER_FUNCTION: |
|---|
| 46 | case ZEND_EVAL_CODE: |
|---|
| 47 | return applyer(&zf->op_array TSRMLS_CC); |
|---|
| 48 | break; |
|---|
| 49 | |
|---|
| 50 | case ZEND_INTERNAL_FUNCTION: |
|---|
| 51 | case ZEND_OVERLOADED_FUNCTION: |
|---|
| 52 | break; |
|---|
| 53 | |
|---|
| 54 | EMPTY_SWITCH_DEFAULT_CASE(); |
|---|
| 55 | } |
|---|
| 56 | return 0; |
|---|
| 57 | } |
|---|
| 58 | /* }}} */ |
|---|
| 59 | typedef struct { |
|---|
| 60 | apply_func_t applyer; |
|---|
| 61 | zend_class_entry *ce; |
|---|
| 62 | } xc_apply_method_info; |
|---|
| 63 | int xc_apply_method(zend_function *zf, xc_apply_method_info *mi TSRMLS_DC) /* {{{ */ |
|---|
| 64 | { |
|---|
| 65 | /* avoid duplicate apply for shadowed method */ |
|---|
| 66 | #ifdef ZEND_ENGINE_2 |
|---|
| 67 | if (mi->ce != zf->common.scope) { |
|---|
| 68 | /* fprintf(stderr, "avoided duplicate %s\n", zf->common.function_name); */ |
|---|
| 69 | return 0; |
|---|
| 70 | } |
|---|
| 71 | #else |
|---|
| 72 | char *name = zf->common.function_name; |
|---|
| 73 | int name_s = strlen(name) + 1; |
|---|
| 74 | zend_class_entry *ce; |
|---|
| 75 | zend_function *ptr; |
|---|
| 76 | |
|---|
| 77 | for (ce = mi->ce->parent; ce; ce = ce->parent) { |
|---|
| 78 | if (zend_hash_find(&ce->function_table, name, name_s, (void **) &ptr) == SUCCESS) { |
|---|
| 79 | if (ptr->op_array.refcount == zf->op_array.refcount) { |
|---|
| 80 | return 0; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | #endif |
|---|
| 85 | return xc_apply_function(zf, mi->applyer TSRMLS_CC); |
|---|
| 86 | } |
|---|
| 87 | /* }}} */ |
|---|
| 88 | #if 0 |
|---|
| 89 | int xc_apply_class(zend_class_entry *ce, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 90 | { |
|---|
| 91 | xc_apply_method_info mi; |
|---|
| 92 | |
|---|
| 93 | mi.applyer = applyer; |
|---|
| 94 | mi.ce = ce; |
|---|
| 95 | zend_hash_apply_with_argument(&(ce->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | /* }}} */ |
|---|
| 99 | #endif |
|---|
| 100 | static int xc_apply_cest(xc_cest_t *cest, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 101 | { |
|---|
| 102 | xc_apply_method_info mi; |
|---|
| 103 | |
|---|
| 104 | mi.applyer = applyer; |
|---|
| 105 | mi.ce = CestToCePtr(*cest); |
|---|
| 106 | zend_hash_apply_with_argument(&(CestToCePtr(*cest)->function_table), (apply_func_arg_t) xc_apply_method, &mi TSRMLS_CC); |
|---|
| 107 | return 0; |
|---|
| 108 | } |
|---|
| 109 | /* }}} */ |
|---|
| 110 | int xc_apply_op_array(xc_compile_result_t *cr, apply_func_t applyer TSRMLS_DC) /* {{{ */ |
|---|
| 111 | { |
|---|
| 112 | zend_hash_apply_with_argument(cr->function_table, (apply_func_arg_t) xc_apply_function, applyer TSRMLS_CC); |
|---|
| 113 | zend_hash_apply_with_argument(cr->class_table, (apply_func_arg_t) xc_apply_cest, applyer TSRMLS_CC); |
|---|
| 114 | |
|---|
| 115 | return applyer(cr->op_array TSRMLS_CC); |
|---|
| 116 | } |
|---|
| 117 | /* }}} */ |
|---|
| 118 | |
|---|
| 119 | int xc_undo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 120 | { |
|---|
| 121 | zend_op *opline, *end; |
|---|
| 122 | |
|---|
| 123 | if (!op_array->done_pass_two) { |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | opline = op_array->opcodes; |
|---|
| 128 | end = opline + op_array->last; |
|---|
| 129 | while (opline < end) { |
|---|
| 130 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 131 | switch (opline->opcode) { |
|---|
| 132 | case ZEND_JMP: |
|---|
| 133 | opline->op1.u.opline_num = opline->op1.u.jmp_addr - op_array->opcodes; |
|---|
| 134 | assert(opline->op1.u.opline_num < op_array->last); |
|---|
| 135 | break; |
|---|
| 136 | case ZEND_JMPZ: |
|---|
| 137 | case ZEND_JMPNZ: |
|---|
| 138 | case ZEND_JMPZ_EX: |
|---|
| 139 | case ZEND_JMPNZ_EX: |
|---|
| 140 | opline->op2.u.opline_num = opline->op2.u.jmp_addr - op_array->opcodes; |
|---|
| 141 | assert(opline->op2.u.opline_num < op_array->last); |
|---|
| 142 | break; |
|---|
| 143 | } |
|---|
| 144 | #endif |
|---|
| 145 | opline++; |
|---|
| 146 | } |
|---|
| 147 | op_array->done_pass_two = 0; |
|---|
| 148 | |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|
| 151 | /* }}} */ |
|---|
| 152 | int xc_redo_pass_two(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 153 | { |
|---|
| 154 | zend_op *opline, *end; |
|---|
| 155 | |
|---|
| 156 | if (op_array->done_pass_two) { |
|---|
| 157 | return 0; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /* |
|---|
| 161 | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last); |
|---|
| 162 | op_array->size = op_array->last; |
|---|
| 163 | */ |
|---|
| 164 | |
|---|
| 165 | opline = op_array->opcodes; |
|---|
| 166 | end = opline + op_array->last; |
|---|
| 167 | while (opline < end) { |
|---|
| 168 | if (opline->op1.op_type == IS_CONST) { |
|---|
| 169 | opline->op1.u.constant.is_ref = 1; |
|---|
| 170 | opline->op1.u.constant.refcount = 2; /* Make sure is_ref won't be reset */ |
|---|
| 171 | } |
|---|
| 172 | if (opline->op2.op_type == IS_CONST) { |
|---|
| 173 | opline->op2.u.constant.is_ref = 1; |
|---|
| 174 | opline->op2.u.constant.refcount = 2; |
|---|
| 175 | } |
|---|
| 176 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 177 | switch (opline->opcode) { |
|---|
| 178 | case ZEND_JMP: |
|---|
| 179 | assert(opline->op1.u.opline_num < op_array->last); |
|---|
| 180 | opline->op1.u.jmp_addr = op_array->opcodes + opline->op1.u.opline_num; |
|---|
| 181 | break; |
|---|
| 182 | case ZEND_JMPZ: |
|---|
| 183 | case ZEND_JMPNZ: |
|---|
| 184 | case ZEND_JMPZ_EX: |
|---|
| 185 | case ZEND_JMPNZ_EX: |
|---|
| 186 | assert(opline->op2.u.opline_num < op_array->last); |
|---|
| 187 | opline->op2.u.jmp_addr = op_array->opcodes + opline->op2.u.opline_num; |
|---|
| 188 | break; |
|---|
| 189 | } |
|---|
| 190 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
|---|
| 191 | #endif |
|---|
| 192 | opline++; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | op_array->done_pass_two = 1; |
|---|
| 196 | return 0; |
|---|
| 197 | } |
|---|
| 198 | /* }}} */ |
|---|
| 199 | |
|---|
| 200 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| 201 | static void xc_fix_opcode_ex_znode(int tofix, xc_op_spec_t spec, znode *znode, int type TSRMLS_DC) /* {{{ */ |
|---|
| 202 | { |
|---|
| 203 | #ifdef ZEND_ENGINE_2 |
|---|
| 204 | if ((znode->op_type != IS_UNUSED && (spec == OPSPEC_UCLASS || spec == OPSPEC_CLASS)) || |
|---|
| 205 | spec == OPSPEC_FETCH) { |
|---|
| 206 | if (tofix) { |
|---|
| 207 | switch (znode->op_type) { |
|---|
| 208 | case IS_VAR: |
|---|
| 209 | case IS_TMP_VAR: |
|---|
| 210 | break; |
|---|
| 211 | |
|---|
| 212 | default: |
|---|
| 213 | /* TODO: data lost, find a way to keep it */ |
|---|
| 214 | /* assert(znode->op_type == IS_CONST); */ |
|---|
| 215 | znode->op_type = IS_TMP_VAR; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | switch (znode->op_type) { |
|---|
| 220 | case IS_TMP_VAR: |
|---|
| 221 | case IS_VAR: |
|---|
| 222 | if (tofix) { |
|---|
| 223 | znode->u.var /= sizeof(temp_variable); |
|---|
| 224 | } |
|---|
| 225 | else { |
|---|
| 226 | znode->u.var *= sizeof(temp_variable); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | #endif |
|---|
| 230 | } |
|---|
| 231 | /* }}} */ |
|---|
| 232 | |
|---|
| 233 | static void xc_fix_opcode_ex(zend_op_array *op_array, int tofix TSRMLS_DC) /* {{{ */ |
|---|
| 234 | { |
|---|
| 235 | zend_op *opline; |
|---|
| 236 | zend_uint i; |
|---|
| 237 | |
|---|
| 238 | opline = op_array->opcodes; |
|---|
| 239 | for (i = 0; i < op_array->last; i ++, opline ++) { |
|---|
| 240 | /* 3rd optimizer may have ... */ |
|---|
| 241 | if (opline->opcode < xc_get_opcode_spec_count()) { |
|---|
| 242 | const xc_opcode_spec_t *spec; |
|---|
| 243 | spec = xc_get_opcode_spec(opline->opcode); |
|---|
| 244 | |
|---|
| 245 | xc_fix_opcode_ex_znode(tofix, spec->op1, &opline->op1, 0 TSRMLS_CC); |
|---|
| 246 | xc_fix_opcode_ex_znode(tofix, spec->op2, &opline->op2, 1 TSRMLS_CC); |
|---|
| 247 | xc_fix_opcode_ex_znode(tofix, spec->res, &opline->result, 2 TSRMLS_CC); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | /* }}} */ |
|---|
| 252 | int xc_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 253 | { |
|---|
| 254 | xc_fix_opcode_ex(op_array, 1 TSRMLS_CC); |
|---|
| 255 | return 0; |
|---|
| 256 | } |
|---|
| 257 | /* }}} */ |
|---|
| 258 | int xc_undo_fix_opcode(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 259 | { |
|---|
| 260 | xc_fix_opcode_ex(op_array, 0 TSRMLS_CC); |
|---|
| 261 | |
|---|
| 262 | return 0; |
|---|
| 263 | } |
|---|
| 264 | /* }}} */ |
|---|
| 265 | #endif |
|---|
| 266 | |
|---|
| 267 | void xc_install_function(char *filename, zend_function *func, zend_uchar type, char *key, uint len TSRMLS_DC) /* {{{ */ |
|---|
| 268 | { |
|---|
| 269 | if (func->type == ZEND_USER_FUNCTION) { |
|---|
| 270 | if (zend_u_hash_add(CG(function_table), type, key, len, |
|---|
| 271 | func, sizeof(zend_op_array), |
|---|
| 272 | NULL |
|---|
| 273 | ) == FAILURE) { |
|---|
| 274 | CG(in_compilation) = 1; |
|---|
| 275 | CG(compiled_filename) = filename; |
|---|
| 276 | CG(zend_lineno) = ZESW(func->op_array.opcodes[0].lineno, func->op_array.line_start); |
|---|
| 277 | zend_error(E_ERROR, "Cannot redeclare %s()", key); |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | /* }}} */ |
|---|
| 282 | ZESW(xc_cest_t *, void) xc_install_class(char *filename, xc_cest_t *cest, zend_uchar type, void *key, uint len TSRMLS_DC) /* {{{ */ |
|---|
| 283 | { |
|---|
| 284 | zend_class_entry *cep = CestToCePtr(*cest); |
|---|
| 285 | ZESW(void *stored_ce_ptr, NOTHING); |
|---|
| 286 | |
|---|
| 287 | if (zend_u_hash_add(CG(class_table), type, key, len, |
|---|
| 288 | cest, sizeof(xc_cest_t), |
|---|
| 289 | ZESW(&stored_ce_ptr, NULL) |
|---|
| 290 | ) == FAILURE) { |
|---|
| 291 | CG(in_compilation) = 1; |
|---|
| 292 | CG(compiled_filename) = filename; |
|---|
| 293 | CG(zend_lineno) = ZESW(0, cep->line_start); |
|---|
| 294 | zend_error(E_ERROR, "Cannot redeclare class %s", (char *) cep->name); |
|---|
| 295 | } |
|---|
| 296 | ZESW(return (xc_cest_t *) stored_ce_ptr, NOTHING); |
|---|
| 297 | } |
|---|
| 298 | /* }}} */ |
|---|
| 299 | |
|---|
| 300 | /* sandbox {{{ */ |
|---|
| 301 | #undef TG |
|---|
| 302 | #undef OG |
|---|
| 303 | #define TG(x) (sandbox->tmp_##x) |
|---|
| 304 | #define OG(x) (sandbox->orig_##x) |
|---|
| 305 | /* }}} */ |
|---|
| 306 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 307 | { |
|---|
| 308 | if (sandbox) { |
|---|
| 309 | memset(sandbox, 0, sizeof(sandbox[0])); |
|---|
| 310 | } |
|---|
| 311 | else { |
|---|
| 312 | ECALLOC_ONE(sandbox); |
|---|
| 313 | sandbox->alloc = 1; |
|---|
| 314 | } |
|---|
| 315 | memcpy(&OG(included_files), &EG(included_files), sizeof(EG(included_files))); |
|---|
| 316 | memcpy(&OG(open_files), &CG(open_files), sizeof(CG(open_files))); |
|---|
| 317 | |
|---|
| 318 | OG(function_table) = CG(function_table); |
|---|
| 319 | CG(function_table) = &TG(function_table); |
|---|
| 320 | |
|---|
| 321 | assert(EG(class_table) == CG(class_table)); |
|---|
| 322 | |
|---|
| 323 | OG(class_table) = CG(class_table); |
|---|
| 324 | CG(class_table) = &TG(class_table); |
|---|
| 325 | EG(class_table) = CG(class_table); |
|---|
| 326 | |
|---|
| 327 | TG(included_files) = &EG(included_files); |
|---|
| 328 | TG(open_files) = &CG(open_files); |
|---|
| 329 | |
|---|
| 330 | zend_llist_init(TG(open_files), sizeof(zend_file_handle), (void (*)(void *)) zend_file_handle_dtor, 0); |
|---|
| 331 | zend_hash_init_ex(TG(included_files), 5, NULL, NULL, 0, 1); |
|---|
| 332 | zend_hash_init_ex(&TG(function_table), 128, NULL, ZEND_FUNCTION_DTOR, 0, 0); |
|---|
| 333 | zend_hash_init_ex(&TG(class_table), 16, NULL, ZEND_CLASS_DTOR, 0, 0); |
|---|
| 334 | |
|---|
| 335 | sandbox->filename = filename; |
|---|
| 336 | |
|---|
| 337 | return sandbox; |
|---|
| 338 | } |
|---|
| 339 | /* }}} */ |
|---|
| 340 | static void xc_sandbox_install(xc_sandbox_t *sandbox TSRMLS_DC) /* {{{ */ |
|---|
| 341 | { |
|---|
| 342 | int i; |
|---|
| 343 | Bucket *b; |
|---|
| 344 | zend_llist_position lpos; |
|---|
| 345 | zend_file_handle *handle; |
|---|
| 346 | |
|---|
| 347 | b = TG(function_table).pListHead; |
|---|
| 348 | /* install function */ |
|---|
| 349 | while (b != NULL) { |
|---|
| 350 | zend_function *func = (zend_function*) b->pData; |
|---|
| 351 | xc_install_function(sandbox->filename, func, |
|---|
| 352 | BUCKET_KEY_TYPE(b), BUCKET_KEY(b), b->nKeyLength TSRMLS_CC); |
|---|
| 353 | b = b->pListNext; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | b = TG(class_table).pListHead; |
|---|
| 357 | /* install class */ |
|---|
| 358 | while (b != NULL) { |
|---|
| 359 | xc_install_class(sandbox->filename, (xc_cest_t*)b->pData, |
|---|
| 360 | BUCKET_KEY_TYPE(b), BUCKET_KEY(b), b->nKeyLength TSRMLS_CC); |
|---|
| 361 | b = b->pListNext; |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | i = 1; |
|---|
| 365 | zend_hash_add(&OG(included_files), sandbox->filename, strlen(sandbox->filename) + 1, (void *)&i, sizeof(int), NULL); |
|---|
| 366 | for (handle = zend_llist_get_first_ex(TG(open_files), &lpos); |
|---|
| 367 | handle; |
|---|
| 368 | handle = zend_llist_get_next_ex(TG(open_files), &lpos)) { |
|---|
| 369 | zend_llist_add_element(&OG(open_files), handle); |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | /* }}} */ |
|---|
| 373 | void xc_sandbox_free(xc_sandbox_t *sandbox, int install TSRMLS_DC) /* {{{ */ |
|---|
| 374 | { |
|---|
| 375 | /* restore first first install function/class */ |
|---|
| 376 | CG(function_table) = OG(function_table); |
|---|
| 377 | CG(class_table) = OG(class_table); |
|---|
| 378 | EG(class_table) = CG(class_table); |
|---|
| 379 | |
|---|
| 380 | if (install) { |
|---|
| 381 | xc_sandbox_install(sandbox TSRMLS_CC); |
|---|
| 382 | |
|---|
| 383 | /* no free as it's installed */ |
|---|
| 384 | TG(function_table).pDestructor = NULL; |
|---|
| 385 | TG(class_table).pDestructor = NULL; |
|---|
| 386 | TG(open_files)->dtor = NULL; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /* destroy all the tmp */ |
|---|
| 390 | zend_hash_destroy(&TG(function_table)); |
|---|
| 391 | zend_hash_destroy(&TG(class_table)); |
|---|
| 392 | zend_hash_destroy(TG(included_files)); |
|---|
| 393 | zend_llist_destroy(TG(open_files)); |
|---|
| 394 | |
|---|
| 395 | /* restore orig here, as EG/CG holded tmp before */ |
|---|
| 396 | memcpy(&EG(included_files), &OG(included_files), sizeof(EG(included_files))); |
|---|
| 397 | memcpy(&CG(open_files), &OG(open_files), sizeof(CG(open_files))); |
|---|
| 398 | |
|---|
| 399 | if (sandbox->alloc) { |
|---|
| 400 | efree(sandbox); |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | /* }}} */ |
|---|