XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2210 lines
54 KiB

#undef DEBUG
/* {{{ 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 "zend_extensions.h"
#include "SAPI.h"
#include "xcache.h"
#include "optimizer.h"
#include "coverager.h"
#include "disassembler.h"
#include "align.h"
#include "stack.h"
#include "xcache_globals.h"
#include "processor.h"
#include "utils.h"
#include "const_string.h"
#include "opcode_spec.h"
#ifdef DEBUG
# undef NDEBUG
# undef inline
# define inline
#else
# ifndef NDEBUG
# define NDEBUG
# endif
#endif
#include <assert.h>
#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(x) do { \
int catched = 0; \
xc_lock(x->lck); \
zend_try { \
do
#define LEAVE_LOCK(x) \
while (0); \
} zend_catch { \
catched = 1; \
} zend_end_try(); \
xc_unlock(x->lck); \
} while(0)
/* }}} */
/* {{{ globals */
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};
/* 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 zend_compile_file_t *origin_compile_file;
static zend_bool xc_test = 0;
static zend_bool xc_readonly_protection = 0;
static zend_bool xc_module_gotup = 0;
static zend_bool xc_zend_extension_gotup = 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 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 {
xc_entry_data_php_t *ap = a->data.php;
xc_entry_data_php_t *bp = b->data.php;
return ap->inode == bp->inode
&& ap->device == bp->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;
}
else {
return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0;
}
#else
return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0;
#endif
} while(0);
default:
assert(0);
}
return 0;
}
/* }}} */
static void xc_entry_free_dmz(volatile xc_entry_t *xce) /* {{{ */
{
xc_mem_free(xce->cache->mem, (xc_entry_t *)xce);
}
/* }}} */
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_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
{
xc_entry_t **last = &(xce->cache->entries[xce->hvalue]);
xc_entry_t *p;
for (p = *last; p; last = &(p->next), p = p->next) {
if (xc_entry_equal_dmz(xce, p)) {
*last = p->next;
xce->cache->entries_count ++;
if (p->refcount == 0) {
xc_entry_free_dmz(p);
}
else {
p->next = p->cache->deletes;
p->cache->deletes = p;
p->dtime = XG(request_time);
xce->cache->deletes_count ++;
}
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->data.php->mtime == xce->data.php->mtime) {
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) /* {{{ */
{
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
/* helper functions for user functions */
static void xc_fillinfo_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
{
zval *blocks;
const xc_block_t *b;
#ifndef NDEBUG
xc_memsize_t avail = 0;
#endif
xc_mem_t *mem = cache->mem;
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("cached"), cache->entries_count);
add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count);
MAKE_STD_ZVAL(blocks);
array_init(blocks);
add_assoc_long_ex(return_value, ZEND_STRS("size"), xc_mem_size(mem));
add_assoc_long_ex(return_value, ZEND_STRS("avail"), xc_mem_avail(mem));
add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection);
for (b = xc_mem_freeblock_first(mem); b; b = xc_mem_freeblock_next(b)) {
zval *bi;
MAKE_STD_ZVAL(bi);
array_init(bi);
add_assoc_long_ex(bi, ZEND_STRS("size"), xc_mem_block_size(b));
add_assoc_long_ex(bi, ZEND_STRS("offset"), xc_mem_block_offset(mem, b));
add_next_index_zval(blocks, bi);
#ifndef NDEBUG
avail += b->size;
#endif
}
add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks);
assert(avail == xc_mem_avail(mem));
}
/* }}} */
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("size"), entry->size);
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("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, 1);
#endif
switch (entry->type) {
case XC_TYPE_PHP:
php = entry->data.php;
add_assoc_long_ex(ei, ZEND_STRS("sourcesize"), php->sourcesize);
#ifdef HAVE_INODE
add_assoc_long_ex(ei, ZEND_STRS("device"), php->device);
add_assoc_long_ex(ei, ZEND_STRS("inode"), php->inode);
#endif
add_assoc_long_ex(ei, ZEND_STRS("mtime"), php->mtime);
add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt);
add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt);
break;
case XC_TYPE_VAR:
var = entry->data.var;
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;
#ifndef ZEND_ENGINE_2
/* new ptr which is stored inside CG(class_table) */
xc_cest_t **new_cest_ptrs = (xc_cest_t **)do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt);
#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 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) ce->parent) - 1;
assert(class_idx < i);
ci->cest.parent = new_cest_ptrs[class_idx];
}
new_cest_ptrs[i] =
#endif
xc_install_class(xce->name.str.val, &ci->cest,
UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC);
}
i = 1;
zend_hash_add(&EG(included_files), xce->name.str.val, xce->name.str.len+1, (void *)&i, sizeof(int), NULL);
zend_llist_add_element(&CG(open_files), h);
#ifndef ZEND_ENGINE_2
free_alloca(new_cest_ptrs);
#endif
return p->op_array;
}
/* }}} */
static void xc_entry_gc_real(xc_cache_t **caches, int size TSRMLS_DC) /* {{{ */
{
time_t t = XG(request_time);
int i;
xc_cache_t *cache;
typedef xc_entry_t *xc_delete_t;
xc_delete_t p, *last;
for (i = 0; i < size; i ++) {
cache = caches[i];
ENTER_LOCK(cache) {
if (cache->deletes) {
last = (xc_delete_t *) &cache->deletes;
for (p = *last; p; p = p->next) {
if (t - p->dtime > 3600) {
p->refcount = 0;
/* issue warning here */
}
if (p->refcount == 0) {
*last = p->next;
cache->deletes_count --;
xc_entry_free_dmz(p);
}
else {
last = &(p->next);
}
}
}
} LEAVE_LOCK(cache);
}
}
/* }}} */
static void xc_entry_gc(TSRMLS_D) /* {{{ */
{
xc_entry_gc_real(xc_php_caches, xc_php_hcache.size TSRMLS_CC);
xc_entry_gc_real(xc_var_caches, xc_var_hcache.size TSRMLS_CC);
}
/* }}} */
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];
if (xc_stack_size(s)) {
cache = ((xc_entry_t *)xc_stack_top(s))->cache;
ENTER_LOCK(cache) {
while (xc_stack_size(holds)) {
xce = (xc_entry_t*) xc_stack_pop(holds);
xce->refcount --;
assert(xce->refcount >= 0);
}
} LEAVE_LOCK(cache);
}
}
}
/* }}} */
static void xc_entry_unholds(TSRMLS_D) /* {{{ */
{
xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC);
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[1024];
char *paths, *path;
char *tokbuf;
int size = strlen(include_path) + 1;
char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' };
paths = (char *)do_alloca(size);
memcpy(paths, include_path, size);
for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) {
if (strlen(path) + strlen(filename) + 1 > 1024) {
continue;
}
snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
if (VCWD_STAT(filepath, pbuf) == 0) {
free_alloca(paths);
return 0;
}
}
free_alloca(paths);
return 1;
}
/* }}} */
#define HASH(i) (i)
#define HASH_USTR_L(t, s, l) HASH(zend_u_inline_hash_func(t, s, (l + 1) * sizeof(UChar)))
#define HASH_STR_L(s, l) HASH(zend_inline_hash_func(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_entry_hash_var(xc_entry_t *xce) /* {{{ */
{
return UNISW(NOTHING, UG(unicode) ? HASH_USTR_L(xce->name_type, (char *)xce->name.ustr.val, xce->name.ustr.len) :)
HASH_STR_L(xce->name.str.val, xce->name.str.len);
}
/* }}} */
static inline xc_hash_value_t xc_entry_hash_php(xc_entry_t *xce) /* {{{ */
{
#ifdef HAVE_INODE
return HASH(xce->data.php->device + xce->data.php->inode);
#else
return xc_entry_hash_var(xce);
#endif
}
/* }}} */
static int xc_entry_init_key_php(xc_entry_t *xce, char *filename TSRMLS_DC) /* {{{ */
{
struct stat buf, *pbuf;
xc_hash_value_t hv;
int cacheid;
xc_entry_data_php_t *php;
char *ptr;
if (!filename || !SG(request_info).path_translated) {
return 0;
}
do {
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) {
break;
}
}
/* absolute path */
pbuf = &buf;
if (IS_ABSOLUTE_PATH(filename, strlen(filename))) {
if (VCWD_STAT(filename, pbuf) != 0) {
return 0;
}
break;
}
/* 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 0;
}
break;
}
not_relative_path:
/* use include_path */
if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != 0) {
return 0;
}
} while (0);
if (XG(request_time) - pbuf->st_mtime < 2) {
return 0;
}
UNISW(NOTHING, xce->name_type = IS_STRING;)
xce->name.str.val = filename;
xce->name.str.len = strlen(filename);
php = xce->data.php;
php->mtime = pbuf->st_mtime;
#ifdef HAVE_INODE
php->device = pbuf->st_dev;
php->inode = pbuf->st_ino;
#endif
php->sourcesize = pbuf->st_size;
hv = xc_entry_hash_php(xce);
cacheid = (hv & xc_php_hcache.mask);
xce->cache = xc_php_caches[cacheid];
hv >>= xc_php_hcache.bits;
xce->hvalue = (hv & xc_php_hentry.mask);
xce->type = XC_TYPE_PHP;
return 1;
}
/* }}} */
static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */
{
xc_sandbox_t sandbox;
zend_op_array *op_array;
xc_entry_t xce, *stored_xce;
xc_entry_data_php_t php;
xc_cache_t *cache;
zend_bool clogged = 0;
zend_bool catched = 0;
char *filename;
if (!xc_initized) {
assert(0);
}
if (!XG(cacher)) {
op_array = origin_compile_file(h, type TSRMLS_CC);
#ifdef HAVE_XCACHE_OPTIMIZER
if (XG(optimizer)) {
xc_optimize(op_array TSRMLS_CC);
}
#endif
return op_array;
}
/* {{{ prepare key
* include_once() and require_once() gives us opened_path
* however, include() and require() non-absolute path which break
* included_files, and may confuse with (include|require)_once
* -- Xuefer
*/
filename = h->opened_path ? h->opened_path : h->filename;
xce.data.php = &php;
if (!xc_entry_init_key_php(&xce, filename TSRMLS_CC)) {
return origin_compile_file(h, type TSRMLS_CC);
}
cache = xce.cache;
/* }}} */
/* {{{ restore */
/* stale precheck */
if (cache->compiling) {
cache->clogs ++; /* is it safe here? */
return origin_compile_file(h, type TSRMLS_CC);
}
stored_xce = NULL;
op_array = NULL;
ENTER_LOCK(cache) {
/* clogged */
if (cache->compiling) {
cache->clogs ++;
op_array = NULL;
clogged = 1;
break;
}
stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC);
/* found */
if (stored_xce) {
#ifdef DEBUG
fprintf(stderr, "found %s, catch it\n", stored_xce->name.str.val);
#endif
xc_entry_hold_php_dmz(stored_xce TSRMLS_CC);
cache->hits ++;
break;
}
cache->compiling = XG(request_time);
cache->misses ++;
} LEAVE_LOCK(cache);
/* found */
if (stored_xce) {
goto restore;
}
/* clogged */
if (clogged) {
return origin_compile_file(h, type TSRMLS_CC);
}
/* }}} */
/* {{{ compile */
#ifdef DEBUG
fprintf(stderr, "compiling %s\n", filename);
#endif
/* make compile inside sandbox */
xc_sandbox_init(&sandbox, filename TSRMLS_CC);
zend_try {
op_array = origin_compile_file(h, type TSRMLS_CC);
} zend_catch {
catched = 1;
} zend_end_try();
if (catched) {
goto err_bailout;
}
if (op_array == NULL) {
goto err_oparray;
}
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);
}
#ifdef HAVE_XCACHE_OPTIMIZER
if (XG(optimizer)) {
xc_optimize(op_array TSRMLS_CC);
}
#endif
php.op_array = op_array;
php.funcinfo_cnt = zend_hash_num_elements(CG(function_table));
php.classinfo_cnt = zend_hash_num_elements(CG(class_table));
php.funcinfos = ECALLOC_N(php.funcinfos, php.funcinfo_cnt);
if (!php.funcinfos) {
goto err_func;
}
php.classinfos = ECALLOC_N(php.classinfos, php.classinfo_cnt);
if (!php.classinfos) {
goto err_class;
}
/* }}} */
/* {{{ shallow copy, pointers only */ {
Bucket *b;
unsigned int i;
b = CG(function_table)->pListHead;
for (i = 0; b; i ++, b = b->pListNext) {
xc_funcinfo_t *fi = &php.funcinfos[i];
assert(i < php.funcinfo_cnt);
assert(b->pData);
memcpy(&fi->func, b->pData, sizeof(zend_function));
UNISW(NOTHING, fi->type = b->key.type;)
fi->key = BUCKET_KEY(b);
fi->key_size = b->nKeyLength;
}
b = CG(class_table)->pListHead;
for (i = 0; b; i ++, b = b->pListNext) {
xc_classinfo_t *ci = &php.classinfos[i];
assert(i < php.classinfo_cnt);
assert(b->pData);
memcpy(&ci->cest, b->pData, sizeof(xc_cest_t));
UNISW(NOTHING, ci->type = b->key.type;)
ci->key = BUCKET_KEY(b);
ci->key_size = b->nKeyLength;
/* need to fix inside store */
}
}
/* }}} */
xc_entry_gc(TSRMLS_C);
ENTER_LOCK(cache) { /* {{{ store/add entry */
stored_xce = xc_entry_store_dmz(&xce TSRMLS_CC);
} LEAVE_LOCK(cache);
/* }}} */
#ifdef DEBUG
fprintf(stderr, "stored\n");
#endif
efree(xce.data.php->classinfos);
err_class:
efree(xce.data.php->funcinfos);
err_func:
err_oparray:
err_bailout:
if (xc_test && stored_xce) {
/* no install, keep open_files too for h */
xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
sandbox.tmp_open_files->dtor = NULL;
}
else {
xc_sandbox_free(&sandbox, 1 TSRMLS_CC);
}
ENTER_LOCK(cache) {
cache->compiling = 0;
} LEAVE_LOCK(cache);
if (catched) {
zend_bailout();
}
if (xc_test && stored_xce) {
goto restore;
}
return op_array;
restore:
if (php_check_open_basedir(stored_xce->name.str.val TSRMLS_CC) != 0) {
return NULL;
}
#ifdef DEBUG
fprintf(stderr, "restoring\n");
#endif
xc_processor_restore_xc_entry_t(&xce, stored_xce, xc_readonly_protection TSRMLS_CC);
op_array = xc_entry_install(&xce, h TSRMLS_CC);
efree(xce.data.php->funcinfos);
efree(xce.data.php->classinfos);
efree(xce.data.php);
#ifdef DEBUG
fprintf(stderr, "restored\n");
#endif
return op_array;
}
/* }}} */
/* gdb helper functions, but N/A for coredump */
int xc_is_rw(const void *p) /* {{{ */
{
int i;
if (!xc_initized) {
return 0;
}
for (i = 0; i < xc_php_hcache.size; i ++) {
if (xc_shm_is_readwrite(xc_php_caches[i]->shm, p)) {
return 1;
}
}
for (i = 0; i < xc_var_hcache.size; i ++) {
if (xc_shm_is_readwrite(xc_var_caches[i]->shm, p)) {
return 1;
}
}
return 0;
}
/* }}} */
int xc_is_ro(const void *p) /* {{{ */
{
int i;
if (!xc_initized) {
return 0;
}
for (i = 0; i < xc_php_hcache.size; i ++) {
if (xc_shm_is_readonly(xc_php_caches[i]->shm, p)) {
return 1;
}
}
for (i = 0; i < xc_var_hcache.size; i ++) {
if (xc_shm_is_readonly(xc_var_caches[i]->shm, p)) {
return 1;
}
}
return 0;
}
/* }}} */
int xc_is_shm(const void *p) /* {{{ */
{
return xc_is_ro(p) || xc_is_rw(p);
}
/* }}} */
/* 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);
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) {
xc_mem_free(cache->mem, cache->entries);
}
xc_mem_free(cache->mem, cache);
*/
xc_mem_destroy(cache->mem);
shm = cache->shm;
}
}
free(caches);
return shm;
}
/* }}} */
static xc_cache_t **xc_cache_init(xc_shm_t *shm, char *ptr, xc_hash_t *hcache, xc_hash_t *hentry, xc_shmsize_t shmsize) /* {{{ */
{
xc_cache_t **caches = NULL, *cache;
xc_mem_t *mem;
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 = xc_mem_init(ptr, memsize), "Failed init memory allocator");
ptr += memsize;
CHECK(cache = xc_mem_calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM");
CHECK(cache->entries = xc_mem_calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM");
CHECK(cache->lck = xc_lock_init(NULL), "can't create lock");
cache->hcache = hcache;
cache->hentry = hentry;
cache->shm = shm;
cache->mem = mem;
cache->cacheid = i;
caches[i] = cache;
}
assert(ptr <= (char*)xc_shm_ptr(shm) + shmsize);
return caches;
err:
if (caches) {
xc_cache_destroy(caches, hcache);
}
return NULL;
}
/* }}} */
static void xc_destroy() /* {{{ */
{
xc_shm_t *shm = 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);
}
}
/* }}} */
static int xc_init(int module_number TSRMLS_DC) /* {{{ */
{
xc_shm_t *shm;
char *ptr;
xc_php_caches = xc_var_caches = NULL;
if (xc_php_size || xc_var_size) {
CHECK(shm = xc_shm_init(xc_mmap_path, ALIGN(xc_php_size) + ALIGN(xc_var_size), xc_readonly_protection), "Cannot create shm");
if (!xc_shm_can_readonly(shm)) {
xc_readonly_protection = 0;
}
ptr = (char *)xc_shm_ptr(shm);
if (xc_php_size) {
origin_compile_file = zend_compile_file;
zend_compile_file = xc_compile_file;
CHECK(xc_php_caches = xc_cache_init(shm, ptr, &xc_php_hcache, &xc_php_hentry, xc_php_size), "failed init opcode cache");
ptr += ALIGN(xc_php_size);
}
if (xc_var_size) {
CHECK(xc_var_caches = xc_cache_init(shm, ptr, &xc_var_hcache, &xc_var_hentry, xc_var_size), "failed init variable cache");
}
}
return 1;
err:
if (xc_php_caches || xc_var_caches) {
xc_destroy();
/* shm destroied */
}
else if (shm) {
xc_shm_destroy(shm);
}
return 0;
}
/* }}} */
static void xc_request_init(TSRMLS_D) /* {{{ */
{
int i;
if (xc_php_hcache.size && !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_hcache.size && !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]));
}
}
if (XG(cacher)) {
#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 HAVE_XCACHE_COVERAGER
xc_coverager_request_shutdown(TSRMLS_C);