source: branches/1.2/processor/head.m4 @ 394

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

merged [393] from trunk: svn:eol-style

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