source: trunk/processor/head.m4 @ 662

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

back patch __FILE and __DIR on cache restore

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