| 1 | dnl {{{ === program start ======================================== |
|---|
| 2 | divert(0) |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | |
|---|
| 6 | #include "php.h" |
|---|
| 7 | #include "zend_compile.h" |
|---|
| 8 | #include "zend_API.h" |
|---|
| 9 | #include "zend_ini.h" |
|---|
| 10 | |
|---|
| 11 | #include "xcache.h" |
|---|
| 12 | #include "align.h" |
|---|
| 13 | #include "const_string.h" |
|---|
| 14 | #include "processor.h" |
|---|
| 15 | #include "stack.h" |
|---|
| 16 | #include "xcache_globals.h" |
|---|
| 17 | |
|---|
| 18 | #if defined(HARDENING_PATCH_HASH_PROTECT) && HARDENING_PATCH_HASH_PROTECT |
|---|
| 19 | extern unsigned int zend_hash_canary; |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | define(`SIZEOF_zend_uint', `sizeof(zend_uint)') |
|---|
| 23 | define(`COUNTOF_zend_uint', `1') |
|---|
| 24 | define(`SIZEOF_int', `sizeof(int)') |
|---|
| 25 | define(`COUNTOF_int', `1') |
|---|
| 26 | define(`SIZEOF_zend_function', `sizeof(zend_function)') |
|---|
| 27 | define(`COUNTOF_zend_function', `1') |
|---|
| 28 | define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)') |
|---|
| 29 | define(`COUNTOF_zval_ptr', `1') |
|---|
| 30 | define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)') |
|---|
| 31 | define(`COUNTOF_xc_entry_name_t', `1') |
|---|
| 32 | |
|---|
| 33 | ifdef(`XCACHE_ENABLE_TEST', ` |
|---|
| 34 | #undef NDEBUG |
|---|
| 35 | #include <assert.h> |
|---|
| 36 | m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)') |
|---|
| 37 | ', ` |
|---|
| 38 | m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)') |
|---|
| 39 | ') |
|---|
| 40 | sinclude(builddir`/structinfo.m4') |
|---|
| 41 | |
|---|
| 42 | #ifndef NDEBUG |
|---|
| 43 | # undef inline |
|---|
| 44 | #define inline |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | typedef zval *zval_ptr; |
|---|
| 48 | typedef zend_uchar zval_data_type; |
|---|
| 49 | |
|---|
| 50 | #define MAX_DUP_STR_LEN 256 |
|---|
| 51 | dnl }}} |
|---|
| 52 | /* export: typedef struct _processor_t processor_t; :export {{{ */ |
|---|
| 53 | struct _processor_t { |
|---|
| 54 | char *p; |
|---|
| 55 | zend_uint size; |
|---|
| 56 | HashTable strings; |
|---|
| 57 | HashTable zvalptrs; |
|---|
| 58 | zend_bool reference; /* enable if to deal with reference */ |
|---|
| 59 | const xc_entry_t *xce_src; |
|---|
| 60 | const xc_entry_t *xce_dst; |
|---|
| 61 | const zend_class_entry *cache_ce; |
|---|
| 62 | zend_uint cache_class_num; |
|---|
| 63 | |
|---|
| 64 | const zend_op *active_opcodes_src; |
|---|
| 65 | zend_op *active_opcodes_dst; |
|---|
| 66 | const zend_class_entry *active_class_entry_src; |
|---|
| 67 | zend_class_entry *active_class_entry_dst; |
|---|
| 68 | zend_uint active_class_num; |
|---|
| 69 | |
|---|
| 70 | zend_bool readonly_protection; /* wheather it's present */ |
|---|
| 71 | IFASSERT(xc_stack_t allocsizes;) |
|---|
| 72 | }; |
|---|
| 73 | /* }}} */ |
|---|
| 74 | #ifdef XCACHE_HAVE_DPRINT |
|---|
| 75 | static void xc_dprint_indent(int indent) /* {{{ */ |
|---|
| 76 | { |
|---|
| 77 | int i; |
|---|
| 78 | for (i = 0; i < indent; i ++) { |
|---|
| 79 | fprintf(stderr, " "); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | #endif |
|---|
| 83 | /* }}} */ |
|---|
| 84 | /* {{{ xc_calc_string_n */ |
|---|
| 85 | REDEF(`KIND', `calc') |
|---|
| 86 | static inline void xc_calc_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) { |
|---|
| 87 | pushdef(`__LINE__', `relayline') |
|---|
| 88 | int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size); |
|---|
| 89 | |
|---|
| 90 | if (realsize > MAX_DUP_STR_LEN) { |
|---|
| 91 | ALLOC(, char, realsize) |
|---|
| 92 | } |
|---|
| 93 | else if (zend_u_hash_add(&processor->strings, type, str, size, (void*)&str, sizeof(char*), NULL) == SUCCESS) { |
|---|
| 94 | /* new string */ |
|---|
| 95 | ALLOC(, char, realsize) |
|---|
| 96 | } |
|---|
| 97 | IFASSERT(` |
|---|
| 98 | else { |
|---|
| 99 | dnl fprintf(stderr, "dupstr %s\n", str); |
|---|
| 100 | } |
|---|
| 101 | ') |
|---|
| 102 | popdef(`__LINE__') |
|---|
| 103 | } |
|---|
| 104 | /* }}} */ |
|---|
| 105 | /* {{{ xc_store_string_n */ |
|---|
| 106 | REDEF(`KIND', `store') |
|---|
| 107 | static inline char *xc_store_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) { |
|---|
| 108 | pushdef(`__LINE__', `relayline') |
|---|
| 109 | int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size); |
|---|
| 110 | char *s; |
|---|
| 111 | |
|---|
| 112 | if (realsize > MAX_DUP_STR_LEN) { |
|---|
| 113 | ALLOC(s, char, realsize) |
|---|
| 114 | memcpy(s, str, realsize); |
|---|
| 115 | } |
|---|
| 116 | else if (zend_u_hash_find(&processor->strings, type, str, size, (void*)&s) != SUCCESS) { |
|---|
| 117 | /* new string */ |
|---|
| 118 | ALLOC(s, char, realsize) |
|---|
| 119 | memcpy(s, str, realsize); |
|---|
| 120 | zend_u_hash_add(&processor->strings, type, str, size, (void*)&s, sizeof(char*), NULL); |
|---|
| 121 | } |
|---|
| 122 | else { |
|---|
| 123 | s = *(char**)s; |
|---|
| 124 | } |
|---|
| 125 | return s; |
|---|
| 126 | popdef(`__LINE__') |
|---|
| 127 | } |
|---|
| 128 | /* }}} */ |
|---|
| 129 | /* {{{ xc_get_class_num |
|---|
| 130 | * return class_index + 1 |
|---|
| 131 | */ |
|---|
| 132 | static zend_uint xc_get_class_num(processor_t *processor, zend_class_entry *ce) { |
|---|
| 133 | zend_uint i; |
|---|
| 134 | const xc_entry_t *xce = processor->xce_src; |
|---|
| 135 | zend_class_entry *ceptr; |
|---|
| 136 | |
|---|
| 137 | if (processor->cache_ce == ce) { |
|---|
| 138 | return processor->cache_class_num; |
|---|
| 139 | } |
|---|
| 140 | for (i = 0; i < xce->data.php->classinfo_cnt; i ++) { |
|---|
| 141 | ceptr = CestToCePtr(xce->data.php->classinfos[i].cest); |
|---|
| 142 | if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) { |
|---|
| 143 | processor->cache_ce = ceptr; |
|---|
| 144 | processor->cache_class_num = i + 1; |
|---|
| 145 | return i + 1; |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | assert(0); |
|---|
| 149 | return (zend_uint) -1; |
|---|
| 150 | } |
|---|
| 151 | /* }}} */ |
|---|
| 152 | /* {{{ xc_get_class */ |
|---|
| 153 | #ifdef ZEND_ENGINE_2 |
|---|
| 154 | static zend_class_entry *xc_get_class(processor_t *processor, zend_uint class_num) { |
|---|
| 155 | /* must be parent or currrent class */ |
|---|
| 156 | assert(class_num <= processor->active_class_num); |
|---|
| 157 | return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest); |
|---|
| 158 | } |
|---|
| 159 | #endif |
|---|
| 160 | /* }}} */ |
|---|
| 161 | #ifdef ZEND_ENGINE_2 |
|---|
| 162 | /* fix method on store */ |
|---|
| 163 | static void xc_fix_method(processor_t *processor, zend_op_array *dst) /* {{{ */ |
|---|
| 164 | { |
|---|
| 165 | zend_function *zf = (zend_function *) dst; |
|---|
| 166 | zend_class_entry *ce = processor->active_class_entry_dst; |
|---|
| 167 | |
|---|
| 168 | /* Fixing up the default functions for objects here since |
|---|
| 169 | * we need to compare with the newly allocated functions |
|---|
| 170 | * |
|---|
| 171 | * caveat: a sub-class method can have the same name as the |
|---|
| 172 | * parent~s constructor and create problems. |
|---|
| 173 | */ |
|---|
| 174 | |
|---|
| 175 | if (zf->common.fn_flags & ZEND_ACC_CTOR) { |
|---|
| 176 | if (!ce->constructor) { |
|---|
| 177 | ce->constructor = zf; |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | else if (zf->common.fn_flags & ZEND_ACC_DTOR) { |
|---|
| 181 | ce->destructor = zf; |
|---|
| 182 | } |
|---|
| 183 | else if (zf->common.fn_flags & ZEND_ACC_CLONE) { |
|---|
| 184 | ce->clone = zf; |
|---|
| 185 | } |
|---|
| 186 | else { |
|---|
| 187 | #define SET_IF_SAME_NAME(member) \ |
|---|
| 188 | do { \ |
|---|
| 189 | if(!strcasecmp(zf->common.function_name, #member)) { \ |
|---|
| 190 | ce->member = zf; \ |
|---|
| 191 | } \ |
|---|
| 192 | } \ |
|---|
| 193 | while(0) |
|---|
| 194 | /* if(ce->member && !strcmp(zf->common.function_name, ce->member->common.function_name)) { \ */ |
|---|
| 195 | |
|---|
| 196 | SET_IF_SAME_NAME(__get); |
|---|
| 197 | SET_IF_SAME_NAME(__set); |
|---|
| 198 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 199 | SET_IF_SAME_NAME(__unset); |
|---|
| 200 | SET_IF_SAME_NAME(__isset); |
|---|
| 201 | #endif |
|---|
| 202 | SET_IF_SAME_NAME(__call); |
|---|
| 203 | #if defined(ZEND_ENGINE_2_2) || PHP_MAJOR_VERSION >= 6 |
|---|
| 204 | SET_IF_SAME_NAME(__tostring); |
|---|
| 205 | #endif |
|---|
| 206 | |
|---|
| 207 | #undef SET_IF_SAME_NAME |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | /* }}} */ |
|---|
| 211 | #endif |
|---|
| 212 | dnl ================ export API |
|---|
| 213 | /* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */ |
|---|
| 214 | xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) { |
|---|
| 215 | xc_entry_t *dst; |
|---|
| 216 | processor_t processor; |
|---|
| 217 | |
|---|
| 218 | memset(&processor, 0, sizeof(processor)); |
|---|
| 219 | if (src->type == XC_TYPE_VAR) { |
|---|
| 220 | processor.reference = 1; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | IFASSERT(`xc_stack_init(&processor.allocsizes);') |
|---|
| 224 | |
|---|
| 225 | /* calc size */ { |
|---|
| 226 | zend_hash_init(&processor.strings, 0, NULL, NULL, 0); |
|---|
| 227 | if (processor.reference) { |
|---|
| 228 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | processor.size = 0; |
|---|
| 232 | /* allocate */ |
|---|
| 233 | processor.size = ALIGN(processor.size + sizeof(src[0])); |
|---|
| 234 | |
|---|
| 235 | xc_calc_xc_entry_t(&processor, src TSRMLS_CC); |
|---|
| 236 | if (processor.reference) { |
|---|
| 237 | zend_hash_destroy(&processor.zvalptrs); |
|---|
| 238 | } |
|---|
| 239 | zend_hash_destroy(&processor.strings); |
|---|
| 240 | } |
|---|
| 241 | src->size = processor.size; |
|---|
| 242 | |
|---|
| 243 | IFASSERT(`xc_stack_reverse(&processor.allocsizes);') |
|---|
| 244 | /* store {{{ */ |
|---|
| 245 | { |
|---|
| 246 | IFASSERT(`char *oldp;') |
|---|
| 247 | zend_hash_init(&processor.strings, 0, NULL, NULL, 0); |
|---|
| 248 | if (processor.reference) { |
|---|
| 249 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /* mem :) */ |
|---|
| 253 | processor.p = (char *)xc_mem_malloc(src->cache->mem, processor.size); |
|---|
| 254 | if (processor.p == NULL) { |
|---|
| 255 | dst = NULL; |
|---|
| 256 | goto err_alloc; |
|---|
| 257 | return NULL; |
|---|
| 258 | } |
|---|
| 259 | IFASSERT(`oldp = processor.p;') |
|---|
| 260 | assert(processor.p == (char *) ALIGN(processor.p)); |
|---|
| 261 | |
|---|
| 262 | /* allocate */ |
|---|
| 263 | dst = (xc_entry_t *) processor.p; |
|---|
| 264 | processor.p = (char *) ALIGN(processor.p + sizeof(dst[0])); |
|---|
| 265 | |
|---|
| 266 | xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC); |
|---|
| 267 | IFASSERT(` { |
|---|
| 268 | int real = processor.p - oldp; |
|---|
| 269 | int should = processor.size; |
|---|
| 270 | if (real != processor.size) { |
|---|
| 271 | fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should); |
|---|
| 272 | abort(); |
|---|
| 273 | } |
|---|
| 274 | }') |
|---|
| 275 | err_alloc: |
|---|
| 276 | if (processor.reference) { |
|---|
| 277 | zend_hash_destroy(&processor.zvalptrs); |
|---|
| 278 | } |
|---|
| 279 | zend_hash_destroy(&processor.strings); |
|---|
| 280 | } |
|---|
| 281 | /* }}} */ |
|---|
| 282 | |
|---|
| 283 | IFASSERT(`xc_stack_destroy(&processor.allocsizes);') |
|---|
| 284 | |
|---|
| 285 | return dst; |
|---|
| 286 | } |
|---|
| 287 | /* }}} */ |
|---|
| 288 | /* 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 {{{ */ |
|---|
| 289 | xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) { |
|---|
| 290 | processor_t processor; |
|---|
| 291 | |
|---|
| 292 | memset(&processor, 0, sizeof(processor)); |
|---|
| 293 | processor.readonly_protection = readonly_protection; |
|---|
| 294 | |
|---|
| 295 | xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC); |
|---|
| 296 | return dst; |
|---|
| 297 | } |
|---|
| 298 | /* }}} */ |
|---|
| 299 | /* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */ |
|---|
| 300 | zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) { |
|---|
| 301 | processor_t processor; |
|---|
| 302 | |
|---|
| 303 | memset(&processor, 0, sizeof(processor)); |
|---|
| 304 | processor.reference = 1; |
|---|
| 305 | |
|---|
| 306 | zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0); |
|---|
| 307 | dnl fprintf(stderr, "mark[%p] = %p\n", src, dst); |
|---|
| 308 | zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL); |
|---|
| 309 | xc_restore_zval(&processor, dst, src TSRMLS_CC); |
|---|
| 310 | zend_hash_destroy(&processor.zvalptrs); |
|---|
| 311 | |
|---|
| 312 | return dst; |
|---|
| 313 | } |
|---|
| 314 | /* }}} */ |
|---|
| 315 | /* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */ |
|---|
| 316 | #ifdef XCACHE_HAVE_DPRINT |
|---|
| 317 | void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) { |
|---|
| 318 | IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");') |
|---|
| 319 | xc_dprint_xc_entry_t(src, indent TSRMLS_CC); |
|---|
| 320 | } |
|---|
| 321 | #endif |
|---|
| 322 | /* }}} */ |
|---|