source: trunk/processor/head.m4 @ 930

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

fix disassembler for 2.4 literal

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