source: trunk/processor/head.m4 @ 195

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

processor: move op_array ctor to be more readable

File size: 9.9 KB
Line 
1dnl {{{ === program start ========================================
2divert(0)
3#include <string.h>
4#include <stdio.h>
5
6#include "php.h"
7#include "zend_extensions.h"
8#include "zend_compile.h"
9#include "zend_API.h"
10#include "zend_ini.h"
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
19#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
20extern unsigned int zend_hash_canary;
21#endif
22
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>
37m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
38', `
39m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
40')
41sinclude(builddir`/structinfo.m4')
42
43#ifndef NDEBUG
44#   undef inline
45#define inline
46#endif
47
48typedef zval *zval_ptr;
49typedef zend_uchar zval_data_type;
50#ifdef IS_UNICODE
51typedef UChar zstr_uchar;
52#endif
53typedef char  zstr_char;
54
55#define MAX_DUP_STR_LEN 256
56dnl }}}
57/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
58struct _xc_processor_t {
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/* }}} */
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
118/* {{{ xc_calc_string_n */
119REDEF(`KIND', `calc')
120static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
121    pushdef(`__LINE__', `relayline')
122    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
123    long dummy = 1;
124
125    if (realsize > MAX_DUP_STR_LEN) {
126        ALLOC(, char, realsize)
127    }
128    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
129        /* new string */
130        ALLOC(, char, realsize)
131    }
132    IFASSERT(`
133        else {
134            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
135        }
136    ')
137    popdef(`__LINE__')
138}
139/* }}} */
140/* {{{ xc_store_string_n */
141REDEF(`KIND', `store')
142static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
143    pushdef(`__LINE__', `relayline')
144    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
145    zstr ret, *pret;
146
147    if (realsize > MAX_DUP_STR_LEN) {
148        ALLOC(ZSTR_V(ret), char, realsize)
149        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
150        return ret;
151    }
152
153    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
154        return *pret;
155    }
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
163    popdef(`__LINE__')
164}
165/* }}} */
166/* {{{ xc_get_class_num
167 * return class_index + 1
168 */
169static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
170    zend_ulong i;
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);
186    return (zend_ulong) -1;
187}
188/* }}} */
189/* {{{ xc_get_class */
190#ifdef ZEND_ENGINE_2
191static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
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 */
200static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst) /* {{{ */
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 {
224dnl FIXME: handle common.function_name here
225#define SET_IF_SAME_NAME(member) \
226        do { \
227            if (!strcasecmp(ZSTR_S(zf->common.function_name), #member)) { \
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);
236#ifdef ZEND_ENGINE_2_1
237        SET_IF_SAME_NAME(__unset);
238        SET_IF_SAME_NAME(__isset);
239#endif
240        SET_IF_SAME_NAME(__call);
241#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
242        SET_IF_SAME_NAME(__tostring);
243#endif
244
245#undef SET_IF_SAME_NAME
246    }
247}
248/* }}} */
249#endif
250/* {{{ call op_array ctor handler */
251extern zend_bool xc_have_op_array_ctor;
252static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
253{
254    if (extension->op_array_ctor) {
255        extension->op_array_ctor(op_array);
256    }
257}
258/* }}} */
259dnl ================ export API
260/* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */
261xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) {
262    xc_entry_t *dst;
263    xc_processor_t processor;
264
265    memset(&processor, 0, sizeof(processor));
266    if (src->type == XC_TYPE_VAR) {
267        processor.reference = 1;
268    }
269
270    IFASSERT(`xc_stack_init(&processor.allocsizes);')
271
272    /* calc size */ {
273        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
274        if (processor.reference) {
275            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
276        }
277
278        processor.size = 0;
279        /* allocate */
280        processor.size = ALIGN(processor.size + sizeof(src[0]));
281
282        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
283        if (processor.reference) {
284            zend_hash_destroy(&processor.zvalptrs);
285        }
286        zend_hash_destroy(&processor.strings);
287    }
288    src->size = processor.size;
289
290    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
291    /* store {{{ */
292    {
293        IFASSERT(`char *oldp;')
294        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
295        if (processor.reference) {
296            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
297        }
298
299        /* mem :) */
300        processor.p = (char *) src->cache->mem->handlers->malloc(src->cache->mem, processor.size);
301        if (processor.p == NULL) {
302            dst = NULL;
303            goto err_alloc;
304        }
305        IFASSERT(`oldp = processor.p;')
306        assert(processor.p == (char *) ALIGN(processor.p));
307
308        /* allocate */
309        dst = (xc_entry_t *) processor.p;
310        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
311
312        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
313        IFASSERT(` {
314            int real = processor.p - oldp;
315            int should = processor.size;
316            if (real != processor.size) {
317                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
318                abort();
319            }
320        }')
321err_alloc:
322        if (processor.reference) {
323            zend_hash_destroy(&processor.zvalptrs);
324        }
325        zend_hash_destroy(&processor.strings);
326    }
327    /* }}} */
328
329    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
330
331    return dst;
332}
333/* }}} */
334/* 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 {{{ */
335xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
336    xc_processor_t processor;
337
338    memset(&processor, 0, sizeof(processor));
339    processor.readonly_protection = readonly_protection;
340
341    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
342    return dst;
343}
344/* }}} */
345/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */
346zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) {
347    xc_processor_t processor;
348
349    memset(&processor, 0, sizeof(processor));
350    processor.reference = 1;
351
352    zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
353    dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
354    zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
355    xc_restore_zval(&processor, dst, src TSRMLS_CC);
356    zend_hash_destroy(&processor.zvalptrs);
357
358    return dst;
359}
360/* }}} */
361/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
362#ifdef XCACHE_HAVE_DPRINT
363void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
364    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
365    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
366}
367#endif
368/* }}} */
Note: See TracBrowser for help on using the repository browser.