source: trunk/processor/head.m4 @ 1452

Last change on this file since 1452 was 1452, checked in by moo, 14 months ago

improve readability using ptradd

  • Property svn:eol-style set to native
File size: 10.5 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
[1443]12EXPORT(`#include "xcache.h"')
13EXPORT(`#include "mod_cacher/xc_cache.h"')
14EXPORT(`#include "xcache/xc_shm.h"')
15EXPORT(`#include "xcache/xc_allocator.h"')
[1444]16#include "xc_processor.h"
[994]17#include "xcache/xc_const_string.h"
[1051]18#include "xcache/xc_utils.h"
[1135]19#include "util/xc_align.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
[1452]83
84#define ptradd(type, ptr, ptrdiff) ((type) (((char *) (ptr)) + (ptrdiff)))
85#define ptrsub(ptr1, ptr2) (((char *) (ptr1)) - ((char *) (ptr2)))
[1]86dnl }}}
[1443]87dnl {{{ _xc_processor_t
[1444]88typedef struct _xc_processor_t {
[1]89    char *p;
[1201]90    size_t size;
[1]91    HashTable strings;
92    HashTable zvalptrs;
93    zend_bool reference; /* enable if to deal with reference */
[233]94    zend_bool have_references;
[851]95    const xc_entry_php_t *entry_php_src;
96    const xc_entry_php_t *entry_php_dst;
[311]97    const xc_entry_data_php_t *php_src;
98    const xc_entry_data_php_t *php_dst;
[1132]99    xc_shm_t                  *shm;
[1135]100    xc_allocator_t            *allocator;
[1]101    const zend_class_entry *cache_ce;
[1211]102    zend_ulong cache_class_index;
[1]103
[779]104    const zend_op_array    *active_op_array_src;
105    zend_op_array          *active_op_array_dst;
[1]106    const zend_class_entry *active_class_entry_src;
107    zend_class_entry       *active_class_entry_dst;
[662]108    zend_uint                 active_class_index;
109    zend_uint                 active_op_array_index;
110    const xc_op_array_info_t *active_op_array_infos_src;
[1]111
112    zend_bool readonly_protection; /* wheather it's present */
[877]113IFAUTOCHECK(xc_stack_t allocsizes;)
[1444]114} xc_processor_t;
[1443]115dnl }}}
116EXPORT(`typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t;')
[836]117/* {{{ memsetptr */
[877]118IFAUTOCHECK(`dnl
[836]119static void *memsetptr(void *mem, void *content, size_t n)
120{
121    void **p = (void **) mem;
[844]122    void **end = (void **) ((char *) mem + n);
[838]123    while (p < end - sizeof(content)) {
[836]124        *p = content;
125        p += sizeof(content);
126    }
[838]127    if (p < end) {
128        memset(p, -1, end - p);
129    }
[836]130    return mem;
131}
132')
133/* }}} */
[230]134#ifdef HAVE_XCACHE_DPRINT
[1]135static void xc_dprint_indent(int indent) /* {{{ */
136{
137    int i;
138    for (i = 0; i < indent; i ++) {
139        fprintf(stderr, "  ");
140    }
141}
[230]142/* }}} */
143static void xc_dprint_str_len(const char *str, int len) /* {{{ */
144{
[295]145    const unsigned char *p = (const unsigned char *) str;
[230]146    int i;
147    for (i = 0; i < len; i ++) {
148        if (p[i] < 32 || p[i] == 127) {
149            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
150        }
151        else {
152            fputc(p[i], stderr);
153        }
154    }
155}
156/* }}} */
[1]157#endif
[1439]158static inline size_t xc_zstrlen_char(const_zstr s) /* {{{ */
[103]159{
160    return strlen(ZSTR_S(s));
161}
162/* }}} */
163#ifdef IS_UNICODE
[1439]164static inline size_t xc_zstrlen_uchar(zstr s) /* {{{ */
[103]165{
[512]166    return u_strlen(ZSTR_U(s));
[103]167}
168/* }}} */
[1439]169static inline size_t 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
[690]179#undef C_RELAYLINE
180#define C_RELAYLINE
[877]181IFAUTOCHECK(`
[690]182#undef C_RELAYLINE
183#define C_RELAYLINE , __LINE__
184')
[1439]185static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
186    pushdef(`PROCESSOR_TYPE', `calc')
[1]187    pushdef(`__LINE__', `relayline')
[1201]188    size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]189    long dummy = 1;
[1]190
191    if (realsize > MAX_DUP_STR_LEN) {
192        ALLOC(, char, realsize)
193    }
[1201]194    else if (zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
[1]195        /* new string */
196        ALLOC(, char, realsize)
197    }
[877]198    IFAUTOCHECK(`
[1]199        else {
[103]200            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
[1]201        }
202    ')
203    popdef(`__LINE__')
[1439]204    popdef(`PROCESSOR_TYPE')
[1]205}
206/* }}} */
[1439]207static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
208    pushdef(`PROCESSOR_TYPE', `store')
[1]209    pushdef(`__LINE__', `relayline')
[1201]210    size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]211    zstr ret, *pret;
[1]212
213    if (realsize > MAX_DUP_STR_LEN) {
[103]214        ALLOC(ZSTR_V(ret), char, realsize)
215        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
216        return ret;
[1]217    }
[103]218
[1201]219    if (zend_u_hash_find(&processor->strings, type, str, (uint) size, (void **) &pret) == SUCCESS) {
[103]220        return *pret;
[1]221    }
[103]222
223    /* new string */
224    ALLOC(ZSTR_V(ret), char, realsize)
225    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
[1201]226    zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &ret, sizeof(zstr), NULL);
[103]227    return ret;
228
[1]229    popdef(`__LINE__')
[1439]230    popdef(`PROCESSOR_TYPE')
[1]231}
232/* }}} */
233/* {{{ xc_get_class_num
234 * return class_index + 1
235 */
[1211]236static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
[1201]237    zend_uint 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 ++) {
[1321]245        ceptr = CestToCePtr(php->classinfos[i].cest);
246        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
[1]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}
[1439]255define(`xc_get_class_num', `IFSTORE(``xc_get_class_num'($@)',``xc_get_class_num' can be use in store only')')
[1]256/* }}} */
[1321]257#ifdef ZEND_ENGINE_2
[1439]258static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) { /* {{{ */
[1]259    /* must be parent or currrent class */
[662]260    assert(class_num <= processor->active_class_index + 1);
[1321]261    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
[1]262}
[1439]263define(`xc_get_class', `IFRESTORE(``xc_get_class'($@)',``xc_get_class' can be use in restore only')')
[1]264/* }}} */
[1439]265#endif
[1321]266#ifdef ZEND_ENGINE_2
[1]267/* fix method on store */
[507]268static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
[1]269{
270    zend_function *zf = (zend_function *) dst;
271    zend_class_entry *ce = processor->active_class_entry_dst;
[507]272    const zend_class_entry *srcce = processor->active_class_entry_src;
[1]273
274    /* Fixing up the default functions for objects here since
275     * we need to compare with the newly allocated functions
276     *
277     * caveat: a sub-class method can have the same name as the
278     * parent~s constructor and create problems.
279     */
280
281    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
282        if (!ce->constructor) {
283            ce->constructor = zf;
284        }
285    }
286    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
287        ce->destructor = zf;
288    }
289    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
290        ce->clone = zf;
291    }
292    else {
[507]293    pushdef(`SET_IF_SAME_NAMEs', `
[1]294        SET_IF_SAME_NAME(__get);
295        SET_IF_SAME_NAME(__set);
[56]296#ifdef ZEND_ENGINE_2_1
[1]297        SET_IF_SAME_NAME(__unset);
298        SET_IF_SAME_NAME(__isset);
[56]299#endif
[1]300        SET_IF_SAME_NAME(__call);
[480]301#ifdef ZEND_CALLSTATIC_FUNC_NAME
302        SET_IF_SAME_NAME(__callstatic);
303#endif
[94]304#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
[1]305        SET_IF_SAME_NAME(__tostring);
306#endif
[507]307    ')
308#ifdef IS_UNICODE
309        if (UG(unicode)) {
310#define SET_IF_SAME_NAME(member) \
311            do { \
[509]312                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
[507]313                    ce->member = zf; \
314                } \
315            } \
316            while(0)
[1]317
[507]318            SET_IF_SAME_NAMEs()
[1]319#undef SET_IF_SAME_NAME
[507]320        }
321        else
322#endif
323        do {
324#define SET_IF_SAME_NAME(member) \
325            do { \
326                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
327                    ce->member = zf; \
328                } \
329            } \
330            while(0)
331
332            SET_IF_SAME_NAMEs()
333#undef SET_IF_SAME_NAME
334        } while (0);
335
336    popdef(`SET_IF_SAME_NAMEs')
337
[1]338    }
339}
340/* }}} */
[1321]341#endif
[195]342/* {{{ call op_array ctor handler */
343extern zend_bool xc_have_op_array_ctor;
344static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
345{
346    if (extension->op_array_ctor) {
347        extension->op_array_ctor(op_array);
348    }
349}
350/* }}} */
[719]351/* {{{ field name checker */
[877]352IFAUTOCHECK(`dnl
[1201]353static 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]354{
355    int errors = 0;
356    if (assert_names_count) {
[1201]357        size_t i;
[719]358        Bucket *b;
359
360        for (i = 0; i < assert_names_count; ++i) {
[1201]361            if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], (uint) strlen(assert_names[i]) + 1)) {
[719]362                fprintf(stderr
[1051]363                    , "Error: missing field at %s `#'%d %s`' : %s\n"
[719]364                    , file, line, functionName
365                    , assert_names[i]
366                    );
367                ++errors;
368            }
369        }
370
371        for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
372            int known = 0;
373            for (i = 0; i < assert_names_count; ++i) {
374                if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
375                    known = 1;
376                    break;
377                }
378            }
379            if (!known) {
380                fprintf(stderr
[1051]381                    , "Error: unknown field at %s `#'%d %s`' : %s\n"
[719]382                    , file, line, functionName
383                    , BUCKET_KEY_S(b)
384                    );
385                ++errors;
386            }
387        }
388    }
389    return errors;
390}
391')
392/* }}} */
Note: See TracBrowser for help on using the repository browser.