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