source: trunk/processor/head.m4 @ 1018

Last change on this file since 1018 was 1018, checked in by moo, 2 years ago

updated deps header

  • Property svn:eol-style set to native
File size: 15.2 KB
Line 
1dnl {{{ === program start ========================================
2divert(0)
3#include <string.h>
4#include <stdio.h>
5
6#include "php.h"
7#include "zend_extensions.h"
8#include "zend_compile.h"
9#include "zend_API.h"
10#include "zend_ini.h"
11
12/* export: #include "xcache.h" :export */
13#include "xcache.h"
14#include "util/xc_align.h"
15#include "xcache/xc_const_string.h"
16#include "xc_processor.h"
17#include "xcache_globals.h"
18
19#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
20extern unsigned int zend_hash_canary;
21#endif
22
23define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
24define(`COUNTOF_zend_uint', `1')
25define(`SIZEOF_int', `sizeof(int)')
26define(`COUNTOF_int', `1')
27define(`SIZEOF_zend_function', `sizeof(zend_function)')
28define(`COUNTOF_zend_function', `1')
29define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
30define(`COUNTOF_zval_ptr', `1')
31define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
32define(`COUNTOF_zval_ptr_nullable', `1')
33define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
34define(`COUNTOF_zend_trait_alias_ptr', `1')
35define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
36define(`COUNTOF_zend_trait_precedence_ptr', `1')
37define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
38define(`COUNTOF_xc_entry_name_t', `1')
39define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
40define(`COUNTOF_xc_ztstring', `1')
41
42ifdef(`XCACHE_ENABLE_TEST', `
43#undef NDEBUG
44#include <assert.h>
45m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
46', `
47m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
48')
49ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
50
51sinclude(builddir`/structinfo.m4')
52
53#ifndef NDEBUG
54#   undef inline
55#define inline
56#endif
57
58typedef zval *zval_ptr;
59typedef zval *zval_ptr_nullable;
60typedef char *xc_ztstring;
61#ifdef ZEND_ENGINE_2_4
62typedef zend_trait_alias *zend_trait_alias_ptr;
63typedef zend_trait_precedence *zend_trait_precedence_ptr;
64#endif
65#ifdef ZEND_ENGINE_2_3
66typedef int last_brk_cont_t;
67#else
68typedef zend_uint last_brk_cont_t;
69#endif
70
71typedef zend_uchar xc_zval_type_t;
72typedef int xc_op_type;
73typedef zend_uchar xc_opcode;
74#ifdef IS_UNICODE
75typedef UChar zstr_uchar;
76#endif
77typedef char  zstr_char;
78
79#define MAX_DUP_STR_LEN 256
80dnl }}}
81/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
82struct _xc_processor_t {
83    char *p;
84    zend_uint size;
85    HashTable strings;
86    HashTable zvalptrs;
87    zend_bool reference; /* enable if to deal with reference */
88    zend_bool have_references;
89    const xc_entry_php_t *entry_php_src;
90    const xc_entry_php_t *entry_php_dst;
91    const xc_entry_data_php_t *php_src;
92    const xc_entry_data_php_t *php_dst;
93    const xc_cache_t          *cache;
94    const zend_class_entry *cache_ce;
95    zend_uint cache_class_index;
96
97    const zend_op_array    *active_op_array_src;
98    zend_op_array          *active_op_array_dst;
99    const zend_class_entry *active_class_entry_src;
100    zend_class_entry       *active_class_entry_dst;
101    zend_uint                 active_class_index;
102    zend_uint                 active_op_array_index;
103    const xc_op_array_info_t *active_op_array_infos_src;
104
105    zend_bool readonly_protection; /* wheather it's present */
106IFAUTOCHECK(xc_stack_t allocsizes;)
107};
108/* }}} */
109/* export: typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t; :export {{{ */
110/* }}} */
111/* {{{ memsetptr */
112IFAUTOCHECK(`dnl
113static void *memsetptr(void *mem, void *content, size_t n)
114{
115    void **p = (void **) mem;
116    void **end = (void **) ((char *) mem + n);
117    while (p < end - sizeof(content)) {
118        *p = content;
119        p += sizeof(content);
120    }
121    if (p < end) {
122        memset(p, -1, end - p);
123    }
124    return mem;
125}
126')
127/* }}} */
128#ifdef HAVE_XCACHE_DPRINT
129static void xc_dprint_indent(int indent) /* {{{ */
130{
131    int i;
132    for (i = 0; i < indent; i ++) {
133        fprintf(stderr, "  ");
134    }
135}
136/* }}} */
137static void xc_dprint_str_len(const char *str, int len) /* {{{ */
138{
139    const unsigned char *p = (const unsigned char *) str;
140    int i;
141    for (i = 0; i < len; i ++) {
142        if (p[i] < 32 || p[i] == 127) {
143            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
144        }
145        else {
146            fputc(p[i], stderr);
147        }
148    }
149}
150/* }}} */
151#endif
152/* {{{ xc_zstrlen_char */
153static inline int xc_zstrlen_char(const_zstr s)
154{
155    return strlen(ZSTR_S(s));
156}
157/* }}} */
158#ifdef IS_UNICODE
159/* {{{ xc_zstrlen_uchar */
160static inline int xc_zstrlen_uchar(zstr s)
161{
162    return u_strlen(ZSTR_U(s));
163}
164/* }}} */
165/* {{{ xc_zstrlen */
166static inline int xc_zstrlen(int type, const_zstr s)
167{
168    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
169}
170/* }}} */
171#else
172/* {{{ xc_zstrlen */
173#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
174/* }}} */
175#endif
176/* {{{ xc_calc_string_n */
177REDEF(`PROCESSOR_TYPE', `calc')
178#undef C_RELAYLINE
179#define C_RELAYLINE
180IFAUTOCHECK(`
181#undef C_RELAYLINE
182#define C_RELAYLINE , __LINE__
183')
184static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
185    pushdef(`__LINE__', `relayline')
186    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
187    long dummy = 1;
188
189    if (realsize > MAX_DUP_STR_LEN) {
190        ALLOC(, char, realsize)
191    }
192    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
193        /* new string */
194        ALLOC(, char, realsize)
195    }
196    IFAUTOCHECK(`
197        else {
198            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
199        }
200    ')
201    popdef(`__LINE__')
202}
203/* }}} */
204/* {{{ xc_store_string_n */
205REDEF(`PROCESSOR_TYPE', `store')
206static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
207    pushdef(`__LINE__', `relayline')
208    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
209    zstr ret, *pret;
210
211    if (realsize > MAX_DUP_STR_LEN) {
212        ALLOC(ZSTR_V(ret), char, realsize)
213        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
214        return ret;
215    }
216
217    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
218        return *pret;
219    }
220
221    /* new string */
222    ALLOC(ZSTR_V(ret), char, realsize)
223    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
224    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
225    return ret;
226
227    popdef(`__LINE__')
228}
229/* }}} */
230/* {{{ xc_get_class_num
231 * return class_index + 1
232 */
233static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
234    zend_ulong i;
235    const xc_entry_data_php_t *php = processor->php_src;
236    zend_class_entry *ceptr;
237
238    if (processor->cache_ce == ce) {
239        return processor->cache_class_index + 1;
240    }
241    for (i = 0; i < php->classinfo_cnt; i ++) {
242        ceptr = CestToCePtr(php->classinfos[i].cest);
243        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
244            processor->cache_ce = ceptr;
245            processor->cache_class_index = i;
246            return i + 1;
247        }
248    }
249    assert(0);
250    return (zend_ulong) -1;
251}
252define(`xc_get_class_num', `xc_get_class_numNOTDEFINED')
253/* }}} */
254/* {{{ xc_get_class */
255#ifdef ZEND_ENGINE_2
256static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
257    /* must be parent or currrent class */
258    assert(class_num <= processor->active_class_index + 1);
259    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
260}
261#endif
262define(`xc_get_class', `xc_get_classNOTDEFINED')
263/* }}} */
264#ifdef ZEND_ENGINE_2
265/* fix method on store */
266static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
267{
268    zend_function *zf = (zend_function *) dst;
269    zend_class_entry *ce = processor->active_class_entry_dst;
270    const zend_class_entry *srcce = processor->active_class_entry_src;
271
272    /* Fixing up the default functions for objects here since
273     * we need to compare with the newly allocated functions
274     *
275     * caveat: a sub-class method can have the same name as the
276     * parent~s constructor and create problems.
277     */
278
279    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
280        if (!ce->constructor) {
281            ce->constructor = zf;
282        }
283    }
284    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
285        ce->destructor = zf;
286    }
287    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
288        ce->clone = zf;
289    }
290    else {
291    pushdef(`SET_IF_SAME_NAMEs', `
292        SET_IF_SAME_NAME(__get);
293        SET_IF_SAME_NAME(__set);
294#ifdef ZEND_ENGINE_2_1
295        SET_IF_SAME_NAME(__unset);
296        SET_IF_SAME_NAME(__isset);
297#endif
298        SET_IF_SAME_NAME(__call);
299#ifdef ZEND_CALLSTATIC_FUNC_NAME
300        SET_IF_SAME_NAME(__callstatic);
301#endif
302#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
303        SET_IF_SAME_NAME(__tostring);
304#endif
305    ')
306#ifdef IS_UNICODE
307        if (UG(unicode)) {
308#define SET_IF_SAME_NAME(member) \
309            do { \
310                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
311                    ce->member = zf; \
312                } \
313            } \
314            while(0)
315
316            SET_IF_SAME_NAMEs()
317#undef SET_IF_SAME_NAME
318        }
319        else
320#endif
321        do {
322#define SET_IF_SAME_NAME(member) \
323            do { \
324                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
325                    ce->member = zf; \
326                } \
327            } \
328            while(0)
329
330            SET_IF_SAME_NAMEs()
331#undef SET_IF_SAME_NAME
332        } while (0);
333
334    popdef(`SET_IF_SAME_NAMEs')
335
336    }
337}
338/* }}} */
339#endif
340/* {{{ call op_array ctor handler */
341extern zend_bool xc_have_op_array_ctor;
342static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
343{
344    if (extension->op_array_ctor) {
345        extension->op_array_ctor(op_array);
346    }
347}
348/* }}} */
349/* {{{ field name checker */
350IFAUTOCHECK(`dnl
351static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, int assert_names_count, HashTable *done_names)
352{
353    int errors = 0;
354    if (assert_names_count) {
355        int i;
356        Bucket *b;
357
358        for (i = 0; i < assert_names_count; ++i) {
359            if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], strlen(assert_names[i]) + 1)) {
360                fprintf(stderr
361                    , "missing field at %s `#'%d %s`' : %s\n"
362                    , file, line, functionName
363                    , assert_names[i]
364                    );
365                ++errors;
366            }
367        }
368
369        for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
370            int known = 0;
371            for (i = 0; i < assert_names_count; ++i) {
372                if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
373                    known = 1;
374                    break;
375                }
376            }
377            if (!known) {
378                fprintf(stderr
379                    , "unknown field at %s `#'%d %s`' : %s\n"
380                    , file, line, functionName
381                    , BUCKET_KEY_S(b)
382                    );
383                ++errors;
384            }
385        }
386    }
387    return errors;
388}
389')
390/* }}} */
391dnl ================ export API
392define(`DEFINE_STORE_API', `
393/* export: $1 *xc_processor_store_$1(xc_cache_t *cache, $1 *src TSRMLS_DC); :export {{{ */
394$1 *xc_processor_store_$1(xc_cache_t *cache, $1 *src TSRMLS_DC) {
395    $1 *dst;
396    xc_processor_t processor;
397
398    memset(&processor, 0, sizeof(processor));
399    processor.reference = 1;
400    processor.cache = cache;
401
402    IFAUTOCHECK(`xc_stack_init(&processor.allocsizes);')
403
404    /* calc size */ {
405        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
406        if (processor.reference) {
407            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
408        }
409
410        processor.size = 0;
411        /* allocate */
412        processor.size = ALIGN(processor.size + sizeof(src[0]));
413
414        xc_calc_$1(&processor, src TSRMLS_CC);
415        if (processor.reference) {
416            zend_hash_destroy(&processor.zvalptrs);
417        }
418        zend_hash_destroy(&processor.strings);
419    }
420    src->ifelse(
421        `$1', `xc_entry_data_php_t', `',
422        `', `', entry.)size = processor.size;
423    ifelse(
424        `$1', `xc_entry_var_t', `src->have_references = processor.have_references;',
425        `$1', `xc_entry_data_php_t', `src->have_references = processor.have_references;'
426    )
427
428    IFAUTOCHECK(`xc_stack_reverse(&processor.allocsizes);')
429    /* store {{{ */
430    {
431        IFAUTOCHECK(`char *oldp;')
432        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
433        if (processor.reference) {
434            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
435        }
436
437        /* mem :) */
438        processor.p = (char *) processor.cache->mem->handlers->malloc(processor.cache->mem, processor.size);
439        if (processor.p == NULL) {
440            dst = NULL;
441            goto err_alloc;
442        }
443        IFAUTOCHECK(`oldp = processor.p;')
444        assert(processor.p == (char *) ALIGN(processor.p));
445
446        /* allocate */
447        dst = ($1 *) processor.p;
448        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
449
450        xc_store_$1(&processor, dst, src TSRMLS_CC);
451        IFAUTOCHECK(` {
452            size_t unexpected = processor.p - oldp;
453            size_t expected = processor.size;
454            if (unexpected != processor.size) {
455                fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
456                abort();
457            }
458        }')
459err_alloc:
460        if (processor.reference) {
461            zend_hash_destroy(&processor.zvalptrs);
462        }
463        zend_hash_destroy(&processor.strings);
464    }
465    /* }}} */
466
467    IFAUTOCHECK(`xc_stack_destroy(&processor.allocsizes);')
468
469    return dst;
470}
471/* }}} */
472')
473DEFINE_STORE_API(`xc_entry_var_t')
474DEFINE_STORE_API(`xc_entry_php_t')
475DEFINE_STORE_API(`xc_entry_data_php_t')
476/* export: xc_entry_php_t *xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC); :export {{{ */
477xc_entry_php_t *xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC) {
478    xc_processor_t processor;
479
480    memset(&processor, 0, sizeof(processor));
481    xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
482
483    return dst;
484}
485/* }}} */
486/* export: xc_entry_data_php_t *xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC); :export {{{ */
487xc_entry_data_php_t *xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC) {
488    xc_processor_t processor;
489
490    memset(&processor, 0, sizeof(processor));
491    processor.readonly_protection = readonly_protection;
492    /* this function is used for php data only */
493    if (src->have_references) {
494        processor.reference = 1;
495    }
496    processor.entry_php_src = entry_php;
497
498    if (processor.reference) {
499        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
500    }
501    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
502    if (processor.reference) {
503        zend_hash_destroy(&processor.zvalptrs);
504    }
505    return dst;
506}
507/* }}} */
508/* export: xc_entry_var_t *xc_processor_restore_xc_entry_var_t(xc_entry_var_t *dst, const xc_entry_var_t *src TSRMLS_DC); :export {{{ */
509xc_entry_var_t *xc_processor_restore_xc_entry_var_t(xc_entry_var_t *dst, const xc_entry_var_t *src TSRMLS_DC) {
510    xc_processor_t processor;
511
512    memset(&processor, 0, sizeof(processor));
513    xc_restore_xc_entry_var_t(&processor, dst, src TSRMLS_CC);
514
515    return dst;
516}
517/* }}} */
518/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC); :export {{{ */
519zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC) {
520    xc_processor_t processor;
521
522    memset(&processor, 0, sizeof(processor));
523    processor.reference = have_references;
524
525    if (processor.reference) {
526        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
527        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
528        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
529    }
530    xc_restore_zval(&processor, dst, src TSRMLS_CC);
531    if (processor.reference) {
532        zend_hash_destroy(&processor.zvalptrs);
533    }
534
535    return dst;
536}
537/* }}} */
538/* export: void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC); :export {{{ */
539#ifdef HAVE_XCACHE_DPRINT
540void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC) {
541    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
542    xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
543}
544#endif
545/* }}} */
Note: See TracBrowser for help on using the repository browser.