source: branches/1.3/processor/head.m4 @ 622

Last change on this file since 622 was 622, checked in by moo, 5 years ago

merged r504:513 from trunk

  • Property svn:eol-style set to native
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')
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    return u_strlen(ZSTR_U(s));
117}
118/* }}} */
119/* {{{ xc_zstrlen */
120static inline int xc_zstrlen(int type, zstr s)
121{
122    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
123}
124/* }}} */
125#else
126/* {{{ xc_zstrlen */
127#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
128/* }}} */
129#endif
130/* {{{ xc_calc_string_n */
131REDEF(`KIND', `calc')
132static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
133    pushdef(`__LINE__', `relayline')
134    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
135    long dummy = 1;
136
137    if (realsize > MAX_DUP_STR_LEN) {
138        ALLOC(, char, realsize)
139    }
140    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
141        /* new string */
142        ALLOC(, char, realsize)
143    }
144    IFASSERT(`
145        else {
146            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
147        }
148    ')
149    popdef(`__LINE__')
150}
151/* }}} */
152/* {{{ xc_store_string_n */
153REDEF(`KIND', `store')
154static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, zstr str, long size IFASSERT(`, int relayline')) {
155    pushdef(`__LINE__', `relayline')
156    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
157    zstr ret, *pret;
158
159    if (realsize > MAX_DUP_STR_LEN) {
160        ALLOC(ZSTR_V(ret), char, realsize)
161        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
162        return ret;
163    }
164
165    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
166        return *pret;
167    }
168
169    /* new string */
170    ALLOC(ZSTR_V(ret), char, realsize)
171    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
172    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
173    return ret;
174
175    popdef(`__LINE__')
176}
177/* }}} */
178/* {{{ xc_get_class_num
179 * return class_index + 1
180 */
181static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
182    zend_ulong i;
183    const xc_entry_t *xce = processor->xce_src;
184    zend_class_entry *ceptr;
185
186    if (processor->cache_ce == ce) {
187        return processor->cache_class_num;
188    }
189    for (i = 0; i < xce->data.php->classinfo_cnt; i ++) {
190        ceptr = CestToCePtr(xce->data.php->classinfos[i].cest);
191        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
192            processor->cache_ce = ceptr;
193            processor->cache_class_num = i + 1;
194            return i + 1;
195        }
196    }
197    assert(0);
198    return (zend_ulong) -1;
199}
200/* }}} */
201/* {{{ xc_get_class */
202#ifdef ZEND_ENGINE_2
203static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
204    /* must be parent or currrent class */
205    assert(class_num <= processor->active_class_num);
206    return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest);
207}
208#endif
209/* }}} */
210#ifdef ZEND_ENGINE_2
211/* fix method on store */
212static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
213{
214    zend_function *zf = (zend_function *) dst;
215    zend_class_entry *ce = processor->active_class_entry_dst;
216    const zend_class_entry *srcce = processor->active_class_entry_src;
217
218    /* Fixing up the default functions for objects here since
219     * we need to compare with the newly allocated functions
220     *
221     * caveat: a sub-class method can have the same name as the
222     * parent~s constructor and create problems.
223     */
224
225    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
226        if (!ce->constructor) {
227            ce->constructor = zf;
228        }
229    }
230    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
231        ce->destructor = zf;
232    }
233    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
234        ce->clone = zf;
235    }
236    else {
237    pushdef(`SET_IF_SAME_NAMEs', `
238        SET_IF_SAME_NAME(__get);
239        SET_IF_SAME_NAME(__set);
240#ifdef ZEND_ENGINE_2_1
241        SET_IF_SAME_NAME(__unset);
242        SET_IF_SAME_NAME(__isset);
243#endif
244        SET_IF_SAME_NAME(__call);
245#ifdef ZEND_CALLSTATIC_FUNC_NAME
246        SET_IF_SAME_NAME(__callstatic);
247#endif
248#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
249        SET_IF_SAME_NAME(__tostring);
250#endif
251    ')
252#ifdef IS_UNICODE
253        if (UG(unicode)) {
254#define SET_IF_SAME_NAME(member) \
255            do { \
256                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
257                    ce->member = zf; \
258                } \
259            } \
260            while(0)
261
262            SET_IF_SAME_NAMEs()
263#undef SET_IF_SAME_NAME
264        }
265        else
266#endif
267        do {
268#define SET_IF_SAME_NAME(member) \
269            do { \
270                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
271                    ce->member = zf; \
272                } \
273            } \
274            while(0)
275
276            SET_IF_SAME_NAMEs()
277#undef SET_IF_SAME_NAME
278        } while (0);
279
280    popdef(`SET_IF_SAME_NAMEs')
281
282    }
283}
284/* }}} */
285#endif
286/* {{{ call op_array ctor handler */
287extern zend_bool xc_have_op_array_ctor;
288static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
289{
290    if (extension->op_array_ctor) {
291        extension->op_array_ctor(op_array);
292    }
293}
294/* }}} */
295dnl ================ export API
296/* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */
297xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) {
298    xc_entry_t *dst;
299    xc_processor_t processor;
300
301    memset(&processor, 0, sizeof(processor));
302    processor.reference = 1;
303
304    IFASSERT(`xc_stack_init(&processor.allocsizes);')
305
306    /* calc size */ {
307        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
308        if (processor.reference) {
309            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
310        }
311
312        processor.size = 0;
313        /* allocate */
314        processor.size = ALIGN(processor.size + sizeof(src[0]));
315
316        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
317        if (processor.reference) {
318            zend_hash_destroy(&processor.zvalptrs);
319        }
320        zend_hash_destroy(&processor.strings);
321    }
322    src->size = processor.size;
323    src->have_references = processor.have_references;
324
325    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
326    /* store {{{ */
327    {
328        IFASSERT(`char *oldp;')
329        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
330        if (processor.reference) {
331            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
332        }
333
334        /* mem :) */
335        processor.p = (char *) src->cache->mem->handlers->malloc(src->cache->mem, processor.size);
336        if (processor.p == NULL) {
337            dst = NULL;
338            goto err_alloc;
339        }
340        IFASSERT(`oldp = processor.p;')
341        assert(processor.p == (char *) ALIGN(processor.p));
342
343        /* allocate */
344        dst = (xc_entry_t *) processor.p;
345        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
346
347        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
348        IFASSERT(` {
349            int real = processor.p - oldp;
350            int should = processor.size;
351            if (real != processor.size) {
352                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
353                abort();
354            }
355        }')
356err_alloc:
357        if (processor.reference) {
358            zend_hash_destroy(&processor.zvalptrs);
359        }
360        zend_hash_destroy(&processor.strings);
361    }
362    /* }}} */
363
364    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
365
366    return dst;
367}
368/* }}} */
369/* 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 {{{ */
370xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
371    xc_processor_t processor;
372
373    memset(&processor, 0, sizeof(processor));
374    processor.readonly_protection = readonly_protection;
375    if (src->have_references) {
376        processor.reference = 1;
377    }
378
379    if (processor.reference) {
380        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
381    }
382    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
383    if (processor.reference) {
384        zend_hash_destroy(&processor.zvalptrs);
385    }
386    return dst;
387}
388/* }}} */
389/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC); :export {{{ */
390zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC) {
391    xc_processor_t processor;
392
393    memset(&processor, 0, sizeof(processor));
394    processor.reference = have_references;
395
396    if (processor.reference) {
397        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
398        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
399        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
400    }
401    xc_restore_zval(&processor, dst, src TSRMLS_CC);
402    if (processor.reference) {
403        zend_hash_destroy(&processor.zvalptrs);
404    }
405
406    return dst;
407}
408/* }}} */
409/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
410#ifdef HAVE_XCACHE_DPRINT
411void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
412    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
413    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
414}
415#endif
416/* }}} */
Note: See TracBrowser for help on using the repository browser.