source: trunk/processor/head.m4 @ 846

Last change on this file since 846 was 846, checked in by moo, 2 years ago

kill most const warning

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