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.
 
 
 
 
 
 

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