source: trunk/processor/head.m4 @ 1443

Last change on this file since 1443 was 1443, checked in by moo, 11 months ago

simplify export code

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