| 1 | #if 0 |
|---|
| 2 | #define XCACHE_DEBUG |
|---|
| 3 | #endif |
|---|
| 4 | |
|---|
| 5 | #if 0 |
|---|
| 6 | #define SHOW_DPRINT |
|---|
| 7 | #endif |
|---|
| 8 | |
|---|
| 9 | /* {{{ macros */ |
|---|
| 10 | #include "xc_cacher.h" |
|---|
| 11 | #include "xcache.h" |
|---|
| 12 | #include "xc_processor.h" |
|---|
| 13 | #include "xcache_globals.h" |
|---|
| 14 | #include "xcache/xc_extension.h" |
|---|
| 15 | #include "xcache/xc_ini.h" |
|---|
| 16 | #include "xcache/xc_utils.h" |
|---|
| 17 | #include "xcache/xc_sandbox.h" |
|---|
| 18 | #include "util/xc_trace.h" |
|---|
| 19 | #include "util/xc_vector.h" |
|---|
| 20 | #include "util/xc_align.h" |
|---|
| 21 | |
|---|
| 22 | #include "php.h" |
|---|
| 23 | #include "ext/standard/info.h" |
|---|
| 24 | #include "ext/standard/md5.h" |
|---|
| 25 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 26 | #include "ext/date/php_date.h" |
|---|
| 27 | #endif |
|---|
| 28 | #include "ext/standard/php_math.h" |
|---|
| 29 | #include "SAPI.h" |
|---|
| 30 | |
|---|
| 31 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > (pentry)->ctime + (time_t) (pentry)->ttl) |
|---|
| 32 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
|---|
| 33 | #define LOCK(x) xc_lock((x)->lck) |
|---|
| 34 | #define UNLOCK(x) xc_unlock((x)->lck) |
|---|
| 35 | |
|---|
| 36 | #define ENTER_LOCK_EX(x) \ |
|---|
| 37 | xc_lock((x)->lck); \ |
|---|
| 38 | zend_try { \ |
|---|
| 39 | do |
|---|
| 40 | #define LEAVE_LOCK_EX(x) \ |
|---|
| 41 | while (0); \ |
|---|
| 42 | } zend_catch { \ |
|---|
| 43 | catched = 1; \ |
|---|
| 44 | } zend_end_try(); \ |
|---|
| 45 | xc_unlock((x)->lck) |
|---|
| 46 | |
|---|
| 47 | #define ENTER_LOCK(x) do { \ |
|---|
| 48 | int catched = 0; \ |
|---|
| 49 | ENTER_LOCK_EX(x) |
|---|
| 50 | #define LEAVE_LOCK(x) \ |
|---|
| 51 | LEAVE_LOCK_EX(x); \ |
|---|
| 52 | if (catched) { \ |
|---|
| 53 | zend_bailout(); \ |
|---|
| 54 | } \ |
|---|
| 55 | } while(0) |
|---|
| 56 | /* }}} */ |
|---|
| 57 | |
|---|
| 58 | /* {{{ globals */ |
|---|
| 59 | static char *xc_shm_scheme = NULL; |
|---|
| 60 | static char *xc_mmap_path = NULL; |
|---|
| 61 | |
|---|
| 62 | static zend_bool xc_admin_enable_auth = 1; |
|---|
| 63 | static xc_hash_t xc_php_hcache = { 0, 0, 0 }; |
|---|
| 64 | static xc_hash_t xc_php_hentry = { 0, 0, 0 }; |
|---|
| 65 | static xc_hash_t xc_var_hcache = { 0, 0, 0 }; |
|---|
| 66 | static xc_hash_t xc_var_hentry = { 0, 0, 0 }; |
|---|
| 67 | |
|---|
| 68 | static zend_ulong xc_php_ttl = 0; |
|---|
| 69 | static zend_ulong xc_var_maxttl = 0; |
|---|
| 70 | |
|---|
| 71 | enum { xc_deletes_gc_interval = 120 }; |
|---|
| 72 | static zend_ulong xc_php_gc_interval = 0; |
|---|
| 73 | static zend_ulong xc_var_gc_interval = 0; |
|---|
| 74 | |
|---|
| 75 | /* total size */ |
|---|
| 76 | static zend_ulong xc_php_size = 0; |
|---|
| 77 | static zend_ulong xc_var_size = 0; |
|---|
| 78 | |
|---|
| 79 | static xc_cache_t **xc_php_caches = NULL; |
|---|
| 80 | static xc_cache_t **xc_var_caches = NULL; |
|---|
| 81 | |
|---|
| 82 | static zend_bool xc_initized = 0; |
|---|
| 83 | static time_t xc_init_time = 0; |
|---|
| 84 | static long unsigned xc_init_instance_id = 0; |
|---|
| 85 | #ifdef ZTS |
|---|
| 86 | static long unsigned xc_init_instance_subid = 0; |
|---|
| 87 | #endif |
|---|
| 88 | static zend_compile_file_t *old_compile_file = NULL; |
|---|
| 89 | |
|---|
| 90 | static zend_bool xc_readonly_protection = 0; |
|---|
| 91 | |
|---|
| 92 | zend_bool xc_have_op_array_ctor = 0; |
|---|
| 93 | |
|---|
| 94 | typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t; |
|---|
| 95 | |
|---|
| 96 | /* TODO */ |
|---|
| 97 | static inline zend_bool xc_cache_disabled() |
|---|
| 98 | { |
|---|
| 99 | return 0; |
|---|
| 100 | } |
|---|
| 101 | /* }}} */ |
|---|
| 102 | |
|---|
| 103 | /* any function in *_unlocked is only safe be called within locked (single thread access) area */ |
|---|
| 104 | |
|---|
| 105 | static void xc_php_add_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 106 | { |
|---|
| 107 | xc_entry_data_php_t **head = &(cache->phps[php->hvalue]); |
|---|
| 108 | php->next = *head; |
|---|
| 109 | *head = php; |
|---|
| 110 | cache->phps_count ++; |
|---|
| 111 | } |
|---|
| 112 | /* }}} */ |
|---|
| 113 | static xc_entry_data_php_t *xc_php_store_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 114 | { |
|---|
| 115 | xc_entry_data_php_t *stored_php; |
|---|
| 116 | |
|---|
| 117 | php->hits = 0; |
|---|
| 118 | php->refcount = 0; |
|---|
| 119 | stored_php = xc_processor_store_xc_entry_data_php_t(cache, php TSRMLS_CC); |
|---|
| 120 | if (stored_php) { |
|---|
| 121 | xc_php_add_unlocked(cache, stored_php); |
|---|
| 122 | return stored_php; |
|---|
| 123 | } |
|---|
| 124 | else { |
|---|
| 125 | cache->ooms ++; |
|---|
| 126 | return NULL; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | /* }}} */ |
|---|
| 130 | static xc_entry_data_php_t *xc_php_find_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 131 | { |
|---|
| 132 | xc_entry_data_php_t *p; |
|---|
| 133 | for (p = cache->phps[php->hvalue]; p; p = (xc_entry_data_php_t *) p->next) { |
|---|
| 134 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
|---|
| 135 | p->hits ++; |
|---|
| 136 | return p; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | return NULL; |
|---|
| 140 | } |
|---|
| 141 | /* }}} */ |
|---|
| 142 | static void xc_php_free_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 143 | { |
|---|
| 144 | cache->mem->handlers->free(cache->mem, (xc_entry_data_php_t *)php); |
|---|
| 145 | } |
|---|
| 146 | /* }}} */ |
|---|
| 147 | static void xc_php_addref_unlocked(xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 148 | { |
|---|
| 149 | php->refcount ++; |
|---|
| 150 | } |
|---|
| 151 | /* }}} */ |
|---|
| 152 | static void xc_php_release_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 153 | { |
|---|
| 154 | if (-- php->refcount == 0) { |
|---|
| 155 | xc_entry_data_php_t **pp = &(cache->phps[php->hvalue]); |
|---|
| 156 | xc_entry_data_php_t *p; |
|---|
| 157 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
|---|
| 158 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
|---|
| 159 | /* unlink */ |
|---|
| 160 | *pp = p->next; |
|---|
| 161 | xc_php_free_unlocked(cache, php); |
|---|
| 162 | return; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | assert(0); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | /* }}} */ |
|---|
| 169 | |
|---|
| 170 | static inline int xc_entry_equal_unlocked(xc_entry_type_t type, const xc_entry_t *entry1, const xc_entry_t *entry2 TSRMLS_DC) /* {{{ */ |
|---|
| 171 | { |
|---|
| 172 | /* this function isn't required but can be in unlocked */ |
|---|
| 173 | switch (type) { |
|---|
| 174 | case XC_TYPE_PHP: |
|---|
| 175 | { |
|---|
| 176 | const xc_entry_php_t *php_entry1 = (const xc_entry_php_t *) entry1; |
|---|
| 177 | const xc_entry_php_t *php_entry2 = (const xc_entry_php_t *) entry2; |
|---|
| 178 | if (php_entry1->file_inode && php_entry2->file_inode) { |
|---|
| 179 | zend_bool inodeIsSame = php_entry1->file_inode == php_entry2->file_inode |
|---|
| 180 | && php_entry1->file_device == php_entry2->file_device; |
|---|
| 181 | if (XG(experimental)) { |
|---|
| 182 | /* new experimental behavior: quick check by inode, first */ |
|---|
| 183 | if (!inodeIsSame) { |
|---|
| 184 | return 0; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /* and then opened_path compare */ |
|---|
| 188 | } |
|---|
| 189 | else { |
|---|
| 190 | /* old behavior: inode check only */ |
|---|
| 191 | return inodeIsSame; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | assert(IS_ABSOLUTE_PATH(entry1->name.str.val, entry1->name.str.len)); |
|---|
| 197 | assert(IS_ABSOLUTE_PATH(entry2->name.str.val, entry2->name.str.len)); |
|---|
| 198 | |
|---|
| 199 | return entry1->name.str.len == entry2->name.str.len |
|---|
| 200 | && memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0; |
|---|
| 201 | |
|---|
| 202 | case XC_TYPE_VAR: |
|---|
| 203 | #ifdef IS_UNICODE |
|---|
| 204 | if (entry1->name_type != entry2->name_type) { |
|---|
| 205 | return 0; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | if (entry1->name_type == IS_UNICODE) { |
|---|
| 209 | return entry1->name.ustr.len == entry2->name.ustr.len |
|---|
| 210 | && memcmp(entry1->name.ustr.val, entry2->name.ustr.val, (entry1->name.ustr.len + 1) * sizeof(UChar)) == 0; |
|---|
| 211 | } |
|---|
| 212 | #endif |
|---|
| 213 | return entry1->name.str.len == entry2->name.str.len |
|---|
| 214 | && memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0; |
|---|
| 215 | break; |
|---|
| 216 | |
|---|
| 217 | default: |
|---|
| 218 | assert(0); |
|---|
| 219 | } |
|---|
| 220 | return 0; |
|---|
| 221 | } |
|---|
| 222 | /* }}} */ |
|---|
| 223 | static inline int xc_entry_has_prefix_unlocked(xc_entry_type_t type, xc_entry_t *entry, zval *prefix) /* {{{ */ |
|---|
| 224 | { |
|---|
| 225 | /* this function isn't required but can be in unlocked */ |
|---|
| 226 | |
|---|
| 227 | #ifdef IS_UNICODE |
|---|
| 228 | if (entry->name_type != prefix->type) { |
|---|
| 229 | return 0; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | if (entry->name_type == IS_UNICODE) { |
|---|
| 233 | if (entry->name.ustr.len < Z_USTRLEN_P(prefix)) { |
|---|
| 234 | return 0; |
|---|
| 235 | } |
|---|
| 236 | return memcmp(entry->name.ustr.val, Z_USTRVAL_P(prefix), Z_USTRLEN_P(prefix) * sizeof(UChar)) == 0; |
|---|
| 237 | } |
|---|
| 238 | #endif |
|---|
| 239 | if (prefix->type != IS_STRING) { |
|---|
| 240 | return 0; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | if (entry->name.str.len < Z_STRLEN_P(prefix)) { |
|---|
| 244 | return 0; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | return memcmp(entry->name.str.val, Z_STRVAL_P(prefix), Z_STRLEN_P(prefix)) == 0; |
|---|
| 248 | } |
|---|
| 249 | /* }}} */ |
|---|
| 250 | static void xc_entry_add_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry) /* {{{ */ |
|---|
| 251 | { |
|---|
| 252 | xc_entry_t **head = &(cache->entries[entryslotid]); |
|---|
| 253 | entry->next = *head; |
|---|
| 254 | *head = entry; |
|---|
| 255 | cache->entries_count ++; |
|---|
| 256 | } |
|---|
| 257 | /* }}} */ |
|---|
| 258 | static xc_entry_t *xc_entry_store_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 259 | { |
|---|
| 260 | xc_entry_t *stored_entry; |
|---|
| 261 | |
|---|
| 262 | entry->hits = 0; |
|---|
| 263 | entry->ctime = XG(request_time); |
|---|
| 264 | entry->atime = XG(request_time); |
|---|
| 265 | stored_entry = type == XC_TYPE_PHP |
|---|
| 266 | ? (xc_entry_t *) xc_processor_store_xc_entry_php_t(cache, (xc_entry_php_t *) entry TSRMLS_CC) |
|---|
| 267 | : (xc_entry_t *) xc_processor_store_xc_entry_var_t(cache, (xc_entry_var_t *) entry TSRMLS_CC); |
|---|
| 268 | if (stored_entry) { |
|---|
| 269 | xc_entry_add_unlocked(cache, entryslotid, stored_entry); |
|---|
| 270 | ++cache->updates; |
|---|
| 271 | return stored_entry; |
|---|
| 272 | } |
|---|
| 273 | else { |
|---|
| 274 | cache->ooms ++; |
|---|
| 275 | return NULL; |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | /* }}} */ |
|---|
| 279 | static xc_entry_php_t *xc_entry_php_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
|---|
| 280 | { |
|---|
| 281 | return (xc_entry_php_t *) xc_entry_store_unlocked(XC_TYPE_PHP, cache, entryslotid, (xc_entry_t *) entry_php TSRMLS_CC); |
|---|
| 282 | } |
|---|
| 283 | /* }}} */ |
|---|
| 284 | static xc_entry_var_t *xc_entry_var_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_var_t *entry_var TSRMLS_DC) /* {{{ */ |
|---|
| 285 | { |
|---|
| 286 | return (xc_entry_var_t *) xc_entry_store_unlocked(XC_TYPE_VAR, cache, entryslotid, (xc_entry_t *) entry_var TSRMLS_CC); |
|---|
| 287 | } |
|---|
| 288 | /* }}} */ |
|---|
| 289 | static void xc_entry_free_real_unlocked(xc_entry_type_t type, xc_cache_t *cache, volatile xc_entry_t *entry) /* {{{ */ |
|---|
| 290 | { |
|---|
| 291 | if (type == XC_TYPE_PHP) { |
|---|
| 292 | xc_php_release_unlocked(cache, ((xc_entry_php_t *) entry)->php); |
|---|
| 293 | } |
|---|
| 294 | cache->mem->handlers->free(cache->mem, (xc_entry_t *)entry); |
|---|
| 295 | } |
|---|
| 296 | /* }}} */ |
|---|
| 297 | static void xc_entry_free_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 298 | { |
|---|
| 299 | cache->entries_count --; |
|---|
| 300 | if ((type == XC_TYPE_PHP ? ((xc_entry_php_t *) entry)->refcount : 0) == 0) { |
|---|
| 301 | xc_entry_free_real_unlocked(type, cache, entry); |
|---|
| 302 | } |
|---|
| 303 | else { |
|---|
| 304 | entry->next = cache->deletes; |
|---|
| 305 | cache->deletes = entry; |
|---|
| 306 | entry->dtime = XG(request_time); |
|---|
| 307 | cache->deletes_count ++; |
|---|
| 308 | } |
|---|
| 309 | return; |
|---|
| 310 | } |
|---|
| 311 | /* }}} */ |
|---|
| 312 | static void xc_entry_remove_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 313 | { |
|---|
| 314 | xc_entry_t **pp = &(cache->entries[entryslotid]); |
|---|
| 315 | xc_entry_t *p; |
|---|
| 316 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
|---|
| 317 | if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) { |
|---|
| 318 | /* unlink */ |
|---|
| 319 | *pp = p->next; |
|---|
| 320 | xc_entry_free_unlocked(type, cache, entry TSRMLS_CC); |
|---|
| 321 | return; |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | assert(0); |
|---|
| 325 | } |
|---|
| 326 | /* }}} */ |
|---|
| 327 | static xc_entry_t *xc_entry_find_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 328 | { |
|---|
| 329 | xc_entry_t *p; |
|---|
| 330 | for (p = cache->entries[entryslotid]; p; p = p->next) { |
|---|
| 331 | if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) { |
|---|
| 332 | zend_bool fresh; |
|---|
| 333 | switch (type) { |
|---|
| 334 | case XC_TYPE_PHP: |
|---|
| 335 | { |
|---|
| 336 | xc_entry_php_t *p_php = (xc_entry_php_t *) p; |
|---|
| 337 | xc_entry_php_t *entry_php = (xc_entry_php_t *) entry; |
|---|
| 338 | fresh = p_php->file_mtime == entry_php->file_mtime && p_php->file_size == entry_php->file_size; |
|---|
| 339 | } |
|---|
| 340 | break; |
|---|
| 341 | |
|---|
| 342 | case XC_TYPE_VAR: |
|---|
| 343 | { |
|---|
| 344 | fresh = !VAR_ENTRY_EXPIRED(p); |
|---|
| 345 | } |
|---|
| 346 | break; |
|---|
| 347 | |
|---|
| 348 | default: |
|---|
| 349 | assert(0); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | if (fresh) { |
|---|
| 353 | p->hits ++; |
|---|
| 354 | p->atime = XG(request_time); |
|---|
| 355 | return p; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | xc_entry_remove_unlocked(type, cache, entryslotid, p TSRMLS_CC); |
|---|
| 359 | return NULL; |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | return NULL; |
|---|
| 363 | } |
|---|
| 364 | /* }}} */ |
|---|
| 365 | static void xc_entry_hold_php_unlocked(xc_cache_t *cache, xc_entry_php_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 366 | { |
|---|
| 367 | TRACE("hold %d:%s", entry->file_inode, entry->entry.name.str.val); |
|---|
| 368 | entry->refcount ++; |
|---|
| 369 | xc_stack_push(&XG(php_holds)[cache->cacheid], (void *)entry); |
|---|
| 370 | } |
|---|
| 371 | /* }}} */ |
|---|
| 372 | static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */ |
|---|
| 373 | { |
|---|
| 374 | if (val + 1 >= count) { |
|---|
| 375 | return 0; |
|---|
| 376 | } |
|---|
| 377 | return val + 1; |
|---|
| 378 | } |
|---|
| 379 | /* }}} */ |
|---|
| 380 | static void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t period, zend_ulong *counters, zend_uint count TSRMLS_DC) /* {{{ */ |
|---|
| 381 | { |
|---|
| 382 | time_t n = XG(request_time) / period; |
|---|
| 383 | if (*curtime != n) { |
|---|
| 384 | zend_uint target_slot = n % count; |
|---|
| 385 | if (n - *curtime > period) { |
|---|
| 386 | memset(counters, 0, sizeof(counters[0]) * count); |
|---|
| 387 | } |
|---|
| 388 | else { |
|---|
| 389 | zend_uint slot; |
|---|
| 390 | for (slot = advance_wrapped(*curslot, count); |
|---|
| 391 | slot != target_slot; |
|---|
| 392 | slot = advance_wrapped(slot, count)) { |
|---|
| 393 | counters[slot] = 0; |
|---|
| 394 | } |
|---|
| 395 | counters[target_slot] = 0; |
|---|
| 396 | } |
|---|
| 397 | *curtime = n; |
|---|
| 398 | *curslot = target_slot; |
|---|
| 399 | } |
|---|
| 400 | counters[*curslot] ++; |
|---|
| 401 | } |
|---|
| 402 | /* }}} */ |
|---|
| 403 | static void xc_cache_hit_unlocked(xc_cache_t *cache TSRMLS_DC) /* {{{ */ |
|---|
| 404 | { |
|---|
| 405 | cache->hits ++; |
|---|
| 406 | |
|---|
| 407 | xc_counters_inc(&cache->hits_by_hour_cur_time |
|---|
| 408 | , &cache->hits_by_hour_cur_slot, 60 * 60 |
|---|
| 409 | , cache->hits_by_hour |
|---|
| 410 | , sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]) |
|---|
| 411 | TSRMLS_CC); |
|---|
| 412 | |
|---|
| 413 | xc_counters_inc(&cache->hits_by_second_cur_time |
|---|
| 414 | , &cache->hits_by_second_cur_slot |
|---|
| 415 | , 1 |
|---|
| 416 | , cache->hits_by_second |
|---|
| 417 | , sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]) |
|---|
| 418 | TSRMLS_CC); |
|---|
| 419 | } |
|---|
| 420 | /* }}} */ |
|---|
| 421 | |
|---|
| 422 | /* helper function that loop through each entry */ |
|---|
| 423 | #define XC_ENTRY_APPLY_FUNC(name) zend_bool name(xc_entry_t *entry TSRMLS_DC) |
|---|
| 424 | typedef XC_ENTRY_APPLY_FUNC((*cache_apply_unlocked_func_t)); |
|---|
| 425 | static void xc_entry_apply_unlocked(xc_entry_type_t type, xc_cache_t *cache, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 426 | { |
|---|
| 427 | xc_entry_t *p, **pp; |
|---|
| 428 | int i, c; |
|---|
| 429 | |
|---|
| 430 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 431 | pp = &(cache->entries[i]); |
|---|
| 432 | for (p = *pp; p; p = *pp) { |
|---|
| 433 | if (apply_func(p TSRMLS_CC)) { |
|---|
| 434 | /* unlink */ |
|---|
| 435 | *pp = p->next; |
|---|
| 436 | xc_entry_free_unlocked(type, cache, p TSRMLS_CC); |
|---|
| 437 | } |
|---|
| 438 | else { |
|---|
| 439 | pp = &(p->next); |
|---|
| 440 | } |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | } |
|---|
| 444 | /* }}} */ |
|---|
| 445 | |
|---|
| 446 | #define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC) |
|---|
| 447 | /* call graph: |
|---|
| 448 | * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_php_entry_unlocked |
|---|
| 449 | * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_var_entry_unlocked |
|---|
| 450 | */ |
|---|
| 451 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_unlocked) /* {{{ */ |
|---|
| 452 | { |
|---|
| 453 | TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl); |
|---|
| 454 | if (XG(request_time) > entry->atime + (time_t) xc_php_ttl) { |
|---|
| 455 | return 1; |
|---|
| 456 | } |
|---|
| 457 | return 0; |
|---|
| 458 | } |
|---|
| 459 | /* }}} */ |
|---|
| 460 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_unlocked) /* {{{ */ |
|---|
| 461 | { |
|---|
| 462 | if (VAR_ENTRY_EXPIRED(entry)) { |
|---|
| 463 | return 1; |
|---|
| 464 | } |
|---|
| 465 | return 0; |
|---|
| 466 | } |
|---|
| 467 | /* }}} */ |
|---|
| 468 | static void xc_gc_expires_one(xc_entry_type_t type, xc_cache_t *cache, zend_ulong gc_interval, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 469 | { |
|---|
| 470 | TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval); |
|---|
| 471 | if (XG(request_time) >= cache->last_gc_expires + (time_t) gc_interval) { |
|---|
| 472 | ENTER_LOCK(cache) { |
|---|
| 473 | if (XG(request_time) >= cache->last_gc_expires + (time_t) gc_interval) { |
|---|
| 474 | cache->last_gc_expires = XG(request_time); |
|---|
| 475 | xc_entry_apply_unlocked(type, cache, apply_func TSRMLS_CC); |
|---|
| 476 | } |
|---|
| 477 | } LEAVE_LOCK(cache); |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | /* }}} */ |
|---|
| 481 | static void xc_gc_expires_php(TSRMLS_D) /* {{{ */ |
|---|
| 482 | { |
|---|
| 483 | int i, c; |
|---|
| 484 | |
|---|
| 485 | if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) { |
|---|
| 486 | return; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 490 | xc_gc_expires_one(XC_TYPE_PHP, xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_unlocked TSRMLS_CC); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | /* }}} */ |
|---|
| 494 | static void xc_gc_expires_var(TSRMLS_D) /* {{{ */ |
|---|
| 495 | { |
|---|
| 496 | int i, c; |
|---|
| 497 | |
|---|
| 498 | if (!xc_var_gc_interval || !xc_var_caches) { |
|---|
| 499 | return; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 503 | xc_gc_expires_one(XC_TYPE_VAR, xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_unlocked TSRMLS_CC); |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | /* }}} */ |
|---|
| 507 | |
|---|
| 508 | static XC_CACHE_APPLY_FUNC(xc_gc_delete_unlocked) /* {{{ */ |
|---|
| 509 | { |
|---|
| 510 | xc_entry_t *p, **pp; |
|---|
| 511 | |
|---|
| 512 | pp = &cache->deletes; |
|---|
| 513 | for (p = *pp; p; p = *pp) { |
|---|
| 514 | xc_entry_php_t *entry = (xc_entry_php_t *) p; |
|---|
| 515 | if (XG(request_time) - p->dtime > 3600) { |
|---|
| 516 | entry->refcount = 0; |
|---|
| 517 | /* issue warning here */ |
|---|
| 518 | } |
|---|
| 519 | if (entry->refcount == 0) { |
|---|
| 520 | /* unlink */ |
|---|
| 521 | *pp = p->next; |
|---|
| 522 | cache->deletes_count --; |
|---|
| 523 | xc_entry_free_real_unlocked(XC_TYPE_PHP, cache, p); |
|---|
| 524 | } |
|---|
| 525 | else { |
|---|
| 526 | pp = &(p->next); |
|---|
| 527 | } |
|---|
| 528 | } |
|---|
| 529 | } |
|---|
| 530 | /* }}} */ |
|---|
| 531 | static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */ |
|---|
| 532 | { |
|---|
| 533 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 534 | ENTER_LOCK(cache) { |
|---|
| 535 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 536 | cache->last_gc_deletes = XG(request_time); |
|---|
| 537 | xc_gc_delete_unlocked(cache TSRMLS_CC); |
|---|
| 538 | } |
|---|
| 539 | } LEAVE_LOCK(cache); |
|---|
| 540 | } |
|---|
| 541 | } |
|---|
| 542 | /* }}} */ |
|---|
| 543 | static void xc_gc_deletes(TSRMLS_D) /* {{{ */ |
|---|
| 544 | { |
|---|
| 545 | int i, c; |
|---|
| 546 | |
|---|
| 547 | if (xc_php_caches) { |
|---|
| 548 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 549 | xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC); |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | if (xc_var_caches) { |
|---|
| 554 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 555 | xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC); |
|---|
| 556 | } |
|---|
| 557 | } |
|---|
| 558 | } |
|---|
| 559 | /* }}} */ |
|---|
| 560 | |
|---|
| 561 | /* helper functions for user functions */ |
|---|
| 562 | static void xc_fillinfo_unlocked(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 563 | { |
|---|
| 564 | zval *blocks, *hits; |
|---|
| 565 | size_t i; |
|---|
| 566 | const xc_block_t *b; |
|---|
| 567 | #ifndef NDEBUG |
|---|
| 568 | xc_memsize_t avail = 0; |
|---|
| 569 | #endif |
|---|
| 570 | xc_mem_t *mem = cache->mem; |
|---|
| 571 | const xc_mem_handlers_t *handlers = mem->handlers; |
|---|
| 572 | zend_ulong interval; |
|---|
| 573 | if (cachetype == XC_TYPE_PHP) { |
|---|
| 574 | interval = xc_php_ttl ? xc_php_gc_interval : 0; |
|---|
| 575 | } |
|---|
| 576 | else { |
|---|
| 577 | interval = xc_var_gc_interval; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
|---|
| 581 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling); |
|---|
| 582 | add_assoc_long_ex(return_value, ZEND_STRS("updates"), cache->updates); |
|---|
| 583 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->updates); /* deprecated */ |
|---|
| 584 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits); |
|---|
| 585 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs); |
|---|
| 586 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms); |
|---|
| 587 | add_assoc_long_ex(return_value, ZEND_STRS("errors"), cache->errors); |
|---|
| 588 | |
|---|
| 589 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count); |
|---|
| 590 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count); |
|---|
| 591 | if (interval) { |
|---|
| 592 | time_t gc = (cache->last_gc_expires + interval) - XG(request_time); |
|---|
| 593 | add_assoc_long_ex(return_value, ZEND_STRS("gc"), gc > 0 ? gc : 0); |
|---|
| 594 | } |
|---|
| 595 | else { |
|---|
| 596 | add_assoc_null_ex(return_value, ZEND_STRS("gc")); |
|---|
| 597 | } |
|---|
| 598 | MAKE_STD_ZVAL(hits); |
|---|
| 599 | array_init(hits); |
|---|
| 600 | for (i = 0; i < sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]); i ++) { |
|---|
| 601 | add_next_index_long(hits, (long) cache->hits_by_hour[i]); |
|---|
| 602 | } |
|---|
| 603 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_hour"), hits); |
|---|
| 604 | |
|---|
| 605 | MAKE_STD_ZVAL(hits); |
|---|
| 606 | array_init(hits); |
|---|
| 607 | for (i = 0; i < sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]); i ++) { |
|---|
| 608 | add_next_index_long(hits, (long) cache->hits_by_second[i]); |
|---|
| 609 | } |
|---|
| 610 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_second"), hits); |
|---|
| 611 | |
|---|
| 612 | MAKE_STD_ZVAL(blocks); |
|---|
| 613 | array_init(blocks); |
|---|
| 614 | |
|---|
| 615 | add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem)); |
|---|
| 616 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem)); |
|---|
| 617 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
|---|
| 618 | |
|---|
| 619 | for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) { |
|---|
| 620 | zval *bi; |
|---|
| 621 | |
|---|
| 622 | MAKE_STD_ZVAL(bi); |
|---|
| 623 | array_init(bi); |
|---|
| 624 | |
|---|
| 625 | add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b)); |
|---|
| 626 | add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b)); |
|---|
| 627 | add_next_index_zval(blocks, bi); |
|---|
| 628 | #ifndef NDEBUG |
|---|
| 629 | avail += handlers->block_size(b); |
|---|
| 630 | #endif |
|---|
| 631 | } |
|---|
| 632 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
|---|
| 633 | #ifndef NDEBUG |
|---|
| 634 | assert(avail == handlers->avail(mem)); |
|---|
| 635 | #endif |
|---|
| 636 | } |
|---|
| 637 | /* }}} */ |
|---|
| 638 | static void xc_fillentry_unlocked(xc_entry_type_t type, const xc_entry_t *entry, xc_hash_value_t entryslotid, int del, zval *list TSRMLS_DC) /* {{{ */ |
|---|
| 639 | { |
|---|
| 640 | zval* ei; |
|---|
| 641 | const xc_entry_data_php_t *php; |
|---|
| 642 | |
|---|
| 643 | ALLOC_INIT_ZVAL(ei); |
|---|
| 644 | array_init(ei); |
|---|
| 645 | |
|---|
| 646 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
|---|
| 647 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
|---|
| 648 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
|---|
| 649 | add_assoc_long_ex(ei, ZEND_STRS("hvalue"), entryslotid); |
|---|
| 650 | if (del) { |
|---|
| 651 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
|---|
| 652 | } |
|---|
| 653 | #ifdef IS_UNICODE |
|---|
| 654 | do { |
|---|
| 655 | zval *zv; |
|---|
| 656 | ALLOC_INIT_ZVAL(zv); |
|---|
| 657 | switch (entry->name_type) { |
|---|
| 658 | case IS_UNICODE: |
|---|
| 659 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
|---|
| 660 | break; |
|---|
| 661 | case IS_STRING: |
|---|
| 662 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
|---|
| 663 | break; |
|---|
| 664 | default: |
|---|
| 665 | assert(0); |
|---|
| 666 | } |
|---|
| 667 | zv->type = entry->name_type; |
|---|
| 668 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
|---|
| 669 | } while (0); |
|---|
| 670 | #else |
|---|
| 671 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1); |
|---|
| 672 | #endif |
|---|
| 673 | switch (type) { |
|---|
| 674 | case XC_TYPE_PHP: { |
|---|
| 675 | xc_entry_php_t *entry_php = (xc_entry_php_t *) entry; |
|---|
| 676 | php = entry_php->php; |
|---|
| 677 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size + php->size); |
|---|
| 678 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry_php->refcount); |
|---|
| 679 | add_assoc_long_ex(ei, ZEND_STRS("phprefcount"), php->refcount); |
|---|
| 680 | add_assoc_long_ex(ei, ZEND_STRS("file_mtime"), entry_php->file_mtime); |
|---|
| 681 | add_assoc_long_ex(ei, ZEND_STRS("file_size"), entry_php->file_size); |
|---|
| 682 | add_assoc_long_ex(ei, ZEND_STRS("file_device"), entry_php->file_device); |
|---|
| 683 | add_assoc_long_ex(ei, ZEND_STRS("file_inode"), entry_php->file_inode); |
|---|
| 684 | |
|---|
| 685 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 686 | add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt); |
|---|
| 687 | #endif |
|---|
| 688 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
|---|
| 689 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
|---|
| 690 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 691 | add_assoc_long_ex(ei, ZEND_STRS("autoglobal_cnt"),php->autoglobal_cnt); |
|---|
| 692 | #endif |
|---|
| 693 | break; |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | case XC_TYPE_VAR: |
|---|
| 697 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), 0); /* for BC only */ |
|---|
| 698 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
|---|
| 699 | break; |
|---|
| 700 | |
|---|
| 701 | default: |
|---|
| 702 | assert(0); |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | add_next_index_zval(list, ei); |
|---|
| 706 | } |
|---|
| 707 | /* }}} */ |
|---|
| 708 | static void xc_filllist_unlocked(xc_entry_type_t type, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 709 | { |
|---|
| 710 | zval* list; |
|---|
| 711 | int i, c; |
|---|
| 712 | xc_entry_t *e; |
|---|
| 713 | |
|---|
| 714 | ALLOC_INIT_ZVAL(list); |
|---|
| 715 | array_init(list); |
|---|
| 716 | |
|---|
| 717 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 718 | for (e = cache->entries[i]; e; e = e->next) { |
|---|
| 719 | xc_fillentry_unlocked(type, e, i, 0, list TSRMLS_CC); |
|---|
| 720 | } |
|---|
| 721 | } |
|---|
| 722 | add_assoc_zval(return_value, "cache_list", list); |
|---|
| 723 | |
|---|
| 724 | ALLOC_INIT_ZVAL(list); |
|---|
| 725 | array_init(list); |
|---|
| 726 | for (e = cache->deletes; e; e = e->next) { |
|---|
| 727 | xc_fillentry_unlocked(XC_TYPE_PHP, e, 0, 1, list TSRMLS_CC); |
|---|
| 728 | } |
|---|
| 729 | add_assoc_zval(return_value, "deleted_list", list); |
|---|
| 730 | } |
|---|
| 731 | /* }}} */ |
|---|
| 732 | |
|---|
| 733 | static zend_op_array *xc_entry_install(xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
|---|
| 734 | { |
|---|
| 735 | zend_uint i; |
|---|
| 736 | xc_entry_data_php_t *p = entry_php->php; |
|---|
| 737 | zend_op_array *old_active_op_array = CG(active_op_array); |
|---|
| 738 | #ifndef ZEND_ENGINE_2 |
|---|
| 739 | ALLOCA_FLAG(use_heap) |
|---|
| 740 | /* new ptr which is stored inside CG(class_table) */ |
|---|
| 741 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)my_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap); |
|---|
| 742 | #endif |
|---|
| 743 | |
|---|
| 744 | CG(active_op_array) = p->op_array; |
|---|
| 745 | |
|---|
| 746 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 747 | /* install constant */ |
|---|
| 748 | for (i = 0; i < p->constinfo_cnt; i ++) { |
|---|
| 749 | xc_constinfo_t *ci = &p->constinfos[i]; |
|---|
| 750 | xc_install_constant(entry_php->entry.name.str.val, &ci->constant, |
|---|
| 751 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
|---|
| 752 | } |
|---|
| 753 | #endif |
|---|
| 754 | |
|---|
| 755 | /* install function */ |
|---|
| 756 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
|---|
| 757 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
|---|
| 758 | xc_install_function(entry_php->entry.name.str.val, &fi->func, |
|---|
| 759 | UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC); |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | /* install class */ |
|---|
| 763 | for (i = 0; i < p->classinfo_cnt; i ++) { |
|---|
| 764 | xc_classinfo_t *ci = &p->classinfos[i]; |
|---|
| 765 | #ifndef ZEND_ENGINE_2 |
|---|
| 766 | zend_class_entry *ce = CestToCePtr(ci->cest); |
|---|
| 767 | /* fix pointer to the be which inside class_table */ |
|---|
| 768 | if (ce->parent) { |
|---|
| 769 | zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1; |
|---|
| 770 | assert(class_idx < i); |
|---|
| 771 | ci->cest.parent = new_cest_ptrs[class_idx]; |
|---|
| 772 | } |
|---|
| 773 | new_cest_ptrs[i] = |
|---|
| 774 | #endif |
|---|
| 775 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 776 | xc_install_class(entry_php->entry.name.str.val, &ci->cest, -1, |
|---|
| 777 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
|---|
| 778 | #else |
|---|
| 779 | xc_install_class(entry_php->entry.name.str.val, &ci->cest, ci->oplineno, |
|---|
| 780 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
|---|
| 781 | #endif |
|---|
| 782 | } |
|---|
| 783 | |
|---|
| 784 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 785 | /* trigger auto_globals jit */ |
|---|
| 786 | for (i = 0; i < p->autoglobal_cnt; i ++) { |
|---|
| 787 | xc_autoglobal_t *aginfo = &p->autoglobals[i]; |
|---|
| 788 | zend_u_is_auto_global(aginfo->type, aginfo->key, aginfo->key_len TSRMLS_CC); |
|---|
| 789 | } |
|---|
| 790 | #endif |
|---|
| 791 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 792 | /* restore trigger errors */ |
|---|
| 793 | for (i = 0; i < p->compilererror_cnt; i ++) { |
|---|
| 794 | xc_compilererror_t *error = &p->compilererrors[i]; |
|---|
| 795 | CG(zend_lineno) = error->lineno; |
|---|
| 796 | zend_error(error->type, "%s", error->error); |
|---|
| 797 | } |
|---|
| 798 | CG(zend_lineno) = 0; |
|---|
| 799 | #endif |
|---|
| 800 | |
|---|
| 801 | i = 1; |
|---|
| 802 | #ifndef ZEND_ENGINE_2_2 |
|---|
| 803 | zend_hash_add(&EG(included_files), entry_php->entry.name.str.val, entry_php->entry.name.str.len+1, (void *)&i, sizeof(int), NULL); |
|---|
| 804 | #endif |
|---|
| 805 | |
|---|
| 806 | #ifndef ZEND_ENGINE_2 |
|---|
| 807 | my_free_alloca(new_cest_ptrs, use_heap); |
|---|
| 808 | #endif |
|---|
| 809 | CG(active_op_array) = old_active_op_array; |
|---|
| 810 | return p->op_array; |
|---|
| 811 | } |
|---|
| 812 | /* }}} */ |
|---|
| 813 | |
|---|
| 814 | static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */ |
|---|
| 815 | { |
|---|
| 816 | int i; |
|---|
| 817 | xc_stack_t *s; |
|---|
| 818 | xc_cache_t *cache; |
|---|
| 819 | xc_entry_php_t *entry_php; |
|---|
| 820 | |
|---|
| 821 | for (i = 0; i < cachecount; i ++) { |
|---|
| 822 | s = &holds[i]; |
|---|
| 823 | TRACE("holded %d items", xc_stack_count(s)); |
|---|
| 824 | if (xc_stack_count(s)) { |
|---|
| 825 | cache = caches[i]; |
|---|
| 826 | ENTER_LOCK(cache) { |
|---|
| 827 | while (xc_stack_count(s)) { |
|---|
| 828 | entry_php = (xc_entry_php_t *) xc_stack_pop(s); |
|---|
| 829 | TRACE("unhold %d:%s", entry_php->file_inode, entry_php->entry.name.str.val); |
|---|
| 830 | assert(entry_php->refcount > 0); |
|---|
| 831 | --entry_php->refcount; |
|---|
| 832 | } |
|---|
| 833 | } LEAVE_LOCK(cache); |
|---|
| 834 | } |
|---|
| 835 | } |
|---|
| 836 | } |
|---|
| 837 | /* }}} */ |
|---|
| 838 | static void xc_entry_unholds(TSRMLS_D) /* {{{ */ |
|---|
| 839 | { |
|---|
| 840 | if (xc_php_caches) { |
|---|
| 841 | xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | if (xc_var_caches) { |
|---|
| 845 | xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
|---|
| 846 | } |
|---|
| 847 | } |
|---|
| 848 | /* }}} */ |
|---|
| 849 | |
|---|
| 850 | #define HASH(i) (i) |
|---|
| 851 | #define HASH_ZSTR_L(t, s, l) HASH(zend_u_inline_hash_func((t), (s), ((l) + 1) * sizeof(UChar))) |
|---|
| 852 | #define HASH_STR_S(s, l) HASH(zend_inline_hash_func((char *) (s), (l))) |
|---|
| 853 | #define HASH_STR_L(s, l) HASH_STR_S((s), (l) + 1) |
|---|
| 854 | #define HASH_STR(s) HASH_STR_L((s), strlen((s)) + 1) |
|---|
| 855 | #define HASH_NUM(n) HASH(n) |
|---|
| 856 | static inline xc_hash_value_t xc_hash_fold(xc_hash_value_t hvalue, const xc_hash_t *hasher) /* {{{ fold hash bits as needed */ |
|---|
| 857 | { |
|---|
| 858 | xc_hash_value_t folded = 0; |
|---|
| 859 | while (hvalue) { |
|---|
| 860 | folded ^= (hvalue & hasher->mask); |
|---|
| 861 | hvalue >>= hasher->bits; |
|---|
| 862 | } |
|---|
| 863 | return folded; |
|---|
| 864 | } |
|---|
| 865 | /* }}} */ |
|---|
| 866 | static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
|---|
| 867 | { |
|---|
| 868 | return UNISW(NOTHING, UG(unicode) ? HASH_ZSTR_L(entry->name_type, entry->name.uni.val, entry->name.uni.len) :) |
|---|
| 869 | HASH_STR_L(entry->name.str.val, entry->name.str.len); |
|---|
| 870 | } |
|---|
| 871 | /* }}} */ |
|---|
| 872 | #define xc_entry_hash_var xc_entry_hash_name |
|---|
| 873 | static void xc_entry_free_key_php(xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
|---|
| 874 | { |
|---|
| 875 | #define X_FREE(var) do {\ |
|---|
| 876 | if (entry_php->var) { \ |
|---|
| 877 | efree(entry_php->var); \ |
|---|
| 878 | } \ |
|---|
| 879 | } while (0) |
|---|
| 880 | X_FREE(dirpath); |
|---|
| 881 | #ifdef IS_UNICODE |
|---|
| 882 | X_FREE(ufilepath); |
|---|
| 883 | X_FREE(udirpath); |
|---|
| 884 | #endif |
|---|
| 885 | |
|---|
| 886 | #undef X_FREE |
|---|
| 887 | } |
|---|
| 888 | /* }}} */ |
|---|
| 889 | static char *xc_expand_url(const char *filepath, char *real_path TSRMLS_DC) /* {{{ */ |
|---|
| 890 | { |
|---|
| 891 | if (strstr(filepath, "://") != NULL) { |
|---|
| 892 | size_t filepath_len = strlen(filepath); |
|---|
| 893 | size_t copy_len = filepath_len > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : filepath_len; |
|---|
| 894 | memcpy(real_path, filepath, filepath_len); |
|---|
| 895 | real_path[copy_len] = '\0'; |
|---|
| 896 | return real_path; |
|---|
| 897 | } |
|---|
| 898 | return expand_filepath(filepath, real_path TSRMLS_CC); |
|---|
| 899 | } |
|---|
| 900 | /* }}} */ |
|---|
| 901 | |
|---|
| 902 | #define XC_RESOLVE_PATH_CHECKER(name) zend_bool name(const char *filepath, size_t filepath_len, void *data TSRMLS_DC) |
|---|
| 903 | typedef XC_RESOLVE_PATH_CHECKER((*xc_resolve_path_checker_func_t)); |
|---|
| 904 | static zend_bool xc_resolve_path(const char *filepath, char *path_buffer, xc_resolve_path_checker_func_t checker_func, void *data TSRMLS_DC) /* {{{ */ |
|---|
| 905 | { |
|---|
| 906 | char *paths, *path; |
|---|
| 907 | char *tokbuf; |
|---|
| 908 | size_t path_buffer_len; |
|---|
| 909 | int size; |
|---|
| 910 | char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' }; |
|---|
| 911 | int ret; |
|---|
| 912 | ALLOCA_FLAG(use_heap) |
|---|
| 913 | |
|---|
| 914 | #if 0 |
|---|
| 915 | if ((*filepath == '.' && |
|---|
| 916 | (IS_SLASH(filepath[1]) || |
|---|
| 917 | ((filepath[1] == '.') && IS_SLASH(filepath[2])))) || |
|---|
| 918 | IS_ABSOLUTE_PATH(filepath, strlen(filepath)) || |
|---|
| 919 | !path || |
|---|
| 920 | !*path) { |
|---|
| 921 | |
|---|
| 922 | if (checker_func(path_buffer, path_buffer_len, data TSRMLS_CC)) { |
|---|
| 923 | ret = 1; |
|---|
| 924 | } |
|---|
| 925 | else { |
|---|
| 926 | ret = FAILURE; |
|---|
| 927 | } |
|---|
| 928 | goto finish; |
|---|
| 929 | } |
|---|
| 930 | #endif |
|---|
| 931 | |
|---|
| 932 | size = strlen(PG(include_path)) + 1; |
|---|
| 933 | paths = (char *)my_do_alloca(size, use_heap); |
|---|
| 934 | memcpy(paths, PG(include_path), size); |
|---|
| 935 | |
|---|
| 936 | for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) { |
|---|
| 937 | path_buffer_len = snprintf(path_buffer, MAXPATHLEN, "%s/%s", path, filepath); |
|---|
| 938 | if (path_buffer_len < MAXPATHLEN - 1) { |
|---|
| 939 | if (checker_func(path_buffer, path_buffer_len, data TSRMLS_CC)) { |
|---|
| 940 | ret = 1; |
|---|
| 941 | goto finish; |
|---|
| 942 | } |
|---|
| 943 | } |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | /* fall back to current directory */ |
|---|
| 947 | if (zend_is_executing(TSRMLS_C)) { |
|---|
| 948 | const char *executed_filename = zend_get_executed_filename(TSRMLS_C); |
|---|
| 949 | if (executed_filename && executed_filename[0] && executed_filename[0] != '[') { |
|---|
| 950 | size_t filename_len = strlen(filepath); |
|---|
| 951 | size_t dirname_len; |
|---|
| 952 | |
|---|
| 953 | for (dirname_len = strlen(executed_filename) - 1; dirname_len > 0; --dirname_len) { |
|---|
| 954 | if (IS_SLASH(executed_filename[dirname_len])) { |
|---|
| 955 | if (dirname_len + filename_len < MAXPATHLEN - 1) { |
|---|
| 956 | ++dirname_len; /* include tailing slash */ |
|---|
| 957 | memcpy(path_buffer, executed_filename, dirname_len); |
|---|
| 958 | memcpy(path_buffer + dirname_len, filepath, filename_len); |
|---|
| 959 | path_buffer_len = dirname_len + filename_len; |
|---|
| 960 | path_buffer[path_buffer_len] = '\0'; |
|---|
| 961 | if (checker_func(path_buffer, path_buffer_len, data TSRMLS_CC)) { |
|---|
| 962 | ret = 1; |
|---|
| 963 | goto finish; |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | break; |
|---|
| 967 | } |
|---|
| 968 | } |
|---|
| 969 | } |
|---|
| 970 | } |
|---|
| 971 | |
|---|
| 972 | ret = 0; |
|---|
| 973 | |
|---|
| 974 | finish: |
|---|
| 975 | my_free_alloca(paths, use_heap); |
|---|
| 976 | |
|---|
| 977 | return ret; |
|---|
| 978 | } |
|---|
| 979 | /* }}} */ |
|---|
| 980 | #ifndef ZEND_ENGINE_2_3 |
|---|
| 981 | static XC_RESOLVE_PATH_CHECKER(xc_stat_file) /* {{{ */ |
|---|
| 982 | { |
|---|
| 983 | return VCWD_STAT(filepath, (struct stat *) data) == 0 ? 1 : 0; |
|---|
| 984 | } |
|---|
| 985 | /* }}} */ |
|---|
| 986 | static int xc_resolve_path_stat(const char *filepath, char *path_buffer, struct stat *pbuf TSRMLS_DC) /* {{{ */ |
|---|
| 987 | { |
|---|
| 988 | return xc_resolve_path(filepath, path_buffer, xc_stat_file, (void *) pbuf TSRMLS_CC) |
|---|
| 989 | ? SUCCESS |
|---|
| 990 | : FAILURE; |
|---|
| 991 | } |
|---|
| 992 | /* }}} */ |
|---|
| 993 | #endif |
|---|
| 994 | typedef struct xc_compiler_t { /* {{{ */ |
|---|
| 995 | /* XCache cached compile state */ |
|---|
| 996 | const char *filename; |
|---|
| 997 | size_t filename_len; |
|---|
| 998 | const char *opened_path; |
|---|
| 999 | char opened_path_buffer[MAXPATHLEN]; |
|---|
| 1000 | |
|---|
| 1001 | xc_entry_hash_t entry_hash; |
|---|
| 1002 | xc_entry_php_t new_entry; |
|---|
| 1003 | xc_entry_data_php_t new_php; |
|---|
| 1004 | } xc_compiler_t; |
|---|
| 1005 | /* }}} */ |
|---|
| 1006 | typedef struct xc_entry_resolve_path_data_t { /* {{{ */ |
|---|
| 1007 | xc_compiler_t *compiler; |
|---|
| 1008 | xc_entry_php_t **stored_entry; |
|---|
| 1009 | } xc_entry_resolve_path_data_t; |
|---|
| 1010 | /* }}} */ |
|---|
| 1011 | static XC_RESOLVE_PATH_CHECKER(xc_entry_resolve_path_func_unlocked) /* {{{ */ |
|---|
| 1012 | { |
|---|
| 1013 | xc_entry_resolve_path_data_t *entry_resolve_path_data = (xc_entry_resolve_path_data_t *) data; |
|---|
| 1014 | xc_compiler_t *compiler = entry_resolve_path_data->compiler; |
|---|
| 1015 | |
|---|
| 1016 | compiler->new_entry.entry.name.str.val = xc_expand_url(filepath, compiler->opened_path_buffer TSRMLS_CC); |
|---|
| 1017 | compiler->new_entry.entry.name.str.len = strlen(compiler->new_entry.entry.name.str.val); |
|---|
| 1018 | |
|---|
| 1019 | *entry_resolve_path_data->stored_entry = (xc_entry_php_t *) xc_entry_find_unlocked( |
|---|
| 1020 | XC_TYPE_PHP |
|---|
| 1021 | , xc_php_caches[compiler->entry_hash.cacheid] |
|---|
| 1022 | , compiler->entry_hash.entryslotid |
|---|
| 1023 | , (xc_entry_t *) &compiler->new_entry |
|---|
| 1024 | TSRMLS_CC); |
|---|
| 1025 | |
|---|
| 1026 | return *entry_resolve_path_data->stored_entry ? 1 : 0; |
|---|
| 1027 | } |
|---|
| 1028 | /* }}} */ |
|---|
| 1029 | static int xc_entry_resolve_path_unlocked(xc_compiler_t *compiler, const char *filepath, xc_entry_php_t **stored_entry TSRMLS_DC) /* {{{ */ |
|---|
| 1030 | { |
|---|
| 1031 | char path_buffer[MAXPATHLEN]; |
|---|
| 1032 | xc_entry_resolve_path_data_t entry_resolve_path_data; |
|---|
| 1033 | entry_resolve_path_data.compiler = compiler; |
|---|
| 1034 | entry_resolve_path_data.stored_entry = stored_entry; |
|---|
| 1035 | |
|---|
| 1036 | return xc_resolve_path(filepath, path_buffer, xc_entry_resolve_path_func_unlocked, (void *) &entry_resolve_path_data TSRMLS_CC) |
|---|
| 1037 | ? SUCCESS |
|---|
| 1038 | : FAILURE; |
|---|
| 1039 | } |
|---|
| 1040 | /* }}} */ |
|---|
| 1041 | static int xc_entry_php_quick_resolve_opened_path(xc_compiler_t *compiler, struct stat *statbuf TSRMLS_DC) /* {{{ */ |
|---|
| 1042 | { |
|---|
| 1043 | if (strcmp(SG(request_info).path_translated, compiler->filename) == 0) { |
|---|
| 1044 | /* sapi has already done this stat() for us */ |
|---|
| 1045 | if (statbuf) { |
|---|
| 1046 | struct stat *sapi_stat = sapi_get_stat(TSRMLS_C); |
|---|
| 1047 | if (!sapi_stat) { |
|---|
| 1048 | goto giveupsapistat; |
|---|
| 1049 | } |
|---|
| 1050 | *statbuf = *sapi_stat; |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
|---|
| 1054 | return SUCCESS; |
|---|
| 1055 | } |
|---|
| 1056 | giveupsapistat: |
|---|
| 1057 | |
|---|
| 1058 | /* absolute path */ |
|---|
| 1059 | if (IS_ABSOLUTE_PATH(compiler->filename, strlen(compiler->filename))) { |
|---|
| 1060 | if (statbuf && VCWD_STAT(compiler->filename, statbuf) != 0) { |
|---|
| 1061 | return FAILURE; |
|---|
| 1062 | } |
|---|
| 1063 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
|---|
| 1064 | return SUCCESS; |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | /* relative path */ |
|---|
| 1068 | if (*compiler->filename == '.' && (IS_SLASH(compiler->filename[1]) || compiler->filename[1] == '.')) { |
|---|
| 1069 | const char *ptr = compiler->filename + 1; |
|---|
| 1070 | if (*ptr == '.') { |
|---|
| 1071 | while (*(++ptr) == '.'); |
|---|
| 1072 | if (!IS_SLASH(*ptr)) { |
|---|
| 1073 | return FAILURE; |
|---|
| 1074 | } |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| 1077 | if (statbuf && VCWD_STAT(compiler->filename, statbuf) != 0) { |
|---|
| 1078 | return FAILURE; |
|---|
| 1079 | } |
|---|
| 1080 | |
|---|
| 1081 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
|---|
| 1082 | return SUCCESS; |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | return FAILURE; |
|---|
| 1086 | } |
|---|
| 1087 | /* }}} */ |
|---|
| 1088 | static int xc_entry_php_resolve_opened_path(xc_compiler_t *compiler, struct stat *statbuf TSRMLS_DC) /* {{{ */ |
|---|
| 1089 | { |
|---|
| 1090 | if (xc_entry_php_quick_resolve_opened_path(compiler, statbuf TSRMLS_CC) == SUCCESS) { |
|---|
| 1091 | /* opened_path resolved */ |
|---|
| 1092 | return SUCCESS; |
|---|
| 1093 | } |
|---|
| 1094 | /* fall back to real stat call */ |
|---|
| 1095 | else { |
|---|
| 1096 | #ifdef ZEND_ENGINE_2_3 |
|---|
| 1097 | char *opened_path = php_resolve_path(compiler->filename, compiler->filename_len, PG(include_path) TSRMLS_CC); |
|---|
| 1098 | if (opened_path) { |
|---|
| 1099 | strcpy(compiler->opened_path_buffer, opened_path); |
|---|
| 1100 | efree(opened_path); |
|---|
| 1101 | compiler->opened_path = compiler->opened_path_buffer; |
|---|
| 1102 | if (!statbuf || VCWD_STAT(compiler->opened_path, statbuf) == 0) { |
|---|
| 1103 | return SUCCESS; |
|---|
| 1104 | } |
|---|
| 1105 | } |
|---|
| 1106 | #else |
|---|
| 1107 | char path_buffer[MAXPATHLEN]; |
|---|
| 1108 | if (xc_resolve_path_stat(compiler->filename, path_buffer, statbuf TSRMLS_CC) == SUCCESS) { |
|---|
| 1109 | compiler->opened_path = xc_expand_url(path_buffer, compiler->opened_path_buffer TSRMLS_CC); |
|---|
| 1110 | return SUCCESS; |
|---|
| 1111 | } |
|---|
| 1112 | #endif |
|---|
| 1113 | } |
|---|
| 1114 | return FAILURE; |
|---|
| 1115 | } |
|---|
| 1116 | /* }}} */ |
|---|
| 1117 | static int xc_entry_php_init_key(xc_compiler_t *compiler TSRMLS_DC) /* {{{ */ |
|---|
| 1118 | { |
|---|
| 1119 | if (XG(stat)) { |
|---|
| 1120 | struct stat buf; |
|---|
| 1121 | time_t delta; |
|---|
| 1122 | |
|---|
| 1123 | if (compiler->opened_path) { |
|---|
| 1124 | if (VCWD_STAT(compiler->opened_path, &buf) != 0) { |
|---|
| 1125 | return FAILURE; |
|---|
| 1126 | } |
|---|
| 1127 | } |
|---|
| 1128 | else { |
|---|
| 1129 | if (xc_entry_php_resolve_opened_path(compiler, &buf TSRMLS_CC) != SUCCESS) { |
|---|
| 1130 | return FAILURE; |
|---|
| 1131 | } |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | delta = XG(request_time) - buf.st_mtime; |
|---|
| 1135 | if (abs(delta) < 2 && !xc_test) { |
|---|
| 1136 | return FAILURE; |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | compiler->new_entry.file_mtime = buf.st_mtime; |
|---|
| 1140 | compiler->new_entry.file_size = buf.st_size; |
|---|
| 1141 | compiler->new_entry.file_device = buf.st_dev; |
|---|
| 1142 | compiler->new_entry.file_inode = buf.st_ino; |
|---|
| 1143 | } |
|---|
| 1144 | else { |
|---|
| 1145 | xc_entry_php_quick_resolve_opened_path(compiler, NULL TSRMLS_CC); |
|---|
| 1146 | |
|---|
| 1147 | compiler->new_entry.file_mtime = 0; |
|---|
| 1148 | compiler->new_entry.file_size = 0; |
|---|
| 1149 | compiler->new_entry.file_device = 0; |
|---|
| 1150 | compiler->new_entry.file_inode = 0; |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | { |
|---|
| 1154 | xc_hash_value_t basename_hash_value; |
|---|
| 1155 | if (xc_php_hcache.size > 1 |
|---|
| 1156 | || !compiler->new_entry.file_inode) { |
|---|
| 1157 | const char *filename_end = compiler->filename + compiler->filename_len; |
|---|
| 1158 | const char *basename_begin = filename_end - 1; |
|---|
| 1159 | |
|---|
| 1160 | /* scan till out of basename part */ |
|---|
| 1161 | while (basename_begin >= compiler->filename && !IS_SLASH(*basename_begin)) { |
|---|
| 1162 | --basename_begin; |
|---|
| 1163 | } |
|---|
| 1164 | /* get back to basename_begin */ |
|---|
| 1165 | ++basename_begin; |
|---|
| 1166 | |
|---|
| 1167 | basename_hash_value = HASH_STR_L(basename_begin, filename_end - basename_begin); |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | compiler->entry_hash.cacheid = xc_php_hcache.size > 1 ? xc_hash_fold(basename_hash_value, &xc_php_hcache) : 0; |
|---|
| 1171 | compiler->entry_hash.entryslotid = xc_hash_fold( |
|---|
| 1172 | compiler->new_entry.file_inode |
|---|
| 1173 | ? (xc_hash_value_t) HASH(compiler->new_entry.file_device + compiler->new_entry.file_inode) |
|---|
| 1174 | : basename_hash_value |
|---|
| 1175 | , &xc_php_hentry); |
|---|
| 1176 | } |
|---|
| 1177 | |
|---|
| 1178 | compiler->new_entry.filepath = NULL; |
|---|
| 1179 | compiler->new_entry.dirpath = NULL; |
|---|
| 1180 | #ifdef IS_UNICODE |
|---|
| 1181 | compiler->new_entry.ufilepath = NULL; |
|---|
| 1182 | compiler->new_entry.udirpath = NULL; |
|---|
| 1183 | #endif |
|---|
| 1184 | |
|---|
| 1185 | return SUCCESS; |
|---|
| 1186 | } |
|---|
| 1187 | /* }}} */ |
|---|
| 1188 | static inline xc_hash_value_t xc_php_hash_md5(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 1189 | { |
|---|
| 1190 | return HASH_STR_S(php->md5.digest, sizeof(php->md5.digest)); |
|---|
| 1191 | } |
|---|
| 1192 | /* }}} */ |
|---|
| 1193 | static int xc_entry_data_php_init_md5(xc_cache_t *cache, xc_compiler_t *compiler TSRMLS_DC) /* {{{ */ |
|---|
| 1194 | { |
|---|
| 1195 | unsigned char buf[1024]; |
|---|
| 1196 | PHP_MD5_CTX context; |
|---|
| 1197 | int n; |
|---|
| 1198 | php_stream *stream; |
|---|
| 1199 | ulong old_rsid = EG(regular_list).nNextFreeElement; |
|---|
| 1200 | |
|---|
| 1201 | stream = php_stream_open_wrapper((char *) compiler->filename, "rb", USE_PATH | REPORT_ERRORS | ENFORCE_SAFE_MODE | STREAM_DISABLE_OPEN_BASEDIR, NULL); |
|---|
| 1202 | if (!stream) { |
|---|
| 1203 | return FAILURE; |
|---|
| 1204 | } |
|---|
| 1205 | |
|---|
| 1206 | PHP_MD5Init(&context); |
|---|
| 1207 | while ((n = php_stream_read(stream, (char *) buf, sizeof(buf))) > 0) { |
|---|
| 1208 | PHP_MD5Update(&context, buf, n); |
|---|
| 1209 | } |
|---|
| 1210 | PHP_MD5Final((unsigned char *) compiler->new_php.md5.digest, &context); |
|---|
| 1211 | |
|---|
| 1212 | php_stream_close(stream); |
|---|
| 1213 | if (EG(regular_list).nNextFreeElement == old_rsid + 1) { |
|---|
| 1214 | EG(regular_list).nNextFreeElement = old_rsid; |
|---|
| 1215 | } |
|---|
| 1216 | |
|---|
| 1217 | if (n < 0) { |
|---|
| 1218 | return FAILURE; |
|---|
| 1219 | } |
|---|
| 1220 | |
|---|
| 1221 | compiler->new_php.hvalue = (xc_php_hash_md5(&compiler->new_php TSRMLS_CC) & cache->hphp->mask); |
|---|
| 1222 | #ifdef XCACHE_DEBUG |
|---|
| 1223 | { |
|---|
| 1224 | char md5str[33]; |
|---|
| 1225 | make_digest(md5str, (unsigned char *) compiler->new_php.md5.digest); |
|---|
| 1226 | TRACE("md5 %s", md5str); |
|---|
| 1227 | } |
|---|
| 1228 | #endif |
|---|
| 1229 | |
|---|
| 1230 | return SUCCESS; |
|---|
| 1231 | } |
|---|
| 1232 | /* }}} */ |
|---|
| 1233 | static void xc_entry_php_init(xc_entry_php_t *entry_php, const char *filepath TSRMLS_DC) /* {{{*/ |
|---|
| 1234 | { |
|---|
| 1235 | entry_php->filepath = ZEND_24((char *), NOTHING) filepath; |
|---|
| 1236 | entry_php->filepath_len = strlen(entry_php->filepath); |
|---|
| 1237 | entry_php->dirpath = estrndup(entry_php->filepath, entry_php->filepath_len); |
|---|
| 1238 | entry_php->dirpath_len = zend_dirname(entry_php->dirpath, entry_php->filepath_len); |
|---|
| 1239 | #ifdef IS_UNICODE |
|---|
| 1240 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &entry_php->ufilepath, &entry_php->ufilepath_len, entry_php->filepath, entry_php->filepath_len TSRMLS_CC); |
|---|
| 1241 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &entry_php->udirpath, &entry_php->udirpath_len, entry_php->dirpath, entry_php->dirpath_len TSRMLS_CC); |
|---|
| 1242 | #endif |
|---|
| 1243 | } |
|---|
| 1244 | /* }}} */ |
|---|
| 1245 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 1246 | static void xc_cache_early_binding_class_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
|---|
| 1247 | { |
|---|
| 1248 | char *class_name; |
|---|
| 1249 | zend_uint i; |
|---|
| 1250 | int class_len; |
|---|
| 1251 | xc_cest_t cest; |
|---|
| 1252 | xc_entry_data_php_t *php = (xc_entry_data_php_t *) data; |
|---|
| 1253 | |
|---|
| 1254 | class_name = Z_OP_CONSTANT(opline->op1).value.str.val; |
|---|
| 1255 | class_len = Z_OP_CONSTANT(opline->op1).value.str.len; |
|---|
| 1256 | if (zend_hash_find(CG(class_table), class_name, class_len, (void **) &cest) == FAILURE) { |
|---|
| 1257 | assert(0); |
|---|
| 1258 | } |
|---|
| 1259 | TRACE("got ZEND_DECLARE_INHERITED_CLASS: %s", class_name + 1); |
|---|
| 1260 | /* let's see which class */ |
|---|
| 1261 | for (i = 0; i < php->classinfo_cnt; i ++) { |
|---|
| 1262 | if (memcmp(ZSTR_S(php->classinfos[i].key), class_name, class_len) == 0) { |
|---|
| 1263 | php->classinfos[i].oplineno = oplineno; |
|---|
| 1264 | php->have_early_binding = 1; |
|---|
| 1265 | break; |
|---|
| 1266 | } |
|---|
| 1267 | } |
|---|
| 1268 | |
|---|
| 1269 | if (i == php->classinfo_cnt) { |
|---|
| 1270 | assert(0); |
|---|
| 1271 | } |
|---|
| 1272 | } |
|---|
| 1273 | /* }}} */ |
|---|
| 1274 | #endif |
|---|
| 1275 | |
|---|
| 1276 | /* {{{ Constant Usage */ |
|---|
| 1277 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 1278 | # define xcache_literal_is_dir 1 |
|---|
| 1279 | # define xcache_literal_is_file 2 |
|---|
| 1280 | #else |
|---|
| 1281 | # define xcache_op1_is_file 1 |
|---|
| 1282 | # define xcache_op1_is_dir 2 |
|---|
| 1283 | # define xcache_op2_is_file 4 |
|---|
| 1284 | # define xcache_op2_is_dir 8 |
|---|
| 1285 | #endif |
|---|
| 1286 | typedef struct { |
|---|
| 1287 | zend_bool filepath_used; |
|---|
| 1288 | zend_bool dirpath_used; |
|---|
| 1289 | zend_bool ufilepath_used; |
|---|
| 1290 | zend_bool udirpath_used; |
|---|
| 1291 | } xc_const_usage_t; |
|---|
| 1292 | /* }}} */ |
|---|
| 1293 | static void xc_collect_op_array_info(xc_compiler_t *compiler, xc_const_usage_t *usage, xc_op_array_info_t *op_array_info, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 1294 | { |
|---|
| 1295 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 1296 | int literalindex; |
|---|
| 1297 | #else |
|---|
| 1298 | zend_uint oplinenum; |
|---|
| 1299 | #endif |
|---|
| 1300 | xc_vector_t details; |
|---|
| 1301 | |
|---|
| 1302 | xc_vector_init(xc_op_array_info_detail_t, &details); |
|---|
| 1303 | |
|---|
| 1304 | #define XCACHE_ANALYZE_LITERAL(type) \ |
|---|
| 1305 | if (zend_binary_strcmp(Z_STRVAL(literal->constant), Z_STRLEN(literal->constant), compiler->new_entry.type##path, compiler->new_entry.type##path_len) == 0) { \ |
|---|
| 1306 | usage->type##path_used = 1; \ |
|---|
| 1307 | literalinfo |= xcache_##literal##_is_##type; \ |
|---|
| 1308 | } |
|---|
| 1309 | |
|---|
| 1310 | #define XCACHE_U_ANALYZE_LITERAL(type) \ |
|---|
| 1311 | if (zend_u_##binary_strcmp(Z_USTRVAL(literal->constant), Z_USTRLEN(literal->constant), compiler->new_entry.u##type##path, compiler->new_entry.u##type##path_len) == 0) { \ |
|---|
| 1312 | usage->u##type##path_used = 1; \ |
|---|
| 1313 | literalinfo |= xcache_##literal##_is_##type; \ |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | #define XCACHE_ANALYZE_OP(type, op) \ |
|---|
| 1317 | if (zend_binary_strcmp(Z_STRVAL(Z_OP_CONSTANT(opline->op)), Z_STRLEN(Z_OP_CONSTANT(opline->op)), compiler->new_entry.type##path, compiler->new_entry.type##path_len) == 0) { \ |
|---|
| 1318 | usage->type##path_used = 1; \ |
|---|
| 1319 | oplineinfo |= xcache_##op##_is_##type; \ |
|---|
| 1320 | } |
|---|
| 1321 | |
|---|
| 1322 | #define XCACHE_U_ANALYZE_OP(type, op) \ |
|---|
| 1323 | if (zend_u_##binary_strcmp(Z_USTRVAL(Z_OP_CONSTANT(opline->op)), Z_USTRLEN(Z_OP_CONSTANT(opline->op)), compiler->new_entry.u##type##path, compiler->new_entry.u##type##path_len) == 0) { \ |
|---|
| 1324 | usage->u##type##path_used = 1; \ |
|---|
| 1325 | oplineinfo |= xcache_##op##_is_##type; \ |
|---|
| 1326 | } |
|---|
| 1327 | |
|---|
| 1328 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 1329 | for (literalindex = 0; literalindex < op_array->last_literal; literalindex++) { |
|---|
| 1330 | zend_literal *literal = &op_array->literals[literalindex]; |
|---|
| 1331 | zend_uint literalinfo = 0; |
|---|
| 1332 | if (Z_TYPE(literal->constant) == IS_STRING) { |
|---|
| 1333 | XCACHE_ANALYZE_LITERAL(file) |
|---|
| 1334 | else XCACHE_ANALYZE_LITERAL(dir) |
|---|
| 1335 | } |
|---|
| 1336 | #ifdef IS_UNICODE |
|---|
| 1337 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
|---|
| 1338 | XCACHE_U_ANALYZE_LITERAL(file) |
|---|
| 1339 | else XCACHE_U_ANALYZE_LITERAL(dir) |
|---|
| 1340 | } |
|---|
| 1341 | #endif |
|---|
| 1342 | if (literalinfo) { |
|---|
| 1343 | xc_op_array_info_detail_t detail; |
|---|
| 1344 | detail.index = literalindex; |
|---|
| 1345 | detail.info = literalinfo; |
|---|
| 1346 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
|---|
| 1347 | } |
|---|
| 1348 | } |
|---|
| 1349 | |
|---|
| 1350 | op_array_info->literalinfo_cnt = details.cnt; |
|---|
| 1351 | op_array_info->literalinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
|---|
| 1352 | #else /* ZEND_ENGINE_2_4 */ |
|---|
| 1353 | for (oplinenum = 0; oplinenum < op_array->last; oplinenum++) { |
|---|
| 1354 | zend_op *opline = &op_array->opcodes[oplinenum]; |
|---|
| 1355 | zend_uint oplineinfo = 0; |
|---|
| 1356 | if (Z_OP_TYPE(opline->op1) == IS_CONST) { |
|---|
| 1357 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
|---|
| 1358 | XCACHE_ANALYZE_OP(file, op1) |
|---|
| 1359 | else XCACHE_ANALYZE_OP(dir, op1) |
|---|
| 1360 | } |
|---|
| 1361 | #ifdef IS_UNICODE |
|---|
| 1362 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
|---|
| 1363 | XCACHE_U_ANALYZE_OP(file, op1) |
|---|
| 1364 | else XCACHE_U_ANALYZE_OP(dir, op1) |
|---|
| 1365 | } |
|---|
| 1366 | #endif |
|---|
| 1367 | } |
|---|
| 1368 | |
|---|
| 1369 | if (Z_OP_TYPE(opline->op2) == IS_CONST) { |
|---|
| 1370 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
|---|
| 1371 | XCACHE_ANALYZE_OP(file, op2) |
|---|
| 1372 | else XCACHE_ANALYZE_OP(dir, op2) |
|---|
| 1373 | } |
|---|
| 1374 | #ifdef IS_UNICODE |
|---|
| 1375 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
|---|
| 1376 | XCACHE_U_ANALYZE_OP(file, op2) |
|---|
| 1377 | else XCACHE_U_ANALYZE_OP(dir, op2) |
|---|
| 1378 | } |
|---|
| 1379 | #endif |
|---|
| 1380 | } |
|---|
| 1381 | |
|---|
| 1382 | if (oplineinfo) { |
|---|
| 1383 | xc_op_array_info_detail_t detail; |
|---|
| 1384 | detail.index = oplinenum; |
|---|
| 1385 | detail.info = oplineinfo; |
|---|
| 1386 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
|---|
| 1387 | } |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | op_array_info->oplineinfo_cnt = details.cnt; |
|---|
| 1391 | op_array_info->oplineinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
|---|
| 1392 | #endif /* ZEND_ENGINE_2_4 */ |
|---|
| 1393 | xc_vector_free(xc_op_array_info_detail_t, &details); |
|---|
| 1394 | } |
|---|
| 1395 | /* }}} */ |
|---|
| 1396 | void xc_fix_op_array_info(const xc_entry_php_t *entry_php, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copy, const xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
|---|
| 1397 | { |
|---|
| 1398 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 1399 | zend_uint literalinfoindex; |
|---|
| 1400 | |
|---|
| 1401 | for (literalinfoindex = 0; literalinfoindex < op_array_info->literalinfo_cnt; ++literalinfoindex) { |
|---|
| 1402 | int literalindex = op_array_info->literalinfos[literalinfoindex].index; |
|---|
| 1403 | int literalinfo = op_array_info->literalinfos[literalinfoindex].info; |
|---|
| 1404 | zend_literal *literal = &op_array->literals[literalindex]; |
|---|
| 1405 | if ((literalinfo & xcache_literal_is_file)) { |
|---|
| 1406 | if (!shallow_copy) { |
|---|
| 1407 | efree(Z_STRVAL(literal->constant)); |
|---|
| 1408 | } |
|---|
| 1409 | if (Z_TYPE(literal->constant) == IS_STRING) { |
|---|
| 1410 | assert(entry_php->filepath); |
|---|
| 1411 | ZVAL_STRINGL(&literal->constant, entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
|---|
| 1412 | TRACE("restored literal constant: %s", entry_php->filepath); |
|---|
| 1413 | } |
|---|
| 1414 | #ifdef IS_UNICODE |
|---|
| 1415 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
|---|
| 1416 | assert(entry_php->ufilepath); |
|---|
| 1417 | ZVAL_UNICODEL(&literal->constant, entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
|---|
| 1418 | } |
|---|
| 1419 | #endif |
|---|
| 1420 | else { |
|---|
| 1421 | assert(0); |
|---|
| 1422 | } |
|---|
| 1423 | } |
|---|
| 1424 | else if ((literalinfo & xcache_literal_is_dir)) { |
|---|
| 1425 | if (!shallow_copy) { |
|---|
| 1426 | efree(Z_STRVAL(literal->constant)); |
|---|
| 1427 | } |
|---|
| 1428 | if (Z_TYPE(literal->constant) == IS_STRING) { |
|---|
| 1429 | assert(entry_php->dirpath); |
|---|
| 1430 | TRACE("restored literal constant: %s", entry_php->dirpath); |
|---|
| 1431 | ZVAL_STRINGL(&literal->constant, entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
|---|
| 1432 | } |
|---|
| 1433 | #ifdef IS_UNICODE |
|---|
| 1434 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
|---|
| 1435 | assert(!entry_php->udirpath); |
|---|
| 1436 | ZVAL_UNICODEL(&literal->constant, entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
|---|
| 1437 | } |
|---|
| 1438 | #endif |
|---|
| 1439 | else { |
|---|
| 1440 | assert(0); |
|---|
| 1441 | } |
|---|
| 1442 | } |
|---|
| 1443 | } |
|---|
| 1444 | #else /* ZEND_ENGINE_2_4 */ |
|---|
| 1445 | zend_uint oplinenum; |
|---|
| 1446 | |
|---|
| 1447 | for (oplinenum = 0; oplinenum < op_array_info->oplineinfo_cnt; ++oplinenum) { |
|---|
| 1448 | int oplineindex = op_array_info->oplineinfos[oplinenum].index; |
|---|
| 1449 | int oplineinfo = op_array_info->oplineinfos[oplinenum].info; |
|---|
| 1450 | zend_op *opline = &op_array->opcodes[oplineindex]; |
|---|
| 1451 | if ((oplineinfo & xcache_op1_is_file)) { |
|---|
| 1452 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
|---|
| 1453 | if (!shallow_copy) { |
|---|
| 1454 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
|---|
| 1455 | } |
|---|
| 1456 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
|---|
| 1457 | assert(entry_php->filepath); |
|---|
| 1458 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
|---|
| 1459 | TRACE("restored op1 constant: %s", entry_php->filepath); |
|---|
| 1460 | } |
|---|
| 1461 | #ifdef IS_UNICODE |
|---|
| 1462 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
|---|
| 1463 | assert(entry_php->ufilepath); |
|---|
| 1464 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
|---|
| 1465 | } |
|---|
| 1466 | #endif |
|---|
| 1467 | else { |
|---|
| 1468 | assert(0); |
|---|
| 1469 | } |
|---|
| 1470 | } |
|---|
| 1471 | else if ((oplineinfo & xcache_op1_is_dir)) { |
|---|
| 1472 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
|---|
| 1473 | if (!shallow_copy) { |
|---|
| 1474 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
|---|
| 1475 | } |
|---|
| 1476 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
|---|
| 1477 | assert(entry_php->dirpath); |
|---|
| 1478 | TRACE("restored op1 constant: %s", entry_php->dirpath); |
|---|
| 1479 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
|---|
| 1480 | } |
|---|
| 1481 | #ifdef IS_UNICODE |
|---|
| 1482 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
|---|
| 1483 | assert(!entry_php->udirpath); |
|---|
| 1484 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
|---|
| 1485 | } |
|---|
| 1486 | #endif |
|---|
| 1487 | else { |
|---|
| 1488 | assert(0); |
|---|
| 1489 | } |
|---|
| 1490 | } |
|---|
| 1491 | |
|---|
| 1492 | if ((oplineinfo & xcache_op2_is_file)) { |
|---|
| 1493 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
|---|
| 1494 | if (!shallow_copy) { |
|---|
| 1495 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
|---|
| 1496 | } |
|---|
| 1497 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
|---|
| 1498 | assert(entry_php->filepath); |
|---|
| 1499 | TRACE("restored op2 constant: %s", entry_php->filepath); |
|---|
| 1500 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
|---|
| 1501 | } |
|---|
| 1502 | #ifdef IS_UNICODE |
|---|
| 1503 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
|---|
| 1504 | assert(entry_php->ufilepath); |
|---|
| 1505 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
|---|
| 1506 | } |
|---|
| 1507 | #endif |
|---|
| 1508 | else { |
|---|
| 1509 | assert(0); |
|---|
| 1510 | } |
|---|
| 1511 | } |
|---|
| 1512 | else if ((oplineinfo & xcache_op2_is_dir)) { |
|---|
| 1513 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
|---|
| 1514 | if (!shallow_copy) { |
|---|
| 1515 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
|---|
| 1516 | } |
|---|
| 1517 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
|---|
| 1518 | assert(entry_php->dirpath); |
|---|
| 1519 | TRACE("restored op2 constant: %s", entry_php->dirpath); |
|---|
| 1520 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
|---|
| 1521 | } |
|---|
| 1522 | #ifdef IS_UNICODE |
|---|
| 1523 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
|---|
| 1524 | assert(entry_php->udirpath); |
|---|
| 1525 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
|---|
| 1526 | } |
|---|
| 1527 | #endif |
|---|
| 1528 | else { |
|---|
| 1529 | assert(0); |
|---|
| 1530 | } |
|---|
| 1531 | } |
|---|
| 1532 | } |
|---|
| 1533 | #endif /* ZEND_ENGINE_2_4 */ |
|---|
| 1534 | } |
|---|
| 1535 | /* }}} */ |
|---|
| 1536 | static void xc_free_op_array_info(xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
|---|
| 1537 | { |
|---|
| 1538 | #ifdef ZEND_ENGINE_2_4 |
|---|
| 1539 | if (op_array_info->literalinfos) { |
|---|
| 1540 | efree(op_array_info->literalinfos); |
|---|
| 1541 | } |
|---|
| 1542 | #else |
|---|
| 1543 | if (op_array_info->oplineinfos) { |
|---|
| 1544 | efree(op_array_info->oplineinfos); |
|---|
| 1545 | } |
|---|
| 1546 | #endif |
|---|
| 1547 | } |
|---|
| 1548 | /* }}} */ |
|---|
| 1549 | static void xc_free_php(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 1550 | { |
|---|
| 1551 | zend_uint i; |
|---|
| 1552 | if (php->classinfos) { |
|---|
| 1553 | for (i = 0; i < php->classinfo_cnt; i ++) { |
|---|
| 1554 | xc_classinfo_t *classinfo = &php->classinfos[i]; |
|---|
| 1555 | zend_uint j; |
|---|
| 1556 | |
|---|
| 1557 | for (j = 0; j < classinfo->methodinfo_cnt; j ++) { |
|---|
| 1558 | xc_free_op_array_info(&classinfo->methodinfos[j] TSRMLS_CC); |
|---|
| 1559 | } |
|---|
| 1560 | |
|---|
| 1561 | if (classinfo->methodinfos) { |
|---|
| 1562 | efree(classinfo->methodinfos); |
|---|
| 1563 | } |
|---|
| 1564 | } |
|---|
| 1565 | } |
|---|
| 1566 | if (php->funcinfos) { |
|---|
| 1567 | for (i = 0; i < php->funcinfo_cnt; i ++) { |
|---|
| 1568 | xc_free_op_array_info(&php->funcinfos[i].op_array_info TSRMLS_CC); |
|---|
| 1569 | } |
|---|
| 1570 | } |
|---|
| 1571 | xc_free_op_array_info(&php->op_array_info TSRMLS_CC); |
|---|
| 1572 | |
|---|
| 1573 | #define X_FREE(var) do {\ |
|---|
| 1574 | if (php->var) { \ |
|---|
| 1575 | efree(php->var); \ |
|---|
| 1576 | } \ |
|---|
| 1577 | } while (0) |
|---|
| 1578 | |
|---|
| 1579 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1580 | X_FREE(autoglobals); |
|---|
| 1581 | #endif |
|---|
| 1582 | X_FREE(classinfos); |
|---|
| 1583 | X_FREE(funcinfos); |
|---|
| 1584 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1585 | X_FREE(constinfos); |
|---|
| 1586 | #endif |
|---|
| 1587 | #undef X_FREE |
|---|
| 1588 | } |
|---|
| 1589 | /* }}} */ |
|---|
| 1590 | static void xc_compile_php(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 1591 | { |
|---|
| 1592 | zend_uint old_constinfo_cnt, old_funcinfo_cnt, old_classinfo_cnt; |
|---|
| 1593 | zend_bool catched = 0; |
|---|
| 1594 | |
|---|
| 1595 | /* {{{ compile */ |
|---|
| 1596 | TRACE("compiling %s", h->opened_path ? h->opened_path : h->filename); |
|---|
| 1597 | |
|---|
| 1598 | old_classinfo_cnt = zend_hash_num_elements(CG(class_table)); |
|---|
| 1599 | old_funcinfo_cnt = zend_hash_num_elements(CG(function_table)); |
|---|
| 1600 | old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants)); |
|---|
| 1601 | |
|---|
| 1602 | zend_try { |
|---|
| 1603 | compiler->new_php.op_array = old_compile_file(h, type TSRMLS_CC); |
|---|
| 1604 | } zend_catch { |
|---|
| 1605 | catched = 1; |
|---|
| 1606 | } zend_end_try(); |
|---|
| 1607 | |
|---|
| 1608 | if (catched) { |
|---|
| 1609 | goto err_bailout; |
|---|
| 1610 | } |
|---|
| 1611 | |
|---|
| 1612 | if (compiler->new_php.op_array == NULL) { |
|---|
| 1613 | goto err_op_array; |
|---|
| 1614 | } |
|---|
| 1615 | |
|---|
| 1616 | if (!XG(initial_compile_file_called)) { |
|---|
| 1617 | TRACE("%s", "!initial_compile_file_called, give up"); |
|---|
| 1618 | return; |
|---|
| 1619 | } |
|---|
| 1620 | |
|---|
| 1621 | /* }}} */ |
|---|
| 1622 | /* {{{ prepare */ |
|---|
| 1623 | zend_restore_compiled_filename(h->opened_path ? h->opened_path : (char *) h->filename TSRMLS_CC); |
|---|
| 1624 | |
|---|
| 1625 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1626 | compiler->new_php.constinfo_cnt = zend_hash_num_elements(EG(zend_constants)) - old_constinfo_cnt; |
|---|
| 1627 | #endif |
|---|
| 1628 | compiler->new_php.funcinfo_cnt = zend_hash_num_elements(CG(function_table)) - old_funcinfo_cnt; |
|---|
| 1629 | compiler->new_php.classinfo_cnt = zend_hash_num_elements(CG(class_table)) - old_classinfo_cnt; |
|---|
| 1630 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1631 | /* {{{ count new_php.autoglobal_cnt */ { |
|---|
| 1632 | Bucket *b; |
|---|
| 1633 | |
|---|
| 1634 | compiler->new_php.autoglobal_cnt = 0; |
|---|
| 1635 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
|---|
| 1636 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
|---|
| 1637 | /* check if actived */ |
|---|
| 1638 | if (auto_global->auto_global_callback && !auto_global->armed) { |
|---|
| 1639 | compiler->new_php.autoglobal_cnt ++; |
|---|
| 1640 | } |
|---|
| 1641 | } |
|---|
| 1642 | } |
|---|
| 1643 | /* }}} */ |
|---|
| 1644 | #endif |
|---|
| 1645 | |
|---|
| 1646 | #define X_ALLOC_N(var, cnt) do { \ |
|---|
| 1647 | if (compiler->new_php.cnt) { \ |
|---|
| 1648 | ECALLOC_N(compiler->new_php.var, compiler->new_php.cnt); \ |
|---|
| 1649 | if (!compiler->new_php.var) { \ |
|---|
| 1650 | goto err_alloc; \ |
|---|
| 1651 | } \ |
|---|
| 1652 | } \ |
|---|
| 1653 | else { \ |
|---|
| 1654 | compiler->new_php.var = NULL; \ |
|---|
| 1655 | } \ |
|---|
| 1656 | } while (0) |
|---|
| 1657 | |
|---|
| 1658 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1659 | X_ALLOC_N(constinfos, constinfo_cnt); |
|---|
| 1660 | #endif |
|---|
| 1661 | X_ALLOC_N(funcinfos, funcinfo_cnt); |
|---|
| 1662 | X_ALLOC_N(classinfos, classinfo_cnt); |
|---|
| 1663 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1664 | X_ALLOC_N(autoglobals, autoglobal_cnt); |
|---|
| 1665 | #endif |
|---|
| 1666 | #undef X_ALLOC |
|---|
| 1667 | /* }}} */ |
|---|
| 1668 | |
|---|
| 1669 | /* {{{ shallow copy, pointers only */ { |
|---|
| 1670 | Bucket *b; |
|---|
| 1671 | zend_uint i; |
|---|
| 1672 | zend_uint j; |
|---|
| 1673 | |
|---|
| 1674 | #define COPY_H(vartype, var, cnt, name, datatype) do { \ |
|---|
| 1675 | for (i = 0, j = 0; b; i ++, b = b->pListNext) { \ |
|---|
| 1676 | vartype *data = &compiler->new_php.var[j]; \ |
|---|
| 1677 | \ |
|---|
| 1678 | if (i < old_##cnt) { \ |
|---|
| 1679 | continue; \ |
|---|
| 1680 | } \ |
|---|
| 1681 | j ++; \ |
|---|
| 1682 | \ |
|---|
| 1683 | assert(i < old_##cnt + compiler->new_php.cnt); \ |
|---|
| 1684 | assert(b->pData); \ |
|---|
| 1685 | memcpy(&data->name, b->pData, sizeof(datatype)); \ |
|---|
| 1686 | UNISW(NOTHING, data->type = b->key.type;) \ |
|---|
| 1687 | if (UNISW(1, b->key.type == IS_STRING)) { \ |
|---|
| 1688 | ZSTR_S(data->key) = BUCKET_KEY_S(b); \ |
|---|
| 1689 | } \ |
|---|
| 1690 | else { \ |
|---|
| 1691 | ZSTR_U(data->key) = BUCKET_KEY_U(b); \ |
|---|
| 1692 | } \ |
|---|
| 1693 | data->key_size = b->nKeyLength; \ |
|---|
| 1694 | data->h = b->h; \ |
|---|
| 1695 | } \ |
|---|
| 1696 | } while(0) |
|---|
| 1697 | |
|---|
| 1698 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1699 | b = EG(zend_constants)->pListHead; COPY_H(xc_constinfo_t, constinfos, constinfo_cnt, constant, zend_constant); |
|---|
| 1700 | #endif |
|---|
| 1701 | b = CG(function_table)->pListHead; COPY_H(xc_funcinfo_t, funcinfos, funcinfo_cnt, func, zend_function); |
|---|
| 1702 | b = CG(class_table)->pListHead; COPY_H(xc_classinfo_t, classinfos, classinfo_cnt, cest, xc_cest_t); |
|---|
| 1703 | |
|---|
| 1704 | #undef COPY_H |
|---|
| 1705 | |
|---|
| 1706 | /* for ZE1, cest need to be fixed inside store */ |
|---|
| 1707 | |
|---|
| 1708 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1709 | /* scan for acatived auto globals */ |
|---|
| 1710 | i = 0; |
|---|
| 1711 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
|---|
| 1712 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
|---|
| 1713 | /* check if actived */ |
|---|
| 1714 | if (auto_global->auto_global_callback && !auto_global->armed) { |
|---|
| 1715 | xc_autoglobal_t *data = &compiler->new_php.autoglobals[i]; |
|---|
| 1716 | |
|---|
| 1717 | assert(i < compiler->new_php.autoglobal_cnt); |
|---|
| 1718 | i ++; |
|---|
| 1719 | UNISW(NOTHING, data->type = b->key.type;) |
|---|
| 1720 | if (UNISW(1, b->key.type == IS_STRING)) { |
|---|
| 1721 | ZSTR_S(data->key) = BUCKET_KEY_S(b); |
|---|
| 1722 | } |
|---|
| 1723 | else { |
|---|
| 1724 | ZSTR_U(data->key) = BUCKET_KEY_U(b); |
|---|
| 1725 | } |
|---|
| 1726 | data->key_len = b->nKeyLength - 1; |
|---|
| 1727 | data->h = b->h; |
|---|
| 1728 | } |
|---|
| 1729 | } |
|---|
| 1730 | #endif |
|---|
| 1731 | } |
|---|
| 1732 | /* }}} */ |
|---|
| 1733 | |
|---|
| 1734 | /* {{{ collect info for file/dir path */ { |
|---|
| 1735 | Bucket *b; |
|---|
| 1736 | xc_const_usage_t const_usage; |
|---|
| 1737 | unsigned int i; |
|---|
| 1738 | |
|---|
| 1739 | xc_entry_php_init(&compiler->new_entry, zend_get_compiled_filename(TSRMLS_C) TSRMLS_CC); |
|---|
| 1740 | memset(&const_usage, 0, sizeof(const_usage)); |
|---|
| 1741 | |
|---|
| 1742 | for (i = 0; i < compiler->new_php.classinfo_cnt; i ++) { |
|---|
| 1743 | xc_classinfo_t *classinfo = &compiler->new_php.classinfos[i]; |
|---|
| 1744 | zend_class_entry *ce = CestToCePtr(classinfo->cest); |
|---|
| 1745 | classinfo->methodinfo_cnt = ce->function_table.nTableSize; |
|---|
| 1746 | if (classinfo->methodinfo_cnt) { |
|---|
| 1747 | int j; |
|---|
| 1748 | |
|---|
| 1749 | ECALLOC_N(classinfo->methodinfos, classinfo->methodinfo_cnt); |
|---|
| 1750 | if (!classinfo->methodinfos) { |
|---|
| 1751 | goto err_alloc; |
|---|
| 1752 | } |
|---|
| 1753 | |
|---|
| 1754 | for (j = 0, b = ce->function_table.pListHead; b; j ++, b = b->pListNext) { |
|---|
| 1755 | xc_collect_op_array_info(compiler, &const_usage, &classinfo->methodinfos[j], (zend_op_array *) b->pData TSRMLS_CC); |
|---|
| 1756 | } |
|---|
| 1757 | } |
|---|
| 1758 | else { |
|---|
| 1759 | classinfo->methodinfos = NULL; |
|---|
| 1760 | } |
|---|
| 1761 | } |
|---|
| 1762 | |
|---|
| 1763 | for (i = 0; i < compiler->new_php.funcinfo_cnt; i ++) { |
|---|
| 1764 | xc_collect_op_array_info(compiler, &const_usage, &compiler->new_php.funcinfos[i].op_array_info, (zend_op_array *) &compiler->new_php.funcinfos[i].func TSRMLS_CC); |
|---|
| 1765 | } |
|---|
| 1766 | |
|---|
| 1767 | xc_collect_op_array_info(compiler, &const_usage, &compiler->new_php.op_array_info, compiler->new_php.op_array TSRMLS_CC); |
|---|
| 1768 | |
|---|
| 1769 | /* file/dir path free unused */ |
|---|
| 1770 | #define X_FREE_UNUSED(var) \ |
|---|
| 1771 | if (!const_usage.var##path_used) { \ |
|---|
| 1772 | efree(compiler->new_entry.var##path); \ |
|---|
| 1773 | compiler->new_entry.var##path = NULL; \ |
|---|
| 1774 | compiler->new_entry.var##path_len = 0; \ |
|---|
| 1775 | } |
|---|
| 1776 | /* filepath is required to restore op_array->filename, so no free filepath here */ |
|---|
| 1777 | X_FREE_UNUSED(dir) |
|---|
| 1778 | #ifdef IS_UNICODE |
|---|
| 1779 | X_FREE_UNUSED(ufile) |
|---|
| 1780 | X_FREE_UNUSED(udir) |
|---|
| 1781 | #endif |
|---|
| 1782 | #undef X_FREE_UNUSED |
|---|
| 1783 | } |
|---|
| 1784 | /* }}} */ |
|---|
| 1785 | #ifdef XCACHE_ERROR_CACHING |
|---|
| 1786 | compiler->new_php.compilererrors = xc_sandbox_compilererrors(TSRMLS_C); |
|---|
| 1787 | compiler->new_php.compilererror_cnt = xc_sandbox_compilererror_cnt(TSRMLS_C); |
|---|
| 1788 | #endif |
|---|
| 1789 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 1790 | /* {{{ find inherited classes that should be early-binding */ |
|---|
| 1791 | compiler->new_php.have_early_binding = 0; |
|---|
| 1792 | { |
|---|
| 1793 | zend_uint i; |
|---|
| 1794 | for (i = 0; i < compiler->new_php.classinfo_cnt; i ++) { |
|---|
| 1795 | compiler->new_php.classinfos[i].oplineno = -1; |
|---|
| 1796 | } |
|---|
| 1797 | } |
|---|
| 1798 | |
|---|
| 1799 | xc_undo_pass_two(compiler->new_php.op_array TSRMLS_CC); |
|---|
| 1800 | xc_foreach_early_binding_class(compiler->new_php.op_array, xc_cache_early_binding_class_cb, (void *) &compiler->new_php TSRMLS_CC); |
|---|
| 1801 | xc_redo_pass_two(compiler->new_php.op_array TSRMLS_CC); |
|---|
| 1802 | /* }}} */ |
|---|
| 1803 | #endif |
|---|
| 1804 | |
|---|
| 1805 | return; |
|---|
| 1806 | |
|---|
| 1807 | err_alloc: |
|---|
| 1808 | xc_free_php(&compiler->new_php TSRMLS_CC); |
|---|
| 1809 | |
|---|
| 1810 | err_bailout: |
|---|
| 1811 | err_op_array: |
|---|
| 1812 | |
|---|
| 1813 | if (catched) { |
|---|
| 1814 | zend_bailout(); |
|---|
| 1815 | } |
|---|
| 1816 | } |
|---|
| 1817 | /* }}} */ |
|---|
| 1818 | static zend_op_array *xc_compile_restore(xc_entry_php_t *stored_entry, xc_entry_data_php_t *stored_php TSRMLS_DC) /* {{{ */ |
|---|
| 1819 | { |
|---|
| 1820 | zend_op_array *op_array; |
|---|
| 1821 | xc_entry_php_t restored_entry; |
|---|
| 1822 | xc_entry_data_php_t restored_php; |
|---|
| 1823 | zend_bool catched; |
|---|
| 1824 | zend_uint i; |
|---|
| 1825 | |
|---|
| 1826 | /* still needed because in zend_language_scanner.l, require()/include() check file_handle.handle.stream.handle */ |
|---|
| 1827 | i = 1; |
|---|
| 1828 | zend_hash_add(&EG(included_files), stored_entry->entry.name.str.val, stored_entry->entry.name.str.len + 1, (void *)&i, sizeof(int), NULL); |
|---|
| 1829 | |
|---|
| 1830 | CG(in_compilation) = 1; |
|---|
| 1831 | CG(compiled_filename) = stored_entry->entry.name.str.val; |
|---|
| 1832 | CG(zend_lineno) = 0; |
|---|
| 1833 | TRACE("restoring %d:%s", stored_entry->file_inode, stored_entry->entry.name.str.val); |
|---|
| 1834 | xc_processor_restore_xc_entry_php_t(&restored_entry, stored_entry TSRMLS_CC); |
|---|
| 1835 | xc_processor_restore_xc_entry_data_php_t(stored_entry, &restored_php, stored_php, xc_readonly_protection TSRMLS_CC); |
|---|
| 1836 | restored_entry.php = &restored_php; |
|---|
| 1837 | #ifdef SHOW_DPRINT |
|---|
| 1838 | xc_dprint(&restored_entry, 0 TSRMLS_CC); |
|---|
| 1839 | #endif |
|---|
| 1840 | |
|---|
| 1841 | catched = 0; |
|---|
| 1842 | zend_try { |
|---|
| 1843 | op_array = xc_entry_install(&restored_entry TSRMLS_CC); |
|---|
| 1844 | } zend_catch { |
|---|
| 1845 | catched = 1; |
|---|
| 1846 | } zend_end_try(); |
|---|
| 1847 | |
|---|
| 1848 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1849 | if (restored_php.constinfos) { |
|---|
| 1850 | efree(restored_php.constinfos); |
|---|
| 1851 | } |
|---|
| 1852 | #endif |
|---|
| 1853 | if (restored_php.funcinfos) { |
|---|
| 1854 | efree(restored_php.funcinfos); |
|---|
| 1855 | } |
|---|
| 1856 | if (restored_php.classinfos) { |
|---|
| 1857 | efree(restored_php.classinfos); |
|---|
| 1858 | } |
|---|
| 1859 | |
|---|
| 1860 | if (catched) { |
|---|
| 1861 | zend_bailout(); |
|---|
| 1862 | } |
|---|
| 1863 | CG(in_compilation) = 0; |
|---|
| 1864 | CG(compiled_filename) = NULL; |
|---|
| 1865 | TRACE("restored %d:%s", stored_entry->file_inode, stored_entry->entry.name.str.val); |
|---|
| 1866 | return op_array; |
|---|
| 1867 | } |
|---|
| 1868 | /* }}} */ |
|---|
| 1869 | typedef struct xc_sandboxed_compiler_t { /* {{{ */ |
|---|
| 1870 | xc_compiler_t *compiler; |
|---|
| 1871 | /* input */ |
|---|
| 1872 | zend_file_handle *h; |
|---|
| 1873 | int type; |
|---|
| 1874 | |
|---|
| 1875 | /* sandbox output */ |
|---|
| 1876 | xc_entry_php_t *stored_entry; |
|---|
| 1877 | xc_entry_data_php_t *stored_php; |
|---|
| 1878 | } xc_sandboxed_compiler_t; |
|---|
| 1879 | /* }}} */ |
|---|
| 1880 | |
|---|
| 1881 | static zend_op_array *xc_compile_file_sandboxed(void *data TSRMLS_DC) /* {{{ */ |
|---|
| 1882 | { |
|---|
| 1883 | xc_sandboxed_compiler_t *sandboxed_compiler = (xc_sandboxed_compiler_t *) data; |
|---|
| 1884 | xc_compiler_t *compiler = sandboxed_compiler->compiler; |
|---|
| 1885 | zend_bool catched = 0; |
|---|
| 1886 | xc_cache_t *cache = xc_php_caches[compiler->entry_hash.cacheid]; |
|---|
| 1887 | xc_entry_php_t *stored_entry; |
|---|
| 1888 | xc_entry_data_php_t *stored_php; |
|---|
| 1889 | |
|---|
| 1890 | /* {{{ compile */ |
|---|
| 1891 | /* make compile inside sandbox */ |
|---|
| 1892 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1893 | compiler->new_php.constinfos = NULL; |
|---|
| 1894 | #endif |
|---|
| 1895 | compiler->new_php.funcinfos = NULL; |
|---|
| 1896 | compiler->new_php.classinfos = NULL; |
|---|
| 1897 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1898 | compiler->new_php.autoglobals = NULL; |
|---|
| 1899 | #endif |
|---|
| 1900 | memset(&compiler->new_php.op_array_info, 0, sizeof(compiler->new_php.op_array_info)); |
|---|
| 1901 | |
|---|
| 1902 | XG(initial_compile_file_called) = 0; |
|---|
| 1903 | zend_try { |
|---|
| 1904 | compiler->new_php.op_array = NULL; |
|---|
| 1905 | xc_compile_php(compiler, sandboxed_compiler->h, sandboxed_compiler->type TSRMLS_CC); |
|---|
| 1906 | } zend_catch { |
|---|
| 1907 | catched = 1; |
|---|
| 1908 | } zend_end_try(); |
|---|
| 1909 | |
|---|
| 1910 | if (catched |
|---|
| 1911 | || !compiler->new_php.op_array /* possible ? */ |
|---|
| 1912 | || !XG(initial_compile_file_called)) { |
|---|
| 1913 | goto err_aftersandbox; |
|---|
| 1914 | } |
|---|
| 1915 | |
|---|
| 1916 | /* }}} */ |
|---|
| 1917 | #ifdef SHOW_DPRINT |
|---|
| 1918 | compiler->new_entry.php = &compiler->new_php; |
|---|
| 1919 | xc_dprint(&compiler->new_entry, 0 TSRMLS_CC); |
|---|
| 1920 | #endif |
|---|
| 1921 | |
|---|
| 1922 | stored_entry = NULL; |
|---|
| 1923 | stored_php = NULL; |
|---|
| 1924 | ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */ |
|---|
| 1925 | /* php_store */ |
|---|
| 1926 | stored_php = xc_php_store_unlocked(cache, &compiler->new_php TSRMLS_CC); |
|---|
| 1927 | if (!stored_php) { |
|---|
| 1928 | /* error */ |
|---|
| 1929 | break; |
|---|
| 1930 | } |
|---|
| 1931 | /* entry_store */ |
|---|
| 1932 | compiler->new_entry.php = stored_php; |
|---|
| 1933 | stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); |
|---|
| 1934 | if (stored_entry) { |
|---|
| 1935 | xc_php_addref_unlocked(stored_php); |
|---|
| 1936 | TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
|---|
| 1937 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
|---|
| 1938 | } |
|---|
| 1939 | } LEAVE_LOCK_EX(cache); |
|---|
| 1940 | /* }}} */ |
|---|
| 1941 | TRACE("%s", stored_entry ? "stored" : "store failed"); |
|---|
| 1942 | |
|---|
| 1943 | if (catched || !stored_php) { |
|---|
| 1944 | goto err_aftersandbox; |
|---|
| 1945 | } |
|---|
| 1946 | |
|---|
| 1947 | cache->compiling = 0; |
|---|
| 1948 | xc_free_php(&compiler->new_php TSRMLS_CC); |
|---|
| 1949 | |
|---|
| 1950 | if (stored_entry) { |
|---|
| 1951 | sandboxed_compiler->stored_entry = stored_entry; |
|---|
| 1952 | sandboxed_compiler->stored_php = stored_php; |
|---|
| 1953 | /* discard newly compiled result, restore from stored one */ |
|---|
| 1954 | if (compiler->new_php.op_array) { |
|---|
| 1955 | #ifdef ZEND_ENGINE_2 |
|---|
| 1956 | destroy_op_array(compiler->new_php.op_array TSRMLS_CC); |
|---|
| 1957 | #else |
|---|
| 1958 | destroy_op_array(compiler->new_php.op_array); |
|---|
| 1959 | #endif |
|---|
| 1960 | efree(compiler->new_php.op_array); |
|---|
| 1961 | compiler->new_php.op_array = NULL; |
|---|
| 1962 | } |
|---|
| 1963 | return NULL; |
|---|
| 1964 | } |
|---|
| 1965 | else { |
|---|
| 1966 | return compiler->new_php.op_array; |
|---|
| 1967 | } |
|---|
| 1968 | |
|---|
| 1969 | err_aftersandbox: |
|---|
| 1970 | xc_free_php(&compiler->new_php TSRMLS_CC); |
|---|
| 1971 | |
|---|
| 1972 | cache->compiling = 0; |
|---|
| 1973 | if (catched) { |
|---|
| 1974 | cache->errors ++; |
|---|
| 1975 | zend_bailout(); |
|---|
| 1976 | } |
|---|
| 1977 | return compiler->new_php.op_array; |
|---|
| 1978 | } /* }}} */ |
|---|
| 1979 | static zend_op_array *xc_compile_file_cached(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 1980 | { |
|---|
| 1981 | /* |
|---|
| 1982 | if (clog) { |
|---|
| 1983 | return old; |
|---|
| 1984 | } |
|---|
| 1985 | |
|---|
| 1986 | if (cached_entry = getby entry_hash) { |
|---|
| 1987 | php = cached_entry.php; |
|---|
| 1988 | php = restore(php); |
|---|
| 1989 | return php; |
|---|
| 1990 | } |
|---|
| 1991 | else { |
|---|
| 1992 | if (!(php = getby md5)) { |
|---|
| 1993 | if (clog) { |
|---|
| 1994 | return old; |
|---|
| 1995 | } |
|---|
| 1996 | |
|---|
| 1997 | inside_sandbox { |
|---|
| 1998 | php = compile; |
|---|
| 1999 | entry = create entries[entry]; |
|---|
| 2000 | } |
|---|
| 2001 | } |
|---|
| 2002 | |
|---|
| 2003 | entry.php = php; |
|---|
| 2004 | return php; |
|---|
| 2005 | } |
|---|
| 2006 | */ |
|---|
| 2007 | |
|---|
| 2008 | xc_entry_php_t *stored_entry; |
|---|
| 2009 | xc_entry_data_php_t *stored_php; |
|---|
| 2010 | zend_bool gaveup = 0; |
|---|
| 2011 | zend_bool catched = 0; |
|---|
| 2012 | zend_op_array *op_array; |
|---|
| 2013 | xc_cache_t *cache = xc_php_caches[compiler->entry_hash.cacheid]; |
|---|
| 2014 | xc_sandboxed_compiler_t sandboxed_compiler; |
|---|
| 2015 | |
|---|
| 2016 | /* stale clogs precheck */ |
|---|
| 2017 | if (XG(request_time) - cache->compiling < 30) { |
|---|
| 2018 | cache->clogs ++; |
|---|
| 2019 | return old_compile_file(h, type TSRMLS_CC); |
|---|
| 2020 | } |
|---|
| 2021 | |
|---|
| 2022 | /* {{{ entry_lookup/hit/md5_init/php_lookup */ |
|---|
| 2023 | stored_entry = NULL; |
|---|
| 2024 | stored_php = NULL; |
|---|
| 2025 | |
|---|
| 2026 | ENTER_LOCK_EX(cache) { |
|---|
| 2027 | if (!compiler->opened_path && xc_entry_resolve_path_unlocked(compiler, compiler->filename, &stored_entry TSRMLS_CC) == SUCCESS) { |
|---|
| 2028 | compiler->opened_path = compiler->new_entry.entry.name.str.val; |
|---|
| 2029 | } |
|---|
| 2030 | else { |
|---|
| 2031 | if (!compiler->opened_path && xc_entry_php_resolve_opened_path(compiler, NULL TSRMLS_CC) != SUCCESS) { |
|---|
| 2032 | gaveup = 1; |
|---|
| 2033 | break; |
|---|
| 2034 | } |
|---|
| 2035 | |
|---|
| 2036 | /* finalize name */ |
|---|
| 2037 | compiler->new_entry.entry.name.str.val = (char *) compiler->opened_path; |
|---|
| 2038 | compiler->new_entry.entry.name.str.len = strlen(compiler->new_entry.entry.name.str.val); |
|---|
| 2039 | |
|---|
| 2040 | stored_entry = (xc_entry_php_t *) xc_entry_find_unlocked(XC_TYPE_PHP, cache, compiler->entry_hash.entryslotid, (xc_entry_t *) &compiler->new_entry TSRMLS_CC); |
|---|
| 2041 | } |
|---|
| 2042 | |
|---|
| 2043 | if (stored_entry) { |
|---|
| 2044 | xc_cache_hit_unlocked(cache TSRMLS_CC); |
|---|
| 2045 | |
|---|
| 2046 | TRACE(" hit %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
|---|
| 2047 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
|---|
| 2048 | stored_php = stored_entry->php; |
|---|
| 2049 | break; |
|---|
| 2050 | } |
|---|
| 2051 | |
|---|
| 2052 | TRACE("miss entry %d:%s", compiler->new_entry.file_inode, compiler->new_entry.entry.name.str.val); |
|---|
| 2053 | |
|---|
| 2054 | if (xc_entry_data_php_init_md5(cache, compiler TSRMLS_CC) != SUCCESS) { |
|---|
| 2055 | gaveup = 1; |
|---|
| 2056 | break; |
|---|
| 2057 | } |
|---|
| 2058 | |
|---|
| 2059 | stored_php = xc_php_find_unlocked(cache, &compiler->new_php TSRMLS_CC); |
|---|
| 2060 | |
|---|
| 2061 | if (stored_php) { |
|---|
| 2062 | compiler->new_entry.php = stored_php; |
|---|
| 2063 | xc_entry_php_init(&compiler->new_entry, compiler->opened_path TSRMLS_CC); |
|---|
| 2064 | stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); |
|---|
| 2065 | if (stored_entry) { |
|---|
| 2066 | xc_php_addref_unlocked(stored_php); |
|---|
| 2067 | TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
|---|
| 2068 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
|---|
| 2069 | } |
|---|
| 2070 | else { |
|---|
| 2071 | gaveup = 1; |
|---|
| 2072 | } |
|---|
| 2073 | break; |
|---|
| 2074 | } |
|---|
| 2075 | |
|---|
| 2076 | if (XG(request_time) - cache->compiling < 30) { |
|---|
| 2077 | TRACE("%s", "miss php, but compiling"); |
|---|
| 2078 | cache->clogs ++; |
|---|
| 2079 | gaveup = 1; |
|---|
| 2080 | break; |
|---|
| 2081 | } |
|---|
| 2082 | |
|---|
| 2083 | TRACE("%s", "miss php, going to compile"); |
|---|
| 2084 | cache->compiling = XG(request_time); |
|---|
| 2085 | } LEAVE_LOCK_EX(cache); |
|---|
| 2086 | |
|---|
| 2087 | if (catched) { |
|---|
| 2088 | cache->compiling = 0; |
|---|
| 2089 | zend_bailout(); |
|---|
| 2090 | } |
|---|
| 2091 | |
|---|
| 2092 | /* found entry */ |
|---|
| 2093 | if (stored_entry && stored_php) { |
|---|
| 2094 | zend_llist_add_element(&CG(open_files), h); |
|---|
| 2095 | return xc_compile_restore(stored_entry, stored_php TSRMLS_CC); |
|---|
| 2096 | } |
|---|
| 2097 | |
|---|
| 2098 | /* gaveup */ |
|---|
| 2099 | if (gaveup) { |
|---|
| 2100 | return old_compile_file(h, type TSRMLS_CC); |
|---|
| 2101 | } |
|---|
| 2102 | /* }}} */ |
|---|
| 2103 | |
|---|
| 2104 | sandboxed_compiler.compiler = compiler; |
|---|
| 2105 | sandboxed_compiler.h = h; |
|---|
| 2106 | sandboxed_compiler.type = type; |
|---|
| 2107 | sandboxed_compiler.stored_php = NULL; |
|---|
| 2108 | sandboxed_compiler.stored_entry = NULL; |
|---|
| 2109 | op_array = xc_sandbox(xc_compile_file_sandboxed, (void *) &sandboxed_compiler, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); |
|---|
| 2110 | if (sandboxed_compiler.stored_entry) { |
|---|
| 2111 | return xc_compile_restore(sandboxed_compiler.stored_entry, sandboxed_compiler.stored_php TSRMLS_CC); |
|---|
| 2112 | } |
|---|
| 2113 | else { |
|---|
| 2114 | return op_array; |
|---|
| 2115 | } |
|---|
| 2116 | } |
|---|
| 2117 | /* }}} */ |
|---|
| 2118 | static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 2119 | { |
|---|
| 2120 | xc_compiler_t compiler; |
|---|
| 2121 | zend_op_array *op_array; |
|---|
| 2122 | |
|---|
| 2123 | assert(xc_initized); |
|---|
| 2124 | |
|---|
| 2125 | TRACE("xc_compile_file: type=%d name=%s", h->type, h->filename ? h->filename : "NULL"); |
|---|
| 2126 | |
|---|
| 2127 | if (!XG(cacher) |
|---|
| 2128 | || !h->filename |
|---|
| 2129 | || !SG(request_info).path_translated |
|---|
| 2130 | || strstr(h->filename, "://") != NULL |
|---|
| 2131 | #ifdef ZEND_ENGINE_2_3 |
|---|
| 2132 | /* supported by php_resolve_path */ |
|---|
| 2133 | || (!XG(stat) && strstr(PG(include_path), "://") != NULL) |
|---|
| 2134 | #else |
|---|
| 2135 | || strstr(PG(include_path), "://") != NULL |
|---|
| 2136 | #endif |
|---|
| 2137 | || xc_cache_disabled() |
|---|
| 2138 | ) { |
|---|
| 2139 | TRACE("%s", "cacher not enabled"); |
|---|
| 2140 | return old_compile_file(h, type TSRMLS_CC); |
|---|
| 2141 | } |
|---|
| 2142 | |
|---|
| 2143 | /* {{{ entry_init_key */ |
|---|
| 2144 | compiler.opened_path = h->opened_path; |
|---|
| 2145 | compiler.filename = compiler.opened_path ? compiler.opened_path : h->filename; |
|---|
| 2146 | compiler.filename_len = strlen(compiler.filename); |
|---|
| 2147 | if (xc_entry_php_init_key(&compiler TSRMLS_CC) != SUCCESS) { |
|---|
| 2148 | TRACE("failed to init key for %s", compiler.filename); |
|---|
| 2149 | return old_compile_file(h, type TSRMLS_CC); |
|---|
| 2150 | } |
|---|
| 2151 | /* }}} */ |
|---|
| 2152 | |
|---|
| 2153 | op_array = xc_compile_file_cached(&compiler, h, type TSRMLS_CC); |
|---|
| 2154 | |
|---|
| 2155 | xc_entry_free_key_php(&compiler.new_entry TSRMLS_CC); |
|---|
| 2156 | |
|---|
| 2157 | return op_array; |
|---|
| 2158 | } |
|---|
| 2159 | /* }}} */ |
|---|
| 2160 | |
|---|
| 2161 | /* gdb helper functions, but N/A for coredump */ |
|---|
| 2162 | int xc_is_rw(const void *p) /* {{{ */ |
|---|
| 2163 | { |
|---|
| 2164 | xc_shm_t *shm; |
|---|
| 2165 | size_t i; |
|---|
| 2166 | |
|---|
| 2167 | if (xc_php_caches) { |
|---|
| 2168 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 2169 | shm = xc_php_caches[i]->shm; |
|---|
| 2170 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| 2171 | return 1; |
|---|
| 2172 | } |
|---|
| 2173 | } |
|---|
| 2174 | } |
|---|
| 2175 | |
|---|
| 2176 | if (xc_var_caches) { |
|---|
| 2177 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 2178 | shm = xc_var_caches[i]->shm; |
|---|
| 2179 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| 2180 | return 1; |
|---|
| 2181 | } |
|---|
| 2182 | } |
|---|
| 2183 | } |
|---|
| 2184 | return 0; |
|---|
| 2185 | } |
|---|
| 2186 | /* }}} */ |
|---|
| 2187 | int xc_is_ro(const void *p) /* {{{ */ |
|---|
| 2188 | { |
|---|
| 2189 | xc_shm_t *shm; |
|---|
| 2190 | size_t i; |
|---|
| 2191 | |
|---|
| 2192 | if (xc_php_caches) { |
|---|
| 2193 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 2194 | shm = xc_php_caches[i]->shm; |
|---|
| 2195 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| 2196 | return 1; |
|---|
| 2197 | } |
|---|
| 2198 | } |
|---|
| 2199 | } |
|---|
| 2200 | |
|---|
| 2201 | if (xc_var_caches) { |
|---|
| 2202 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 2203 | shm = xc_var_caches[i]->shm; |
|---|
| 2204 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| 2205 | return 1; |
|---|
| 2206 | } |
|---|
| 2207 | } |
|---|
| 2208 | } |
|---|
| 2209 | return 0; |
|---|
| 2210 | } |
|---|
| 2211 | /* }}} */ |
|---|
| 2212 | int xc_is_shm(const void *p) /* {{{ */ |
|---|
| 2213 | { |
|---|
| 2214 | return xc_is_ro(p) || xc_is_rw(p); |
|---|
| 2215 | } |
|---|
| 2216 | /* }}} */ |
|---|
| 2217 | |
|---|
| 2218 | void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC) /* {{{ */ |
|---|
| 2219 | { |
|---|
| 2220 | zend_llist_add_element(&XG(gc_op_arrays), (void *) gc_op_array); |
|---|
| 2221 | } |
|---|
| 2222 | /* }}} */ |
|---|
| 2223 | static void xc_gc_op_array(void *pDest) /* {{{ */ |
|---|
| 2224 | { |
|---|
| 2225 | xc_gc_op_array_t *op_array = (xc_gc_op_array_t *) pDest; |
|---|
| 2226 | zend_uint i; |
|---|
| 2227 | #ifdef ZEND_ENGINE_2 |
|---|
| 2228 | if (op_array->arg_info) { |
|---|
| 2229 | for (i = 0; i < op_array->num_args; i++) { |
|---|
| 2230 | efree((char *) ZSTR_V(op_array->arg_info[i].name)); |
|---|
| 2231 | if (ZSTR_V(op_array->arg_info[i].class_name)) { |
|---|
| 2232 | efree((char *) ZSTR_V(op_array->arg_info[i].class_name)); |
|---|
| 2233 | } |
|---|
| 2234 | } |
|---|
| 2235 | efree(op_array->arg_info); |
|---|
| 2236 | } |
|---|
| 2237 | #endif |
|---|
| 2238 | if (op_array->opcodes) { |
|---|
| 2239 | efree(op_array->opcodes); |
|---|
| 2240 | } |
|---|
| 2241 | } |
|---|
| 2242 | /* }}} */ |
|---|
| 2243 | |
|---|
| 2244 | /* module helper function */ |
|---|
| 2245 | static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 2246 | { |
|---|
| 2247 | zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 2248 | zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 2249 | return 0; |
|---|
| 2250 | } |
|---|
| 2251 | /* }}} */ |
|---|
| 2252 | static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */ |
|---|
| 2253 | { |
|---|
| 2254 | size_t i; |
|---|
| 2255 | xc_cache_t *cache; |
|---|
| 2256 | xc_shm_t *shm = NULL; |
|---|
| 2257 | |
|---|
| 2258 | if (!caches) { |
|---|
| 2259 | return shm; |
|---|
| 2260 | } |
|---|
| 2261 | |
|---|
| 2262 | for (i = 0; i < hcache->size; i ++) { |
|---|
| 2263 | cache = caches[i]; |
|---|
| 2264 | if (cache) { |
|---|
| 2265 | if (cache->lck) { |
|---|
| 2266 | xc_lock_destroy(cache->lck); |
|---|
| 2267 | } |
|---|
| 2268 | /* do NOT free |
|---|
| 2269 | if (cache->entries) { |
|---|
| 2270 | cache->mem->handlers->free(cache->mem, cache->entries); |
|---|
| 2271 | } |
|---|
| 2272 | cache->mem->handlers->free(cache->mem, cache); |
|---|
| 2273 | */ |
|---|
| 2274 | shm = cache->shm; |
|---|
| 2275 | cache->shm->handlers->memdestroy(cache->mem); |
|---|
| 2276 | } |
|---|
| 2277 | } |
|---|
| 2278 | free(caches); |
|---|
| 2279 | return shm; |
|---|
| 2280 | } |
|---|
| 2281 | /* }}} */ |
|---|
| 2282 | static xc_cache_t **xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *hentry, xc_hash_t *hphp, xc_shmsize_t shmsize) /* {{{ */ |
|---|
| 2283 | { |
|---|
| 2284 | xc_cache_t **caches = NULL, *cache; |
|---|
| 2285 | xc_mem_t *mem; |
|---|
| 2286 | time_t now = time(NULL); |
|---|
| 2287 | size_t i; |
|---|
| 2288 | xc_memsize_t memsize; |
|---|
| 2289 | |
|---|
| 2290 | memsize = shmsize / hcache->size; |
|---|
| 2291 | |
|---|
| 2292 | /* Don't let it break out of mem after ALIGNed |
|---|
| 2293 | * This is important for |
|---|
| 2294 | * Simply loop until it fit our need |
|---|
| 2295 | */ |
|---|
| 2296 | while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) { |
|---|
| 2297 | if (memsize < ALIGN(1)) { |
|---|
| 2298 | CHECK(NULL, "cache too small"); |
|---|
| 2299 | } |
|---|
| 2300 | memsize --; |
|---|
| 2301 | } |
|---|
| 2302 | |
|---|
| 2303 | CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t *)), "caches OOM"); |
|---|
| 2304 | |
|---|
| 2305 | for (i = 0; i < hcache->size; i ++) { |
|---|
| 2306 | CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator"); |
|---|
| 2307 | CHECK(cache = mem->handlers->calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM"); |
|---|
| 2308 | CHECK(cache->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM"); |
|---|
| 2309 | if (hphp) { |
|---|
| 2310 | CHECK(cache->phps= mem->handlers->calloc(mem, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM"); |
|---|
| 2311 | } |
|---|
| 2312 | CHECK(cache->lck = xc_lock_init(NULL), "can't create lock"); |
|---|
| 2313 | |
|---|
| 2314 | cache->hcache = hcache; |
|---|
| 2315 | cache->hentry = hentry; |
|---|
| 2316 | cache->hphp = hphp; |
|---|
| 2317 | cache->shm = shm; |
|---|
| 2318 | cache->mem = mem; |
|---|
| 2319 | cache->cacheid = i; |
|---|
| 2320 | cache->last_gc_deletes = now; |
|---|
| 2321 | cache->last_gc_expires = now; |
|---|
| 2322 | caches[i] = cache; |
|---|
| 2323 | } |
|---|
| 2324 | return caches; |
|---|
| 2325 | |
|---|
| 2326 | err: |
|---|
| 2327 | if (caches) { |
|---|
| 2328 | xc_cache_destroy(caches, hcache); |
|---|
| 2329 | } |
|---|
| 2330 | return NULL; |
|---|
| 2331 | } |
|---|
| 2332 | /* }}} */ |
|---|
| 2333 | static void xc_destroy() /* {{{ */ |
|---|
| 2334 | { |
|---|
| 2335 | xc_shm_t *shm = NULL; |
|---|
| 2336 | if (old_compile_file && zend_compile_file == xc_compile_file) { |
|---|
| 2337 | zend_compile_file = old_compile_file; |
|---|
| 2338 | old_compile_file = NULL; |
|---|
| 2339 | } |
|---|
| 2340 | |
|---|
| 2341 | if (xc_php_caches) { |
|---|
| 2342 | shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); |
|---|
| 2343 | xc_php_caches = NULL; |
|---|
| 2344 | } |
|---|
| 2345 | |
|---|
| 2346 | if (xc_var_caches) { |
|---|
| 2347 | shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); |
|---|
| 2348 | xc_var_caches = NULL; |
|---|
| 2349 | } |
|---|
| 2350 | |
|---|
| 2351 | if (shm) { |
|---|
| 2352 | xc_shm_destroy(shm); |
|---|
| 2353 | } |
|---|
| 2354 | |
|---|
| 2355 | xc_initized = 0; |
|---|
| 2356 | } |
|---|
| 2357 | /* }}} */ |
|---|
| 2358 | static int xc_init() /* {{{ */ |
|---|
| 2359 | { |
|---|
| 2360 | xc_shm_t *shm = NULL; |
|---|
| 2361 | xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size); |
|---|
| 2362 | |
|---|
| 2363 | xc_php_caches = xc_var_caches = NULL; |
|---|
| 2364 | |
|---|
| 2365 | if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) { |
|---|
| 2366 | zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative"); |
|---|
| 2367 | goto err; |
|---|
| 2368 | } |
|---|
| 2369 | |
|---|
| 2370 | if (xc_php_size || xc_var_size) { |
|---|
| 2371 | CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm"); |
|---|
| 2372 | if (!shm->handlers->can_readonly(shm)) { |
|---|
| 2373 | xc_readonly_protection = 0; |
|---|
| 2374 | } |
|---|
| 2375 | |
|---|
| 2376 | if (xc_php_size) { |
|---|
| 2377 | CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache"); |
|---|
| 2378 | } |
|---|
| 2379 | |
|---|
| 2380 | if (xc_var_size) { |
|---|
| 2381 | CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache"); |
|---|
| 2382 | } |
|---|
| 2383 | } |
|---|
| 2384 | return SUCCESS; |
|---|
| 2385 | |
|---|
| 2386 | err: |
|---|
| 2387 | if (xc_php_caches || xc_var_caches) { |
|---|
| 2388 | xc_destroy(); |
|---|
| 2389 | /* shm destroied in xc_destroy() */ |
|---|
| 2390 | } |
|---|
| 2391 | else if (shm) { |
|---|
| 2392 | xc_destroy(); |
|---|
| 2393 | xc_shm_destroy(shm); |
|---|
| 2394 | shm = NULL; |
|---|
| 2395 | } |
|---|
| 2396 | return 0; |
|---|
| 2397 | } |
|---|
| 2398 | /* }}} */ |
|---|
| 2399 | static void xc_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 2400 | { |
|---|
| 2401 | size_t i; |
|---|
| 2402 | |
|---|
| 2403 | if (!XG(internal_table_copied)) { |
|---|
| 2404 | zend_function tmp_func; |
|---|
| 2405 | xc_cest_t tmp_cest; |
|---|
| 2406 | |
|---|
| 2407 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 2408 | zend_hash_destroy(&XG(internal_constant_table)); |
|---|
| 2409 | #endif |
|---|
| 2410 | zend_hash_destroy(&XG(internal_function_table)); |
|---|
| 2411 | zend_hash_destroy(&XG(internal_class_table)); |
|---|
| 2412 | |
|---|
| 2413 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 2414 | zend_hash_init_ex(&XG(internal_constant_table), 20, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0); |
|---|
| 2415 | #endif |
|---|
| 2416 | zend_hash_init_ex(&XG(internal_function_table), 100, NULL, NULL, 1, 0); |
|---|
| 2417 | zend_hash_init_ex(&XG(internal_class_table), 10, NULL, NULL, 1, 0); |
|---|
| 2418 | |
|---|
| 2419 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 2420 | xc_copy_internal_zend_constants(&XG(internal_constant_table), EG(zend_constants)); |
|---|
| 2421 | #endif |
|---|
| 2422 | zend_hash_copy(&XG(internal_function_table), CG(function_table), NULL, &tmp_func, sizeof(tmp_func)); |
|---|
| 2423 | zend_hash_copy(&XG(internal_class_table), CG(class_table), NULL, &tmp_cest, sizeof(tmp_cest)); |
|---|
| 2424 | |
|---|
| 2425 | XG(internal_table_copied) = 1; |
|---|
| 2426 | } |
|---|
| 2427 | if (xc_php_caches && !XG(php_holds)) { |
|---|
| 2428 | XG(php_holds_size) = xc_php_hcache.size; |
|---|
| 2429 | XG(php_holds) = calloc(XG(php_holds_size), sizeof(xc_stack_t)); |
|---|
| 2430 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 2431 | xc_stack_init(&XG(php_holds[i])); |
|---|
| 2432 | } |
|---|
| 2433 | } |
|---|
| 2434 | |
|---|
| 2435 | if (xc_var_caches && !XG(var_holds)) { |
|---|
| 2436 | XG(var_holds_size) = xc_var_hcache.size; |
|---|
| 2437 | XG(var_holds) = calloc(XG(var_holds_size), sizeof(xc_stack_t)); |
|---|
| 2438 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 2439 | xc_stack_init(&XG(var_holds[i])); |
|---|
| 2440 | } |
|---|
| 2441 | } |
|---|
| 2442 | |
|---|
| 2443 | #ifdef ZEND_ENGINE_2 |
|---|
| 2444 | zend_llist_init(&XG(gc_op_arrays), sizeof(xc_gc_op_array_t), xc_gc_op_array, 0); |
|---|
| 2445 | #endif |
|---|
| 2446 | |
|---|
| 2447 | #if PHP_API_VERSION <= 20041225 |
|---|
| 2448 | XG(request_time) = time(NULL); |
|---|
| 2449 | #else |
|---|
| 2450 | XG(request_time) = sapi_get_request_time(TSRMLS_C); |
|---|
| 2451 | #endif |
|---|
| 2452 | } |
|---|
| 2453 | /* }}} */ |
|---|
| 2454 | static void xc_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 2455 | { |
|---|
| 2456 | if (!xc_cache_disabled()) { |
|---|
| 2457 | xc_entry_unholds(TSRMLS_C); |
|---|
| 2458 | xc_gc_expires_php(TSRMLS_C); |
|---|
| 2459 | xc_gc_expires_var(TSRMLS_C); |
|---|
| 2460 | xc_gc_deletes(TSRMLS_C); |
|---|
| 2461 | } |
|---|
| 2462 | #ifdef ZEND_ENGINE_2 |
|---|
| 2463 | zend_llist_destroy(&XG(gc_op_arrays)); |
|---|
| 2464 | #endif |
|---|
| 2465 | } |
|---|
| 2466 | /* }}} */ |
|---|
| 2467 | |
|---|
| 2468 | /* user functions */ |
|---|
| 2469 | static int xcache_admin_auth_check(TSRMLS_D) /* {{{ */ |
|---|
| 2470 | { |
|---|
| 2471 | zval **server = NULL; |
|---|
| 2472 | zval **user = NULL; |
|---|
| 2473 | zval **pass = NULL; |
|---|
| 2474 | char *admin_user = NULL; |
|---|
| 2475 | char *admin_pass = NULL; |
|---|
| 2476 | HashTable *ht; |
|---|
| 2477 | |
|---|
| 2478 | /* auth disabled, nothing to do.. */ |
|---|
| 2479 | if (!xc_admin_enable_auth) { |
|---|
| 2480 | return 1; |
|---|
| 2481 | } |
|---|
| 2482 | |
|---|
| 2483 | if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) { |
|---|
| 2484 | admin_user = NULL; |
|---|
| 2485 | } |
|---|
| 2486 | if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) { |
|---|
| 2487 | admin_pass = NULL; |
|---|
| 2488 | } |
|---|
| 2489 | |
|---|
| 2490 | if (admin_user == NULL || admin_pass == NULL) { |
|---|
| 2491 | php_error_docref(XCACHE_WIKI_URL "/InstallAdministration" TSRMLS_CC, E_ERROR, |
|---|
| 2492 | "xcache.admin.user and/or xcache.admin.pass settings is not configured." |
|---|
| 2493 | " Make sure you've modified the correct php ini file for your php used in webserver."); |
|---|
| 2494 | zend_bailout(); |
|---|
| 2495 | } |
|---|
| 2496 | if (strlen(admin_pass) != 32) { |
|---|
| 2497 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.pass is %lu chars unexpectedly, it is supposed to be the password after md5() which should be 32 chars", (unsigned long) strlen(admin_pass)); |
|---|
| 2498 | zend_bailout(); |
|---|
| 2499 | } |
|---|
| 2500 | |
|---|
| 2501 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 2502 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
|---|
| 2503 | #endif |
|---|
| 2504 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) { |
|---|
| 2505 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted"); |
|---|
| 2506 | zend_bailout(); |
|---|
| 2507 | } |
|---|
| 2508 | ht = HASH_OF((*server)); |
|---|
| 2509 | |
|---|
| 2510 | if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) { |
|---|
| 2511 | user = NULL; |
|---|
| 2512 | } |
|---|
| 2513 | else if (Z_TYPE_PP(user) != IS_STRING) { |
|---|
| 2514 | user = NULL; |
|---|
| 2515 | } |
|---|
| 2516 | |
|---|
| 2517 | if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) { |
|---|
| 2518 | pass = NULL; |
|---|
| 2519 | } |
|---|
| 2520 | else if (Z_TYPE_PP(pass) != IS_STRING) { |
|---|
| 2521 | pass = NULL; |
|---|
| 2522 | } |
|---|
| 2523 | |
|---|
| 2524 | if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) { |
|---|
| 2525 | PHP_MD5_CTX context; |
|---|
| 2526 | char md5str[33]; |
|---|
| 2527 | unsigned char digest[16]; |
|---|
| 2528 | |
|---|
| 2529 | PHP_MD5Init(&context); |
|---|
| 2530 | PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass)); |
|---|
| 2531 | PHP_MD5Final(digest, &context); |
|---|
| 2532 | |
|---|
| 2533 | md5str[0] = '\0'; |
|---|
| 2534 | make_digest(md5str, digest); |
|---|
| 2535 | if (strcmp(admin_pass, md5str) == 0) { |
|---|
| 2536 | return 1; |
|---|
| 2537 | } |
|---|
| 2538 | } |
|---|
| 2539 | |
|---|
| 2540 | #define STR "HTTP/1.0 401 Unauthorized" |
|---|
| 2541 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 2542 | #undef STR |
|---|
| 2543 | #define STR "WWW-authenticate: Basic Realm=\"XCache Administration\"" |
|---|
| 2544 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 2545 | #undef STR |
|---|
| 2546 | #define STR "Content-type: text/html; charset=UTF-8" |
|---|
| 2547 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 2548 | #undef STR |
|---|
| 2549 | ZEND_PUTS("<html>\n"); |
|---|
| 2550 | ZEND_PUTS("<head><title>XCache Authentication Failed</title></head>\n"); |
|---|
| 2551 | ZEND_PUTS("<body>\n"); |
|---|
| 2552 | ZEND_PUTS("<h1>XCache Authentication Failed</h1>\n"); |
|---|
| 2553 | ZEND_PUTS("<p>You're not authorized to access this page due to wrong username and/or password you typed.<br />The following check points is suggested:</p>\n"); |
|---|
| 2554 | ZEND_PUTS("<ul>\n"); |
|---|
| 2555 | ZEND_PUTS("<li>Be aware that `Username' and `Password' is case sense. Check capslock status led on your keyboard, and punch left/right Shift keys once for each</li>\n"); |
|---|
| 2556 | ZEND_PUTS("<li>Make sure the md5 password is generated correctly. You may use <a href=\"mkpassword.php\">mkpassword.php</a></li>\n"); |
|---|
| 2557 | ZEND_PUTS("<li>Reload browser cache by pressing F5 and/or Ctrl+F5, or simply clear browser cache after you've updated username/password in php ini.</li>\n"); |
|---|
| 2558 | ZEND_PUTS("</ul>\n"); |
|---|
| 2559 | ZEND_PUTS("Check <a href=\"" XCACHE_WIKI_URL "/InstallAdministration\">XCache wiki page</a> for more information.\n"); |
|---|
| 2560 | ZEND_PUTS("</body>\n"); |
|---|
| 2561 | ZEND_PUTS("</html>\n"); |
|---|
| 2562 | |
|---|
| 2563 | zend_bailout(); |
|---|
| 2564 | return 0; |
|---|
| 2565 | } |
|---|
| 2566 | /* }}} */ |
|---|
| 2567 | static void xc_clear(long type, xc_cache_t *cache TSRMLS_DC) /* {{{ */ |
|---|
| 2568 | { |
|---|
| 2569 | xc_entry_t *e, *next; |
|---|
| 2570 | int entryslotid, c; |
|---|
| 2571 | |
|---|
| 2572 | ENTER_LOCK(cache) { |
|---|
| 2573 | for (entryslotid = 0, c = cache->hentry->size; entryslotid < c; entryslotid ++) { |
|---|
| 2574 | for (e = cache->entries[entryslotid]; e; e = next) { |
|---|
| 2575 | next = e->next; |
|---|
| 2576 | xc_entry_remove_unlocked(type, cache, entryslotid, e TSRMLS_CC); |
|---|
| 2577 | } |
|---|
| 2578 | cache->entries[entryslotid] = NULL; |
|---|
| 2579 | } |
|---|
| 2580 | } LEAVE_LOCK(cache); |
|---|
| 2581 | } /* }}} */ |
|---|
| 2582 | /* {{{ xcache_admin_operate */ |
|---|
| 2583 | typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type; |
|---|
| 2584 | static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS) |
|---|
| 2585 | { |
|---|
| 2586 | long type; |
|---|
| 2587 | int size; |
|---|
| 2588 | xc_cache_t **caches, *cache; |
|---|
| 2589 | long id = 0; |
|---|
| 2590 | |
|---|
| 2591 | xcache_admin_auth_check(TSRMLS_C); |
|---|
| 2592 | |
|---|
| 2593 | if (!xc_initized || xc_cache_disabled()) { |
|---|
| 2594 | RETURN_NULL(); |
|---|
| 2595 | } |
|---|
| 2596 | |
|---|
| 2597 | switch (optype) { |
|---|
| 2598 | case XC_OP_COUNT: |
|---|
| 2599 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) { |
|---|
| 2600 | return; |
|---|
| 2601 | } |
|---|
| 2602 | break; |
|---|
| 2603 | case XC_OP_CLEAR: |
|---|
| 2604 | id = -1; |
|---|
| 2605 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &type, &id) == FAILURE) { |
|---|
| 2606 | return; |
|---|
| 2607 | } |
|---|
| 2608 | break; |
|---|
| 2609 | default: |
|---|
| 2610 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) { |
|---|
| 2611 | return; |
|---|
| 2612 | } |
|---|
| 2613 | } |
|---|
| 2614 | |
|---|
| 2615 | switch (type) { |
|---|
| 2616 | case XC_TYPE_PHP: |
|---|
| 2617 | size = xc_php_hcache.size; |
|---|
| 2618 | caches = xc_php_caches; |
|---|
| 2619 | break; |
|---|
| 2620 | |
|---|
| 2621 | case XC_TYPE_VAR: |
|---|
| 2622 | size = xc_var_hcache.size; |
|---|
| 2623 | caches = xc_var_caches; |
|---|
| 2624 | break; |
|---|
| 2625 | |
|---|
| 2626 | default: |
|---|
| 2627 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type); |
|---|
| 2628 | RETURN_FALSE; |
|---|
| 2629 | } |
|---|
| 2630 | |
|---|
| 2631 | switch (optype) { |
|---|
| 2632 | case XC_OP_COUNT: |
|---|
| 2633 | RETURN_LONG(caches ? size : 0) |
|---|
| 2634 | break; |
|---|
| 2635 | |
|---|
| 2636 | case XC_OP_INFO: |
|---|
| 2637 | case XC_OP_LIST: |
|---|
| 2638 | if (!caches || id < 0 || id >= size) { |
|---|
| 2639 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 2640 | RETURN_FALSE; |
|---|
| 2641 | } |
|---|
| 2642 | |
|---|
| 2643 | array_init(return_value); |
|---|
| 2644 | |
|---|
| 2645 | cache = caches[id]; |
|---|
| 2646 | ENTER_LOCK(cache) { |
|---|
| 2647 | if (optype == XC_OP_INFO) { |
|---|
| 2648 | xc_fillinfo_unlocked(type, cache, return_value TSRMLS_CC); |
|---|
| 2649 | } |
|---|
| 2650 | else { |
|---|
| 2651 | xc_filllist_unlocked(type, cache, return_value TSRMLS_CC); |
|---|
| 2652 | } |
|---|
| 2653 | } LEAVE_LOCK(cache); |
|---|
| 2654 | break; |
|---|
| 2655 | |
|---|
| 2656 | case XC_OP_CLEAR: |
|---|
| 2657 | if (!caches || id < -1 || id >= size) { |
|---|
| 2658 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 2659 | RETURN_FALSE; |
|---|
| 2660 | } |
|---|
| 2661 | |
|---|
| 2662 | if (id == -1) { |
|---|
| 2663 | for (id = 0; id < size; ++id) { |
|---|
| 2664 | xc_clear(type, caches[id] TSRMLS_CC); |
|---|
| 2665 | } |
|---|
| 2666 | } |
|---|
| 2667 | else { |
|---|
| 2668 | xc_clear(type, caches[id] TSRMLS_CC); |
|---|
| 2669 | } |
|---|
| 2670 | |
|---|
| 2671 | xc_gc_deletes(TSRMLS_C); |
|---|
| 2672 | break; |
|---|
| 2673 | |
|---|
| 2674 | default: |
|---|
| 2675 | assert(0); |
|---|
| 2676 | } |
|---|
| 2677 | } |
|---|
| 2678 | /* }}} */ |
|---|
| 2679 | /* {{{ proto int xcache_count(int type) |
|---|
| 2680 | Return count of cache on specified cache type */ |
|---|
| 2681 | PHP_FUNCTION(xcache_count) |
|---|
| 2682 | { |
|---|
| 2683 | xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 2684 | } |
|---|
| 2685 | /* }}} */ |
|---|
| 2686 | /* {{{ proto array xcache_info(int type, int id) |
|---|
| 2687 | Get cache info by id on specified cache type */ |
|---|
| 2688 | PHP_FUNCTION(xcache_info) |
|---|
| 2689 | { |
|---|
| 2690 | xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 2691 | } |
|---|
| 2692 | /* }}} */ |
|---|
| 2693 | /* {{{ proto array xcache_list(int type, int id) |
|---|
| 2694 | Get cache entries list by id on specified cache type */ |
|---|
| 2695 | PHP_FUNCTION(xcache_list) |
|---|
| 2696 | { |
|---|
| 2697 | xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 2698 | } |
|---|
| 2699 | /* }}} */ |
|---|
| 2700 | /* {{{ proto array xcache_clear_cache(int type, [ int id = -1 ]) |
|---|
| 2701 | Clear cache by id on specified cache type */ |
|---|
| 2702 | PHP_FUNCTION(xcache_clear_cache) |
|---|
| 2703 | { |
|---|
| 2704 | xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 2705 | } |
|---|
| 2706 | /* }}} */ |
|---|
| 2707 | |
|---|
| 2708 | #define VAR_DISABLED_WARNING() do { \ |
|---|
| 2709 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "XCache var cache was not initialized properly. Check php log for actual reason"); \ |
|---|
| 2710 | } while (0) |
|---|
| 2711 | |
|---|
| 2712 | static int xc_entry_var_init_key(xc_entry_var_t *entry_var, xc_entry_hash_t *entry_hash, zval *name TSRMLS_DC) /* {{{ */ |
|---|
| 2713 | { |
|---|
| 2714 | xc_hash_value_t hv; |
|---|
| 2715 | |
|---|
| 2716 | switch (name->type) { |
|---|
| 2717 | #ifdef IS_UNICODE |
|---|
| 2718 | case IS_UNICODE: |
|---|
| 2719 | case IS_STRING: |
|---|
| 2720 | #endif |
|---|
| 2721 | default: |
|---|
| 2722 | #ifdef IS_UNICODE |
|---|
| 2723 | convert_to_unicode(name); |
|---|
| 2724 | #else |
|---|
| 2725 | convert_to_string(name); |
|---|
| 2726 | #endif |
|---|
| 2727 | } |
|---|
| 2728 | |
|---|
| 2729 | #ifdef IS_UNICODE |
|---|
| 2730 | entry_var->name_type = name->type; |
|---|
| 2731 | #endif |
|---|
| 2732 | entry_var->entry.name = name->value; |
|---|
| 2733 | |
|---|
| 2734 | hv = xc_entry_hash_var((xc_entry_t *) entry_var TSRMLS_CC); |
|---|
| 2735 | |
|---|
| 2736 | entry_hash->cacheid = (hv & xc_var_hcache.mask); |
|---|
| 2737 | hv >>= xc_var_hcache.bits; |
|---|
| 2738 | entry_hash->entryslotid = (hv & xc_var_hentry.mask); |
|---|
| 2739 | return SUCCESS; |
|---|
| 2740 | } |
|---|
| 2741 | /* }}} */ |
|---|
| 2742 | /* {{{ proto mixed xcache_get(string name) |
|---|
| 2743 | Get cached data by specified name */ |
|---|
| 2744 | PHP_FUNCTION(xcache_get) |
|---|
| 2745 | { |
|---|
| 2746 | xc_entry_hash_t entry_hash; |
|---|
| 2747 | xc_cache_t *cache; |
|---|
| 2748 | xc_entry_var_t entry_var, *stored_entry_var; |
|---|
| 2749 | zval *name; |
|---|
| 2750 | |
|---|
| 2751 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2752 | VAR_DISABLED_WARNING(); |
|---|
| 2753 | RETURN_NULL(); |
|---|
| 2754 | } |
|---|
| 2755 | |
|---|
| 2756 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 2757 | return; |
|---|
| 2758 | } |
|---|
| 2759 | xc_entry_var_init_key(&entry_var, &entry_hash, name TSRMLS_CC); |
|---|
| 2760 | cache = xc_var_caches[entry_hash.cacheid]; |
|---|
| 2761 | |
|---|
| 2762 | ENTER_LOCK(cache) { |
|---|
| 2763 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
|---|
| 2764 | if (stored_entry_var) { |
|---|
| 2765 | /* return */ |
|---|
| 2766 | xc_processor_restore_zval(return_value, stored_entry_var->value, stored_entry_var->have_references TSRMLS_CC); |
|---|
| 2767 | xc_cache_hit_unlocked(cache TSRMLS_CC); |
|---|
| 2768 | } |
|---|
| 2769 | else { |
|---|
| 2770 | RETVAL_NULL(); |
|---|
| 2771 | } |
|---|
| 2772 | } LEAVE_LOCK(cache); |
|---|
| 2773 | } |
|---|
| 2774 | /* }}} */ |
|---|
| 2775 | /* {{{ proto bool xcache_set(string name, mixed value [, int ttl]) |
|---|
| 2776 | Store data to cache by specified name */ |
|---|
| 2777 | PHP_FUNCTION(xcache_set) |
|---|
| 2778 | { |
|---|
| 2779 | xc_entry_hash_t entry_hash; |
|---|
| 2780 | xc_cache_t *cache; |
|---|
| 2781 | xc_entry_var_t entry_var, *stored_entry_var; |
|---|
| 2782 | zval *name; |
|---|
| 2783 | zval *value; |
|---|
| 2784 | |
|---|
| 2785 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2786 | VAR_DISABLED_WARNING(); |
|---|
| 2787 | RETURN_NULL(); |
|---|
| 2788 | } |
|---|
| 2789 | |
|---|
| 2790 | entry_var.entry.ttl = XG(var_ttl); |
|---|
| 2791 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &name, &value, &entry_var.entry.ttl) == FAILURE) { |
|---|
| 2792 | return; |
|---|
| 2793 | } |
|---|
| 2794 | |
|---|
| 2795 | if (Z_TYPE_P(value) == IS_OBJECT) { |
|---|
| 2796 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "Objects cannot be stored in the variable cache. Use serialize before xcache_set"); |
|---|
| 2797 | RETURN_NULL(); |
|---|
| 2798 | } |
|---|
| 2799 | |
|---|
| 2800 | /* max ttl */ |
|---|
| 2801 | if (xc_var_maxttl && (!entry_var.entry.ttl || entry_var.entry.ttl > xc_var_maxttl)) { |
|---|
| 2802 | entry_var.entry.ttl = xc_var_maxttl; |
|---|
| 2803 | } |
|---|
| 2804 | |
|---|
| 2805 | xc_entry_var_init_key(&entry_var, &entry_hash, name TSRMLS_CC); |
|---|
| 2806 | cache = xc_var_caches[entry_hash.cacheid]; |
|---|
| 2807 | |
|---|
| 2808 | ENTER_LOCK(cache) { |
|---|
| 2809 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
|---|
| 2810 | if (stored_entry_var) { |
|---|
| 2811 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
|---|
| 2812 | } |
|---|
| 2813 | entry_var.value = value; |
|---|
| 2814 | RETVAL_BOOL(xc_entry_var_store_unlocked(cache, entry_hash.entryslotid, &entry_var TSRMLS_CC) != NULL ? 1 : 0); |
|---|
| 2815 | } LEAVE_LOCK(cache); |
|---|
| 2816 | } |
|---|
| 2817 | /* }}} */ |
|---|
| 2818 | /* {{{ proto bool xcache_isset(string name) |
|---|
| 2819 | Check if an entry exists in cache by specified name */ |
|---|
| 2820 | PHP_FUNCTION(xcache_isset) |
|---|
| 2821 | { |
|---|
| 2822 | xc_entry_hash_t entry_hash; |
|---|
| 2823 | xc_cache_t *cache; |
|---|
| 2824 | xc_entry_var_t entry_var, *stored_entry_var; |
|---|
| 2825 | zval *name; |
|---|
| 2826 | |
|---|
| 2827 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2828 | VAR_DISABLED_WARNING(); |
|---|
| 2829 | RETURN_FALSE; |
|---|
| 2830 | } |
|---|
| 2831 | |
|---|
| 2832 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 2833 | return; |
|---|
| 2834 | } |
|---|
| 2835 | xc_entry_var_init_key(&entry_var, &entry_hash, name TSRMLS_CC); |
|---|
| 2836 | cache = xc_var_caches[entry_hash.cacheid]; |
|---|
| 2837 | |
|---|
| 2838 | ENTER_LOCK(cache) { |
|---|
| 2839 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
|---|
| 2840 | if (stored_entry_var) { |
|---|
| 2841 | xc_cache_hit_unlocked(cache TSRMLS_CC); |
|---|
| 2842 | RETVAL_TRUE; |
|---|
| 2843 | /* return */ |
|---|
| 2844 | } |
|---|
| 2845 | else { |
|---|
| 2846 | RETVAL_FALSE; |
|---|
| 2847 | } |
|---|
| 2848 | |
|---|
| 2849 | } LEAVE_LOCK(cache); |
|---|
| 2850 | } |
|---|
| 2851 | /* }}} */ |
|---|
| 2852 | /* {{{ proto bool xcache_unset(string name) |
|---|
| 2853 | Unset existing data in cache by specified name */ |
|---|
| 2854 | PHP_FUNCTION(xcache_unset) |
|---|
| 2855 | { |
|---|
| 2856 | xc_entry_hash_t entry_hash; |
|---|
| 2857 | xc_cache_t *cache; |
|---|
| 2858 | xc_entry_var_t entry_var, *stored_entry_var; |
|---|
| 2859 | zval *name; |
|---|
| 2860 | |
|---|
| 2861 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2862 | VAR_DISABLED_WARNING(); |
|---|
| 2863 | RETURN_FALSE; |
|---|
| 2864 | } |
|---|
| 2865 | |
|---|
| 2866 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 2867 | return; |
|---|
| 2868 | } |
|---|
| 2869 | xc_entry_var_init_key(&entry_var, &entry_hash, name TSRMLS_CC); |
|---|
| 2870 | cache = xc_var_caches[entry_hash.cacheid]; |
|---|
| 2871 | |
|---|
| 2872 | ENTER_LOCK(cache) { |
|---|
| 2873 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
|---|
| 2874 | if (stored_entry_var) { |
|---|
| 2875 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
|---|
| 2876 | RETVAL_TRUE; |
|---|
| 2877 | } |
|---|
| 2878 | else { |
|---|
| 2879 | RETVAL_FALSE; |
|---|
| 2880 | } |
|---|
| 2881 | } LEAVE_LOCK(cache); |
|---|
| 2882 | } |
|---|
| 2883 | /* }}} */ |
|---|
| 2884 | /* {{{ proto bool xcache_unset_by_prefix(string prefix) |
|---|
| 2885 | Unset existing data in cache by specified prefix */ |
|---|
| 2886 | PHP_FUNCTION(xcache_unset_by_prefix) |
|---|
| 2887 | { |
|---|
| 2888 | zval *prefix; |
|---|
| 2889 | int i, iend; |
|---|
| 2890 | |
|---|
| 2891 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2892 | VAR_DISABLED_WARNING(); |
|---|
| 2893 | RETURN_FALSE; |
|---|
| 2894 | } |
|---|
| 2895 | |
|---|
| 2896 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &prefix) == FAILURE) { |
|---|
| 2897 | return; |
|---|
| 2898 | } |
|---|
| 2899 | |
|---|
| 2900 | for (i = 0, iend = xc_var_hcache.size; i < iend; i ++) { |
|---|
| 2901 | xc_cache_t *cache = xc_var_caches[i]; |
|---|
| 2902 | ENTER_LOCK(cache) { |
|---|
| 2903 | int entryslotid, jend; |
|---|
| 2904 | for (entryslotid = 0, jend = cache->hentry->size; entryslotid < jend; entryslotid ++) { |
|---|
| 2905 | xc_entry_t *entry, *next; |
|---|
| 2906 | for (entry = cache->entries[entryslotid]; entry; entry = next) { |
|---|
| 2907 | next = entry->next; |
|---|
| 2908 | if (xc_entry_has_prefix_unlocked(XC_TYPE_VAR, entry, prefix)) { |
|---|
| 2909 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entryslotid, entry TSRMLS_CC); |
|---|
| 2910 | } |
|---|
| 2911 | } |
|---|
| 2912 | } |
|---|
| 2913 | } LEAVE_LOCK(cache); |
|---|
| 2914 | } |
|---|
| 2915 | } |
|---|
| 2916 | /* }}} */ |
|---|
| 2917 | static inline void xc_var_inc_dec(int inc, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ |
|---|
| 2918 | { |
|---|
| 2919 | xc_entry_hash_t entry_hash; |
|---|
| 2920 | xc_cache_t *cache; |
|---|
| 2921 | xc_entry_var_t entry_var, *stored_entry_var; |
|---|
| 2922 | zval *name; |
|---|
| 2923 | long count = 1; |
|---|
| 2924 | long value = 0; |
|---|
| 2925 | zval oldzval; |
|---|
| 2926 | |
|---|
| 2927 | if (!xc_var_caches || xc_cache_disabled()) { |
|---|
| 2928 | VAR_DISABLED_WARNING(); |
|---|
| 2929 | RETURN_NULL(); |
|---|
| 2930 | } |
|---|
| 2931 | |
|---|
| 2932 | entry_var.entry.ttl = XG(var_ttl); |
|---|
| 2933 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &name, &count, &entry_var.entry.ttl) == FAILURE) { |
|---|
| 2934 | return; |
|---|
| 2935 | } |
|---|
| 2936 | |
|---|
| 2937 | /* max ttl */ |
|---|
| 2938 | if (xc_var_maxttl && (!entry_var.entry.ttl || entry_var.entry.ttl > xc_var_maxttl)) { |
|---|
| 2939 | entry_var.entry.ttl = xc_var_maxttl; |
|---|
| 2940 | } |
|---|
| 2941 | |
|---|
| 2942 | xc_entry_var_init_key(&entry_var, &entry_hash, name TSRMLS_CC); |
|---|
| 2943 | cache = xc_var_caches[entry_hash.cacheid]; |
|---|
| 2944 | |
|---|
| 2945 | ENTER_LOCK(cache) { |
|---|
| 2946 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
|---|
| 2947 | if (stored_entry_var) { |
|---|
| 2948 | TRACE("incdec: got entry_var %s", entry_var.entry.name.str.val); |
|---|
| 2949 | /* do it in place */ |
|---|
| 2950 | if (Z_TYPE_P(stored_entry_var->value) == IS_LONG) { |
|---|
| 2951 | zval *zv; |
|---|
| 2952 | stored_entry_var->entry.ctime = XG(request_time); |
|---|
| 2953 | stored_entry_var->entry.ttl = entry_var.entry.ttl; |
|---|
| 2954 | TRACE("%s", "incdec: islong"); |
|---|
| 2955 | value = Z_LVAL_P(stored_entry_var->value); |
|---|
| 2956 | value += (inc == 1 ? count : - count); |
|---|
| 2957 | RETVAL_LONG(value); |
|---|
| 2958 | |
|---|
| 2959 | zv = (zval *) cache->shm->handlers->to_readwrite(cache->shm, (char *) stored_entry_var->value); |
|---|
| 2960 | Z_LVAL_P(zv) = value; |
|---|
| 2961 | ++cache->updates; |
|---|
| 2962 | break; /* leave lock */ |
|---|
| 2963 | } |
|---|
| 2964 | |
|---|
| 2965 | TRACE("%s", "incdec: notlong"); |
|---|
| 2966 | xc_processor_restore_zval(&oldzval, stored_entry_var->value, stored_entry_var->have_references TSRMLS_CC); |
|---|
| 2967 | convert_to_long(&oldzval); |
|---|
| 2968 | value = Z_LVAL(oldzval); |
|---|
| 2969 | zval_dtor(&oldzval); |
|---|
| 2970 | } |
|---|
| 2971 | else { |
|---|
| 2972 | TRACE("incdec: %s not found", entry_var.entry.name.str.val); |
|---|
| 2973 | } |
|---|
| 2974 | |
|---|
| 2975 | value += (inc == 1 ? count : - count); |
|---|
| 2976 | RETVAL_LONG(value); |
|---|
| 2977 | entry_var.value = return_value; |
|---|
| 2978 | |
|---|
| 2979 | if (stored_entry_var) { |
|---|
| 2980 | entry_var.entry.atime = stored_entry_var->entry.atime; |
|---|
| 2981 | entry_var.entry.ctime = stored_entry_var->entry.ctime; |
|---|
| 2982 | entry_var.entry.hits = stored_entry_var->entry.hits; |
|---|
| 2983 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
|---|
| 2984 | } |
|---|
| 2985 | xc_entry_var_store_unlocked(cache, entry_hash.entryslotid, &entry_var TSRMLS_CC); |
|---|
| 2986 | } LEAVE_LOCK(cache); |
|---|
| 2987 | } |
|---|
| 2988 | /* }}} */ |
|---|
| 2989 | /* {{{ proto int xcache_inc(string name [, int value [, int ttl]]) |
|---|
| 2990 | Increase an int counter in cache by specified name, create it if not exists */ |
|---|
| 2991 | PHP_FUNCTION(xcache_inc) |
|---|
| 2992 | { |
|---|
| 2993 | xc_var_inc_dec(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 2994 | } |
|---|
| 2995 | /* }}} */ |
|---|
| 2996 | /* {{{ proto int xcache_dec(string name [, int value [, int ttl]]) |
|---|
| 2997 | Decrease an int counter in cache by specified name, create it if not exists */ |
|---|
| 2998 | PHP_FUNCTION(xcache_dec) |
|---|
| 2999 | { |
|---|
| 3000 | xc_var_inc_dec(-1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 3001 | } |
|---|
| 3002 | /* }}} */ |
|---|
| 3003 | static zend_function_entry xcache_cacher_functions[] = /* {{{ */ |
|---|
| 3004 | { |
|---|
| 3005 | PHP_FE(xcache_count, NULL) |
|---|
| 3006 | PHP_FE(xcache_info, NULL) |
|---|
| 3007 | PHP_FE(xcache_list, NULL) |
|---|
| 3008 | PHP_FE(xcache_clear_cache, NULL) |
|---|
| 3009 | PHP_FE(xcache_get, NULL) |
|---|
| 3010 | PHP_FE(xcache_set, NULL) |
|---|
| 3011 | PHP_FE(xcache_isset, NULL) |
|---|
| 3012 | PHP_FE(xcache_unset, NULL) |
|---|
| 3013 | PHP_FE(xcache_unset_by_prefix, NULL) |
|---|
| 3014 | PHP_FE(xcache_inc, NULL) |
|---|
| 3015 | PHP_FE(xcache_dec, NULL) |
|---|
| 3016 | PHP_FE_END |
|---|
| 3017 | }; |
|---|
| 3018 | /* }}} */ |
|---|
| 3019 | |
|---|
| 3020 | static int xc_cacher_zend_startup(zend_extension *extension) /* {{{ */ |
|---|
| 3021 | { |
|---|
| 3022 | if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { |
|---|
| 3023 | if (xc_init() != SUCCESS) { |
|---|
| 3024 | zend_error(E_ERROR, "XCache: Cannot init"); |
|---|
| 3025 | return FAILURE; |
|---|
| 3026 | } |
|---|
| 3027 | xc_initized = 1; |
|---|
| 3028 | xc_init_time = time(NULL); |
|---|
| 3029 | #ifdef PHP_WIN32 |
|---|
| 3030 | xc_init_instance_id = GetCurrentProcessId(); |
|---|
| 3031 | #else |
|---|
| 3032 | xc_init_instance_id = getpid(); |
|---|
| 3033 | #endif |
|---|
| 3034 | #ifdef ZTS |
|---|
| 3035 | xc_init_instance_subid = tsrm_thread_id(); |
|---|
| 3036 | #endif |
|---|
| 3037 | } |
|---|
| 3038 | |
|---|
| 3039 | if (xc_php_size) { |
|---|
| 3040 | old_compile_file = zend_compile_file; |
|---|
| 3041 | zend_compile_file = xc_compile_file; |
|---|
| 3042 | } |
|---|
| 3043 | |
|---|
| 3044 | return SUCCESS; |
|---|
| 3045 | } |
|---|
| 3046 | /* }}} */ |
|---|
| 3047 | static void xc_cacher_zend_shutdown(zend_extension *extension) /* {{{ */ |
|---|
| 3048 | { |
|---|
| 3049 | if (xc_initized) { |
|---|
| 3050 | xc_destroy(); |
|---|
| 3051 | } |
|---|
| 3052 | } |
|---|
| 3053 | /* }}} */ |
|---|
| 3054 | /* {{{ zend extension definition structure */ |
|---|
| 3055 | static zend_extension xc_cacher_zend_extension_entry = { |
|---|
| 3056 | XCACHE_NAME " Cacher", |
|---|
| 3057 | XCACHE_VERSION, |
|---|
| 3058 | XCACHE_AUTHOR, |
|---|
| 3059 | XCACHE_URL, |
|---|
| 3060 | XCACHE_COPYRIGHT, |
|---|
| 3061 | xc_cacher_zend_startup, |
|---|
| 3062 | xc_cacher_zend_shutdown, |
|---|
| 3063 | NULL, /* activate_func_t */ |
|---|
| 3064 | NULL, /* deactivate_func_t */ |
|---|
| 3065 | NULL, /* message_handler_func_t */ |
|---|
| 3066 | NULL, /* op_array_handler_func_t */ |
|---|
| 3067 | NULL, /* statement_handler_func_t */ |
|---|
| 3068 | NULL, /* fcall_begin_handler_func_t */ |
|---|
| 3069 | NULL, /* fcall_end_handler_func_t */ |
|---|
| 3070 | NULL, /* op_array_ctor_func_t */ |
|---|
| 3071 | NULL, /* op_array_dtor_func_t */ |
|---|
| 3072 | STANDARD_ZEND_EXTENSION_PROPERTIES |
|---|
| 3073 | }; |
|---|
| 3074 | /* }}} */ |
|---|
| 3075 | |
|---|
| 3076 | /* {{{ ini */ |
|---|
| 3077 | #ifdef ZEND_WIN32 |
|---|
| 3078 | # define DEFAULT_PATH "xcache" |
|---|
| 3079 | #else |
|---|
| 3080 | # define DEFAULT_PATH "/dev/zero" |
|---|
| 3081 | #endif |
|---|
| 3082 | PHP_INI_BEGIN() |
|---|
| 3083 | PHP_INI_ENTRY1 ("xcache.mmap_path", DEFAULT_PATH, PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_mmap_path) |
|---|
| 3084 | PHP_INI_ENTRY1 ("xcache.readonly_protection", "0", PHP_INI_SYSTEM, xcache_OnUpdateBool, &xc_readonly_protection) |
|---|
| 3085 | /* opcode cache */ |
|---|
| 3086 | PHP_INI_ENTRY1 ("xcache.admin.enable_auth", "1", PHP_INI_SYSTEM, xcache_OnUpdateBool, &xc_admin_enable_auth) |
|---|
| 3087 | PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3088 | PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3089 | PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3090 | PHP_INI_ENTRY1 ("xcache.shm_scheme", "mmap", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_shm_scheme) |
|---|
| 3091 | PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_ttl) |
|---|
| 3092 | PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_gc_interval) |
|---|
| 3093 | STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals) |
|---|
| 3094 | STD_PHP_INI_BOOLEAN("xcache.stat", "1", PHP_INI_ALL, OnUpdateBool, stat, zend_xcache_globals, xcache_globals) |
|---|
| 3095 | /* var cache */ |
|---|
| 3096 | PHP_INI_ENTRY1 ("xcache.var_size", "0", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3097 | PHP_INI_ENTRY1 ("xcache.var_count", "1", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3098 | PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 3099 | PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_maxttl) |
|---|
| 3100 | PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_gc_interval) |
|---|
| 3101 | STD_PHP_INI_ENTRY ("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals) |
|---|
| 3102 | PHP_INI_END() |
|---|
| 3103 | /* }}} */ |
|---|
| 3104 | static PHP_MINFO_FUNCTION(xcache_cacher) /* {{{ */ |
|---|
| 3105 | { |
|---|
| 3106 | char buf[100]; |
|---|
| 3107 | char *ptr; |
|---|
| 3108 | int left, len; |
|---|
| 3109 | xc_shm_scheme_t *scheme; |
|---|
| 3110 | |
|---|
| 3111 | php_info_print_table_start(); |
|---|
| 3112 | php_info_print_table_row(2, "Readonly Protection", xc_readonly_protection ? "enabled" : "disabled"); |
|---|
| 3113 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 3114 | ptr = php_format_date("Y-m-d H:i:s", sizeof("Y-m-d H:i:s") - 1, xc_init_time, 1 TSRMLS_CC); |
|---|
| 3115 | php_info_print_table_row(2, "Cache Init Time", ptr); |
|---|
| 3116 | efree(ptr); |
|---|
| 3117 | #else |
|---|
| 3118 | snprintf(buf, sizeof(buf), "%lu", (long unsigned) xc_init_time); |
|---|
| 3119 | php_info_print_table_row(2, "Cache Init Time", buf); |
|---|
| 3120 | #endif |
|---|
| 3121 | |
|---|
| 3122 | #ifdef ZTS |
|---|
| 3123 | snprintf(buf, sizeof(buf), "%lu.%lu", xc_init_instance_id, xc_init_instance_subid); |
|---|
| 3124 | #else |
|---|
| 3125 | snprintf(buf, sizeof(buf), "%lu", xc_init_instance_id); |
|---|
| 3126 | #endif |
|---|
| 3127 | php_info_print_table_row(2, "Cache Instance Id", buf); |
|---|
| 3128 | |
|---|
| 3129 | if (xc_php_size) { |
|---|
| 3130 | ptr = _php_math_number_format(xc_php_size, 0, '.', ','); |
|---|
| 3131 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %lu split(s), with %lu slots each", ptr, xc_php_hcache.size, xc_php_hentry.size); |
|---|
| 3132 | php_info_print_table_row(2, "Opcode Cache", buf); |
|---|
| 3133 | efree(ptr); |
|---|
| 3134 | } |
|---|
| 3135 | else { |
|---|
| 3136 | php_info_print_table_row(2, "Opcode Cache", "disabled"); |
|---|
| 3137 | } |
|---|
| 3138 | if (xc_var_size) { |
|---|
| 3139 | ptr = _php_math_number_format(xc_var_size, 0, '.', ','); |
|---|
| 3140 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %lu split(s), with %lu slots each", ptr, xc_var_hcache.size, xc_var_hentry.size); |
|---|
| 3141 | php_info_print_table_row(2, "Variable Cache", buf); |
|---|
| 3142 | efree(ptr); |
|---|
| 3143 | } |
|---|
| 3144 | else { |
|---|
| 3145 | php_info_print_table_row(2, "Variable Cache", "disabled"); |
|---|
| 3146 | } |
|---|
| 3147 | |
|---|
| 3148 | left = sizeof(buf); |
|---|
| 3149 | ptr = buf; |
|---|
| 3150 | buf[0] = '\0'; |
|---|
| 3151 | for (scheme = xc_shm_scheme_first(); scheme; scheme = xc_shm_scheme_next(scheme)) { |
|---|
| 3152 | len = snprintf(ptr, left, ptr == buf ? "%s" : ", %s", xc_shm_scheme_name(scheme)); |
|---|
| 3153 | left -= len; |
|---|
| 3154 | ptr += len; |
|---|
| 3155 | } |
|---|
| 3156 | php_info_print_table_row(2, "Shared Memory Schemes", buf); |
|---|
| 3157 | |
|---|
| 3158 | php_info_print_table_end(); |
|---|
| 3159 | |
|---|
| 3160 | DISPLAY_INI_ENTRIES(); |
|---|
| 3161 | } |
|---|
| 3162 | /* }}} */ |
|---|
| 3163 | static int xc_config_hash(xc_hash_t *p, char *name, char *default_value) /* {{{ */ |
|---|
| 3164 | { |
|---|
| 3165 | size_t bits, size; |
|---|
| 3166 | char *value; |
|---|
| 3167 | |
|---|
| 3168 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 3169 | value = default_value; |
|---|
| 3170 | } |
|---|
| 3171 | |
|---|
| 3172 | p->size = zend_atoi(value, strlen(value)); |
|---|
| 3173 | for (size = 1, bits = 1; size < p->size; bits ++, size <<= 1) { |
|---|
| 3174 | /* empty body */ |
|---|
| 3175 | } |
|---|
| 3176 | p->size = size; |
|---|
| 3177 | p->bits = bits; |
|---|
| 3178 | p->mask = size - 1; |
|---|
| 3179 | |
|---|
| 3180 | return SUCCESS; |
|---|
| 3181 | } |
|---|
| 3182 | /* }}} */ |
|---|
| 3183 | static int xc_config_long(zend_ulong *p, char *name, char *default_value) /* {{{ */ |
|---|
| 3184 | { |
|---|
| 3185 | char *value; |
|---|
| 3186 | |
|---|
| 3187 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 3188 | value = default_value; |
|---|
| 3189 | } |
|---|
| 3190 | |
|---|
| 3191 | *p = zend_atol(value, strlen(value)); |
|---|
| 3192 | return SUCCESS; |
|---|
| 3193 | } |
|---|
| 3194 | /* }}} */ |
|---|
| 3195 | static PHP_MINIT_FUNCTION(xcache_cacher) /* {{{ */ |
|---|
| 3196 | { |
|---|
| 3197 | char *env; |
|---|
| 3198 | zend_extension *ext; |
|---|
| 3199 | zend_llist_position lpos; |
|---|
| 3200 | |
|---|
| 3201 | ext = zend_get_extension("Zend Optimizer"); |
|---|
| 3202 | if (ext) { |
|---|
| 3203 | /* zend_optimizer.optimization_level>0 is not compatible with other cacher, disabling */ |
|---|
| 3204 | ext->op_array_handler = NULL; |
|---|
| 3205 | } |
|---|
| 3206 | /* cache if there's an op_array_ctor */ |
|---|
| 3207 | for (ext = zend_llist_get_first_ex(&zend_extensions, &lpos); |
|---|
| 3208 | ext; |
|---|
| 3209 | ext = zend_llist_get_next_ex(&zend_extensions, &lpos)) { |
|---|
| 3210 | if (ext->op_array_ctor) { |
|---|
| 3211 | xc_have_op_array_ctor = 1; |
|---|
| 3212 | break; |
|---|
| 3213 | } |
|---|
| 3214 | } |
|---|
| 3215 | |
|---|
| 3216 | /* additional_functions requires PHP 5.3. TODO: find simpler way to do it */ |
|---|
| 3217 | #ifdef ZEND_ENGINE_2_3 |
|---|
| 3218 | if (strcmp(sapi_module.name, "cgi-fcgi") == 0 && !sapi_module.additional_functions && !getenv("XCACHE_SKIP_FCGI_WARNING") && !getenv("GATEWAY_INTERFACE")) { |
|---|
| 3219 | if ((getenv("PHP_FCGI_CHILDREN") == NULL) || (atoi(getenv("PHP_FCGI_CHILDREN")) < 1)) { |
|---|
| 3220 | zend_error(E_WARNING, "PHP_FCGI_CHILDREN should be >= 1 and use 1 group of parent/childs model. Set XCACHE_SKIP_FCGI_WARNING=1 to skip this warning. See " XCACHE_WIKI_URL "/Faq"); |
|---|
| 3221 | } |
|---|
| 3222 | } |
|---|
| 3223 | #endif |
|---|
| 3224 | |
|---|
| 3225 | xc_config_long(&xc_php_size, "xcache.size", "0"); |
|---|
| 3226 | xc_config_hash(&xc_php_hcache, "xcache.count", "1"); |
|---|
| 3227 | xc_config_hash(&xc_php_hentry, "xcache.slots", "8K"); |
|---|
| 3228 | |
|---|
| 3229 | xc_config_long(&xc_var_size, "xcache.var_size", "0"); |
|---|
| 3230 | xc_config_hash(&xc_var_hcache, "xcache.var_count", "1"); |
|---|
| 3231 | xc_config_hash(&xc_var_hentry, "xcache.var_slots", "8K"); |
|---|
| 3232 | |
|---|
| 3233 | if (strcmp(sapi_module.name, "cli") == 0) { |
|---|
| 3234 | if ((env = getenv("XCACHE_TEST")) != NULL) { |
|---|
| 3235 | xc_test = atoi(env); |
|---|
| 3236 | } |
|---|
| 3237 | if (!xc_test) { |
|---|
| 3238 | /* disable cache for cli except for testing */ |
|---|
| 3239 | xc_php_size = xc_var_size = 0; |
|---|
| 3240 | } |
|---|
| 3241 | } |
|---|
| 3242 | |
|---|
| 3243 | if (xc_php_size <= 0) { |
|---|
| 3244 | xc_php_size = xc_php_hcache.size = 0; |
|---|
| 3245 | } |
|---|
| 3246 | if (xc_var_size <= 0) { |
|---|
| 3247 | xc_var_size = xc_var_hcache.size = 0; |
|---|
| 3248 | } |
|---|
| 3249 | |
|---|
| 3250 | xc_init_constant(module_number TSRMLS_CC); |
|---|
| 3251 | |
|---|
| 3252 | REGISTER_INI_ENTRIES(); |
|---|
| 3253 | |
|---|
| 3254 | xc_sandbox_module_init(module_number TSRMLS_CC); |
|---|
| 3255 | return xcache_zend_extension_register(&xc_cacher_zend_extension_entry, 0); |
|---|
| 3256 | |
|---|
| 3257 | err_init: |
|---|
| 3258 | return FAILURE; |
|---|
| 3259 | } |
|---|
| 3260 | /* }}} */ |
|---|
| 3261 | static PHP_MSHUTDOWN_FUNCTION(xcache_cacher) /* {{{ */ |
|---|
| 3262 | { |
|---|
| 3263 | xc_sandbox_module_shutdown(); |
|---|
| 3264 | |
|---|
| 3265 | xcache_zend_extension_unregister(&xc_cacher_zend_extension_entry); |
|---|
| 3266 | UNREGISTER_INI_ENTRIES(); |
|---|
| 3267 | |
|---|
| 3268 | if (xc_mmap_path) { |
|---|
| 3269 | pefree(xc_mmap_path, 1); |
|---|
| 3270 | xc_mmap_path = NULL; |
|---|
| 3271 | } |
|---|
| 3272 | if (xc_shm_scheme) { |
|---|
| 3273 | pefree(xc_shm_scheme, 1); |
|---|
| 3274 | xc_shm_scheme = NULL; |
|---|
| 3275 | } |
|---|
| 3276 | |
|---|
| 3277 | return SUCCESS; |
|---|
| 3278 | } |
|---|
| 3279 | /* }}} */ |
|---|
| 3280 | static PHP_RINIT_FUNCTION(xcache_cacher) /* {{{ */ |
|---|
| 3281 | { |
|---|
| 3282 | xc_request_init(TSRMLS_C); |
|---|
| 3283 | return SUCCESS; |
|---|
| 3284 | } |
|---|
| 3285 | /* }}} */ |
|---|
| 3286 | /* {{{ static PHP_RSHUTDOWN_FUNCTION(xcache_cacher) */ |
|---|
| 3287 | #ifndef ZEND_ENGINE_2 |
|---|
| 3288 | static PHP_RSHUTDOWN_FUNCTION(xcache_cacher) |
|---|
| 3289 | #else |
|---|
| 3290 | static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(xcache_cacher) |
|---|
| 3291 | #endif |
|---|
| 3292 | { |
|---|
| 3293 | #ifdef ZEND_ENGINE_2 |
|---|
| 3294 | TSRMLS_FETCH(); |
|---|
| 3295 | #endif |
|---|
| 3296 | |
|---|
| 3297 | xc_request_shutdown(TSRMLS_C); |
|---|
| 3298 | return SUCCESS; |
|---|
| 3299 | } |
|---|
| 3300 | /* }}} */ |
|---|
| 3301 | static zend_module_entry xcache_cacher_module_entry = { /* {{{ */ |
|---|
| 3302 | STANDARD_MODULE_HEADER, |
|---|
| 3303 | XCACHE_NAME "_Cacher", |
|---|
| 3304 | xcache_cacher_functions, |
|---|
| 3305 | PHP_MINIT(xcache_cacher), |
|---|
| 3306 | PHP_MSHUTDOWN(xcache_cacher), |
|---|
| 3307 | PHP_RINIT(xcache_cacher), |
|---|
| 3308 | #ifndef ZEND_ENGINE_2 |
|---|
| 3309 | PHP_RSHUTDOWN(xcache_cacher), |
|---|
| 3310 | #else |
|---|
| 3311 | NULL, |
|---|
| 3312 | #endif |
|---|
| 3313 | PHP_MINFO(xcache_cacher), |
|---|
| 3314 | XCACHE_VERSION, |
|---|
| 3315 | #ifdef PHP_GINIT |
|---|
| 3316 | NO_MODULE_GLOBALS, |
|---|
| 3317 | #endif |
|---|
| 3318 | #ifdef ZEND_ENGINE_2 |
|---|
| 3319 | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(xcache_cacher), |
|---|
| 3320 | #else |
|---|
| 3321 | NULL, |
|---|
| 3322 | NULL, |
|---|
| 3323 | #endif |
|---|
| 3324 | STANDARD_MODULE_PROPERTIES_EX |
|---|
| 3325 | }; |
|---|
| 3326 | /* }}} */ |
|---|
| 3327 | int xc_cacher_startup_module() /* {{{ */ |
|---|
| 3328 | { |
|---|
| 3329 | return zend_startup_module(&xcache_cacher_module_entry); |
|---|
| 3330 | } |
|---|
| 3331 | /* }}} */ |
|---|