source: trunk/processor/head.m4 @ 1446

Last change on this file since 1446 was 1446, checked in by moo, 10 months ago

update relocate

  • Property svn:eol-style set to native
File size: 10.3 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"')
13EXPORT(`#include "mod_cacher/xc_cache.h"')
14EXPORT(`#include "xcache/xc_shm.h"')
15EXPORT(`#include "xcache/xc_allocator.h"')
16#include "xc_processor.h"
17#include "xcache/xc_const_string.h"
18#include "xcache/xc_utils.h"
19#include "util/xc_align.h"
20#include "xcache_globals.h"
21
22#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
23extern unsigned int zend_hash_canary;
24#endif
25
26define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
27define(`COUNTOF_zend_uint', `1')
28define(`SIZEOF_int', `sizeof(int)')
29define(`COUNTOF_int', `1')
30define(`SIZEOF_zend_function', `sizeof(zend_function)')
31define(`COUNTOF_zend_function', `1')
32define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
33define(`COUNTOF_zval_ptr', `1')
34define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
35define(`COUNTOF_zval_ptr_nullable', `1')
36define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
37define(`COUNTOF_zend_trait_alias_ptr', `1')
38define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
39define(`COUNTOF_zend_trait_precedence_ptr', `1')
40define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
41define(`COUNTOF_xc_entry_name_t', `1')
42define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
43define(`COUNTOF_xc_ztstring', `1')
44
45ifdef(`XCACHE_ENABLE_TEST', `
46#undef NDEBUG
47#include <assert.h>
48m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
49', `
50m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
51')
52ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
53
54sinclude(builddir`/structinfo.m4')
55
56#ifndef NDEBUG
57#   undef inline
58#define inline
59#endif
60
61typedef zval *zval_ptr;
62typedef zval *zval_ptr_nullable;
63typedef char *xc_ztstring;
64#ifdef ZEND_ENGINE_2_4
65typedef zend_trait_alias *zend_trait_alias_ptr;
66typedef zend_trait_precedence *zend_trait_precedence_ptr;
67#endif
68#ifdef ZEND_ENGINE_2_3
69typedef int last_brk_cont_t;
70#else
71typedef zend_uint last_brk_cont_t;
72#endif
73
74typedef zend_uchar xc_zval_type_t;
75typedef int xc_op_type;
76typedef zend_uchar xc_opcode;
77#ifdef IS_UNICODE
78typedef UChar zstr_uchar;
79#endif
80typedef char  zstr_char;
81
82#define MAX_DUP_STR_LEN 256
83dnl }}}
84dnl {{{ _xc_processor_t
85typedef struct _xc_processor_t {
86    char *p;
87    size_t size;
88    HashTable strings;
89    HashTable zvalptrs;
90    zend_bool reference; /* enable if to deal with reference */
91    zend_bool have_references;
92    const xc_entry_php_t *entry_php_src;
93    const xc_entry_php_t *entry_php_dst;
94    const xc_entry_data_php_t *php_src;
95    const xc_entry_data_php_t *php_dst;
96    xc_shm_t                  *shm;
97    xc_allocator_t            *allocator;
98    const zend_class_entry *cache_ce;
99    zend_ulong cache_class_index;
100
101    const zend_op_array    *active_op_array_src;
102    zend_op_array          *active_op_array_dst;
103    const zend_class_entry *active_class_entry_src;
104    zend_class_entry       *active_class_entry_dst;
105    zend_uint                 active_class_index;
106    zend_uint                 active_op_array_index;
107    const xc_op_array_info_t *active_op_array_infos_src;
108
109    zend_bool readonly_protection; /* wheather it's present */
110IFAUTOCHECK(xc_stack_t allocsizes;)
111} xc_processor_t;
112dnl }}}
113EXPORT(`typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t;')
114/* {{{ memsetptr */
115IFAUTOCHECK(`dnl
116static void *memsetptr(void *mem, void *content, size_t n)
117{
118    void **p = (void **) mem;
119    void **end = (void **) ((char *) mem + n);
120    while (p < end - sizeof(content)) {
121        *p = content;
122        p += sizeof(content);
123    }
124    if (p < end) {
125        memset(p, -1, end - p);
126    }
127    return mem;
128}
129')
130/* }}} */
131#ifdef HAVE_XCACHE_DPRINT
132static void xc_dprint_indent(int indent) /* {{{ */
133{
134    int i;
135    for (i = 0; i < indent; i ++) {
136        fprintf(stderr, "  ");
137    }
138}
139/* }}} */
140static void xc_dprint_str_len(const char *str, int len) /* {{{ */
141{
142    const unsigned char *p = (const unsigned char *) str;
143    int i;
144    for (i = 0; i < len; i ++) {
145        if (p[i] < 32 || p[i] == 127) {
146            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
147        }
148        else {
149            fputc(p[i], stderr);
150        }
151    }
152}
153/* }}} */
154#endif
155static inline size_t xc_zstrlen_char(const_zstr s) /* {{{ */
156{
157    return strlen(ZSTR_S(s));
158}
159/* }}} */
160#ifdef IS_UNICODE
161static inline size_t xc_zstrlen_uchar(zstr s) /* {{{ */
162{
163    return u_strlen(ZSTR_U(s));
164}
165/* }}} */
166static inline size_t xc_zstrlen(int type, const_zstr s) /* {{{ */
167{
168    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
169}
170/* }}} */
171#else
172/* {{{ xc_zstrlen */
173#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
174/* }}} */
175#endif
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(`PROCESSOR_TYPE', `calc')
184    pushdef(`__LINE__', `relayline')
185    size_t 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, (uint) 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    popdef(`PROCESSOR_TYPE')
202}
203/* }}} */
204static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
205    pushdef(`PROCESSOR_TYPE', `store')
206    pushdef(`__LINE__', `relayline')
207    size_t 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, (uint) 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, (uint) size, (void *) &ret, sizeof(zstr), NULL);
224    return ret;
225
226    popdef(`__LINE__')
227    popdef(`PROCESSOR_TYPE')
228}
229/* }}} */
230/* {{{ xc_get_class_num
231 * return class_index + 1
232 */
233static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
234    zend_uint i;
235    const xc_entry_data_php_t *php = processor->php_src;
236    zend_class_entry *ceptr;
237
238    if (processor->cache_ce == ce) {
239        return processor->cache_class_index + 1;
240    }
241    for (i = 0; i < php->classinfo_cnt; i ++) {
242        ceptr = CestToCePtr(php->classinfos[i].cest);
243        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
244            processor->cache_ce = ceptr;
245            processor->cache_class_index = i;
246            return i + 1;
247        }
248    }
249    assert(0);
250    return (zend_ulong) -1;
251}
252define(`xc_get_class_num', `IFSTORE(``xc_get_class_num'($@)',``xc_get_class_num' can be use in store only')')
253/* }}} */
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}
260define(`xc_get_class', `IFRESTORE(``xc_get_class'($@)',``xc_get_class' can be use in restore only')')
261/* }}} */
262#endif
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, size_t assert_names_count, HashTable *done_names)
351{
352    int errors = 0;
353    if (assert_names_count) {
354        size_t 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], (uint) strlen(assert_names[i]) + 1)) {
359                fprintf(stderr
360                    , "Error: 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                    , "Error: 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/* }}} */
Note: See TracBrowser for help on using the repository browser.