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