source: tags/1.3.2-rc1/processor/head.m4

Last change on this file was 773, checked in by moo, 4 years ago

tagged 1.3.2-rc1

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