source: trunk/processor/head.m4 @ 684

Last change on this file since 684 was 684, checked in by moo, 5 years ago

the logic of implemenation of dir/file path fixing was wrong. file/dir path should be cached before hardlink

  • 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 *entry_src;
68    const xc_entry_t *entry_dst;
69    const xc_entry_data_php_t *php_src;
70    const xc_entry_data_php_t *php_dst;
71    const xc_cache_t          *cache;
72    const zend_class_entry *cache_ce;
73    zend_uint cache_class_index;
74
75    const zend_op          *active_opcodes_src;
76    zend_op                *active_opcodes_dst;
77    const zend_class_entry *active_class_entry_src;
78    zend_class_entry       *active_class_entry_dst;
79    zend_uint                 active_class_index;
80    zend_uint                 active_op_array_index;
81    const xc_op_array_info_t *active_op_array_infos_src;
82
83    zend_bool readonly_protection; /* wheather it's present */
84IFASSERT(xc_stack_t allocsizes;)
85};
86/* }}} */
87#ifdef HAVE_XCACHE_DPRINT
88static void xc_dprint_indent(int indent) /* {{{ */
89{
90    int i;
91    for (i = 0; i < indent; i ++) {
92        fprintf(stderr, "  ");
93    }
94}
95/* }}} */
96static void xc_dprint_str_len(const char *str, int len) /* {{{ */
97{
98    const unsigned char *p = (const unsigned char *) str;
99    int i;
100    for (i = 0; i < len; i ++) {
101        if (p[i] < 32 || p[i] == 127) {
102            fprintf(stderr, "\\%03o", (unsigned int) p[i]);
103        }
104        else {
105            fputc(p[i], stderr);
106        }
107    }
108}
109/* }}} */
110#endif
111/* {{{ xc_zstrlen_char */
112static inline int xc_zstrlen_char(zstr s)
113{
114    return strlen(ZSTR_S(s));
115}
116/* }}} */
117#ifdef IS_UNICODE
118/* {{{ xc_zstrlen_uchar */
119static inline int xc_zstrlen_uchar(zstr s)
120{
121    return u_strlen(ZSTR_U(s));
122}
123/* }}} */
124/* {{{ xc_zstrlen */
125static inline int xc_zstrlen(int type, zstr s)
126{
127    return type == IS_UNICODE ? xc_zstrlen_uchar(s) : xc_zstrlen_char(s);
128}
129/* }}} */
130#else
131/* {{{ xc_zstrlen */
132#define xc_zstrlen(dummy, s) xc_zstrlen_char(s)
133/* }}} */
134#endif
135/* {{{ xc_calc_string_n */
136REDEF(`KIND', `calc')
137static inline void xc_calc_string_n(xc_processor_t *processor, zend_uchar type, const zstr str, long size IFASSERT(`, int relayline')) {
138    pushdef(`__LINE__', `relayline')
139    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
140    long dummy = 1;
141
142    if (realsize > MAX_DUP_STR_LEN) {
143        ALLOC(, char, realsize)
144    }
145    else if (zend_u_hash_add(&processor->strings, type, str, size, (void *) &dummy, sizeof(dummy), NULL) == SUCCESS) {
146        /* new string */
147        ALLOC(, char, realsize)
148    }
149    IFASSERT(`
150        else {
151            dnl fprintf(stderr, "dupstr %s\n", ZSTR_S(str));
152        }
153    ')
154    popdef(`__LINE__')
155}
156/* }}} */
157/* {{{ xc_store_string_n */
158REDEF(`KIND', `store')
159static inline zstr xc_store_string_n(xc_processor_t *processor, zend_uchar type, const zstr str, long size IFASSERT(`, int relayline')) {
160    pushdef(`__LINE__', `relayline')
161    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
162    zstr ret, *pret;
163
164    if (realsize > MAX_DUP_STR_LEN) {
165        ALLOC(ZSTR_V(ret), char, realsize)
166        memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
167        return ret;
168    }
169
170    if (zend_u_hash_find(&processor->strings, type, str, size, (void **) &pret) == SUCCESS) {
171        return *pret;
172    }
173
174    /* new string */
175    ALLOC(ZSTR_V(ret), char, realsize)
176    memcpy(ZSTR_V(ret), ZSTR_V(str), realsize);
177    zend_u_hash_add(&processor->strings, type, str, size, (void *) &ret, sizeof(zstr), NULL);
178    return ret;
179
180    popdef(`__LINE__')
181}
182/* }}} */
183/* {{{ xc_get_class_num
184 * return class_index + 1
185 */
186static zend_ulong xc_get_class_num(xc_processor_t *processor, zend_class_entry *ce) {
187    zend_ulong i;
188    const xc_entry_data_php_t *php = processor->php_src;
189    zend_class_entry *ceptr;
190
191    if (processor->cache_ce == ce) {
192        return processor->cache_class_index + 1;
193    }
194    for (i = 0; i < php->classinfo_cnt; i ++) {
195        ceptr = CestToCePtr(php->classinfos[i].cest);
196        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
197            processor->cache_ce = ceptr;
198            processor->cache_class_index = i;
199            return i + 1;
200        }
201    }
202    assert(0);
203    return (zend_ulong) -1;
204}
205define(`xc_get_class_num', `xc_get_class_numNOTDEFINED')
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_index + 1);
212    return CestToCePtr(processor->php_dst->classinfos[class_num - 1].cest);
213}
214#endif
215define(`xc_get_class', `xc_get_classNOTDEFINED')
216/* }}} */
217#ifdef ZEND_ENGINE_2
218/* fix method on store */
219static void xc_fix_method(xc_processor_t *processor, zend_op_array *dst TSRMLS_DC) /* {{{ */
220{
221    zend_function *zf = (zend_function *) dst;
222    zend_class_entry *ce = processor->active_class_entry_dst;
223    const zend_class_entry *srcce = processor->active_class_entry_src;
224
225    /* Fixing up the default functions for objects here since
226     * we need to compare with the newly allocated functions
227     *
228     * caveat: a sub-class method can have the same name as the
229     * parent~s constructor and create problems.
230     */
231
232    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
233        if (!ce->constructor) {
234            ce->constructor = zf;
235        }
236    }
237    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
238        ce->destructor = zf;
239    }
240    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
241        ce->clone = zf;
242    }
243    else {
244    pushdef(`SET_IF_SAME_NAMEs', `
245        SET_IF_SAME_NAME(__get);
246        SET_IF_SAME_NAME(__set);
247#ifdef ZEND_ENGINE_2_1
248        SET_IF_SAME_NAME(__unset);
249        SET_IF_SAME_NAME(__isset);
250#endif
251        SET_IF_SAME_NAME(__call);
252#ifdef ZEND_CALLSTATIC_FUNC_NAME
253        SET_IF_SAME_NAME(__callstatic);
254#endif
255#if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6
256        SET_IF_SAME_NAME(__tostring);
257#endif
258    ')
259#ifdef IS_UNICODE
260        if (UG(unicode)) {
261#define SET_IF_SAME_NAME(member) \
262            do { \
263                if (srcce->member && u_strcmp(ZSTR_U(zf->common.function_name), ZSTR_U(srcce->member->common.function_name)) == 0) { \
264                    ce->member = zf; \
265                } \
266            } \
267            while(0)
268
269            SET_IF_SAME_NAMEs()
270#undef SET_IF_SAME_NAME
271        }
272        else
273#endif
274        do {
275#define SET_IF_SAME_NAME(member) \
276            do { \
277                if (srcce->member && strcmp(ZSTR_S(zf->common.function_name), ZSTR_S(srcce->member->common.function_name)) == 0) { \
278                    ce->member = zf; \
279                } \
280            } \
281            while(0)
282
283            SET_IF_SAME_NAMEs()
284#undef SET_IF_SAME_NAME
285        } while (0);
286
287    popdef(`SET_IF_SAME_NAMEs')
288
289    }
290}
291/* }}} */
292#endif
293/* {{{ call op_array ctor handler */
294extern zend_bool xc_have_op_array_ctor;
295static void xc_zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
296{
297    if (extension->op_array_ctor) {
298        extension->op_array_ctor(op_array);
299    }
300}
301/* }}} */
302dnl ================ export API
303define(`DEFINE_STORE_API', `
304/* export: $1 *xc_processor_store_$1($1 *src TSRMLS_DC); :export {{{ */
305$1 *xc_processor_store_$1($1 *src TSRMLS_DC) {
306    $1 *dst;
307    xc_processor_t processor;
308
309    memset(&processor, 0, sizeof(processor));
310    processor.reference = 1;
311    processor.cache = src->cache;
312
313    IFASSERT(`xc_stack_init(&processor.allocsizes);')
314
315    /* calc size */ {
316        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
317        if (processor.reference) {
318            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
319        }
320
321        processor.size = 0;
322        /* allocate */
323        processor.size = ALIGN(processor.size + sizeof(src[0]));
324
325        xc_calc_$1(&processor, src TSRMLS_CC);
326        if (processor.reference) {
327            zend_hash_destroy(&processor.zvalptrs);
328        }
329        zend_hash_destroy(&processor.strings);
330    }
331    src->size = processor.size;
332    ifelse(`$1', `xc_entry_t', `
333        src->data.var->have_references = processor.have_references;
334    ', `
335        src->have_references = processor.have_references;
336    ')
337
338    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
339    /* store {{{ */
340    {
341        IFASSERT(`char *oldp;')
342        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
343        if (processor.reference) {
344            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
345        }
346
347        /* mem :) */
348        processor.p = (char *) processor.cache->mem->handlers->malloc(processor.cache->mem, processor.size);
349        if (processor.p == NULL) {
350            dst = NULL;
351            goto err_alloc;
352        }
353        IFASSERT(`oldp = processor.p;')
354        assert(processor.p == (char *) ALIGN(processor.p));
355
356        /* allocate */
357        dst = ($1 *) processor.p;
358        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
359
360        xc_store_$1(&processor, dst, src TSRMLS_CC);
361        IFASSERT(` {
362            int real = processor.p - oldp;
363            int should = processor.size;
364            if (real != processor.size) {
365                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
366                abort();
367            }
368        }')
369err_alloc:
370        if (processor.reference) {
371            zend_hash_destroy(&processor.zvalptrs);
372        }
373        zend_hash_destroy(&processor.strings);
374    }
375    /* }}} */
376
377    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
378
379    return dst;
380}
381/* }}} */
382')
383DEFINE_STORE_API(`xc_entry_t')
384DEFINE_STORE_API(`xc_entry_data_php_t')
385/* export: xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src TSRMLS_DC); :export {{{ */
386xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src TSRMLS_DC) {
387    xc_processor_t processor;
388
389    memset(&processor, 0, sizeof(processor));
390    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
391
392    return dst;
393}
394/* }}} */
395/* 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 {{{ */
396xc_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) {
397    xc_processor_t processor;
398
399    memset(&processor, 0, sizeof(processor));
400    processor.readonly_protection = readonly_protection;
401    /* this function is used for php data only */
402    if (src->have_references) {
403        processor.reference = 1;
404    }
405    processor.entry_src = xce;
406
407    if (processor.reference) {
408        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
409    }
410    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
411    if (processor.reference) {
412        zend_hash_destroy(&processor.zvalptrs);
413    }
414    return dst;
415}
416/* }}} */
417/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC); :export {{{ */
418zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC) {
419    xc_processor_t processor;
420
421    memset(&processor, 0, sizeof(processor));
422    processor.reference = have_references;
423
424    if (processor.reference) {
425        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
426        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
427        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
428    }
429    xc_restore_zval(&processor, dst, src TSRMLS_CC);
430    if (processor.reference) {
431        zend_hash_destroy(&processor.zvalptrs);
432    }
433
434    return dst;
435}
436/* }}} */
437/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
438#ifdef HAVE_XCACHE_DPRINT
439void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
440    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
441    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
442}
443#endif
444/* }}} */
Note: See TracBrowser for help on using the repository browser.