source: trunk/processor/head.m4 @ 877

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

kill warning

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