source: trunk/processor/head.m4 @ 860

Last change on this file since 860 was 860, checked in by moo, 3 years ago

kill warnings

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