source: trunk/processor/head.m4 @ 103

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

PHP_6: builds again with php6. update UChar/void ptr to zstr.

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