source: trunk/processor/head.m4

Last change on this file was 1490, checked in by moo, 8 weeks ago

cacher: PHP_5_6: fix support for debugInfo

  • Property svn:eol-style set to native
File size: 10.7 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 <stddef.h>')
13EXPORT(`#include "xcache.h"')
14EXPORT(`#include "mod_cacher/xc_cache.h"')
15EXPORT(`#include "xcache/xc_shm.h"')
16EXPORT(`#include "xcache/xc_allocator.h"')
17#include "xc_processor.h"
18#include "xcache/xc_const_string.h"
19#include "xcache/xc_utils.h"
20#include "util/xc_align.h"
21#include "xcache_globals.h"
22
23#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
24extern unsigned int zend_hash_canary;
25#endif
26
27define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
28define(`COUNTOF_zend_uint', `1')
29define(`SIZEOF_int', `sizeof(int)')
30define(`COUNTOF_int', `1')
31define(`SIZEOF_zend_function', `sizeof(zend_function)')
32define(`COUNTOF_zend_function', `1')
33define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
34define(`COUNTOF_zval_ptr', `1')
35define(`SIZEOF_zval_ptr_nullable', `sizeof(zval_ptr_nullable)')
36define(`COUNTOF_zval_ptr_nullable', `1')
37define(`SIZEOF_zend_trait_alias_ptr', `sizeof(zend_trait_alias)')
38define(`COUNTOF_zend_trait_alias_ptr', `1')
39define(`SIZEOF_zend_trait_precedence_ptr', `sizeof(zend_trait_precedence)')
40define(`COUNTOF_zend_trait_precedence_ptr', `1')
41define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
42define(`COUNTOF_xc_entry_name_t', `1')
43define(`SIZEOF_xc_ztstring', `sizeof(xc_ztstring)')
44define(`COUNTOF_xc_ztstring', `1')
45
46ifdef(`XCACHE_ENABLE_TEST', `
47#undef NDEBUG
48#include <assert.h>
49m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
50', `
51m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
52')
53ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
54
55#ifndef NDEBUG
56#   undef inline
57#define inline
58#endif
59
60typedef zval *zval_ptr;
61typedef zval *zval_ptr_nullable;
62typedef char *xc_ztstring;
63#ifdef ZEND_ENGINE_2_4
64typedef zend_trait_alias *zend_trait_alias_ptr;
65typedef zend_trait_precedence *zend_trait_precedence_ptr;
66#endif
67#ifdef ZEND_ENGINE_2_3
68typedef int last_brk_cont_t;
69#else
70typedef zend_uint last_brk_cont_t;
71#endif
72
73typedef zend_uchar xc_zval_type_t;
74typedef int xc_op_type;
75typedef zend_uchar xc_opcode;
76#ifdef IS_UNICODE
77typedef UChar zstr_uchar;
78#endif
79typedef char  zstr_char;
80
81#define MAX_DUP_STR_LEN 256
82
83#define ptradd(type, ptr, ptrdiff) ((type) (((char *) (ptr)) + (ptrdiff)))
84#define ptrsub(ptr1, ptr2) (((char *) (ptr1)) - ((char *) (ptr2)))
85#ifdef NDEBUG
86    #define notnullable(ptr) (ptr)
87#else
88static inline void *notnullable(const void *ptr)
89{
90    assert(ptr);
91    return (void *) ptr;
92}
93#endif
94dnl }}}
95dnl {{{ _xc_processor_t
96typedef struct _xc_processor_t {
97    char *p;
98    size_t size;
99    HashTable strings;
100    HashTable zvalptrs;
101    zend_bool handle_reference; /* enable if to deal with reference */
102    zend_bool have_references;
103    ptrdiff_t relocatediff;
104
105    const xc_entry_php_t *entry_php_src;
106    const xc_entry_php_t *entry_php_dst;
107    const xc_entry_data_php_t *php_src;
108    const xc_entry_data_php_t *php_dst;
109    const zend_class_entry *cache_ce;
110    zend_ulong cache_class_index;
111
112    const zend_op_array    *active_op_array_src;
113    zend_op_array          *active_op_array_dst;
114    const zend_class_entry *active_class_entry_src;
115    zend_class_entry       *active_class_entry_dst;
116    zend_uint                 active_class_index;
117    zend_uint                 active_op_array_index;
118    const xc_op_array_info_t *active_op_array_infos_src;
119
120    zend_bool readonly_protection; /* wheather it's present */
121IFAUTOCHECK(xc_stack_t allocsizes;)
122} xc_processor_t;
123dnl }}}
124EXPORT(`typedef struct _xc_dasm_t { const zend_op_array *active_op_array_src; } xc_dasm_t;')
125/* {{{ memsetptr */
126IFAUTOCHECK(`dnl
127static void *memsetptr(void *mem, void *content, size_t n)
128{
129    void **p = (void **) mem;
130    void **end = (void **) ((char *) mem + n);
131    while (p < end - sizeof(content)) {
132        *p = content;
133        p += sizeof(content);
134    }
135    if (p < end) {
136        memset(p, -1, end - p);
137    }
138    return mem;
139}
140')
141/* }}} */
142#ifdef HAVE_XCACHE_DPRINT
143static void xc_dprint_indent(int indent) /* {{{ */
144{
145    int i;
146    for (i = 0; i < indent; i ++) {
147        fprintf(stderr, "  ");
148    }
149}
150/* }}} */
151static void xc_dprint_str_len(const char *str, int len) /* {{{ */
152{
153    const unsigned char *p = (const unsigned char *) str;
154    int i;
155    for (i = 0; i < len; i ++) {
156        if (p[i] < 32 || p[i] == 127) {
157            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
158        }
159        else {
160            fputc(p[i], stderr);
161        }
162    }
163}
164/* }}} */
165#endif
166static inline size_t xc_zstrlen_char(const_zstr s) /* {{{ */
167{
168    return strlen(ZSTR_S(s));
169}
170/* }}} */
171#ifdef IS_UNICODE
172static inline size_t xc_zstrlen_uchar(zstr s) /* {{{ */
173{
174    return u_strlen(ZSTR_U(s));
175}
176/* }}} */
177static inline size_t xc_zstrlen(int type, const_zstr s) /* {{{ */
178{
179    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
180}
181/* }}} */
182#else
183/* {{{ xc_zstrlen */
184#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
185/* }}} */
186#endif
187#undef C_RELAYLINE
188#define C_RELAYLINE
189IFAUTOCHECK(`
190#undef C_RELAYLINE
191#define C_RELAYLINE , __LINE__
192')
193static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
194    pushdef(`PROCESSOR_TYPE', `calc')
195    pushdef(`__LINE__', `relayline')
196    size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
197    long dummy = 1;
198
199    if (realsize > MAX_DUP_STR_LEN) {
200        ALLOC(, char, realsize)
201    }
202    else if (zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
203        /* new string */
204        ALLOC(, char, realsize)
205    }
206    IFAUTOCHECK(`
207        else {
208            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
209        }
210    ')
211    popdef(`__LINE__')
212    popdef(`PROCESSOR_TYPE')
213}
214/* }}} */
215static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const_zstr str, long size IFAUTOCHECK(`, int relayline')) { /* {{{ */
216    pushdef(`PROCESSOR_TYPE', `store')
217    pushdef(`__LINE__', `relayline')
218    size_t realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
219    zstr ret, *pret;
220
221    if (realsize > MAX_DUP_STR_LEN) {
222        ALLOC(ZSTR_V(ret), char, realsize)
223        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
224        return ret;
225    }
226
227    if (zend_u_hash_find(&processor->strings, type, str, (uint) size, (void **) &pret) == SUCCESS) {
228        return *pret;
229    }
230
231    /* new string */
232    ALLOC(ZSTR_V(ret), char, realsize)
233    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
234    zend_u_hash_add(&processor->strings, type, str, (uint) size, (void *) &ret, sizeof(zstr), NULL);
235    return ret;
236
237    popdef(`__LINE__')
238    popdef(`PROCESSOR_TYPE')
239}
240/* }}} */
241/* {{{ xc_get_class_num
242 * return class_index + 1
243 */
244static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
245    zend_uint i;
246    const xc_entry_data_php_t *php = processor->php_src;
247    zend_class_entry *ceptr;
248
249    if (processor->cache_ce == ce) {
250        return processor->cache_class_index + 1;
251    }
252    for (i = 0; i < php->classinfo_cnt; i ++) {
253        ceptr = CestToCePtr(php->classinfos[i].cest);
254        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
255            processor->cache_ce = ceptr;
256            processor->cache_class_index = i;
257            assert(i <= processor->active_class_index);
258            return i + 1;
259        }
260    }
261    assert(0);
262    return (zend_ulong) -1;
263}
264define(`xc_get_class_num', `IFSTORE(``xc_get_class_num'($@)',``xc_get_class_num' can be use in store only')')
265/* }}} */
266#ifdef ZEND_ENGINE_2
267static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) { /* {{{ */
268    /* must be parent or currrent class */
269    assert(class_num > 0);
270    assert(class_num <= processor->active_class_index + 1);
271    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
272}
273define(`xc_get_class', `IFRESTORE(``xc_get_class'($@)',``xc_get_class' can be use in restore only')')
274/* }}} */
275#endif
276#ifdef ZEND_ENGINE_2
277/* fix method on store */
278static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
279{
280    zend_function *zf = (zend_function *) dst;
281    zend_class_entry *ce = processor->active_class_entry_dst;
282    const zend_class_entry *srcce = processor->active_class_entry_src;
283
284    /* Fixing up the default functions for objects here since
285     * we need to compare with the newly allocated functions
286     *
287     * caveat: a sub-class method can have the same name as the
288     * parent~s constructor and create problems.
289     */
290
291    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
292        if (!ce->constructor) {
293            ce->constructor = zf;
294        }
295    }
296    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
297        ce->destructor = zf;
298    }
299    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
300        ce->clone = zf;
301    }
302    else {
303    pushdef(`SET_IF_SAME_NAMEs', `
304        SET_IF_SAME_NAME(__get);
305        SET_IF_SAME_NAME(__set);
306#ifdef ZEND_ENGINE_2_1
307        SET_IF_SAME_NAME(__unset);
308        SET_IF_SAME_NAME(__isset);
309#endif
310        SET_IF_SAME_NAME(__call);
311#ifdef ZEND_CALLSTATIC_FUNC_NAME
312        SET_IF_SAME_NAME(__callstatic);
313#endif
314#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
315        SET_IF_SAME_NAME(__tostring);
316#endif
317#if defined(ZEND_ENGINE_2_6)
318        SET_IF_SAME_NAME(__debugInfo);
319#endif
320    ')
321#ifdef IS_UNICODE
322        if (UG(unicode)) {
323#define SET_IF_SAME_NAME(member) \
324            do { \
325                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
326                    ce->member = zf; \
327                } \
328            } \
329            while(0)
330
331            SET_IF_SAME_NAMEs()
332#undef SET_IF_SAME_NAME
333        }
334        else
335#endif
336        do {
337#define SET_IF_SAME_NAME(member) \
338            do { \
339                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
340                    ce->member = zf; \
341                } \
342            } \
343            while(0)
344
345            SET_IF_SAME_NAMEs()
346#undef SET_IF_SAME_NAME
347        } while (0);
348
349    popdef(`SET_IF_SAME_NAMEs')
350
351    }
352}
353/* }}} */
354#endif
355/* {{{ call op_array ctor handler */
356extern zend_bool xc_have_op_array_ctor;
357static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
358{
359    if (extension->op_array_ctor) {
360        extension->op_array_ctor(op_array);
361    }
362}
363/* }}} */
364/* {{{ field name checker */
365IFAUTOCHECK(`dnl
366static int xc_check_names(const char *file, int line, const char *functionName, const char **assert_names, size_t assert_names_count, HashTable *done_names)
367{
368    int errors = 0;
369    if (assert_names_count) {
370        size_t i;
371        Bucket *b;
372
373        for (i = 0; i < assert_names_count; ++i) {
374            if (!zend_u_hash_exists(done_names, IS_STRING, assert_names[i], (uint) strlen(assert_names[i]) + 1)) {
375                fprintf(stderr
376                    , "Error: missing field at %s `#'%d %s`' : %s\n"
377                    , file, line, functionName
378                    , assert_names[i]
379                    );
380                ++errors;
381            }
382        }
383
384        for (b = done_names->pListHead; b != NULL; b = b->pListNext) {
385            int known = 0;
386            for (i = 0; i < assert_names_count; ++i) {
387                if (strcmp(assert_names[i], BUCKET_KEY_S(b)) == 0) {
388                    known = 1;
389                    break;
390                }
391            }
392            if (!known) {
393                fprintf(stderr
394                    , "Error: unknown field at %s `#'%d %s`' : %s\n"
395                    , file, line, functionName
396                    , BUCKET_KEY_S(b)
397                    );
398                ++errors;
399            }
400        }
401    }
402    return errors;
403}
404')
405/* }}} */
Note: See TracBrowser for help on using the repository browser.