source: trunk/processor/head.m4 @ 295

Last change on this file since 295 was 295, checked in by moo, 7 years ago

remove warnings

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