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.

157 lines
3.5 KiB

  1. #include "disassembler.h"
  2. #include "xcache.h"
  3. #include "utils.h"
  4. #include "processor.h"
  5. #define return_value dst
  6. #ifndef HAVE_XCACHE_OPCODE_SPEC_DEF
  7. #error disassembler cannot be built without xcache/opcode_spec_def.h
  8. #endif
  9. static void xc_dasm(zval *dst, zend_op_array *op_array TSRMLS_DC) /* {{{ */
  10. {
  11. Bucket *b;
  12. zval *zv, *list;
  13. xc_compile_result_t cr;
  14. int bufsize = 2;
  15. char *buf;
  16. int keysize;
  17. xc_compile_result_init_cur(&cr, op_array TSRMLS_CC);
  18. xc_apply_op_array(&cr, (apply_func_t) xc_undo_pass_two TSRMLS_CC);
  19. xc_apply_op_array(&cr, (apply_func_t) xc_fix_opcode TSRMLS_CC);
  20. /* go */
  21. array_init(dst);
  22. ALLOC_INIT_ZVAL(zv);
  23. array_init(zv);
  24. xc_dasm_zend_op_array(zv, op_array TSRMLS_CC);
  25. add_assoc_zval_ex(dst, ZEND_STRS("op_array"), zv);
  26. ALLOC_INIT_ZVAL(list);
  27. array_init(list);
  28. xc_dasm_HashTable_zend_function(list, CG(function_table) TSRMLS_CC);
  29. add_assoc_zval_ex(dst, ZEND_STRS("function_table"), list);
  30. buf = emalloc(bufsize);
  31. ALLOC_INIT_ZVAL(list);
  32. array_init(list);
  33. for (b = CG(class_table)->pListHead; b; b = b->pListNext) {
  34. ALLOC_INIT_ZVAL(zv);
  35. array_init(zv);
  36. xc_dasm_zend_class_entry(zv, CestToCePtr(*(xc_cest_t *)b->pData) TSRMLS_CC);
  37. keysize = BUCKET_KEY_SIZE(b) + 2;
  38. if (keysize > bufsize) {
  39. do {
  40. bufsize *= 2;
  41. } while (keysize > bufsize);
  42. buf = erealloc(buf, bufsize);
  43. }
  44. memcpy(buf, BUCKET_KEY(b), keysize);
  45. buf[keysize - 2] = buf[keysize - 1] = ""[0];
  46. keysize = b->nKeyLength;
  47. #ifdef IS_UNICODE
  48. if (BUCKET_KEY_TYPE(b) == IS_UNICODE) {
  49. if (buf[0] == ""[0] && buf[1] == ""[0]) {
  50. keysize ++;
  51. }
  52. } else
  53. #endif
  54. {
  55. if (buf[0] == ""[0]) {
  56. keysize ++;
  57. }
  58. }
  59. add_u_assoc_zval_ex(list, BUCKET_KEY_TYPE(b), buf, b->nKeyLength, zv);
  60. }
  61. efree(buf);
  62. add_assoc_zval_ex(dst, ZEND_STRS("class_table"), list);
  63. /*xc_apply_op_array(&cr, (apply_func_t) xc_redo_pass_two TSRMLS_CC);*/
  64. xc_compile_result_free(&cr);
  65. return;
  66. }
  67. /* }}} */
  68. void xc_dasm_string(zval *dst, zval *source TSRMLS_DC) /* {{{ */
  69. {
  70. int catched;
  71. zend_op_array *op_array = NULL;
  72. xc_sandbox_t sandbox;
  73. char *eval_name = zend_make_compiled_string_description("runtime-created function" TSRMLS_CC);
  74. xc_sandbox_init(&sandbox, eval_name TSRMLS_CC);
  75. catched = 0;
  76. zend_try {
  77. op_array = compile_string(source, eval_name TSRMLS_CC);
  78. } zend_catch {
  79. catched = 1;
  80. } zend_end_try();
  81. if (catched || !op_array) {
  82. goto err_compile;
  83. }
  84. xc_dasm(dst, op_array TSRMLS_CC);
  85. /* free */
  86. efree(eval_name);
  87. destroy_op_array(op_array TSRMLS_CC);
  88. efree(op_array);
  89. xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
  90. return;
  91. err_compile:
  92. efree(eval_name);
  93. xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
  94. RETURN_FALSE;
  95. }
  96. /* }}} */
  97. void xc_dasm_file(zval *dst, const char *filename TSRMLS_DC) /* {{{ */
  98. {
  99. int catched;
  100. zend_op_array *op_array = NULL;
  101. xc_sandbox_t sandbox;
  102. zval *zfilename;
  103. MAKE_STD_ZVAL(zfilename);
  104. zfilename->value.str.val = estrdup(filename);
  105. zfilename->value.str.len = strlen(filename);
  106. zfilename->type = IS_STRING;
  107. xc_sandbox_init(&sandbox, zfilename->value.str.val TSRMLS_CC);
  108. catched = 0;
  109. zend_try {
  110. op_array = compile_filename(ZEND_REQUIRE, zfilename TSRMLS_CC);
  111. } zend_catch {
  112. catched = 1;
  113. } zend_end_try();
  114. if (catched || !op_array) {
  115. goto err_compile;
  116. }
  117. xc_dasm(dst, op_array TSRMLS_CC);
  118. /* free */
  119. destroy_op_array(op_array TSRMLS_CC);
  120. efree(op_array);
  121. xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
  122. zval_dtor(zfilename);
  123. FREE_ZVAL(zfilename);
  124. return;
  125. err_compile:
  126. xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
  127. zval_dtor(zfilename);
  128. FREE_ZVAL(zfilename);
  129. RETURN_FALSE;
  130. }
  131. /* }}} */