source: trunk/processor/head.m4 @ 189

Last change on this file since 189 was 189, checked in by moo, 8 years ago

call op_array_ctor handler on restore to be more compatible with other zend_extension

File size: 9.8 KB
RevLine 
[1]1dnl {{{ === program start ========================================
2divert(0)
3#include <string.h>
4#include <stdio.h>
5
6#include "php.h"
[189]7#include "zend_extensions.h"
[1]8#include "zend_compile.h"
9#include "zend_API.h"
[4]10#include "zend_ini.h"
[1]11
12#include "xcache.h"
13#include "align.h"
14#include "const_string.h"
15#include "processor.h"
16#include "stack.h"
17#include "xcache_globals.h"
18
[58]19#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
20extern unsigned int zend_hash_canary;
21#endif
[189]22extern zend_bool xc_have_op_array_ctor;
23static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
24{
25    if (extension->op_array_ctor) {
26        extension->op_array_ctor(op_array);
27    }
28}
[58]29
[1]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_xc_entry_name_t', `sizeof(xc_entry_name_t)')
39define(`COUNTOF_xc_entry_name_t', `1')
40
41ifdef(`XCACHE_ENABLE_TEST', `
42#undef NDEBUG
43#include <assert.h>
[29]44m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
45', `
46m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
[1]47')
[29]48sinclude(builddir`/structinfo.m4')
49
[1]50#ifndef NDEBUG
51#   undef inline
52#define inline
53#endif
54
55typedef zval *zval_ptr;
56typedef zend_uchar zval_data_type;
[103]57#ifdef IS_UNICODE
58typedef UChar zstr_uchar;
59#endif
60typedef char  zstr_char;
[1]61
62#define MAX_DUP_STR_LEN 256
63dnl }}}
[178]64/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
65struct _xc_processor_t {
[1]66    char *p;
67    zend_uint size;
68    HashTable strings;
69    HashTable zvalptrs;
70    zend_bool reference; /* enable if to deal with reference */
71    const xc_entry_t *xce_src;
72    const xc_entry_t *xce_dst;
73    const zend_class_entry *cache_ce;
74    zend_uint cache_class_num;
75
76    const zend_op          *active_opcodes_src;
77    zend_op                *active_opcodes_dst;
78    const zend_class_entry *active_class_entry_src;
79    zend_class_entry       *active_class_entry_dst;
80    zend_uint               active_class_num;
81
82    zend_bool readonly_protection; /* wheather it's present */
83IFASSERT(xc_stack_t allocsizes;)
84};
85/* }}} */
86#ifdef XCACHE_HAVE_DPRINT
87static void xc_dprint_indent(int indent) /* {{{ */
88{
89    int i;
90    for (i = 0; i < indent; i ++) {
91        fprintf(stderr, "  ");
92    }
93}
94#endif
95/* }}} */
[103]96/* {{{ xc_zstrlen_char */
97static inline int xc_zstrlen_char(zstr s)
98{
99    return strlen(ZSTR_S(s));
100}
101/* }}} */
102#ifdef IS_UNICODE
103/* {{{ xc_zstrlen_uchar */
104static inline int xc_zstrlen_uchar(zstr s)
105{
106    int i;
107    UChar *p = ZSTR_U(s);
108    for (i = 0; *p; i ++, p++) {
109        /* empty */
110    }
111    return i;
112}
113/* }}} */
114/* {{{ xc_zstrlen */
115static inline int xc_zstrlen(int type, zstr s)
116{
117    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
118}
119/* }}} */
120#else
121/* {{{ xc_zstrlen */
122#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
123/* }}} */
124#endif
[1]125/* {{{ xc_calc_string_n */
126REDEF(`KIND', `calc')
[178]127static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
[1]128    pushdef(`__LINE__', `relayline')
129    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]130    long dummy = 1;
[1]131
132    if (realsize > MAX_DUP_STR_LEN) {
133        ALLOC(, char, realsize)
134    }
[103]135    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
[1]136        /* new string */
137        ALLOC(, char, realsize)
138    }
139    IFASSERT(`
140        else {
[103]141            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
[1]142        }
143    ')
144    popdef(`__LINE__')
145}
146/* }}} */
147/* {{{ xc_store_string_n */
148REDEF(`KIND', `store')
[178]149static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
[1]150    pushdef(`__LINE__', `relayline')
151    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]152    zstr ret, *pret;
[1]153
154    if (realsize > MAX_DUP_STR_LEN) {
[103]155        ALLOC(ZSTR_V(ret), char, realsize)
156        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
157        return ret;
[1]158    }
[103]159
160    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
161        return *pret;
[1]162    }
[103]163
164    /* new string */
165    ALLOC(ZSTR_V(ret), char, realsize)
166    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
167    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
168    return ret;
169
[1]170    popdef(`__LINE__')
171}
172/* }}} */
173/* {{{ xc_get_class_num
174 * return class_index + 1
175 */
[178]176static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
[90]177    zend_ulong i;
[1]178    const xc_entry_t *xce = processor->xce_src;
179    zend_class_entry *ceptr;
180
181    if (processor->cache_ce == ce) {
182        return processor->cache_class_num;
183    }
184    for (i = 0; i < xce->data.php->classinfo_cnt; i ++) {
185        ceptr = CestToCePtr(xce->data.php->classinfos[i].cest);
186        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
187            processor->cache_ce = ceptr;
188            processor->cache_class_num = i + 1;
189            return i + 1;
190        }
191    }
192    assert(0);
[90]193    return (zend_ulong) -1;
[1]194}
195/* }}} */
196/* {{{ xc_get_class */
197#ifdef ZEND_ENGINE_2
[178]198static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
[1]199    /* must be parent or currrent class */
200    assert(class_num <= processor->active_class_num);
201    return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest);
202}
203#endif
204/* }}} */
205#ifdef ZEND_ENGINE_2
206/* fix method on store */
[178]207static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst) /* {{{ */
[1]208{
209    zend_function *zf = (zend_function *) dst;
210    zend_class_entry *ce = processor->active_class_entry_dst;
211
212    /* Fixing up the default functions for objects here since
213     * we need to compare with the newly allocated functions
214     *
215     * caveat: a sub-class method can have the same name as the
216     * parent~s constructor and create problems.
217     */
218
219    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
220        if (!ce->constructor) {
221            ce->constructor = zf;
222        }
223    }
224    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
225        ce->destructor = zf;
226    }
227    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
228        ce->clone = zf;
229    }
230    else {
[103]231dnl FIXME: handle common.function_name here
[1]232#define SET_IF_SAME_NAME(member) \
233        do { \
[103]234            if (!strcasecmp(ZSTR_S(zf->common.function_name), #member)) { \
[1]235                ce->member = zf; \
236            } \
237        } \
238        while(0)
239        /* if(ce->member && !strcmp(zf->common.function_name, ce->member->common.function_name)) { \ */
240
241        SET_IF_SAME_NAME(__get);
242        SET_IF_SAME_NAME(__set);
[56]243#ifdef ZEND_ENGINE_2_1
[1]244        SET_IF_SAME_NAME(__unset);
245        SET_IF_SAME_NAME(__isset);
[56]246#endif
[1]247        SET_IF_SAME_NAME(__call);
[94]248#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
[1]249        SET_IF_SAME_NAME(__tostring);
250#endif
251
252#undef SET_IF_SAME_NAME
253    }
254}
255/* }}} */
256#endif
257dnl ================ export API
258/* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */
259xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) {
260    xc_entry_t *dst;
[178]261    xc_processor_t processor;
[1]262
263    memset(&processor, 0, sizeof(processor));
264    if (src->type == XC_TYPE_VAR) {
265        processor.reference = 1;
266    }
267
268    IFASSERT(`xc_stack_init(&processor.allocsizes);')
269
270    /* calc size */ {
271        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
272        if (processor.reference) {
273            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
274        }
275
276        processor.size = 0;
277        /* allocate */
278        processor.size = ALIGN(processor.size + sizeof(src[0]));
279
280        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
281        if (processor.reference) {
282            zend_hash_destroy(&processor.zvalptrs);
283        }
284        zend_hash_destroy(&processor.strings);
285    }
286    src->size = processor.size;
287
288    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
289    /* store {{{ */
290    {
291        IFASSERT(`char *oldp;')
292        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
293        if (processor.reference) {
294            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
295        }
296
297        /* mem :) */
[148]298        processor.p = (char *) src->cache->mem->handlers->malloc(src->cache->mem, processor.size);
[1]299        if (processor.p == NULL) {
300            dst = NULL;
301            goto err_alloc;
302        }
303        IFASSERT(`oldp = processor.p;')
304        assert(processor.p == (char *) ALIGN(processor.p));
305
306        /* allocate */
307        dst = (xc_entry_t *) processor.p;
308        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
309
310        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
311        IFASSERT(` {
312            int real = processor.p - oldp;
313            int should = processor.size;
314            if (real != processor.size) {
315                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
316                abort();
317            }
318        }')
319err_alloc:
320        if (processor.reference) {
321            zend_hash_destroy(&processor.zvalptrs);
322        }
323        zend_hash_destroy(&processor.strings);
324    }
325    /* }}} */
326
327    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
328
329    return dst;
330}
331/* }}} */
332/* export: xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC); :export {{{ */
333xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
[178]334    xc_processor_t processor;
[1]335
336    memset(&processor, 0, sizeof(processor));
337    processor.readonly_protection = readonly_protection;
338
339    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
340    return dst;
341}
342/* }}} */
343/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */
344zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) {
[178]345    xc_processor_t processor;
[1]346
347    memset(&processor, 0, sizeof(processor));
348    processor.reference = 1;
349
350    zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
351    dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
352    zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
353    xc_restore_zval(&processor, dst, src TSRMLS_CC);
354    zend_hash_destroy(&processor.zvalptrs);
355
356    return dst;
357}
358/* }}} */
359/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
360#ifdef XCACHE_HAVE_DPRINT
361void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
362    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
363    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
364}
365#endif
366/* }}} */
Note: See TracBrowser for help on using the repository browser.