| [1] | 1 | #include "php.h" |
|---|
| [305] | 2 | #include "xcache.h" |
|---|
| [1] | 3 | |
|---|
| [543] | 4 | #ifdef XCACHE_DEBUG |
|---|
| [305] | 5 | # define IFDEBUG(x) (x) |
|---|
| [349] | 6 | int xc_vtrace(const char *fmt, va_list args); |
|---|
| [305] | 7 | int xc_trace(const char *fmt, ...) ZEND_ATTRIBUTE_PTR_FORMAT(printf, 1, 2); |
|---|
| [349] | 8 | |
|---|
| 9 | # ifdef ZEND_WIN32 |
|---|
| 10 | static inline int TRACE(const char *fmt, ...) |
|---|
| 11 | { |
|---|
| 12 | va_list args; |
|---|
| 13 | int ret; |
|---|
| 14 | |
|---|
| 15 | va_start(args, fmt); |
|---|
| 16 | ret = xc_vtrace(fmt, args); |
|---|
| 17 | va_end(args); |
|---|
| 18 | return ret; |
|---|
| 19 | } |
|---|
| 20 | # else |
|---|
| 21 | # define TRACE(fmt, ...) \ |
|---|
| 22 | xc_trace("%s:%d: " fmt "\r\n", __FILE__, __LINE__, __VA_ARGS__) |
|---|
| 23 | # endif /* ZEND_WIN32 */ |
|---|
| [305] | 24 | # undef NDEBUG |
|---|
| 25 | # undef inline |
|---|
| 26 | # define inline |
|---|
| [543] | 27 | #else /* XCACHE_DEBUG */ |
|---|
| [349] | 28 | |
|---|
| 29 | # ifdef ZEND_WIN32 |
|---|
| [350] | 30 | static inline int TRACE_DUMMY(const char *fmt, ...) |
|---|
| [349] | 31 | { |
|---|
| 32 | return 0; |
|---|
| 33 | } |
|---|
| [351] | 34 | # define TRACE 1 ? 0 : TRACE_DUMMY |
|---|
| [349] | 35 | # else |
|---|
| 36 | # define TRACE(fmt, ...) do { } while (0) |
|---|
| 37 | # endif /* ZEND_WIN32 */ |
|---|
| 38 | |
|---|
| [305] | 39 | # define IFDEBUG(x) do { } while (0) |
|---|
| [543] | 40 | #endif /* XCACHE_DEBUG */ |
|---|
| [305] | 41 | #include <assert.h> |
|---|
| 42 | |
|---|
| [1] | 43 | typedef struct { |
|---|
| 44 | int alloc; |
|---|
| 45 | zend_op_array *op_array; |
|---|
| 46 | HashTable *function_table; |
|---|
| 47 | HashTable *class_table; |
|---|
| 48 | } xc_compile_result_t; |
|---|
| 49 | |
|---|
| 50 | xc_compile_result_t *xc_compile_result_init(xc_compile_result_t *cr, |
|---|
| 51 | zend_op_array *op_array, |
|---|
| 52 | HashTable *function_table, |
|---|
| 53 | HashTable *class_table); |
|---|
| 54 | void xc_compile_result_free(xc_compile_result_t *cr); |
|---|
| 55 | xc_compile_result_t *xc_compile_result_init_cur(xc_compile_result_t *cr, zend_op_array *op_array TSRMLS_DC); |
|---|
| 56 | /* apply func */ |
|---|
| 57 | int xc_apply_function(zend_function *zf, apply_func_t applyer TSRMLS_DC); |
|---|
| 58 | int xc_apply_class(zend_class_entry *ce, apply_func_t applyer TSRMLS_DC); |
|---|
| 59 | int xc_apply_op_array(xc_compile_result_t *cr, apply_func_t applyer TSRMLS_DC); |
|---|
| 60 | |
|---|
| 61 | int xc_undo_pass_two(zend_op_array *op_array TSRMLS_DC); |
|---|
| 62 | int xc_redo_pass_two(zend_op_array *op_array TSRMLS_DC); |
|---|
| 63 | int xc_fix_opcode(zend_op_array *op_array TSRMLS_DC); |
|---|
| 64 | int xc_undo_fix_opcode(zend_op_array *op_array TSRMLS_DC); |
|---|
| 65 | zend_uchar xc_get_fixed_opcode(zend_uchar opcode, int line); |
|---|
| 66 | |
|---|
| [212] | 67 | int xc_foreach_early_binding_class(zend_op_array *op_array, void (*callback)(zend_op *opline, int oplineno, void *data TSRMLS_DC), void *data TSRMLS_DC); |
|---|
| 68 | |
|---|
| [1] | 69 | /* installer */ |
|---|
| [95] | 70 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| [846] | 71 | void xc_install_constant(ZEND_24(const) char *filename, zend_constant *constant, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC); |
|---|
| [95] | 72 | #endif |
|---|
| [846] | 73 | void xc_install_function(ZEND_24(const) char *filename, zend_function *func, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC); |
|---|
| 74 | ZESW(xc_cest_t *, void) xc_install_class(ZEND_24(const) char *filename, xc_cest_t *cest, int oplineno, zend_uchar type, const24_zstr key, uint len, ulong h TSRMLS_DC); |
|---|
| [1] | 75 | |
|---|
| [669] | 76 | #if defined(E_STRICT) || defined(E_DEPRECATED) |
|---|
| 77 | #define XCACHE_ERROR_CACHING |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| [1] | 80 | /* sandbox */ |
|---|
| 81 | typedef struct { |
|---|
| 82 | int alloc; |
|---|
| [846] | 83 | ZEND_24(const) char *filename; |
|---|
| [1] | 84 | |
|---|
| 85 | HashTable orig_included_files; |
|---|
| 86 | HashTable *tmp_included_files; |
|---|
| 87 | |
|---|
| [95] | 88 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 89 | HashTable *orig_zend_constants; |
|---|
| 90 | HashTable tmp_zend_constants; |
|---|
| 91 | #endif |
|---|
| [1] | 92 | HashTable *orig_function_table; |
|---|
| 93 | HashTable *orig_class_table; |
|---|
| [268] | 94 | HashTable *orig_auto_globals; |
|---|
| [1] | 95 | HashTable tmp_function_table; |
|---|
| 96 | HashTable tmp_class_table; |
|---|
| [268] | 97 | HashTable tmp_auto_globals; |
|---|
| [588] | 98 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 99 | Bucket *tmp_internal_constant_tail; |
|---|
| 100 | #endif |
|---|
| [344] | 101 | Bucket *tmp_internal_function_tail; |
|---|
| 102 | Bucket *tmp_internal_class_tail; |
|---|
| [496] | 103 | |
|---|
| [669] | 104 | #ifdef XCACHE_ERROR_CACHING |
|---|
| [496] | 105 | int orig_user_error_handler_error_reporting; |
|---|
| [522] | 106 | void (*orig_zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args); |
|---|
| [496] | 107 | zend_uint compilererror_cnt; |
|---|
| 108 | zend_uint compilererror_size; |
|---|
| 109 | xc_compilererror_t *compilererrors; |
|---|
| 110 | #endif |
|---|
| [548] | 111 | |
|---|
| 112 | #ifdef ZEND_COMPILE_IGNORE_INTERNAL_CLASSES |
|---|
| 113 | zend_uint orig_compiler_options; |
|---|
| 114 | #endif |
|---|
| [1] | 115 | } xc_sandbox_t; |
|---|
| 116 | |
|---|
| [405] | 117 | typedef enum _xc_install_action_t { |
|---|
| 118 | XC_NoInstall, |
|---|
| 119 | XC_Install, |
|---|
| 120 | XC_InstallNoBinding |
|---|
| 121 | } xc_install_action_t; |
|---|
| 122 | |
|---|
| [345] | 123 | void xc_zend_class_add_ref(zend_class_entry ZESW(*ce, **ce)); |
|---|
| [846] | 124 | xc_sandbox_t *xc_sandbox_init(xc_sandbox_t *sandbox, ZEND_24(const) char *filename TSRMLS_DC); |
|---|
| [405] | 125 | void xc_sandbox_free(xc_sandbox_t *sandbox, xc_install_action_t install TSRMLS_DC); |
|---|
| [640] | 126 | |
|---|
| 127 | typedef zend_bool (*xc_if_func_t)(void *data); |
|---|
| 128 | |
|---|
| 129 | void xc_hash_copy_if(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, xc_if_func_t checker); |
|---|
| 130 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 131 | void xc_zend_constant_ctor(zend_constant *c); |
|---|
| 132 | void xc_zend_constant_dtor(zend_constant *c); |
|---|
| 133 | void xc_copy_internal_zend_constants(HashTable *target, HashTable *source); |
|---|
| 134 | #endif |
|---|
| [662] | 135 | |
|---|
| [671] | 136 | #ifndef ZEND_ENGINE_2_3 |
|---|
| 137 | size_t zend_dirname(char *path, size_t len); |
|---|
| 138 | long zend_atol(const char *str, int len); |
|---|
| 139 | #endif |
|---|
| 140 | |
|---|
| [662] | 141 | typedef struct { |
|---|
| 142 | zend_uint size; |
|---|
| 143 | zend_uint cnt; |
|---|
| 144 | void *data; |
|---|
| 145 | } xc_vector_t; |
|---|
| 146 | |
|---|
| 147 | #define xc_vector_init(type, vector) do { \ |
|---|
| 148 | (vector)->cnt = 0; \ |
|---|
| 149 | (vector)->size = 0; \ |
|---|
| 150 | (vector)->data = NULL; \ |
|---|
| 151 | } while (0) |
|---|
| 152 | |
|---|
| 153 | #define xc_vector_add(type, vector, value) do { \ |
|---|
| 154 | if ((vector)->cnt == (vector)->size) { \ |
|---|
| 155 | if ((vector)->size) { \ |
|---|
| 156 | (vector)->size <<= 1; \ |
|---|
| 157 | (vector)->data = erealloc((vector)->data, sizeof(type) * (vector)->size); \ |
|---|
| 158 | } \ |
|---|
| 159 | else { \ |
|---|
| 160 | (vector)->size = 8; \ |
|---|
| 161 | (vector)->data = emalloc(sizeof(type) * (vector)->size); \ |
|---|
| 162 | } \ |
|---|
| 163 | } \ |
|---|
| 164 | ((type *) (vector)->data)[(vector)->cnt++] = value; \ |
|---|
| 165 | } while (0) |
|---|
| 166 | |
|---|
| 167 | static inline void *xc_vector_detach_impl(xc_vector_t *vector) |
|---|
| 168 | { |
|---|
| 169 | void *data = vector->data; |
|---|
| 170 | vector->data = NULL; |
|---|
| 171 | vector->size = 0; |
|---|
| 172 | vector->cnt = 0; |
|---|
| 173 | return data; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | #define xc_vector_detach(type, vector) ((type *) xc_vector_detach_impl(vector)) |
|---|
| 177 | |
|---|
| 178 | static inline void xc_vector_free_impl(xc_vector_t *vector TSRMLS_DC) |
|---|
| 179 | { |
|---|
| 180 | if (vector->data) { |
|---|
| 181 | efree(vector->data); |
|---|
| 182 | } |
|---|
| 183 | vector->size = 0; |
|---|
| 184 | vector->cnt = 0; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | #define xc_vector_free(type, vector) xc_vector_free_impl(vector TSRMLS_CC) |
|---|
| 188 | |
|---|