source: trunk/processor/head.m4 @ 1132

Last change on this file since 1132 was 1132, checked in by moo, 21 months ago

cacher: hide more detail

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