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