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.

554 lines
16 KiB

  1. dnl {{{ === program start ========================================
  2. divert(0)
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include "php.h"
  6. #include "zend_extensions.h"
  7. #include "zend_compile.h"
  8. #include "zend_API.h"
  9. #include "zend_ini.h"
  10. /* export: #include "xcache.h" :export */
  11. #include "xcache.h"
  12. /* export: #include "mod_cacher/xc_cache.h" :export */
  13. #include "mod_cacher/xc_cache.h"
  14. /* export: #include "xcache/xc_shm.h" :export */
  15. #include "xcache/xc_shm.h"
  16. /* export: #include "xcache/xc_allocator.h" :export */
  17. #include "xcache/xc_allocator.h"
  18. #include "xcache/xc_const_string.h"
  19. #include "xcache/xc_utils.h"
  20. #include "util/xc_align.h"
  21. #include "xc_processor.h"
  22. #include "xcache_globals.h"
  23. #if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
  24. extern unsigned int zend_hash_canary;
  25. #endif
  26. define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
  27. define(`COUNTOF_zend_uint', `1')
  28. define(`SIZEOF_int', `sizeof(int)')
  29. define(`COUNTOF_int', `1')
  30. define(`SIZEOF_zend_function', `sizeof(zend_function)')
  31. define(`COUNTOF_zend_function', `1')
  32. define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
  33. define(`COUNTOF_zval_ptr', `1')
  34. define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
  35. define(`COUNTOF_zval_ptr_nullable', `1')
  36. define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
  37. define(`COUNTOF_zend_trait_alias_ptr', `1')
  38. define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
  39. define(`COUNTOF_zend_trait_precedence_ptr', `1')
  40. define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
  41. define(`COUNTOF_xc_entry_name_t', `1')
  42. define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
  43. define(`COUNTOF_xc_ztstring', `1')
  44. ifdef(`XCACHE_ENABLE_TEST', `
  45. #undef NDEBUG
  46. #include <assert.h>
  47. m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
  48. ', `
  49. m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
  50. ')
  51. ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
  52. sinclude(builddir`/structinfo.m4')
  53. #ifndef NDEBUG
  54. # undef inline
  55. #define inline
  56. #endif
  57. typedef zval *zval_ptr;
  58. typedef zval *zval_ptr_nullable;
  59. typedef char *xc_ztstring;
  60. #ifdef ZEND_ENGINE_2_4
  61. typedef zend_trait_alias *zend_trait_alias_ptr;
  62. typedef zend_trait_precedence *zend_trait_precedence_ptr;
  63. #endif
  64. #ifdef ZEND_ENGINE_2_3
  65. typedef int last_brk_cont_t;
  66. #else
  67. typedef zend_uint last_brk_cont_t;
  68. #endif
  69. typedef zend_uchar xc_zval_type_t;
  70. typedef int xc_op_type;
  71. typedef zend_uchar xc_opcode;
  72. #ifdef IS_UNICODE
  73. typedef UChar zstr_uchar;
  74. #endif
  75. typedef char zstr_char;
  76. #define MAX_DUP_STR_LEN 256
  77. dnl }}}
  78. /* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
  79. struct _xc_processor_t {
  80. char *p;
  81. size_t size;
  82. HashTable strings;
  83. HashTable zvalptrs;
  84. zend_bool reference; /* enable if to deal with reference */
  85. zend_bool have_references;
  86. const xc_entry_php_t *entry_php_src;
  87. const xc_entry_php_t *entry_php_dst;
  88. const xc_entry_data_php_t *php_src;
  89. const xc_entry_data_php_t *php_dst;
  90. xc_shm_t *shm;
  91. xc_allocator_t *allocator;
  92. const zend_class_entry *cache_ce;
  93. zend_uint cache_class_index;
  94. const zend_op_array *active_op_array_src;
  95. zend_op_array *active_op_array_dst;
  96. const zend_class_entry *active_class_entry_src;
  97. zend_class_entry *active_class_entry_dst;
  98. zend_uint active_class_index;
  99. zend_uint active_op_array_index;
  100. const xc_op_array_info_t *active_op_array_infos_src;
  101. zend_bool readonly_protection; /* wheather it's present */
  102. IFAUTOCHECK(xc_stack_t allocsizes;)
  103. };
  104. /* }}} */
  105. /* export: typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t; :export {{{ */
  106. /* }}} */
  107. /* {{{ memsetptr */
  108. IFAUTOCHECK(`dnl
  109. static void *memsetptr(void *mem, void *content, size_t n)
  110. {
  111. void **p = (void **) mem;
  112. void **end = (void **) ((char *) mem + n);
  113. while (p < end - sizeof(content)) {
  114. *p = content;
  115. p += sizeof(content);
  116. }
  117. if (p < end) {
  118. memset(p, -1, end - p);
  119. }
  120. return mem;
  121. }
  122. ')
  123. /* }}} */
  124. #ifdef HAVE_XCACHE_DPRINT
  125. static void xc_dprint_indent(int indent) /* {{{ */
  126. {
  127. int i;
  128. for (i = 0; i < indent; i ++) {
  129. fprintf(stderr, " ");
  130. }
  131. }
  132. /* }}} */
  133. static void xc_dprint_str_len(const char *str, int len) /* {{{ */
  134. {
  135. const unsigned char *p = (const unsigned char *) str;
  136. int i;
  137. for (i = 0; i < len; i ++) {
  138. if (p[i] < 32 || p[i] == 127) {
  139. fprintf(stderr, "\\%03o", (unsigned int) p[i]);
  140. }
  141. else {
  142. fputc(p[i], stderr);
  143. }
  144. }
  145. }
  146. /* }}} */
  147. #endif
  148. /* {{{ xc_zstrlen_char */
  149. static inline size_t xc_zstrlen_char(const_zstr s)
  150. {
  151. return strlen(ZSTR_S(s));
  152. }
  153. /* }}} */
  154. #ifdef IS_UNICODE
  155. /* {{{ xc_zstrlen_uchar */
  156. static inline size_t xc_zstrlen_uchar(zstr s)
  157. {
  158. return u_strlen(ZSTR_U(s));
  159. }
  160. /* }}} */
  161. /* {{{ xc_zstrlen */
  162. static inline size_t xc_zstrlen(int type, const_zstr s)
  163. {
  164. return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
  165. }
  166. /* }}} */
  167. #else
  168. /* {{{ xc_zstrlen */
  169. #define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
  170. /* }}} */
  171. #endif
  172. /* {{{ xc_calc_string_n */
  173. REDEF(`PROCESSOR_TYPE', `calc')
  174. #undef C_RELAYLINE
  175. #define C_RELAYLINE
  176. IFAUTOCHECK(`
  177. #undef C_RELAYLINE
  178. #define C_RELAYLINE , __LINE__
  179. ')
  180. static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
  181. pushdef(`__LINE__', `relayline')
  182. size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
  183. long dummy = 1;
  184. if (realsize > MAX_DUP_STR_LEN) {
  185. ALLOC(, char, realsize)
  186. }
  187. else if (zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
  188. /* new string */
  189. ALLOC(, char, realsize)
  190. }
  191. IFAUTOCHECK(`
  192. else {
  193. dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
  194. }
  195. ')
  196. popdef(`__LINE__')
  197. }
  198. /* }}} */
  199. /* {{{ xc_store_string_n */
  200. REDEF(`PROCESSOR_TYPE', `store')
  201. static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
  202. pushdef(`__LINE__', `relayline')
  203. size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
  204. zstr ret, *pret;
  205. if (realsize > MAX_DUP_STR_LEN) {
  206. ALLOC(ZSTR_V(ret), char, realsize)
  207. memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
  208. return ret;
  209. }
  210. if (zend_u_hash_find(&processor->strings, type, str, (uint) size, (void **) &pret) == SUCCESS) {
  211. return *pret;
  212. }
  213. /* new string */
  214. ALLOC(ZSTR_V(ret), char, realsize)
  215. memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
  216. zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &ret, sizeof(zstr), NULL);
  217. return ret;
  218. popdef(`__LINE__')
  219. }
  220. /* }}} */
  221. /* {{{ xc_get_class_num
  222. * return class_index + 1
  223. */
  224. static zend_uint xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
  225. zend_uint i;
  226. const xc_entry_data_php_t *php = processor->php_src;
  227. zend_class_entry *ceptr;
  228. if (processor->cache_ce == ce) {
  229. return processor->cache_class_index + 1;
  230. }
  231. for (i = 0; i < php->classinfo_cnt; i ++) {
  232. ceptr = CestToCePtr(php->classinfos[i].cest);
  233. if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
  234. processor->cache_ce = ceptr;
  235. processor->cache_class_index = i;
  236. return i + 1;
  237. }
  238. }
  239. assert(0);
  240. return (zend_ulong) -1;
  241. }
  242. define(`xc_get_class_num', `xc_get_class_numNOTDEFINED')
  243. /* }}} */
  244. /* {{{ xc_get_class */
  245. #ifdef ZEND_ENGINE_2
  246. static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
  247. /* must be parent or currrent class */
  248. assert(class_num <= processor->active_class_index + 1);
  249. return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
  250. }
  251. #endif
  252. define(`xc_get_class', `xc_get_classNOTDEFINED')
  253. /* }}} */
  254. #ifdef ZEND_ENGINE_2
  255. /* fix method on store */
  256. static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
  257. {
  258. zend_function *zf = (zend_function *) dst;
  259. zend_class_entry *ce = processor->active_class_entry_dst;
  260. const zend_class_entry *srcce = processor->active_class_entry_src;
  261. /* Fixing up the default functions for objects here since
  262. * we need to compare with the newly allocated functions
  263. *
  264. * caveat: a sub-class method can have the same name as the
  265. * parent~s constructor and create problems.
  266. */
  267. if (zf->common.fn_flags & ZEND_ACC_CTOR) {
  268. if (!ce->constructor) {
  269. ce->constructor = zf;
  270. }
  271. }
  272. else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
  273. ce->destructor = zf;
  274. }
  275. else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
  276. ce->clone = zf;
  277. }
  278. else {
  279. pushdef(`SET_IF_SAME_NAMEs', `
  280. SET_IF_SAME_NAME(__get);
  281. SET_IF_SAME_NAME(__set);
  282. #ifdef ZEND_ENGINE_2_1
  283. SET_IF_SAME_NAME(__unset);
  284. SET_IF_SAME_NAME(__isset);
  285. #endif
  286. SET_IF_SAME_NAME(__call);
  287. #ifdef ZEND_CALLSTATIC_FUNC_NAME
  288. SET_IF_SAME_NAME(__callstatic);
  289. #endif
  290. #if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
  291. SET_IF_SAME_NAME(__tostring);
  292. #endif
  293. ')
  294. #ifdef IS_UNICODE
  295. if (UG(unicode)) {
  296. #define SET_IF_SAME_NAME(member) \
  297. do { \
  298. if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
  299. ce->member = zf; \
  300. } \
  301. } \
  302. while(0)
  303. SET_IF_SAME_NAMEs()
  304. #undef SET_IF_SAME_NAME
  305. }
  306. else
  307. #endif
  308. do {
  309. #define SET_IF_SAME_NAME(member) \
  310. do { \
  311. if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
  312. ce->member = zf; \
  313. } \
  314. } \
  315. while(0)
  316. SET_IF_SAME_NAMEs()
  317. #undef SET_IF_SAME_NAME
  318. } while (0);
  319. popdef(`SET_IF_SAME_NAMEs')
  320. }
  321. }
  322. /* }}} */
  323. #endif
  324. /* {{{ call op_array ctor handler */
  325. extern zend_bool xc_have_op_array_ctor;
  326. static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  327. {
  328. if (extension->op_array_ctor) {
  329. extension->op_array_ctor(op_array);
  330. }
  331. }
  332. /* }}} */
  333. /* {{{ field name checker */
  334. IFAUTOCHECK(`dnl
  335. static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, size_t assert_names_count, HashTable *done_names)
  336. {
  337. int errors = 0;
  338. if (assert_names_count) {
  339. size_t i;
  340. Bucket *b;
  341. for (i = 0; i < assert_names_count; ++i) {
  342. if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], (uint) strlen(assert_names[i]) + 1)) {
  343. fprintf(stderr
  344. , "Error: missing field at %s `#'%d %s`' : %s\n"
  345. , file, line, functionName
  346. , assert_names[i]
  347. );
  348. ++errors;
  349. }
  350. }
  351. for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
  352. int known = 0;
  353. for (i = 0; i < assert_names_count; ++i) {
  354. if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
  355. known = 1;
  356. break;
  357. }
  358. }
  359. if (!known) {
  360. fprintf(stderr
  361. , "Error: unknown field at %s `#'%d %s`' : %s\n"
  362. , file, line, functionName
  363. , BUCKET_KEY_S(b)
  364. );
  365. ++errors;
  366. }
  367. }
  368. }
  369. return errors;
  370. }
  371. ')
  372. /* }}} */
  373. dnl ================ export API
  374. define(`DEFINE_STORE_API', `
  375. /* export: $1 *xc_processor_store_$1(xc_shm_t *shm, xc_allocator_t *allocator, $1 *src TSRMLS_DC); :export {{{ */
  376. $1 *xc_processor_store_$1(xc_shm_t *shm, xc_allocator_t *allocator, $1 *src TSRMLS_DC) {
  377. $1 *dst;
  378. xc_processor_t processor;
  379. memset(&processor, 0, sizeof(processor));
  380. processor.reference = 1;
  381. processor.shm = shm;
  382. processor.allocator = allocator;
  383. IFAUTOCHECK(`xc_stack_init(&processor.allocsizes);')
  384. /* calc size */ {
  385. zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
  386. if (processor.reference) {
  387. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  388. }
  389. processor.size = 0;
  390. /* allocate */
  391. processor.size = ALIGN(processor.size + sizeof(src[0]));
  392. xc_calc_$1(&processor, src TSRMLS_CC);
  393. if (processor.reference) {
  394. zend_hash_destroy(&processor.zvalptrs);
  395. }
  396. zend_hash_destroy(&processor.strings);
  397. }
  398. src->ifelse(
  399. `$1', `xc_entry_data_php_t', `',
  400. `', `', entry.)size = processor.size;
  401. ifelse(
  402. `$1', `xc_entry_var_t', `src->have_references = processor.have_references;',
  403. `$1', `xc_entry_data_php_t', `src->have_references = processor.have_references;'
  404. )
  405. IFAUTOCHECK(`xc_stack_reverse(&processor.allocsizes);')
  406. /* store {{{ */
  407. {
  408. IFAUTOCHECK(`char *oldp;')
  409. zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
  410. if (processor.reference) {
  411. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  412. }
  413. /* allocator :) */
  414. processor.p = (char *) processor.allocator->vtable->malloc(processor.allocator, processor.size);
  415. if (processor.p == NULL) {
  416. dst = NULL;
  417. goto err_alloc;
  418. }
  419. IFAUTOCHECK(`oldp = processor.p;')
  420. assert(processor.p == (char *) ALIGN(processor.p));
  421. /* allocate */
  422. dst = ($1 *) processor.p;
  423. processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
  424. xc_store_$1(&processor, dst, src TSRMLS_CC);
  425. IFAUTOCHECK(` {
  426. size_t unexpected = processor.p - oldp;
  427. size_t expected = processor.size;
  428. if (unexpected != processor.size) {
  429. fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
  430. abort();
  431. }
  432. }')
  433. err_alloc:
  434. if (processor.reference) {
  435. zend_hash_destroy(&processor.zvalptrs);
  436. }
  437. zend_hash_destroy(&processor.strings);
  438. }
  439. /* }}} */
  440. IFAUTOCHECK(`xc_stack_destroy(&processor.allocsizes);')
  441. return dst;
  442. }
  443. /* }}} */
  444. ')
  445. DEFINE_STORE_API(`xc_entry_var_t')
  446. DEFINE_STORE_API(`xc_entry_php_t')
  447. DEFINE_STORE_API(`xc_entry_data_php_t')
  448. /* export: xc_entry_php_t *xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC); :export {{{ */
  449. xc_entry_php_t *xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC) {
  450. xc_processor_t processor;
  451. memset(&processor, 0, sizeof(processor));
  452. xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
  453. return dst;
  454. }
  455. /* }}} */
  456. /* export: xc_entry_data_php_t *xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC); :export {{{ */
  457. xc_entry_data_php_t *xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC) {
  458. xc_processor_t processor;
  459. memset(&processor, 0, sizeof(processor));
  460. processor.readonly_protection = readonly_protection;
  461. /* this function is used for php data only */
  462. if (src->have_references) {
  463. processor.reference = 1;
  464. }
  465. processor.entry_php_src = entry_php;
  466. if (processor.reference) {
  467. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  468. }
  469. xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
  470. if (processor.reference) {
  471. zend_hash_destroy(&processor.zvalptrs);
  472. }
  473. return dst;
  474. }
  475. /* }}} */
  476. /* export: xc_entry_var_t *xc_processor_restore_xc_entry_var_t(xc_entry_var_t *dst, const xc_entry_var_t *src TSRMLS_DC); :export {{{ */
  477. xc_entry_var_t *xc_processor_restore_xc_entry_var_t(xc_entry_var_t *dst, const xc_entry_var_t *src TSRMLS_DC) {
  478. xc_processor_t processor;
  479. memset(&processor, 0, sizeof(processor));
  480. xc_restore_xc_entry_var_t(&processor, dst, src TSRMLS_CC);
  481. return dst;
  482. }
  483. /* }}} */
  484. /* export: zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC); :export {{{ */
  485. zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC) {
  486. xc_processor_t processor;
  487. memset(&processor, 0, sizeof(processor));
  488. processor.reference = have_references;
  489. if (processor.reference) {
  490. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  491. dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
  492. zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
  493. }
  494. xc_restore_zval(&processor, dst, src TSRMLS_CC);
  495. if (processor.reference) {
  496. zend_hash_destroy(&processor.zvalptrs);
  497. }
  498. return dst;
  499. }
  500. /* }}} */
  501. /* export: void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC); :export {{{ */
  502. #ifdef HAVE_XCACHE_DPRINT
  503. void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC) {
  504. IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
  505. xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
  506. }
  507. #endif
  508. /* }}} */