source: trunk/processor/head.m4 @ 1452

Last change on this file since 1452 was 1452, checked in by moo, 5 months ago

improve readability using ptradd

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