| 1 | |
|---|
| 2 | #undef DEBUG |
|---|
| 3 | |
|---|
| 4 | /* {{{ macros */ |
|---|
| 5 | #include <stdlib.h> |
|---|
| 6 | #include <stdio.h> |
|---|
| 7 | #include <string.h> |
|---|
| 8 | |
|---|
| 9 | #include <signal.h> |
|---|
| 10 | |
|---|
| 11 | #include "php.h" |
|---|
| 12 | #include "ext/standard/info.h" |
|---|
| 13 | #include "ext/standard/md5.h" |
|---|
| 14 | #include "zend_extensions.h" |
|---|
| 15 | #include "SAPI.h" |
|---|
| 16 | |
|---|
| 17 | #include "xcache.h" |
|---|
| 18 | #include "optimizer.h" |
|---|
| 19 | #include "coverager.h" |
|---|
| 20 | #include "disassembler.h" |
|---|
| 21 | #include "align.h" |
|---|
| 22 | #include "stack.h" |
|---|
| 23 | #include "xcache_globals.h" |
|---|
| 24 | #include "processor.h" |
|---|
| 25 | #include "utils.h" |
|---|
| 26 | #include "const_string.h" |
|---|
| 27 | #include "opcode_spec.h" |
|---|
| 28 | |
|---|
| 29 | #ifdef DEBUG |
|---|
| 30 | # undef NDEBUG |
|---|
| 31 | # undef inline |
|---|
| 32 | # define inline |
|---|
| 33 | #else |
|---|
| 34 | # ifndef NDEBUG |
|---|
| 35 | # define NDEBUG |
|---|
| 36 | # endif |
|---|
| 37 | #endif |
|---|
| 38 | #include <assert.h> |
|---|
| 39 | |
|---|
| 40 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
|---|
| 41 | #define LOCK(x) xc_lock(x->lck) |
|---|
| 42 | #define UNLOCK(x) xc_unlock(x->lck) |
|---|
| 43 | #define ENTER_LOCK(x) do { \ |
|---|
| 44 | int catched = 0; \ |
|---|
| 45 | xc_lock(x->lck); \ |
|---|
| 46 | zend_try { \ |
|---|
| 47 | do |
|---|
| 48 | #define LEAVE_LOCK(x) \ |
|---|
| 49 | while (0); \ |
|---|
| 50 | } zend_catch { \ |
|---|
| 51 | catched = 1; \ |
|---|
| 52 | } zend_end_try(); \ |
|---|
| 53 | xc_unlock(x->lck); \ |
|---|
| 54 | } while(0) |
|---|
| 55 | /* }}} */ |
|---|
| 56 | |
|---|
| 57 | /* {{{ globals */ |
|---|
| 58 | static char *xc_mmap_path = NULL; |
|---|
| 59 | static char *xc_coredump_dir = NULL; |
|---|
| 60 | |
|---|
| 61 | static xc_hash_t xc_php_hcache = {0}; |
|---|
| 62 | static xc_hash_t xc_php_hentry = {0}; |
|---|
| 63 | static xc_hash_t xc_var_hcache = {0}; |
|---|
| 64 | static xc_hash_t xc_var_hentry = {0}; |
|---|
| 65 | |
|---|
| 66 | /* total size */ |
|---|
| 67 | static zend_ulong xc_php_size = 0; |
|---|
| 68 | static zend_ulong xc_var_size = 0; |
|---|
| 69 | |
|---|
| 70 | static xc_cache_t **xc_php_caches = NULL; |
|---|
| 71 | static xc_cache_t **xc_var_caches = NULL; |
|---|
| 72 | |
|---|
| 73 | static zend_bool xc_initized = 0; |
|---|
| 74 | static zend_compile_file_t *origin_compile_file; |
|---|
| 75 | |
|---|
| 76 | static zend_bool xc_test = 0; |
|---|
| 77 | static zend_bool xc_readonly_protection = 0; |
|---|
| 78 | |
|---|
| 79 | static zend_bool xc_module_gotup = 0; |
|---|
| 80 | static zend_bool xc_zend_extension_gotup = 0; |
|---|
| 81 | #if !COMPILE_DL_XCACHE |
|---|
| 82 | # define zend_extension_entry xcache_zend_extension_entry |
|---|
| 83 | #endif |
|---|
| 84 | ZEND_DLEXPORT zend_extension zend_extension_entry; |
|---|
| 85 | ZEND_DECLARE_MODULE_GLOBALS(xcache); |
|---|
| 86 | /* }}} */ |
|---|
| 87 | |
|---|
| 88 | /* any function in *_dmz is only safe be called within locked(single thread) area */ |
|---|
| 89 | |
|---|
| 90 | static inline int xc_entry_equal_dmz(xc_entry_t *a, xc_entry_t *b) /* {{{ */ |
|---|
| 91 | { |
|---|
| 92 | /* this function isn't required but can be in dmz */ |
|---|
| 93 | |
|---|
| 94 | if (a->type != b->type) { |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | switch (a->type) { |
|---|
| 98 | case XC_TYPE_PHP: |
|---|
| 99 | #ifdef HAVE_INODE |
|---|
| 100 | do { |
|---|
| 101 | xc_entry_data_php_t *ap = a->data.php; |
|---|
| 102 | xc_entry_data_php_t *bp = b->data.php; |
|---|
| 103 | return ap->inode == bp->inode |
|---|
| 104 | && ap->device == bp->device; |
|---|
| 105 | } while(0); |
|---|
| 106 | #endif |
|---|
| 107 | /* fall */ |
|---|
| 108 | |
|---|
| 109 | case XC_TYPE_VAR: |
|---|
| 110 | do { |
|---|
| 111 | #ifdef IS_UNICODE |
|---|
| 112 | if (a->name_type == IS_UNICODE) { |
|---|
| 113 | if (a->name.ustr.len != b->name.ustr.len) { |
|---|
| 114 | return 0; |
|---|
| 115 | } |
|---|
| 116 | return memcmp(a->name.ustr.val, b->name.ustr.val, (a->name.ustr.len + 1) * sizeof(UChar)) == 0; |
|---|
| 117 | } |
|---|
| 118 | else { |
|---|
| 119 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 120 | } |
|---|
| 121 | #else |
|---|
| 122 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 123 | #endif |
|---|
| 124 | |
|---|
| 125 | } while(0); |
|---|
| 126 | default: |
|---|
| 127 | assert(0); |
|---|
| 128 | } |
|---|
| 129 | return 0; |
|---|
| 130 | } |
|---|
| 131 | /* }}} */ |
|---|
| 132 | static void xc_entry_free_dmz(volatile xc_entry_t *xce) /* {{{ */ |
|---|
| 133 | { |
|---|
| 134 | xc_mem_free(xce->cache->mem, (xc_entry_t *)xce); |
|---|
| 135 | } |
|---|
| 136 | /* }}} */ |
|---|
| 137 | static void xc_entry_add_dmz(xc_entry_t *xce) /* {{{ */ |
|---|
| 138 | { |
|---|
| 139 | xc_entry_t **head = &(xce->cache->entries[xce->hvalue]); |
|---|
| 140 | xce->next = *head; |
|---|
| 141 | *head = xce; |
|---|
| 142 | xce->cache->entries_count ++; |
|---|
| 143 | } |
|---|
| 144 | /* }}} */ |
|---|
| 145 | static xc_entry_t *xc_entry_store_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 146 | { |
|---|
| 147 | xc_entry_t *stored_xce; |
|---|
| 148 | |
|---|
| 149 | xce->hits = 0; |
|---|
| 150 | xce->ctime = XG(request_time); |
|---|
| 151 | xce->atime = XG(request_time); |
|---|
| 152 | stored_xce = xc_processor_store_xc_entry_t(xce TSRMLS_CC); |
|---|
| 153 | if (stored_xce) { |
|---|
| 154 | xc_entry_add_dmz(stored_xce); |
|---|
| 155 | return stored_xce; |
|---|
| 156 | } |
|---|
| 157 | else { |
|---|
| 158 | xce->cache->ooms ++; |
|---|
| 159 | return NULL; |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | /* }}} */ |
|---|
| 163 | static void xc_entry_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 164 | { |
|---|
| 165 | xc_entry_t **last = &(xce->cache->entries[xce->hvalue]); |
|---|
| 166 | xc_entry_t *p; |
|---|
| 167 | for (p = *last; p; last = &(p->next), p = p->next) { |
|---|
| 168 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 169 | *last = p->next; |
|---|
| 170 | xce->cache->entries_count ++; |
|---|
| 171 | if (p->refcount == 0) { |
|---|
| 172 | xc_entry_free_dmz(p); |
|---|
| 173 | } |
|---|
| 174 | else { |
|---|
| 175 | p->next = p->cache->deletes; |
|---|
| 176 | p->cache->deletes = p; |
|---|
| 177 | p->dtime = XG(request_time); |
|---|
| 178 | xce->cache->deletes_count ++; |
|---|
| 179 | } |
|---|
| 180 | return; |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | assert(0); |
|---|
| 184 | } |
|---|
| 185 | /* }}} */ |
|---|
| 186 | static xc_entry_t *xc_entry_find_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 187 | { |
|---|
| 188 | xc_entry_t *p; |
|---|
| 189 | for (p = xce->cache->entries[xce->hvalue]; p; p = p->next) { |
|---|
| 190 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 191 | if (p->type == XC_TYPE_VAR || /* PHP */ p->data.php->mtime == xce->data.php->mtime) { |
|---|
| 192 | p->hits ++; |
|---|
| 193 | p->atime = XG(request_time); |
|---|
| 194 | return p; |
|---|
| 195 | } |
|---|
| 196 | else { |
|---|
| 197 | xc_entry_remove_dmz(p TSRMLS_CC); |
|---|
| 198 | return NULL; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | return NULL; |
|---|
| 203 | } |
|---|
| 204 | /* }}} */ |
|---|
| 205 | static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 206 | { |
|---|
| 207 | xce->refcount ++; |
|---|
| 208 | xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 209 | } |
|---|
| 210 | /* }}} */ |
|---|
| 211 | #if 0 |
|---|
| 212 | static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 213 | { |
|---|
| 214 | xce->refcount ++; |
|---|
| 215 | xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 216 | } |
|---|
| 217 | /* }}} */ |
|---|
| 218 | #endif |
|---|
| 219 | |
|---|
| 220 | /* helper functions for user functions */ |
|---|
| 221 | static void xc_fillinfo_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 222 | { |
|---|
| 223 | zval *blocks; |
|---|
| 224 | const xc_block_t *b; |
|---|
| 225 | #ifndef NDEBUG |
|---|
| 226 | xc_memsize_t avail = 0; |
|---|
| 227 | #endif |
|---|
| 228 | xc_mem_t *mem = cache->mem; |
|---|
| 229 | |
|---|
| 230 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
|---|
| 231 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling); |
|---|
| 232 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->misses); |
|---|
| 233 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits); |
|---|
| 234 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs); |
|---|
| 235 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms); |
|---|
| 236 | |
|---|
| 237 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count); |
|---|
| 238 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count); |
|---|
| 239 | |
|---|
| 240 | MAKE_STD_ZVAL(blocks); |
|---|
| 241 | array_init(blocks); |
|---|
| 242 | |
|---|
| 243 | add_assoc_long_ex(return_value, ZEND_STRS("size"), xc_mem_size(mem)); |
|---|
| 244 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), xc_mem_avail(mem)); |
|---|
| 245 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
|---|
| 246 | |
|---|
| 247 | for (b = xc_mem_freeblock_first(mem); b; b = xc_mem_freeblock_next(b)) { |
|---|
| 248 | zval *bi; |
|---|
| 249 | |
|---|
| 250 | MAKE_STD_ZVAL(bi); |
|---|
| 251 | array_init(bi); |
|---|
| 252 | |
|---|
| 253 | add_assoc_long_ex(bi, ZEND_STRS("size"), xc_mem_block_size(b)); |
|---|
| 254 | add_assoc_long_ex(bi, ZEND_STRS("offset"), xc_mem_block_offset(mem, b)); |
|---|
| 255 | add_next_index_zval(blocks, bi); |
|---|
| 256 | #ifndef NDEBUG |
|---|
| 257 | avail += b->size; |
|---|
| 258 | #endif |
|---|
| 259 | } |
|---|
| 260 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
|---|
| 261 | assert(avail == xc_mem_avail(mem)); |
|---|
| 262 | } |
|---|
| 263 | /* }}} */ |
|---|
| 264 | static void xc_fillentry_dmz(xc_entry_t *entry, int del, zval *list TSRMLS_DC) /* {{{ */ |
|---|
| 265 | { |
|---|
| 266 | zval* ei; |
|---|
| 267 | xc_entry_data_php_t *php; |
|---|
| 268 | xc_entry_data_var_t *var; |
|---|
| 269 | |
|---|
| 270 | ALLOC_INIT_ZVAL(ei); |
|---|
| 271 | array_init(ei); |
|---|
| 272 | |
|---|
| 273 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
|---|
| 274 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry->refcount); |
|---|
| 275 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
|---|
| 276 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
|---|
| 277 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
|---|
| 278 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
|---|
| 279 | #ifdef IS_UNICODE |
|---|
| 280 | do { |
|---|
| 281 | zval *zv; |
|---|
| 282 | ALLOC_INIT_ZVAL(zv); |
|---|
| 283 | switch (entry->name_type) { |
|---|
| 284 | case IS_UNICODE: |
|---|
| 285 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
|---|
| 286 | break; |
|---|
| 287 | case IS_STRING: |
|---|
| 288 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
|---|
| 289 | break; |
|---|
| 290 | default: |
|---|
| 291 | assert(0); |
|---|
| 292 | } |
|---|
| 293 | zv->type = entry->name_type; |
|---|
| 294 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
|---|
| 295 | } while (0); |
|---|
| 296 | #else |
|---|
| 297 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len + 1, 1); |
|---|
| 298 | #endif |
|---|
| 299 | switch (entry->type) { |
|---|
| 300 | case XC_TYPE_PHP: |
|---|
| 301 | php = entry->data.php; |
|---|
| 302 | add_assoc_long_ex(ei, ZEND_STRS("sourcesize"), php->sourcesize); |
|---|
| 303 | #ifdef HAVE_INODE |
|---|
| 304 | add_assoc_long_ex(ei, ZEND_STRS("device"), php->device); |
|---|
| 305 | add_assoc_long_ex(ei, ZEND_STRS("inode"), php->inode); |
|---|
| 306 | #endif |
|---|
| 307 | add_assoc_long_ex(ei, ZEND_STRS("mtime"), php->mtime); |
|---|
| 308 | |
|---|
| 309 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
|---|
| 310 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
|---|
| 311 | break; |
|---|
| 312 | case XC_TYPE_VAR: |
|---|
| 313 | var = entry->data.var; |
|---|
| 314 | break; |
|---|
| 315 | |
|---|
| 316 | default: |
|---|
| 317 | assert(0); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | add_next_index_zval(list, ei); |
|---|
| 321 | } |
|---|
| 322 | /* }}} */ |
|---|
| 323 | static void xc_filllist_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 324 | { |
|---|
| 325 | zval* list; |
|---|
| 326 | int i, c; |
|---|
| 327 | xc_entry_t *e; |
|---|
| 328 | |
|---|
| 329 | ALLOC_INIT_ZVAL(list); |
|---|
| 330 | array_init(list); |
|---|
| 331 | |
|---|
| 332 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 333 | for (e = cache->entries[i]; e; e = e->next) { |
|---|
| 334 | xc_fillentry_dmz(e, 0, list TSRMLS_CC); |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | add_assoc_zval(return_value, "cache_list", list); |
|---|
| 338 | |
|---|
| 339 | ALLOC_INIT_ZVAL(list); |
|---|
| 340 | array_init(list); |
|---|
| 341 | for (e = cache->deletes; e; e = e->next) { |
|---|
| 342 | xc_fillentry_dmz(e, 1, list TSRMLS_CC); |
|---|
| 343 | } |
|---|
| 344 | add_assoc_zval(return_value, "deleted_list", list); |
|---|
| 345 | } |
|---|
| 346 | /* }}} */ |
|---|
| 347 | |
|---|
| 348 | static zend_op_array *xc_entry_install(xc_entry_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */ |
|---|
| 349 | { |
|---|
| 350 | zend_uint i; |
|---|
| 351 | xc_entry_data_php_t *p = xce->data.php; |
|---|
| 352 | #ifndef ZEND_ENGINE_2 |
|---|
| 353 | /* new ptr which is stored inside CG(class_table) */ |
|---|
| 354 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt); |
|---|
| 355 | #endif |
|---|
| 356 | |
|---|
| 357 | /* install function */ |
|---|
| 358 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
|---|
| 359 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
|---|
| 360 | xc_install_function(xce->name.str.val, &fi->func, |
|---|
| 361 | UNISW(0, fi->type), fi->key, fi->key_size TSRMLS_CC); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | /* install class */ |
|---|
| 365 | for (i = 0; i < p->classinfo_cnt; i ++) { |
|---|
| 366 | xc_classinfo_t *ci = &p->classinfos[i]; |
|---|
| 367 | #ifndef ZEND_ENGINE_2 |
|---|
| 368 | zend_class_entry *ce = CestToCePtr(ci->cest); |
|---|
| 369 | /* fix pointer to the be which inside class_table */ |
|---|
| 370 | if (ce->parent) { |
|---|
| 371 | zend_uint class_idx = (/* class_num */ (int) ce->parent) - 1; |
|---|
| 372 | assert(class_idx < i); |
|---|
| 373 | ci->cest.parent = new_cest_ptrs[class_idx]; |
|---|
| 374 | } |
|---|
| 375 | new_cest_ptrs[i] = |
|---|
| 376 | #endif |
|---|
| 377 | xc_install_class(xce->name.str.val, &ci->cest, |
|---|
| 378 | UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC); |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | i = 1; |
|---|
| 382 | zend_hash_add(&EG(included_files), xce->name.str.val, xce->name.str.len+1, (void *)&i, sizeof(int), NULL); |
|---|
| 383 | zend_llist_add_element(&CG(open_files), h); |
|---|
| 384 | |
|---|
| 385 | #ifndef ZEND_ENGINE_2 |
|---|
| 386 | free_alloca(new_cest_ptrs); |
|---|
| 387 | #endif |
|---|
| 388 | return p->op_array; |
|---|
| 389 | } |
|---|
| 390 | /* }}} */ |
|---|
| 391 | static void xc_entry_gc_real(xc_cache_t **caches, int size TSRMLS_DC) /* {{{ */ |
|---|
| 392 | { |
|---|
| 393 | time_t t = XG(request_time); |
|---|
| 394 | int i; |
|---|
| 395 | xc_cache_t *cache; |
|---|
| 396 | typedef xc_entry_t *xc_delete_t; |
|---|
| 397 | xc_delete_t p, *last; |
|---|
| 398 | |
|---|
| 399 | for (i = 0; i < size; i ++) { |
|---|
| 400 | cache = caches[i]; |
|---|
| 401 | ENTER_LOCK(cache) { |
|---|
| 402 | if (cache->deletes) { |
|---|
| 403 | last = (xc_delete_t *) &cache->deletes; |
|---|
| 404 | for (p = *last; p; p = p->next) { |
|---|
| 405 | if (t - p->dtime > 3600) { |
|---|
| 406 | p->refcount = 0; |
|---|
| 407 | /* issue warning here */ |
|---|
| 408 | } |
|---|
| 409 | if (p->refcount == 0) { |
|---|
| 410 | *last = p->next; |
|---|
| 411 | cache->deletes_count --; |
|---|
| 412 | xc_entry_free_dmz(p); |
|---|
| 413 | } |
|---|
| 414 | else { |
|---|
| 415 | last = &(p->next); |
|---|
| 416 | } |
|---|
| 417 | } |
|---|
| 418 | } |
|---|
| 419 | } LEAVE_LOCK(cache); |
|---|
| 420 | } |
|---|
| 421 | } |
|---|
| 422 | /* }}} */ |
|---|
| 423 | static void xc_entry_gc(TSRMLS_D) /* {{{ */ |
|---|
| 424 | { |
|---|
| 425 | xc_entry_gc_real(xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
|---|
| 426 | xc_entry_gc_real(xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
|---|
| 427 | } |
|---|
| 428 | /* }}} */ |
|---|
| 429 | static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */ |
|---|
| 430 | { |
|---|
| 431 | int i; |
|---|
| 432 | xc_stack_t *s; |
|---|
| 433 | xc_cache_t *cache; |
|---|
| 434 | xc_entry_t *xce; |
|---|
| 435 | |
|---|
| 436 | for (i = 0; i < cachecount; i ++) { |
|---|
| 437 | s = &holds[i]; |
|---|
| 438 | if (xc_stack_size(s)) { |
|---|
| 439 | cache = ((xc_entry_t *)xc_stack_top(s))->cache; |
|---|
| 440 | ENTER_LOCK(cache) { |
|---|
| 441 | while (xc_stack_size(holds)) { |
|---|
| 442 | xce = (xc_entry_t*) xc_stack_pop(holds); |
|---|
| 443 | xce->refcount --; |
|---|
| 444 | assert(xce->refcount >= 0); |
|---|
| 445 | } |
|---|
| 446 | } LEAVE_LOCK(cache); |
|---|
| 447 | } |
|---|
| 448 | } |
|---|
| 449 | } |
|---|
| 450 | /* }}} */ |
|---|
| 451 | static void xc_entry_unholds(TSRMLS_D) /* {{{ */ |
|---|
| 452 | { |
|---|
| 453 | xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
|---|
| 454 | xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
|---|
| 455 | } |
|---|
| 456 | /* }}} */ |
|---|
| 457 | static int xc_stat(const char *filename, const char *include_path, struct stat *pbuf TSRMLS_DC) /* {{{ */ |
|---|
| 458 | { |
|---|
| 459 | char filepath[1024]; |
|---|
| 460 | char *paths, *path; |
|---|
| 461 | char *tokbuf; |
|---|
| 462 | int size = strlen(include_path) + 1; |
|---|
| 463 | char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' }; |
|---|
| 464 | |
|---|
| 465 | paths = (char *)do_alloca(size); |
|---|
| 466 | memcpy(paths, include_path, size); |
|---|
| 467 | |
|---|
| 468 | for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) { |
|---|
| 469 | if (strlen(path) + strlen(filename) + 1 > 1024) { |
|---|
| 470 | continue; |
|---|
| 471 | } |
|---|
| 472 | snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); |
|---|
| 473 | if (VCWD_STAT(filepath, pbuf) == 0) { |
|---|
| 474 | free_alloca(paths); |
|---|
| 475 | return 0; |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | free_alloca(paths); |
|---|
| 480 | |
|---|
| 481 | return 1; |
|---|
| 482 | } |
|---|
| 483 | /* }}} */ |
|---|
| 484 | |
|---|
| 485 | #define HASH(i) (i) |
|---|
| 486 | #define HASH_USTR_L(t, s, l) HASH(zend_u_inline_hash_func(t, s, (l + 1) * sizeof(UChar))) |
|---|
| 487 | #define HASH_STR_L(s, l) HASH(zend_inline_hash_func(s, l + 1)) |
|---|
| 488 | #define HASH_STR(s) HASH_STR_L(s, strlen(s) + 1) |
|---|
| 489 | #define HASH_NUM(n) HASH(n) |
|---|
| 490 | static inline xc_hash_value_t xc_entry_hash_var(xc_entry_t *xce) /* {{{ */ |
|---|
| 491 | { |
|---|
| 492 | return UNISW(NOTHING, UG(unicode) ? HASH_USTR_L(xce->name_type, (char *)xce->name.ustr.val, xce->name.ustr.len) :) |
|---|
| 493 | HASH_STR_L(xce->name.str.val, xce->name.str.len); |
|---|
| 494 | } |
|---|
| 495 | /* }}} */ |
|---|
| 496 | static inline xc_hash_value_t xc_entry_hash_php(xc_entry_t *xce) /* {{{ */ |
|---|
| 497 | { |
|---|
| 498 | #ifdef HAVE_INODE |
|---|
| 499 | return HASH(xce->data.php->device + xce->data.php->inode); |
|---|
| 500 | #else |
|---|
| 501 | return xc_entry_hash_var(xce); |
|---|
| 502 | #endif |
|---|
| 503 | } |
|---|
| 504 | /* }}} */ |
|---|
| 505 | static int xc_entry_init_key_php(xc_entry_t *xce, char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 506 | { |
|---|
| 507 | struct stat buf, *pbuf; |
|---|
| 508 | xc_hash_value_t hv; |
|---|
| 509 | int cacheid; |
|---|
| 510 | xc_entry_data_php_t *php; |
|---|
| 511 | |
|---|
| 512 | if (!filename || !SG(request_info).path_translated) { |
|---|
| 513 | return 0; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | do { |
|---|
| 517 | if (strcmp(SG(request_info).path_translated, filename) == 0) { |
|---|
| 518 | /* sapi has already done this stat() for us */ |
|---|
| 519 | pbuf = sapi_get_stat(TSRMLS_C); |
|---|
| 520 | if (pbuf) { |
|---|
| 521 | break; |
|---|
| 522 | } |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | pbuf = &buf; |
|---|
| 526 | if (IS_ABSOLUTE_PATH(filename, strlen(filename))) { |
|---|
| 527 | if (VCWD_STAT(filename, pbuf) != 0) { |
|---|
| 528 | return 0; |
|---|
| 529 | } |
|---|
| 530 | } |
|---|
| 531 | else { |
|---|
| 532 | if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != 0) { |
|---|
| 533 | return 0; |
|---|
| 534 | } |
|---|
| 535 | } |
|---|
| 536 | } while (0); |
|---|
| 537 | |
|---|
| 538 | if (XG(request_time) - pbuf->st_mtime < 2) { |
|---|
| 539 | return 0; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | UNISW(NOTHING, xce->name_type = IS_STRING;) |
|---|
| 543 | xce->name.str.val = filename; |
|---|
| 544 | xce->name.str.len = strlen(filename); |
|---|
| 545 | |
|---|
| 546 | php = xce->data.php; |
|---|
| 547 | php->mtime = pbuf->st_mtime; |
|---|
| 548 | #ifdef HAVE_INODE |
|---|
| 549 | php->device = pbuf->st_dev; |
|---|
| 550 | php->inode = pbuf->st_ino; |
|---|
| 551 | #endif |
|---|
| 552 | php->sourcesize = pbuf->st_size; |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | hv = xc_entry_hash_php(xce); |
|---|
| 556 | cacheid = (hv & xc_php_hcache.mask); |
|---|
| 557 | xce->cache = xc_php_caches[cacheid]; |
|---|
| 558 | hv >>= xc_php_hcache.bits; |
|---|
| 559 | xce->hvalue = (hv & xc_php_hentry.mask); |
|---|
| 560 | |
|---|
| 561 | xce->type = XC_TYPE_PHP; |
|---|
| 562 | return 1; |
|---|
| 563 | } |
|---|
| 564 | /* }}} */ |
|---|
| 565 | static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 566 | { |
|---|
| 567 | xc_sandbox_t sandbox; |
|---|
| 568 | zend_op_array *op_array; |
|---|
| 569 | xc_entry_t xce, *stored_xce; |
|---|
| 570 | xc_entry_data_php_t php; |
|---|
| 571 | xc_cache_t *cache; |
|---|
| 572 | zend_bool clogged = 0; |
|---|
| 573 | zend_bool catched = 0; |
|---|
| 574 | char *filename; |
|---|
| 575 | |
|---|
| 576 | if (!xc_initized) { |
|---|
| 577 | assert(0); |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | if (!XG(cacher)) { |
|---|
| 581 | op_array = origin_compile_file(h, type TSRMLS_CC); |
|---|
| 582 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 583 | if (XG(optimizer)) { |
|---|
| 584 | xc_optimize(op_array TSRMLS_CC); |
|---|
| 585 | } |
|---|
| 586 | #endif |
|---|
| 587 | return op_array; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | /* {{{ prepare key |
|---|
| 591 | * include_once() and require_once() gives us opened_path |
|---|
| 592 | * however, include() and require() non-absolute path which break |
|---|
| 593 | * included_files, and may confuse with (include|require)_once |
|---|
| 594 | * -- Xuefer |
|---|
| 595 | */ |
|---|
| 596 | |
|---|
| 597 | filename = h->opened_path ? h->opened_path : h->filename; |
|---|
| 598 | xce.data.php = &php; |
|---|
| 599 | if (!xc_entry_init_key_php(&xce, filename TSRMLS_CC)) { |
|---|
| 600 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 601 | } |
|---|
| 602 | cache = xce.cache; |
|---|
| 603 | /* }}} */ |
|---|
| 604 | /* {{{ restore */ |
|---|
| 605 | /* stale precheck */ |
|---|
| 606 | if (cache->compiling) { |
|---|
| 607 | cache->clogs ++; /* is it safe here? */ |
|---|
| 608 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | stored_xce = NULL; |
|---|
| 612 | op_array = NULL; |
|---|
| 613 | ENTER_LOCK(cache) { |
|---|
| 614 | /* clogged */ |
|---|
| 615 | if (cache->compiling) { |
|---|
| 616 | cache->clogs ++; |
|---|
| 617 | op_array = NULL; |
|---|
| 618 | clogged = 1; |
|---|
| 619 | break; |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 623 | /* found */ |
|---|
| 624 | if (stored_xce) { |
|---|
| 625 | #ifdef DEBUG |
|---|
| 626 | fprintf(stderr, "found %s, catch it\n", stored_xce->name.str.val); |
|---|
| 627 | #endif |
|---|
| 628 | xc_entry_hold_php_dmz(stored_xce TSRMLS_CC); |
|---|
| 629 | cache->hits ++; |
|---|
| 630 | break; |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | cache->compiling = XG(request_time); |
|---|
| 634 | cache->misses ++; |
|---|
| 635 | } LEAVE_LOCK(cache); |
|---|
| 636 | |
|---|
| 637 | /* found */ |
|---|
| 638 | if (stored_xce) { |
|---|
| 639 | goto restore; |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | /* clogged */ |
|---|
| 643 | if (clogged) { |
|---|
| 644 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 645 | } |
|---|
| 646 | /* }}} */ |
|---|
| 647 | |
|---|
| 648 | /* {{{ compile */ |
|---|
| 649 | #ifdef DEBUG |
|---|
| 650 | fprintf(stderr, "compiling %s\n", filename); |
|---|
| 651 | #endif |
|---|
| 652 | |
|---|
| 653 | /* make compile inside sandbox */ |
|---|
| 654 | xc_sandbox_init(&sandbox, filename TSRMLS_CC); |
|---|
| 655 | |
|---|
| 656 | zend_try { |
|---|
| 657 | op_array = origin_compile_file(h, type TSRMLS_CC); |
|---|
| 658 | } zend_catch { |
|---|
| 659 | catched = 1; |
|---|
| 660 | } zend_end_try(); |
|---|
| 661 | |
|---|
| 662 | if (catched) { |
|---|
| 663 | goto err_bailout; |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | if (op_array == NULL) { |
|---|
| 667 | goto err_oparray; |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 671 | if (XG(optimizer)) { |
|---|
| 672 | xc_optimize(op_array TSRMLS_CC); |
|---|
| 673 | } |
|---|
| 674 | #endif |
|---|
| 675 | |
|---|
| 676 | php.op_array = op_array; |
|---|
| 677 | |
|---|
| 678 | php.funcinfo_cnt = zend_hash_num_elements(CG(function_table)); |
|---|
| 679 | php.classinfo_cnt = zend_hash_num_elements(CG(class_table)); |
|---|
| 680 | |
|---|
| 681 | php.funcinfos = ECALLOC_N(php.funcinfos, php.funcinfo_cnt); |
|---|
| 682 | if (!php.funcinfos) { |
|---|
| 683 | goto err_func; |
|---|
| 684 | } |
|---|
| 685 | php.classinfos = ECALLOC_N(php.classinfos, php.classinfo_cnt); |
|---|
| 686 | if (!php.classinfos) { |
|---|
| 687 | goto err_class; |
|---|
| 688 | } |
|---|
| 689 | /* }}} */ |
|---|
| 690 | /* {{{ shallow copy, pointers only */ { |
|---|
| 691 | Bucket *b; |
|---|
| 692 | unsigned int i; |
|---|
| 693 | |
|---|
| 694 | b = CG(function_table)->pListHead; |
|---|
| 695 | for (i = 0; b; i ++, b = b->pListNext) { |
|---|
| 696 | xc_funcinfo_t *fi = &php.funcinfos[i]; |
|---|
| 697 | |
|---|
| 698 | assert(i < php.funcinfo_cnt); |
|---|
| 699 | assert(b->pData); |
|---|
| 700 | memcpy(&fi->func, b->pData, sizeof(zend_function)); |
|---|
| 701 | UNISW(NOTHING, fi->type = b->key.type;) |
|---|
| 702 | fi->key = BUCKET_KEY(b); |
|---|
| 703 | fi->key_size = b->nKeyLength; |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | b = CG(class_table)->pListHead; |
|---|
| 707 | for (i = 0; b; i ++, b = b->pListNext) { |
|---|
| 708 | xc_classinfo_t *ci = &php.classinfos[i]; |
|---|
| 709 | |
|---|
| 710 | assert(i < php.classinfo_cnt); |
|---|
| 711 | assert(b->pData); |
|---|
| 712 | memcpy(&ci->cest, b->pData, sizeof(xc_cest_t)); |
|---|
| 713 | UNISW(NOTHING, ci->type = b->key.type;) |
|---|
| 714 | ci->key = BUCKET_KEY(b); |
|---|
| 715 | ci->key_size = b->nKeyLength; |
|---|
| 716 | /* need to fix inside store */ |
|---|
| 717 | } |
|---|
| 718 | } |
|---|
| 719 | /* }}} */ |
|---|
| 720 | xc_entry_gc(TSRMLS_C); |
|---|
| 721 | ENTER_LOCK(cache) { /* {{{ store/add entry */ |
|---|
| 722 | stored_xce = xc_entry_store_dmz(&xce TSRMLS_CC); |
|---|
| 723 | } LEAVE_LOCK(cache); |
|---|
| 724 | /* }}} */ |
|---|
| 725 | #ifdef DEBUG |
|---|
| 726 | fprintf(stderr, "stored\n"); |
|---|
| 727 | #endif |
|---|
| 728 | |
|---|
| 729 | efree(xce.data.php->classinfos); |
|---|
| 730 | err_class: |
|---|
| 731 | efree(xce.data.php->funcinfos); |
|---|
| 732 | err_func: |
|---|
| 733 | err_oparray: |
|---|
| 734 | err_bailout: |
|---|
| 735 | |
|---|
| 736 | if (xc_test && stored_xce) { |
|---|
| 737 | /* no install, keep open_files too for h */ |
|---|
| 738 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
|---|
| 739 | sandbox.tmp_open_files->dtor = NULL; |
|---|
| 740 | } |
|---|
| 741 | else { |
|---|
| 742 | xc_sandbox_free(&sandbox, 1 TSRMLS_CC); |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | ENTER_LOCK(cache) { |
|---|
| 746 | cache->compiling = 0; |
|---|
| 747 | } LEAVE_LOCK(cache); |
|---|
| 748 | if (catched) { |
|---|
| 749 | zend_bailout(); |
|---|
| 750 | } |
|---|
| 751 | if (xc_test && stored_xce) { |
|---|
| 752 | |
|---|