source: trunk/processor/head.m4 @ 1009

Last change on this file since 1009 was 1009, checked in by moo, 21 months ago

kill signed warning

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