source: trunk/processor/head.m4 @ 512

Last change on this file since 512 was 512, checked in by moo, 6 years ago

use u_strlen instead of for loop

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