source: trunk/processor/head.m4 @ 230

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

processor: dprint upgrade, added --enable-xcache-dprint

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