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

Last change on this file since 691 was 691, checked in by moo, 4 years ago

merge r690 from trunk

  • Property svn:eol-style set to native
File size: 11.2 KB
RevLine 
[1]1dnl {{{ === program start ========================================
2divert(0)
3#include <string.h>
4#include <stdio.h>
5
6#include "php.h"
[189]7#include "zend_extensions.h"
[1]8#include "zend_compile.h"
9#include "zend_API.h"
[4]10#include "zend_ini.h"
[1]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
[58]19#if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT
20extern unsigned int zend_hash_canary;
21#endif
22
[1]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>
[29]37m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
38', `
39m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
[1]40')
[300]41ifdef(`DEBUG_SIZE', `static int xc_totalsize = 0;')
42
[29]43sinclude(builddir`/structinfo.m4')
44
[1]45#ifndef NDEBUG
46#   undef inline
47#define inline
48#endif
49
50typedef zval *zval_ptr;
51typedef zend_uchar zval_data_type;
[103]52#ifdef IS_UNICODE
53typedef UChar zstr_uchar;
54#endif
55typedef char  zstr_char;
[1]56
57#define MAX_DUP_STR_LEN 256
58dnl }}}
[178]59/* export: typedef struct _xc_processor_t xc_processor_t; :export {{{ */
60struct _xc_processor_t {
[1]61    char *p;
62    zend_uint size;
63    HashTable strings;
64    HashTable zvalptrs;
65    zend_bool reference; /* enable if to deal with reference */
[233]66    zend_bool have_references;
[1]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/* }}} */
[230]82#ifdef HAVE_XCACHE_DPRINT
[1]83static void xc_dprint_indent(int indent) /* {{{ */
84{
85    int i;
86    for (i = 0; i < indent; i ++) {
87        fprintf(stderr, "  ");
88    }
89}
[230]90/* }}} */
91static void xc_dprint_str_len(const char *str, int len) /* {{{ */
92{
[295]93    const unsigned char *p = (const unsigned char *) str;
[230]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/* }}} */
[1]105#endif
[103]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{
[622]116    return u_strlen(ZSTR_U(s));
[103]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
[1]130/* {{{ xc_calc_string_n */
131REDEF(`KIND', `calc')
[691]132#undef C_RELAYLINE
133#define C_RELAYLINE
134IFASSERT(`
135#undef C_RELAYLINE
136#define C_RELAYLINE , __LINE__
137')
[646]138static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const zstr str, long size IFASSERT(`, int relayline')) {
[1]139    pushdef(`__LINE__', `relayline')
140    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]141    long dummy = 1;
[1]142
143    if (realsize > MAX_DUP_STR_LEN) {
144        ALLOC(, char, realsize)
145    }
[103]146    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
[1]147        /* new string */
148        ALLOC(, char, realsize)
149    }
150    IFASSERT(`
151        else {
[103]152            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
[1]153        }
154    ')
155    popdef(`__LINE__')
156}
157/* }}} */
158/* {{{ xc_store_string_n */
159REDEF(`KIND', `store')
[646]160static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const zstr str, long size IFASSERT(`, int relayline')) {
[1]161    pushdef(`__LINE__', `relayline')
162    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
[103]163    zstr ret, *pret;
[1]164
165    if (realsize > MAX_DUP_STR_LEN) {
[103]166        ALLOC(ZSTR_V(ret), char, realsize)
167        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
168        return ret;
[1]169    }
[103]170
171    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
172        return *pret;
[1]173    }
[103]174
175    /* new string */
176    ALLOC(ZSTR_V(ret), char, realsize)
177    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
178    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
179    return ret;
180
[1]181    popdef(`__LINE__')
182}
183/* }}} */
184/* {{{ xc_get_class_num
185 * return class_index + 1
186 */
[178]187static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
[90]188    zend_ulong i;
[1]189    const xc_entry_t *xce = processor->xce_src;
190    zend_class_entry *ceptr;
191
192    if (processor->cache_ce == ce) {
193        return processor->cache_class_num;
194    }
195    for (i = 0; i < xce->data.php->classinfo_cnt; i ++) {
196        ceptr = CestToCePtr(xce->data.php->classinfos[i].cest);
197        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
198            processor->cache_ce = ceptr;
199            processor->cache_class_num = i + 1;
200            return i + 1;
201        }
202    }
203    assert(0);
[90]204    return (zend_ulong) -1;
[1]205}
206/* }}} */
207/* {{{ xc_get_class */
208#ifdef ZEND_ENGINE_2
[178]209static zend_class_entry *xc_get_class(xc_processor_t *processor, zend_ulong class_num) {
[1]210    /* must be parent or currrent class */
211    assert(class_num <= processor->active_class_num);
212    return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest);
213}
214#endif
215/* }}} */
216#ifdef ZEND_ENGINE_2
217/* fix method on store */
[622]218static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
[1]219{
220    zend_function *zf = (zend_function *) dst;
221    zend_class_entry *ce = processor->active_class_entry_dst;
[622]222    const zend_class_entry *srcce = processor->active_class_entry_src;
[1]223
224    /* Fixing up the default functions for objects here since
225     * we need to compare with the newly allocated functions
226     *
227     * caveat: a sub-class method can have the same name as the
228     * parent~s constructor and create problems.
229     */
230
231    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
232        if (!ce->constructor) {
233            ce->constructor = zf;
234        }
235    }
236    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
237        ce->destructor = zf;
238    }
239    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
240        ce->clone = zf;
241    }
242    else {
[622]243    pushdef(`SET_IF_SAME_NAMEs', `
[1]244        SET_IF_SAME_NAME(__get);
245        SET_IF_SAME_NAME(__set);
[56]246#ifdef ZEND_ENGINE_2_1
[1]247        SET_IF_SAME_NAME(__unset);
248        SET_IF_SAME_NAME(__isset);
[56]249#endif
[1]250        SET_IF_SAME_NAME(__call);
[481]251#ifdef ZEND_CALLSTATIC_FUNC_NAME
252        SET_IF_SAME_NAME(__callstatic);
253#endif
[94]254#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
[1]255        SET_IF_SAME_NAME(__tostring);
256#endif
[622]257    ')
258#ifdef IS_UNICODE
259        if (UG(unicode)) {
260#define SET_IF_SAME_NAME(member) \
261            do { \
262                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
263                    ce->member = zf; \
264                } \
265            } \
266            while(0)
[1]267
[622]268            SET_IF_SAME_NAMEs()
[1]269#undef SET_IF_SAME_NAME
[622]270        }
271        else
272#endif
273        do {
274#define SET_IF_SAME_NAME(member) \
275            do { \
276                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
277                    ce->member = zf; \
278                } \
279            } \
280            while(0)
281
282            SET_IF_SAME_NAMEs()
283#undef SET_IF_SAME_NAME
284        } while (0);
285
286    popdef(`SET_IF_SAME_NAMEs')
287
[1]288    }
289}
290/* }}} */
[199]291#endif
[195]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/* }}} */
[1]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;
[178]305    xc_processor_t processor;
[1]306
307    memset(&processor, 0, sizeof(processor));
[233]308    processor.reference = 1;
[1]309
310    IFASSERT(`xc_stack_init(&processor.allocsizes);')
311
312    /* calc size */ {
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        processor.size = 0;
319        /* allocate */
320        processor.size = ALIGN(processor.size + sizeof(src[0]));
321
322        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
323        if (processor.reference) {
324            zend_hash_destroy(&processor.zvalptrs);
325        }
326        zend_hash_destroy(&processor.strings);
327    }
328    src->size = processor.size;
[233]329    src->have_references = processor.have_references;
[1]330
331    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
332    /* store {{{ */
333    {
334        IFASSERT(`char *oldp;')
335        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
336        if (processor.reference) {
337            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
338        }
339
340        /* mem :) */
[148]341        processor.p = (char *) src->cache->mem->handlers->malloc(src->cache->mem, processor.size);
[1]342        if (processor.p == NULL) {
343            dst = NULL;
344            goto err_alloc;
345        }
346        IFASSERT(`oldp = processor.p;')
347        assert(processor.p == (char *) ALIGN(processor.p));
348
349        /* allocate */
350        dst = (xc_entry_t *) processor.p;
351        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
352
353        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
354        IFASSERT(` {
355            int real = processor.p - oldp;
356            int should = processor.size;
357            if (real != processor.size) {
358                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
359                abort();
360            }
361        }')
362err_alloc:
363        if (processor.reference) {
364            zend_hash_destroy(&processor.zvalptrs);
365        }
366        zend_hash_destroy(&processor.strings);
367    }
368    /* }}} */
369
370    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
371
372    return dst;
373}
374/* }}} */
375/* 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 {{{ */
376xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
[178]377    xc_processor_t processor;
[1]378
379    memset(&processor, 0, sizeof(processor));
380    processor.readonly_protection = readonly_protection;
[233]381    if (src->have_references) {
382        processor.reference = 1;
383    }
[1]384
[233]385    if (processor.reference) {
386        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
387    }
[1]388    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
[233]389    if (processor.reference) {
390        zend_hash_destroy(&processor.zvalptrs);
391    }
[1]392    return dst;
393}
394/* }}} */
[233]395/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC); :export {{{ */
396zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC) {
[178]397    xc_processor_t processor;
[1]398
399    memset(&processor, 0, sizeof(processor));
[233]400    processor.reference = have_references;
[1]401
[233]402    if (processor.reference) {
403        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
404        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
405        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
406    }
[1]407    xc_restore_zval(&processor, dst, src TSRMLS_CC);
[233]408    if (processor.reference) {
409        zend_hash_destroy(&processor.zvalptrs);
410    }
[1]411
412    return dst;
413}
414/* }}} */
415/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
[230]416#ifdef HAVE_XCACHE_DPRINT
[1]417void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
418    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
419    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
420}
421#endif
422/* }}} */
Note: See TracBrowser for help on using the repository browser.