source: trunk/processor/head.m4 @ 1442

Last change on this file since 1442 was 1442, checked in by moo, 13 months ago

rename ptrmove to relocate, reduce export to only necessary onces

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