source: trunk/processor/head.m4 @ 836

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

fill uninitialized with line number for debugging purpose

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