source: trunk/processor/head.m4 @ 1135

Last change on this file since 1135 was 1135, checked in by moo, 21 months ago

refactor: s/mem/allocator/

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