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