source: tags/1.2.2/processor/head.m4 @ 493

Last change on this file since 493 was 493, checked in by moo, 6 years ago

released 1.2.2

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