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