source: trunk/processor/head.m4 @ 196

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

fix #36, wrong inheriting static class variables

File size: 11.5 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/* }}} */
249static int xc_hash_static_member_check(xc_processor_t *processor, Bucket *b TSRMLS_DC) /* {{{ */
250{
251    zend_class_entry *src = processor->active_class_entry_src;
252    if (src->parent) {
253        zval **srczv;
254        if (zend_hash_quick_find(CE_STATIC_MEMBERS(src), b->arKey, b->nKeyLength, b->h, (void **) &srczv) == SUCCESS) {
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{
267    (*p)->refcount++;
268    (*p)->is_ref = 1;
269}
270/* }}} */
271static void xc_fix_static_members(xc_processor_t *processor, zend_class_entry *dst TSRMLS_DC) /* {{{ */
272{
273    zend_class_entry *parent_ce = dst->parent;
274    if (parent_ce->type != dst->type) {
275        /* User class extends internal class */
276        zend_update_class_constants(parent_ce  TSRMLS_CC);
277        zend_hash_merge(&dst->default_static_members, CE_STATIC_MEMBERS(parent_ce), (void (*)(void *)) inherit_static_prop, NULL, sizeof(zval *), 0);
278    }
279    else {
280        zend_hash_merge(&dst->default_static_members, &parent_ce->default_static_members, (void (*)(void *)) inherit_static_prop, NULL, sizeof(zval *), 0);
281    }
282
283    /*
284    HashPosition pos;
285    HashTable *pmembers = CE_STATIC_MEMBERS(dst->parent);
286    zval **zv;
287    for (zend_hash_internal_pointer_reset_ex(pmembers, &pos);
288            zend_hash_get_current_data_ex(pmembers, (void **) &zv, &pos) == SUCCESS;
289            zend_hash_move_forward_ex(pmembers, &pos)) {
290            if (zend_hash_quick_find(parent->static_members, p->arKey, p->nKeyLength, p->h, &pprop.ptr) == SUCCESS) {
291            }
292    }
293    */
294}
295/* }}} */
296#endif
297/* {{{ call op_array ctor handler */
298extern zend_bool xc_have_op_array_ctor;
299static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
300{
301    if (extension->op_array_ctor) {
302        extension->op_array_ctor(op_array);
303    }
304}
305/* }}} */
306dnl ================ export API
307/* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */
308xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) {
309    xc_entry_t *dst;
310    xc_processor_t processor;
311
312    memset(&processor, 0, sizeof(processor));
313    if (src->type == XC_TYPE_VAR) {
314        processor.reference = 1;
315    }
316
317    IFASSERT(`xc_stack_init(&processor.allocsizes);')
318
319    /* calc size */ {
320        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
321        if (processor.reference) {
322            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
323        }
324
325        processor.size = 0;
326        /* allocate */
327        processor.size = ALIGN(processor.size + sizeof(src[0]));
328
329        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
330        if (processor.reference) {
331            zend_hash_destroy(&processor.zvalptrs);
332        }
333        zend_hash_destroy(&processor.strings);
334    }
335    src->size = processor.size;
336
337    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
338    /* store {{{ */
339    {
340        IFASSERT(`char *oldp;')
341        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
342        if (processor.reference) {
343            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
344        }
345
346        /* mem :) */
347        processor.p = (char *) src->cache->mem->handlers->malloc(src->cache->mem, processor.size);
348        if (processor.p == NULL) {
349            dst = NULL;
350            goto err_alloc;
351        }
352        IFASSERT(`oldp = processor.p;')
353        assert(processor.p == (char *) ALIGN(processor.p));
354
355        /* allocate */
356        dst = (xc_entry_t *) processor.p;
357        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
358
359        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
360        IFASSERT(` {
361            int real = processor.p - oldp;
362            int should = processor.size;
363            if (real != processor.size) {
364                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
365                abort();
366            }
367        }')
368err_alloc:
369        if (processor.reference) {
370            zend_hash_destroy(&processor.zvalptrs);
371        }
372        zend_hash_destroy(&processor.strings);
373    }
374    /* }}} */
375
376    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
377
378    return dst;
379}
380/* }}} */
381/* 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 {{{ */
382xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
383    xc_processor_t processor;
384
385    memset(&processor, 0, sizeof(processor));
386    processor.readonly_protection = readonly_protection;
387
388    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
389    return dst;
390}
391/* }}} */
392/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */
393zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) {
394    xc_processor_t processor;
395
396    memset(&processor, 0, sizeof(processor));
397    processor.reference = 1;
398
399    zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
400    dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
401    zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
402    xc_restore_zval(&processor, dst, src TSRMLS_CC);
403    zend_hash_destroy(&processor.zvalptrs);
404
405    return dst;
406}
407/* }}} */
408/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
409#ifdef XCACHE_HAVE_DPRINT
410void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
411    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
412    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
413}
414#endif
415/* }}} */
Note: See TracBrowser for help on using the repository browser.