source: trunk/processor/head.m4 @ 838

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

avoid overflow in memsetptr

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