source: trunk/processor/head.m4 @ 779

Last change on this file since 779 was 779, checked in by moo, 3 years ago

cacher: fix segv if oplineinfo is used for __FILE __DIR

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