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