source: trunk/processor/head.m4 @ 1458

Last change on this file since 1458 was 1458, checked in by moo, 13 months ago

relocate: assert that srcptr use on non-nullable only

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