3806 lines
99 KiB
C
3806 lines
99 KiB
C
|
|
#if 0
|
|
#define XCACHE_DEBUG
|
|
#endif
|
|
|
|
#if 0
|
|
#define SHOW_DPRINT
|
|
#endif
|
|
|
|
/* {{{ macros */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include "php.h"
|
|
#include "ext/standard/info.h"
|
|
#include "ext/standard/md5.h"
|
|
#include "ext/standard/php_math.h"
|
|
#include "ext/standard/php_string.h"
|
|
#include "zend_extensions.h"
|
|
#include "SAPI.h"
|
|
|
|
#include "xcache.h"
|
|
#ifdef ZEND_ENGINE_2_1
|
|
#include "ext/date/php_date.h"
|
|
#endif
|
|
#include "optimizer.h"
|
|
#include "coverager.h"
|
|
#include "disassembler.h"
|
|
#include "align.h"
|
|
#include "stack.h"
|
|
#include "xcache_globals.h"
|
|
#include "processor.h"
|
|
#include "const_string.h"
|
|
#include "opcode_spec.h"
|
|
#include "utils.h"
|
|
|
|
#define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl)
|
|
#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
|
|
#define LOCK(x) xc_lock(x->lck)
|
|
#define UNLOCK(x) xc_unlock(x->lck)
|
|
|
|
#define ENTER_LOCK_EX(x) \
|
|
xc_lock(x->lck); \
|
|
zend_try { \
|
|
do
|
|
#define LEAVE_LOCK_EX(x) \
|
|
while (0); \
|
|
} zend_catch { \
|
|
catched = 1; \
|
|
} zend_end_try(); \
|
|
xc_unlock(x->lck)
|
|
|
|
#define ENTER_LOCK(x) do { \
|
|
int catched = 0; \
|
|
ENTER_LOCK_EX(x)
|
|
#define LEAVE_LOCK(x) \
|
|
LEAVE_LOCK_EX(x); \
|
|
if (catched) { \
|
|
zend_bailout(); \
|
|
} \
|
|
} while(0)
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ globals */
|
|
static char *xc_shm_scheme = NULL;
|
|
static char *xc_mmap_path = NULL;
|
|
static char *xc_coredump_dir = NULL;
|
|
|
|
static xc_hash_t xc_php_hcache = {0};
|
|
static xc_hash_t xc_php_hentry = {0};
|
|
static xc_hash_t xc_var_hcache = {0};
|
|
static xc_hash_t xc_var_hentry = {0};
|
|
|
|
static zend_ulong xc_php_ttl = 0;
|
|
static zend_ulong xc_var_maxttl = 0;
|
|
|
|
enum { xc_deletes_gc_interval = 120 };
|
|
static zend_ulong xc_php_gc_interval = 0;
|
|
static zend_ulong xc_var_gc_interval = 0;
|
|
|
|
/* total size */
|
|
static zend_ulong xc_php_size = 0;
|
|
static zend_ulong xc_var_size = 0;
|
|
|
|
static xc_cache_t **xc_php_caches = NULL;
|
|
static xc_cache_t **xc_var_caches = NULL;
|
|
|
|
static zend_bool xc_initized = 0;
|
|
static time_t xc_init_time = 0;
|
|
static long unsigned xc_init_instance_id = 0;
|
|
#ifdef ZTS
|
|
static long unsigned xc_init_instance_subid = 0;
|
|
#endif
|
|
static zend_compile_file_t *origin_compile_file = NULL;
|
|
static zend_compile_file_t *old_compile_file = NULL;
|
|
static zend_llist_element *xc_llist_zend_extension = NULL;
|
|
|
|
static zend_bool xc_test = 0;
|
|
static zend_bool xc_readonly_protection = 0;
|
|
|
|
zend_bool xc_have_op_array_ctor = 0;
|
|
|
|
static zend_bool xc_module_gotup = 0;
|
|
static zend_bool xc_zend_extension_gotup = 0;
|
|
static zend_bool xc_zend_extension_faked = 0;
|
|
#if !COMPILE_DL_XCACHE
|
|
# define zend_extension_entry xcache_zend_extension_entry
|
|
#endif
|
|
ZEND_DLEXPORT zend_extension zend_extension_entry;
|
|
ZEND_DECLARE_MODULE_GLOBALS(xcache);
|
|
/* }}} */
|
|
|
|
/* any function in *_dmz is only safe be called within locked(single thread) area */
|
|
|
|
static void xc_php_add_dmz(xc_entry_data_php_t *php) /* {{{ */
|
|
{
|
|
xc_entry_data_php_t **head = &(php->cache->phps[php->hvalue]);
|
|
php->next = *head;
|
|
*head = php;
|
|
php->cache->phps_count ++;
|
|
}
|
|
/* }}} */
|
|
static xc_entry_data_php_t *xc_php_store_dmz(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_data_php_t *stored_php;
|
|
|
|
php->hits = 0;
|
|
php->refcount = 0;
|
|
stored_php = xc_processor_store_xc_entry_data_php_t(php TSRMLS_CC);
|
|
if (stored_php) {
|
|
xc_php_add_dmz(stored_php);
|
|
return stored_php;
|
|
}
|
|
else {
|
|
php->cache->ooms ++;
|
|
return NULL;
|
|
}
|
|
}
|
|
/* }}} */
|
|
static xc_entry_data_php_t *xc_php_find_dmz(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_data_php_t *p;
|
|
for (p = php->cache->phps[php->hvalue]; p; p = p->next) {
|
|
if (memcmp(php->md5, p->md5, sizeof(php->md5)) == 0) {
|
|
p->hits ++;
|
|
return p;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
/* }}} */
|
|
static void xc_php_free_dmz(xc_entry_data_php_t *php) /* {{{ */
|
|
{
|
|
php->cache->mem->handlers->free(php->cache->mem, (xc_entry_data_php_t *)php);
|
|
}
|
|
/* }}} */
|
|
static void xc_php_addref_dmz(xc_entry_data_php_t *php) /* {{{ */
|
|
{
|
|
php->refcount ++;
|
|
}
|
|
/* }}} */
|
|
static void xc_php_release_dmz(xc_entry_data_php_t *php) /* {{{ */
|
|
{
|
|
if (-- php->refcount == 0) {
|
|
xc_entry_data_php_t **pp = &(php->cache->phps[php->hvalue]);
|
|
xc_entry_data_php_t *p;
|
|
for (p = *pp; p; pp = &(p->next), p = p->next) {
|
|
if (memcmp(php->md5, p->md5, sizeof(php->md5)) == 0) {
|
|
/* unlink */
|
|
*pp = p->next;
|
|
xc_php_free_dmz(php);
|
|
return;
|
|
}
|
|
}
|
|
assert(0);
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
static inline int xc_entry_equal_dmz(xc_entry_t *a, xc_entry_t *b) /* {{{ */
|
|
{
|
|
/* this function isn't required but can be in dmz */
|
|
|
|
if (a->type != b->type) {
|
|
return 0;
|
|
}
|
|
switch (a->type) {
|
|
case XC_TYPE_PHP:
|
|
#ifdef HAVE_INODE
|
|
do {
|
|
if (a->inode) {
|
|
return a->inode == b->inode
|
|
&& a->device == b->device;
|
|
}
|
|
} while(0);
|
|
#endif
|
|
/* fall */
|
|
|
|
case XC_TYPE_VAR:
|
|
do {
|
|
#ifdef IS_UNICODE
|
|
if (a->name_type == IS_UNICODE) {
|
|
if (a->name.ustr.len != b->name.ustr.len) {
|
|
return 0;
|
|
}
|
|
return memcmp(a->name.ustr.val, b->name.ustr.val, (a->name.ustr.len + 1) * sizeof(UChar)) == 0;
|
|
}
|
|
#endif
|
|
if (a->name.str.len != b->name.str.len) {
|
|
return 0;
|
|
}
|
|
return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0;
|
|
|
|
} while(0);
|
|
default:
|
|
assert(0);
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static inline int xc_entry_has_prefix_dmz(xc_entry_t *entry, zval *prefix) /* {{{ */
|
|
{
|
|
/* this function isn't required but can be in dmz */
|
|
|
|
switch (entry->type) {
|
|
case XC_TYPE_PHP:
|
|
case XC_TYPE_VAR:
|
|
do {
|
|
#ifdef IS_UNICODE
|
|
if (entry->name_type != prefix->type) {
|
|
return 0;
|
|
}
|
|
|
|
if (entry->name_type == IS_UNICODE) {
|
|
if (entry->name.ustr.len < Z_USTRLEN_P(prefix)) {
|
|
return 0;
|
|
}
|
|
return memcmp(entry->name.ustr.val, Z_USTRVAL_P(prefix), Z_USTRLEN_P(prefix) * sizeof(UChar)) == 0;
|
|
}
|
|
#endif
|
|
if (prefix->type != IS_STRING) {
|
|
return 0;
|
|
}
|
|
|
|
if (entry->name.str.len < Z_STRLEN_P(prefix)) {
|
|
return 0;
|
|
}
|
|
|
|
return memcmp(entry->name.str.val, Z_STRVAL_P(prefix), Z_STRLEN_P(prefix)) == 0;
|
|
|
|
} while(0);
|
|
default:
|
|
assert(0);
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_add_dmz(xc_entry_t *xce) /* {{{ */
|
|
{
|
|
xc_entry_t **head = &(xce->cache->entries[xce->hvalue]);
|
|
xce->next = *head;
|
|
*head = xce;
|
|
xce->cache->entries_count ++;
|
|
}
|
|
/* }}} */
|
|
static xc_entry_t *xc_entry_store_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_t *stored_xce;
|
|
|
|
xce->hits = 0;
|
|
xce->ctime = XG(request_time);
|
|
xce->atime = XG(request_time);
|
|
stored_xce = xc_processor_store_xc_entry_t(xce TSRMLS_CC);
|
|
if (stored_xce) {
|
|
xc_entry_add_dmz(stored_xce);
|
|
return stored_xce;
|
|
}
|
|
else {
|
|
xce->cache->ooms ++;
|
|
return NULL;
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_free_real_dmz(volatile xc_entry_t *xce) /* {{{ */
|
|
{
|
|
if (xce->type == XC_TYPE_PHP) {
|
|
xc_php_release_dmz(xce->data.php);
|
|
}
|
|
xce->cache->mem->handlers->free(xce->cache->mem, (xc_entry_t *)xce);
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_free_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
xce->cache->entries_count --;
|
|
if (xce->refcount == 0) {
|
|
xc_entry_free_real_dmz(xce);
|
|
}
|
|
else {
|
|
xce->next = xce->cache->deletes;
|
|
xce->cache->deletes = xce;
|
|
xce->dtime = XG(request_time);
|
|
xce->cache->deletes_count ++;
|
|
}
|
|
return;
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_t **pp = &(xce->cache->entries[xce->hvalue]);
|
|
xc_entry_t *p;
|
|
for (p = *pp; p; pp = &(p->next), p = p->next) {
|
|
if (xc_entry_equal_dmz(xce, p)) {
|
|
/* unlink */
|
|
*pp = p->next;
|
|
xc_entry_free_dmz(xce TSRMLS_CC);
|
|
return;
|
|
}
|
|
}
|
|
assert(0);
|
|
}
|
|
/* }}} */
|
|
static xc_entry_t *xc_entry_find_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_t *p;
|
|
for (p = xce->cache->entries[xce->hvalue]; p; p = p->next) {
|
|
if (xc_entry_equal_dmz(xce, p)) {
|
|
if (p->type == XC_TYPE_VAR || /* PHP */ (p->mtime == xce->mtime && p->data.php->sourcesize == xce->data.php->sourcesize)) {
|
|
p->hits ++;
|
|
p->atime = XG(request_time);
|
|
return p;
|
|
}
|
|
else {
|
|
xc_entry_remove_dmz(p TSRMLS_CC);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
TRACE("hold %s", xce->name.str.val);
|
|
xce->refcount ++;
|
|
xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce);
|
|
}
|
|
/* }}} */
|
|
#if 0
|
|
static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
xce->refcount ++;
|
|
xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce);
|
|
}
|
|
/* }}} */
|
|
#endif
|
|
static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */
|
|
{
|
|
if (val + 1 >= count) {
|
|
return 0;
|
|
}
|
|
return val + 1;
|
|
}
|
|
/* }}} */
|
|
static void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t period, zend_ulong *counters, zend_uint count TSRMLS_DC) /* {{{ */
|
|
{
|
|
time_t n = XG(request_time) / period;
|
|
if (*curtime != n) {
|
|
zend_uint target_slot = n % count;
|
|
if (n - *curtime > period) {
|
|
memset(counters, 0, sizeof(counters[0]) * count);
|
|
}
|
|
else {
|
|
zend_uint slot;
|
|
for (slot = advance_wrapped(*curslot, count);
|
|
slot != target_slot;
|
|
slot = advance_wrapped(slot, count)) {
|
|
counters[slot] = 0;
|
|
}
|
|
counters[target_slot] = 0;
|
|
}
|
|
*curtime = n;
|
|
*curslot = target_slot;
|
|
}
|
|
counters[*curslot] ++;
|
|
}
|
|
/* }}} */
|
|
static void xc_cache_hit_dmz(xc_cache_t *cache TSRMLS_DC) /* {{{ */
|
|
{
|
|
cache->hits ++;
|
|
|
|
xc_counters_inc(&cache->hits_by_hour_cur_time
|
|
, &cache->hits_by_hour_cur_slot, 60 * 60
|
|
, cache->hits_by_hour
|
|
, sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0])
|
|
TSRMLS_CC);
|
|
|
|
xc_counters_inc(&cache->hits_by_second_cur_time
|
|
, &cache->hits_by_second_cur_slot
|
|
, 1
|
|
, cache->hits_by_second
|
|
, sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0])
|
|
TSRMLS_CC);
|
|
}
|
|
/* }}} */
|
|
|
|
/* helper function that loop through each entry */
|
|
#define XC_ENTRY_APPLY_FUNC(name) int name(xc_entry_t *entry TSRMLS_DC)
|
|
typedef XC_ENTRY_APPLY_FUNC((*cache_apply_dmz_func_t));
|
|
static void xc_entry_apply_dmz(xc_cache_t *cache, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_entry_t *p, **pp;
|
|
int i, c;
|
|
|
|
for (i = 0, c = cache->hentry->size; i < c; i ++) {
|
|
pp = &(cache->entries[i]);
|
|
for (p = *pp; p; p = *pp) {
|
|
if (apply_func(p TSRMLS_CC)) {
|
|
/* unlink */
|
|
*pp = p->next;
|
|
xc_entry_free_dmz(p TSRMLS_CC);
|
|
}
|
|
else {
|
|
pp = &(p->next);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
#define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC)
|
|
/* call graph:
|
|
* xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_php_entry_dmz
|
|
* xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_var_entry_dmz
|
|
*/
|
|
static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */
|
|
{
|
|
TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl);
|
|
if (XG(request_time) > entry->atime + xc_php_ttl) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_dmz) /* {{{ */
|
|
{
|
|
if (VAR_ENTRY_EXPIRED(entry)) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static void xc_gc_expires_one(xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */
|
|
{
|
|
TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval);
|
|
if (XG(request_time) - cache->last_gc_expires >= gc_interval) {
|
|
ENTER_LOCK(cache) {
|
|
if (XG(request_time) - cache->last_gc_expires >= gc_interval) {
|
|
cache->last_gc_expires = XG(request_time);
|
|
xc_entry_apply_dmz(cache, apply_func TSRMLS_CC);
|
|
}
|
|
} LEAVE_LOCK(cache);
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_gc_expires_php(TSRMLS_D) /* {{{ */
|
|
{
|
|
int i, c;
|
|
|
|
if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) {
|
|
return;
|
|
}
|
|
|
|
for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
|
|
xc_gc_expires_one(xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_dmz TSRMLS_CC);
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_gc_expires_var(TSRMLS_D) /* {{{ */
|
|
{
|
|
int i, c;
|
|
|
|
if (!xc_var_gc_interval || !xc_var_caches) {
|
|
return;
|
|
}
|
|
|
|
for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
|
|
xc_gc_expires_one(xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_dmz TSRMLS_CC);
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
static XC_CACHE_APPLY_FUNC(xc_gc_delete_dmz) /* {{{ */
|
|
{
|
|
xc_entry_t *p, **pp;
|
|
|
|
pp = &cache->deletes;
|
|
for (p = *pp; p; p = *pp) {
|
|
if (XG(request_time) - p->dtime > 3600) {
|
|
p->refcount = 0;
|
|
/* issue warning here */
|
|
}
|
|
if (p->refcount == 0) {
|
|
/* unlink */
|
|
*pp = p->next;
|
|
cache->deletes_count --;
|
|
xc_entry_free_real_dmz(p);
|
|
}
|
|
else {
|
|
pp = &(p->next);
|
|
}
|
|
}
|
|
}
|
|
/* }}} */
|
|
static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */
|
|
{
|
|
if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) {
|
|
ENTER_LOCK(cache) {
|
|
if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) {
|
|
cache->last_gc_deletes = XG(request_time);
|
|
xc_gc_delete_dmz(cache TSRMLS_CC);
|
|
}
|
|
} LEAVE_LOCK(cache);
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_gc_deletes(TSRMLS_D) /* {{{ */
|
|
{
|
|
int i, c;
|
|
|
|
if (xc_php_caches) {
|
|
for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
|
|
xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC);
|
|
}
|
|
}
|
|
|
|
if (xc_var_caches) {
|
|
for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
|
|
xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC);
|
|
}
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
/* helper functions for user functions */
|
|
static void xc_fillinfo_dmz(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
|
|
{
|
|
zval *blocks, *hits;
|
|
int i;
|
|
const xc_block_t *b;
|
|
#ifndef NDEBUG
|
|
xc_memsize_t avail = 0;
|
|
#endif
|
|
xc_mem_t *mem = cache->mem;
|
|
const xc_mem_handlers_t *handlers = mem->handlers;
|
|
zend_ulong interval;
|
|
if (cachetype == XC_TYPE_PHP) {
|
|
interval = xc_php_ttl ? xc_php_gc_interval : 0;
|
|
}
|
|
else {
|
|
interval = xc_var_gc_interval;
|
|
}
|
|
|
|
add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->misses);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("errors"), cache->errors);
|
|
|
|
add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count);
|
|
if (interval) {
|
|
time_t gc = (cache->last_gc_expires + interval) - XG(request_time);
|
|
add_assoc_long_ex(return_value, ZEND_STRS("gc"), gc > 0 ? gc : 0);
|
|
}
|
|
else {
|
|
add_assoc_null_ex(return_value, ZEND_STRS("gc"));
|
|
}
|
|
MAKE_STD_ZVAL(hits);
|
|
array_init(hits);
|
|
for (i = 0; i < sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]); i ++) {
|
|
add_next_index_long(hits, (long) cache->hits_by_hour[i]);
|
|
}
|
|
add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_hour"), hits);
|
|
|
|
MAKE_STD_ZVAL(hits);
|
|
array_init(hits);
|
|
for (i = 0; i < sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]); i ++) {
|
|
add_next_index_long(hits, (long) cache->hits_by_second[i]);
|
|
}
|
|
add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_second"), hits);
|
|
|
|
MAKE_STD_ZVAL(blocks);
|
|
array_init(blocks);
|
|
|
|
add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem));
|
|
add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem));
|
|
add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection);
|
|
|
|
for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) {
|
|
zval *bi;
|
|
|
|
MAKE_STD_ZVAL(bi);
|
|
array_init(bi);
|
|
|
|
add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b));
|
|
add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b));
|
|
add_next_index_zval(blocks, bi);
|
|
#ifndef NDEBUG
|
|
avail += handlers->block_size(b);
|
|
#endif
|
|
}
|
|
add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks);
|
|
#ifndef NDEBUG
|
|
assert(avail == handlers->avail(mem));
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
static void xc_fillentry_dmz(xc_entry_t *entry, int del, zval *list TSRMLS_DC) /* {{{ */
|
|
{
|
|
zval* ei;
|
|
xc_entry_data_php_t *php;
|
|
xc_entry_data_var_t *var;
|
|
|
|
ALLOC_INIT_ZVAL(ei);
|
|
array_init(ei);
|
|
|
|
add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry->refcount);
|
|
add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits);
|
|
add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime);
|
|
add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime);
|
|
add_assoc_long_ex(ei, ZEND_STRS("hvalue"), entry->hvalue);
|
|
if (del) {
|
|
add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime);
|
|
}
|
|
#ifdef IS_UNICODE
|
|
do {
|
|
zval *zv;
|
|
ALLOC_INIT_ZVAL(zv);
|
|
switch (entry->name_type) {
|
|
case IS_UNICODE:
|
|
ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1);
|
|
break;
|
|
case IS_STRING:
|
|
ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1);
|
|
break;
|
|
default:
|
|
assert(0);
|
|
}
|
|
zv->type = entry->name_type;
|
|
add_assoc_zval_ex(ei, ZEND_STRS("name"), zv);
|
|
} while (0);
|
|
#else
|
|
add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1);
|
|
#endif
|
|
switch (entry->type) {
|
|
case XC_TYPE_PHP:
|
|
php = entry->data.php;
|
|
add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size + php->size);
|
|
add_assoc_long_ex(ei, ZEND_STRS("phprefcount"), php->refcount);
|
|
add_assoc_long_ex(ei, ZEND_STRS("sourcesize"), php->sourcesize);
|
|
#ifdef HAVE_INODE
|
|
add_assoc_long_ex(ei, ZEND_STRS("device"), entry->device);
|
|
add_assoc_long_ex(ei, ZEND_STRS("inode"), entry->inode);
|
|
#endif
|
|
add_assoc_long_ex(ei, ZEND_STRS("mtime"), entry->mtime);
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt);
|
|
#endif
|
|
add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt);
|
|
add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt);
|
|
#ifdef ZEND_ENGINE_2_1
|
|
add_assoc_long_ex(ei, ZEND_STRS("autoglobal_cnt"),php->autoglobal_cnt);
|
|
#endif
|
|
break;
|
|
|
|
case XC_TYPE_VAR:
|
|
var = entry->data.var;
|
|
add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size);
|
|
break;
|
|
|
|
default:
|
|
assert(0);
|
|
}
|
|
|
|
add_next_index_zval(list, ei);
|
|
}
|
|
/* }}} */
|
|
static void xc_filllist_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
|
|
{
|
|
zval* list;
|
|
int i, c;
|
|
xc_entry_t *e;
|
|
|
|
ALLOC_INIT_ZVAL(list);
|
|
array_init(list);
|
|
|
|
for (i = 0, c = cache->hentry->size; i < c; i ++) {
|
|
for (e = cache->entries[i]; e; e = e->next) {
|
|
xc_fillentry_dmz(e, 0, list TSRMLS_CC);
|
|
}
|
|
}
|
|
add_assoc_zval(return_value, "cache_list", list);
|
|
|
|
ALLOC_INIT_ZVAL(list);
|
|
array_init(list);
|
|
for (e = cache->deletes; e; e = e->next) {
|
|
xc_fillentry_dmz(e, 1, list TSRMLS_CC);
|
|
}
|
|
add_assoc_zval(return_value, "deleted_list", list);
|
|
}
|
|
/* }}} */
|
|
|
|
static zend_op_array *xc_entry_install(xc_entry_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */
|
|
{
|
|
zend_uint i;
|
|
xc_entry_data_php_t *p = xce->data.php;
|
|
zend_op_array *old_active_op_array = CG(active_op_array);
|
|
#ifndef ZEND_ENGINE_2
|
|
ALLOCA_FLAG(use_heap)
|
|
/* new ptr which is stored inside CG(class_table) */
|
|
xc_cest_t **new_cest_ptrs = (xc_cest_t **)my_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap);
|
|
#endif
|
|
|
|
CG(active_op_array) = p->op_array;
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
/* install constant */
|
|
for (i = 0; i < p->constinfo_cnt; i ++) {
|
|
xc_constinfo_t *ci = &p->constinfos[i];
|
|
xc_install_constant(xce->name.str.val, &ci->constant,
|
|
UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC);
|
|
}
|
|
#endif
|
|
|
|
/* install function */
|
|
for (i = 0; i < p->funcinfo_cnt; i ++) {
|
|
xc_funcinfo_t *fi = &p->funcinfos[i];
|
|
xc_install_function(xce->name.str.val, &fi->func,
|
|
UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC);
|
|
}
|
|
|
|
/* install class */
|
|
for (i = 0; i < p->classinfo_cnt; i ++) {
|
|
xc_classinfo_t *ci = &p->classinfos[i];
|
|
#ifndef ZEND_ENGINE_2
|
|
zend_class_entry *ce = CestToCePtr(ci->cest);
|
|
/* fix pointer to the be which inside class_table */
|
|
if (ce->parent) {
|
|
zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1;
|
|
assert(class_idx < i);
|
|
ci->cest.parent = new_cest_ptrs[class_idx];
|
|
}
|
|
new_cest_ptrs[i] =
|
|
#endif
|
|
#ifdef ZEND_COMPILE_DELAYED_BINDING
|
|
xc_install_class(xce->name.str.val, &ci->cest, -1,
|
|
UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC);
|
|
#else
|
|
xc_install_class(xce->name.str.val, &ci->cest, ci->oplineno,
|
|
UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC);
|
|
#endif
|
|
}
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
/* trigger auto_globals jit */
|
|
for (i = 0; i < p->autoglobal_cnt; i ++) {
|
|
xc_autoglobal_t *aginfo = &p->autoglobals[i];
|
|
zend_u_is_auto_global(aginfo->type, aginfo->key, aginfo->key_len TSRMLS_CC);
|
|
}
|
|
#endif
|
|
#ifdef XCACHE_ERROR_CACHING
|
|
/* restore trigger errors */
|
|
for (i = 0; i < p->compilererror_cnt; i ++) {
|
|
xc_compilererror_t *error = &p->compilererrors[i];
|
|
CG(zend_lineno) = error->lineno;
|
|
zend_error(error->type, "%s", error->error);
|
|
}
|
|
CG(zend_lineno) = 0;
|
|
#endif
|
|
|
|
i = 1;
|
|
zend_hash_add(&EG(included_files), xce->name.str.val, xce->name.str.len+1, (void *)&i, sizeof(int), NULL);
|
|
if (h) {
|
|
zend_llist_add_element(&CG(open_files), h);
|
|
}
|
|
|
|
#ifndef ZEND_ENGINE_2
|
|
my_free_alloca(new_cest_ptrs, use_heap);
|
|
#endif
|
|
CG(active_op_array) = old_active_op_array;
|
|
return p->op_array;
|
|
}
|
|
/* }}} */
|
|
|
|
static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */
|
|
{
|
|
int i;
|
|
xc_stack_t *s;
|
|
xc_cache_t *cache;
|
|
xc_entry_t *xce;
|
|
|
|
for (i = 0; i < cachecount; i ++) {
|
|
s = &holds[i];
|
|
TRACE("holded %d", xc_stack_count(s));
|
|
if (xc_stack_count(s)) {
|
|
cache = ((xc_entry_t *)xc_stack_top(s))->cache;
|
|
ENTER_LOCK(cache) {
|
|
while (xc_stack_count(s)) {
|
|
xce = (xc_entry_t*) xc_stack_pop(s);
|
|
TRACE("unhold %s", xce->name.str.val);
|
|
xce->refcount --;
|
|
assert(xce->refcount >= 0);
|
|
}
|
|
} LEAVE_LOCK(cache);
|
|
}
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_unholds(TSRMLS_D) /* {{{ */
|
|
{
|
|
if (xc_php_caches) {
|
|
xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC);
|
|
}
|
|
|
|
if (xc_var_caches) {
|
|
xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC);
|
|
}
|
|
}
|
|
/* }}} */
|
|
static int xc_stat(const char *filename, const char *include_path, struct stat *pbuf TSRMLS_DC) /* {{{ */
|
|
{
|
|
char filepath[MAXPATHLEN];
|
|
char *paths, *path;
|
|
char *tokbuf;
|
|
int size = strlen(include_path) + 1;
|
|
char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' };
|
|
int ret;
|
|
ALLOCA_FLAG(use_heap)
|
|
|
|
paths = (char *)my_do_alloca(size, use_heap);
|
|
memcpy(paths, include_path, size);
|
|
|
|
for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) {
|
|
if (snprintf(filepath, sizeof(filepath), "%s/%s", path, filename) < MAXPATHLEN - 1) {
|
|
if (VCWD_STAT(filepath, pbuf) == 0) {
|
|
ret = SUCCESS;
|
|
goto finish;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* fall back to current directory */
|
|
if (zend_is_executing(TSRMLS_C)) {
|
|
char *path = zend_get_executed_filename(TSRMLS_C);
|
|
if (path && path[0] != '[') {
|
|
int len = strlen(path);
|
|
while ((--len >= 0) && !IS_SLASH(path[len])) {
|
|
/* skipped */
|
|
}
|
|
if (len > 0 && len + strlen(filename) + 1 < MAXPATHLEN - 1) {
|
|
strcpy(filepath, path);
|
|
strcpy(filepath + len + 1, filename);
|
|
if (VCWD_STAT(filepath, pbuf) == 0) {
|
|
ret = SUCCESS;
|
|
goto finish;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ret = FAILURE;
|
|
|
|
finish:
|
|
my_free_alloca(paths, use_heap);
|
|
|
|
return ret;
|
|
}
|
|
/* }}} */
|
|
|
|
#define HASH(i) (i)
|
|
#define HASH_ZSTR_L(t, s, l) HASH(zend_u_inline_hash_func(t, s, (l + 1) * sizeof(UChar)))
|
|
#define HASH_STR_S(s, l) HASH(zend_inline_hash_func(s, l))
|
|
#define HASH_STR_L(s, l) HASH_STR_S(s, l + 1)
|
|
#define HASH_STR(s) HASH_STR_L(s, strlen(s) + 1)
|
|
#define HASH_NUM(n) HASH(n)
|
|
static inline xc_hash_value_t xc_hash_fold(xc_hash_value_t hvalue, const xc_hash_t *hasher) /* {{{ fold hash bits as needed */
|
|
{
|
|
xc_hash_value_t folded = 0;
|
|
while (hvalue) {
|
|
folded ^= (hvalue & hasher->mask);
|
|
hvalue >>= hasher->bits;
|
|
}
|
|
return folded;
|
|
}
|
|
/* }}} */
|
|
static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
return UNISW(NOTHING, UG(unicode) ? HASH_ZSTR_L(xce->name_type, xce->name.uni.val, xce->name.uni.len) :)
|
|
HASH_STR_L(xce->name.str.val, xce->name.str.len);
|
|
}
|
|
/* }}} */
|
|
#define xc_entry_hash_var xc_entry_hash_name
|
|
static inline xc_hash_value_t xc_entry_hash_php(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
#ifdef HAVE_INODE
|
|
if (xce->inode) {
|
|
return HASH(xce->device + xce->inode);
|
|
}
|
|
#endif
|
|
return xc_entry_hash_name(xce TSRMLS_CC);
|
|
}
|
|
/* }}} */
|
|
static inline xc_hash_value_t xc_entry_hash_php_basename(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
#ifdef IS_UNICODE
|
|
if (UG(unicode) && xce->name_type == IS_UNICODE) {
|
|
zstr basename;
|
|
size_t basename_len;
|
|
php_u_basename(xce->name.ustr.val, xce->name.ustr.len, NULL, 0, &basename.u, &basename_len TSRMLS_CC);
|
|
return HASH_ZSTR_L(IS_UNICODE, basename, basename_len);
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
char *basename;
|
|
xc_hash_value_t h;
|
|
size_t basename_len;
|
|
#ifdef ZEND_ENGINE_2
|
|
php_basename(xce->name.str.val, xce->name.str.len, "", 0, &basename, &basename_len TSRMLS_CC);
|
|
#else
|
|
basename = php_basename(xce->name.str.val, xce->name.str.len, "", 0);
|
|
basename_len = strlen(basename);
|
|
#endif
|
|
h = HASH_STR_L(basename, basename_len);
|
|
efree(basename);
|
|
return h;
|
|
}
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_free_key_php(xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
#define X_FREE(var) do {\
|
|
if (xce->var) { \
|
|
efree(xce->var); \
|
|
} \
|
|
} while (0)
|
|
X_FREE(dirpath);
|
|
#ifdef IS_UNICODE
|
|
X_FREE(ufilepath);
|
|
X_FREE(udirpath);
|
|
#endif
|
|
|
|
#undef X_FREE
|
|
}
|
|
/* }}} */
|
|
|
|
static int xc_entry_init_key_php(xc_entry_t *xce, char *filename TSRMLS_DC) /* {{{ */
|
|
{
|
|
char opened_path_buffer[MAXPATHLEN];
|
|
struct stat buf, *pbuf;
|
|
xc_hash_value_t hv;
|
|
int cacheid;
|
|
xc_entry_data_php_t *php;
|
|
char *ptr;
|
|
time_t delta;
|
|
|
|
if (!filename || !SG(request_info).path_translated) {
|
|
return FAILURE;
|
|
}
|
|
|
|
if (strstr(filename, "://") != NULL) {
|
|
return FAILURE;
|
|
}
|
|
|
|
php = xce->data.php;
|
|
|
|
if (XG(stat)) {
|
|
if (strcmp(SG(request_info).path_translated, filename) == 0) {
|
|
/* sapi has already done this stat() for us */
|
|
pbuf = sapi_get_stat(TSRMLS_C);
|
|
if (pbuf) {
|
|
goto stat_done;
|
|
}
|
|
}
|
|
|
|
/* absolute path */
|
|
pbuf = &buf;
|
|
if (IS_ABSOLUTE_PATH(filename, strlen(filename))) {
|
|
if (VCWD_STAT(filename, pbuf) != 0) {
|
|
return FAILURE;
|
|
}
|
|
goto stat_done;
|
|
}
|
|
|
|
/* relative path */
|
|
if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
|
|
ptr = filename + 1;
|
|
if (*ptr == '.') {
|
|
while (*(++ptr) == '.');
|
|
if (!IS_SLASH(*ptr)) {
|
|
goto not_relative_path;
|
|
}
|
|
}
|
|
|
|
if (VCWD_STAT(filename, pbuf) != 0) {
|
|
return FAILURE;
|
|
}
|
|
goto stat_done;
|
|
}
|
|
not_relative_path:
|
|
|
|
/* use include_path */
|
|
if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != SUCCESS) {
|
|
return FAILURE;
|
|
}
|
|
|
|
/* fall */
|
|
|
|
stat_done:
|
|
delta = XG(request_time) - pbuf->st_mtime;
|
|
if (abs(delta) < 2 && !xc_test) {
|
|
return FAILURE;
|
|
}
|
|
|
|
xce->mtime = pbuf->st_mtime;
|
|
#ifdef HAVE_INODE
|
|
xce->device = pbuf->st_dev;
|
|
xce->inode = pbuf->st_ino;
|
|
#endif
|
|
php->sourcesize = pbuf->st_size;
|
|
}
|
|
else { /* XG(inode) */
|
|
xce->mtime = 0;
|
|
#ifdef HAVE_INODE
|
|
xce->device = 0;
|
|
xce->inode = 0;
|
|
#endif
|
|
php->sourcesize = 0;
|
|
}
|
|
|
|
#ifdef HAVE_INODE
|
|
if (!xce->inode)
|
|
#endif
|
|
{
|
|
/* hash on filename, let's expand it to real path */
|
|
/* FIXME */
|
|
filename = expand_filepath(filename, opened_path_buffer TSRMLS_CC);
|
|
if (filename == NULL) {
|
|
return FAILURE;
|
|
}
|
|
}
|
|
|
|
UNISW(NOTHING, xce->name_type = IS_STRING;)
|
|
xce->name.str.val = filename;
|
|
xce->name.str.len = strlen(filename);
|
|
|
|
if (xc_php_hcache.size > 1) {
|
|
hv = xc_entry_hash_php_basename(xce TSRMLS_CC);
|
|
cacheid = xc_hash_fold(hv, &xc_php_hcache);
|
|
}
|
|
else {
|
|
cacheid = 0;
|
|
}
|
|
xce->cache = xc_php_caches[cacheid];
|
|
|
|
hv = xc_entry_hash_php(xce TSRMLS_CC);
|
|
xce->hvalue = xc_hash_fold(hv, &xc_php_hentry);
|
|
|
|
xce->type = XC_TYPE_PHP;
|
|
|
|
xce->filepath = NULL;
|
|
xce->dirpath = NULL;
|
|
#ifdef IS_UNICODE
|
|
xce->ufilepath = NULL;
|
|
xce->udirpath = NULL;
|
|
#endif
|
|
|
|
return SUCCESS;
|
|
}
|
|
/* }}} */
|
|
static inline xc_hash_value_t xc_php_hash_md5(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
{
|
|
return HASH_STR_S(php->md5, sizeof(php->md5));
|
|
}
|
|
/* }}} */
|
|
static int xc_entry_init_key_php_md5(xc_entry_data_php_t *php, xc_entry_t *xce TSRMLS_DC) /* {{{ */
|
|
{
|
|
unsigned char buf[1024];
|
|
PHP_MD5_CTX context;
|
|
int n;
|
|
php_stream *stream;
|
|
xc_hash_value_t hv;
|
|
ulong old_rsid = EG(regular_list).nNextFreeElement;
|
|
|
|
stream = php_stream_open_wrapper(xce->name.str.val, "rb", USE_PATH | REPORT_ERRORS | ENFORCE_SAFE_MODE | STREAM_DISABLE_OPEN_BASEDIR, NULL);
|
|
if (!stream) {
|
|
return FAILURE;
|
|
}
|
|
|
|
PHP_MD5Init(&context);
|
|
while ((n = php_stream_read(stream, (char *) buf, sizeof(buf))) > 0) {
|
|
PHP_MD5Update(&context, buf, n);
|
|
}
|
|
PHP_MD5Final((unsigned char *) php->md5, &context);
|
|
|
|
php_stream_close(stream);
|
|
if (EG(regular_list).nNextFreeElement == old_rsid + 1) {
|
|
EG(regular_list).nNextFreeElement = old_rsid;
|
|
}
|
|
|
|
if (n < 0) {
|
|
return FAILURE;
|
|
}
|
|
|
|
hv = xc_php_hash_md5(php TSRMLS_CC);
|
|
php->cache = xce->cache;
|
|
php->hvalue = (hv & php->cache->hphp->mask);
|
|
#ifdef XCACHE_DEBUG
|
|
{
|
|
char md5str[33];
|
|
make_digest(md5str, (unsigned char *) php->md5);
|
|
TRACE("md5 %s", md5str);
|
|
}
|
|
#endif
|
|
|
|
return SUCCESS;
|
|
}
|
|
/* }}} */
|
|
static void xc_entry_init_key_php_entry(xc_entry_t *xce, char *filepath TSRMLS_DC) /* {{{*/
|
|
{
|
|
xce->filepath = filepath;
|
|
xce->filepath_len = strlen(xce->filepath);
|
|
xce->dirpath = estrndup(xce->filepath, xce->filepath_len);
|
|
xce->dirpath_len = zend_dirname(xce->dirpath, xce->filepath_len);
|
|
#ifdef IS_UNICODE
|
|
zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &xce->ufilepath, &xce->ufilepath_len, xce->filepath, xce->filepath_len TSRMLS_CC);
|
|
zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &xce->udirpath, &xce->udirpath_len, xce->dirpath, xce->dirpath_len TSRMLS_CC);
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
#ifndef ZEND_COMPILE_DELAYED_BINDING
|
|
static void xc_cache_early_binding_class_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */
|
|
{
|
|
char *class_name;
|
|
int i, class_len;
|
|
xc_cest_t cest;
|
|
xc_entry_data_php_t *php = (xc_entry_data_php_t *) data;
|
|
|
|
class_name = Z_OP_CONSTANT(opline->op1).value.str.val;
|
|
class_len = Z_OP_CONSTANT(opline->op1).value.str.len;
|
|
if (zend_hash_find(CG(class_table), class_name, class_len, (void **) &cest) == FAILURE) {
|
|
assert(0);
|
|
}
|
|
TRACE("got ZEND_DECLARE_INHERITED_CLASS: %s", class_name + 1);
|
|
/* let's see which class */
|
|
for (i = 0; i < php->classinfo_cnt; i ++) {
|
|
if (memcmp(ZSTR_S(php->classinfos[i].key), class_name, class_len) == 0) {
|
|
php->classinfos[i].oplineno = oplineno;
|
|
php->have_early_binding = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (i == php->classinfo_cnt) {
|
|
assert(0);
|
|
}
|
|
}
|
|
/* }}} */
|
|
#endif
|
|
|
|
/* {{{ Constant Usage */
|
|
#ifdef ZEND_ENGINE_2_4
|
|
#else
|
|
# define xcache_op1_is_file 1
|
|
# define xcache_op1_is_dir 2
|
|
# define xcache_op2_is_file 4
|
|
# define xcache_op2_is_dir 8
|
|
#endif
|
|
typedef struct {
|
|
zend_bool filepath_used;
|
|
zend_bool dirpath_used;
|
|
zend_bool ufilepath_used;
|
|
zend_bool udirpath_used;
|
|
} xc_const_usage_t;
|
|
/* }}} */
|
|
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) /* {{{ */
|
|
{
|
|
#ifdef ZEND_ENGINE_2_4
|
|
int oplineno;
|
|
#else
|
|
int oplineno;
|
|
#endif
|
|
xc_vector_t vector_int;
|
|
|
|
xc_vector_init(int, &vector_int);
|
|
|
|
#ifdef ZEND_ENGINE_2_4
|
|
#else
|
|
#define XCACHE_CHECK_OP(type, op) \
|
|
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) { \
|
|
usage->type##path_used = 1; \
|
|
oplineinfo |= xcache_##op##_is_##type; \
|
|
}
|
|
|
|
#define XCACHE_U_CHECK_OP(type, op) \
|
|
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) { \
|
|
usage->u##type##path_used = 1; \
|
|
oplineinfo |= xcache_##op##_is_##type; \
|
|
}
|
|
|
|
for (oplineno = 0; oplineno < op_array->last; oplineno++) {
|
|
zend_op *opline = &op_array->opcodes[oplineno];
|
|
int oplineinfo = 0;
|
|
if (Z_OP_TYPE(opline->op1) == IS_CONST) {
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) {
|
|
XCACHE_CHECK_OP(file, op1)
|
|
else XCACHE_CHECK_OP(dir, op1)
|
|
}
|
|
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) {
|
|
XCACHE_U_CHECK_OP(file, op1)
|
|
else XCACHE_U_CHECK_OP(dir, op1)
|
|
}
|
|
#endif
|
|
}
|
|
if (Z_OP_TYPE(opline->op2) == IS_CONST) {
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) {
|
|
XCACHE_CHECK_OP(file, op2)
|
|
else XCACHE_CHECK_OP(dir, op2)
|
|
}
|
|
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) {
|
|
XCACHE_U_CHECK_OP(file, op2)
|
|
else XCACHE_U_CHECK_OP(dir, op2)
|
|
}
|
|
#endif
|
|
}
|
|
if (oplineinfo) {
|
|
xc_vector_add(int, &vector_int, oplineno);
|
|
xc_vector_add(int, &vector_int, oplineinfo);
|
|
}
|
|
}
|
|
|
|
op_array_info->oplineinfo_cnt = vector_int.cnt;
|
|
op_array_info->oplineinfos = xc_vector_detach(int, &vector_int);
|
|
#endif
|
|
xc_vector_free(int, &vector_int);
|
|
}
|
|
/* }}} */
|
|
void xc_fix_op_array_info(const xc_entry_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int copy, const xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */
|
|
{
|
|
#ifdef ZEND_ENGINE_2_4
|
|
#else
|
|
int i;
|
|
#endif
|
|
|
|
#ifdef ZEND_ENGINE_2_4
|
|
#else
|
|
for (i = 0; i < op_array_info->oplineinfo_cnt; i += 2) {
|
|
int oplineno = op_array_info->oplineinfos[i];
|
|
int oplineinfo = op_array_info->oplineinfos[i + 1];
|
|
zend_op *opline = &op_array->opcodes[oplineno];
|
|
if ((oplineinfo & xcache_op1_is_file)) {
|
|
assert(Z_OP_TYPE(opline->op1) == IS_CONST);
|
|
if (copy) {
|
|
efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1)));
|
|
}
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) {
|
|
assert(xce->filepath);
|
|
ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), xce->filepath, xce->filepath_len, copy);
|
|
TRACE("fixing op1 to %s", xce->filepath);
|
|
}
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) {
|
|
assert(xce->ufilepath);
|
|
ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), xce->ufilepath, xce->ufilepath_len, copy);
|
|
}
|
|
#endif
|
|
else {
|
|
assert(0);
|
|
}
|
|
}
|
|
else if ((oplineinfo & xcache_op1_is_dir)) {
|
|
assert(Z_OP_TYPE(opline->op1) == IS_CONST);
|
|
if (copy) {
|
|
efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1)));
|
|
}
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) {
|
|
assert(xce->dirpath);
|
|
TRACE("fixing op1 to %s", xce->dirpath);
|
|
ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), xce->dirpath, xce->dirpath_len, copy);
|
|
}
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) {
|
|
assert(!xce->udirpath);
|
|
ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), xce->udirpath, xce->udirpath_len, copy);
|
|
}
|
|
#endif
|
|
else {
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
if ((oplineinfo & xcache_op2_is_file)) {
|
|
assert(Z_OP_TYPE(opline->op2) == IS_CONST);
|
|
if (copy) {
|
|
efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2)));
|
|
}
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) {
|
|
assert(xce->filepath);
|
|
TRACE("fixing op2 to %s", xce->filepath);
|
|
ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), xce->filepath, xce->filepath_len, copy);
|
|
}
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) {
|
|
assert(xce->ufilepath);
|
|
ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), xce->ufilepath, xce->ufilepath_len, copy);
|
|
}
|
|
#endif
|
|
else {
|
|
assert(0);
|
|
}
|
|
}
|
|
else if ((oplineinfo & xcache_op2_is_dir)) {
|
|
assert(Z_OP_TYPE(opline->op2) == IS_CONST);
|
|
if (copy) {
|
|
efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2)));
|
|
}
|
|
if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) {
|
|
assert(!xce->dirpath);
|
|
TRACE("fixing op2 to %s", xce->dirpath);
|
|
ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), xce->dirpath, xce->dirpath_len, copy);
|
|
}
|
|
#ifdef IS_UNICODE
|
|
else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) {
|
|
assert(!xce->udirpath);
|
|
ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), xce->udirpath, xce->udirpath_len, copy);
|
|
}
|
|
#endif
|
|
else {
|
|
assert(0);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
static void xc_free_op_array_info(xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */
|
|
{
|
|
#ifdef ZEND_ENGINE_2_4
|
|
if (op_array_info->literalinfos) {
|
|
efree(op_array_info->literalinfos);
|
|
}
|
|
#else
|
|
if (op_array_info->oplineinfos) {
|
|
efree(op_array_info->oplineinfos);
|
|
}
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
static void xc_free_php(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
{
|
|
int i;
|
|
if (php->classinfos) {
|
|
for (i = 0; i < php->classinfo_cnt; i ++) {
|
|
xc_classinfo_t *classinfo = &php->classinfos[i];
|
|
int j;
|
|
for (j = 0; j < classinfo->methodinfo_cnt; j ++) {
|
|
xc_free_op_array_info(&classinfo->methodinfos[j] TSRMLS_CC);
|
|
}
|
|
|
|
if (classinfo->methodinfos) {
|
|
efree(classinfo->methodinfos);
|
|
}
|
|
}
|
|
}
|
|
if (php->funcinfos) {
|
|
for (i = 0; i < php->funcinfo_cnt; i ++) {
|
|
xc_free_op_array_info(&php->funcinfos[i].op_array_info TSRMLS_CC);
|
|
}
|
|
}
|
|
xc_free_op_array_info(&php->op_array_info TSRMLS_CC);
|
|
|
|
#define X_FREE(var) do {\
|
|
if (php->var) { \
|
|
efree(php->var); \
|
|
} \
|
|
} while (0)
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
X_FREE(autoglobals);
|
|
#endif
|
|
X_FREE(classinfos);
|
|
X_FREE(funcinfos);
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
X_FREE(constinfos);
|
|
#endif
|
|
#undef X_FREE
|
|
}
|
|
/* }}} */
|
|
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) /* {{{ */
|
|
{
|
|
zend_op_array *op_array;
|
|
int old_constinfo_cnt, old_funcinfo_cnt, old_classinfo_cnt;
|
|
zend_bool catched = 0;
|
|
|
|
/* {{{ compile */
|
|
TRACE("compiling %s", h->opened_path ? h->opened_path : h->filename);
|
|
|
|
old_classinfo_cnt = zend_hash_num_elements(CG(class_table));
|
|
old_funcinfo_cnt = zend_hash_num_elements(CG(function_table));
|
|
old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants));
|
|
|
|
php->op_array = NULL;
|
|
XG(initial_compile_file_called) = 0;
|
|
zend_try {
|
|
op_array = old_compile_file(h, type TSRMLS_CC);
|
|
} zend_catch {
|
|
catched = 1;
|
|
} zend_end_try();
|
|
|
|
if (catched) {
|
|
goto err_bailout;
|
|
}
|
|
|
|
if (op_array == NULL) {
|
|
goto err_op_array;
|
|
}
|
|
|
|
if (!XG(initial_compile_file_called)) {
|
|
return op_array;
|
|
}
|
|
|
|
/* }}} */
|
|
/* {{{ prepare */
|
|
zend_restore_compiled_filename(h->opened_path ? h->opened_path : h->filename TSRMLS_CC);
|
|
php->op_array = op_array;
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
php->constinfo_cnt = zend_hash_num_elements(EG(zend_constants)) - old_constinfo_cnt;
|
|
#endif
|
|
php->funcinfo_cnt = zend_hash_num_elements(CG(function_table)) - old_funcinfo_cnt;
|
|
php->classinfo_cnt = zend_hash_num_elements(CG(class_table)) - old_classinfo_cnt;
|
|
#ifdef ZEND_ENGINE_2_1
|
|
/* {{{ count php->autoglobal_cnt */ {
|
|
Bucket *b;
|
|
|
|
php->autoglobal_cnt = 0;
|
|
for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) {
|
|
zend_auto_global *auto_global = (zend_auto_global *) b->pData;
|
|
/* check if actived */
|
|
if (auto_global->auto_global_callback && !auto_global->armed) {
|
|
php->autoglobal_cnt ++;
|
|
}
|
|
}
|
|
}
|
|
/* }}} */
|
|
#endif
|
|
|
|
#define X_ALLOC_N(var, cnt) do { \
|
|
if (php->cnt) { \
|
|
ECALLOC_N(php->var, php->cnt); \
|
|
if (!php->var) { \
|
|
goto err_alloc; \
|
|
} \
|
|
} \
|
|
else { \
|
|
php->var = NULL; \
|
|
} \
|
|
} while (0)
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
X_ALLOC_N(constinfos, constinfo_cnt);
|
|
#endif
|
|
X_ALLOC_N(funcinfos, funcinfo_cnt);
|
|
X_ALLOC_N(classinfos, classinfo_cnt);
|
|
#ifdef ZEND_ENGINE_2_1
|
|
X_ALLOC_N(autoglobals, autoglobal_cnt);
|
|
#endif
|
|
#undef X_ALLOC
|
|
/* }}} */
|
|
|
|
/* {{{ shallow copy, pointers only */ {
|
|
Bucket *b;
|
|
unsigned int i;
|
|
unsigned int j;
|
|
|
|
#define COPY_H(vartype, var, cnt, name, datatype) do { \
|
|
for (i = 0, j = 0; b; i ++, b = b->pListNext) { \
|
|
vartype *data = &php->var[j]; \
|
|
\
|
|
if (i < old_##cnt) { \
|
|
continue; \
|
|
} \
|
|
j ++; \
|
|
\
|
|
assert(i < old_##cnt + php->cnt); \
|
|
assert(b->pData); \
|
|
memcpy(&data->name, b->pData, sizeof(datatype)); \
|
|
UNISW(NOTHING, data->type = b->key.type;) \
|
|
if (UNISW(1, b->key.type == IS_STRING)) { \
|
|
ZSTR_S(data->key) = BUCKET_KEY_S(b); \
|
|
} \
|
|
else { \
|
|
ZSTR_U(data->key) = BUCKET_KEY_U(b); \
|
|
} \
|
|
data->key_size = b->nKeyLength; \
|
|
data->h = b->h; \
|
|
} \
|
|
} while(0)
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
b = EG(zend_constants)->pListHead; COPY_H(xc_constinfo_t, constinfos, constinfo_cnt, constant, zend_constant);
|
|
#endif
|
|
b = CG(function_table)->pListHead; COPY_H(xc_funcinfo_t, funcinfos, funcinfo_cnt, func, zend_function);
|
|
b = CG(class_table)->pListHead; COPY_H(xc_classinfo_t, classinfos, classinfo_cnt, cest, xc_cest_t);
|
|
|
|
#undef COPY_H
|
|
|
|
/* for ZE1, cest need to be fixed inside store */
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
/* scan for acatived auto globals */
|
|
i = 0;
|
|
for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) {
|
|
zend_auto_global *auto_global = (zend_auto_global *) b->pData;
|
|
/* check if actived */
|
|
if (auto_global->auto_global_callback && !auto_global->armed) {
|
|
xc_autoglobal_t *data = &php->autoglobals[i];
|
|
|
|
assert(i < php->autoglobal_cnt);
|
|
i ++;
|
|
UNISW(NOTHING, data->type = b->key.type;)
|
|
if (UNISW(1, b->key.type == IS_STRING)) {
|
|
ZSTR_S(data->key) = BUCKET_KEY_S(b);
|
|
}
|
|
else {
|
|
ZSTR_U(data->key) = BUCKET_KEY_U(b);
|
|
}
|
|
data->key_len = b->nKeyLength - 1;
|
|
data->h = b->h;
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
|
|
/* {{{ collect info for file/dir path */ {
|
|
Bucket *b;
|
|
xc_const_usage_t const_usage;
|
|
unsigned int i;
|
|
|
|
xc_entry_init_key_php_entry(xce, zend_get_compiled_filename(TSRMLS_C) TSRMLS_CC);
|
|
memset(&const_usage, 0, sizeof(const_usage));
|
|
|
|
for (i = 0; i < php->classinfo_cnt; i ++) {
|
|
xc_classinfo_t *classinfo = &php->classinfos[i];
|
|
zend_class_entry *ce = CestToCePtr(classinfo->cest);
|
|
classinfo->methodinfo_cnt = ce->function_table.nTableSize;
|
|
if (classinfo->methodinfo_cnt) {
|
|
int j;
|
|
|
|
ECALLOC_N(classinfo->methodinfos, classinfo->methodinfo_cnt);
|
|
if (!classinfo->methodinfos) {
|
|
goto err_alloc;
|
|
}
|
|
|
|
for (j = 0, b = ce->function_table.pListHead; b; j ++, b = b->pListNext) {
|
|
xc_collect_op_array_info(xce, php, &const_usage, &classinfo->methodinfos[j], (zend_op_array *) b->pData TSRMLS_CC);
|
|
}
|
|
}
|
|
else {
|
|
classinfo->methodinfos = NULL;
|
|
}
|
|
}
|
|
|
|
for (i = 0; i < php->funcinfo_cnt; i ++) {
|
|
xc_collect_op_array_info(xce, php, &const_usage, &php->funcinfos[i].op_array_info, (zend_op_array *) &php->funcinfos[i].func TSRMLS_CC);
|
|
}
|
|
|
|
xc_collect_op_array_info(xce, php, &const_usage, &php->op_array_info, php->op_array TSRMLS_CC);
|
|
|
|
/* file/dir path free unused */
|
|
#define X_FREE_UNUSED(var) \
|
|
if (!const_usage.var##path_used) { \
|
|
efree(xce->var##path); \
|
|
xce->var##path = NULL; \
|
|
xce->var##path_len = 0; \
|
|
}
|
|
/* filepath is required to restore op_array->filename, so no free filepath here */
|
|
X_FREE_UNUSED(dir)
|
|
#ifdef IS_UNICODE
|
|
X_FREE_UNUSED(ufile)
|
|
X_FREE_UNUSED(udir)
|
|
#endif
|
|
#undef X_FREE_UNUSED
|
|
}
|
|
/* }}} */
|
|
#ifdef XCACHE_ERROR_CACHING
|
|
php->compilererrors = ((xc_sandbox_t *) XG(sandbox))->compilererrors;
|
|
php->compilererror_cnt = ((xc_sandbox_t *) XG(sandbox))->compilererror_cnt;
|
|
#endif
|
|
#ifndef ZEND_COMPILE_DELAYED_BINDING
|
|
/* {{{ find inherited classes that should be early-binding */
|
|
php->have_early_binding = 0;
|
|
{
|
|
int i;
|
|
for (i = 0; i < php->classinfo_cnt; i ++) {
|
|
php->classinfos[i].oplineno = -1;
|
|
}
|
|
}
|
|
|
|
xc_undo_pass_two(php->op_array TSRMLS_CC);
|
|
xc_foreach_early_binding_class(php->op_array, xc_cache_early_binding_class_cb, (void *) php TSRMLS_CC);
|
|
xc_redo_pass_two(php->op_array TSRMLS_CC);
|
|
/* }}} */
|
|
#endif
|
|
|
|
return op_array;
|
|
|
|
err_alloc:
|
|
xc_free_php(php TSRMLS_CC);
|
|
|
|
err_bailout:
|
|
err_op_array:
|
|
|
|
if (catched) {
|
|
zend_bailout();
|
|
}
|
|
|
|
return op_array;
|
|
}
|
|
/* }}} */
|
|
static zend_op_array *xc_compile_restore(xc_entry_t *stored_xce, zend_file_handle *h TSRMLS_DC) /* {{{ */
|
|
{
|
|
zend_op_array *op_array;
|
|
xc_entry_t xce;
|
|
xc_entry_data_php_t php;
|
|
zend_bool catched;
|
|
|
|
CG(in_compilation) = 1;
|
|
CG(compiled_filename) = stored_xce->name.str.val;
|
|
CG(zend_lineno) = 0;
|
|
TRACE("restoring %s", stored_xce->name.str.val);
|
|
xc_processor_restore_xc_entry_t(&xce, stored_xce TSRMLS_CC);
|
|
xc_processor_restore_xc_entry_data_php_t(stored_xce, &php, xce.data.php, xc_readonly_protection TSRMLS_CC);
|
|
xce.data.php = &php;
|
|
#ifdef SHOW_DPRINT
|
|
xc_dprint(&xce, 0 TSRMLS_CC);
|
|
#endif
|
|
|
|
catched = 0;
|
|
zend_try {
|
|
op_array = xc_entry_install(&xce, h TSRMLS_CC);
|
|
} zend_catch {
|
|
catched = 1;
|
|
} zend_end_try();
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
if (php.constinfos) {
|
|
efree(php.constinfos);
|
|
}
|
|
#endif
|
|
if (php.funcinfos) {
|
|
efree(php.funcinfos);
|
|
}
|
|
if (php.classinfos) {
|
|
efree(php.classinfos);
|
|
}
|
|
|
|
if (catched) {
|
|
zend_bailout();
|
|
}
|
|
CG(in_compilation) = 0;
|
|
CG(compiled_filename) = NULL;
|
|
TRACE("restored %s", stored_xce->name.str.val);
|
|
return op_array;
|
|
}
|
|
/* }}} */
|
|
static zend_op_array *xc_check_initial_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */
|
|
{
|
|
XG(initial_compile_file_called) = 1;
|
|
return origin_compile_file(h, type TSRMLS_CC);
|
|
}
|
|
/* }}} */
|
|
static zend_op_array *xc_compile_file_ex(xc_entry_t *xce, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */
|
|
{
|
|
zend_op_array *op_array;
|
|
xc_entry_t *stored_xce;
|
|
xc_entry_data_php_t *stored_php;
|
|
xc_cache_t *cache = xce->cache;
|
|
zend_bool gaveup = 0;
|
|
zend_bool catched = 0;
|
|
zend_bool newlycompiled;
|
|
xc_sandbox_t sandbox;
|
|
|
|
/* stale clogs precheck */
|
|
if (XG(request_time) - cache->compiling < 30) {
|
|
cache->clogs ++;
|
|
return old_compile_file(h, type TSRMLS_CC);
|
|
}
|
|
/* {{{ entry_lookup/hit/md5_init/php_lookup */
|
|
stored_xce = NULL;
|
|
stored_php = NULL;
|
|
ENTER_LOCK_EX(cache) {
|
|
stored_xce = xc_entry_find_dmz(xce TSRMLS_CC);
|
|
if (stored_xce) {
|
|
xc_cache_hit_dmz(cache TSRMLS_CC);
|
|
|
|
TRACE("hit %s, holding", stored_xce->name.str.val);
|
|
xc_entry_hold_php_dmz(stored_xce TSRMLS_CC);
|
|
}
|
|
else {
|
|
cache->misses ++;
|
|
TRACE("miss %s", xce->name.str.val);
|
|
|
|
if (xc_entry_init_key_php_md5(xce->data.php, xce TSRMLS_CC) != SUCCESS) {
|
|
gaveup = 1;
|
|
break;
|
|
}
|
|
|
|
stored_php = xc_php_find_dmz(xce->data.php TSRMLS_CC);
|
|
|
|
/* miss but compiling */
|
|
if (!stored_php) {
|
|
if (XG(request_time) - cache->compiling < 30) {
|
|
TRACE("%s", "miss but compiling");
|
|
cache->clogs ++;
|
|
gaveup = 1;
|
|
break;
|
|
}
|
|
TRACE("%s", "php_lookup miss");
|
|
}
|
|
else {
|
|
TRACE("%s", "php_lookup hit");
|
|
}
|
|
|
|
cache->compiling = XG(request_time);
|
|
}
|
|
} LEAVE_LOCK_EX(cache);
|
|
|
|
if (catched) {
|
|
cache->compiling = 0;
|
|
zend_bailout();
|
|
}
|
|
|
|
/* hit */
|
|
if (stored_xce) {
|
|
return xc_compile_restore(stored_xce, h TSRMLS_CC);
|
|
}
|
|
|
|
/* gaveup */
|
|
if (gaveup) {
|
|
return old_compile_file(h, type TSRMLS_CC);
|
|
}
|
|
/* }}} */
|
|
op_array = NULL;
|
|
/* {{{ compile */
|
|
if (stored_php) {
|
|
newlycompiled = 0;
|
|
xc_entry_init_key_php_entry(xce, h->opened_path ? h->opened_path : h->filename TSRMLS_CC);
|
|
xce->data.php = stored_php;
|
|
}
|
|
else {
|
|
newlycompiled = 1;
|
|
|
|
/* make compile inside sandbox */
|
|
xc_sandbox_init(&sandbox, h->opened_path ? h->opened_path : h->filename TSRMLS_CC);
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
xce->data.php->constinfos = NULL;
|
|
#endif
|
|
xce->data.php->funcinfos = NULL;
|
|
xce->data.php->classinfos = NULL;
|
|
#ifdef ZEND_ENGINE_2_1
|
|
xce->data.php->autoglobals = NULL;
|
|
#endif
|
|
|
|
memset(&xce->data.php->op_array_info, 0, sizeof(xce->data.php->op_array_info));
|
|
|
|
zend_try {
|
|
op_array = xc_compile_php(xce, xce->data.php, h, type TSRMLS_CC);
|
|
} zend_catch {
|
|
catched = 1;
|
|
} zend_end_try();
|
|
|
|
if (catched || !op_array) {
|
|
goto err_aftersandbox;
|
|
}
|
|
|
|
/* not cachable */
|
|
if (!xce->data.php->op_array) {
|
|
cache->compiling = 0;
|
|
/* it's not cachable, but don't scare the users with high misses */
|
|
cache->misses --;
|
|
xc_sandbox_free(&sandbox, XC_InstallNoBinding TSRMLS_CC);
|
|
return op_array;
|
|
}
|
|
}
|
|
/* }}} */
|
|
#ifdef HAVE_INODE
|
|
/* {{{ path name fix
|
|
* inode enabled entry hash/compare on name
|
|
* do not update to its name to real pathname
|
|
* WARNING: this code is required to be after compile
|
|
*/
|
|
if (xce->inode) {
|
|
char *filename = h->opened_path ? h->opened_path : h->filename;
|
|
if (xce->name.str.val != filename) {
|
|
xce->name.str.val = filename;
|
|
xce->name.str.len = strlen(filename);
|
|
}
|
|
}
|
|
/* }}} */
|
|
#endif
|
|
#ifdef SHOW_DPRINT
|
|
xc_dprint(xce, 0 TSRMLS_CC);
|
|
#endif
|
|
stored_xce = NULL;
|
|
ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */
|
|
/* php_store */
|
|
if (newlycompiled) {
|
|
stored_php = xc_php_store_dmz(xce->data.php TSRMLS_CC);
|
|
if (!stored_php) {
|
|
/* error */
|
|
break;
|
|
}
|
|
}
|
|
/* entry_store */
|
|
xc_php_addref_dmz(stored_php);
|
|
stored_xce = xc_entry_store_dmz(xce TSRMLS_CC);
|
|
if (stored_xce) {
|
|
stored_xce->data.php = stored_php;
|
|
}
|
|
else {
|
|
/* error */
|
|
xc_php_release_dmz(stored_php);
|
|
}
|
|
} LEAVE_LOCK_EX(cache);
|
|
/* }}} */
|
|
TRACE("%s", stored_xce ? "stored" : "store failed");
|
|
|
|
cache->compiling = 0;
|
|
if (catched) {
|
|
goto err_aftersandbox;
|
|
}
|
|
|
|
if (newlycompiled) {
|
|
xc_free_php(xce->data.php TSRMLS_CC);
|
|
}
|
|
|
|
if (stored_xce) {
|
|
if (op_array) {
|
|
#ifdef ZEND_ENGINE_2
|
|
destroy_op_array(op_array TSRMLS_CC);
|
|
#else
|
|
destroy_op_array(op_array);
|
|
#endif
|
|
efree(op_array);
|
|
h = NULL;
|
|
}
|
|
if (newlycompiled) {
|
|
xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC);
|
|
}
|
|
return xc_compile_restore(stored_xce, h TSRMLS_CC);
|
|
}
|
|
else {
|
|
if (newlycompiled) {
|
|
zend_op_array *old_active_op_array = CG(active_op_array);
|
|
/* install it */
|
|
CG(active_op_array) = op_array;
|
|
xc_sandbox_free(&sandbox, XC_Install TSRMLS_CC);
|
|
CG(active_op_array) = old_active_op_array;
|
|
}
|
|
}
|
|
return op_array;
|
|
|
|
err_aftersandbox:
|
|
if (newlycompiled) {
|
|
xc_free_php(xce->data.php TSRMLS_CC);
|
|
xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC);
|
|
}
|
|
|
|
if (catched) {
|
|
cache->compiling = 0;
|
|
cache->errors ++;
|
|
zend_bailout();
|
|
}
|
|
return op_array;
|
|
}
|
|
/* }}} */
|
|
static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */
|
|
{
|
|
zend_op_array *op_array;
|
|
xc_entry_t xce;
|
|
xc_entry_data_php_t php;
|
|
char *filename;
|
|
|
|
assert(xc_initized);
|
|
|
|
TRACE("type = %d\n", h->type);
|
|
if (!XG(cacher)) {
|
|
op_array = old_compile_file(h, type TSRMLS_CC);
|
|
return op_array;
|
|
}
|
|
|
|
/* {{{ entry_init_key */
|
|
filename = h->opened_path ? h->opened_path : h->filename;
|
|
xce.data.php = &php;
|
|
if (xc_entry_init_key_php(&xce, filename TSRMLS_CC) != SUCCESS) {
|
|
TRACE("failed to init key for %s", filename);
|
|
return old_compile_file(h, type TSRMLS_CC);
|
|
}
|
|
/* }}} */
|
|
|
|
op_array = xc_compile_file_ex(&xce, h, type TSRMLS_CC);
|
|
|
|
xc_entry_free_key_php(&xce TSRMLS_CC);
|
|
|
|
return op_array;
|
|
}
|
|
/* }}} */
|
|
|
|
/* gdb helper functions, but N/A for coredump */
|
|
int xc_is_rw(const void *p) /* {{{ */
|
|
{
|
|
xc_shm_t *shm;
|
|
int i;
|
|
|
|
if (xc_php_caches) {
|
|
for (i = 0; i < xc_php_hcache.size; i ++) {
|
|
shm = xc_php_caches[i]->shm;
|
|
if (shm->handlers->is_readwrite(shm, p)) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (xc_var_caches) {
|
|
for (i = 0; i < xc_var_hcache.size; i ++) {
|
|
shm = xc_var_caches[i]->shm;
|
|
if (shm->handlers->is_readwrite(shm, p)) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
int xc_is_ro(const void *p) /* {{{ */
|
|
{
|
|
xc_shm_t *shm;
|
|
int i;
|
|
|
|
if (xc_php_caches) {
|
|
for (i = 0; i < xc_php_hcache.size; i ++) {
|
|
shm = xc_php_caches[i]->shm;
|
|
if (shm->handlers->is_readonly(shm, p)) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (xc_var_caches) {
|
|
for (i = 0; i < xc_var_hcache.size; i ++) {
|
|
shm = xc_var_caches[i]->shm;
|
|
if (shm->handlers->is_readonly(shm, p)) {
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
int xc_is_shm(const void *p) /* {{{ */
|
|
{
|
|
return xc_is_ro(p) || xc_is_rw(p);
|
|
}
|
|
/* }}} */
|
|
|
|
void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC) /* {{{ */
|
|
{
|
|
zend_llist_add_element(&XG(gc_op_arrays), (void *) gc_op_array);
|
|
}
|
|
/* }}} */
|
|
static void xc_gc_op_array(void *pDest) /* {{{ */
|
|
{
|
|
xc_gc_op_array_t *op_array = (xc_gc_op_array_t *) pDest;
|
|
zend_uint i;
|
|
#ifdef ZEND_ENGINE_2
|
|
if (op_array->arg_info) {
|
|
for (i = 0; i < op_array->num_args; i++) {
|
|
efree((char *) ZSTR_V(op_array->arg_info[i].name));
|
|
if (ZSTR_V(op_array->arg_info[i].class_name)) {
|
|
efree((char *) ZSTR_V(op_array->arg_info[i].class_name));
|
|
}
|
|
}
|
|
efree(op_array->arg_info);
|
|
}
|
|
#endif
|
|
if (op_array->opcodes) {
|
|
efree(op_array->opcodes);
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
/* module helper function */
|
|
static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */
|
|
{
|
|
typedef struct {
|
|
const char *prefix;
|
|
zend_uchar (*getsize)();
|
|
const char *(*get)(zend_uchar i);
|
|
} xc_meminfo_t;
|
|
xc_meminfo_t nameinfos[] = {
|
|
{ "", xc_get_op_type_count, xc_get_op_type },
|
|
{ "", xc_get_data_type_count, xc_get_data_type },
|
|
{ "", xc_get_opcode_count, xc_get_opcode },
|
|
{ "OPSPEC_", xc_get_op_spec_count, xc_get_op_spec },
|
|
{ NULL, NULL, NULL }
|
|
};
|
|
xc_meminfo_t* p;
|
|
zend_uchar i, count;
|
|
char const_name[96];
|
|
int const_name_len;
|
|
int undefdone = 0;
|
|
|
|
for (p = nameinfos; p->getsize; p ++) {
|
|
count = p->getsize();
|
|
for (i = 0; i < count; i ++) {
|
|
const char *name = p->get(i);
|
|
if (!name) continue;
|
|
if (strcmp(name, "UNDEF") == 0) {
|
|
if (undefdone) continue;
|
|
undefdone = 1;
|
|
}
|
|
const_name_len = snprintf(const_name, sizeof(const_name), "XC_%s%s", p->prefix, name);
|
|
zend_register_long_constant(const_name, const_name_len+1, i, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
}
|
|
}
|
|
|
|
zend_register_long_constant(ZEND_STRS("XC_SIZEOF_TEMP_VARIABLE"), sizeof(temp_variable), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
zend_register_stringl_constant(ZEND_STRS("XCACHE_VERSION"), ZEND_STRL(XCACHE_VERSION), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
zend_register_stringl_constant(ZEND_STRS("XCACHE_MODULES"), ZEND_STRL(XCACHE_MODULES), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */
|
|
{
|
|
int i;
|
|
xc_cache_t *cache;
|
|
xc_shm_t *shm;
|
|
|
|
if (!caches) {
|
|
return NULL;
|
|
}
|
|
shm = NULL;
|
|
for (i = 0; i < hcache->size; i ++) {
|
|
cache = caches[i];
|
|
if (cache) {
|
|
if (cache->lck) {
|
|
xc_lock_destroy(cache->lck);
|
|
}
|
|
/* do NOT free
|
|
if (cache->entries) {
|
|
cache->mem->handlers->free(cache->mem, cache->entries);
|
|
}
|
|
cache->mem->handlers->free(cache->mem, cache);
|
|
*/
|
|
shm = cache->shm;
|
|
shm->handlers->memdestroy(cache->mem);
|
|
}
|
|
}
|
|
free(caches);
|
|
return shm;
|
|
}
|
|
/* }}} */
|
|
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) /* {{{ */
|
|
{
|
|
xc_cache_t **caches = NULL, *cache;
|
|
xc_mem_t *mem;
|
|
time_t now = time(NULL);
|
|
int i;
|
|
xc_memsize_t memsize;
|
|
|
|
memsize = shmsize / hcache->size;
|
|
|
|
/* Don't let it break out of mem after ALIGNed
|
|
* This is important for
|
|
* Simply loop until it fit our need
|
|
*/
|
|
while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) {
|
|
if (memsize < ALIGN(1)) {
|
|
CHECK(NULL, "cache too small");
|
|
}
|
|
memsize --;
|
|
}
|
|
|
|
CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t *)), "caches OOM");
|
|
|
|
for (i = 0; i < hcache->size; i ++) {
|
|
CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator");
|
|
CHECK(cache = mem->handlers->calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM");
|
|
CHECK(cache->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM");
|
|
if (hphp) {
|
|
CHECK(cache->phps= mem->handlers->calloc(mem, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM");
|
|
}
|
|
CHECK(cache->lck = xc_lock_init(NULL), "can't create lock");
|
|
|
|
cache->hcache = hcache;
|
|
cache->hentry = hentry;
|
|
cache->hphp = hphp;
|
|
cache->shm = shm;
|
|
cache->mem = mem;
|
|
cache->cacheid = i;
|
|
cache->last_gc_deletes = now;
|
|
cache->last_gc_expires = now;
|
|
caches[i] = cache;
|
|
}
|
|
return caches;
|
|
|
|
err:
|
|
if (caches) {
|
|
xc_cache_destroy(caches, hcache);
|
|
}
|
|
return NULL;
|
|
}
|
|
/* }}} */
|
|
static void xc_destroy() /* {{{ */
|
|
{
|
|
xc_shm_t *shm = NULL;
|
|
|
|
if (old_compile_file) {
|
|
zend_compile_file = old_compile_file;
|
|
old_compile_file = NULL;
|
|
}
|
|
|
|
if (origin_compile_file) {
|
|
zend_compile_file = origin_compile_file;
|
|
origin_compile_file = NULL;
|
|
}
|
|
|
|
if (xc_php_caches) {
|
|
shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache);
|
|
xc_php_caches = NULL;
|
|
}
|
|
|
|
if (xc_var_caches) {
|
|
shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache);
|
|
xc_var_caches = NULL;
|
|
}
|
|
|
|
if (shm) {
|
|
xc_shm_destroy(shm);
|
|
}
|
|
|
|
xc_initized = 0;
|
|
}
|
|
/* }}} */
|
|
static int xc_init(int module_number TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_shm_t *shm;
|
|
xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size);
|
|
|
|
xc_php_caches = xc_var_caches = NULL;
|
|
shm = NULL;
|
|
|
|
if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) {
|
|
zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative");
|
|
goto err;
|
|
}
|
|
|
|
if (xc_php_size || xc_var_size) {
|
|
CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm");
|
|
if (!shm->handlers->can_readonly(shm)) {
|
|
xc_readonly_protection = 0;
|
|
}
|
|
|
|
if (xc_php_size) {
|
|
old_compile_file = zend_compile_file;
|
|
zend_compile_file = xc_compile_file;
|
|
|
|
CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache");
|
|
}
|
|
|
|
if (xc_var_size) {
|
|
CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache");
|
|
}
|
|
}
|
|
return SUCCESS;
|
|
|
|
err:
|
|
if (xc_php_caches || xc_var_caches) {
|
|
xc_destroy();
|
|
/* shm destroied in xc_destroy() */
|
|
}
|
|
else if (shm) {
|
|
xc_destroy();
|
|
xc_shm_destroy(shm);
|
|
}
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
static void xc_request_init(TSRMLS_D) /* {{{ */
|
|
{
|
|
int i;
|
|
|
|
if (!XG(internal_table_copied)) {
|
|
zend_function tmp_func;
|
|
xc_cest_t tmp_cest;
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
zend_hash_destroy(&XG(internal_constant_table));
|
|
#endif
|
|
zend_hash_destroy(&XG(internal_function_table));
|
|
zend_hash_destroy(&XG(internal_class_table));
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
zend_hash_init_ex(&XG(internal_constant_table), 20, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0);
|
|
#endif
|
|
zend_hash_init_ex(&XG(internal_function_table), 100, NULL, NULL, 1, 0);
|
|
zend_hash_init_ex(&XG(internal_class_table), 10, NULL, NULL, 1, 0);
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
xc_copy_internal_zend_constants(&XG(internal_constant_table), EG(zend_constants));
|
|
#endif
|
|
zend_hash_copy(&XG(internal_function_table), CG(function_table), NULL, &tmp_func, sizeof(tmp_func));
|
|
zend_hash_copy(&XG(internal_class_table), CG(class_table), NULL, &tmp_cest, sizeof(tmp_cest));
|
|
|
|
XG(internal_table_copied) = 1;
|
|
}
|
|
if (xc_php_caches && !XG(php_holds)) {
|
|
XG(php_holds) = calloc(xc_php_hcache.size, sizeof(xc_stack_t));
|
|
for (i = 0; i < xc_php_hcache.size; i ++) {
|
|
xc_stack_init(&XG(php_holds[i]));
|
|
}
|
|
}
|
|
|
|
if (xc_var_caches && !XG(var_holds)) {
|
|
XG(var_holds) = calloc(xc_var_hcache.size, sizeof(xc_stack_t));
|
|
for (i = 0; i < xc_var_hcache.size; i ++) {
|
|
xc_stack_init(&XG(var_holds[i]));
|
|
}
|
|
}
|
|
|
|
#ifdef ZEND_ENGINE_2
|
|
zend_llist_init(&XG(gc_op_arrays), sizeof(xc_gc_op_array_t), xc_gc_op_array, 0);
|
|
#endif
|
|
|
|
#if PHP_API_VERSION <= 20041225
|
|
XG(request_time) = time(NULL);
|
|
#else
|
|
XG(request_time) = sapi_get_request_time(TSRMLS_C);
|
|
#endif
|
|
|
|
#ifdef HAVE_XCACHE_COVERAGER
|
|
xc_coverager_request_init(TSRMLS_C);
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
static void xc_request_shutdown(TSRMLS_D) /* {{{ */
|
|
{
|
|
xc_entry_unholds(TSRMLS_C);
|
|
#ifdef ZEND_ENGINE_2
|
|
zend_llist_destroy(&XG(gc_op_arrays));
|
|
#endif
|
|
xc_gc_expires_php(TSRMLS_C);
|
|
xc_gc_expires_var(TSRMLS_C);
|
|
xc_gc_deletes(TSRMLS_C);
|
|
#ifdef HAVE_XCACHE_COVERAGER
|
|
xc_coverager_request_shutdown(TSRMLS_C);
|
|
#endif
|
|
}
|
|
/* }}} */
|
|
/* {{{ PHP_GINIT_FUNCTION(xcache) */
|
|
static
|
|
#ifdef PHP_GINIT_FUNCTION
|
|
PHP_GINIT_FUNCTION(xcache)
|
|
#else
|
|
void xc_init_globals(zend_xcache_globals* xcache_globals TSRMLS_DC)
|
|
#endif
|
|
{
|
|
memset(xcache_globals, 0, sizeof(zend_xcache_globals));
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
zend_hash_init_ex(&xcache_globals->internal_constant_table, 1, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0);
|
|
#endif
|
|
zend_hash_init_ex(&xcache_globals->internal_function_table, 1, NULL, NULL, 1, 0);
|
|
zend_hash_init_ex(&xcache_globals->internal_class_table, 1, NULL, NULL, 1, 0);
|
|
}
|
|
/* }}} */
|
|
/* {{{ PHP_GSHUTDOWN_FUNCTION(xcache) */
|
|
static
|
|
#ifdef PHP_GSHUTDOWN_FUNCTION
|
|
PHP_GSHUTDOWN_FUNCTION(xcache)
|
|
#else
|
|
void xc_shutdown_globals(zend_xcache_globals* xcache_globals TSRMLS_DC)
|
|
#endif
|
|
{
|
|
int i;
|
|
|
|
if (xcache_globals->php_holds != NULL) {
|
|
for (i = 0; i < xc_php_hcache.size; i ++) {
|
|
xc_stack_destroy(&xcache_globals->php_holds[i]);
|
|
}
|
|
free(xcache_globals->php_holds);
|
|
xcache_globals->php_holds = NULL;
|
|
}
|
|
|
|
if (xcache_globals->var_holds != NULL) {
|
|
for (i = 0; i < xc_var_hcache.size; i ++) {
|
|
xc_stack_destroy(&xcache_globals->var_holds[i]);
|
|
}
|
|
free(xcache_globals->var_holds);
|
|
xcache_globals->var_holds = NULL;
|
|
}
|
|
|
|
if (xcache_globals->internal_table_copied) {
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
zend_hash_destroy(&xcache_globals->internal_constant_table);
|
|
#endif
|
|
zend_hash_destroy(&xcache_globals->internal_function_table);
|
|
zend_hash_destroy(&xcache_globals->internal_class_table);
|
|
}
|
|
}
|
|
/* }}} */
|
|
|
|
/* user functions */
|
|
static int xcache_admin_auth_check(TSRMLS_D) /* {{{ */
|
|
{
|
|
zval **server = NULL;
|
|
zval **user = NULL;
|
|
zval **pass = NULL;
|
|
char *admin_user = NULL;
|
|
char *admin_pass = NULL;
|
|
HashTable *ht;
|
|
|
|
if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) {
|
|
admin_user = NULL;
|
|
}
|
|
if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) {
|
|
admin_pass = NULL;
|
|
}
|
|
|
|
if (admin_user == NULL || admin_pass == NULL) {
|
|
php_error_docref(XCACHE_WIKI_URL "/InstallAdministration" TSRMLS_CC, E_ERROR,
|
|
"xcache.admin.user and/or xcache.admin.pass settings is not configured."
|
|
" Make sure you've modified the correct php ini file for your php used in webserver.");
|
|
zend_bailout();
|
|
}
|
|
if (strlen(admin_pass) != 32) {
|
|
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));
|
|
zend_bailout();
|
|
}
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC);
|
|
#endif
|
|
if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) {
|
|
php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted");
|
|
zend_bailout();
|
|
}
|
|
ht = HASH_OF((*server));
|
|
|
|
if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) {
|
|
user = NULL;
|
|
}
|
|
else if (Z_TYPE_PP(user) != IS_STRING) {
|
|
user = NULL;
|
|
}
|
|
|
|
if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) {
|
|
pass = NULL;
|
|
}
|
|
else if (Z_TYPE_PP(pass) != IS_STRING) {
|
|
pass = NULL;
|
|
}
|
|
|
|
if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) {
|
|
PHP_MD5_CTX context;
|
|
char md5str[33];
|
|
unsigned char digest[16];
|
|
|
|
PHP_MD5Init(&context);
|
|
PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass));
|
|
PHP_MD5Final(digest, &context);
|
|
|
|
md5str[0] = '\0';
|
|
make_digest(md5str, digest);
|
|
if (strcmp(admin_pass, md5str) == 0) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
#define STR "HTTP/1.0 401 Unauthorized"
|
|
sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC);
|
|
#undef STR
|
|
#define STR "WWW-authenticate: Basic Realm=\"XCache Administration\""
|
|
sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC);
|
|
#undef STR
|
|
#define STR "Content-type: text/html; charset=UTF-8"
|
|
sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC);
|
|
#undef STR
|
|
ZEND_PUTS("<html>\n");
|
|
ZEND_PUTS("<head><title>XCache Authentication Failed</title></head>\n");
|
|
ZEND_PUTS("<body>\n");
|
|
ZEND_PUTS("<h1>XCache Authentication Failed</h1>\n");
|
|
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");
|
|
ZEND_PUTS("<ul>\n");
|
|
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");
|
|
ZEND_PUTS("<li>Make sure the md5 password is generated correctly. You may use <a href=\"mkpassword.php\">mkpassword.php</a></li>\n");
|
|
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");
|
|
ZEND_PUTS("</ul>\n");
|
|
ZEND_PUTS("Check <a href=\"" XCACHE_WIKI_URL "/InstallAdministration\">XCache wiki page</a> for more information.\n");
|
|
ZEND_PUTS("</body>\n");
|
|
ZEND_PUTS("</html>\n");
|
|
|
|
zend_bailout();
|
|
return 0;
|
|
}
|
|
/* }}} */
|
|
/* {{{ xcache_admin_operate */
|
|
typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type;
|
|
static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS)
|
|
{
|
|
long type;
|
|
int size;
|
|
xc_cache_t **caches, *cache;
|
|
long id = 0;
|
|
|
|
xcache_admin_auth_check(TSRMLS_C);
|
|
|
|
if (!xc_initized) {
|
|
RETURN_NULL();
|
|
}
|
|
|
|
if (optype == XC_OP_COUNT) {
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) {
|
|
return;
|
|
}
|
|
}
|
|
else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) {
|
|
return;
|
|
}
|
|
|
|
switch (type) {
|
|
case XC_TYPE_PHP:
|
|
size = xc_php_hcache.size;
|
|
caches = xc_php_caches;
|
|
break;
|
|
|
|
case XC_TYPE_VAR:
|
|
size = xc_var_hcache.size;
|
|
caches = xc_var_caches;
|
|
break;
|
|
|
|
default:
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type);
|
|
RETURN_FALSE;
|
|
}
|
|
|
|
switch (optype) {
|
|
case XC_OP_COUNT:
|
|
RETURN_LONG(size)
|
|
break;
|
|
|
|
case XC_OP_INFO:
|
|
case XC_OP_LIST:
|
|
if (id < 0 || id >= size) {
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists");
|
|
RETURN_FALSE;
|
|
}
|
|
|
|
array_init(return_value);
|
|
|
|
cache = caches[id];
|
|
ENTER_LOCK(cache) {
|
|
if (optype == XC_OP_INFO) {
|
|
xc_fillinfo_dmz(type, cache, return_value TSRMLS_CC);
|
|
}
|
|
else {
|
|
xc_filllist_dmz(cache, return_value TSRMLS_CC);
|
|
}
|
|
} LEAVE_LOCK(cache);
|
|
break;
|
|
case XC_OP_CLEAR:
|
|
{
|
|
xc_entry_t *e, *next;
|
|
int i, c;
|
|
|
|
if (id < 0 || id >= size) {
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists");
|
|
RETURN_FALSE;
|
|
}
|
|
|
|
cache = caches[id];
|
|
ENTER_LOCK(cache) {
|
|
for (i = 0, c = cache->hentry->size; i < c; i ++) {
|
|
for (e = cache->entries[i]; e; e = next) {
|
|
next = e->next;
|
|
xc_entry_remove_dmz(e TSRMLS_CC);
|
|
}
|
|
cache->entries[i] = NULL;
|
|
}
|
|
} LEAVE_LOCK(cache);
|
|
xc_gc_deletes(TSRMLS_C);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
assert(0);
|
|
}
|
|
}
|
|
/* }}} */
|
|
/* {{{ proto int xcache_count(int type)
|
|
Return count of cache on specified cache type */
|
|
PHP_FUNCTION(xcache_count)
|
|
{
|
|
xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU);
|
|
}
|
|
/* }}} */
|
|
/* {{{ proto array xcache_info(int type, int id)
|
|
Get cache info by id on specified cache type */
|
|
PHP_FUNCTION(xcache_info)
|
|
{
|
|
xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU);
|
|
}
|
|
/* }}} */
|
|
/* {{{ proto array xcache_list(int type, int id)
|
|
Get cache entries list by id on specified cache type */
|
|
PHP_FUNCTION(xcache_list)
|
|
{
|
|
xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU);
|
|
}
|
|
/* }}} */
|
|
/* {{{ proto array xcache_clear_cache(int type, int id)
|
|
Clear cache by id on specified cache type */
|
|
PHP_FUNCTION(xcache_clear_cache)
|
|
{
|
|
xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU);
|
|
}
|
|
/* }}} */
|
|
|
|
#define VAR_DISABLED_WARNING() do { \
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "xcache.var_size is either 0 or too small to enable var data caching"); \
|
|
} while (0)
|
|
|
|
static int xc_entry_init_key_var(xc_entry_t *xce, zval *name TSRMLS_DC) /* {{{ */
|
|
{
|
|
xc_hash_value_t hv;
|
|
int cacheid;
|
|
|
|
switch (Z_TYPE_P(name)) {
|
|
#ifdef IS_UNICODE
|
|
case IS_UNICODE:
|
|
#endif
|
|
case IS_STRING:
|
|
break;
|
|
default:
|
|
#ifdef IS_UNICODE
|
|
convert_to_unicode(name);
|
|
#else
|
|
convert_to_string(name);
|
|
#endif
|
|
}
|
|
#ifdef IS_UNICODE
|
|
xce->name_type = name->type;
|
|
#endif
|
|
xce->name = name->value;
|
|
|
|
hv = xc_entry_hash_var(xce TSRMLS_CC);
|
|
|
|
cacheid = (hv & xc_var_hcache.mask);
|
|
xce->cache = xc_var_caches[cacheid];
|
|
hv >>= xc_var_hcache.bits;
|
|
xce->hvalue = (hv & xc_var_hentry.mask);
|
|
|
|
xce->type = XC_TYPE_VAR;
|
|
return SUCCESS;
|
|
}
|
|
/* }}} */
|
|
/* {{{ proto mixed xcache_get(string name)
|
|
Get cached data by specified name */
|
|
PHP_FUNCTION(xcache_get)
|
|
{
|
|
xc_entry_t xce, *stored_xce;
|
|
xc_entry_data_var_t var;
|
|
zval *name;
|
|
int found = 0;
|
|
|
|
if (!xc_var_caches) {
|
|
VAR_DISABLED_WARNING();
|
|
RETURN_NULL();
|
|
}
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) {
|
|
return;
|
|
}
|
|
xce.data.var = &var;
|
|
xc_entry_init_key_var(&xce, name TSRMLS_CC);
|
|
|
|
ENTER_LOCK(xce.cache) {
|
|
stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC);
|
|
if (stored_xce) {
|
|
if (!VAR_ENTRY_EXPIRED(stored_xce)) {
|
|
found = 1;
|
|
xc_processor_restore_zval(return_value, stored_xce->data.var->value, stored_xce->data.var->have_references TSRMLS_CC);
|
|
/* return */
|
|
break;
|
|
}
|
|
else {
|
|
xc_entry_remove_dmz(stored_xce TSRMLS_CC);
|
|
}
|
|
}
|
|
|
|
RETVAL_NULL();
|
|
} LEAVE_LOCK(xce.cache);
|
|
if (found) {
|
|
xc_cache_hit_dmz(xce.cache TSRMLS_CC);
|
|
}
|
|
else {
|
|
xce.cache->misses ++;
|
|
}
|
|
} |