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.

408 lines
11 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 <stddef.h>')
  11. EXPORT(`#include "xcache.h"')
  12. EXPORT(`#include "mod_cacher/xc_cache.h"')
  13. EXPORT(`#include "xcache/xc_shm.h"')
  14. EXPORT(`#include "xcache/xc_allocator.h"')
  15. #include "xc_processor.h"
  16. #include "xcache/xc_const_string.h"
  17. #include "xcache/xc_utils.h"
  18. #include "util/xc_align.h"
  19. #include "util/xc_trace.h"
  20. #include "xcache_globals.h"
  21. #if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
  22. extern unsigned int zend_hash_canary;
  23. #endif
  24. define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
  25. define(`COUNTOF_zend_uint', `1')
  26. define(`SIZEOF_int', `sizeof(int)')
  27. define(`COUNTOF_int', `1')
  28. define(`SIZEOF_zend_function', `sizeof(zend_function)')
  29. define(`COUNTOF_zend_function', `1')
  30. define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
  31. define(`COUNTOF_zval_ptr', `1')
  32. define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
  33. define(`COUNTOF_zval_ptr_nullable', `1')
  34. define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
  35. define(`COUNTOF_zend_trait_alias_ptr', `1')
  36. define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
  37. define(`COUNTOF_zend_trait_precedence_ptr', `1')
  38. define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
  39. define(`COUNTOF_xc_entry_name_t', `1')
  40. define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
  41. define(`COUNTOF_xc_ztstring', `1')
  42. ifdef(`XCACHE_ENABLE_TEST', `
  43. #undef NDEBUG
  44. #include <assert.h>
  45. m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
  46. ', `
  47. m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
  48. ')
  49. ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
  50. #ifndef NDEBUG
  51. # undef inline
  52. #define inline
  53. #endif
  54. typedef zval *zval_ptr;
  55. typedef zval *zval_ptr_nullable;
  56. typedef char *xc_ztstring;
  57. #ifdef ZEND_ENGINE_2_4
  58. typedef zend_trait_alias *zend_trait_alias_ptr;
  59. typedef zend_trait_precedence *zend_trait_precedence_ptr;
  60. #endif
  61. #ifdef ZEND_ENGINE_2_3
  62. typedef int last_brk_cont_t;
  63. #else
  64. typedef zend_uint last_brk_cont_t;
  65. #endif
  66. typedef zend_uchar xc_zval_type_t;
  67. typedef int xc_op_type;
  68. typedef zend_uchar xc_opcode;
  69. #ifdef IS_UNICODE
  70. typedef UChar zstr_uchar;
  71. #endif
  72. typedef char zstr_char;
  73. #define MAX_DUP_STR_LEN 256
  74. #define ptradd(type, ptr, ptrdiff) ((type) (((char *) (ptr)) + (ptrdiff)))
  75. #define ptrsub(ptr1, ptr2) (((char *) (ptr1)) - ((char *) (ptr2)))
  76. #ifdef NDEBUG
  77. #define notnullable(ptr) (ptr)
  78. #else
  79. static inline void *notnullable(const void *ptr)
  80. {
  81. assert(ptr);
  82. return (void *) ptr;
  83. }
  84. #endif
  85. dnl }}}
  86. dnl {{{ _xc_processor_t
  87. typedef struct _xc_processor_t {
  88. char *p;
  89. size_t size;
  90. HashTable strings;
  91. HashTable zvalptrs;
  92. zend_bool handle_reference; /* enable if to deal with reference */
  93. zend_bool have_references;
  94. ptrdiff_t relocatediff;
  95. const xc_entry_php_t *entry_php_src;
  96. const xc_entry_php_t *entry_php_dst;
  97. const xc_entry_data_php_t *php_src;
  98. const xc_entry_data_php_t *php_dst;
  99. const zend_class_entry *cache_ce;
  100. zend_ulong cache_class_index;
  101. const zend_op_array *active_op_array_src;
  102. zend_op_array *active_op_array_dst;
  103. const zend_class_entry *active_class_entry_src;
  104. zend_class_entry *active_class_entry_dst;
  105. zend_uint active_class_index;
  106. zend_uint active_op_array_index;
  107. const xc_op_array_info_t *active_op_array_infos_src;
  108. zend_bool readonly_protection; /* wheather it's present */
  109. IFAUTOCHECK(xc_vector_t allocsizes;)
  110. } xc_processor_t;
  111. dnl }}}
  112. EXPORT(`typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t;')
  113. /* {{{ memsetptr */
  114. IFAUTOCHECK(`dnl
  115. static void *memsetptr(void *mem, void *content, size_t n)
  116. {
  117. void **p = (void **) mem;
  118. void **end = (void **) ((char *) mem + n);
  119. while (p < end - sizeof(content)) {
  120. *p = content;
  121. p += sizeof(content);
  122. }
  123. if (p < end) {
  124. memset(p, -1, end - p);
  125. }
  126. return mem;
  127. }
  128. ')
  129. /* }}} */
  130. #ifdef HAVE_XCACHE_DPRINT
  131. static void xc_dprint_indent(int indent) /* {{{ */
  132. {
  133. int i;
  134. for (i = 0; i < indent; i ++) {
  135. fprintf(stderr, " ");
  136. }
  137. }
  138. /* }}} */
  139. static void xc_dprint_str_len(const char *str, int len) /* {{{ */
  140. {
  141. const unsigned char *p = (const unsigned char *) str;
  142. int i;
  143. for (i = 0; i < len; i ++) {
  144. if (p[i] < 32 || p[i] == 127) {
  145. fprintf(stderr, "\\%03o", (unsigned int) p[i]);
  146. }
  147. else {
  148. fputc(p[i], stderr);
  149. }
  150. }
  151. }
  152. /* }}} */
  153. #endif
  154. static inline size_t xc_zstrlen_char(const_zstr s) /* {{{ */
  155. {
  156. return strlen(ZSTR_S(s));
  157. }
  158. /* }}} */
  159. #ifdef IS_UNICODE
  160. static inline size_t xc_zstrlen_uchar(zstr s) /* {{{ */
  161. {
  162. return u_strlen(ZSTR_U(s));
  163. }
  164. /* }}} */
  165. static inline size_t xc_zstrlen(int type, const_zstr s) /* {{{ */
  166. {
  167. return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
  168. }
  169. /* }}} */
  170. #else
  171. /* {{{ xc_zstrlen */
  172. #define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
  173. /* }}} */
  174. #endif
  175. #undef C_RELAYLINE
  176. #define C_RELAYLINE
  177. IFAUTOCHECK(`
  178. #undef C_RELAYLINE
  179. #define C_RELAYLINE , __LINE__
  180. ')
  181. static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline') TSRMLS_DC) { /* {{{ */
  182. pushdef(`PROCESSOR_TYPE', `calc')
  183. pushdef(`__LINE__', `relayline')
  184. size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
  185. long dummy = 1;
  186. if (realsize > MAX_DUP_STR_LEN) {
  187. ALLOC(, char, realsize)
  188. }
  189. else if (zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
  190. /* new string */
  191. ALLOC(, char, realsize)
  192. }
  193. IFAUTOCHECK(`
  194. else {
  195. dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
  196. }
  197. ')
  198. popdef(`__LINE__')
  199. popdef(`PROCESSOR_TYPE')
  200. }
  201. /* }}} */
  202. static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
  203. pushdef(`PROCESSOR_TYPE', `store')
  204. pushdef(`__LINE__', `relayline')
  205. size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
  206. zstr ret, *pret;
  207. if (realsize > MAX_DUP_STR_LEN) {
  208. ALLOC(ZSTR_V(ret), char, realsize)
  209. memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
  210. return ret;
  211. }
  212. if (zend_u_hash_find(&processor->strings, type, str, (uint) size, (void **) &pret) == SUCCESS) {
  213. TRACE("found old string %s:%ld %p", str, size, *pret);
  214. return *pret;
  215. }
  216. /* new string */
  217. ALLOC(ZSTR_V(ret), char, realsize)
  218. memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
  219. zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &ret, sizeof(zstr), NULL);
  220. TRACE("stored new string %s:%ld %p", str, size, ret);
  221. return ret;
  222. popdef(`__LINE__')
  223. popdef(`PROCESSOR_TYPE')
  224. }
  225. /* }}} */
  226. /* {{{ xc_get_class_num
  227. * return class_index + 1
  228. */
  229. static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
  230. zend_uint i;
  231. const xc_entry_data_php_t *php = processor->php_src;
  232. zend_class_entry *ceptr;
  233. if (processor->cache_ce == ce) {
  234. return processor->cache_class_index + 1;
  235. }
  236. for (i = 0; i < php->classinfo_cnt; i ++) {
  237. ceptr = CestToCePtr(php->classinfos[i].cest);
  238. if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
  239. processor->cache_ce = ceptr;
  240. processor->cache_class_index = i;
  241. assert(i <= processor->active_class_index);
  242. return i + 1;
  243. }
  244. }
  245. assert(0);
  246. return (zend_ulong) -1;
  247. }
  248. define(`xc_get_class_num', `IFSTORE(``xc_get_class_num'($@)',``xc_get_class_num' can be use in store only')')
  249. /* }}} */
  250. #ifdef ZEND_ENGINE_2
  251. static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) { /* {{{ */
  252. /* must be parent or currrent class */
  253. assert(class_num > 0);
  254. assert(class_num <= processor->active_class_index + 1);
  255. return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
  256. }
  257. define(`xc_get_class', `IFRESTORE(``xc_get_class'($@)',``xc_get_class' can be use in restore only')')
  258. /* }}} */
  259. #endif
  260. #ifdef ZEND_ENGINE_2
  261. /* fix method on store */
  262. static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
  263. {
  264. zend_function *zf = (zend_function *) dst;
  265. zend_class_entry *ce = processor->active_class_entry_dst;
  266. const zend_class_entry *srcce = processor->active_class_entry_src;
  267. /* Fixing up the default functions for objects here since
  268. * we need to compare with the newly allocated functions
  269. *
  270. * caveat: a sub-class method can have the same name as the
  271. * parent~s constructor and create problems.
  272. */
  273. if (zf->common.fn_flags & ZEND_ACC_CTOR) {
  274. if (!ce->constructor) {
  275. ce->constructor = zf;
  276. }
  277. }
  278. else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
  279. ce->destructor = zf;
  280. }
  281. else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
  282. ce->clone = zf;
  283. }
  284. else {
  285. pushdef(`SET_IF_SAME_NAMEs', `
  286. SET_IF_SAME_NAME(__get);
  287. SET_IF_SAME_NAME(__set);
  288. #ifdef ZEND_ENGINE_2_1
  289. SET_IF_SAME_NAME(__unset);
  290. SET_IF_SAME_NAME(__isset);
  291. #endif
  292. SET_IF_SAME_NAME(__call);
  293. #ifdef ZEND_CALLSTATIC_FUNC_NAME
  294. SET_IF_SAME_NAME(__callstatic);
  295. #endif
  296. #if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
  297. SET_IF_SAME_NAME(__tostring);
  298. #endif
  299. #if defined(ZEND_ENGINE_2_6)
  300. SET_IF_SAME_NAME(__debugInfo);
  301. #endif
  302. ')
  303. #ifdef IS_UNICODE
  304. if (UG(unicode)) {
  305. #define SET_IF_SAME_NAME(member) \
  306. do { \
  307. if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
  308. ce->member = zf; \
  309. } \
  310. } \
  311. while(0)
  312. SET_IF_SAME_NAMEs()
  313. #undef SET_IF_SAME_NAME
  314. }
  315. else
  316. #endif
  317. do {
  318. #define SET_IF_SAME_NAME(member) \
  319. do { \
  320. if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
  321. ce->member = zf; \
  322. } \
  323. } \
  324. while(0)
  325. SET_IF_SAME_NAMEs()
  326. #undef SET_IF_SAME_NAME
  327. } while (0);
  328. popdef(`SET_IF_SAME_NAMEs')
  329. }
  330. }
  331. /* }}} */
  332. #endif
  333. /* {{{ call op_array ctor handler */
  334. extern zend_bool xc_have_op_array_ctor;
  335. static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  336. {
  337. if (extension->op_array_ctor) {
  338. extension->op_array_ctor(op_array);
  339. }
  340. }
  341. /* }}} */
  342. /* {{{ field name checker */
  343. IFAUTOCHECK(`dnl
  344. 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)
  345. {
  346. int errors = 0;
  347. if (assert_names_count) {
  348. size_t i;
  349. Bucket *b;
  350. for (i = 0; i < assert_names_count; ++i) {
  351. if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], (uint) strlen(assert_names[i]) + 1)) {
  352. fprintf(stderr
  353. , "Error: missing field at %s `#'%d %s`' : %s\n"
  354. , file, line, functionName
  355. , assert_names[i]
  356. );
  357. ++errors;
  358. }
  359. }
  360. for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
  361. int known = 0;
  362. for (i = 0; i < assert_names_count; ++i) {
  363. if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
  364. known = 1;
  365. break;
  366. }
  367. }
  368. if (!known) {
  369. fprintf(stderr
  370. , "Error: unknown field at %s `#'%d %s`' : %s\n"
  371. , file, line, functionName
  372. , BUCKET_KEY_S(b)
  373. );
  374. ++errors;
  375. }
  376. }
  377. }
  378. return errors;
  379. }
  380. ')
  381. /* }}} */