source: trunk/processor/head.m4 @ 1009

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

kill signed warning

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