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