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.
 
 
 
 
 
 

4148 lines
122 KiB

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