1 | #if 0 |
---|
2 | #define XCACHE_DEBUG |
---|
3 | #endif |
---|
4 | |
---|
5 | #if 0 |
---|
6 | #define SHOW_DPRINT |
---|
7 | #endif |
---|
8 | |
---|
9 | /* {{{ macros */ |
---|
10 | #include "xc_cacher.h" |
---|
11 | #include "xc_cache.h" |
---|
12 | #include "xcache.h" |
---|
13 | #include "xc_processor.h" |
---|
14 | #include "xcache_globals.h" |
---|
15 | #include "xcache/xc_extension.h" |
---|
16 | #include "xcache/xc_ini.h" |
---|
17 | #include "xcache/xc_utils.h" |
---|
18 | #include "xcache/xc_sandbox.h" |
---|
19 | #include "util/xc_trace.h" |
---|
20 | #include "util/xc_vector.h" |
---|
21 | #include "util/xc_align.h" |
---|
22 | |
---|
23 | #include "php.h" |
---|
24 | #include "ext/standard/info.h" |
---|
25 | #include "ext/standard/md5.h" |
---|
26 | #ifdef ZEND_ENGINE_2_1 |
---|
27 | # include "ext/date/php_date.h" |
---|
28 | #endif |
---|
29 | #ifdef ZEND_WIN32 |
---|
30 | # include <process.h> |
---|
31 | #endif |
---|
32 | #include "ext/standard/php_math.h" |
---|
33 | #include "SAPI.h" |
---|
34 | |
---|
35 | #define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0]))) |
---|
36 | #define ECALLOC_ONE(x) ECALLOC_N(x, 1) |
---|
37 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > (pentry)->ctime + (time_t) (pentry)->ttl) |
---|
38 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
---|
39 | #define LOCK(x) xc_lock((x)->lck) |
---|
40 | #define UNLOCK(x) xc_unlock((x)->lck) |
---|
41 | |
---|
42 | #define ENTER_LOCK_EX(x) \ |
---|
43 | LOCK((x)); \ |
---|
44 | zend_try { \ |
---|
45 | do |
---|
46 | #define LEAVE_LOCK_EX(x) \ |
---|
47 | while (0); \ |
---|
48 | } zend_catch { \ |
---|
49 | catched = 1; \ |
---|
50 | } zend_end_try(); \ |
---|
51 | UNLOCK((x)) |
---|
52 | |
---|
53 | #define ENTER_LOCK(x) do { \ |
---|
54 | int catched = 0; \ |
---|
55 | ENTER_LOCK_EX(x) |
---|
56 | #define LEAVE_LOCK(x) \ |
---|
57 | LEAVE_LOCK_EX(x); \ |
---|
58 | if (catched) { \ |
---|
59 | zend_bailout(); \ |
---|
60 | } \ |
---|
61 | } while(0) |
---|
62 | /* }}} */ |
---|
63 | |
---|
64 | struct _xc_hash_t { /* {{{ */ |
---|
65 | size_t bits; |
---|
66 | size_t size; |
---|
67 | xc_hash_value_t mask; |
---|
68 | }; |
---|
69 | /* }}} */ |
---|
70 | struct _xc_cached_t { /* {{{ stored in shm */ |
---|
71 | int cacheid; |
---|
72 | |
---|
73 | time_t compiling; |
---|
74 | time_t disabled; |
---|
75 | zend_ulong updates; |
---|
76 | zend_ulong hits; |
---|
77 | zend_ulong skips; |
---|
78 | zend_ulong ooms; |
---|
79 | zend_ulong errors; |
---|
80 | |
---|
81 | xc_entry_t **entries; |
---|
82 | int entries_count; |
---|
83 | xc_entry_data_php_t **phps; |
---|
84 | int phps_count; |
---|
85 | xc_entry_t *deletes; |
---|
86 | int deletes_count; |
---|
87 | |
---|
88 | time_t last_gc_deletes; |
---|
89 | time_t last_gc_expires; |
---|
90 | |
---|
91 | time_t hits_by_hour_cur_time; |
---|
92 | zend_uint hits_by_hour_cur_slot; |
---|
93 | zend_ulong hits_by_hour[24]; |
---|
94 | time_t hits_by_second_cur_time; |
---|
95 | zend_uint hits_by_second_cur_slot; |
---|
96 | zend_ulong hits_by_second[5]; |
---|
97 | }; |
---|
98 | /* }}} */ |
---|
99 | typedef struct { /* {{{ xc_cache_t: only cache info, not in shm */ |
---|
100 | int cacheid; |
---|
101 | xc_hash_t *hcache; /* hash to cacheid */ |
---|
102 | |
---|
103 | struct _xc_lock_t *lck; |
---|
104 | struct _xc_shm_t *shm; /* which shm contains us */ |
---|
105 | xc_allocator_t *allocator; |
---|
106 | |
---|
107 | xc_hash_t *hentry; /* hash settings to entry */ |
---|
108 | xc_hash_t *hphp; /* hash settings to php */ |
---|
109 | xc_cached_t *cached; |
---|
110 | } xc_cache_t; |
---|
111 | /* }}} */ |
---|
112 | |
---|
113 | /* {{{ globals */ |
---|
114 | static char *xc_shm_scheme = NULL; |
---|
115 | static char *xc_mmap_path = NULL; |
---|
116 | |
---|
117 | static zend_bool xc_admin_enable_auth = 1; |
---|
118 | static xc_hash_t xc_php_hcache = { 0, 0, 0 }; |
---|
119 | static xc_hash_t xc_php_hentry = { 0, 0, 0 }; |
---|
120 | static xc_hash_t xc_var_hcache = { 0, 0, 0 }; |
---|
121 | static xc_hash_t xc_var_hentry = { 0, 0, 0 }; |
---|
122 | |
---|
123 | static zend_ulong xc_php_ttl = 0; |
---|
124 | static zend_ulong xc_var_maxttl = 0; |
---|
125 | |
---|
126 | enum { xc_deletes_gc_interval = 120 }; |
---|
127 | static zend_ulong xc_php_gc_interval = 0; |
---|
128 | static zend_ulong xc_var_gc_interval = 0; |
---|
129 | |
---|
130 | static char *xc_php_allocator = NULL; |
---|
131 | static char *xc_var_allocator = NULL; |
---|
132 | |
---|
133 | /* total size */ |
---|
134 | static zend_ulong xc_php_size = 0; |
---|
135 | static zend_ulong xc_var_size = 0; |
---|
136 | |
---|
137 | static xc_cache_t *xc_php_caches = NULL; |
---|
138 | static xc_cache_t *xc_var_caches = NULL; |
---|
139 | |
---|
140 | static zend_bool xc_initized = 0; |
---|
141 | static time_t xc_init_time = 0; |
---|
142 | static long unsigned xc_init_instance_id = 0; |
---|
143 | #ifdef ZTS |
---|
144 | static long unsigned xc_init_instance_subid = 0; |
---|
145 | #endif |
---|
146 | static zend_compile_file_t *old_compile_file = NULL; |
---|
147 | |
---|
148 | static zend_bool xc_readonly_protection = 0; |
---|
149 | |
---|
150 | static zend_ulong xc_var_namespace_mode = 0; |
---|
151 | static char *xc_var_namespace = NULL; |
---|
152 | |
---|
153 | |
---|
154 | zend_bool xc_have_op_array_ctor = 0; |
---|
155 | /* }}} */ |
---|
156 | |
---|
157 | typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t; |
---|
158 | |
---|
159 | /* any function in *_unlocked is only safe be called within locked (single thread access) area */ |
---|
160 | |
---|
161 | static void xc_php_add_unlocked(xc_cached_t *cached, xc_entry_data_php_t *php) /* {{{ */ |
---|
162 | { |
---|
163 | xc_entry_data_php_t **head = &(cached->phps[php->hvalue]); |
---|
164 | php->next = *head; |
---|
165 | *head = php; |
---|
166 | cached->phps_count ++; |
---|
167 | } |
---|
168 | /* }}} */ |
---|
169 | static xc_entry_data_php_t *xc_php_store_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
170 | { |
---|
171 | xc_entry_data_php_t *stored_php; |
---|
172 | |
---|
173 | php->hits = 0; |
---|
174 | php->refcount = 0; |
---|
175 | stored_php = xc_processor_store_xc_entry_data_php_t(cache->shm, cache->allocator, php TSRMLS_CC); |
---|
176 | if (stored_php) { |
---|
177 | xc_php_add_unlocked(cache->cached, stored_php); |
---|
178 | return stored_php; |
---|
179 | } |
---|
180 | else { |
---|
181 | cache->cached->ooms ++; |
---|
182 | return NULL; |
---|
183 | } |
---|
184 | } |
---|
185 | /* }}} */ |
---|
186 | static xc_entry_data_php_t *xc_php_find_unlocked(xc_cached_t *cached, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
187 | { |
---|
188 | xc_entry_data_php_t *p; |
---|
189 | for (p = cached->phps[php->hvalue]; p; p = (xc_entry_data_php_t *) p->next) { |
---|
190 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
---|
191 | p->hits ++; |
---|
192 | return p; |
---|
193 | } |
---|
194 | } |
---|
195 | return NULL; |
---|
196 | } |
---|
197 | /* }}} */ |
---|
198 | static void xc_php_free_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
---|
199 | { |
---|
200 | cache->allocator->vtable->free(cache->allocator, (xc_entry_data_php_t *)php); |
---|
201 | } |
---|
202 | /* }}} */ |
---|
203 | static void xc_php_addref_unlocked(xc_entry_data_php_t *php) /* {{{ */ |
---|
204 | { |
---|
205 | php->refcount ++; |
---|
206 | } |
---|
207 | /* }}} */ |
---|
208 | static void xc_php_release_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
---|
209 | { |
---|
210 | if (-- php->refcount == 0) { |
---|
211 | xc_entry_data_php_t **pp = &(cache->cached->phps[php->hvalue]); |
---|
212 | xc_entry_data_php_t *p; |
---|
213 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
---|
214 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
---|
215 | /* unlink */ |
---|
216 | *pp = p->next; |
---|
217 | xc_php_free_unlocked(cache, php); |
---|
218 | return; |
---|
219 | } |
---|
220 | } |
---|
221 | assert(0); |
---|
222 | } |
---|
223 | } |
---|
224 | /* }}} */ |
---|
225 | |
---|
226 | static inline zend_bool xc_entry_equal_unlocked(xc_entry_type_t type, const xc_entry_t *entry1, const xc_entry_t *entry2 TSRMLS_DC) /* {{{ */ |
---|
227 | { |
---|
228 | /* this function isn't required but can be in unlocked */ |
---|
229 | switch (type) { |
---|
230 | case XC_TYPE_PHP: |
---|
231 | { |
---|
232 | const xc_entry_php_t *php_entry1 = (const xc_entry_php_t *) entry1; |
---|
233 | const xc_entry_php_t *php_entry2 = (const xc_entry_php_t *) entry2; |
---|
234 | if (php_entry1->file_inode && php_entry2->file_inode) { |
---|
235 | zend_bool inodeIsSame = php_entry1->file_inode == php_entry2->file_inode |
---|
236 | && php_entry1->file_device == php_entry2->file_device; |
---|
237 | if (XG(experimental)) { |
---|
238 | /* new experimental behavior: quick check by inode, first */ |
---|
239 | if (!inodeIsSame) { |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | /* and then opened_path compare */ |
---|
244 | } |
---|
245 | else { |
---|
246 | /* old behavior: inode check only */ |
---|
247 | return inodeIsSame; |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | assert(strstr(entry1->name.str.val, "://") != NULL || IS_ABSOLUTE_PATH(entry1->name.str.val, entry1->name.str.len)); |
---|
253 | assert(strstr(entry1->name.str.val, "://") != NULL || IS_ABSOLUTE_PATH(entry2->name.str.val, entry2->name.str.len)); |
---|
254 | |
---|
255 | return entry1->name.str.len == entry2->name.str.len |
---|
256 | && memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0; |
---|
257 | |
---|
258 | case XC_TYPE_VAR: |
---|
259 | #ifdef IS_UNICODE |
---|
260 | if (entry1->name_type != entry2->name_type) { |
---|
261 | return 0; |
---|
262 | } |
---|
263 | |
---|
264 | if (entry1->name_type == IS_UNICODE) { |
---|
265 | return entry1->name.ustr.len == entry2->name.ustr.len |
---|
266 | && memcmp(entry1->name.ustr.val, entry2->name.ustr.val, (entry1->name.ustr.len + 1) * sizeof(entry1->name.ustr.val[0])) == 0; |
---|
267 | } |
---|
268 | #endif |
---|
269 | return entry1->name.str.len == entry2->name.str.len |
---|
270 | && memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0; |
---|
271 | break; |
---|
272 | |
---|
273 | default: |
---|
274 | assert(0); |
---|
275 | } |
---|
276 | return 0; |
---|
277 | } |
---|
278 | /* }}} */ |
---|
279 | static void xc_entry_add_unlocked(xc_cached_t *cached, xc_hash_value_t entryslotid, xc_entry_t *entry) /* {{{ */ |
---|
280 | { |
---|
281 | xc_entry_t **head = &(cached->entries[entryslotid]); |
---|
282 | entry->next = *head; |
---|
283 | *head = entry; |
---|
284 | cached->entries_count ++; |
---|
285 | } |
---|
286 | /* }}} */ |
---|
287 | static xc_entry_t *xc_entry_store_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
---|
288 | { |
---|
289 | xc_entry_t *stored_entry; |
---|
290 | |
---|
291 | entry->hits = 0; |
---|
292 | entry->ctime = XG(request_time); |
---|
293 | entry->atime = XG(request_time); |
---|
294 | stored_entry = type == XC_TYPE_PHP |
---|
295 | ? (xc_entry_t *) xc_processor_store_xc_entry_php_t(cache->shm, cache->allocator, (xc_entry_php_t *) entry TSRMLS_CC) |
---|
296 | : (xc_entry_t *) xc_processor_store_xc_entry_var_t(cache->shm, cache->allocator, (xc_entry_var_t *) entry TSRMLS_CC); |
---|
297 | if (stored_entry) { |
---|
298 | xc_entry_add_unlocked(cache->cached, entryslotid, stored_entry); |
---|
299 | ++cache->cached->updates; |
---|
300 | return stored_entry; |
---|
301 | } |
---|
302 | else { |
---|
303 | cache->cached->ooms ++; |
---|
304 | return NULL; |
---|
305 | } |
---|
306 | } |
---|
307 | /* }}} */ |
---|
308 | static xc_entry_php_t *xc_entry_php_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
---|
309 | { |
---|
310 | return (xc_entry_php_t *) xc_entry_store_unlocked(XC_TYPE_PHP, cache, entryslotid, (xc_entry_t *) entry_php TSRMLS_CC); |
---|
311 | } |
---|
312 | /* }}} */ |
---|
313 | static xc_entry_var_t *xc_entry_var_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_var_t *entry_var TSRMLS_DC) /* {{{ */ |
---|
314 | { |
---|
315 | return (xc_entry_var_t *) xc_entry_store_unlocked(XC_TYPE_VAR, cache, entryslotid, (xc_entry_t *) entry_var TSRMLS_CC); |
---|
316 | } |
---|
317 | /* }}} */ |
---|
318 | static void xc_entry_free_real_unlocked(xc_entry_type_t type, xc_cache_t *cache, volatile xc_entry_t *entry) /* {{{ */ |
---|
319 | { |
---|
320 | if (type == XC_TYPE_PHP) { |
---|
321 | xc_php_release_unlocked(cache, ((xc_entry_php_t *) entry)->php); |
---|
322 | } |
---|
323 | cache->allocator->vtable->free(cache->allocator, (xc_entry_t *)entry); |
---|
324 | } |
---|
325 | /* }}} */ |
---|
326 | static void xc_entry_free_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
---|
327 | { |
---|
328 | cache->cached->entries_count --; |
---|
329 | if ((type == XC_TYPE_PHP ? ((xc_entry_php_t *) entry)->refcount : 0) == 0) { |
---|
330 | xc_entry_free_real_unlocked(type, cache, entry); |
---|
331 | } |
---|
332 | else { |
---|
333 | entry->next = cache->cached->deletes; |
---|
334 | cache->cached->deletes = entry; |
---|
335 | entry->dtime = XG(request_time); |
---|
336 | cache->cached->deletes_count ++; |
---|
337 | } |
---|
338 | return; |
---|
339 | } |
---|
340 | /* }}} */ |
---|
341 | static void xc_entry_remove_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
---|
342 | { |
---|
343 | xc_entry_t **pp = &(cache->cached->entries[entryslotid]); |
---|
344 | xc_entry_t *p; |
---|
345 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
---|
346 | if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) { |
---|
347 | /* unlink */ |
---|
348 | *pp = p->next; |
---|
349 | xc_entry_free_unlocked(type, cache, entry TSRMLS_CC); |
---|
350 | return; |
---|
351 | } |
---|
352 | } |
---|
353 | assert(0); |
---|
354 | } |
---|
355 | /* }}} */ |
---|
356 | static xc_entry_t *xc_entry_find_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
---|
357 | { |
---|
358 | xc_entry_t *p; |
---|
359 | for (p = cache->cached->entries[entryslotid]; p; p = p->next) { |
---|
360 | if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) { |
---|
361 | zend_bool fresh; |
---|
362 | switch (type) { |
---|
363 | case XC_TYPE_PHP: |
---|
364 | { |
---|
365 | xc_entry_php_t *p_php = (xc_entry_php_t *) p; |
---|
366 | xc_entry_php_t *entry_php = (xc_entry_php_t *) entry; |
---|
367 | fresh = p_php->file_mtime == entry_php->file_mtime && p_php->file_size == entry_php->file_size; |
---|
368 | } |
---|
369 | break; |
---|
370 | |
---|
371 | case XC_TYPE_VAR: |
---|
372 | { |
---|
373 | fresh = !VAR_ENTRY_EXPIRED(p); |
---|
374 | } |
---|
375 | break; |
---|
376 | |
---|
377 | default: |
---|
378 | assert(0); |
---|
379 | } |
---|
380 | |
---|
381 | if (fresh) { |
---|
382 | p->hits ++; |
---|
383 | p->atime = XG(request_time); |
---|
384 | return p; |
---|
385 | } |
---|
386 | |
---|
387 | xc_entry_remove_unlocked(type, cache, entryslotid, p TSRMLS_CC); |
---|
388 | return NULL; |
---|
389 | } |
---|
390 | } |
---|
391 | return NULL; |
---|
392 | } |
---|
393 | /* }}} */ |
---|
394 | static void xc_entry_hold_php_unlocked(xc_cache_t *cache, xc_entry_php_t *entry TSRMLS_DC) /* {{{ */ |
---|
395 | { |
---|
396 | TRACE("hold %d:%s", entry->file_inode, entry->entry.name.str.val); |
---|
397 | entry->refcount ++; |
---|
398 | xc_stack_push(&XG(php_holds)[cache->cacheid], (void *)entry); |
---|
399 | } |
---|
400 | /* }}} */ |
---|
401 | static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */ |
---|
402 | { |
---|
403 | if (val + 1 >= count) { |
---|
404 | return 0; |
---|
405 | } |
---|
406 | return val + 1; |
---|
407 | } |
---|
408 | /* }}} */ |
---|
409 | static inline void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t interval, zend_ulong *counters, zend_uint count TSRMLS_DC) /* {{{ */ |
---|
410 | { |
---|
411 | time_t n = XG(request_time) / interval; |
---|
412 | if (*curtime != n) { |
---|
413 | zend_uint target_slot = ((zend_uint) n) % count; |
---|
414 | zend_uint slot; |
---|
415 | for (slot = advance_wrapped(*curslot, count); |
---|
416 | slot != target_slot; |
---|
417 | slot = advance_wrapped(slot, count)) { |
---|
418 | counters[slot] = 0; |
---|
419 | } |
---|
420 | counters[target_slot] = 0; |
---|
421 | *curtime = n; |
---|
422 | *curslot = target_slot; |
---|
423 | } |
---|
424 | counters[*curslot] ++; |
---|
425 | } |
---|
426 | /* }}} */ |
---|
427 | static inline void xc_cached_hit_unlocked(xc_cached_t *cached TSRMLS_DC) /* {{{ */ |
---|
428 | { |
---|
429 | cached->hits ++; |
---|
430 | |
---|
431 | xc_counters_inc(&cached->hits_by_hour_cur_time |
---|
432 | , &cached->hits_by_hour_cur_slot, 60 * 60 |
---|
433 | , cached->hits_by_hour |
---|
434 | , sizeof(cached->hits_by_hour) / sizeof(cached->hits_by_hour[0]) |
---|
435 | TSRMLS_CC); |
---|
436 | |
---|
437 | xc_counters_inc(&cached->hits_by_second_cur_time |
---|
438 | , &cached->hits_by_second_cur_slot, 1 |
---|
439 | , cached->hits_by_second |
---|
440 | , sizeof(cached->hits_by_second) / sizeof(cached->hits_by_second[0]) |
---|
441 | TSRMLS_CC); |
---|
442 | } |
---|
443 | /* }}} */ |
---|
444 | |
---|
445 | /* helper function that loop through each entry */ |
---|
446 | #define XC_ENTRY_APPLY_FUNC(name) zend_bool name(xc_entry_t *entry TSRMLS_DC) |
---|
447 | typedef XC_ENTRY_APPLY_FUNC((*cache_apply_unlocked_func_t)); |
---|
448 | static void xc_entry_apply_unlocked(xc_entry_type_t type, xc_cache_t *cache, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */ |
---|
449 | { |
---|
450 | xc_entry_t *p, **pp; |
---|
451 | size_t i, c; |
---|
452 | |
---|
453 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
---|
454 | pp = &(cache->cached->entries[i]); |
---|
455 | for (p = *pp; p; p = *pp) { |
---|
456 | if (apply_func(p TSRMLS_CC)) { |
---|
457 | /* unlink */ |
---|
458 | *pp = p->next; |
---|
459 | xc_entry_free_unlocked(type, cache, p TSRMLS_CC); |
---|
460 | } |
---|
461 | else { |
---|
462 | pp = &(p->next); |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | /* }}} */ |
---|
468 | |
---|
469 | #define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC) |
---|
470 | /* call graph: |
---|
471 | * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_php_entry_unlocked |
---|
472 | * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_var_entry_unlocked |
---|
473 | */ |
---|
474 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_unlocked) /* {{{ */ |
---|
475 | { |
---|
476 | TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl); |
---|
477 | if (XG(request_time) > entry->atime + (time_t) xc_php_ttl) { |
---|
478 | return 1; |
---|
479 | } |
---|
480 | return 0; |
---|
481 | } |
---|
482 | /* }}} */ |
---|
483 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_unlocked) /* {{{ */ |
---|
484 | { |
---|
485 | if (VAR_ENTRY_EXPIRED(entry)) { |
---|
486 | return 1; |
---|
487 | } |
---|
488 | return 0; |
---|
489 | } |
---|
490 | /* }}} */ |
---|
491 | static void xc_gc_expires_one(xc_entry_type_t type, xc_cache_t *cache, zend_ulong gc_interval, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */ |
---|
492 | { |
---|
493 | TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->cached->last_gc_expires, gc_interval); |
---|
494 | if (!cache->cached->disabled && XG(request_time) >= cache->cached->last_gc_expires + (time_t) gc_interval) { |
---|
495 | ENTER_LOCK(cache) { |
---|
496 | if (XG(request_time) >= cache->cached->last_gc_expires + (time_t) gc_interval) { |
---|
497 | cache->cached->last_gc_expires = XG(request_time); |
---|
498 | xc_entry_apply_unlocked(type, cache, apply_func TSRMLS_CC); |
---|
499 | } |
---|
500 | } LEAVE_LOCK(cache); |
---|
501 | } |
---|
502 | } |
---|
503 | /* }}} */ |
---|
504 | static void xc_gc_expires_php(TSRMLS_D) /* {{{ */ |
---|
505 | { |
---|
506 | size_t i, c; |
---|
507 | |
---|
508 | if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) { |
---|
509 | return; |
---|
510 | } |
---|
511 | |
---|
512 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
---|
513 | xc_gc_expires_one(XC_TYPE_PHP, &xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_unlocked TSRMLS_CC); |
---|
514 | } |
---|
515 | } |
---|
516 | /* }}} */ |
---|
517 | static void xc_gc_expires_var(TSRMLS_D) /* {{{ */ |
---|
518 | { |
---|
519 | size_t i, c; |
---|
520 | |
---|
521 | if (!xc_var_gc_interval || !xc_var_caches) { |
---|
522 | return; |
---|
523 | } |
---|
524 | |
---|
525 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
---|
526 | xc_gc_expires_one(XC_TYPE_VAR, &xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_unlocked TSRMLS_CC); |
---|
527 | } |
---|
528 | } |
---|
529 | /* }}} */ |
---|
530 | |
---|
531 | static XC_CACHE_APPLY_FUNC(xc_gc_delete_unlocked) /* {{{ */ |
---|
532 | { |
---|
533 | xc_entry_t *p, **pp; |
---|
534 | |
---|
535 | pp = &cache->cached->deletes; |
---|
536 | for (p = *pp; p; p = *pp) { |
---|
537 | xc_entry_php_t *entry = (xc_entry_php_t *) p; |
---|
538 | if (XG(request_time) - p->dtime > 3600) { |
---|
539 | entry->refcount = 0; |
---|
540 | /* issue warning here */ |
---|
541 | } |
---|
542 | if (entry->refcount == 0) { |
---|
543 | /* unlink */ |
---|
544 | *pp = p->next; |
---|
545 | cache->cached->deletes_count --; |
---|
546 | xc_entry_free_real_unlocked(XC_TYPE_PHP, cache, p); |
---|
547 | } |
---|
548 | else { |
---|
549 | pp = &(p->next); |
---|
550 | } |
---|
551 | } |
---|
552 | } |
---|
553 | /* }}} */ |
---|
554 | static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */ |
---|
555 | { |
---|
556 | if (!cache->cached->disabled && cache->cached->deletes && XG(request_time) - cache->cached->last_gc_deletes > xc_deletes_gc_interval) { |
---|
557 | ENTER_LOCK(cache) { |
---|
558 | if (cache->cached->deletes && XG(request_time) - cache->cached->last_gc_deletes > xc_deletes_gc_interval) { |
---|
559 | cache->cached->last_gc_deletes = XG(request_time); |
---|
560 | xc_gc_delete_unlocked(cache TSRMLS_CC); |
---|
561 | } |
---|
562 | } LEAVE_LOCK(cache); |
---|
563 | } |
---|
564 | } |
---|
565 | /* }}} */ |
---|
566 | static void xc_gc_deletes(TSRMLS_D) /* {{{ */ |
---|
567 | { |
---|
568 | size_t i, c; |
---|
569 | |
---|
570 | if (xc_php_caches) { |
---|
571 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
---|
572 | xc_gc_deletes_one(&xc_php_caches[i] TSRMLS_CC); |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | if (xc_var_caches) { |
---|
577 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
---|
578 | xc_gc_deletes_one(&xc_var_caches[i] TSRMLS_CC); |
---|
579 | } |
---|
580 | } |
---|
581 | } |
---|
582 | /* }}} */ |
---|
583 | |
---|
584 | /* helper functions for user functions */ |
---|
585 | static void xc_fillinfo_unlocked(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
---|
586 | { |
---|
587 | zval *blocks, *hits; |
---|
588 | size_t i; |
---|
589 | const xc_allocator_block_t *b; |
---|
590 | #ifndef NDEBUG |
---|
591 | xc_memsize_t avail = 0; |
---|
592 | #endif |
---|
593 | const xc_allocator_t *allocator = cache->allocator; |
---|
594 | const xc_allocator_vtable_t *vtable = allocator->vtable; |
---|
595 | zend_ulong interval; |
---|
596 | const xc_cached_t *cached = cache->cached; |
---|
597 | |
---|
598 | if (cachetype == XC_TYPE_PHP) { |
---|
599 | interval = xc_php_ttl ? xc_php_gc_interval : 0; |
---|
600 | } |
---|
601 | else { |
---|
602 | interval = xc_var_gc_interval; |
---|
603 | } |
---|
604 | |
---|
605 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
---|
606 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cached->compiling); |
---|
607 | add_assoc_long_ex(return_value, ZEND_STRS("disabled"), cached->disabled); |
---|
608 | add_assoc_long_ex(return_value, ZEND_STRS("updates"), cached->updates); |
---|
609 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cached->updates); /* deprecated */ |
---|
610 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cached->hits); |
---|
611 | add_assoc_long_ex(return_value, ZEND_STRS("skips"), cached->skips); |
---|
612 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cached->skips); /* deprecated */ |
---|
613 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cached->ooms); |
---|
614 | add_assoc_long_ex(return_value, ZEND_STRS("errors"), cached->errors); |
---|
615 | |
---|
616 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cached->entries_count); |
---|
617 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cached->deletes_count); |
---|
618 | if (interval) { |
---|
619 | time_t gc = (cached->last_gc_expires + interval) - XG(request_time); |
---|
620 | add_assoc_long_ex(return_value, ZEND_STRS("gc"), gc > 0 ? gc : 0); |
---|
621 | } |
---|
622 | else { |
---|
623 | add_assoc_null_ex(return_value, ZEND_STRS("gc")); |
---|
624 | } |
---|
625 | MAKE_STD_ZVAL(hits); |
---|
626 | array_init(hits); |
---|
627 | for (i = 0; i < sizeof(cached->hits_by_hour) / sizeof(cached->hits_by_hour[0]); i ++) { |
---|
628 | add_next_index_long(hits, (long) cached->hits_by_hour[i]); |
---|
629 | } |
---|
630 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_hour"), hits); |
---|
631 | |
---|
632 | MAKE_STD_ZVAL(hits); |
---|
633 | array_init(hits); |
---|
634 | for (i = 0; i < sizeof(cached->hits_by_second) / sizeof(cached->hits_by_second[0]); i ++) { |
---|
635 | add_next_index_long(hits, (long) cached->hits_by_second[i]); |
---|
636 | } |
---|
637 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_second"), hits); |
---|
638 | |
---|
639 | MAKE_STD_ZVAL(blocks); |
---|
640 | array_init(blocks); |
---|
641 | |
---|
642 | add_assoc_long_ex(return_value, ZEND_STRS("size"), vtable->size(allocator)); |
---|
643 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), vtable->avail(allocator)); |
---|
644 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
---|
645 | |
---|
646 | for (b = vtable->freeblock_first(allocator); b; b = vtable->freeblock_next(b)) { |
---|
647 | zval *bi; |
---|
648 | |
---|
649 | MAKE_STD_ZVAL(bi); |
---|
650 | array_init(bi); |
---|
651 | |
---|
652 | add_assoc_long_ex(bi, ZEND_STRS("size"), vtable->block_size(b)); |
---|
653 | add_assoc_long_ex(bi, ZEND_STRS("offset"), vtable->block_offset(allocator, b)); |
---|
654 | add_next_index_zval(blocks, bi); |
---|
655 | #ifndef NDEBUG |
---|
656 | avail += vtable->block_size(b); |
---|
657 | #endif |
---|
658 | } |
---|
659 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
---|
660 | #ifndef NDEBUG |
---|
661 | assert(avail == vtable->avail(allocator)); |
---|
662 | #endif |
---|
663 | } |
---|
664 | /* }}} */ |
---|
665 | static void xc_fillentry_unlocked(xc_entry_type_t type, const xc_entry_t *entry, xc_hash_value_t entryslotid, int del, zval *list TSRMLS_DC) /* {{{ */ |
---|
666 | { |
---|
667 | zval* ei; |
---|
668 | const xc_entry_data_php_t *php; |
---|
669 | |
---|
670 | ALLOC_INIT_ZVAL(ei); |
---|
671 | array_init(ei); |
---|
672 | |
---|
673 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
---|
674 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
---|
675 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
---|
676 | add_assoc_long_ex(ei, ZEND_STRS("hvalue"), entryslotid); |
---|
677 | if (del) { |
---|
678 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
---|
679 | } |
---|
680 | #ifdef IS_UNICODE |
---|
681 | do { |
---|
682 | zval *zv; |
---|
683 | ALLOC_INIT_ZVAL(zv); |
---|
684 | switch (entry->name_type) { |
---|
685 | case IS_UNICODE: |
---|
686 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
---|
687 | break; |
---|
688 | case IS_STRING: |
---|
689 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
---|
690 | break; |
---|
691 | default: |
---|
692 | assert(0); |
---|
693 | } |
---|
694 | zv->type = entry->name_type; |
---|
695 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
---|
696 | } while (0); |
---|
697 | #else |
---|
698 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1); |
---|
699 | #endif |
---|
700 | switch (type) { |
---|
701 | case XC_TYPE_PHP: { |
---|
702 | xc_entry_php_t *entry_php = (xc_entry_php_t *) entry; |
---|
703 | php = entry_php->php; |
---|
704 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size + php->size); |
---|
705 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry_php->refcount); |
---|
706 | add_assoc_long_ex(ei, ZEND_STRS("phprefcount"), php->refcount); |
---|
707 | add_assoc_long_ex(ei, ZEND_STRS("file_mtime"), entry_php->file_mtime); |
---|
708 | add_assoc_long_ex(ei, ZEND_STRS("file_size"), entry_php->file_size); |
---|
709 | add_assoc_long_ex(ei, ZEND_STRS("file_device"), entry_php->file_device); |
---|
710 | add_assoc_long_ex(ei, ZEND_STRS("file_inode"), entry_php->file_inode); |
---|
711 | |
---|
712 | #ifdef HAVE_XCACHE_CONSTANT |
---|
713 | add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt); |
---|
714 | #endif |
---|
715 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
---|
716 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
---|
717 | #ifdef ZEND_ENGINE_2_1 |
---|
718 | add_assoc_long_ex(ei, ZEND_STRS("autoglobal_cnt"),php->autoglobal_cnt); |
---|
719 | #endif |
---|
720 | break; |
---|
721 | } |
---|
722 | |
---|
723 | case XC_TYPE_VAR: |
---|
724 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), 0); /* for BC only */ |
---|
725 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
---|
726 | break; |
---|
727 | |
---|
728 | default: |
---|
729 | assert(0); |
---|
730 | } |
---|
731 | |
---|
732 | add_next_index_zval(list, ei); |
---|
733 | } |
---|
734 | /* }}} */ |
---|
735 | static void xc_filllist_unlocked(xc_entry_type_t type, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
---|
736 | { |
---|
737 | zval* list; |
---|
738 | size_t i, c; |
---|
739 | xc_entry_t *e; |
---|
740 | |
---|
741 | ALLOC_INIT_ZVAL(list); |
---|
742 | array_init(list); |
---|
743 | |
---|
744 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
---|
745 | for (e = cache->cached->entries[i]; e; e = e->next) { |
---|
746 | xc_fillentry_unlocked(type, e, i, 0, list TSRMLS_CC); |
---|
747 | } |
---|
748 | } |
---|
749 | add_assoc_zval(return_value, "cache_list", list); |
---|
750 | |
---|
751 | ALLOC_INIT_ZVAL(list); |
---|
752 | array_init(list); |
---|
753 | for (e = cache->cached->deletes; e; e = e->next) { |
---|
754 | xc_fillentry_unlocked(XC_TYPE_PHP, e, 0, 1, list TSRMLS_CC); |
---|
755 | } |
---|
756 | add_assoc_zval(return_value, "deleted_list", list); |
---|
757 | } |
---|
758 | /* }}} */ |
---|
759 | |
---|
760 | static zend_op_array *xc_entry_install(xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
---|
761 | { |
---|
762 | zend_uint i; |
---|
763 | xc_entry_data_php_t *p = entry_php->php; |
---|
764 | zend_op_array *old_active_op_array = CG(active_op_array); |
---|
765 | #ifndef ZEND_ENGINE_2 |
---|
766 | ALLOCA_FLAG(use_heap) |
---|
767 | /* new ptr which is stored inside CG(class_table) */ |
---|
768 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)xc_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap); |
---|
769 | #endif |
---|
770 | |
---|
771 | CG(active_op_array) = p->op_array; |
---|
772 | |
---|
773 | #ifdef HAVE_XCACHE_CONSTANT |
---|
774 | /* install constant */ |
---|
775 | for (i = 0; i < p->constinfo_cnt; i ++) { |
---|
776 | xc_constinfo_t *ci = &p->constinfos[i]; |
---|
777 | xc_install_constant(entry_php->entry.name.str.val, &ci->constant, |
---|
778 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
779 | } |
---|
780 | #endif |
---|
781 | |
---|
782 | /* install function */ |
---|
783 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
---|
784 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
---|
785 | xc_install_function(entry_php->entry.name.str.val, &fi->func, |
---|
786 | UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC); |
---|
787 | } |
---|
788 | |
---|
789 | /* install class */ |
---|
790 | for (i = 0; i < p->classinfo_cnt; i ++) { |
---|
791 | xc_classinfo_t *ci = &p->classinfos[i]; |
---|
792 | #ifndef ZEND_ENGINE_2 |
---|
793 | zend_class_entry *ce = CestToCePtr(ci->cest); |
---|
794 | /* fix pointer to the be which inside class_table */ |
---|
795 | if (ce->parent) { |
---|
796 | zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1; |
---|
797 | assert(class_idx < i); |
---|
798 | ci->cest.parent = new_cest_ptrs[class_idx]; |
---|
799 | } |
---|
800 | new_cest_ptrs[i] = |
---|
801 | #endif |
---|
802 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
---|
803 | xc_install_class(entry_php->entry.name.str.val, &ci->cest, -1, |
---|
804 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
805 | #else |
---|
806 | xc_install_class(entry_php->entry.name.str.val, &ci->cest, ci->oplineno, |
---|
807 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
808 | #endif |
---|
809 | } |
---|
810 | |
---|
811 | #ifdef ZEND_ENGINE_2_1 |
---|
812 | /* trigger auto_globals jit */ |
---|
813 | for (i = 0; i < p->autoglobal_cnt; i ++) { |
---|
814 | xc_autoglobal_t *aginfo = &p->autoglobals[i]; |
---|
815 | zend_u_is_auto_global(aginfo->type, aginfo->key, aginfo->key_len TSRMLS_CC); |
---|
816 | } |
---|
817 | #endif |
---|
818 | #ifdef XCACHE_ERROR_CACHING |
---|
819 | /* restore trigger errors */ |
---|
820 | for (i = 0; i < p->compilererror_cnt; i ++) { |
---|
821 | xc_compilererror_t *error = &p->compilererrors[i]; |
---|
822 | CG(zend_lineno) = error->lineno; |
---|
823 | zend_error(error->type, "%s", error->error); |
---|
824 | } |
---|
825 | CG(zend_lineno) = 0; |
---|
826 | #endif |
---|
827 | |
---|
828 | i = 1; |
---|
829 | #ifndef ZEND_ENGINE_2_2 |
---|
830 | zend_hash_add(&EG(included_files), entry_php->entry.name.str.val, entry_php->entry.name.str.len+1, (void *)&i, sizeof(int), NULL); |
---|
831 | #endif |
---|
832 | |
---|
833 | #ifndef ZEND_ENGINE_2 |
---|
834 | xc_free_alloca(new_cest_ptrs, use_heap); |
---|
835 | #endif |
---|
836 | CG(active_op_array) = old_active_op_array; |
---|
837 | return p->op_array; |
---|
838 | } |
---|
839 | /* }}} */ |
---|
840 | |
---|
841 | static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t *caches, size_t cachecount TSRMLS_DC) /* {{{ */ |
---|
842 | { |
---|
843 | size_t i; |
---|
844 | xc_stack_t *s; |
---|
845 | xc_cache_t *cache; |
---|
846 | xc_entry_php_t *entry_php; |
---|
847 | |
---|
848 | for (i = 0; i < cachecount; i ++) { |
---|
849 | s = &holds[i]; |
---|
850 | TRACE("holded %d items", xc_stack_count(s)); |
---|
851 | if (xc_stack_count(s)) { |
---|
852 | cache = &caches[i]; |
---|
853 | ENTER_LOCK(cache) { |
---|
854 | while (xc_stack_count(s)) { |
---|
855 | entry_php = (xc_entry_php_t *) xc_stack_pop(s); |
---|
856 | TRACE("unhold %d:%s", entry_php->file_inode, entry_php->entry.name.str.val); |
---|
857 | assert(entry_php->refcount > 0); |
---|
858 | --entry_php->refcount; |
---|
859 | } |
---|
860 | } LEAVE_LOCK(cache); |
---|
861 | } |
---|
862 | } |
---|
863 | } |
---|
864 | /* }}} */ |
---|
865 | static void xc_entry_unholds(TSRMLS_D) /* {{{ */ |
---|
866 | { |
---|
867 | if (xc_php_caches) { |
---|
868 | xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
---|
869 | } |
---|
870 | |
---|
871 | if (xc_var_caches) { |
---|
872 | xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
---|
873 | } |
---|
874 | } |
---|
875 | /* }}} */ |
---|
876 | |
---|
877 | #define HASH(i) (i) |
---|
878 | #define HASH_ZSTR_L(t, s, l) HASH(zend_u_inline_hash_func((t), (s), ((l) + 1) * sizeof(UChar))) |
---|
879 | #define HASH_STR_S(s, l) HASH(zend_inline_hash_func((char *) (s), (l))) |
---|
880 | #define HASH_STR_L(s, l) HASH_STR_S((s), (l) + 1) |
---|
881 | #define HASH_STR(s) HASH_STR_L((s), strlen((s)) + 1) |
---|
882 | #define HASH_NUM(n) HASH(n) |
---|
883 | static inline xc_hash_value_t xc_hash_fold(xc_hash_value_t hvalue, const xc_hash_t *hasher) /* {{{ fold hash bits as needed */ |
---|
884 | { |
---|
885 | xc_hash_value_t folded = 0; |
---|
886 | while (hvalue) { |
---|
887 | folded ^= (hvalue & hasher->mask); |
---|
888 | hvalue >>= hasher->bits; |
---|
889 | } |
---|
890 | return folded; |
---|
891 | } |
---|
892 | /* }}} */ |
---|
893 | static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *entry TSRMLS_DC) /* {{{ */ |
---|
894 | { |
---|
895 | return UNISW(NOTHING, UG(unicode) ? HASH_ZSTR_L(entry->name_type, entry->name.uni.val, entry->name.uni.len) :) |
---|
896 | HASH_STR_L(entry->name.str.val, entry->name.str.len); |
---|
897 | } |
---|
898 | /* }}} */ |
---|
899 | #define xc_entry_hash_var xc_entry_hash_name |
---|
900 | static void xc_entry_free_key_php(xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */ |
---|
901 | { |
---|
902 | #define X_FREE(var) do {\ |
---|
903 | if (entry_php->var) { \ |
---|
904 | efree(entry_php->var); \ |
---|
905 | } \ |
---|
906 | } while (0) |
---|
907 | X_FREE(dirpath); |
---|
908 | #ifdef IS_UNICODE |
---|
909 | X_FREE(ufilepath); |
---|
910 | X_FREE(udirpath); |
---|
911 | #endif |
---|
912 | |
---|
913 | #undef X_FREE |
---|
914 | } |
---|
915 | /* }}} */ |
---|
916 | static char *xc_expand_url(const char *filepath, char *real_path TSRMLS_DC) /* {{{ */ |
---|
917 | { |
---|
918 | if (strstr(filepath, "://") != NULL) { |
---|
919 | size_t filepath_len = strlen(filepath); |
---|
920 | size_t copy_len = filepath_len > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : filepath_len; |
---|
921 | memcpy(real_path, filepath, filepath_len); |
---|
922 | real_path[copy_len] = '\0'; |
---|
923 | return real_path; |
---|
924 | } |
---|
925 | return expand_filepath(filepath, real_path TSRMLS_CC); |
---|
926 | } |
---|
927 | /* }}} */ |
---|
928 | |
---|
929 | #define XC_RESOLVE_PATH_CHECKER(name) int name(const char *filepath, size_t filepath_len, void *data TSRMLS_DC) |
---|
930 | typedef XC_RESOLVE_PATH_CHECKER((*xc_resolve_path_checker_func_t)); |
---|
931 | static int xc_resolve_path(const char *filepath, char *path_buffer, xc_resolve_path_checker_func_t checker_func, void *data TSRMLS_DC) /* {{{ */ |
---|
932 | { |
---|
933 | char *paths, *path; |
---|
934 | char *tokbuf; |
---|
935 | size_t path_buffer_len; |
---|
936 | size_t size; |
---|
937 | char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' }; |
---|
938 | int ret; |
---|
939 | ALLOCA_FLAG(use_heap) |
---|
940 | |
---|
941 | #if 0 |
---|
942 | if ((*filepath == '.' && |
---|
943 | (IS_SLASH(filepath[1]) || |
---|
944 | ((filepath[1] == '.') && IS_SLASH(filepath[2])))) || |
---|
945 | IS_ABSOLUTE_PATH(filepath, strlen(filepath)) || |
---|
946 | !path || |
---|
947 | !*path) { |
---|
948 | |
---|
949 | ret = checker_func(path_buffer, path_buffer_len, data TSRMLS_CC); |
---|
950 | goto finish; |
---|
951 | } |
---|
952 | #endif |
---|
953 | |
---|
954 | size = strlen(PG(include_path)) + 1; |
---|
955 | paths = (char *)xc_do_alloca(size, use_heap); |
---|
956 | memcpy(paths, PG(include_path), size); |
---|
957 | |
---|
958 | for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) { |
---|
959 | path_buffer_len = snprintf(path_buffer, MAXPATHLEN, "%s/%s", path, filepath); |
---|
960 | if (path_buffer_len < MAXPATHLEN - 1) { |
---|
961 | ret = checker_func(path_buffer, path_buffer_len, data TSRMLS_CC); |
---|
962 | if (ret == SUCCESS) { |
---|
963 | goto finish; |
---|
964 | } |
---|
965 | } |
---|
966 | } |
---|
967 | |
---|
968 | /* fall back to current directory */ |
---|
969 | if (zend_is_executing(TSRMLS_C)) { |
---|
970 | const char *executing_filename = zend_get_executed_filename(TSRMLS_C); |
---|
971 | int dirname_len = (int) strlen(executing_filename); |
---|
972 | size_t filename_len = strlen(filepath); |
---|
973 | |
---|
974 | while ((--dirname_len >= 0) && !IS_SLASH(executing_filename[dirname_len])); |
---|
975 | if (executing_filename && dirname_len > 0 && executing_filename[0] && executing_filename[0] != '[' |
---|
976 | && dirname_len + 1 + filename_len + 1 < MAXPATHLEN) { |
---|
977 | memcpy(path_buffer, executing_filename, dirname_len + 1); |
---|
978 | memcpy(path_buffer + dirname_len + 1, filepath, filename_len + 1); |
---|
979 | path_buffer_len = dirname_len + 1 + filename_len; |
---|
980 | ret = checker_func(path_buffer, path_buffer_len, data TSRMLS_CC); |
---|
981 | if (ret == SUCCESS) { |
---|
982 | goto finish; |
---|
983 | } |
---|
984 | } |
---|
985 | } |
---|
986 | |
---|
987 | ret = FAILURE; |
---|
988 | |
---|
989 | finish: |
---|
990 | xc_free_alloca(paths, use_heap); |
---|
991 | |
---|
992 | return ret; |
---|
993 | } |
---|
994 | /* }}} */ |
---|
995 | |
---|
996 | static zend_bool xc_is_absolute(const char *filepath, size_t filepath_len) /* {{{ */ |
---|
997 | { |
---|
998 | const char *p; |
---|
999 | |
---|
1000 | if (IS_ABSOLUTE_PATH(filepath, filepath_len)) { |
---|
1001 | return 1; |
---|
1002 | } |
---|
1003 | |
---|
1004 | for (p = filepath; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++); |
---|
1005 | if ((*p == ':') && (p - filepath > 1) && (p[1] == '/') && (p[2] == '/')) { |
---|
1006 | return 1; |
---|
1007 | } |
---|
1008 | |
---|
1009 | return 0; |
---|
1010 | } |
---|
1011 | /* }}} */ |
---|
1012 | static int xc_stat(const char *filepath, struct stat *statbuf TSRMLS_DC) /* {{{ */ |
---|
1013 | { |
---|
1014 | if (strstr(filepath, "://") != NULL) { |
---|
1015 | php_stream_statbuf ssb; |
---|
1016 | php_stream_wrapper *wrapper = NULL; |
---|
1017 | char *path_for_open = NULL; |
---|
1018 | |
---|
1019 | wrapper = php_stream_locate_url_wrapper(filepath, &path_for_open, 0 TSRMLS_CC); |
---|
1020 | if (wrapper && wrapper->wops->url_stat |
---|
1021 | && wrapper != &php_plain_files_wrapper |
---|
1022 | && wrapper->wops->url_stat(wrapper, path_for_open, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL TSRMLS_CC) == SUCCESS) { |
---|
1023 | *statbuf = ssb.sb; |
---|
1024 | return SUCCESS; |
---|
1025 | } |
---|
1026 | |
---|
1027 | return FAILURE; |
---|
1028 | } |
---|
1029 | |
---|
1030 | return VCWD_STAT(filepath, statbuf); |
---|
1031 | } |
---|
1032 | /* }}} */ |
---|
1033 | static XC_RESOLVE_PATH_CHECKER(xc_resolve_path_stat_checker) /* {{{ */ |
---|
1034 | { |
---|
1035 | return xc_stat(filepath, (struct stat *) data TSRMLS_CC); |
---|
1036 | } |
---|
1037 | /* }}} */ |
---|
1038 | #ifndef ZEND_ENGINE_2_3 |
---|
1039 | static int xc_resolve_path_stat(const char *filepath, char *path_buffer, struct stat *pbuf TSRMLS_DC) /* {{{ */ |
---|
1040 | { |
---|
1041 | return xc_resolve_path(filepath, path_buffer, xc_resolve_path_stat_checker, (void *) pbuf TSRMLS_CC); |
---|
1042 | } |
---|
1043 | /* }}} */ |
---|
1044 | #endif |
---|
1045 | typedef struct xc_compiler_t { /* {{{ */ |
---|
1046 | /* XCache cached compile state */ |
---|
1047 | const char *filename; |
---|
1048 | size_t filename_len; |
---|
1049 | const char *opened_path; |
---|
1050 | char opened_path_buffer[MAXPATHLEN]; |
---|
1051 | |
---|
1052 | xc_entry_hash_t entry_hash; |
---|
1053 | xc_entry_php_t new_entry; |
---|
1054 | xc_entry_data_php_t new_php; |
---|
1055 | } xc_compiler_t; |
---|
1056 | /* }}} */ |
---|
1057 | typedef struct xc_resolve_path_entry_checker_t { /* {{{ */ |
---|
1058 | xc_compiler_t *compiler; |
---|
1059 | xc_entry_php_t **stored_entry; |
---|
1060 | } xc_resolve_path_entry_checker_data_t; |
---|
1061 | /* }}} */ |
---|
1062 | static XC_RESOLVE_PATH_CHECKER(xc_resolve_path_entry_checker) /* {{{ */ |
---|
1063 | { |
---|
1064 | xc_resolve_path_entry_checker_data_t *entry_checker_data = (xc_resolve_path_entry_checker_data_t *) data; |
---|
1065 | xc_compiler_t *compiler = entry_checker_data->compiler; |
---|
1066 | |
---|
1067 | compiler->new_entry.entry.name.str.val = xc_expand_url(filepath, compiler->opened_path_buffer TSRMLS_CC); |
---|
1068 | compiler->new_entry.entry.name.str.len = (int) strlen(compiler->new_entry.entry.name.str.val); |
---|
1069 | |
---|
1070 | *entry_checker_data->stored_entry = (xc_entry_php_t *) xc_entry_find_unlocked( |
---|
1071 | XC_TYPE_PHP |
---|
1072 | , &xc_php_caches[compiler->entry_hash.cacheid] |
---|
1073 | , compiler->entry_hash.entryslotid |
---|
1074 | , (xc_entry_t *) &compiler->new_entry |
---|
1075 | TSRMLS_CC); |
---|
1076 | |
---|
1077 | return *entry_checker_data->stored_entry ? SUCCESS : FAILURE; |
---|
1078 | } |
---|
1079 | /* }}} */ |
---|
1080 | static int xc_resolve_path_check_entry_unlocked(xc_compiler_t *compiler, const char *filepath, xc_entry_php_t **stored_entry TSRMLS_DC) /* {{{ */ |
---|
1081 | { |
---|
1082 | char path_buffer[MAXPATHLEN]; |
---|
1083 | xc_resolve_path_entry_checker_data_t entry_checker_data; |
---|
1084 | entry_checker_data.compiler = compiler; |
---|
1085 | entry_checker_data.stored_entry = stored_entry; |
---|
1086 | |
---|
1087 | return xc_resolve_path(filepath, path_buffer, xc_resolve_path_entry_checker, (void *) &entry_checker_data TSRMLS_CC); |
---|
1088 | } |
---|
1089 | /* }}} */ |
---|
1090 | static int xc_entry_php_quick_resolve_opened_path(xc_compiler_t *compiler, struct stat *statbuf TSRMLS_DC) /* {{{ */ |
---|
1091 | { |
---|
1092 | if (strcmp(SG(request_info).path_translated, compiler->filename) == 0) { |
---|
1093 | /* sapi has already done this stat() for us */ |
---|
1094 | if (statbuf) { |
---|
1095 | struct stat *sapi_stat = sapi_get_stat(TSRMLS_C); |
---|
1096 | if (!sapi_stat) { |
---|
1097 | goto giveupsapistat; |
---|
1098 | } |
---|
1099 | *statbuf = *sapi_stat; |
---|
1100 | } |
---|
1101 | |
---|
1102 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
---|
1103 | return SUCCESS; |
---|
1104 | } |
---|
1105 | giveupsapistat: |
---|
1106 | |
---|
1107 | /* absolute path */ |
---|
1108 | if (xc_is_absolute(compiler->filename, strlen(compiler->filename))) { |
---|
1109 | if (statbuf && xc_stat(compiler->filename, statbuf TSRMLS_CC) != SUCCESS) { |
---|
1110 | return FAILURE; |
---|
1111 | } |
---|
1112 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
---|
1113 | return SUCCESS; |
---|
1114 | } |
---|
1115 | |
---|
1116 | /* relative path */ |
---|
1117 | if (*compiler->filename == '.' && (IS_SLASH(compiler->filename[1]) || compiler->filename[1] == '.')) { |
---|
1118 | const char *ptr = compiler->filename + 1; |
---|
1119 | if (*ptr == '.') { |
---|
1120 | while (*(++ptr) == '.'); |
---|
1121 | if (!IS_SLASH(*ptr)) { |
---|
1122 | return FAILURE; |
---|
1123 | } |
---|
1124 | } |
---|
1125 | |
---|
1126 | if (statbuf && VCWD_STAT(compiler->filename, statbuf) != 0) { |
---|
1127 | return FAILURE; |
---|
1128 | } |
---|
1129 | |
---|
1130 | compiler->opened_path = xc_expand_url(compiler->filename, compiler->opened_path_buffer TSRMLS_CC); |
---|
1131 | return SUCCESS; |
---|
1132 | } |
---|
1133 | |
---|
1134 | return FAILURE; |
---|
1135 | } |
---|
1136 | /* }}} */ |
---|
1137 | static int xc_entry_php_resolve_opened_path(xc_compiler_t *compiler, struct stat *statbuf TSRMLS_DC) /* {{{ */ |
---|
1138 | { |
---|
1139 | if (xc_entry_php_quick_resolve_opened_path(compiler, statbuf TSRMLS_CC) == SUCCESS) { |
---|
1140 | /* opened_path resolved */ |
---|
1141 | return SUCCESS; |
---|
1142 | } |
---|
1143 | /* fall back to real stat call */ |
---|
1144 | else { |
---|
1145 | #ifdef ZEND_ENGINE_2_3 |
---|
1146 | char *opened_path = php_resolve_path(compiler->filename, (int) compiler->filename_len, PG(include_path) TSRMLS_CC); |
---|
1147 | if (opened_path) { |
---|
1148 | strcpy(compiler->opened_path_buffer, opened_path); |
---|
1149 | efree(opened_path); |
---|
1150 | compiler->opened_path = compiler->opened_path_buffer; |
---|
1151 | if (!statbuf || xc_stat(compiler->opened_path, statbuf TSRMLS_CC) == SUCCESS) { |
---|
1152 | return SUCCESS; |
---|
1153 | } |
---|
1154 | } |
---|
1155 | #else |
---|
1156 | char path_buffer[MAXPATHLEN]; |
---|
1157 | if (xc_resolve_path_stat(compiler->filename, path_buffer, statbuf TSRMLS_CC) == SUCCESS) { |
---|
1158 | compiler->opened_path = xc_expand_url(path_buffer, compiler->opened_path_buffer TSRMLS_CC); |
---|
1159 | return SUCCESS; |
---|
1160 | } |
---|
1161 | #endif |
---|
1162 | } |
---|
1163 | return FAILURE; |
---|
1164 | } |
---|
1165 | /* }}} */ |
---|
1166 | static int xc_entry_php_init_key(xc_compiler_t *compiler TSRMLS_DC) /* {{{ */ |
---|
1167 | { |
---|
1168 | if (XG(stat)) { |
---|
1169 | struct stat buf; |
---|
1170 | time_t delta; |
---|
1171 | |
---|
1172 | if (compiler->opened_path) { |
---|
1173 | if (xc_stat(compiler->opened_path, &buf TSRMLS_CC) != SUCCESS) { |
---|
1174 | return FAILURE; |
---|
1175 | } |
---|
1176 | } |
---|
1177 | else { |
---|
1178 | if (xc_entry_php_resolve_opened_path(compiler, &buf TSRMLS_CC) != SUCCESS) { |
---|
1179 | return FAILURE; |
---|
1180 | } |
---|
1181 | } |
---|
1182 | |
---|
1183 | delta = XG(request_time) - buf.st_mtime; |
---|
1184 | if (abs((int) delta) < 2 && !xc_test) { |
---|
1185 | return FAILURE; |
---|
1186 | } |
---|
1187 | |
---|
1188 | compiler->new_entry.file_mtime = buf.st_mtime; |
---|
1189 | compiler->new_entry.file_size = buf.st_size; |
---|
1190 | compiler->new_entry.file_device = buf.st_dev; |
---|
1191 | compiler->new_entry.file_inode = buf.st_ino; |
---|
1192 | } |
---|
1193 | else { |
---|
1194 | xc_entry_php_quick_resolve_opened_path(compiler, NULL TSRMLS_CC); |
---|
1195 | |
---|
1196 | compiler->new_entry.file_mtime = 0; |
---|
1197 | compiler->new_entry.file_size = 0; |
---|
1198 | compiler->new_entry.file_device = 0; |
---|
1199 | compiler->new_entry.file_inode = 0; |
---|
1200 | } |
---|
1201 | |
---|
1202 | { |
---|
1203 | xc_hash_value_t basename_hash_value; |
---|
1204 | if (xc_php_hcache.size > 1 |
---|
1205 | || !compiler->new_entry.file_inode) { |
---|
1206 | const char *filename_end = compiler->filename + compiler->filename_len; |
---|
1207 | const char *basename_begin = filename_end - 1; |
---|
1208 | |
---|
1209 | /* scan till out of basename part */ |
---|
1210 | while (basename_begin >= compiler->filename && !IS_SLASH(*basename_begin)) { |
---|
1211 | --basename_begin; |
---|
1212 | } |
---|
1213 | /* get back to basename_begin */ |
---|
1214 | ++basename_begin; |
---|
1215 | |
---|
1216 | basename_hash_value = HASH_STR_L(basename_begin, (uint) (filename_end - basename_begin)); |
---|
1217 | } |
---|
1218 | |
---|
1219 | compiler->entry_hash.cacheid = xc_php_hcache.size > 1 ? xc_hash_fold(basename_hash_value, &xc_php_hcache) : 0; |
---|
1220 | compiler->entry_hash.entryslotid = xc_hash_fold( |
---|
1221 | compiler->new_entry.file_inode |
---|
1222 | ? (xc_hash_value_t) HASH(compiler->new_entry.file_device + compiler->new_entry.file_inode) |
---|
1223 | : basename_hash_value |
---|
1224 | , &xc_php_hentry); |
---|
1225 | } |
---|
1226 | |
---|
1227 | compiler->new_entry.filepath = NULL; |
---|
1228 | compiler->new_entry.dirpath = NULL; |
---|
1229 | #ifdef IS_UNICODE |
---|
1230 | compiler->new_entry.ufilepath = NULL; |
---|
1231 | compiler->new_entry.udirpath = NULL; |
---|
1232 | #endif |
---|
1233 | |
---|
1234 | return SUCCESS; |
---|
1235 | } |
---|
1236 | /* }}} */ |
---|
1237 | static inline xc_hash_value_t xc_php_hash_md5(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
1238 | { |
---|
1239 | return HASH_STR_S(php->md5.digest, sizeof(php->md5.digest)); |
---|
1240 | } |
---|
1241 | /* }}} */ |
---|
1242 | static int xc_entry_data_php_init_md5(xc_cache_t *cache, xc_compiler_t *compiler TSRMLS_DC) /* {{{ */ |
---|
1243 | { |
---|
1244 | unsigned char buf[1024]; |
---|
1245 | PHP_MD5_CTX context; |
---|
1246 | int n; |
---|
1247 | php_stream *stream; |
---|
1248 | ulong old_rsid = EG(regular_list).nNextFreeElement; |
---|
1249 | |
---|
1250 | stream = php_stream_open_wrapper((char *) compiler->filename, "rb", USE_PATH | REPORT_ERRORS | ENFORCE_SAFE_MODE | STREAM_DISABLE_OPEN_BASEDIR, NULL); |
---|
1251 | if (!stream) { |
---|
1252 | return FAILURE; |
---|
1253 | } |
---|
1254 | |
---|
1255 | PHP_MD5Init(&context); |
---|
1256 | while ((n = php_stream_read(stream, (char *) buf, (int) sizeof(buf))) > 0) { |
---|
1257 | PHP_MD5Update(&context, buf, n); |
---|
1258 | } |
---|
1259 | PHP_MD5Final((unsigned char *) compiler->new_php.md5.digest, &context); |
---|
1260 | |
---|
1261 | php_stream_close(stream); |
---|
1262 | if (EG(regular_list).nNextFreeElement == old_rsid + 1) { |
---|
1263 | EG(regular_list).nNextFreeElement = old_rsid; |
---|
1264 | } |
---|
1265 | |
---|
1266 | if (n < 0) { |
---|
1267 | return FAILURE; |
---|
1268 | } |
---|
1269 | |
---|
1270 | compiler->new_php.hvalue = (xc_php_hash_md5(&compiler->new_php TSRMLS_CC) & cache->hphp->mask); |
---|
1271 | #ifdef XCACHE_DEBUG |
---|
1272 | { |
---|
1273 | char md5str[33]; |
---|
1274 | make_digest(md5str, (unsigned char *) compiler->new_php.md5.digest); |
---|
1275 | TRACE("md5 %s", md5str); |
---|
1276 | } |
---|
1277 | #endif |
---|
1278 | |
---|
1279 | return SUCCESS; |
---|
1280 | } |
---|
1281 | /* }}} */ |
---|
1282 | static void xc_entry_php_init(xc_entry_php_t *entry_php, const char *filepath TSRMLS_DC) /* {{{*/ |
---|
1283 | { |
---|
1284 | entry_php->filepath = ZEND_24((char *), NOTHING) filepath; |
---|
1285 | entry_php->filepath_len = strlen(entry_php->filepath); |
---|
1286 | entry_php->dirpath = estrndup(entry_php->filepath, entry_php->filepath_len); |
---|
1287 | entry_php->dirpath_len = zend_dirname(entry_php->dirpath, entry_php->filepath_len); |
---|
1288 | #ifdef IS_UNICODE |
---|
1289 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &entry_php->ufilepath, &entry_php->ufilepath_len, entry_php->filepath, entry_php->filepath_len TSRMLS_CC); |
---|
1290 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &entry_php->udirpath, &entry_php->udirpath_len, entry_php->dirpath, entry_php->dirpath_len TSRMLS_CC); |
---|
1291 | #endif |
---|
1292 | } |
---|
1293 | /* }}} */ |
---|
1294 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
1295 | static void xc_cache_early_binding_class_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
---|
1296 | { |
---|
1297 | char *class_name; |
---|
1298 | zend_uint i; |
---|
1299 | int class_len; |
---|
1300 | xc_cest_t cest; |
---|
1301 | xc_entry_data_php_t *php = (xc_entry_data_php_t *) data; |
---|
1302 | |
---|
1303 | class_name = Z_OP_CONSTANT(opline->op1).value.str.val; |
---|
1304 | class_len = Z_OP_CONSTANT(opline->op1).value.str.len; |
---|
1305 | if (zend_hash_find(CG(class_table), class_name, class_len, (void **) &cest) == FAILURE) { |
---|
1306 | assert(0); |
---|
1307 | } |
---|
1308 | TRACE("got ZEND_DECLARE_INHERITED_CLASS: %s", class_name + 1); |
---|
1309 | /* let's see which class */ |
---|
1310 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1311 | if (memcmp(ZSTR_S(php->classinfos[i].key), class_name, class_len) == 0) { |
---|
1312 | php->classinfos[i].oplineno = oplineno; |
---|
1313 | php->have_early_binding = 1; |
---|
1314 | break; |
---|
1315 | } |
---|
1316 | } |
---|
1317 | |
---|
1318 | if (i == php->classinfo_cnt) { |
---|
1319 | assert(0); |
---|
1320 | } |
---|
1321 | } |
---|
1322 | /* }}} */ |
---|
1323 | #endif |
---|
1324 | |
---|
1325 | /* {{{ Constant Usage */ |
---|
1326 | #ifdef ZEND_ENGINE_2_4 |
---|
1327 | # define xcache_literal_is_dir 1 |
---|
1328 | # define xcache_literal_is_file 2 |
---|
1329 | #else |
---|
1330 | # define xcache_op1_is_file 1 |
---|
1331 | # define xcache_op1_is_dir 2 |
---|
1332 | # define xcache_op2_is_file 4 |
---|
1333 | # define xcache_op2_is_dir 8 |
---|
1334 | #endif |
---|
1335 | typedef struct { |
---|
1336 | zend_bool filepath_used; |
---|
1337 | zend_bool dirpath_used; |
---|
1338 | zend_bool ufilepath_used; |
---|
1339 | zend_bool udirpath_used; |
---|
1340 | } xc_const_usage_t; |
---|
1341 | /* }}} */ |
---|
1342 | static void xc_collect_op_array_info(xc_compiler_t *compiler, xc_const_usage_t *usage, xc_op_array_info_t *op_array_info, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
1343 | { |
---|
1344 | #ifdef ZEND_ENGINE_2_4 |
---|
1345 | int literalindex; |
---|
1346 | #else |
---|
1347 | zend_uint oplinenum; |
---|
1348 | #endif |
---|
1349 | xc_vector_t details; |
---|
1350 | |
---|
1351 | xc_vector_init(xc_op_array_info_detail_t, &details); |
---|
1352 | |
---|
1353 | #define XCACHE_ANALYZE_LITERAL(type) \ |
---|
1354 | if (zend_binary_strcmp(Z_STRVAL(literal->constant), Z_STRLEN(literal->constant), compiler->new_entry.type##path, compiler->new_entry.type##path_len) == 0) { \ |
---|
1355 | usage->type##path_used = 1; \ |
---|
1356 | literalinfo |= xcache_##literal##_is_##type; \ |
---|
1357 | } |
---|
1358 | |
---|
1359 | #define XCACHE_U_ANALYZE_LITERAL(type) \ |
---|
1360 | if (zend_u_##binary_strcmp(Z_USTRVAL(literal->constant), Z_USTRLEN(literal->constant), compiler->new_entry.u##type##path, compiler->new_entry.u##type##path_len) == 0) { \ |
---|
1361 | usage->u##type##path_used = 1; \ |
---|
1362 | literalinfo |= xcache_##literal##_is_##type; \ |
---|
1363 | } |
---|
1364 | |
---|
1365 | #define XCACHE_ANALYZE_OP(type, op) \ |
---|
1366 | if (zend_binary_strcmp(Z_STRVAL(Z_OP_CONSTANT(opline->op)), Z_STRLEN(Z_OP_CONSTANT(opline->op)), compiler->new_entry.type##path, compiler->new_entry.type##path_len) == 0) { \ |
---|
1367 | usage->type##path_used = 1; \ |
---|
1368 | oplineinfo |= xcache_##op##_is_##type; \ |
---|
1369 | } |
---|
1370 | |
---|
1371 | #define XCACHE_U_ANALYZE_OP(type, op) \ |
---|
1372 | if (zend_u_##binary_strcmp(Z_USTRVAL(Z_OP_CONSTANT(opline->op)), Z_USTRLEN(Z_OP_CONSTANT(opline->op)), compiler->new_entry.u##type##path, compiler->new_entry.u##type##path_len) == 0) { \ |
---|
1373 | usage->u##type##path_used = 1; \ |
---|
1374 | oplineinfo |= xcache_##op##_is_##type; \ |
---|
1375 | } |
---|
1376 | |
---|
1377 | #ifdef ZEND_ENGINE_2_4 |
---|
1378 | for (literalindex = 0; literalindex < op_array->last_literal; literalindex++) { |
---|
1379 | zend_literal *literal = &op_array->literals[literalindex]; |
---|
1380 | zend_uint literalinfo = 0; |
---|
1381 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1382 | XCACHE_ANALYZE_LITERAL(file) |
---|
1383 | else XCACHE_ANALYZE_LITERAL(dir) |
---|
1384 | } |
---|
1385 | #ifdef IS_UNICODE |
---|
1386 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1387 | XCACHE_U_ANALYZE_LITERAL(file) |
---|
1388 | else XCACHE_U_ANALYZE_LITERAL(dir) |
---|
1389 | } |
---|
1390 | #endif |
---|
1391 | if (literalinfo) { |
---|
1392 | xc_op_array_info_detail_t detail; |
---|
1393 | detail.index = literalindex; |
---|
1394 | detail.info = literalinfo; |
---|
1395 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
---|
1396 | } |
---|
1397 | } |
---|
1398 | |
---|
1399 | op_array_info->literalinfo_cnt = details.cnt; |
---|
1400 | op_array_info->literalinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
---|
1401 | #else /* ZEND_ENGINE_2_4 */ |
---|
1402 | for (oplinenum = 0; oplinenum < op_array->last; oplinenum++) { |
---|
1403 | zend_op *opline = &op_array->opcodes[oplinenum]; |
---|
1404 | zend_uint oplineinfo = 0; |
---|
1405 | if (Z_OP_TYPE(opline->op1) == IS_CONST) { |
---|
1406 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1407 | XCACHE_ANALYZE_OP(file, op1) |
---|
1408 | else XCACHE_ANALYZE_OP(dir, op1) |
---|
1409 | } |
---|
1410 | #ifdef IS_UNICODE |
---|
1411 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1412 | XCACHE_U_ANALYZE_OP(file, op1) |
---|
1413 | else XCACHE_U_ANALYZE_OP(dir, op1) |
---|
1414 | } |
---|
1415 | #endif |
---|
1416 | } |
---|
1417 | |
---|
1418 | if (Z_OP_TYPE(opline->op2) == IS_CONST) { |
---|
1419 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1420 | XCACHE_ANALYZE_OP(file, op2) |
---|
1421 | else XCACHE_ANALYZE_OP(dir, op2) |
---|
1422 | } |
---|
1423 | #ifdef IS_UNICODE |
---|
1424 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1425 | XCACHE_U_ANALYZE_OP(file, op2) |
---|
1426 | else XCACHE_U_ANALYZE_OP(dir, op2) |
---|
1427 | } |
---|
1428 | #endif |
---|
1429 | } |
---|
1430 | |
---|
1431 | if (oplineinfo) { |
---|
1432 | xc_op_array_info_detail_t detail; |
---|
1433 | detail.index = oplinenum; |
---|
1434 | detail.info = oplineinfo; |
---|
1435 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
---|
1436 | } |
---|
1437 | } |
---|
1438 | |
---|
1439 | op_array_info->oplineinfo_cnt = details.cnt; |
---|
1440 | op_array_info->oplineinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
---|
1441 | #endif /* ZEND_ENGINE_2_4 */ |
---|
1442 | xc_vector_free(xc_op_array_info_detail_t, &details); |
---|
1443 | } |
---|
1444 | /* }}} */ |
---|
1445 | void xc_fix_op_array_info(const xc_entry_php_t *entry_php, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copy, const xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
---|
1446 | { |
---|
1447 | #ifdef ZEND_ENGINE_2_4 |
---|
1448 | zend_uint literalinfoindex; |
---|
1449 | |
---|
1450 | for (literalinfoindex = 0; literalinfoindex < op_array_info->literalinfo_cnt; ++literalinfoindex) { |
---|
1451 | int literalindex = op_array_info->literalinfos[literalinfoindex].index; |
---|
1452 | int literalinfo = op_array_info->literalinfos[literalinfoindex].info; |
---|
1453 | zend_literal *literal = &op_array->literals[literalindex]; |
---|
1454 | if ((literalinfo & xcache_literal_is_file)) { |
---|
1455 | if (!shallow_copy) { |
---|
1456 | efree(Z_STRVAL(literal->constant)); |
---|
1457 | } |
---|
1458 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1459 | assert(entry_php->filepath); |
---|
1460 | ZVAL_STRINGL(&literal->constant, entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
---|
1461 | TRACE("restored literal constant: %s", entry_php->filepath); |
---|
1462 | } |
---|
1463 | #ifdef IS_UNICODE |
---|
1464 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1465 | assert(entry_php->ufilepath); |
---|
1466 | ZVAL_UNICODEL(&literal->constant, entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
---|
1467 | } |
---|
1468 | #endif |
---|
1469 | else { |
---|
1470 | assert(0); |
---|
1471 | } |
---|
1472 | } |
---|
1473 | else if ((literalinfo & xcache_literal_is_dir)) { |
---|
1474 | if (!shallow_copy) { |
---|
1475 | efree(Z_STRVAL(literal->constant)); |
---|
1476 | } |
---|
1477 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1478 | assert(entry_php->dirpath); |
---|
1479 | TRACE("restored literal constant: %s", entry_php->dirpath); |
---|
1480 | ZVAL_STRINGL(&literal->constant, entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
---|
1481 | } |
---|
1482 | #ifdef IS_UNICODE |
---|
1483 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1484 | assert(!entry_php->udirpath); |
---|
1485 | ZVAL_UNICODEL(&literal->constant, entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
---|
1486 | } |
---|
1487 | #endif |
---|
1488 | else { |
---|
1489 | assert(0); |
---|
1490 | } |
---|
1491 | } |
---|
1492 | } |
---|
1493 | #else /* ZEND_ENGINE_2_4 */ |
---|
1494 | zend_uint oplinenum; |
---|
1495 | |
---|
1496 | for (oplinenum = 0; oplinenum < op_array_info->oplineinfo_cnt; ++oplinenum) { |
---|
1497 | int oplineindex = op_array_info->oplineinfos[oplinenum].index; |
---|
1498 | int oplineinfo = op_array_info->oplineinfos[oplinenum].info; |
---|
1499 | zend_op *opline = &op_array->opcodes[oplineindex]; |
---|
1500 | if ((oplineinfo & xcache_op1_is_file)) { |
---|
1501 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
---|
1502 | if (!shallow_copy) { |
---|
1503 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
---|
1504 | } |
---|
1505 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1506 | assert(entry_php->filepath); |
---|
1507 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
---|
1508 | TRACE("restored op1 constant: %s", entry_php->filepath); |
---|
1509 | } |
---|
1510 | #ifdef IS_UNICODE |
---|
1511 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1512 | assert(entry_php->ufilepath); |
---|
1513 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
---|
1514 | } |
---|
1515 | #endif |
---|
1516 | else { |
---|
1517 | assert(0); |
---|
1518 | } |
---|
1519 | } |
---|
1520 | else if ((oplineinfo & xcache_op1_is_dir)) { |
---|
1521 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
---|
1522 | if (!shallow_copy) { |
---|
1523 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
---|
1524 | } |
---|
1525 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1526 | assert(entry_php->dirpath); |
---|
1527 | TRACE("restored op1 constant: %s", entry_php->dirpath); |
---|
1528 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
---|
1529 | } |
---|
1530 | #ifdef IS_UNICODE |
---|
1531 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1532 | assert(!entry_php->udirpath); |
---|
1533 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
---|
1534 | } |
---|
1535 | #endif |
---|
1536 | else { |
---|
1537 | assert(0); |
---|
1538 | } |
---|
1539 | } |
---|
1540 | |
---|
1541 | if ((oplineinfo & xcache_op2_is_file)) { |
---|
1542 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
---|
1543 | if (!shallow_copy) { |
---|
1544 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
---|
1545 | } |
---|
1546 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1547 | assert(entry_php->filepath); |
---|
1548 | TRACE("restored op2 constant: %s", entry_php->filepath); |
---|
1549 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), entry_php->filepath, entry_php->filepath_len, !shallow_copy); |
---|
1550 | } |
---|
1551 | #ifdef IS_UNICODE |
---|
1552 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1553 | assert(entry_php->ufilepath); |
---|
1554 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), entry_php->ufilepath, entry_php->ufilepath_len, !shallow_copy); |
---|
1555 | } |
---|
1556 | #endif |
---|
1557 | else { |
---|
1558 | assert(0); |
---|
1559 | } |
---|
1560 | } |
---|
1561 | else if ((oplineinfo & xcache_op2_is_dir)) { |
---|
1562 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
---|
1563 | if (!shallow_copy) { |
---|
1564 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
---|
1565 | } |
---|
1566 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1567 | assert(entry_php->dirpath); |
---|
1568 | TRACE("restored op2 constant: %s", entry_php->dirpath); |
---|
1569 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), entry_php->dirpath, entry_php->dirpath_len, !shallow_copy); |
---|
1570 | } |
---|
1571 | #ifdef IS_UNICODE |
---|
1572 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1573 | assert(entry_php->udirpath); |
---|
1574 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), entry_php->udirpath, entry_php->udirpath_len, !shallow_copy); |
---|
1575 | } |
---|
1576 | #endif |
---|
1577 | else { |
---|
1578 | assert(0); |
---|
1579 | } |
---|
1580 | } |
---|
1581 | } |
---|
1582 | #endif /* ZEND_ENGINE_2_4 */ |
---|
1583 | } |
---|
1584 | /* }}} */ |
---|
1585 | static void xc_free_op_array_info(xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
---|
1586 | { |
---|
1587 | #ifdef ZEND_ENGINE_2_4 |
---|
1588 | if (op_array_info->literalinfos) { |
---|
1589 | efree(op_array_info->literalinfos); |
---|
1590 | } |
---|
1591 | #else |
---|
1592 | if (op_array_info->oplineinfos) { |
---|
1593 | efree(op_array_info->oplineinfos); |
---|
1594 | } |
---|
1595 | #endif |
---|
1596 | } |
---|
1597 | /* }}} */ |
---|
1598 | static void xc_free_php(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
1599 | { |
---|
1600 | zend_uint i; |
---|
1601 | if (php->classinfos) { |
---|
1602 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1603 | xc_classinfo_t *classinfo = &php->classinfos[i]; |
---|
1604 | zend_uint j; |
---|
1605 | |
---|
1606 | for (j = 0; j < classinfo->methodinfo_cnt; j ++) { |
---|
1607 | xc_free_op_array_info(&classinfo->methodinfos[j] TSRMLS_CC); |
---|
1608 | } |
---|
1609 | |
---|
1610 | if (classinfo->methodinfos) { |
---|
1611 | efree(classinfo->methodinfos); |
---|
1612 | } |
---|
1613 | } |
---|
1614 | } |
---|
1615 | if (php->funcinfos) { |
---|
1616 | for (i = 0; i < php->funcinfo_cnt; i ++) { |
---|
1617 | xc_free_op_array_info(&php->funcinfos[i].op_array_info TSRMLS_CC); |
---|
1618 | } |
---|
1619 | } |
---|
1620 | xc_free_op_array_info(&php->op_array_info TSRMLS_CC); |
---|
1621 | |
---|
1622 | #define X_FREE(var) do {\ |
---|
1623 | if (php->var) { \ |
---|
1624 | efree(php->var); \ |
---|
1625 | } \ |
---|
1626 | } while (0) |
---|
1627 | |
---|
1628 | #ifdef ZEND_ENGINE_2_1 |
---|
1629 | X_FREE(autoglobals); |
---|
1630 | #endif |
---|
1631 | X_FREE(classinfos); |
---|
1632 | X_FREE(funcinfos); |
---|
1633 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1634 | X_FREE(constinfos); |
---|
1635 | #endif |
---|
1636 | #undef X_FREE |
---|
1637 | } |
---|
1638 | /* }}} */ |
---|
1639 | static void xc_compile_php(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
1640 | { |
---|
1641 | zend_uint old_constinfo_cnt, old_funcinfo_cnt, old_classinfo_cnt; |
---|
1642 | zend_bool catched = 0; |
---|
1643 | |
---|
1644 | /* {{{ compile */ |
---|
1645 | TRACE("compiling %s", h->opened_path ? h->opened_path : h->filename); |
---|
1646 | |
---|
1647 | old_classinfo_cnt = zend_hash_num_elements(CG(class_table)); |
---|
1648 | old_funcinfo_cnt = zend_hash_num_elements(CG(function_table)); |
---|
1649 | old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants)); |
---|
1650 | |
---|
1651 | zend_try { |
---|
1652 | compiler->new_php.op_array = old_compile_file(h, type TSRMLS_CC); |
---|
1653 | } zend_catch { |
---|
1654 | catched = 1; |
---|
1655 | } zend_end_try(); |
---|
1656 | |
---|
1657 | if (catched) { |
---|
1658 | goto err_bailout; |
---|
1659 | } |
---|
1660 | |
---|
1661 | if (compiler->new_php.op_array == NULL) { |
---|
1662 | goto err_op_array; |
---|
1663 | } |
---|
1664 | |
---|
1665 | if (!XG(initial_compile_file_called)) { |
---|
1666 | TRACE("%s", "!initial_compile_file_called, give up"); |
---|
1667 | return; |
---|
1668 | } |
---|
1669 | |
---|
1670 | /* }}} */ |
---|
1671 | /* {{{ prepare */ |
---|
1672 | zend_restore_compiled_filename(h->opened_path ? h->opened_path : (char *) h->filename TSRMLS_CC); |
---|
1673 | |
---|
1674 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1675 | compiler->new_php.constinfo_cnt = zend_hash_num_elements(EG(zend_constants)) - old_constinfo_cnt; |
---|
1676 | #endif |
---|
1677 | compiler->new_php.funcinfo_cnt = zend_hash_num_elements(CG(function_table)) - old_funcinfo_cnt; |
---|
1678 | compiler->new_php.classinfo_cnt = zend_hash_num_elements(CG(class_table)) - old_classinfo_cnt; |
---|
1679 | #ifdef ZEND_ENGINE_2_1 |
---|
1680 | /* {{{ count new_php.autoglobal_cnt */ { |
---|
1681 | Bucket *b; |
---|
1682 | |
---|
1683 | compiler->new_php.autoglobal_cnt = 0; |
---|
1684 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
---|
1685 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
1686 | /* check if actived */ |
---|
1687 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
1688 | compiler->new_php.autoglobal_cnt ++; |
---|
1689 | } |
---|
1690 | } |
---|
1691 | } |
---|
1692 | /* }}} */ |
---|
1693 | #endif |
---|
1694 | |
---|
1695 | #define X_ALLOC_N(var, cnt) do { \ |
---|
1696 | if (compiler->new_php.cnt) { \ |
---|
1697 | ECALLOC_N(compiler->new_php.var, compiler->new_php.cnt); \ |
---|
1698 | if (!compiler->new_php.var) { \ |
---|
1699 | goto err_alloc; \ |
---|
1700 | } \ |
---|
1701 | } \ |
---|
1702 | else { \ |
---|
1703 | compiler->new_php.var = NULL; \ |
---|
1704 | } \ |
---|
1705 | } while (0) |
---|
1706 | |
---|
1707 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1708 | X_ALLOC_N(constinfos, constinfo_cnt); |
---|
1709 | #endif |
---|
1710 | X_ALLOC_N(funcinfos, funcinfo_cnt); |
---|
1711 | X_ALLOC_N(classinfos, classinfo_cnt); |
---|
1712 | #ifdef ZEND_ENGINE_2_1 |
---|
1713 | X_ALLOC_N(autoglobals, autoglobal_cnt); |
---|
1714 | #endif |
---|
1715 | #undef X_ALLOC |
---|
1716 | /* }}} */ |
---|
1717 | |
---|
1718 | /* {{{ shallow copy, pointers only */ { |
---|
1719 | Bucket *b; |
---|
1720 | zend_uint i; |
---|
1721 | zend_uint j; |
---|
1722 | |
---|
1723 | #define COPY_H(vartype, var, cnt, name, datatype) do { \ |
---|
1724 | for (i = 0, j = 0; b; i ++, b = b->pListNext) { \ |
---|
1725 | vartype *data = &compiler->new_php.var[j]; \ |
---|
1726 | \ |
---|
1727 | if (i < old_##cnt) { \ |
---|
1728 | continue; \ |
---|
1729 | } \ |
---|
1730 | j ++; \ |
---|
1731 | \ |
---|
1732 | assert(i < old_##cnt + compiler->new_php.cnt); \ |
---|
1733 | assert(b->pData); \ |
---|
1734 | memcpy(&data->name, b->pData, sizeof(datatype)); \ |
---|
1735 | UNISW(NOTHING, data->type = b->key.type;) \ |
---|
1736 | if (UNISW(1, b->key.type == IS_STRING)) { \ |
---|
1737 | ZSTR_S(data->key) = BUCKET_KEY_S(b); \ |
---|
1738 | } \ |
---|
1739 | else { \ |
---|
1740 | ZSTR_U(data->key) = BUCKET_KEY_U(b); \ |
---|
1741 | } \ |
---|
1742 | data->key_size = b->nKeyLength; \ |
---|
1743 | data->h = b->h; \ |
---|
1744 | } \ |
---|
1745 | } while(0) |
---|
1746 | |
---|
1747 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1748 | b = EG(zend_constants)->pListHead; COPY_H(xc_constinfo_t, constinfos, constinfo_cnt, constant, zend_constant); |
---|
1749 | #endif |
---|
1750 | b = CG(function_table)->pListHead; COPY_H(xc_funcinfo_t, funcinfos, funcinfo_cnt, func, zend_function); |
---|
1751 | b = CG(class_table)->pListHead; COPY_H(xc_classinfo_t, classinfos, classinfo_cnt, cest, xc_cest_t); |
---|
1752 | |
---|
1753 | #undef COPY_H |
---|
1754 | |
---|
1755 | /* for ZE1, cest need to be fixed inside store */ |
---|
1756 | |
---|
1757 | #ifdef ZEND_ENGINE_2_1 |
---|
1758 | /* scan for acatived auto globals */ |
---|
1759 | i = 0; |
---|
1760 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
---|
1761 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
1762 | /* check if actived */ |
---|
1763 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
1764 | xc_autoglobal_t *data = &compiler->new_php.autoglobals[i]; |
---|
1765 | |
---|
1766 | assert(i < compiler->new_php.autoglobal_cnt); |
---|
1767 | i ++; |
---|
1768 | UNISW(NOTHING, data->type = b->key.type;) |
---|
1769 | if (UNISW(1, b->key.type == IS_STRING)) { |
---|
1770 | ZSTR_S(data->key) = BUCKET_KEY_S(b); |
---|
1771 | } |
---|
1772 | else { |
---|
1773 | ZSTR_U(data->key) = BUCKET_KEY_U(b); |
---|
1774 | } |
---|
1775 | data->key_len = b->nKeyLength - 1; |
---|
1776 | data->h = b->h; |
---|
1777 | } |
---|
1778 | } |
---|
1779 | #endif |
---|
1780 | } |
---|
1781 | /* }}} */ |
---|
1782 | |
---|
1783 | /* {{{ collect info for file/dir path */ { |
---|
1784 | Bucket *b; |
---|
1785 | xc_const_usage_t const_usage; |
---|
1786 | unsigned int i; |
---|
1787 | |
---|
1788 | xc_entry_php_init(&compiler->new_entry, zend_get_compiled_filename(TSRMLS_C) TSRMLS_CC); |
---|
1789 | memset(&const_usage, 0, sizeof(const_usage)); |
---|
1790 | |
---|
1791 | for (i = 0; i < compiler->new_php.classinfo_cnt; i ++) { |
---|
1792 | xc_classinfo_t *classinfo = &compiler->new_php.classinfos[i]; |
---|
1793 | zend_class_entry *ce = CestToCePtr(classinfo->cest); |
---|
1794 | classinfo->methodinfo_cnt = ce->function_table.nTableSize; |
---|
1795 | if (classinfo->methodinfo_cnt) { |
---|
1796 | int j; |
---|
1797 | |
---|
1798 | ECALLOC_N(classinfo->methodinfos, classinfo->methodinfo_cnt); |
---|
1799 | if (!classinfo->methodinfos) { |
---|
1800 | goto err_alloc; |
---|
1801 | } |
---|
1802 | |
---|
1803 | for (j = 0, b = ce->function_table.pListHead; b; j ++, b = b->pListNext) { |
---|
1804 | xc_collect_op_array_info(compiler, &const_usage, &classinfo->methodinfos[j], (zend_op_array *) b->pData TSRMLS_CC); |
---|
1805 | } |
---|
1806 | } |
---|
1807 | else { |
---|
1808 | classinfo->methodinfos = NULL; |
---|
1809 | } |
---|
1810 | } |
---|
1811 | |
---|
1812 | for (i = 0; i < compiler->new_php.funcinfo_cnt; i ++) { |
---|
1813 | xc_collect_op_array_info(compiler, &const_usage, &compiler->new_php.funcinfos[i].op_array_info, (zend_op_array *) &compiler->new_php.funcinfos[i].func TSRMLS_CC); |
---|
1814 | } |
---|
1815 | |
---|
1816 | xc_collect_op_array_info(compiler, &const_usage, &compiler->new_php.op_array_info, compiler->new_php.op_array TSRMLS_CC); |
---|
1817 | |
---|
1818 | /* file/dir path free unused */ |
---|
1819 | #define X_FREE_UNUSED(var) \ |
---|
1820 | if (!const_usage.var##path_used) { \ |
---|
1821 | efree(compiler->new_entry.var##path); \ |
---|
1822 | compiler->new_entry.var##path = NULL; \ |
---|
1823 | compiler->new_entry.var##path_len = 0; \ |
---|
1824 | } |
---|
1825 | /* filepath is required to restore op_array->filename, so no free filepath here */ |
---|
1826 | X_FREE_UNUSED(dir) |
---|
1827 | #ifdef IS_UNICODE |
---|
1828 | X_FREE_UNUSED(ufile) |
---|
1829 | X_FREE_UNUSED(udir) |
---|
1830 | #endif |
---|
1831 | #undef X_FREE_UNUSED |
---|
1832 | } |
---|
1833 | /* }}} */ |
---|
1834 | #ifdef XCACHE_ERROR_CACHING |
---|
1835 | compiler->new_php.compilererrors = xc_sandbox_compilererrors(TSRMLS_C); |
---|
1836 | compiler->new_php.compilererror_cnt = xc_sandbox_compilererror_cnt(TSRMLS_C); |
---|
1837 | #endif |
---|
1838 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
1839 | /* {{{ find inherited classes that should be early-binding */ |
---|
1840 | compiler->new_php.have_early_binding = 0; |
---|
1841 | { |
---|
1842 | zend_uint i; |
---|
1843 | for (i = 0; i < compiler->new_php.classinfo_cnt; i ++) { |
---|
1844 | compiler->new_php.classinfos[i].oplineno = -1; |
---|
1845 | } |
---|
1846 | } |
---|
1847 | |
---|
1848 | xc_undo_pass_two(compiler->new_php.op_array TSRMLS_CC); |
---|
1849 | xc_foreach_early_binding_class(compiler->new_php.op_array, xc_cache_early_binding_class_cb, (void *) &compiler->new_php TSRMLS_CC); |
---|
1850 | xc_redo_pass_two(compiler->new_php.op_array TSRMLS_CC); |
---|
1851 | /* }}} */ |
---|
1852 | #endif |
---|
1853 | |
---|
1854 | return; |
---|
1855 | |
---|
1856 | err_alloc: |
---|
1857 | xc_free_php(&compiler->new_php TSRMLS_CC); |
---|
1858 | |
---|
1859 | err_bailout: |
---|
1860 | err_op_array: |
---|
1861 | |
---|
1862 | if (catched) { |
---|
1863 | zend_bailout(); |
---|
1864 | } |
---|
1865 | } |
---|
1866 | /* }}} */ |
---|
1867 | static zend_op_array *xc_compile_restore(xc_entry_php_t *stored_entry, xc_entry_data_php_t *stored_php TSRMLS_DC) /* {{{ */ |
---|
1868 | { |
---|
1869 | zend_op_array *op_array; |
---|
1870 | xc_entry_php_t restored_entry; |
---|
1871 | xc_entry_data_php_t restored_php; |
---|
1872 | zend_bool catched; |
---|
1873 | zend_uint i; |
---|
1874 | |
---|
1875 | /* still needed because in zend_language_scanner.l, require()/include() check file_handle.handle.stream.handle */ |
---|
1876 | i = 1; |
---|
1877 | zend_hash_add(&EG(included_files), stored_entry->entry.name.str.val, stored_entry->entry.name.str.len + 1, (void *)&i, sizeof(int), NULL); |
---|
1878 | |
---|
1879 | CG(in_compilation) = 1; |
---|
1880 | CG(compiled_filename) = stored_entry->entry.name.str.val; |
---|
1881 | CG(zend_lineno) = 0; |
---|
1882 | TRACE("restoring %d:%s", stored_entry->file_inode, stored_entry->entry.name.str.val); |
---|
1883 | xc_processor_restore_xc_entry_php_t(&restored_entry, stored_entry TSRMLS_CC); |
---|
1884 | xc_processor_restore_xc_entry_data_php_t(stored_entry, &restored_php, stored_php, xc_readonly_protection TSRMLS_CC); |
---|
1885 | restored_entry.php = &restored_php; |
---|
1886 | #ifdef SHOW_DPRINT |
---|
1887 | xc_dprint(&restored_entry, 0 TSRMLS_CC); |
---|
1888 | #endif |
---|
1889 | |
---|
1890 | catched = 0; |
---|
1891 | zend_try { |
---|
1892 | op_array = xc_entry_install(&restored_entry TSRMLS_CC); |
---|
1893 | } zend_catch { |
---|
1894 | catched = 1; |
---|
1895 | } zend_end_try(); |
---|
1896 | |
---|
1897 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1898 | if (restored_php.constinfos) { |
---|
1899 | efree(restored_php.constinfos); |
---|
1900 | } |
---|
1901 | #endif |
---|
1902 | if (restored_php.funcinfos) { |
---|
1903 | efree(restored_php.funcinfos); |
---|
1904 | } |
---|
1905 | if (restored_php.classinfos) { |
---|
1906 | efree(restored_php.classinfos); |
---|
1907 | } |
---|
1908 | |
---|
1909 | if (catched) { |
---|
1910 | zend_bailout(); |
---|
1911 | } |
---|
1912 | CG(in_compilation) = 0; |
---|
1913 | CG(compiled_filename) = NULL; |
---|
1914 | TRACE("restored %d:%s", stored_entry->file_inode, stored_entry->entry.name.str.val); |
---|
1915 | return op_array; |
---|
1916 | } |
---|
1917 | /* }}} */ |
---|
1918 | typedef struct xc_sandboxed_compiler_t { /* {{{ */ |
---|
1919 | xc_compiler_t *compiler; |
---|
1920 | /* input */ |
---|
1921 | zend_file_handle *h; |
---|
1922 | int type; |
---|
1923 | |
---|
1924 | /* sandbox output */ |
---|
1925 | xc_entry_php_t *stored_entry; |
---|
1926 | xc_entry_data_php_t *stored_php; |
---|
1927 | } xc_sandboxed_compiler_t; |
---|
1928 | /* }}} */ |
---|
1929 | |
---|
1930 | static zend_op_array *xc_compile_file_sandboxed(void *data TSRMLS_DC) /* {{{ */ |
---|
1931 | { |
---|
1932 | xc_sandboxed_compiler_t *sandboxed_compiler = (xc_sandboxed_compiler_t *) data; |
---|
1933 | xc_compiler_t *compiler = sandboxed_compiler->compiler; |
---|
1934 | zend_bool catched = 0; |
---|
1935 | xc_cache_t *cache = &xc_php_caches[compiler->entry_hash.cacheid]; |
---|
1936 | xc_entry_php_t *stored_entry; |
---|
1937 | xc_entry_data_php_t *stored_php; |
---|
1938 | |
---|
1939 | /* {{{ compile */ |
---|
1940 | /* make compile inside sandbox */ |
---|
1941 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1942 | compiler->new_php.constinfos = NULL; |
---|
1943 | #endif |
---|
1944 | compiler->new_php.funcinfos = NULL; |
---|
1945 | compiler->new_php.classinfos = NULL; |
---|
1946 | #ifdef ZEND_ENGINE_2_1 |
---|
1947 | compiler->new_php.autoglobals = NULL; |
---|
1948 | #endif |
---|
1949 | memset(&compiler->new_php.op_array_info, 0, sizeof(compiler->new_php.op_array_info)); |
---|
1950 | |
---|
1951 | zend_try { |
---|
1952 | compiler->new_php.op_array = NULL; |
---|
1953 | xc_compile_php(compiler, sandboxed_compiler->h, sandboxed_compiler->type TSRMLS_CC); |
---|
1954 | } zend_catch { |
---|
1955 | catched = 1; |
---|
1956 | } zend_end_try(); |
---|
1957 | |
---|
1958 | if (catched |
---|
1959 | || !compiler->new_php.op_array /* possible ? */ |
---|
1960 | || !XG(initial_compile_file_called)) { |
---|
1961 | goto err_aftersandbox; |
---|
1962 | } |
---|
1963 | |
---|
1964 | /* }}} */ |
---|
1965 | #ifdef SHOW_DPRINT |
---|
1966 | compiler->new_entry.php = &compiler->new_php; |
---|
1967 | xc_dprint(&compiler->new_entry, 0 TSRMLS_CC); |
---|
1968 | #endif |
---|
1969 | |
---|
1970 | stored_entry = NULL; |
---|
1971 | stored_php = NULL; |
---|
1972 | ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */ |
---|
1973 | /* php_store */ |
---|
1974 | stored_php = xc_php_store_unlocked(cache, &compiler->new_php TSRMLS_CC); |
---|
1975 | if (!stored_php) { |
---|
1976 | /* error */ |
---|
1977 | break; |
---|
1978 | } |
---|
1979 | /* entry_store */ |
---|
1980 | compiler->new_entry.php = stored_php; |
---|
1981 | stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); |
---|
1982 | if (stored_entry) { |
---|
1983 | xc_php_addref_unlocked(stored_php); |
---|
1984 | TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
---|
1985 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
---|
1986 | } |
---|
1987 | } LEAVE_LOCK_EX(cache); |
---|
1988 | /* }}} */ |
---|
1989 | TRACE("%s", stored_entry ? "stored" : "store failed"); |
---|
1990 | |
---|
1991 | if (catched || !stored_php) { |
---|
1992 | goto err_aftersandbox; |
---|
1993 | } |
---|
1994 | |
---|
1995 | cache->cached->compiling = 0; |
---|
1996 | xc_free_php(&compiler->new_php TSRMLS_CC); |
---|
1997 | |
---|
1998 | if (stored_entry) { |
---|
1999 | sandboxed_compiler->stored_entry = stored_entry; |
---|
2000 | sandboxed_compiler->stored_php = stored_php; |
---|
2001 | /* discard newly compiled result, restore from stored one */ |
---|
2002 | if (compiler->new_php.op_array) { |
---|
2003 | #ifdef ZEND_ENGINE_2 |
---|
2004 | destroy_op_array(compiler->new_php.op_array TSRMLS_CC); |
---|
2005 | #else |
---|
2006 | destroy_op_array(compiler->new_php.op_array); |
---|
2007 | #endif |
---|
2008 | efree(compiler->new_php.op_array); |
---|
2009 | compiler->new_php.op_array = NULL; |
---|
2010 | } |
---|
2011 | return NULL; |
---|
2012 | } |
---|
2013 | else { |
---|
2014 | return compiler->new_php.op_array; |
---|
2015 | } |
---|
2016 | |
---|
2017 | err_aftersandbox: |
---|
2018 | xc_free_php(&compiler->new_php TSRMLS_CC); |
---|
2019 | |
---|
2020 | cache->cached->compiling = 0; |
---|
2021 | if (catched) { |
---|
2022 | cache->cached->errors ++; |
---|
2023 | zend_bailout(); |
---|
2024 | } |
---|
2025 | return compiler->new_php.op_array; |
---|
2026 | } /* }}} */ |
---|
2027 | static zend_op_array *xc_compile_file_cached(xc_compiler_t *compiler, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
2028 | { |
---|
2029 | /* |
---|
2030 | if (clog) { |
---|
2031 | return old; |
---|
2032 | } |
---|
2033 | |
---|
2034 | if (cached_entry = getby entry_hash) { |
---|
2035 | php = cached_entry.php; |
---|
2036 | php = restore(php); |
---|
2037 | return php; |
---|
2038 | } |
---|
2039 | else { |
---|
2040 | if (!(php = getby md5)) { |
---|
2041 | if (clog) { |
---|
2042 | return old; |
---|
2043 | } |
---|
2044 | |
---|
2045 | inside_sandbox { |
---|
2046 | php = compile; |
---|
2047 | entry = create entries[entry]; |
---|
2048 | } |
---|
2049 | } |
---|
2050 | |
---|
2051 | entry.php = php; |
---|
2052 | return php; |
---|
2053 | } |
---|
2054 | */ |
---|
2055 | |
---|
2056 | xc_entry_php_t *stored_entry; |
---|
2057 | xc_entry_data_php_t *stored_php; |
---|
2058 | zend_bool gaveup = 0; |
---|
2059 | zend_bool catched = 0; |
---|
2060 | zend_op_array *op_array; |
---|
2061 | xc_cache_t *cache = &xc_php_caches[compiler->entry_hash.cacheid]; |
---|
2062 | xc_sandboxed_compiler_t sandboxed_compiler; |
---|
2063 | |
---|
2064 | if (cache->cached->disabled) { |
---|
2065 | return old_compile_file(h, type TSRMLS_CC); |
---|
2066 | } |
---|
2067 | /* stale skips precheck */ |
---|
2068 | if (cache->cached->disabled || XG(request_time) - cache->cached->compiling < 30) { |
---|
2069 | cache->cached->skips ++; |
---|
2070 | return old_compile_file(h, type TSRMLS_CC); |
---|
2071 | } |
---|
2072 | |
---|
2073 | /* {{{ entry_lookup/hit/md5_init/php_lookup */ |
---|
2074 | stored_entry = NULL; |
---|
2075 | stored_php = NULL; |
---|
2076 | |
---|
2077 | ENTER_LOCK_EX(cache) { |
---|
2078 | if (!compiler->opened_path && xc_resolve_path_check_entry_unlocked(compiler, compiler->filename, &stored_entry TSRMLS_CC) == SUCCESS) { |
---|
2079 | compiler->opened_path = compiler->new_entry.entry.name.str.val; |
---|
2080 | } |
---|
2081 | else { |
---|
2082 | if (!compiler->opened_path && xc_entry_php_resolve_opened_path(compiler, NULL TSRMLS_CC) != SUCCESS) { |
---|
2083 | gaveup = 1; |
---|
2084 | break; |
---|
2085 | } |
---|
2086 | |
---|
2087 | /* finalize name */ |
---|
2088 | compiler->new_entry.entry.name.str.val = (char *) compiler->opened_path; |
---|
2089 | compiler->new_entry.entry.name.str.len = strlen(compiler->new_entry.entry.name.str.val); |
---|
2090 | |
---|
2091 | stored_entry = (xc_entry_php_t *) xc_entry_find_unlocked(XC_TYPE_PHP, cache, compiler->entry_hash.entryslotid, (xc_entry_t *) &compiler->new_entry TSRMLS_CC); |
---|
2092 | } |
---|
2093 | |
---|
2094 | if (stored_entry) { |
---|
2095 | xc_cached_hit_unlocked(cache->cached TSRMLS_CC); |
---|
2096 | |
---|
2097 | TRACE(" hit %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
---|
2098 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
---|
2099 | stored_php = stored_entry->php; |
---|
2100 | break; |
---|
2101 | } |
---|
2102 | |
---|
2103 | TRACE("miss entry %d:%s", compiler->new_entry.file_inode, compiler->new_entry.entry.name.str.val); |
---|
2104 | |
---|
2105 | if (xc_entry_data_php_init_md5(cache, compiler TSRMLS_CC) != SUCCESS) { |
---|
2106 | gaveup = 1; |
---|
2107 | break; |
---|
2108 | } |
---|
2109 | |
---|
2110 | stored_php = xc_php_find_unlocked(cache->cached, &compiler->new_php TSRMLS_CC); |
---|
2111 | |
---|
2112 | if (stored_php) { |
---|
2113 | compiler->new_entry.php = stored_php; |
---|
2114 | xc_entry_php_init(&compiler->new_entry, compiler->opened_path TSRMLS_CC); |
---|
2115 | stored_entry = xc_entry_php_store_unlocked(cache, compiler->entry_hash.entryslotid, &compiler->new_entry TSRMLS_CC); |
---|
2116 | if (stored_entry) { |
---|
2117 | xc_php_addref_unlocked(stored_php); |
---|
2118 | TRACE(" cached %d:%s, holding", compiler->new_entry.file_inode, stored_entry->entry.name.str.val); |
---|
2119 | xc_entry_hold_php_unlocked(cache, stored_entry TSRMLS_CC); |
---|
2120 | } |
---|
2121 | else { |
---|
2122 | gaveup = 1; |
---|
2123 | } |
---|
2124 | break; |
---|
2125 | } |
---|
2126 | |
---|
2127 | if (XG(request_time) - cache->cached->compiling < 30) { |
---|
2128 | TRACE("%s", "miss php, but compiling"); |
---|
2129 | cache->cached->skips ++; |
---|
2130 | gaveup = 1; |
---|
2131 | break; |
---|
2132 | } |
---|
2133 | |
---|
2134 | TRACE("%s", "miss php, going to compile"); |
---|
2135 | cache->cached->compiling = XG(request_time); |
---|
2136 | } LEAVE_LOCK_EX(cache); |
---|
2137 | |
---|
2138 | if (catched) { |
---|
2139 | cache->cached->compiling = 0; |
---|
2140 | zend_bailout(); |
---|
2141 | } |
---|
2142 | |
---|
2143 | /* found entry */ |
---|
2144 | if (stored_entry && stored_php) { |
---|
2145 | zend_llist_add_element(&CG(open_files), h); |
---|
2146 | return xc_compile_restore(stored_entry, stored_php TSRMLS_CC); |
---|
2147 | } |
---|
2148 | |
---|
2149 | /* gaveup */ |
---|
2150 | if (gaveup) { |
---|
2151 | return old_compile_file(h, type TSRMLS_CC); |
---|
2152 | } |
---|
2153 | /* }}} */ |
---|
2154 | |
---|
2155 | sandboxed_compiler.compiler = compiler; |
---|
2156 | sandboxed_compiler.h = h; |
---|
2157 | sandboxed_compiler.type = type; |
---|
2158 | sandboxed_compiler.stored_php = NULL; |
---|
2159 | sandboxed_compiler.stored_entry = NULL; |
---|
2160 | op_array = xc_sandbox(xc_compile_file_sandboxed, (void *) &sandboxed_compiler, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); |
---|
2161 | if (sandboxed_compiler.stored_entry) { |
---|
2162 | return xc_compile_restore(sandboxed_compiler.stored_entry, sandboxed_compiler.stored_php TSRMLS_CC); |
---|
2163 | } |
---|
2164 | else { |
---|
2165 | return op_array; |
---|
2166 | } |
---|
2167 | } |
---|
2168 | /* }}} */ |
---|
2169 | static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
2170 | { |
---|
2171 | xc_compiler_t compiler; |
---|
2172 | zend_op_array *op_array; |
---|
2173 | |
---|
2174 | assert(xc_initized); |
---|
2175 | |
---|
2176 | TRACE("xc_compile_file: type=%d name=%s", h->type, h->filename ? h->filename : "NULL"); |
---|
2177 | |
---|
2178 | if (!XG(cacher) |
---|
2179 | || !h->filename |
---|
2180 | || !SG(request_info).path_translated |
---|
2181 | |
---|
2182 | ) { |
---|
2183 | TRACE("%s", "cacher not enabled"); |
---|
2184 | return old_compile_file(h, type TSRMLS_CC); |
---|
2185 | } |
---|
2186 | |
---|
2187 | /* {{{ entry_init_key */ |
---|
2188 | compiler.opened_path = h->opened_path; |
---|
2189 | compiler.filename = compiler.opened_path ? compiler.opened_path : h->filename; |
---|
2190 | compiler.filename_len = strlen(compiler.filename); |
---|
2191 | if (xc_entry_php_init_key(&compiler TSRMLS_CC) != SUCCESS) { |
---|
2192 | TRACE("failed to init key for %s", compiler.filename); |
---|
2193 | return old_compile_file(h, type TSRMLS_CC); |
---|
2194 | } |
---|
2195 | /* }}} */ |
---|
2196 | |
---|
2197 | op_array = xc_compile_file_cached(&compiler, h, type TSRMLS_CC); |
---|
2198 | |
---|
2199 | xc_entry_free_key_php(&compiler.new_entry TSRMLS_CC); |
---|
2200 | |
---|
2201 | return op_array; |
---|
2202 | } |
---|
2203 | /* }}} */ |
---|
2204 | |
---|
2205 | /* gdb helper functions, but N/A for coredump */ |
---|
2206 | zend_bool xc_is_rw(const void *p) /* {{{ */ |
---|
2207 | { |
---|
2208 | xc_shm_t *shm; |
---|
2209 | size_t i; |
---|
2210 | |
---|
2211 | if (xc_php_caches) { |
---|
2212 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2213 | shm = xc_php_caches[i].shm; |
---|
2214 | if (shm->handlers->is_readwrite(shm, p)) { |
---|
2215 | return 1; |
---|
2216 | } |
---|
2217 | } |
---|
2218 | } |
---|
2219 | |
---|
2220 | if (xc_var_caches) { |
---|
2221 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2222 | shm = xc_var_caches[i].shm; |
---|
2223 | if (shm->handlers->is_readwrite(shm, p)) { |
---|
2224 | return 1; |
---|
2225 | } |
---|
2226 | } |
---|
2227 | } |
---|
2228 | return 0; |
---|
2229 | } |
---|
2230 | /* }}} */ |
---|
2231 | zend_bool xc_is_ro(const void *p) /* {{{ */ |
---|
2232 | { |
---|
2233 | xc_shm_t *shm; |
---|
2234 | size_t i; |
---|
2235 | |
---|
2236 | if (xc_php_caches) { |
---|
2237 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2238 | shm = xc_php_caches[i].shm; |
---|
2239 | if (shm->handlers->is_readonly(shm, p)) { |
---|
2240 | return 1; |
---|
2241 | } |
---|
2242 | } |
---|
2243 | } |
---|
2244 | |
---|
2245 | if (xc_var_caches) { |
---|
2246 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2247 | shm = xc_var_caches[i].shm; |
---|
2248 | if (shm->handlers->is_readonly(shm, p)) { |
---|
2249 | return 1; |
---|
2250 | } |
---|
2251 | } |
---|
2252 | } |
---|
2253 | return 0; |
---|
2254 | } |
---|
2255 | /* }}} */ |
---|
2256 | zend_bool xc_is_shm(const void *p) /* {{{ */ |
---|
2257 | { |
---|
2258 | return xc_is_ro(p) || xc_is_rw(p); |
---|
2259 | } |
---|
2260 | /* }}} */ |
---|
2261 | |
---|
2262 | void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC) /* {{{ */ |
---|
2263 | { |
---|
2264 | zend_llist_add_element(&XG(gc_op_arrays), (void *) gc_op_array); |
---|
2265 | } |
---|
2266 | /* }}} */ |
---|
2267 | static void xc_gc_op_array(void *pDest) /* {{{ */ |
---|
2268 | { |
---|
2269 | xc_gc_op_array_t *op_array = (xc_gc_op_array_t *) pDest; |
---|
2270 | zend_uint i; |
---|
2271 | #ifdef ZEND_ENGINE_2 |
---|
2272 | if (op_array->arg_info) { |
---|
2273 | for (i = 0; i < op_array->num_args; i++) { |
---|
2274 | efree((char *) ZSTR_V(op_array->arg_info[i].name)); |
---|
2275 | if (ZSTR_V(op_array->arg_info[i].class_name)) { |
---|
2276 | efree((char *) ZSTR_V(op_array->arg_info[i].class_name)); |
---|
2277 | } |
---|
2278 | } |
---|
2279 | efree(op_array->arg_info); |
---|
2280 | } |
---|
2281 | #endif |
---|
2282 | if (op_array->opcodes) { |
---|
2283 | efree(op_array->opcodes); |
---|
2284 | } |
---|
2285 | #ifdef ZEND_ENGINE_2_4 |
---|
2286 | if (op_array->literals) { |
---|
2287 | efree(op_array->literals); |
---|
2288 | } |
---|
2289 | #endif |
---|
2290 | } |
---|
2291 | /* }}} */ |
---|
2292 | |
---|
2293 | /* variable namespace */ |
---|
2294 | #ifdef IS_UNICODE |
---|
2295 | void xc_var_namespace_init_from_unicodel(const UChar *string, int len TSRMLS_DC) /* {{{ */ |
---|
2296 | { |
---|
2297 | if (!len) { |
---|
2298 | #ifdef IS_UNICODE |
---|
2299 | ZVAL_EMPTY_UNICODE(&XG(uvar_namespace_hard)); |
---|
2300 | #endif |
---|
2301 | ZVAL_EMPTY_STRING(&XG(var_namespace_hard)); |
---|
2302 | } |
---|
2303 | else { |
---|
2304 | ZVAL_UNICODE_L(&XG(uvar_namespace_hard), string, len, 1); |
---|
2305 | /* TODO: copy to var */ |
---|
2306 | } |
---|
2307 | } |
---|
2308 | /* }}} */ |
---|
2309 | #endif |
---|
2310 | void xc_var_namespace_init_from_stringl(const char *string, int len TSRMLS_DC) /* {{{ */ |
---|
2311 | { |
---|
2312 | if (!len) { |
---|
2313 | #ifdef IS_UNICODE |
---|
2314 | ZVAL_EMPTY_UNICODE(&XG(uvar_namespace_hard)); |
---|
2315 | #endif |
---|
2316 | ZVAL_EMPTY_STRING(&XG(var_namespace_hard)); |
---|
2317 | } |
---|
2318 | else { |
---|
2319 | ZVAL_STRINGL(&XG(var_namespace_hard), string, len, 1); |
---|
2320 | #ifdef IS_UNICODE |
---|
2321 | /* TODO: copy to uvar */ |
---|
2322 | #endif |
---|
2323 | } |
---|
2324 | } |
---|
2325 | /* }}} */ |
---|
2326 | void xc_var_namespace_init_from_long(long value TSRMLS_DC) /* {{{ */ |
---|
2327 | { |
---|
2328 | ZVAL_LONG(&XG(var_namespace_hard), value); |
---|
2329 | #ifdef IS_UNICODE |
---|
2330 | /* TODO: copy to uvar_namespace */ |
---|
2331 | #endif |
---|
2332 | } |
---|
2333 | /* }}} */ |
---|
2334 | #ifdef IS_UNICODE |
---|
2335 | void xc_var_namespace_set_unicodel(const UChar *unicode, int len TSRMLS_DC) /* {{{ */ |
---|
2336 | { |
---|
2337 | zval_dtor(&XG(uvar_namespace_soft)); |
---|
2338 | zval_dtor(&XG(var_namespace_soft)); |
---|
2339 | if (len) { |
---|
2340 | if (!Z_USTRLEN_P(&XG(uvar_namespace_soft))) { |
---|
2341 | ZVAL_UNICODEL(&XG(uvar_namespace_soft), unicode, len, 1); |
---|
2342 | } |
---|
2343 | else { |
---|
2344 | int buffer_len = Z_USTRLEN_P(&XG(var_namespace_hard)) + 1 + len; |
---|
2345 | char *buffer = emalloc((buffer_len + 1) * sizeof(unicode[0])); |
---|
2346 | char *p = buffer; |
---|
2347 | memcpy(p, Z_USTRVAL_P(&XG(var_namespace_hard)), (Z_USTRLEN_P(&XG(var_namespace_hard)) + 1)); |
---|
2348 | p += (Z_USTRLEN_P(&XG(var_namespace_hard)) + 1) * sizeof(unicode[0]); |
---|
2349 | memcpy(p, unicode, (len + 1) * sizeof(unicode[0])); |
---|
2350 | ZVAL_UNICODEL(&XG(uvar_namespace_soft), buffer, buffer_len, 0); |
---|
2351 | } |
---|
2352 | /* TODO: copy to var */ |
---|
2353 | } |
---|
2354 | else { |
---|
2355 | #ifdef IS_UNICODE |
---|
2356 | XG(uvar_namespace_soft) = XG(uvar_namespace_hard); |
---|
2357 | zval_copy_ctor(&XG(uvar_namespace_soft)); |
---|
2358 | #endif |
---|
2359 | XG(var_namespace_soft) = XG(var_namespace_hard); |
---|
2360 | zval_copy_ctor(&XG(var_namespace_soft)); |
---|
2361 | } |
---|
2362 | } |
---|
2363 | /* }}} */ |
---|
2364 | #endif |
---|
2365 | void xc_var_namespace_set_stringl(const char *string, int len TSRMLS_DC) /* {{{ */ |
---|
2366 | { |
---|
2367 | #ifdef IS_UNICODE |
---|
2368 | zval_dtor(&XG(uvar_namespace_soft)); |
---|
2369 | #endif |
---|
2370 | zval_dtor(&XG(var_namespace_soft)); |
---|
2371 | if (len) { |
---|
2372 | if (!Z_STRLEN_P(&XG(var_namespace_soft))) { |
---|
2373 | ZVAL_STRINGL(&XG(var_namespace_soft), string, len, 1); |
---|
2374 | } |
---|
2375 | else { |
---|
2376 | int buffer_len = Z_STRLEN_P(&XG(var_namespace_hard)) + 1 + len; |
---|
2377 | char *buffer = emalloc(buffer_len + 1); |
---|
2378 | char *p = buffer; |
---|
2379 | memcpy(p, Z_STRVAL_P(&XG(var_namespace_hard)), Z_STRLEN_P(&XG(var_namespace_hard)) + 1); |
---|
2380 | p += Z_STRLEN_P(&XG(var_namespace_hard)) + 1; |
---|
2381 | memcpy(p, string, len + 1); |
---|
2382 | ZVAL_STRINGL(&XG(var_namespace_soft), buffer, buffer_len, 0); |
---|
2383 | } |
---|
2384 | #ifdef IS_UNICODE |
---|
2385 | /* TODO: copy to uvar */ |
---|
2386 | #endif |
---|
2387 | } |
---|
2388 | else { |
---|
2389 | #ifdef IS_UNICODE |
---|
2390 | XG(uvar_namespace_soft) = XG(uvar_namespace_hard); |
---|
2391 | zval_copy_ctor(&XG(uvar_namespace_soft)); |
---|
2392 | #endif |
---|
2393 | XG(var_namespace_soft) = XG(var_namespace_hard); |
---|
2394 | zval_copy_ctor(&XG(var_namespace_soft)); |
---|
2395 | } |
---|
2396 | } |
---|
2397 | /* }}} */ |
---|
2398 | static void xc_var_namespace_break(TSRMLS_D) /* {{{ */ |
---|
2399 | { |
---|
2400 | #ifdef IS_UNICODE |
---|
2401 | zval_dtor(&XG(uvar_namespace_soft)); |
---|
2402 | #endif |
---|
2403 | zval_dtor(&XG(var_namespace_soft)); |
---|
2404 | #ifdef IS_UNICODE |
---|
2405 | ZVAL_EMPTY_UNICODE(&XG(uvar_namespace_soft)); |
---|
2406 | #endif |
---|
2407 | ZVAL_EMPTY_STRING(&XG(var_namespace_soft)); |
---|
2408 | } |
---|
2409 | /* }}} */ |
---|
2410 | static void xc_var_namespace_init(TSRMLS_D) /* {{{ */ |
---|
2411 | { |
---|
2412 | uid_t id = (uid_t) -1; |
---|
2413 | |
---|
2414 | switch (xc_var_namespace_mode) { |
---|
2415 | case 1: |
---|
2416 | { |
---|
2417 | zval **server; |
---|
2418 | HashTable *ht; |
---|
2419 | zval **val; |
---|
2420 | |
---|
2421 | #ifdef ZEND_ENGINE_2_1 |
---|
2422 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
---|
2423 | #endif |
---|
2424 | |
---|
2425 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void**)&server) == FAILURE |
---|
2426 | || Z_TYPE_PP(server) != IS_ARRAY |
---|
2427 | || !(ht = Z_ARRVAL_P(*server)) |
---|
2428 | || zend_hash_find(ht, xc_var_namespace, strlen(xc_var_namespace) + 1, (void**)&val) == FAILURE) { |
---|
2429 | xc_var_namespace_init_from_stringl(NULL, 0 TSRMLS_CC); |
---|
2430 | } |
---|
2431 | else { |
---|
2432 | #ifdef IS_UNICODE |
---|
2433 | if (Z_TYPE_PP(val) == IS_UNICODE) { |
---|
2434 | xc_var_namespace_init_from_unicodel(Z_USTRVAL_PP(val), Z_USTRLEN_PP(val) TSRMLS_CC); |
---|
2435 | } |
---|
2436 | else |
---|
2437 | #endif |
---|
2438 | { |
---|
2439 | xc_var_namespace_init_from_stringl(Z_STRVAL_PP(val), Z_STRLEN_PP(val) TSRMLS_CC); |
---|
2440 | } |
---|
2441 | } |
---|
2442 | } |
---|
2443 | break; |
---|
2444 | |
---|
2445 | case 2: |
---|
2446 | if (strncmp(xc_var_namespace, "uid", 3) == 0) { |
---|
2447 | id = getuid(); |
---|
2448 | } |
---|
2449 | else if (strncmp(xc_var_namespace, "gid", 3) == 0) { |
---|
2450 | id = getgid(); |
---|
2451 | } |
---|
2452 | |
---|
2453 | if (id == (uid_t) -1){ |
---|
2454 | xc_var_namespace_init_from_stringl(NULL, 0 TSRMLS_CC); |
---|
2455 | } |
---|
2456 | else { |
---|
2457 | xc_var_namespace_init_from_long((long) id TSRMLS_CC); |
---|
2458 | } |
---|
2459 | break; |
---|
2460 | |
---|
2461 | case 0: |
---|
2462 | default: |
---|
2463 | xc_var_namespace_init_from_stringl(xc_var_namespace, strlen(xc_var_namespace) TSRMLS_CC); |
---|
2464 | break; |
---|
2465 | } |
---|
2466 | |
---|
2467 | #ifdef IS_UNICODE |
---|
2468 | INIT_ZVAL(XG(uvar_namespace_soft)); |
---|
2469 | #endif |
---|
2470 | INIT_ZVAL(XG(var_namespace_soft)); |
---|
2471 | xc_var_namespace_set_stringl("", 0 TSRMLS_CC); |
---|
2472 | } |
---|
2473 | /* }}} */ |
---|
2474 | static void xc_var_namespace_destroy(TSRMLS_D) /* {{{ */ |
---|
2475 | { |
---|
2476 | #ifdef IS_UNICODE |
---|
2477 | zval_dtor(&XG(uvar_namespace_hard)); |
---|
2478 | zval_dtor(&XG(uvar_namespace_soft)); |
---|
2479 | #endif |
---|
2480 | zval_dtor(&XG(var_namespace_hard)); |
---|
2481 | zval_dtor(&XG(var_namespace_soft)); |
---|
2482 | } |
---|
2483 | /* }}} */ |
---|
2484 | static int xc_var_buffer_prepare(zval *name TSRMLS_DC) /* {{{ prepare name, calculate buffer size */ |
---|
2485 | { |
---|
2486 | int namespace_len; |
---|
2487 | switch (name->type) { |
---|
2488 | #ifdef IS_UNICODE |
---|
2489 | case IS_UNICODE: |
---|
2490 | do_unicode: |
---|
2491 | namespace_len = Z_USTRLEN_P(&XG(uvar_namespace_soft)); |
---|
2492 | return (namespace_len ? namespace_len + 1 : 0) + Z_USTRLEN_P(name); |
---|
2493 | #endif |
---|
2494 | |
---|
2495 | case IS_STRING: |
---|
2496 | do_string: |
---|
2497 | namespace_len = Z_STRLEN_P(&XG(var_namespace_soft)); |
---|
2498 | return (namespace_len ? namespace_len + 1 : 0) + Z_STRLEN_P(name); |
---|
2499 | |
---|
2500 | default: |
---|
2501 | #ifdef IS_UNICODE |
---|
2502 | convert_to_unicode(name); |
---|
2503 | goto do_unicode; |
---|
2504 | #else |
---|
2505 | convert_to_string(name); |
---|
2506 | goto do_string; |
---|
2507 | #endif |
---|
2508 | } |
---|
2509 | } |
---|
2510 | /* }}} */ |
---|
2511 | static int xc_var_buffer_alloca_size(zval *name TSRMLS_DC) /* {{{ prepare name, calculate buffer size */ |
---|
2512 | { |
---|
2513 | int namespace_len; |
---|
2514 | switch (name->type) { |
---|
2515 | #ifdef IS_UNICODE |
---|
2516 | case IS_UNICODE: |
---|
2517 | namespace_len = Z_USTRLEN_P(&XG(uvar_namespace_soft)); |
---|
2518 | return !namespace_len ? 0 : (namespace_len + 1 + Z_USTRLEN_P(name) + 1) * sizeof(Z_USTRVAL_P(&XG(uvar_namespace_soft))[0]); |
---|
2519 | #endif |
---|
2520 | |
---|
2521 | case IS_STRING: |
---|
2522 | namespace_len = Z_STRLEN_P(&XG(var_namespace_soft)); |
---|
2523 | return !namespace_len ? 0 : (namespace_len + 1 + Z_STRLEN_P(name) + 1); |
---|
2524 | } |
---|
2525 | assert(0); |
---|
2526 | return 0; |
---|
2527 | } |
---|
2528 | /* }}} */ |
---|
2529 | static void xc_var_buffer_init(char *buffer, zval *name TSRMLS_DC) /* {{{ prepare name, calculate buffer size */ |
---|
2530 | { |
---|
2531 | #ifdef IS_UNICODE |
---|
2532 | if (Z_TYPE(name) == IS_UNICODE) { |
---|
2533 | memcpy(buffer, Z_USTRVAL_P(&XG(uvar_namespace_soft)), (Z_USTRLEN_P(&XG(uvar_namespace_soft)) + 1) * sizeof(Z_USTRVAL_P(name)[0])); |
---|
2534 | buffer += (Z_USTRLEN_P(&XG(uvar_namespace_soft)) + 1) * sizeof(Z_USTRVAL_P(name)[0]); |
---|
2535 | memcpy(buffer, Z_USTRVAL_P(name), (Z_USTRLEN_P(name) + 1) * sizeof(Z_USTRVAL_P(name)[0])); |
---|
2536 | } |
---|
2537 | #endif |
---|
2538 | memcpy(buffer, Z_STRVAL_P(&XG(var_namespace_soft)), (Z_STRLEN_P(&XG(var_namespace_soft)) + 1)); |
---|
2539 | buffer += (Z_STRLEN_P(&XG(var_namespace_soft)) + 1); |
---|
2540 | memcpy(buffer, Z_STRVAL_P(name), (Z_STRLEN_P(name) + 1)); |
---|
2541 | } |
---|
2542 | /* }}} */ |
---|
2543 | typedef struct xc_namebuffer_t_ { /* {{{ */ |
---|
2544 | ALLOCA_FLAG(useheap) |
---|
2545 | void *buffer; |
---|
2546 | int alloca_size; |
---|
2547 | int len; |
---|
2548 | } xc_namebuffer_t; |
---|
2549 | /* }}} */ |
---|
2550 | |
---|
2551 | #define VAR_BUFFER_FLAGS(name) \ |
---|
2552 | xc_namebuffer_t name##_buffer; |
---|
2553 | |
---|
2554 | #define VAR_BUFFER_INIT(name) \ |
---|
2555 | name##_buffer.len = xc_var_buffer_prepare(name TSRMLS_CC); \ |
---|
2556 | name##_buffer.alloca_size = xc_var_buffer_alloca_size(name TSRMLS_CC); \ |
---|
2557 | name##_buffer.buffer = name##_buffer.alloca_size \ |
---|
2558 | ? xc_do_alloca(name##_buffer.alloca_size, name##_buffer.useheap) \ |
---|
2559 | : UNISW(Z_STRVAL_P(name), Z_TYPE(name) == IS_UNICODE ? Z_USTRVAL_P(name) : Z_STRVAL_P(name)); \ |
---|
2560 | if (name##_buffer.alloca_size) xc_var_buffer_init(name##_buffer.buffer, name TSRMLS_CC); |
---|
2561 | |
---|
2562 | #define VAR_BUFFER_FREE(name) \ |
---|
2563 | if (name##_buffer.alloca_size) { \ |
---|
2564 | xc_free_alloca(name##_buffer.buffer, name##_buffer.useheap); \ |
---|
2565 | } |
---|
2566 | |
---|
2567 | static inline zend_bool xc_var_has_prefix(xc_entry_t *entry, zval *prefix TSRMLS_DC) /* {{{ */ |
---|
2568 | { |
---|
2569 | zend_bool result = 0; |
---|
2570 | VAR_BUFFER_FLAGS(prefix); |
---|
2571 | |
---|
2572 | if (UNISW(IS_STRING, entry->name_type) != prefix->type) { |
---|
2573 | return 0; |
---|
2574 | } |
---|
2575 | VAR_BUFFER_INIT(prefix); |
---|
2576 | |
---|
2577 | #ifdef IS_UNICODE |
---|
2578 | if (Z_TYPE(prefix) == IS_UNICODE) { |
---|
2579 | result = entry->name.ustr.len >= prefix_buffer.len |
---|
2580 | && memcmp(entry->name.ustr.val, prefix_buffer.buffer, prefix_buffer.len * sizeof(Z_USTRVAL_P(prefix)[0])) == 0; |
---|
2581 | goto finish; |
---|
2582 | } |
---|
2583 | #endif |
---|
2584 | |
---|
2585 | result = entry->name.str.len >= prefix_buffer.len |
---|
2586 | && memcmp(entry->name.str.val, prefix_buffer.buffer, prefix_buffer.len) == 0; |
---|
2587 | goto finish; |
---|
2588 | |
---|
2589 | finish: |
---|
2590 | VAR_BUFFER_FREE(prefix); |
---|
2591 | return result; |
---|
2592 | } |
---|
2593 | /* }}} */ |
---|
2594 | |
---|
2595 | /* module helper function */ |
---|
2596 | static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */ |
---|
2597 | { |
---|
2598 | zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2599 | zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2600 | return SUCCESS; |
---|
2601 | } |
---|
2602 | /* }}} */ |
---|
2603 | static xc_shm_t *xc_cache_destroy(xc_cache_t *caches, xc_hash_t *hcache) /* {{{ */ |
---|
2604 | { |
---|
2605 | size_t i; |
---|
2606 | xc_shm_t *shm = NULL; |
---|
2607 | |
---|
2608 | assert(caches); |
---|
2609 | |
---|
2610 | for (i = 0; i < hcache->size; i ++) { |
---|
2611 | xc_cache_t *cache = &caches[i]; |
---|
2612 | if (cache) { |
---|
2613 | if (cache->lck) { |
---|
2614 | xc_lock_destroy(cache->lck); |
---|
2615 | } |
---|
2616 | /* do NOT touch cached data */ |
---|
2617 | shm = cache->shm; |
---|
2618 | if (shm) { |
---|
2619 | cache->shm->handlers->memdestroy(cache->allocator); |
---|
2620 | } |
---|
2621 | } |
---|
2622 | } |
---|
2623 | free(caches); |
---|
2624 | return shm; |
---|
2625 | } |
---|
2626 | /* }}} */ |
---|
2627 | static xc_cache_t *xc_cache_init(xc_shm_t *shm, const char *allocator_name, xc_hash_t *hcache, xc_hash_t *hentry, xc_hash_t *hphp, xc_shmsize_t shmsize) /* {{{ */ |
---|
2628 | { |
---|
2629 | xc_cache_t *caches = NULL; |
---|
2630 | xc_allocator_t *allocator; |
---|
2631 | time_t now = time(NULL); |
---|
2632 | size_t i; |
---|
2633 | xc_memsize_t memsize; |
---|
2634 | |
---|
2635 | memsize = shmsize / hcache->size; |
---|
2636 | |
---|
2637 | /* Don't let it break out of mem after ALIGNed |
---|
2638 | * This is important for |
---|
2639 | * Simply loop until it fit our need |
---|
2640 | */ |
---|
2641 | while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) { |
---|
2642 | if (memsize < ALIGN(1)) { |
---|
2643 | CHECK(NULL, "cache too small"); |
---|
2644 | } |
---|
2645 | memsize --; |
---|
2646 | } |
---|
2647 | |
---|
2648 | CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t)), "caches OOM"); |
---|
2649 | |
---|
2650 | for (i = 0; i < hcache->size; i ++) { |
---|
2651 | xc_cache_t *cache = &caches[i]; |
---|
2652 | CHECK(allocator = shm->handlers->meminit(shm, memsize), "Failed init shm"); |
---|
2653 | if (!(allocator->vtable = xc_allocator_find(allocator_name))) { |
---|
2654 | zend_error(E_ERROR, "Allocator %s not found", allocator_name); |
---|
2655 | goto err; |
---|
2656 | } |
---|
2657 | CHECK(allocator->vtable->init(shm, allocator, memsize), "Failed init allocator"); |
---|
2658 | CHECK(cache->cached = allocator->vtable->calloc(allocator, 1, sizeof(xc_cached_t)), "create cache OOM"); |
---|
2659 | CHECK(cache->cached->entries = allocator->vtable->calloc(allocator, hentry->size, sizeof(xc_entry_t*)), "create entries OOM"); |
---|
2660 | if (hphp) { |
---|
2661 | CHECK(cache->cached->phps = allocator->vtable->calloc(allocator, hphp->size, sizeof(xc_entry_data_php_t*)), "create phps OOM"); |
---|
2662 | } |
---|
2663 | CHECK(cache->lck = allocator->vtable->calloc(allocator, 1, xc_lock_size()), "create lock OOM"); |
---|
2664 | CHECK(xc_lock_init(cache->lck, NULL, 1), "can't create lock"); |
---|
2665 | |
---|
2666 | cache->hcache = hcache; |
---|
2667 | cache->hentry = hentry; |
---|
2668 | cache->hphp = hphp; |
---|
2669 | cache->shm = shm; |
---|
2670 | cache->allocator = allocator; |
---|
2671 | cache->cacheid = i; |
---|
2672 | cache->cached->last_gc_deletes = now; |
---|
2673 | cache->cached->last_gc_expires = now; |
---|
2674 | } |
---|
2675 | return caches; |
---|
2676 | |
---|
2677 | err: |
---|
2678 | if (caches) { |
---|
2679 | xc_cache_destroy(caches, hcache); |
---|
2680 | } |
---|
2681 | return NULL; |
---|
2682 | } |
---|
2683 | /* }}} */ |
---|
2684 | static void xc_destroy() /* {{{ */ |
---|
2685 | { |
---|
2686 | xc_shm_t *shm = NULL; |
---|
2687 | if (old_compile_file && zend_compile_file == xc_compile_file) { |
---|
2688 | zend_compile_file = old_compile_file; |
---|
2689 | old_compile_file = NULL; |
---|
2690 | } |
---|
2691 | |
---|
2692 | if (xc_php_caches) { |
---|
2693 | shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); |
---|
2694 | xc_php_caches = NULL; |
---|
2695 | } |
---|
2696 | |
---|
2697 | if (xc_var_caches) { |
---|
2698 | shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); |
---|
2699 | xc_var_caches = NULL; |
---|
2700 | } |
---|
2701 | |
---|
2702 | if (shm) { |
---|
2703 | xc_shm_destroy(shm); |
---|
2704 | } |
---|
2705 | |
---|
2706 | xc_initized = 0; |
---|
2707 | } |
---|
2708 | /* }}} */ |
---|
2709 | static int xc_init() /* {{{ */ |
---|
2710 | { |
---|
2711 | xc_shm_t *shm = NULL; |
---|
2712 | xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size); |
---|
2713 | |
---|
2714 | xc_php_caches = xc_var_caches = NULL; |
---|
2715 | |
---|
2716 | if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) { |
---|
2717 | zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative"); |
---|
2718 | goto err; |
---|
2719 | } |
---|
2720 | |
---|
2721 | if (xc_php_size || xc_var_size) { |
---|
2722 | CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm"); |
---|
2723 | if (!shm->handlers->can_readonly(shm)) { |
---|
2724 | xc_readonly_protection = 0; |
---|
2725 | } |
---|
2726 | |
---|
2727 | if (xc_php_size) { |
---|
2728 | CHECK(xc_php_caches = xc_cache_init(shm, xc_php_allocator, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache"); |
---|
2729 | } |
---|
2730 | |
---|
2731 | if (xc_var_size) { |
---|
2732 | CHECK(xc_var_caches = xc_cache_init(shm, xc_var_allocator, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache"); |
---|
2733 | } |
---|
2734 | } |
---|
2735 | return SUCCESS; |
---|
2736 | |
---|
2737 | err: |
---|
2738 | if (xc_php_caches || xc_var_caches) { |
---|
2739 | xc_destroy(); |
---|
2740 | /* shm destroied in xc_destroy() */ |
---|
2741 | } |
---|
2742 | else if (shm) { |
---|
2743 | xc_destroy(); |
---|
2744 | xc_shm_destroy(shm); |
---|
2745 | shm = NULL; |
---|
2746 | } |
---|
2747 | return FAILURE; |
---|
2748 | } |
---|
2749 | /* }}} */ |
---|
2750 | static void xc_request_init(TSRMLS_D) /* {{{ */ |
---|
2751 | { |
---|
2752 | size_t i; |
---|
2753 | |
---|
2754 | if (!XG(internal_table_copied)) { |
---|
2755 | zend_function tmp_func; |
---|
2756 | xc_cest_t tmp_cest; |
---|
2757 | |
---|
2758 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2759 | zend_hash_destroy(&XG(internal_constant_table)); |
---|
2760 | #endif |
---|
2761 | zend_hash_destroy(&XG(internal_function_table)); |
---|
2762 | zend_hash_destroy(&XG(internal_class_table)); |
---|
2763 | |
---|
2764 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2765 | zend_hash_init_ex(&XG(internal_constant_table), 20, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0); |
---|
2766 | #endif |
---|
2767 | zend_hash_init_ex(&XG(internal_function_table), 100, NULL, NULL, 1, 0); |
---|
2768 | zend_hash_init_ex(&XG(internal_class_table), 10, NULL, NULL, 1, 0); |
---|
2769 | |
---|
2770 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2771 | xc_copy_internal_zend_constants(&XG(internal_constant_table), EG(zend_constants)); |
---|
2772 | #endif |
---|
2773 | zend_hash_copy(&XG(internal_function_table), CG(function_table), NULL, &tmp_func, sizeof(tmp_func)); |
---|
2774 | zend_hash_copy(&XG(internal_class_table), CG(class_table), NULL, &tmp_cest, sizeof(tmp_cest)); |
---|
2775 | |
---|
2776 | XG(internal_table_copied) = 1; |
---|
2777 | } |
---|
2778 | if (xc_php_caches && !XG(php_holds)) { |
---|
2779 | XG(php_holds_size) = xc_php_hcache.size; |
---|
2780 | XG(php_holds) = calloc(XG(php_holds_size), sizeof(xc_stack_t)); |
---|
2781 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2782 | xc_stack_init(&XG(php_holds[i])); |
---|
2783 | } |
---|
2784 | } |
---|
2785 | |
---|
2786 | if (xc_var_caches && !XG(var_holds)) { |
---|
2787 | XG(var_holds_size) = xc_var_hcache.size; |
---|
2788 | XG(var_holds) = calloc(XG(var_holds_size), sizeof(xc_stack_t)); |
---|
2789 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2790 | xc_stack_init(&XG(var_holds[i])); |
---|
2791 | } |
---|
2792 | } |
---|
2793 | xc_var_namespace_init(TSRMLS_C); |
---|
2794 | #ifdef ZEND_ENGINE_2 |
---|
2795 | zend_llist_init(&XG(gc_op_arrays), sizeof(xc_gc_op_array_t), xc_gc_op_array, 0); |
---|
2796 | #endif |
---|
2797 | |
---|
2798 | #if PHP_API_VERSION <= 20041225 |
---|
2799 | XG(request_time) = time(NULL); |
---|
2800 | #else |
---|
2801 | XG(request_time) = sapi_get_request_time(TSRMLS_C); |
---|
2802 | #endif |
---|
2803 | } |
---|
2804 | /* }}} */ |
---|
2805 | static void xc_request_shutdown(TSRMLS_D) /* {{{ */ |
---|
2806 | { |
---|
2807 | xc_entry_unholds(TSRMLS_C); |
---|
2808 | xc_gc_expires_php(TSRMLS_C); |
---|
2809 | xc_gc_expires_var(TSRMLS_C); |
---|
2810 | xc_gc_deletes(TSRMLS_C); |
---|
2811 | xc_var_namespace_destroy(TSRMLS_C); |
---|
2812 | #ifdef ZEND_ENGINE_2 |
---|
2813 | zend_llist_destroy(&XG(gc_op_arrays)); |
---|
2814 | #endif |
---|
2815 | } |
---|
2816 | /* }}} */ |
---|
2817 | |
---|
2818 | /* user functions */ |
---|
2819 | static zend_bool xcache_admin_auth_check(TSRMLS_D) /* {{{ */ |
---|
2820 | { |
---|
2821 | zval **server = NULL; |
---|
2822 | zval **user = NULL; |
---|
2823 | zval **pass = NULL; |
---|
2824 | char *admin_user = NULL; |
---|
2825 | char *admin_pass = NULL; |
---|
2826 | HashTable *ht; |
---|
2827 | |
---|
2828 | /* auth disabled, nothing to do.. */ |
---|
2829 | if (!xc_admin_enable_auth) { |
---|
2830 | return 1; |
---|
2831 | } |
---|
2832 | |
---|
2833 | if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) { |
---|
2834 | admin_user = NULL; |
---|
2835 | } |
---|
2836 | if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) { |
---|
2837 | admin_pass = NULL; |
---|
2838 | } |
---|
2839 | |
---|
2840 | if (admin_user == NULL || admin_pass == NULL) { |
---|
2841 | php_error_docref(XCACHE_WIKI_URL "/InstallAdministration" TSRMLS_CC, E_ERROR, |
---|
2842 | "xcache.admin.user and/or xcache.admin.pass settings is not configured." |
---|
2843 | " Make sure you've modified the correct php ini file for your php used in webserver."); |
---|
2844 | zend_bailout(); |
---|
2845 | } |
---|
2846 | if (strlen(admin_pass) != 32) { |
---|
2847 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.pass is %lu chars unexpectedly, it is supposed to be the password after md5() which should be 32 chars", (unsigned long) strlen(admin_pass)); |
---|
2848 | zend_bailout(); |
---|
2849 | } |
---|
2850 | |
---|
2851 | #ifdef ZEND_ENGINE_2_1 |
---|
2852 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
---|
2853 | #endif |
---|
2854 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) { |
---|
2855 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted"); |
---|
2856 | zend_bailout(); |
---|
2857 | } |
---|
2858 | ht = Z_ARRVAL_P((*server)); |
---|
2859 | |
---|
2860 | if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) { |
---|
2861 | user = NULL; |
---|
2862 | } |
---|
2863 | else if (Z_TYPE_PP(user) != IS_STRING) { |
---|
2864 | user = NULL; |
---|
2865 | } |
---|
2866 | |
---|
2867 | if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) { |
---|
2868 | pass = NULL; |
---|
2869 | } |
---|
2870 | else if (Z_TYPE_PP(pass) != IS_STRING) { |
---|
2871 | pass = NULL; |
---|
2872 | } |
---|
2873 | |
---|
2874 | if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) { |
---|
2875 | PHP_MD5_CTX context; |
---|
2876 | char md5str[33]; |
---|
2877 | unsigned char digest[16]; |
---|
2878 | |
---|
2879 | PHP_MD5Init(&context); |
---|
2880 | PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass)); |
---|
2881 | PHP_MD5Final(digest, &context); |
---|
2882 | |
---|
2883 | md5str[0] = '\0'; |
---|
2884 | make_digest(md5str, digest); |
---|
2885 | if (strcmp(admin_pass, md5str) == 0) { |
---|
2886 | return 1; |
---|
2887 | } |
---|
2888 | } |
---|
2889 | |
---|
2890 | #define STR "HTTP/1.0 401 Unauthorized" |
---|
2891 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2892 | #undef STR |
---|
2893 | #define STR "WWW-authenticate: Basic Realm=\"XCache Administration\"" |
---|
2894 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2895 | #undef STR |
---|
2896 | #define STR "Content-type: text/html; charset=UTF-8" |
---|
2897 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2898 | #undef STR |
---|
2899 | ZEND_PUTS("<html>\n"); |
---|
2900 | ZEND_PUTS("<head><title>XCache Authentication Failed</title></head>\n"); |
---|
2901 | ZEND_PUTS("<body>\n"); |
---|
2902 | ZEND_PUTS("<h1>XCache Authentication Failed</h1>\n"); |
---|
2903 | ZEND_PUTS("<p>You're not authorized to access this page due to wrong username and/or password you typed.<br />The following check points is suggested:</p>\n"); |
---|
2904 | ZEND_PUTS("<ul>\n"); |
---|
2905 | ZEND_PUTS("<li>Be aware that `Username' and `Password' is case sense. Check capslock status led on your keyboard, and punch left/right Shift keys once for each</li>\n"); |
---|
2906 | ZEND_PUTS("<li>Make sure the md5 password is generated correctly. You may use <a href=\"mkpassword.php\">mkpassword.php</a></li>\n"); |
---|
2907 | ZEND_PUTS("<li>Reload browser cache by pressing F5 and/or Ctrl+F5, or simply clear browser cache after you've updated username/password in php ini.</li>\n"); |
---|
2908 | ZEND_PUTS("</ul>\n"); |
---|
2909 | ZEND_PUTS("Check <a href=\"" XCACHE_WIKI_URL "/InstallAdministration\">XCache wiki page</a> for more information.\n"); |
---|
2910 | ZEND_PUTS("</body>\n"); |
---|
2911 | ZEND_PUTS("</html>\n"); |
---|
2912 | |
---|
2913 | zend_bailout(); |
---|
2914 | return 0; |
---|
2915 | } |
---|
2916 | /* }}} */ |
---|
2917 | static void xc_clear(long type, xc_cache_t *cache TSRMLS_DC) /* {{{ */ |
---|
2918 | { |
---|
2919 | xc_entry_t *e, *next; |
---|
2920 | int entryslotid, c; |
---|
2921 | |
---|
2922 | ENTER_LOCK(cache) { |
---|
2923 | for (entryslotid = 0, c = cache->hentry->size; entryslotid < c; entryslotid ++) { |
---|
2924 | for (e = cache->cached->entries[entryslotid]; e; e = next) { |
---|
2925 | next = e->next; |
---|
2926 | xc_entry_remove_unlocked(type, cache, entryslotid, e TSRMLS_CC); |
---|
2927 | } |
---|
2928 | cache->cached->entries[entryslotid] = NULL; |
---|
2929 | } |
---|
2930 | } LEAVE_LOCK(cache); |
---|
2931 | } /* }}} */ |
---|
2932 | /* {{{ xcache_admin_operate */ |
---|
2933 | typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR, XC_OP_ENABLE } xcache_op_type; |
---|
2934 | static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS) |
---|
2935 | { |
---|
2936 | long type; |
---|
2937 | long size; |
---|
2938 | xc_cache_t *caches, *cache; |
---|
2939 | long id = 0; |
---|
2940 | zend_bool enable = 1; |
---|
2941 | |
---|
2942 | xcache_admin_auth_check(TSRMLS_C); |
---|
2943 | |
---|
2944 | if (!xc_initized) { |
---|
2945 | RETURN_NULL(); |
---|
2946 | } |
---|
2947 | |
---|
2948 | switch (optype) { |
---|
2949 | case XC_OP_COUNT: |
---|
2950 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) { |
---|
2951 | return; |
---|
2952 | } |
---|
2953 | break; |
---|
2954 | |
---|
2955 | case XC_OP_CLEAR: |
---|
2956 | id = -1; |
---|
2957 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &type, &id) == FAILURE) { |
---|
2958 | return; |
---|
2959 | } |
---|
2960 | break; |
---|
2961 | |
---|
2962 | case XC_OP_ENABLE: |
---|
2963 | id = -1; |
---|
2964 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|lb", &type, &id, &enable) == FAILURE) { |
---|
2965 | return; |
---|
2966 | } |
---|
2967 | break; |
---|
2968 | |
---|
2969 | default: |
---|
2970 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) { |
---|
2971 | return; |
---|
2972 | } |
---|
2973 | } |
---|
2974 | |
---|
2975 | switch (type) { |
---|
2976 | case XC_TYPE_PHP: |
---|
2977 | size = xc_php_hcache.size; |
---|
2978 | caches = xc_php_caches; |
---|
2979 | break; |
---|
2980 | |
---|
2981 | case XC_TYPE_VAR: |
---|
2982 | size = xc_var_hcache.size; |
---|
2983 | caches = xc_var_caches; |
---|
2984 | break; |
---|
2985 | |
---|
2986 | default: |
---|
2987 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type); |
---|
2988 | RETURN_FALSE; |
---|
2989 | } |
---|
2990 | |
---|
2991 | switch (optype) { |
---|
2992 | case XC_OP_COUNT: |
---|
2993 | RETURN_LONG(caches ? size : 0) |
---|
2994 | break; |
---|
2995 | |
---|
2996 | case XC_OP_INFO: |
---|
2997 | case XC_OP_LIST: |
---|
2998 | if (!caches || id < 0 || id >= size) { |
---|
2999 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
---|
3000 | RETURN_FALSE; |
---|
3001 | } |
---|
3002 | |
---|
3003 | array_init(return_value); |
---|
3004 | |
---|
3005 | cache = &caches[id]; |
---|
3006 | ENTER_LOCK(cache) { |
---|
3007 | if (optype == XC_OP_INFO) { |
---|
3008 | xc_fillinfo_unlocked(type, cache, return_value TSRMLS_CC); |
---|
3009 | } |
---|
3010 | else { |
---|
3011 | xc_filllist_unlocked(type, cache, return_value TSRMLS_CC); |
---|
3012 | } |
---|
3013 | } LEAVE_LOCK(cache); |
---|
3014 | break; |
---|
3015 | |
---|
3016 | case XC_OP_CLEAR: |
---|
3017 | if (!caches || id < -1 || id >= size) { |
---|
3018 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
---|
3019 | RETURN_FALSE; |
---|
3020 | } |
---|
3021 | |
---|
3022 | if (id == -1) { |
---|
3023 | for (id = 0; id < size; ++id) { |
---|
3024 | xc_clear(type, &caches[id] TSRMLS_CC); |
---|
3025 | } |
---|
3026 | } |
---|
3027 | else { |
---|
3028 | xc_clear(type, &caches[id] TSRMLS_CC); |
---|
3029 | } |
---|
3030 | |
---|
3031 | xc_gc_deletes(TSRMLS_C); |
---|
3032 | break; |
---|
3033 | |
---|
3034 | case XC_OP_ENABLE: |
---|
3035 | if (!caches || id < -1 || id >= size) { |
---|
3036 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
---|
3037 | RETURN_FALSE; |
---|
3038 | } |
---|
3039 | |
---|
3040 | if (id == -1) { |
---|
3041 | for (id = 0; id < size; ++id) { |
---|
3042 | caches[id].cached->disabled = !enable ? XG(request_time) : 0; |
---|
3043 | } |
---|
3044 | } |
---|
3045 | else { |
---|
3046 | caches[id].cached->disabled = !enable ? XG(request_time) : 0; |
---|
3047 | } |
---|
3048 | |
---|
3049 | break; |
---|
3050 | |
---|
3051 | default: |
---|
3052 | assert(0); |
---|
3053 | } |
---|
3054 | } |
---|
3055 | /* }}} */ |
---|
3056 | /* {{{ proto int xcache_count(int type) |
---|
3057 | Return count of cache on specified cache type */ |
---|
3058 | PHP_FUNCTION(xcache_count) |
---|
3059 | { |
---|
3060 | xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3061 | } |
---|
3062 | /* }}} */ |
---|
3063 | /* {{{ proto array xcache_info(int type, int id) |
---|
3064 | Get cache info by id on specified cache type */ |
---|
3065 | PHP_FUNCTION(xcache_info) |
---|
3066 | { |
---|
3067 | xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3068 | } |
---|
3069 | /* }}} */ |
---|
3070 | /* {{{ proto array xcache_list(int type, int id) |
---|
3071 | Get cache entries list by id on specified cache type */ |
---|
3072 | PHP_FUNCTION(xcache_list) |
---|
3073 | { |
---|
3074 | xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3075 | } |
---|
3076 | /* }}} */ |
---|
3077 | /* {{{ proto array xcache_clear_cache(int type, [ int id = -1 ]) |
---|
3078 | Clear cache by id on specified cache type */ |
---|
3079 | PHP_FUNCTION(xcache_clear_cache) |
---|
3080 | { |
---|
3081 | xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3082 | } |
---|
3083 | /* }}} */ |
---|
3084 | /* {{{ proto array xcache_enable_cache(int type, [ int id = -1, [ bool enable = true ] ]) |
---|
3085 | Enable or disable cache by id on specified cache type */ |
---|
3086 | PHP_FUNCTION(xcache_enable_cache) |
---|
3087 | { |
---|
3088 | xcache_admin_operate(XC_OP_ENABLE, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3089 | } |
---|
3090 | /* }}} */ |
---|
3091 | /* {{{ proto mixed xcache_admin_namespace() |
---|
3092 | Break out of namespace limitation */ |
---|
3093 | PHP_FUNCTION(xcache_admin_namespace) |
---|
3094 | { |
---|
3095 | xcache_admin_auth_check(TSRMLS_C); |
---|
3096 | xc_var_namespace_break(TSRMLS_C); |
---|
3097 | } |
---|
3098 | /* }}} */ |
---|
3099 | |
---|
3100 | #define VAR_CACHE_NOT_INITIALIZED() do { \ |
---|
3101 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "XCache var cache was not initialized properly. Check php log for actual reason"); \ |
---|
3102 | } while (0) |
---|
3103 | |
---|
3104 | static int xc_entry_var_init_key(xc_entry_var_t *entry_var, xc_entry_hash_t *entry_hash, xc_namebuffer_t *name_buffer TSRMLS_DC) /* {{{ */ |
---|
3105 | { |
---|
3106 | xc_hash_value_t hv; |
---|
3107 | |
---|
3108 | #ifdef IS_UNICODE |
---|
3109 | entry_var->name_type = name->type; |
---|
3110 | #endif |
---|
3111 | entry_var->entry.name.str.val = name_buffer->buffer; |
---|
3112 | entry_var->entry.name.str.len = name_buffer->len; |
---|
3113 | |
---|
3114 | hv = xc_entry_hash_var((xc_entry_t *) entry_var TSRMLS_CC); |
---|
3115 | |
---|
3116 | entry_hash->cacheid = (hv & xc_var_hcache.mask); |
---|
3117 | hv >>= xc_var_hcache.bits; |
---|
3118 | entry_hash->entryslotid = (hv & xc_var_hentry.mask); |
---|
3119 | return SUCCESS; |
---|
3120 | } |
---|
3121 | /* }}} */ |
---|
3122 | /* {{{ proto mixed xcache_set_namespace(string namespace) |
---|
3123 | Switch to user defined namespace */ |
---|
3124 | PHP_FUNCTION(xcache_set_namespace) |
---|
3125 | { |
---|
3126 | zval *namespace; |
---|
3127 | |
---|
3128 | if (!xc_var_caches) { |
---|
3129 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3130 | RETURN_NULL(); |
---|
3131 | } |
---|
3132 | |
---|
3133 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &namespace) == FAILURE) { |
---|
3134 | return; |
---|
3135 | } |
---|
3136 | |
---|
3137 | if (Z_TYPE_P(namespace) == IS_STRING) { |
---|
3138 | xc_var_namespace_set_stringl(Z_STRVAL_P(namespace), Z_STRLEN_P(namespace) TSRMLS_CC); |
---|
3139 | } |
---|
3140 | #ifdef IS_UNICODE |
---|
3141 | else if (Z_TYPE_P(namespace) == IS_UNICODE) { |
---|
3142 | xc_var_namespace_set_unicodel(Z_USTRVAL_P(namespace), Z_USTRLEN_P(namespace) TSRMLS_CC); |
---|
3143 | } |
---|
3144 | #endif |
---|
3145 | } |
---|
3146 | /* }}} */ |
---|
3147 | /* {{{ proto mixed xcache_get(string name) |
---|
3148 | Get cached data by specified name */ |
---|
3149 | PHP_FUNCTION(xcache_get) |
---|
3150 | { |
---|
3151 | xc_entry_hash_t entry_hash; |
---|
3152 | xc_cache_t *cache; |
---|
3153 | xc_entry_var_t entry_var, *stored_entry_var; |
---|
3154 | zval *name; |
---|
3155 | VAR_BUFFER_FLAGS(name); |
---|
3156 | |
---|
3157 | if (!xc_var_caches) { |
---|
3158 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3159 | RETURN_NULL(); |
---|
3160 | } |
---|
3161 | |
---|
3162 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
3163 | return; |
---|
3164 | } |
---|
3165 | VAR_BUFFER_INIT(name); |
---|
3166 | xc_entry_var_init_key(&entry_var, &entry_hash, &name_buffer TSRMLS_CC); |
---|
3167 | cache = &xc_var_caches[entry_hash.cacheid]; |
---|
3168 | |
---|
3169 | if (cache->cached->disabled) { |
---|
3170 | VAR_BUFFER_FREE(name); |
---|
3171 | RETURN_NULL(); |
---|
3172 | } |
---|
3173 | |
---|
3174 | ENTER_LOCK(cache) { |
---|
3175 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
---|
3176 | if (stored_entry_var) { |
---|
3177 | /* return */ |
---|
3178 | xc_processor_restore_zval(return_value, stored_entry_var->value, stored_entry_var->have_references TSRMLS_CC); |
---|
3179 | xc_cached_hit_unlocked(cache->cached TSRMLS_CC); |
---|
3180 | } |
---|
3181 | else { |
---|
3182 | RETVAL_NULL(); |
---|
3183 | } |
---|
3184 | } LEAVE_LOCK(cache); |
---|
3185 | VAR_BUFFER_FREE(name); |
---|
3186 | } |
---|
3187 | /* }}} */ |
---|
3188 | /* {{{ proto bool xcache_set(string name, mixed value [, int ttl]) |
---|
3189 | Store data to cache by specified name */ |
---|
3190 | PHP_FUNCTION(xcache_set) |
---|
3191 | { |
---|
3192 | xc_entry_hash_t entry_hash; |
---|
3193 | xc_cache_t *cache; |
---|
3194 | xc_entry_var_t entry_var, *stored_entry_var; |
---|
3195 | zval *name; |
---|
3196 | zval *value; |
---|
3197 | VAR_BUFFER_FLAGS(name); |
---|
3198 | |
---|
3199 | if (!xc_var_caches) { |
---|
3200 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3201 | RETURN_NULL(); |
---|
3202 | } |
---|
3203 | |
---|
3204 | entry_var.entry.ttl = XG(var_ttl); |
---|
3205 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &name, &value, &entry_var.entry.ttl) == FAILURE) { |
---|
3206 | return; |
---|
3207 | } |
---|
3208 | |
---|
3209 | if (Z_TYPE_P(value) == IS_OBJECT) { |
---|
3210 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "Objects cannot be stored in the variable cache. Use serialize before xcache_set"); |
---|
3211 | RETURN_NULL(); |
---|
3212 | } |
---|
3213 | |
---|
3214 | /* max ttl */ |
---|
3215 | if (xc_var_maxttl && (!entry_var.entry.ttl || entry_var.entry.ttl > xc_var_maxttl)) { |
---|
3216 | entry_var.entry.ttl = xc_var_maxttl; |
---|
3217 | } |
---|
3218 | |
---|
3219 | VAR_BUFFER_INIT(name); |
---|
3220 | xc_entry_var_init_key(&entry_var, &entry_hash, &name_buffer TSRMLS_CC); |
---|
3221 | cache = &xc_var_caches[entry_hash.cacheid]; |
---|
3222 | |
---|
3223 | if (cache->cached->disabled) { |
---|
3224 | VAR_BUFFER_FREE(name); |
---|
3225 | RETURN_NULL(); |
---|
3226 | } |
---|
3227 | |
---|
3228 | ENTER_LOCK(cache) { |
---|
3229 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
---|
3230 | if (stored_entry_var) { |
---|
3231 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
---|
3232 | } |
---|
3233 | entry_var.value = value; |
---|
3234 | RETVAL_BOOL(xc_entry_var_store_unlocked(cache, entry_hash.entryslotid, &entry_var TSRMLS_CC) != NULL ? 1 : 0); |
---|
3235 | } LEAVE_LOCK(cache); |
---|
3236 | VAR_BUFFER_FREE(name); |
---|
3237 | } |
---|
3238 | /* }}} */ |
---|
3239 | /* {{{ proto bool xcache_isset(string name) |
---|
3240 | Check if an entry exists in cache by specified name */ |
---|
3241 | PHP_FUNCTION(xcache_isset) |
---|
3242 | { |
---|
3243 | xc_entry_hash_t entry_hash; |
---|
3244 | xc_cache_t *cache; |
---|
3245 | xc_entry_var_t entry_var, *stored_entry_var; |
---|
3246 | zval *name; |
---|
3247 | VAR_BUFFER_FLAGS(name); |
---|
3248 | |
---|
3249 | if (!xc_var_caches) { |
---|
3250 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3251 | RETURN_FALSE; |
---|
3252 | } |
---|
3253 | |
---|
3254 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
3255 | return; |
---|
3256 | } |
---|
3257 | VAR_BUFFER_INIT(name); |
---|
3258 | xc_entry_var_init_key(&entry_var, &entry_hash, &name_buffer TSRMLS_CC); |
---|
3259 | cache = &xc_var_caches[entry_hash.cacheid]; |
---|
3260 | |
---|
3261 | if (cache->cached->disabled) { |
---|
3262 | VAR_BUFFER_FREE(name); |
---|
3263 | RETURN_FALSE; |
---|
3264 | } |
---|
3265 | |
---|
3266 | ENTER_LOCK(cache) { |
---|
3267 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
---|
3268 | if (stored_entry_var) { |
---|
3269 | xc_cached_hit_unlocked(cache->cached TSRMLS_CC); |
---|
3270 | RETVAL_TRUE; |
---|
3271 | /* return */ |
---|
3272 | } |
---|
3273 | else { |
---|
3274 | RETVAL_FALSE; |
---|
3275 | } |
---|
3276 | |
---|
3277 | } LEAVE_LOCK(cache); |
---|
3278 | VAR_BUFFER_FREE(name); |
---|
3279 | } |
---|
3280 | /* }}} */ |
---|
3281 | /* {{{ proto bool xcache_unset(string name) |
---|
3282 | Unset existing data in cache by specified name */ |
---|
3283 | PHP_FUNCTION(xcache_unset) |
---|
3284 | { |
---|
3285 | xc_entry_hash_t entry_hash; |
---|
3286 | xc_cache_t *cache; |
---|
3287 | xc_entry_var_t entry_var, *stored_entry_var; |
---|
3288 | zval *name; |
---|
3289 | VAR_BUFFER_FLAGS(name); |
---|
3290 | |
---|
3291 | if (!xc_var_caches) { |
---|
3292 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3293 | RETURN_FALSE; |
---|
3294 | } |
---|
3295 | |
---|
3296 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
3297 | return; |
---|
3298 | } |
---|
3299 | VAR_BUFFER_INIT(name); |
---|
3300 | xc_entry_var_init_key(&entry_var, &entry_hash, &name_buffer TSRMLS_CC); |
---|
3301 | cache = &xc_var_caches[entry_hash.cacheid]; |
---|
3302 | |
---|
3303 | if (cache->cached->disabled) { |
---|
3304 | VAR_BUFFER_FREE(name); |
---|
3305 | RETURN_FALSE; |
---|
3306 | } |
---|
3307 | |
---|
3308 | ENTER_LOCK(cache) { |
---|
3309 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
---|
3310 | if (stored_entry_var) { |
---|
3311 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
---|
3312 | RETVAL_TRUE; |
---|
3313 | } |
---|
3314 | else { |
---|
3315 | RETVAL_FALSE; |
---|
3316 | } |
---|
3317 | } LEAVE_LOCK(cache); |
---|
3318 | VAR_BUFFER_FREE(name); |
---|
3319 | } |
---|
3320 | /* }}} */ |
---|
3321 | /* {{{ proto bool xcache_unset_by_prefix(string prefix) |
---|
3322 | Unset existing data in cache by specified prefix */ |
---|
3323 | PHP_FUNCTION(xcache_unset_by_prefix) |
---|
3324 | { |
---|
3325 | zval *prefix; |
---|
3326 | int i, iend; |
---|
3327 | |
---|
3328 | if (!xc_var_caches) { |
---|
3329 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3330 | RETURN_FALSE; |
---|
3331 | } |
---|
3332 | |
---|
3333 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &prefix) == FAILURE) { |
---|
3334 | return; |
---|
3335 | } |
---|
3336 | |
---|
3337 | for (i = 0, iend = xc_var_hcache.size; i < iend; i ++) { |
---|
3338 | xc_cache_t *cache = &xc_var_caches[i]; |
---|
3339 | if (cache->cached->disabled) { |
---|
3340 | continue; |
---|
3341 | } |
---|
3342 | |
---|
3343 | ENTER_LOCK(cache) { |
---|
3344 | int entryslotid, jend; |
---|
3345 | for (entryslotid = 0, jend = cache->hentry->size; entryslotid < jend; entryslotid ++) { |
---|
3346 | xc_entry_t *entry, *next; |
---|
3347 | for (entry = cache->cached->entries[entryslotid]; entry; entry = next) { |
---|
3348 | next = entry->next; |
---|
3349 | if (xc_var_has_prefix(entry, prefix TSRMLS_CC)) { |
---|
3350 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entryslotid, entry TSRMLS_CC); |
---|
3351 | } |
---|
3352 | } |
---|
3353 | } |
---|
3354 | } LEAVE_LOCK(cache); |
---|
3355 | } |
---|
3356 | } |
---|
3357 | /* }}} */ |
---|
3358 | static inline void xc_var_inc_dec(int inc, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ |
---|
3359 | { |
---|
3360 | xc_entry_hash_t entry_hash; |
---|
3361 | xc_cache_t *cache; |
---|
3362 | xc_entry_var_t entry_var, *stored_entry_var; |
---|
3363 | zval *name; |
---|
3364 | long count = 1; |
---|
3365 | long value = 0; |
---|
3366 | zval oldzval; |
---|
3367 | VAR_BUFFER_FLAGS(name); |
---|
3368 | |
---|
3369 | if (!xc_var_caches) { |
---|
3370 | VAR_CACHE_NOT_INITIALIZED(); |
---|
3371 | RETURN_NULL(); |
---|
3372 | } |
---|
3373 | |
---|
3374 | entry_var.entry.ttl = XG(var_ttl); |
---|
3375 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &name, &count, &entry_var.entry.ttl) == FAILURE) { |
---|
3376 | return; |
---|
3377 | } |
---|
3378 | |
---|
3379 | /* max ttl */ |
---|
3380 | if (xc_var_maxttl && (!entry_var.entry.ttl || entry_var.entry.ttl > xc_var_maxttl)) { |
---|
3381 | entry_var.entry.ttl = xc_var_maxttl; |
---|
3382 | } |
---|
3383 | |
---|
3384 | VAR_BUFFER_INIT(name); |
---|
3385 | xc_entry_var_init_key(&entry_var, &entry_hash, &name_buffer TSRMLS_CC); |
---|
3386 | cache = &xc_var_caches[entry_hash.cacheid]; |
---|
3387 | |
---|
3388 | if (cache->cached->disabled) { |
---|
3389 | VAR_BUFFER_FREE(name); |
---|
3390 | RETURN_NULL(); |
---|
3391 | } |
---|
3392 | |
---|
3393 | ENTER_LOCK(cache) { |
---|
3394 | stored_entry_var = (xc_entry_var_t *) xc_entry_find_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &entry_var TSRMLS_CC); |
---|
3395 | if (stored_entry_var) { |
---|
3396 | TRACE("incdec: got entry_var %s", entry_var.entry.name.str.val); |
---|
3397 | /* do it in place */ |
---|
3398 | if (Z_TYPE_P(stored_entry_var->value) == IS_LONG) { |
---|
3399 | zval *zv; |
---|
3400 | stored_entry_var->entry.ctime = XG(request_time); |
---|
3401 | stored_entry_var->entry.ttl = entry_var.entry.ttl; |
---|
3402 | TRACE("%s", "incdec: islong"); |
---|
3403 | value = Z_LVAL_P(stored_entry_var->value); |
---|
3404 | value += (inc == 1 ? count : - count); |
---|
3405 | RETVAL_LONG(value); |
---|
3406 | |
---|
3407 | zv = (zval *) cache->shm->handlers->to_readwrite(cache->shm, (char *) stored_entry_var->value); |
---|
3408 | Z_LVAL_P(zv) = value; |
---|
3409 | ++cache->cached->updates; |
---|
3410 | break; /* leave lock */ |
---|
3411 | } |
---|
3412 | |
---|
3413 | TRACE("%s", "incdec: notlong"); |
---|
3414 | xc_processor_restore_zval(&oldzval, stored_entry_var->value, stored_entry_var->have_references TSRMLS_CC); |
---|
3415 | convert_to_long(&oldzval); |
---|
3416 | value = Z_LVAL(oldzval); |
---|
3417 | zval_dtor(&oldzval); |
---|
3418 | } |
---|
3419 | else { |
---|
3420 | TRACE("incdec: %s not found", entry_var.entry.name.str.val); |
---|
3421 | } |
---|
3422 | |
---|
3423 | value += (inc == 1 ? count : - count); |
---|
3424 | RETVAL_LONG(value); |
---|
3425 | entry_var.value = return_value; |
---|
3426 | |
---|
3427 | if (stored_entry_var) { |
---|
3428 | entry_var.entry.atime = stored_entry_var->entry.atime; |
---|
3429 | entry_var.entry.ctime = stored_entry_var->entry.ctime; |
---|
3430 | entry_var.entry.hits = stored_entry_var->entry.hits; |
---|
3431 | xc_entry_remove_unlocked(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_entry_var TSRMLS_CC); |
---|
3432 | } |
---|
3433 | xc_entry_var_store_unlocked(cache, entry_hash.entryslotid, &entry_var TSRMLS_CC); |
---|
3434 | } LEAVE_LOCK(cache); |
---|
3435 | VAR_BUFFER_FREE(name); |
---|
3436 | } |
---|
3437 | /* }}} */ |
---|
3438 | /* {{{ proto int xcache_inc(string name [, int value [, int ttl]]) |
---|
3439 | Increase an int counter in cache by specified name, create it if not exists */ |
---|
3440 | PHP_FUNCTION(xcache_inc) |
---|
3441 | { |
---|
3442 | xc_var_inc_dec(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3443 | } |
---|
3444 | /* }}} */ |
---|
3445 | /* {{{ proto int xcache_dec(string name [, int value [, int ttl]]) |
---|
3446 | Decrease an int counter in cache by specified name, create it if not exists */ |
---|
3447 | PHP_FUNCTION(xcache_dec) |
---|
3448 | { |
---|
3449 | xc_var_inc_dec(-1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3450 | } |
---|
3451 | /* }}} */ |
---|
3452 | static zend_function_entry xcache_cacher_functions[] = /* {{{ */ |
---|
3453 | { |
---|
3454 | PHP_FE(xcache_count, NULL) |
---|
3455 | PHP_FE(xcache_info, NULL) |
---|
3456 | PHP_FE(xcache_list, NULL) |
---|
3457 | PHP_FE(xcache_clear_cache, NULL) |
---|
3458 | PHP_FE(xcache_enable_cache, NULL) |
---|
3459 | PHP_FE(xcache_admin_namespace, NULL) |
---|
3460 | PHP_FE(xcache_set_namespace, NULL) |
---|
3461 | PHP_FE(xcache_get, NULL) |
---|
3462 | PHP_FE(xcache_set, NULL) |
---|
3463 | PHP_FE(xcache_isset, NULL) |
---|
3464 | PHP_FE(xcache_unset, NULL) |
---|
3465 | PHP_FE(xcache_unset_by_prefix, NULL) |
---|
3466 | PHP_FE(xcache_inc, NULL) |
---|
3467 | PHP_FE(xcache_dec, NULL) |
---|
3468 | PHP_FE_END |
---|
3469 | }; |
---|
3470 | /* }}} */ |
---|
3471 | |
---|
3472 | static int xc_cacher_zend_startup(zend_extension *extension) /* {{{ */ |
---|
3473 | { |
---|
3474 | if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { |
---|
3475 | if (xc_init() != SUCCESS) { |
---|
3476 | zend_error(E_ERROR, "XCache: Cannot init"); |
---|
3477 | return FAILURE; |
---|
3478 | } |
---|
3479 | xc_initized = 1; |
---|
3480 | xc_init_time = time(NULL); |
---|
3481 | xc_init_instance_id = getpid(); |
---|
3482 | #ifdef ZTS |
---|
3483 | xc_init_instance_subid = tsrm_thread_id(); |
---|
3484 | #endif |
---|
3485 | } |
---|
3486 | |
---|
3487 | if (xc_php_size) { |
---|
3488 | old_compile_file = zend_compile_file; |
---|
3489 | zend_compile_file = xc_compile_file; |
---|
3490 | } |
---|
3491 | |
---|
3492 | return SUCCESS; |
---|
3493 | } |
---|
3494 | /* }}} */ |
---|
3495 | static void xc_cacher_zend_shutdown(zend_extension *extension) /* {{{ */ |
---|
3496 | { |
---|
3497 | if (xc_initized) { |
---|
3498 | xc_destroy(); |
---|
3499 | } |
---|
3500 | } |
---|
3501 | /* }}} */ |
---|
3502 | /* {{{ zend extension definition structure */ |
---|
3503 | static zend_extension xc_cacher_zend_extension_entry = { |
---|
3504 | XCACHE_NAME " Cacher", |
---|
3505 | XCACHE_VERSION, |
---|
3506 | XCACHE_AUTHOR, |
---|
3507 | XCACHE_URL, |
---|
3508 | XCACHE_COPYRIGHT, |
---|
3509 | xc_cacher_zend_startup, |
---|
3510 | xc_cacher_zend_shutdown, |
---|
3511 | NULL, /* activate_func_t */ |
---|
3512 | NULL, /* deactivate_func_t */ |
---|
3513 | NULL, /* message_handler_func_t */ |
---|
3514 | NULL, /* op_array_handler_func_t */ |
---|
3515 | NULL, /* statement_handler_func_t */ |
---|
3516 | NULL, /* fcall_begin_handler_func_t */ |
---|
3517 | NULL, /* fcall_end_handler_func_t */ |
---|
3518 | NULL, /* op_array_ctor_func_t */ |
---|
3519 | NULL, /* op_array_dtor_func_t */ |
---|
3520 | STANDARD_ZEND_EXTENSION_PROPERTIES |
---|
3521 | }; |
---|
3522 | /* }}} */ |
---|
3523 | |
---|
3524 | /* {{{ ini */ |
---|
3525 | #ifdef ZEND_WIN32 |
---|
3526 | # define DEFAULT_PATH "xcache" |
---|
3527 | #else |
---|
3528 | # define DEFAULT_PATH "/dev/zero" |
---|
3529 | #endif |
---|
3530 | PHP_INI_BEGIN() |
---|
3531 | PHP_INI_ENTRY1 ("xcache.shm_scheme", "mmap", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_shm_scheme) |
---|
3532 | PHP_INI_ENTRY1 ("xcache.mmap_path", DEFAULT_PATH, PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_mmap_path) |
---|
3533 | PHP_INI_ENTRY1_EX ("xcache.readonly_protection", "0", PHP_INI_SYSTEM, xcache_OnUpdateBool, &xc_readonly_protection, zend_ini_boolean_displayer_cb) |
---|
3534 | /* opcode cache */ |
---|
3535 | PHP_INI_ENTRY1_EX ("xcache.admin.enable_auth", "1", PHP_INI_SYSTEM, xcache_OnUpdateBool, &xc_admin_enable_auth, zend_ini_boolean_displayer_cb) |
---|
3536 | PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3537 | PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3538 | PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3539 | PHP_INI_ENTRY1 ("xcache.allocator", "bestfit", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_php_allocator) |
---|
3540 | PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_ttl) |
---|
3541 | PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_gc_interval) |
---|
3542 | STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals) |
---|
3543 | STD_PHP_INI_BOOLEAN("xcache.stat", "1", PHP_INI_ALL, OnUpdateBool, stat, zend_xcache_globals, xcache_globals) |
---|
3544 | /* var cache */ |
---|
3545 | PHP_INI_ENTRY1 ("xcache.var_size", "0", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3546 | PHP_INI_ENTRY1 ("xcache.var_count", "1", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3547 | PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
3548 | PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_maxttl) |
---|
3549 | PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_gc_interval) |
---|
3550 | PHP_INI_ENTRY1 ("xcache.var_allocator", "bestfit", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_var_allocator) |
---|
3551 | STD_PHP_INI_ENTRY ("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals) |
---|
3552 | PHP_INI_ENTRY1 ("xcache.var_namespace_mode", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_namespace_mode) |
---|
3553 | PHP_INI_ENTRY1 ("xcache.var_namespace", "", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_var_namespace) |
---|
3554 | PHP_INI_END() |
---|
3555 | /* }}} */ |
---|
3556 | static PHP_MINFO_FUNCTION(xcache_cacher) /* {{{ */ |
---|
3557 | { |
---|
3558 | char buf[100]; |
---|
3559 | char *ptr; |
---|
3560 | int left, len; |
---|
3561 | xc_shm_scheme_t *scheme; |
---|
3562 | |
---|
3563 | php_info_print_table_start(); |
---|
3564 | php_info_print_table_row(2, "XCache Cacher Module", "enabled"); |
---|
3565 | php_info_print_table_row(2, "Readonly Protection", xc_readonly_protection ? "enabled" : "disabled"); |
---|
3566 | #ifdef ZEND_ENGINE_2_1 |
---|
3567 | ptr = php_format_date("Y-m-d H:i:s", sizeof("Y-m-d H:i:s") - 1, XG(request_time), 1 TSRMLS_CC); |
---|
3568 | php_info_print_table_row(2, "Page Request Time", ptr); |
---|
3569 | efree(ptr); |
---|
3570 | |
---|
3571 | ptr = php_format_date("Y-m-d H:i:s", sizeof("Y-m-d H:i:s") - 1, xc_init_time, 1 TSRMLS_CC); |
---|
3572 | php_info_print_table_row(2, "Cache Init Time", ptr); |
---|
3573 | efree(ptr); |
---|
3574 | #else |
---|
3575 | snprintf(buf, sizeof(buf), "%lu", (long unsigned) XG(request_time)); |
---|
3576 | php_info_print_table_row(2, "Page Request Time", buf); |
---|
3577 | |
---|
3578 | snprintf(buf, sizeof(buf), "%lu", (long unsigned) xc_init_time); |
---|
3579 | php_info_print_table_row(2, "Cache Init Time", buf); |
---|
3580 | #endif |
---|
3581 | |
---|
3582 | #ifdef ZTS |
---|
3583 | snprintf(buf, sizeof(buf), "%lu.%lu", xc_init_instance_id, xc_init_instance_subid); |
---|
3584 | #else |
---|
3585 | snprintf(buf, sizeof(buf), "%lu", xc_init_instance_id); |
---|
3586 | #endif |
---|
3587 | php_info_print_table_row(2, "Cache Instance Id", buf); |
---|
3588 | |
---|
3589 | if (xc_php_size) { |
---|
3590 | ptr = _php_math_number_format(xc_php_size, 0, '.', ','); |
---|
3591 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %lu split(s), with %lu slots each", ptr, (unsigned long) xc_php_hcache.size, xc_php_hentry.size); |
---|
3592 | php_info_print_table_row(2, "Opcode Cache", buf); |
---|
3593 | efree(ptr); |
---|
3594 | } |
---|
3595 | else { |
---|
3596 | php_info_print_table_row(2, "Opcode Cache", "disabled"); |
---|
3597 | } |
---|
3598 | if (xc_var_size) { |
---|
3599 | ptr = _php_math_number_format(xc_var_size, 0, '.', ','); |
---|
3600 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %lu split(s), with %lu slots each", ptr, (unsigned long) xc_var_hcache.size, xc_var_hentry.size); |
---|
3601 | php_info_print_table_row(2, "Variable Cache", buf); |
---|
3602 | efree(ptr); |
---|
3603 | } |
---|
3604 | else { |
---|
3605 | php_info_print_table_row(2, "Variable Cache", "disabled"); |
---|
3606 | } |
---|
3607 | |
---|
3608 | left = sizeof(buf); |
---|
3609 | ptr = buf; |
---|
3610 | buf[0] = '\0'; |
---|
3611 | for (scheme = xc_shm_scheme_first(); scheme; scheme = xc_shm_scheme_next(scheme)) { |
---|
3612 | len = snprintf(ptr, left, ptr == buf ? "%s" : ", %s", xc_shm_scheme_name(scheme)); |
---|
3613 | left -= len; |
---|
3614 | ptr += len; |
---|
3615 | } |
---|
3616 | php_info_print_table_row(2, "Shared Memory Schemes", buf); |
---|
3617 | |
---|
3618 | php_info_print_table_end(); |
---|
3619 | |
---|
3620 | DISPLAY_INI_ENTRIES(); |
---|
3621 | } |
---|
3622 | /* }}} */ |
---|
3623 | static int xc_config_hash(xc_hash_t *p, char *name, char *default_value) /* {{{ */ |
---|
3624 | { |
---|
3625 | size_t bits, size; |
---|
3626 | char *value; |
---|
3627 | |
---|
3628 | if (cfg_get_string(name, &value) != SUCCESS) { |
---|
3629 | value = default_value; |
---|
3630 | } |
---|
3631 | |
---|
3632 | p->size = zend_atoi(value, strlen(value)); |
---|
3633 | for (size = 1, bits = 1; size < p->size; bits ++, size <<= 1) { |
---|
3634 | /* empty body */ |
---|
3635 | } |
---|
3636 | p->size = size; |
---|
3637 | p->bits = bits; |
---|
3638 | p->mask = size - 1; |
---|
3639 | |
---|
3640 | return SUCCESS; |
---|
3641 | } |
---|
3642 | /* }}} */ |
---|
3643 | static int xc_config_long(zend_ulong *p, char *name, char *default_value) /* {{{ */ |
---|
3644 | { |
---|
3645 | char *value; |
---|
3646 | |
---|
3647 | if (cfg_get_string(name, &value) != SUCCESS) { |
---|
3648 | value = default_value; |
---|
3649 | } |
---|
3650 | |
---|
3651 | *p = zend_atol(value, strlen(value)); |
---|
3652 | return SUCCESS; |
---|
3653 | } |
---|
3654 | /* }}} */ |
---|
3655 | static PHP_MINIT_FUNCTION(xcache_cacher) /* {{{ */ |
---|
3656 | { |
---|
3657 | zend_extension *ext; |
---|
3658 | zend_llist_position lpos; |
---|
3659 | |
---|
3660 | ext = zend_get_extension("Zend Optimizer"); |
---|
3661 | if (ext) { |
---|
3662 | /* zend_optimizer.optimization_level>0 is not compatible with other cacher, disabling */ |
---|
3663 | ext->op_array_handler = NULL; |
---|
3664 | } |
---|
3665 | /* cache if there's an op_array_ctor */ |
---|
3666 | for (ext = zend_llist_get_first_ex(&zend_extensions, &lpos); |
---|
3667 | ext; |
---|
3668 | ext = zend_llist_get_next_ex(&zend_extensions, &lpos)) { |
---|
3669 | if (ext->op_array_ctor) { |
---|
3670 | xc_have_op_array_ctor = 1; |
---|
3671 | break; |
---|
3672 | } |
---|
3673 | } |
---|
3674 | |
---|
3675 | xc_config_long(&xc_php_size, "xcache.size", "0"); |
---|
3676 | xc_config_hash(&xc_php_hcache, "xcache.count", "1"); |
---|
3677 | xc_config_hash(&xc_php_hentry, "xcache.slots", "8K"); |
---|
3678 | |
---|
3679 | xc_config_long(&xc_var_size, "xcache.var_size", "0"); |
---|
3680 | xc_config_hash(&xc_var_hcache, "xcache.var_count", "1"); |
---|
3681 | xc_config_hash(&xc_var_hentry, "xcache.var_slots", "8K"); |
---|
3682 | |
---|
3683 | if (strcmp(sapi_module.name, "cli") == 0) { |
---|
3684 | if (!xc_test) { |
---|
3685 | /* disable cache for cli except for testing */ |
---|
3686 | xc_php_size = xc_var_size = 0; |
---|
3687 | } |
---|
3688 | } |
---|
3689 | |
---|
3690 | if (xc_php_size <= 0) { |
---|
3691 | xc_php_size = xc_php_hcache.size = 0; |
---|
3692 | } |
---|
3693 | if (xc_var_size <= 0) { |
---|
3694 | xc_var_size = xc_var_hcache.size = 0; |
---|
3695 | } |
---|
3696 | |
---|
3697 | xc_init_constant(module_number TSRMLS_CC); |
---|
3698 | |
---|
3699 | REGISTER_INI_ENTRIES(); |
---|
3700 | |
---|
3701 | xc_sandbox_module_init(module_number TSRMLS_CC); |
---|
3702 | return xcache_zend_extension_add(&xc_cacher_zend_extension_entry, 0); |
---|
3703 | } |
---|
3704 | /* }}} */ |
---|
3705 | static PHP_MSHUTDOWN_FUNCTION(xcache_cacher) /* {{{ */ |
---|
3706 | { |
---|
3707 | xc_sandbox_module_shutdown(); |
---|
3708 | |
---|
3709 | xcache_zend_extension_remove(&xc_cacher_zend_extension_entry); |
---|
3710 | UNREGISTER_INI_ENTRIES(); |
---|
3711 | |
---|
3712 | if (xc_mmap_path) { |
---|
3713 | pefree(xc_mmap_path, 1); |
---|
3714 | xc_mmap_path = NULL; |
---|
3715 | } |
---|
3716 | if (xc_shm_scheme) { |
---|
3717 | pefree(xc_shm_scheme, 1); |
---|
3718 | xc_shm_scheme = NULL; |
---|
3719 | } |
---|
3720 | if (xc_php_allocator) { |
---|
3721 | pefree(xc_php_allocator, 1); |
---|
3722 | xc_php_allocator = NULL; |
---|
3723 | } |
---|
3724 | if (xc_var_allocator) { |
---|
3725 | pefree(xc_var_allocator, 1); |
---|
3726 | xc_var_allocator = NULL; |
---|
3727 | } |
---|
3728 | if (xc_var_namespace) { |
---|
3729 | pefree(xc_var_namespace, 1); |
---|
3730 | xc_var_namespace = NULL; |
---|
3731 | } |
---|
3732 | |
---|
3733 | return SUCCESS; |
---|
3734 | } |
---|
3735 | /* }}} */ |
---|
3736 | static PHP_RINIT_FUNCTION(xcache_cacher) /* {{{ */ |
---|
3737 | { |
---|
3738 | xc_request_init(TSRMLS_C); |
---|
3739 | return SUCCESS; |
---|
3740 | } |
---|
3741 | /* }}} */ |
---|
3742 | /* {{{ static PHP_RSHUTDOWN_FUNCTION(xcache_cacher) */ |
---|
3743 | #ifndef ZEND_ENGINE_2 |
---|
3744 | static PHP_RSHUTDOWN_FUNCTION(xcache_cacher) |
---|
3745 | #else |
---|
3746 | static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(xcache_cacher) |
---|
3747 | #endif |
---|
3748 | { |
---|
3749 | #ifdef ZEND_ENGINE_2 |
---|
3750 | TSRMLS_FETCH(); |
---|
3751 | #endif |
---|
3752 | |
---|
3753 | xc_request_shutdown(TSRMLS_C); |
---|
3754 | return SUCCESS; |
---|
3755 | } |
---|
3756 | /* }}} */ |
---|
3757 | static zend_module_entry xcache_cacher_module_entry = { /* {{{ */ |
---|
3758 | STANDARD_MODULE_HEADER, |
---|
3759 | XCACHE_NAME " Cacher", |
---|
3760 | xcache_cacher_functions, |
---|
3761 | PHP_MINIT(xcache_cacher), |
---|
3762 | PHP_MSHUTDOWN(xcache_cacher), |
---|
3763 | PHP_RINIT(xcache_cacher), |
---|
3764 | #ifndef ZEND_ENGINE_2 |
---|
3765 | PHP_RSHUTDOWN(xcache_cacher), |
---|
3766 | #else |
---|
3767 | NULL, |
---|
3768 | #endif |
---|
3769 | PHP_MINFO(xcache_cacher), |
---|
3770 | XCACHE_VERSION, |
---|
3771 | #ifdef PHP_GINIT |
---|
3772 | NO_MODULE_GLOBALS, |
---|
3773 | #endif |
---|
3774 | #ifdef ZEND_ENGINE_2 |
---|
3775 | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(xcache_cacher), |
---|
3776 | #else |
---|
3777 | NULL, |
---|
3778 | NULL, |
---|
3779 | #endif |
---|
3780 | STANDARD_MODULE_PROPERTIES_EX |
---|
3781 | }; |
---|
3782 | /* }}} */ |
---|
3783 | int xc_cacher_startup_module() /* {{{ */ |
---|
3784 | { |
---|
3785 | return zend_startup_module(&xcache_cacher_module_entry); |
---|
3786 | } |
---|
3787 | /* }}} */ |
---|
3788 | void xc_cacher_disable() /* {{{ */ |
---|
3789 | { |
---|
3790 | time_t now = time(NULL); |
---|
3791 | size_t i; |
---|
3792 | |
---|
3793 | if (xc_php_caches) { |
---|
3794 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
3795 | if (xc_php_caches[i].cached) { |
---|
3796 | xc_php_caches[i].cached->disabled = now; |
---|
3797 | } |
---|
3798 | } |
---|
3799 | } |
---|
3800 | |
---|
3801 | if (xc_var_caches) { |
---|
3802 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
3803 | if (xc_var_caches[i].cached) { |
---|
3804 | xc_var_caches[i].cached->disabled = now; |
---|
3805 | } |
---|
3806 | } |
---|
3807 | } |
---|
3808 | } |
---|
3809 | /* }}} */ |
---|