source: trunk/processor/head.m4 @ 917

Last change on this file since 917 was 917, checked in by moo, 22 months ago

kill sign/const warning

  • Property svn:eol-style set to native
File size: 15.0 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#ifdef ZEND_ENGINE_2_3
66typedef int last_brk_cont_t;
67#else
68typedef zend_uint last_brk_cont_t;
69#endif
70
71typedef zend_uchar xc_zval_type_t;
72typedef int xc_op_type;
73typedef zend_uchar xc_opcode;
74#ifdef IS_UNICODE
75typedef UChar zstr_uchar;
76#endif
77typedef char  zstr_char;
78
79#define MAX_DUP_STR_LEN 256
80dnl }}}
81/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
82struct _xc_processor_t {
83    char *p;
84    zend_uint size;
85    HashTable strings;
86    HashTable zvalptrs;
87    zend_bool reference; /* enable if to deal with reference */
88    zend_bool have_references;
89    const xc_entry_php_t *entry_php_src;
90    const xc_entry_php_t *entry_php_dst;
91    const xc_entry_data_php_t *php_src;
92    const xc_entry_data_php_t *php_dst;
93    const xc_cache_t          *cache;
94    const zend_class_entry *cache_ce;
95    zend_uint cache_class_index;
96
97    const zend_op_array    *active_op_array_src;
98    zend_op_array          *active_op_array_dst;
99    const zend_class_entry *active_class_entry_src;
100    zend_class_entry       *active_class_entry_dst;
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;
104
105    zend_bool readonly_protection; /* wheather it's present */
106IFAUTOCHECK(xc_stack_t allocsizes;)
107};
108/* }}} */
109/* {{{ memsetptr */
110IFAUTOCHECK(`dnl
111static void *memsetptr(void *mem, void *content, size_t n)
112{
113    void **p = (void **) mem;
114    void **end = (void **) ((char *) mem + n);
115    while (p < end - sizeof(content)) {
116        *p = content;
117        p += sizeof(content);
118    }
119    if (p < end) {
120        memset(p, -1, end - p);
121    }
122    return mem;
123}
124')
125/* }}} */
126#ifdef HAVE_XCACHE_DPRINT
127static void xc_dprint_indent(int indent) /* {{{ */
128{
129    int i;
130    for (i = 0; i < indent; i ++) {
131        fprintf(stderr, "  ");
132    }
133}
134/* }}} */
135static void xc_dprint_str_len(const char *str, int len) /* {{{ */
136{
137    const unsigned char *p = (const unsigned char *) str;
138    int i;
139    for (i = 0; i < len; i ++) {
140        if (p[i] < 32 || p[i] == 127) {
141            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
142        }
143        else {
144            fputc(p[i], stderr);
145        }
146    }
147}
148/* }}} */
149#endif
150/* {{{ xc_zstrlen_char */
151static inline int xc_zstrlen_char(const_zstr s)
152{
153    return strlen(ZSTR_S(s));
154}
155/* }}} */
156#ifdef IS_UNICODE
157/* {{{ xc_zstrlen_uchar */
158static inline int xc_zstrlen_uchar(zstr s)
159{
160    return u_strlen(ZSTR_U(s));
161}
162/* }}} */
163/* {{{ xc_zstrlen */
164static inline int xc_zstrlen(int type, const_zstr s)
165{
166    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
167}
168/* }}} */
169#else
170/* {{{ xc_zstrlen */
171#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
172/* }}} */
173#endif
174/* {{{ xc_calc_string_n */
175REDEF(`PROCESSOR_TYPE', `calc')
176#undef C_RELAYLINE
177#define C_RELAYLINE
178IFAUTOCHECK(`
179#undef C_RELAYLINE
180#define C_RELAYLINE , __LINE__
181')
182static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
183    pushdef(`__LINE__', `relayline')
184    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
185    long dummy = 1;
186
187    if (realsize > MAX_DUP_STR_LEN) {
188        ALLOC(, char, realsize)
189    }
190    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
191        /* new string */
192        ALLOC(, char, realsize)
193    }
194    IFAUTOCHECK(`
195        else {
196            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
197        }
198    ')
199    popdef(`__LINE__')
200}
201/* }}} */
202/* {{{ xc_store_string_n */
203REDEF(`PROCESSOR_TYPE', `store')
204static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) {
205    pushdef(`__LINE__', `relayline')
206    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
207    zstr ret, *pret;
208
209    if (realsize > MAX_DUP_STR_LEN) {
210        ALLOC(ZSTR_V(ret), char, realsize)
211        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
212        return ret;
213    }
214
215    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
216        return *pret;
217    }
218
219    /* new string */
220    ALLOC(ZSTR_V(ret), char, realsize)
221    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
222    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
223    return ret;
224
225    popdef(`__LINE__')
226}
227/* }}} */
228/* {{{ xc_get_class_num
229 * return class_index + 1
230 */
231static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
232    zend_ulong i;
233    const xc_entry_data_php_t *php = processor->php_src;
234    zend_class_entry *ceptr;
235
236    if (processor->cache_ce == ce) {
237        return processor->cache_class_index + 1;
238    }
239    for (i = 0; i < php->classinfo_cnt; i ++) {
240        ceptr = CestToCePtr(php->classinfos[i].cest);
241        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
242            processor->cache_ce = ceptr;
243            processor->cache_class_index = i;
244            return i + 1;
245        }
246    }
247    assert(0);
248    return (zend_ulong) -1;
249}
250define(`xc_get_class_num', `xc_get_class_numNOTDEFINED')
251/* }}} */
252/* {{{ xc_get_class */
253#ifdef ZEND_ENGINE_2
254static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
255    /* must be parent or currrent class */
256    assert(class_num <= processor->active_class_index + 1);
257    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
258}
259#endif
260define(`xc_get_class', `xc_get_classNOTDEFINED')
261/* }}} */
262#ifdef ZEND_ENGINE_2
263/* fix method on store */
264static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
265{
266    zend_function *zf = (zend_function *) dst;
267    zend_class_entry *ce = processor->active_class_entry_dst;
268    const zend_class_entry *srcce = processor->active_class_entry_src;
269
270    /* Fixing up the default functions for objects here since
271     * we need to compare with the newly allocated functions
272     *
273     * caveat: a sub-class method can have the same name as the
274     * parent~s constructor and create problems.
275     */
276
277    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
278        if (!ce->constructor) {
279            ce->constructor = zf;
280        }
281    }
282    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
283        ce->destructor = zf;
284    }
285    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
286        ce->clone = zf;
287    }
288    else {
289    pushdef(`SET_IF_SAME_NAMEs', `
290        SET_IF_SAME_NAME(__get);
291        SET_IF_SAME_NAME(__set);
292#ifdef ZEND_ENGINE_2_1
293        SET_IF_SAME_NAME(__unset);
294        SET_IF_SAME_NAME(__isset);
295#endif
296        SET_IF_SAME_NAME(__call);
297#ifdef ZEND_CALLSTATIC_FUNC_NAME
298        SET_IF_SAME_NAME(__callstatic);
299#endif
300#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
301        SET_IF_SAME_NAME(__tostring);
302#endif
303    ')
304#ifdef IS_UNICODE
305        if (UG(unicode)) {
306#define SET_IF_SAME_NAME(member) \
307            do { \
308                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
309                    ce->member = zf; \
310                } \
311            } \
312            while(0)
313
314            SET_IF_SAME_NAMEs()
315#undef SET_IF_SAME_NAME
316        }
317        else
318#endif
319        do {
320#define SET_IF_SAME_NAME(member) \
321            do { \
322                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
323                    ce->member = zf; \
324                } \
325            } \
326            while(0)
327
328            SET_IF_SAME_NAMEs()
329#undef SET_IF_SAME_NAME
330        } while (0);
331
332    popdef(`SET_IF_SAME_NAMEs')
333
334    }
335}
336/* }}} */
337#endif
338/* {{{ call op_array ctor handler */
339extern zend_bool xc_have_op_array_ctor;
340static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
341{
342    if (extension->op_array_ctor) {
343        extension->op_array_ctor(op_array);
344    }
345}
346/* }}} */
347/* {{{ field name checker */
348IFAUTOCHECK(`dnl
349static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, int assert_names_count, HashTable *done_names)
350{
351    int errors = 0;
352    if (assert_names_count) {
353        int i;
354        Bucket *b;
355
356        for (i = 0; i < assert_names_count; ++i) {
357            if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], strlen(assert_names[i]) + 1)) {
358                fprintf(stderr
359                    , "missing field at %s `#'%d %s`' : %s\n"
360                    , file, line, functionName
361                    , assert_names[i]
362                    );
363                ++errors;
364            }
365        }
366
367        for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
368            int known = 0;
369            int i;
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            int real = processor.p - oldp;
452            int should = processor.size;
453            if (real != processor.size) {
454                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
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.