source: trunk/processor/head.m4 @ 217

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

processor: reset refcount to 0 to kill leak warning

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