source: trunk/processor/head.m4 @ 917

Last change on this file since 917 was 917, checked in by moo, 22 months ago

kill sign/const warning

  • Property svn:eol-style set to native
File size: 15.0 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"
13#include "align.h"
14#include "const_string.h"
15#include "processor.h"
16#include "stack.h"
17#include "xcache_globals.h"
18
[58]19#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
20extern unsigned int zend_hash_canary;
21#endif
22
[1]23define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
24define(`COUNTOF_zend_uint', `1')
25define(`SIZEOF_int', `sizeof(int)')
26define(`COUNTOF_int', `1')
27define(`SIZEOF_zend_function', `sizeof(zend_function)')
28define(`COUNTOF_zend_function', `1')
29define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
30define(`COUNTOF_zval_ptr', `1')
[825]31define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
32define(`COUNTOF_zval_ptr_nullable', `1')
[836]33define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
34define(`COUNTOF_zend_trait_alias_ptr', `1')
35define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
36define(`COUNTOF_zend_trait_precedence_ptr', `1')
[1]37define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
38define(`COUNTOF_xc_entry_name_t', `1')
[843]39define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
40define(`COUNTOF_xc_ztstring', `1')
[1]41
42ifdef(`XCACHE_ENABLE_TEST', `
43#undef NDEBUG
44#include <assert.h>
[29]45m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
46', `
47m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
[1]48')
[300]49ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
50
[29]51sinclude(builddir`/structinfo.m4')
52
[1]53#ifndef NDEBUG
54#   undef inline
55#define inline
56#endif
57
58typedef zval *zval_ptr;
[825]59typedef zval *zval_ptr_nullable;
[843]60typedef char *xc_ztstring;
[836]61#ifdef ZEND_ENGINE_2_4
62typedef zend_trait_alias *zend_trait_alias_ptr;
63typedef zend_trait_precedence *zend_trait_precedence_ptr;
64#endif
[917]65#ifdef ZEND_ENGINE_2_3
66typedef int last_brk_cont_t;
67#else
68typedef zend_uint last_brk_cont_t;
69#endif
[836]70
[917]71typedef zend_uchar xc_zval_type_t;
[843]72typedef int xc_op_type;
73typedef zend_uchar xc_opcode;
[103]74#ifdef IS_UNICODE
75typedef UChar zstr_uchar;
76#endif
77typedef char  zstr_char;
[1]78
79#define MAX_DUP_STR_LEN 256
80dnl }}}
[178]81/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
82struct _xc_processor_t {
[1]83    char *p;
84    zend_uint size;
85    HashTable strings;
86    HashTable zvalptrs;
87    zend_bool reference; /* enable if to deal with reference */
[233]88    zend_bool have_references;
[851]89    const xc_entry_php_t *entry_php_src;
90    const xc_entry_php_t *entry_php_dst;
[311]91    const xc_entry_data_php_t *php_src;
92    const xc_entry_data_php_t *php_dst;
93    const xc_cache_t          *cache;
[1]94    const zend_class_entry *cache_ce;
[662]95    zend_uint cache_class_index;
[1]96
[779]97    const zend_op_array    *active_op_array_src;
98    zend_op_array          *active_op_array_dst;
[1]99    const zend_class_entry *active_class_entry_src;
100    zend_class_entry       *active_class_entry_dst;
[662]101    zend_uint                 active_class_index;
102    zend_uint                 active_op_array_index;
103    const xc_op_array_info_t *active_op_array_infos_src;
[1]104
105    zend_bool readonly_protection; /* wheather it's present */
[877]106IFAUTOCHECK(xc_stack_t allocsizes;)
[1]107};
108/* }}} */
[836]109/* {{{ memsetptr */
[877]110IFAUTOCHECK(`dnl
[836]111static void *memsetptr(void *mem, void *content, size_t n)
112{
113    void **p = (void **) mem;
[844]114    void **end = (void **) ((char *) mem + n);
[838]115    while (p < end - sizeof(content)) {
[836]116        *p = content;
117        p += sizeof(content);
118    }
[838]119    if (p < end) {
120        memset(p, -1, end - p);
121    }
[836]122    return mem;
123}
124')
125/* }}} */
[230]126#ifdef HAVE_XCACHE_DPRINT
[1]127static void xc_dprint_indent(int indent) /* {{{ */
128{
129    int i;
130    for (i = 0; i < indent; i ++) {
131        fprintf(stderr, "  ");
132    }
133}
[230]134/* }}} */
135static void xc_dprint_str_len(const char *str, int len) /* {{{ */
136{
[295]137    const unsigned char *p = (const unsigned char *) str;
[230]138    int i;
139    for (i = 0; i < len; i ++) {
140        if (p[i] < 32 || p[i] == 127) {
141            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
142        }
143        else {
144            fputc(p[i], stderr);
145        }
146    }
147}
148/* }}} */
[1]149#endif
[103]150/* {{{ xc_zstrlen_char */
[844]151static inline int xc_zstrlen_char(const_zstr s)
[103]152{
153    return strlen(ZSTR_S(s));
154}
155/* }}} */
156#ifdef IS_UNICODE
157/* {{{ xc_zstrlen_uchar */
158static inline int xc_zstrlen_uchar(zstr s)
159{
[512]160    return u_strlen(ZSTR_U(s));
[103]161}
162/* }}} */
163/* {{{ xc_zstrlen */
[844]164static inline int xc_zstrlen(int type, const_zstr s)
[103]165{
166    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
167}
168/* }}} */
169#else
170/* {{{ xc_zstrlen */
171#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
172/* }}} */
173#endif
[1]174/* {{{ xc_calc_string_n */
[878]175REDEF(`PROCESSOR_TYPE', `calc')
[690]176#undef C_RELAYLINE
177#define C_RELAYLINE
[877]178IFAUTOCHECK(`
[690]179#undef C_RELAYLINE
180#define C_RELAYLINE , __LINE__
181')
[877]182static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
[1]183    pushdef(`__LINE__', `relayline')
184    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]185    long dummy = 1;
[1]186
187    if (realsize > MAX_DUP_STR_LEN) {
188        ALLOC(, char, realsize)
189    }
[103]190    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
[1]191        /* new string */
192        ALLOC(, char, realsize)
193    }
[877]194    IFAUTOCHECK(`
[1]195        else {
[103]196            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
[1]197        }
198    ')
199    popdef(`__LINE__')
200}
201/* }}} */
202/* {{{ xc_store_string_n */
[878]203REDEF(`PROCESSOR_TYPE', `store')
[877]204static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
[1]205    pushdef(`__LINE__', `relayline')
206    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]207    zstr ret, *pret;
[1]208
209    if (realsize > MAX_DUP_STR_LEN) {
[103]210        ALLOC(ZSTR_V(ret), char, realsize)
211        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
212        return ret;
[1]213    }
[103]214
215    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
216        return *pret;
[1]217    }
[103]218
219    /* new string */
220    ALLOC(ZSTR_V(ret), char, realsize)
221    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
222    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
223    return ret;
224
[1]225    popdef(`__LINE__')
226}
227/* }}} */
228/* {{{ xc_get_class_num
229 * return class_index + 1
230 */
[178]231static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
[90]232    zend_ulong i;
[311]233    const xc_entry_data_php_t *php = processor->php_src;
[1]234    zend_class_entry *ceptr;
235
236    if (processor->cache_ce == ce) {
[662]237        return processor->cache_class_index + 1;
[1]238    }
[311]239    for (i = 0; i < php->classinfo_cnt; i ++) {
240        ceptr = CestToCePtr(php->classinfos[i].cest);
[1]241        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
242            processor->cache_ce = ceptr;
[667]243            processor->cache_class_index = i;
[1]244            return i + 1;
245        }
246    }
247    assert(0);
[90]248    return (zend_ulong) -1;
[1]249}
[662]250define(`xc_get_class_num', `xc_get_class_numNOTDEFINED')
[1]251/* }}} */
252/* {{{ xc_get_class */
253#ifdef ZEND_ENGINE_2
[178]254static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
[1]255    /* must be parent or currrent class */
[662]256    assert(class_num <= processor->active_class_index + 1);
[311]257    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
[1]258}
259#endif
[662]260define(`xc_get_class', `xc_get_classNOTDEFINED')
[1]261/* }}} */
262#ifdef ZEND_ENGINE_2
263/* fix method on store */
[507]264static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
[1]265{
266    zend_function *zf = (zend_function *) dst;
267    zend_class_entry *ce = processor->active_class_entry_dst;
[507]268    const zend_class_entry *srcce = processor->active_class_entry_src;
[1]269
270    /* Fixing up the default functions for objects here since
271     * we need to compare with the newly allocated functions
272     *
273     * caveat: a sub-class method can have the same name as the
274     * parent~s constructor and create problems.
275     */
276
277    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
278        if (!ce->constructor) {
279            ce->constructor = zf;
280        }
281    }
282    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
283        ce->destructor = zf;
284    }
285    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
286        ce->clone = zf;
287    }
288    else {
[507]289    pushdef(`SET_IF_SAME_NAMEs', `
[1]290        SET_IF_SAME_NAME(__get);
291        SET_IF_SAME_NAME(__set);
[56]292#ifdef ZEND_ENGINE_2_1
[1]293        SET_IF_SAME_NAME(__unset);
294        SET_IF_SAME_NAME(__isset);
[56]295#endif
[1]296        SET_IF_SAME_NAME(__call);
[480]297#ifdef ZEND_CALLSTATIC_FUNC_NAME
298        SET_IF_SAME_NAME(__callstatic);
299#endif
[94]300#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
[1]301        SET_IF_SAME_NAME(__tostring);
302#endif
[507]303    ')
304#ifdef IS_UNICODE
305        if (UG(unicode)) {
306#define SET_IF_SAME_NAME(member) \
307            do { \
[509]308                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
[507]309                    ce->member = zf; \
310                } \
311            } \
312            while(0)
[1]313
[507]314            SET_IF_SAME_NAMEs()
[1]315#undef SET_IF_SAME_NAME
[507]316        }
317        else
318#endif
319        do {
320#define SET_IF_SAME_NAME(member) \
321            do { \
322                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
323                    ce->member = zf; \
324                } \
325            } \
326            while(0)
327
328            SET_IF_SAME_NAMEs()
329#undef SET_IF_SAME_NAME
330        } while (0);
331
332    popdef(`SET_IF_SAME_NAMEs')
333
[1]334    }
335}
336/* }}} */
[199]337#endif
[195]338/* {{{ call op_array ctor handler */
339extern zend_bool xc_have_op_array_ctor;
340static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
341{
342    if (extension->op_array_ctor) {
343        extension->op_array_ctor(op_array);
344    }
345}
346/* }}} */
[719]347/* {{{ field name checker */
[877]348IFAUTOCHECK(`dnl
[836]349static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, int assert_names_count, HashTable *done_names)
[719]350{
351    int errors = 0;
352    if (assert_names_count) {
353        int i;
354        Bucket *b;
355
356        for (i = 0; i < assert_names_count; ++i) {
[860]357            if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], strlen(assert_names[i]) + 1)) {
[719]358                fprintf(stderr
359                    , "missing field at %s `#'%d %s`' : %s\n"
360                    , file, line, functionName
361                    , assert_names[i]
362                    );
363                ++errors;
364            }
365        }
366
367        for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
368            int known = 0;
369            int i;
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(` {
[1]451            int real = processor.p - oldp;
452            int should = processor.size;
453            if (real != processor.size) {
454                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
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.