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