source: trunk/processor/head.m4 @ 200

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

BUCKET_KEY readability and PHP_6 build fix

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