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