source: trunk/processor/head.m4 @ 1051

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

refactor: move more cache stuffs to mod_cacher

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