source: trunk/processor/head.m4 @ 1440

Last change on this file since 1440 was 1440, checked in by moo, 12 months ago

adds ptrmove

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