source: trunk/processor/head.m4 @ 825

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

PHP_5_4: improve handling traits yet still need review. values of default_properties_table is nullable

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