source: trunk/processor/head.m4 @ 719

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

runtime check for field list assertion to tell which is mismatch

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