forked from eaccelerator/eaccelerator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheaccelerator.c
More file actions
2360 lines (2155 loc) · 81.9 KB
/
eaccelerator.c
File metadata and controls
2360 lines (2155 loc) · 81.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| eAccelerator project |
+----------------------------------------------------------------------+
| Copyright (c) 2004 - 2012 eAccelerator |
| http://eaccelerator.net |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| MA 02111-1307, USA. |
| |
| A copy is availble at http://www.gnu.org/copyleft/gpl.txt |
+----------------------------------------------------------------------+
$Id: eaccelerator.c 426 2010-07-28 09:41:24Z bart $
*/
#include "eaccelerator.h"
#include "eaccelerator_version.h"
#ifdef HAVE_EACCELERATOR
#include "opcodes.h"
#include "zend.h"
#include "zend_API.h"
#include "zend_extensions.h"
#include "debug.h"
#include "ea_store.h"
#include "ea_restore.h"
#include "ea_info.h"
#include "ea_dasm.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifdef ZEND_WIN32
# include "fnmatch.h"
# include "win32/time.h"
# include <time.h>
# include <sys/utime.h>
#else
# include <fnmatch.h>
# include <sys/file.h>
# include <sys/time.h>
# include <utime.h>
#endif
#include <fcntl.h>
#ifndef O_BINARY
# define O_BINARY 0
#endif
#include "php.h"
#include "php_ini.h"
#include "php_logos.h"
#include "main/fopen_wrappers.h"
#include "ext/standard/info.h"
#include "ext/standard/php_incomplete_class.h"
#include "ext/standard/md5.h"
#include "SAPI.h"
#define MAX_DUP_STR_LEN 256
/* Globals (different for each process/thread) */
ZEND_DECLARE_MODULE_GLOBALS(eaccelerator)
/* Globals (common for each process/thread) */
static long ea_shm_size = 0;
static long ea_shm_ttl = 0;
static long ea_shm_prune_period = 0;
extern long ea_debug;
zend_bool ea_scripts_shm_only = 0;
eaccelerator_mm *ea_mm_instance = NULL;
static int ea_is_zend_extension = 0;
static int ea_is_extension = 0;
zend_extension* ZendOptimizer = NULL;
static HashTable ea_global_function_table;
static HashTable ea_global_class_table;
int binary_eaccelerator_version[2];
int binary_php_version[2];
int binary_zend_version[2];
/* pointer to the properties_info hashtable destructor */
extern dtor_func_t properties_info_dtor;
/* saved original functions */
static zend_op_array *(*ea_saved_zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
#ifdef DEBUG
static void (*ea_saved_zend_execute)(zend_op_array *op_array TSRMLS_DC);
#endif
/* external declarations */
PHPAPI void php_stripslashes(char *str, int *len TSRMLS_DC);
ZEND_DLEXPORT zend_op_array* eaccelerator_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
/******************************************************************************/
/* hash mm functions */
/******************************************************************************/
/* Find a script entry with the given hash key */
static ea_cache_entry* hash_find_mm(const char *key,
struct stat *buf,
int *nreloads,
time_t ttl TSRMLS_DC)
{
unsigned int hv, slot;
ea_cache_entry *p, *q;
hv = zend_get_hash_value((char *)key, strlen(key));
slot = hv & EA_HASH_MAX;
EACCELERATOR_LOCK_RW();
q = NULL;
p = ea_mm_instance->hash[slot];
while (p != NULL) {
if ((p->hv == hv) && (strcmp(p->realfilename, key) == 0)) {
if (EAG(check_mtime_enabled) && ea_mm_instance->check_mtime_enabled &&
(buf->st_mtime != p->mtime || buf->st_size != p->filesize)) {
/* key is invalid. Remove it. */
*nreloads = p->nreloads+1;
if (q == NULL) {
ea_mm_instance->hash[slot] = p->next;
} else {
q->next = p->next;
}
ea_mm_instance->hash_cnt--;
if (p->use_cnt > 0) {
/* key is used by other process/thread. Schedule it for removal */
p->removed = 1;
p->next = ea_mm_instance->removed;
ea_mm_instance->removed = p;
ea_mm_instance->rem_cnt++;
EACCELERATOR_UNLOCK_RW();
return NULL;
} else {
/* key is unused. Remove it. */
eaccelerator_free_nolock(p);
EACCELERATOR_UNLOCK_RW();
return NULL;
}
} else {
/* key is valid */
p->nhits++;
p->use_cnt++;
p->ttl = ttl;
EACCELERATOR_UNLOCK_RW();
return p;
}
}
q = p;
p = p->next;
}
EACCELERATOR_UNLOCK_RW();
return NULL;
}
/* Add a new entry to the hashtable */
static void hash_add_mm(ea_cache_entry *x)
{
ea_cache_entry *p,*q;
unsigned int slot;
x->hv = zend_get_hash_value(x->realfilename, strlen(x->realfilename));
slot = x->hv & EA_HASH_MAX;
EACCELERATOR_LOCK_RW();
x->next = ea_mm_instance->hash[slot];
ea_mm_instance->hash[slot] = x;
ea_mm_instance->hash_cnt++;
q = x;
p = x->next;
while (p != NULL) {
if ((p->hv == x->hv) &&
(strcmp(p->realfilename, x->realfilename) == 0)) {
q->next = p->next;
ea_mm_instance->hash_cnt--;
ea_mm_instance->hash[slot]->nreloads += p->nreloads;
if (p->use_cnt > 0) {
/* key is used by other process/thread. Shedule it to remove */
p->removed = 1;
p->next = ea_mm_instance->removed;
ea_mm_instance->removed = p;
ea_mm_instance->rem_cnt++;
EACCELERATOR_UNLOCK_RW();
return;
} else {
/* key is unused. Remove it. */
eaccelerator_free_nolock(p);
EACCELERATOR_UNLOCK_RW();
return;
}
}
q = p;
p = p->next;
}
EACCELERATOR_UNLOCK_RW();
}
/* Initialise the shared memory */
static int init_mm(TSRMLS_D)
{
pid_t owner = getpid();
MM *mm;
size_t total;
char mm_path[MAXPATHLEN];
#ifdef ZEND_WIN32
snprintf(mm_path, MAXPATHLEN, "%s.%s", EACCELERATOR_MM_FILE, sapi_module.name);
#else
snprintf(mm_path, MAXPATHLEN, "%s.%s%d", EACCELERATOR_MM_FILE, sapi_module.name, owner);
#endif
/* snprintf(mm_path, MAXPATHLEN, "%s.%s%d", EACCELERATOR_MM_FILE, sapi_module.name, geteuid());*/
if ((ea_mm_instance = (eaccelerator_mm*)mm_attach(ea_shm_size*1024*1024, mm_path)) != NULL) {
#ifdef ZTS
ea_mutex = tsrm_mutex_alloc();
#endif
return SUCCESS;
}
mm = mm_create(ea_shm_size*1024*1024, mm_path);
if (!mm) {
return FAILURE;
}
#ifdef ZEND_WIN32
DBG(ea_debug_printf, (EA_DEBUG, "init_mm [%d]\n", owner));
#else
DBG(ea_debug_printf, (EA_DEBUG, "init_mm [%d,%d]\n", owner, getppid()));
#endif
#ifdef ZTS
ea_mutex = tsrm_mutex_alloc();
#endif
total = mm_available(mm);
ea_mm_instance = mm_malloc_lock(mm, sizeof(eaccelerator_mm));
if (!ea_mm_instance) {
return FAILURE;
}
mm_set_attach(mm, ea_mm_instance);
memset(ea_mm_instance, 0, sizeof(eaccelerator_mm));
ea_mm_instance->owner = owner;
ea_mm_instance->mm = mm;
ea_mm_instance->total = total;
ea_mm_instance->hash_cnt = 0;
ea_mm_instance->rem_cnt = 0;
ea_mm_instance->enabled = 1;
ea_mm_instance->optimizer_enabled = 1;
ea_mm_instance->check_mtime_enabled = 1;
ea_mm_instance->removed = NULL;
ea_mm_instance->cache_dir_uid = 0;
ea_mm_instance->last_prune = time(NULL); /* this time() call is harmless since this is init phase */
return SUCCESS;
}
/* Clean up the shared memory */
static void shutdown_mm(TSRMLS_D)
{
if (ea_mm_instance) {
#ifdef ZEND_WIN32
if (ea_mm_instance->owner == getpid()) {
#else
if (getpgrp() == getpid()) {
#endif
MM *mm = ea_mm_instance->mm;
#ifdef ZEND_WIN32
DBG(ea_debug_printf, (EA_DEBUG, "shutdown_mm [%d]\n", getpid()));
#else
DBG(ea_debug_printf, (EA_DEBUG, "shutdown_mm [%d,%d]\n", getpid(), getppid()));
#endif
#ifdef ZTS
tsrm_mutex_free(ea_mutex);
#endif
if (mm) {
mm_destroy(mm);
}
ea_mm_instance = NULL;
}
}
}
void encode_version(const char *str, int *version, int *extra)
{
unsigned int a = 0;
unsigned int b = 0;
unsigned int c = 0;
unsigned int d = 0;
size_t len;
char s[255];
char buf[255];
len = strlen(str);
memcpy(buf, str, (len > 255) ? 255 : len);
buf[254] = '\0';
memset(s, 0, 255);
sscanf(str, "%u.%u.%u%s", &a, &b, &c, s);
if (s[0] == '.') {
sscanf(s, ".%u-%s", &d, buf);
} else if (s[0] == '-') {
memcpy(buf, &s[1], 254);
} else {
memcpy(buf, s, 255);
}
*version = ((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff);
if (buf[0] == 0) {
a = 0;
b = 0;
} else if (strncasecmp(buf, "rev", 3) == 0) {
a = 1;
sscanf(buf, "rev%u", &b);
} else if (strncasecmp(buf, "rc", 2) == 0) {
a = 2;
sscanf(buf, "rc%u", &b);
} else if (strncasecmp(buf, "beta", 4) == 0) {
a = 3;
sscanf(buf, "beta%u", &b);
} else {
a = 0xf;
// just encode the first 4 bytes
b = ((buf[0] & 0x7f) << 21) | ((buf[1] & 0x7f) << 14) | ((buf[2] & 0x7f) << 7) | (buf[3] & 0x7f);
}
*extra = ((a & 0xf) << 28) | (0x0fffffff & b);
}
#ifdef DEBUG
static void decode_version(int version, int extra, char *str, size_t len)
{
int number;
if ((version & 0xff) == 0) {
number = snprintf(str, len, "%u.%u.%u", (version >> 24), ((version >> 16) & 0xff), ((version >> 8) & 0xff));
} else {
number = snprintf(str, len, "%u.%u.%u.%u", (version >> 24), ((version >> 16) & 0xff), ((version >> 8) & 0xff), (version & 0xff));
}
if (extra != 0) {
unsigned int type = ((extra >> 28) & 0xf);
extra = (extra & 0x0fffffff);
switch (type) {
case 1:
snprintf(&str[number], len, "-rev%u", extra);
break;
case 2:
snprintf(&str[number], len, "-rc%u", extra);
break;
case 3:
snprintf(&str[number], len, "-beta%u", extra);
break;
case 15:
if ((int)len >= number + 5) {
str[number] = '-';
str[number + 1] = (extra >> 21) & 0x7f;
str[number + 2] = (extra >> 14) & 0x7f;
str[number + 3] = (extra >> 7) & 0x7f;
str[number + 4] = extra & 0x7f;
str[number + 5] = '\0';
}
break;
default:
break;
}
}
}
#endif
static char num2hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
/* Function to create a hash key when filenames are used */
int eaccelerator_md5(char* s, const char* prefix, const char* key TSRMLS_DC)
{
char md5str[33];
PHP_MD5_CTX context;
unsigned char digest[16];
int i;
int n;
md5str[0] = '\0';
PHP_MD5Init(&context);
PHP_MD5Update(&context, (unsigned char*)key, strlen(key));
PHP_MD5Final(digest, &context);
make_digest(md5str, digest);
snprintf(s, MAXPATHLEN-1, "%s/%d/", EAG(cache_dir), ea_mm_instance->cache_dir_uid);
n = strlen(s);
for (i = 0; i < EACCELERATOR_HASH_LEVEL && n < MAXPATHLEN - 1; i++) {
s[n++] = md5str[i];
s[n++] = '/';
}
s[n] = 0;
snprintf(&s[n], MAXPATHLEN-1-n, "%s%s", prefix, md5str);
return 1;
}
/* Remove expired keys, content and scripts from the memory cache */
void eaccelerator_prune(time_t t)
{
unsigned int i;
EACCELERATOR_LOCK_RW();
ea_mm_instance->last_prune = t;
for (i = 0; i < EA_HASH_SIZE; i++) {
ea_cache_entry **p = &ea_mm_instance->hash[i];
while (*p != NULL) {
struct stat buf;
if (((*p)->ttl != 0 && (*p)->ttl < t && (*p)->use_cnt <= 0) ||
stat((*p)->realfilename,&buf) != 0 ||
(*p)->mtime != buf.st_mtime ||
(*p)->filesize != buf.st_size) {
ea_cache_entry *r = *p;
*p = (*p)->next;
ea_mm_instance->hash_cnt--;
eaccelerator_free_nolock(r);
} else {
p = &(*p)->next;
}
}
}
EACCELERATOR_UNLOCK_RW();
}
/* Allocate a new cache chunk */
void* eaccelerator_malloc2(size_t size TSRMLS_DC)
{
void *p = NULL;
if (ea_shm_prune_period > 0) {
if (EAG(req_start) - ea_mm_instance->last_prune > ea_shm_prune_period) {
eaccelerator_prune(EAG(req_start));
p = eaccelerator_malloc(size);
}
}
return p;
}
#define EACCELERATOR_CRC32(crc, ch) (crc = (crc >> 8) ^ crc32tab[(crc ^ (ch)) & 0xff])
static const unsigned int crc32tab[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
unsigned int eaccelerator_crc32(const char *p, size_t n)
{
unsigned int crc = ~0;
for (; n--; ++p) {
EACCELERATOR_CRC32(crc, *p);
}
return ~crc;
}
/******************************************************************************/
/* Cache file functions. */
/******************************************************************************/
/* A function to check if the header of a cache file valid is.
*/
inline int check_header(ea_file_header *hdr)
{
#ifdef DEBUG
char current[255];
char cache[255];
#endif
if (strncmp(hdr->magic, EA_MAGIC, 8) != 0) {
#ifdef DEBUG
ea_debug_printf(EA_DEBUG, "Magic header mismatch.");
#endif
return 0;
}
if (hdr->eaccelerator_version[0] != binary_eaccelerator_version[0]
|| hdr->eaccelerator_version[1] != binary_eaccelerator_version[1]) {
#ifdef DEBUG
decode_version(hdr->eaccelerator_version[0], hdr->eaccelerator_version[1], cache, 255);
decode_version(binary_eaccelerator_version[0], binary_eaccelerator_version[1], current, 255);
ea_debug_printf(EA_DEBUG, "eAccelerator version mismatch, cache file %s and current version %s\n", cache, current);
#endif
return 0;
}
if (hdr->zend_version[0] != binary_zend_version[0]
|| hdr->zend_version[1] != binary_zend_version[1]) {
#ifdef DEBUG
decode_version(hdr->zend_version[0], hdr->zend_version[1], cache, 255);
decode_version(binary_zend_version[0], binary_zend_version[1], current, 255);
ea_debug_printf(EA_DEBUG, "Zend version mismatch, cache file %s and current version %s\n", cache, current);
#endif
return 0;
}
if (hdr->php_version[0] != binary_php_version[0]
|| hdr->php_version[1] != binary_php_version[1]) {
#ifdef DEBUG
decode_version(hdr->php_version[0], hdr->php_version[1], cache, 255);
decode_version(binary_php_version[0], binary_php_version[1], current, 255);
ea_debug_printf(EA_DEBUG, "PHP version mismatch, cache file %s and current version %s\n", cache, current);
#endif
return 0;
}
return 1;
}
/* A function to create the header for a cache file.
*/
inline void init_header(ea_file_header *hdr)
{
strncpy(hdr->magic, EA_MAGIC, 8);
hdr->eaccelerator_version[0] = binary_eaccelerator_version[0];
hdr->eaccelerator_version[1] = binary_eaccelerator_version[1];
hdr->zend_version[0] = binary_zend_version[0];
hdr->zend_version[1] = binary_zend_version[1];
hdr->php_version[0] = binary_php_version[0];
hdr->php_version[1] = binary_php_version[1];
}
/* Retrieve a cache entry from the cache directory */
static ea_cache_entry* hash_find_file(const char *key, struct stat *buf TSRMLS_DC)
{
int f;
char s[MAXPATHLEN];
ea_file_header hdr;
ea_cache_entry *p = NULL;
int use_shm = 1;
if (!eaccelerator_md5(s, "/eaccelerator-", key TSRMLS_CC)) {
return NULL;
}
if ((f = open(s, O_RDONLY | O_BINARY)) > 0) {
EACCELERATOR_FLOCK(f, LOCK_SH);
if (read(f, &hdr, sizeof(hdr)) != sizeof(hdr)) {
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
return NULL;
}
if (!check_header(&hdr)) {
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
unlink(s);
return NULL;
}
p = eaccelerator_malloc(hdr.size);
if (p == NULL) {
p = eaccelerator_malloc2(hdr.size TSRMLS_CC);
}
if (p == NULL) {
p = emalloc(hdr.size);
use_shm = 0;
}
if (p == NULL) {
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
return NULL;
}
if (read(f, p, hdr.size) != hdr.size ||
p->size != hdr.size ||
hdr.crc32 != eaccelerator_crc32((const char*)p,p->size)) {
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
unlink(s);
if (use_shm) {
eaccelerator_free(p);
} else {
efree(p);
}
DBG(ea_debug_printf, (EA_DEBUG, "cache file is corrupted\n"));
return NULL;
}
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
if (strcmp(key,p->realfilename) != 0) {
if (use_shm) {
eaccelerator_free(p);
} else {
efree(p);
}
return NULL;
}
if ((EAG(check_mtime_enabled) && ea_mm_instance->check_mtime_enabled &&
(buf->st_mtime != p->mtime || buf->st_size != p->filesize))
) {
/* key is invalid. Remove it. */
if (use_shm) {
eaccelerator_free(p);
} else {
efree(p);
}
unlink(s);
return NULL;
}
eaccelerator_fixup(p TSRMLS_CC);
if (use_shm) {
p->nhits = 1;
p->nreloads = 1;
p->use_cnt = 1;
p->removed = 0;
if (ea_shm_ttl > 0) {
p->ttl = EAG(req_start) + ea_shm_ttl;
} else {
p->ttl = 0;
}
p->ts = hdr.ts; /* get cached item creation timestamp from cache file */
hash_add_mm(p);
} else {
p->use_cnt = 0;
p->removed = 1;
}
mm_check_mem(p);
return p;
}
return NULL;
}
/* Add a cache entry to the cache directory */
static int hash_add_file(ea_cache_entry *p TSRMLS_DC)
{
int f;
int ret = 0;
char s[MAXPATHLEN];
ea_file_header hdr;
if (!eaccelerator_md5(s, "/eaccelerator-", p->realfilename TSRMLS_CC)) {
return 0;
}
unlink(s);
f = open(s, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, S_IRUSR | S_IWUSR);
if (f > 0) {
EACCELERATOR_FLOCK(f, LOCK_EX);
init_header(&hdr);
hdr.size = p->size;
hdr.mtime = p->mtime;
hdr.ts = p->ts;
p->next = p;
hdr.crc32 = eaccelerator_crc32((const char*)p,p->size);
ret = (write(f, &hdr, sizeof(hdr)) == sizeof(hdr));
if (ret) {
ret = (write(f, p, p->size) == p->size);
}
EACCELERATOR_FLOCK(f, LOCK_UN);
close(f);
} else {
ea_debug_log("EACCELERATOR: Open for write failed for \"%s\": %s\n", s, strerror(errno));
}
return ret;
}
/* called after succesful compilation, from eaccelerator_compile file */
/* Adds the data from the compilation of the script to the cache */
static int eaccelerator_store(char* key, struct stat *buf, int nreloads,
zend_op_array* op_array,
Bucket* f, Bucket *c TSRMLS_DC)
{
ea_cache_entry *p;
int len = strlen(key);
int use_shm = 1;
int ret = 0;
int size = 0;
void *data = NULL;
zend_try {
size = calc_size(key, op_array, f, c TSRMLS_CC);
} zend_catch {
size = 0;
} zend_end_try();
if (size == 0) {
return 0;
}
EAG(mem) = eaccelerator_malloc(size);
if (EAG(mem) == NULL) {
EAG(mem) = eaccelerator_malloc2(size TSRMLS_CC);
}
if (!EAG(mem) && !ea_scripts_shm_only) {
EAG(mem) = emalloc(size);
use_shm = 0;
}
if (EAG(mem)) {
data = EAG(mem);
memset(EAG(mem), 0, size);
p = (ea_cache_entry *)EAG(mem);
eaccelerator_store_int(p, key, len, op_array, f, c TSRMLS_CC);
p->mtime = buf->st_mtime;
p->ts = EAG(req_start);
p->filesize = buf->st_size;
p->size = size;
p->nreloads = nreloads;
if (use_shm) {
if (ea_shm_ttl > 0) {
p->ttl = EAG(req_start) + ea_shm_ttl;
} else {
p->ttl = 0;
}
if (!ea_scripts_shm_only) {
hash_add_file(p TSRMLS_CC);
}
hash_add_mm(p);
ret = 1;
mm_check_mem(data);
} else {
ret = hash_add_file(p TSRMLS_CC);
efree(p);
}
}
return ret;
}
/* Try to restore a file from the cache. If the file isn't found in memory, the
the disk cache is checked */
static zend_op_array* eaccelerator_restore(char *realname, struct stat *buf,
int *nreloads, time_t compile_time TSRMLS_DC)
{
ea_cache_entry *p;
zend_op_array *op_array = NULL;
*nreloads = 1;
p = hash_find_mm(realname, buf, nreloads, ((ea_shm_ttl > 0)?(compile_time + ea_shm_ttl):0) TSRMLS_CC);
if (p == NULL && !ea_scripts_shm_only) {
p = hash_find_file(realname, buf TSRMLS_CC);
}
if (p != NULL && p->op_array != NULL) {
/* only restore file when open_basedir allows it */
if (PG(open_basedir) && php_check_open_basedir(p->realfilename TSRMLS_CC)) {
return NULL;
}
EAG(class_entry) = NULL;
op_array = restore_op_array(NULL, p->op_array TSRMLS_CC);
if (op_array != NULL) {
ea_fc_entry *e;
ea_used_entry *used = emalloc(sizeof(ea_used_entry));
used->entry = p;
used->next = (ea_used_entry*)EAG(used_entries);
EAG(used_entries) = (void*)used;
EAG(mem) = (char*)op_array->filename;
/* only restore the classes and functions when we restore this script
* for the first time.
*/
if (!zend_hash_exists(&EAG(restored), p->realfilename, strlen(p->realfilename))) {
for (e = p->c_head; e!=NULL; e = e->next) {
restore_class(e TSRMLS_CC);
}
for (e = p->f_head; e!=NULL; e = e->next) {
restore_function(e TSRMLS_CC);
}
zend_hash_add(&EAG(restored), p->realfilename, strlen(p->realfilename), NULL, 0, NULL);
}
EAG(mem) = p->realfilename;
}
#ifdef ZEND_COMPILE_DELAYED_BINDING
zend_do_delayed_early_binding(op_array TSRMLS_CC);
#endif
}
return op_array;
}
/*
* Returns the realpath for given filename according to include path
*/
static char *ea_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC)
{
char resolved_path[MAXPATHLEN];
char trypath[MAXPATHLEN];
const char *ptr, *end, *p;
char *actual_path;
php_stream_wrapper *wrapper;
if (!filename) {
return NULL;
}
/* Don't resolve paths which contain protocol (except of file://) */
for (p = filename; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
;
}
if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) {
wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
if (wrapper == &php_plain_files_wrapper) {
if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
}
}
return NULL;
}
if ((*filename == '.' &&
(IS_SLASH(filename[1]) ||
((filename[1] == '.') && IS_SLASH(filename[2])))) ||
IS_ABSOLUTE_PATH(filename, filename_length) ||
!path ||
!*path) {
if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
} else {
return NULL;
}
}
ptr = path;
while (ptr && *ptr) {
/* Check for stream wrapper */
int is_stream_wrapper = 0;
for (p = ptr; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
;
}
if ((*p == ':') && (p - ptr > 1) && (p[1] == '/') && (p[2] == '/')) {
/* .:// or ..:// is not a stream wrapper */
if (p[-1] != '.' || p[-2] != '.' || p - 2 != ptr) {
p += 3;
is_stream_wrapper = 1;
}
}
end = strchr(p, DEFAULT_DIR_SEPARATOR);
if (end) {
if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
ptr = end + 1;
continue;
}
memcpy(trypath, ptr, end-ptr);
trypath[end-ptr] = '/';
memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
ptr = end+1;
} else {
int len = strlen(ptr);
if (len + 1 + filename_length + 1 >= MAXPATHLEN) {
break;
}
memcpy(trypath, ptr, len);
trypath[len] = '/';
memcpy(trypath+len+1, filename, filename_length+1);
ptr = NULL;
}
actual_path = trypath;
if (is_stream_wrapper) {
wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
if (!wrapper) {
continue;
} else if (wrapper != &php_plain_files_wrapper) {
if (wrapper->wops->url_stat) {
php_stream_statbuf ssb;
if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
return estrdup(trypath);
}
}
continue;
}
}
if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
}
} /* end provided path */
/* check in calling scripts' current working directory as a fall back case
*/
if (zend_is_executing(TSRMLS_C)) {
#ifdef ZEND_ENGINE_2_4
const char *exec_fname = zend_get_executed_filename(TSRMLS_C);
#else
char *exec_fname = zend_get_executed_filename(TSRMLS_C);
#endif
int exec_fname_length = strlen(exec_fname);
while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length])) {
;
}
if (exec_fname && exec_fname[0] != '[' &&
exec_fname_length > 0 &&
exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
memcpy(trypath, exec_fname, exec_fname_length + 1);
memcpy(trypath+exec_fname_length + 1, filename, filename_length+1);
actual_path = trypath;
/* Check for stream wrapper */
for (p = trypath; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
;
}
if ((*p == ':') && (p - trypath > 1) && (p[1] == '/') && (p[2] == '/')) {
wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
if (!wrapper) {
return NULL;
} else if (wrapper != &php_plain_files_wrapper) {
if (wrapper->wops->url_stat) {
php_stream_statbuf ssb;
if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
return estrdup(trypath);
}
}
return NULL;
}
}
if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
}
}
}
return NULL;
}
/**
* Get the real filename of the file represented by the given file_handle.
* If unable to determine the realfilename this function returns 0, otherwise
* it returns 1.
*
* realfilename should be MAXPATHLEN long.
*/
static int ea_get_realname(zend_file_handle *file_handle, char* realname TSRMLS_DC)
{
// at least one of these should have value
if (file_handle->opened_path == NULL && file_handle->filename == NULL) {
return 0;
}
if (file_handle->opened_path != NULL) {
strcpy(realname, file_handle->opened_path);
return 1;
}
if (PG(include_path) == NULL ||
file_handle->filename[0] == '.' ||
IS_SLASH(file_handle->filename[0]) ||
IS_ABSOLUTE_PATH(file_handle->filename, strlen(file_handle->filename))) {