1 | dnl {{{ === program start ======================================== |
---|
2 | divert(0) |
---|
3 | #include <string.h> |
---|
4 | #include <stdio.h> |
---|
5 | |
---|
6 | #include "php.h" |
---|
7 | #include "zend_compile.h" |
---|
8 | #include "zend_API.h" |
---|
9 | #include "zend_ini.h" |
---|
10 | |
---|
11 | #include "xcache.h" |
---|
12 | #include "align.h" |
---|
13 | #include "const_string.h" |
---|
14 | #include "processor.h" |
---|
15 | #include "stack.h" |
---|
16 | #include "xcache_globals.h" |
---|
17 | |
---|
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_xc_entry_name_t', `sizeof(xc_entry_name_t)') |
---|
27 | define(`COUNTOF_xc_entry_name_t', `1') |
---|
28 | |
---|
29 | ifdef(`XCACHE_ENABLE_TEST', ` |
---|
30 | #undef NDEBUG |
---|
31 | #include <assert.h> |
---|
32 | m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)') |
---|
33 | ', ` |
---|
34 | m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)') |
---|
35 | ') |
---|
36 | sinclude(builddir`/structinfo.m4') |
---|
37 | |
---|
38 | #ifndef NDEBUG |
---|
39 | # undef inline |
---|
40 | #define inline |
---|
41 | #endif |
---|
42 | |
---|
43 | typedef zval *zval_ptr; |
---|
44 | typedef zend_uchar zval_data_type; |
---|
45 | |
---|
46 | #define MAX_DUP_STR_LEN 256 |
---|
47 | dnl }}} |
---|
48 | /* export: typedef struct _processor_t processor_t; :export {{{ */ |
---|
49 | struct _processor_t { |
---|
50 | char *p; |
---|
51 | zend_uint size; |
---|
52 | HashTable strings; |
---|
53 | HashTable zvalptrs; |
---|
54 | zend_bool reference; /* enable if to deal with reference */ |
---|
55 | const xc_entry_t *xce_src; |
---|
56 | const xc_entry_t *xce_dst; |
---|
57 | const zend_class_entry *cache_ce; |
---|
58 | zend_uint cache_class_num; |
---|
59 | |
---|
60 | const zend_op *active_opcodes_src; |
---|
61 | zend_op *active_opcodes_dst; |
---|
62 | const zend_class_entry *active_class_entry_src; |
---|
63 | zend_class_entry *active_class_entry_dst; |
---|
64 | zend_uint active_class_num; |
---|
65 | |
---|
66 | zend_bool readonly_protection; /* wheather it's present */ |
---|
67 | IFASSERT(xc_stack_t allocsizes;) |
---|
68 | }; |
---|
69 | /* }}} */ |
---|
70 | #ifdef XCACHE_HAVE_DPRINT |
---|
71 | static void xc_dprint_indent(int indent) /* {{{ */ |
---|
72 | { |
---|
73 | int i; |
---|
74 | for (i = 0; i < indent; i ++) { |
---|
75 | fprintf(stderr, " "); |
---|
76 | } |
---|
77 | } |
---|
78 | #endif |
---|
79 | /* }}} */ |
---|
80 | /* {{{ xc_calc_string_n */ |
---|
81 | REDEF(`KIND', `calc') |
---|
82 | static inline void xc_calc_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) { |
---|
83 | pushdef(`__LINE__', `relayline') |
---|
84 | int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size); |
---|
85 | |
---|
86 | if (realsize > MAX_DUP_STR_LEN) { |
---|
87 | ALLOC(, char, realsize) |
---|
88 | } |
---|
89 | else if (zend_u_hash_add(&processor->strings, type, str, size, (void*)&str, sizeof(char*), NULL) == SUCCESS) { |
---|
90 | /* new string */ |
---|
91 | ALLOC(, char, realsize) |
---|
92 | } |
---|
93 | IFASSERT(` |
---|
94 | else { |
---|
95 | dnl fprintf(stderr, "dupstr %s\n", str); |
---|
96 | } |
---|
97 | ') |
---|
98 | popdef(`__LINE__') |
---|
99 | } |
---|
100 | /* }}} */ |
---|
101 | /* {{{ xc_store_string_n */ |
---|
102 | REDEF(`KIND', `store') |
---|
103 | static inline char *xc_store_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) { |
---|
104 | pushdef(`__LINE__', `relayline') |
---|
105 | int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size); |
---|
106 | char *s; |
---|
107 | |
---|
108 | if (realsize > MAX_DUP_STR_LEN) { |
---|
109 | ALLOC(s, char, realsize) |
---|
110 | memcpy(s, str, realsize); |
---|
111 | } |
---|
112 | else if (zend_u_hash_find(&processor->strings, type, str, size, (void*)&s) != SUCCESS) { |
---|
113 | /* new string */ |
---|
114 | ALLOC(s, char, realsize) |
---|
115 | memcpy(s, str, realsize); |
---|
116 | zend_u_hash_add(&processor->strings, type, str, size, (void*)&s, sizeof(char*), NULL); |
---|
117 | } |
---|
118 | else { |
---|
119 | s = *(char**)s; |
---|
120 | } |
---|
121 | return s; |
---|
122 | popdef(`__LINE__') |
---|
123 | } |
---|
124 | /* }}} */ |
---|
125 | /* {{{ xc_get_class_num |
---|
126 | * return class_index + 1 |
---|
127 | */ |
---|
128 | static zend_uint xc_get_class_num(processor_t *processor, zend_class_entry *ce) { |
---|
129 | zend_uint i; |
---|
130 | const xc_entry_t *xce = processor->xce_src; |
---|
131 | zend_class_entry *ceptr; |
---|
132 | |
---|
133 | if (processor->cache_ce == ce) { |
---|
134 | return processor->cache_class_num; |
---|
135 | } |
---|
136 | for (i = 0; i < xce->data.php->classinfo_cnt; i ++) { |
---|
137 | ceptr = CestToCePtr(xce->data.php->classinfos[i].cest); |
---|
138 | if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) { |
---|
139 | processor->cache_ce = ceptr; |
---|
140 | processor->cache_class_num = i + 1; |
---|
141 | return i + 1; |
---|
142 | } |
---|
143 | } |
---|
144 | assert(0); |
---|
145 | return (zend_uint) -1; |
---|
146 | } |
---|
147 | /* }}} */ |
---|
148 | /* {{{ xc_get_class */ |
---|
149 | #ifdef ZEND_ENGINE_2 |
---|
150 | static zend_class_entry *xc_get_class(processor_t *processor, zend_uint class_num) { |
---|
151 | /* must be parent or currrent class */ |
---|
152 | assert(class_num <= processor->active_class_num); |
---|
153 | return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest); |
---|
154 | } |
---|
155 | #endif |
---|
156 | /* }}} */ |
---|
157 | #ifdef ZEND_ENGINE_2 |
---|
158 | /* fix method on store */ |
---|
159 | static void xc_fix_method(processor_t *processor, zend_op_array *dst) /* {{{ */ |
---|
160 | { |
---|
161 | zend_function *zf = (zend_function *) dst; |
---|
162 | zend_class_entry *ce = processor->active_class_entry_dst; |
---|
163 | |
---|
164 | /* Fixing up the default functions for objects here since |
---|
165 | * we need to compare with the newly allocated functions |
---|
166 | * |
---|
167 | * caveat: a sub-class method can have the same name as the |
---|
168 | * parent~s constructor and create problems. |
---|
169 | */ |
---|
170 | |
---|
171 | if (zf->common.fn_flags & ZEND_ACC_CTOR) { |
---|
172 | if (!ce->constructor) { |
---|
173 | ce->constructor = zf; |
---|
174 | } |
---|
175 | } |
---|
176 | else if (zf->common.fn_flags & ZEND_ACC_DTOR) { |
---|
177 | ce->destructor = zf; |
---|
178 | } |
---|
179 | else if (zf->common.fn_flags & ZEND_ACC_CLONE) { |
---|
180 | ce->clone = zf; |
---|
181 | } |
---|
182 | else { |
---|
183 | #define SET_IF_SAME_NAME(member) \ |
---|
184 | do { \ |
---|
185 | if(!strcasecmp(zf->common.function_name, #member)) { \ |
---|
186 | ce->member = zf; \ |
---|
187 | } \ |
---|
188 | } \ |
---|
189 | while(0) |
---|
190 | /* if(ce->member && !strcmp(zf->common.function_name, ce->member->common.function_name)) { \ */ |
---|
191 | |
---|
192 | SET_IF_SAME_NAME(__get); |
---|
193 | SET_IF_SAME_NAME(__set); |
---|
194 | SET_IF_SAME_NAME(__unset); |
---|
195 | SET_IF_SAME_NAME(__isset); |
---|
196 | SET_IF_SAME_NAME(__call); |
---|
197 | #if PHP_MAJOR_VERSION >= 6 |
---|
198 | SET_IF_SAME_NAME(__tostring); |
---|
199 | #endif |
---|
200 | |
---|
201 | #undef SET_IF_SAME_NAME |
---|
202 | } |
---|
203 | } |
---|
204 | /* }}} */ |
---|
205 | #endif |
---|
206 | dnl ================ export API |
---|
207 | /* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */ |
---|
208 | xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) { |
---|
209 | xc_entry_t *dst; |
---|
210 | processor_t processor; |
---|
211 | |
---|
212 | memset(&processor, 0, sizeof(processor)); |
---|
213 | if (src->type == XC_TYPE_VAR) { |
---|
214 | processor.reference = 1; |
---|
215 | } |
---|
216 | |
---|
217 | IFASSERT(`xc_stack_init(&processor.allocsizes);') |
---|
218 | |
---|
219 | /* calc size */ { |
---|
220 | zend_hash_init(&processor.strings, 0, NULL, NULL, 0); |
---|
221 | if (processor.reference) { |
---|
222 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
---|
223 | } |
---|
224 | |
---|
225 | processor.size = 0; |
---|
226 | /* allocate */ |
---|
227 | processor.size = ALIGN(processor.size + sizeof(src[0])); |
---|
228 | |
---|
229 | xc_calc_xc_entry_t(&processor, src TSRMLS_CC); |
---|
230 | if (processor.reference) { |
---|
231 | zend_hash_destroy(&processor.zvalptrs); |
---|
232 | } |
---|
233 | zend_hash_destroy(&processor.strings); |
---|
234 | } |
---|
235 | src->size = processor.size; |
---|
236 | |
---|
237 | IFASSERT(`xc_stack_reverse(&processor.allocsizes);') |
---|
238 | /* store {{{ */ |
---|
239 | { |
---|
240 | IFASSERT(`char *oldp;') |
---|
241 | zend_hash_init(&processor.strings, 0, NULL, NULL, 0); |
---|
242 | if (processor.reference) { |
---|
243 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
---|
244 | } |
---|
245 | |
---|
246 | /* mem :) */ |
---|
247 | processor.p = (char *)xc_mem_malloc(src->cache->mem, processor.size); |
---|
248 | if (processor.p == NULL) { |
---|
249 | dst = NULL; |
---|
250 | goto err_alloc; |
---|
251 | return NULL; |
---|
252 | } |
---|
253 | IFASSERT(`oldp = processor.p;') |
---|
254 | assert(processor.p == (char *) ALIGN(processor.p)); |
---|
255 | |
---|
256 | /* allocate */ |
---|
257 | dst = (xc_entry_t *) processor.p; |
---|
258 | processor.p = (char *) ALIGN(processor.p + sizeof(dst[0])); |
---|
259 | |
---|
260 | xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC); |
---|
261 | IFASSERT(` { |
---|
262 | int real = processor.p - oldp; |
---|
263 | int should = processor.size; |
---|
264 | if (real != processor.size) { |
---|
265 | fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should); |
---|
266 | abort(); |
---|
267 | } |
---|
268 | }') |
---|
269 | err_alloc: |
---|
270 | if (processor.reference) { |
---|
271 | zend_hash_destroy(&processor.zvalptrs); |
---|
272 | } |
---|
273 | zend_hash_destroy(&processor.strings); |
---|
274 | } |
---|
275 | /* }}} */ |
---|
276 | |
---|
277 | IFASSERT(`xc_stack_destroy(&processor.allocsizes);') |
---|
278 | |
---|
279 | return dst; |
---|
280 | } |
---|
281 | /* }}} */ |
---|
282 | /* export: xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC); :export {{{ */ |
---|
283 | xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) { |
---|
284 | processor_t processor; |
---|
285 | |
---|
286 | memset(&processor, 0, sizeof(processor)); |
---|
287 | processor.readonly_protection = readonly_protection; |
---|
288 | |
---|
289 | xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC); |
---|
290 | return dst; |
---|
291 | } |
---|
292 | /* }}} */ |
---|
293 | /* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */ |
---|
294 | zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) { |
---|
295 | processor_t processor; |
---|
296 | |
---|
297 | memset(&processor, 0, sizeof(processor)); |
---|
298 | processor.reference = 1; |
---|
299 | |
---|
300 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
---|
301 | dnl fprintf(stderr, "mark[%p] = %p\n", src, dst); |
---|
302 | zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL); |
---|
303 | xc_restore_zval(&processor, dst, src TSRMLS_CC); |
---|
304 | zend_hash_destroy(&processor.zvalptrs); |
---|
305 | |
---|
306 | return dst; |
---|
307 | } |
---|
308 | /* }}} */ |
---|
309 | /* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */ |
---|
310 | #ifdef XCACHE_HAVE_DPRINT |
---|
311 | void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) { |
---|
312 | IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");') |
---|
313 | xc_dprint_xc_entry_t(src, indent TSRMLS_CC); |
---|
314 | } |
---|
315 | #endif |
---|
316 | /* }}} */ |
---|