PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/opcache - ZendAccelerator.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 603 1196 50.4 %
Date: 2015-08-04 Functions: 37 59 62.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include "main/php.h"
      23             : #include "main/php_globals.h"
      24             : #include "zend.h"
      25             : #include "zend_extensions.h"
      26             : #include "zend_compile.h"
      27             : #include "ZendAccelerator.h"
      28             : #include "zend_persist.h"
      29             : #include "zend_shared_alloc.h"
      30             : #include "zend_accelerator_module.h"
      31             : #include "zend_accelerator_blacklist.h"
      32             : #include "zend_list.h"
      33             : #include "zend_execute.h"
      34             : #include "main/SAPI.h"
      35             : #include "main/php_streams.h"
      36             : #include "main/php_open_temporary_file.h"
      37             : #include "zend_API.h"
      38             : #include "zend_ini.h"
      39             : #include "zend_virtual_cwd.h"
      40             : #include "zend_accelerator_util_funcs.h"
      41             : #include "zend_accelerator_hash.h"
      42             : #include "ext/pcre/php_pcre.h"
      43             : 
      44             : #ifdef HAVE_OPCACHE_FILE_CACHE
      45             : # include "zend_file_cache.h"
      46             : # include "ext/standard/md5.h"
      47             : #endif
      48             : 
      49             : #ifndef ZEND_WIN32
      50             : #include  <netdb.h>
      51             : #endif
      52             : 
      53             : #ifdef ZEND_WIN32
      54             : typedef int uid_t;
      55             : typedef int gid_t;
      56             : #include <io.h>
      57             : #endif
      58             : 
      59             : #ifndef ZEND_WIN32
      60             : # include <sys/time.h>
      61             : #else
      62             : # include <process.h>
      63             : #endif
      64             : 
      65             : #ifdef HAVE_UNISTD_H
      66             : # include <unistd.h>
      67             : #endif
      68             : #include <fcntl.h>
      69             : #include <signal.h>
      70             : #include <time.h>
      71             : 
      72             : #ifndef ZEND_WIN32
      73             : # include <sys/types.h>
      74             : # include <sys/ipc.h>
      75             : #endif
      76             : 
      77             : #include <sys/stat.h>
      78             : #include <errno.h>
      79             : 
      80             : #define SHM_PROTECT() \
      81             :         do { \
      82             :                 if (ZCG(accel_directives).protect_memory) { \
      83             :                         zend_accel_shared_protect(1); \
      84             :                 } \
      85             :         } while (0)
      86             : #define SHM_UNPROTECT() \
      87             :         do { \
      88             :                 if (ZCG(accel_directives).protect_memory) { \
      89             :                         zend_accel_shared_protect(0); \
      90             :                 } \
      91             :         } while (0)
      92             : 
      93             : ZEND_EXTENSION();
      94             : 
      95             : #ifndef ZTS
      96             : zend_accel_globals accel_globals;
      97             : #else
      98             : int accel_globals_id;
      99             : #if defined(COMPILE_DL_OPCACHE)
     100             : ZEND_TSRMLS_CACHE_DEFINE();
     101             : #endif
     102             : #endif
     103             : 
     104             : /* Points to the structure shared across all PHP processes */
     105             : zend_accel_shared_globals *accel_shared_globals = NULL;
     106             : 
     107             : /* true globals, no need for thread safety */
     108             : zend_bool accel_startup_ok = 0;
     109             : static char *zps_failure_reason = NULL;
     110             : char *zps_api_failure_reason = NULL;
     111             : 
     112             : static zend_op_array *(*accelerator_orig_compile_file)(zend_file_handle *file_handle, int type);
     113             : static int (*accelerator_orig_zend_stream_open_function)(const char *filename, zend_file_handle *handle );
     114             : static zend_string *(*accelerator_orig_zend_resolve_path)(const char *filename, int filename_len);
     115             : static void (*orig_chdir)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
     116             : static ZEND_INI_MH((*orig_include_path_on_modify)) = NULL;
     117             : 
     118             : #ifdef ZEND_WIN32
     119             : # define INCREMENT(v) InterlockedIncrement64(&ZCSG(v))
     120             : # define DECREMENT(v) InterlockedDecrement64(&ZCSG(v))
     121             : # define LOCKVAL(v)   (ZCSG(v))
     122             : #endif
     123             : 
     124             : #ifdef ZEND_WIN32
     125             : static time_t zend_accel_get_time(void)
     126             : {
     127             :         FILETIME now;
     128             :         GetSystemTimeAsFileTime(&now);
     129             : 
     130             :         return (time_t) ((((((__int64)now.dwHighDateTime) << 32)|now.dwLowDateTime) - 116444736000000000L)/10000000);
     131             : }
     132             : #else
     133             : # define zend_accel_get_time() time(NULL)
     134             : #endif
     135             : 
     136         446 : static inline int is_stream_path(const char *filename)
     137             : {
     138             :         const char *p;
     139             : 
     140        3938 :         for (p = filename;
     141        1089 :              (*p >= 'a' && *p <= 'z') ||
     142         475 :              (*p >= 'A' && *p <= 'Z') ||
     143         503 :              (*p >= '0' && *p <= '9') ||
     144        1425 :              *p == '+' || *p == '-' || *p == '.';
     145         336 :              p++);
     146         446 :         return ((p != filename) && (p[0] == ':') && (p[1] == '/') && (p[2] == '/'));
     147             : }
     148             : 
     149          28 : static inline int is_cacheable_stream_path(const char *filename)
     150             : {
     151          56 :         return memcmp(filename, "file://", sizeof("file://") - 1) == 0 ||
     152          28 :                memcmp(filename, "phar://", sizeof("phar://") - 1) == 0;
     153             : }
     154             : 
     155             : /* O+ overrides PHP chdir() function and remembers the current working directory
     156             :  * in ZCG(cwd) and ZCG(cwd_len). Later accel_getcwd() can use stored value and
     157             :  * avoid getcwd() call.
     158             :  */
     159           0 : static ZEND_FUNCTION(accel_chdir)
     160             : {
     161             :         char cwd[MAXPATHLEN];
     162             : 
     163           0 :         orig_chdir(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     164           0 :         if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
     165           0 :                 if (ZCG(cwd)) {
     166           0 :                         zend_string_release(ZCG(cwd));
     167             :                 }
     168           0 :                 ZCG(cwd) = zend_string_init(cwd, strlen(cwd), 0);
     169             :         } else {
     170           0 :                 if (ZCG(cwd)) {
     171           0 :                         zend_string_release(ZCG(cwd));
     172           0 :                         ZCG(cwd) = NULL;
     173             :                 }
     174             :         }
     175           0 :         ZCG(cwd_key_len) = 0;
     176           0 :         ZCG(cwd_check) = 1;
     177           0 : }
     178             : 
     179          25 : static inline zend_string* accel_getcwd(void)
     180             : {
     181          25 :         if (ZCG(cwd)) {
     182           0 :                 return ZCG(cwd);
     183             :         } else {
     184             :                 char cwd[MAXPATHLEN + 1];
     185             : 
     186          25 :                 if (!VCWD_GETCWD(cwd, MAXPATHLEN)) {
     187           0 :                         return NULL;
     188             :                 }
     189          50 :                 ZCG(cwd) = zend_string_init(cwd, strlen(cwd), 0);
     190          25 :                 ZCG(cwd_key_len) = 0;
     191          25 :                 ZCG(cwd_check) = 1;
     192          25 :                 return ZCG(cwd);
     193             :         }
     194             : }
     195             : 
     196           0 : void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason)
     197             : {
     198           0 :         if ((((double) ZSMMG(wasted_shared_memory)) / ZCG(accel_directives).memory_consumption) >= ZCG(accel_directives).max_wasted_percentage) {
     199           0 :                 zend_accel_schedule_restart(reason);
     200             :         }
     201           0 : }
     202             : 
     203             : /* O+ tracks changes of "include_path" directive. It stores all the requested
     204             :  * values in ZCG(include_paths) shared hash table, current value in
     205             :  * ZCG(include_path)/ZCG(include_path_len) and one letter "path key" in
     206             :  * ZCG(include_path_key).
     207             :  */
     208           6 : static ZEND_INI_MH(accel_include_path_on_modify)
     209             : {
     210           6 :         int ret = orig_include_path_on_modify(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
     211             : 
     212           6 :         if (ret == SUCCESS) {
     213           6 :                 ZCG(include_path) = new_value;
     214           6 :                 ZCG(include_path_key_len) = 0;
     215           6 :                 ZCG(include_path_check) = 1;
     216             :         }
     217           6 :         return ret;
     218             : }
     219             : 
     220             : /* Interned strings support */
     221             : static zend_string *(*orig_new_interned_string)(zend_string *str);
     222             : static void (*orig_interned_strings_snapshot)(void);
     223             : static void (*orig_interned_strings_restore)(void);
     224             : 
     225             : /* O+ disables creation of interned strings by regular PHP compiler, instead,
     226             :  * it creates interned strings in shared memory when saves a script.
     227             :  * Such interned strings are shared across all PHP processes
     228             :  */
     229        4538 : static zend_string *accel_new_interned_string_for_php(zend_string *str)
     230             : {
     231        4538 :         return str;
     232             : }
     233             : 
     234         374 : static void accel_interned_strings_snapshot_for_php(void)
     235             : {
     236         374 : }
     237             : 
     238         374 : static void accel_interned_strings_restore_for_php(void)
     239             : {
     240         374 : }
     241             : 
     242             : #ifndef ZTS
     243           0 : static void accel_interned_strings_restore_state(void)
     244             : {
     245           0 :     uint idx = ZCSG(interned_strings).nNumUsed;
     246             :     uint nIndex;
     247             :     Bucket *p;
     248             : 
     249           0 :         ZCSG(interned_strings_top) = ZCSG(interned_strings_saved_top);
     250           0 :     while (idx > 0) {
     251           0 :         idx--;
     252           0 :                 p = ZCSG(interned_strings).arData + idx;
     253           0 :                 if ((char*)p->key < ZCSG(interned_strings_top)) break;
     254           0 :                 ZCSG(interned_strings).nNumUsed--;
     255           0 :                 ZCSG(interned_strings).nNumOfElements--;
     256             : 
     257           0 :                 nIndex = p->h | ZCSG(interned_strings).nTableMask;
     258           0 :                 if (HT_HASH(&ZCSG(interned_strings), nIndex) == HT_IDX_TO_HASH(idx)) {
     259           0 :                         HT_HASH(&ZCSG(interned_strings), nIndex) = Z_NEXT(p->val);
     260             :                 } else {
     261           0 :                         uint32_t prev = HT_HASH(&ZCSG(interned_strings), nIndex);
     262           0 :                         while (Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val) != idx) {
     263           0 :                                 prev = Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val);
     264             :                         }
     265           0 :                         Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val) = Z_NEXT(p->val);
     266             :                 }
     267             :         }
     268           0 : }
     269             : 
     270         374 : static void accel_interned_strings_save_state(void)
     271             : {
     272         374 :         ZCSG(interned_strings_saved_top) = ZCSG(interned_strings_top);
     273         374 : }
     274             : #endif
     275             : 
     276             : #ifndef ZTS
     277          50 : static zend_string *accel_find_interned_string(zend_string *str)
     278             : {
     279             : /* for now interned strings are supported only for non-ZTS build */
     280             :         zend_ulong h;
     281             :         uint nIndex;
     282             :         uint idx;
     283             :         Bucket *arData, *p;
     284             : 
     285          50 :         if (IS_ACCEL_INTERNED(str)) {
     286             :                 /* this is already an interned string */
     287           2 :                 return str;
     288             :         }
     289             : 
     290          48 :         h = zend_string_hash_val(str);
     291          48 :         nIndex = h | ZCSG(interned_strings).nTableMask;
     292             : 
     293             :         /* check for existing interned string */
     294          48 :         idx = HT_HASH(&ZCSG(interned_strings), nIndex);
     295          48 :         arData = ZCSG(interned_strings).arData;
     296          96 :         while (idx != HT_INVALID_IDX) {
     297           0 :                 p = HT_HASH_TO_BUCKET_EX(arData, idx);
     298           0 :                 if ((p->h == h) && (p->key->len == str->len)) {
     299           0 :                         if (!memcmp(p->key->val, str->val, str->len)) {
     300           0 :                                 return p->key;
     301             :                         }
     302             :                 }
     303           0 :                 idx = Z_NEXT(p->val);
     304             :         }
     305             : 
     306          48 :         return NULL;
     307             : }
     308             : #endif
     309             : 
     310     4138876 : zend_string *accel_new_interned_string(zend_string *str)
     311             : {
     312             : /* for now interned strings are supported only for non-ZTS build */
     313             : #ifndef ZTS
     314             :         zend_ulong h;
     315             :         uint nIndex;
     316             :         uint idx;
     317             :         Bucket *p;
     318             : 
     319             : #ifdef HAVE_OPCACHE_FILE_CACHE
     320             :         if (ZCG(accel_directives).file_cache_only) {
     321             :                 return str;
     322             :         }
     323             : #endif
     324             : 
     325     4138876 :         if (IS_ACCEL_INTERNED(str)) {
     326             :                 /* this is already an interned string */
     327           0 :                 return str;
     328             :         }
     329             : 
     330     4138876 :         h = zend_string_hash_val(str);
     331     4138876 :         nIndex = h | ZCSG(interned_strings).nTableMask;
     332             : 
     333             :         /* check for existing interned string */
     334     4138876 :         idx = HT_HASH(&ZCSG(interned_strings), nIndex);
     335     8328692 :         while (idx != HT_INVALID_IDX) {
     336     2553212 :                 p = HT_HASH_TO_BUCKET(&ZCSG(interned_strings), idx);
     337     2553212 :                 if ((p->h == h) && (p->key->len == str->len)) {
     338     2502272 :                         if (!memcmp(p->key->val, str->val, str->len)) {
     339             :                                 zend_string_release(str);
     340     2502272 :                                 return p->key;
     341             :                         }
     342             :                 }
     343       50940 :                 idx = Z_NEXT(p->val);
     344             :         }
     345             : 
     346     3273208 :         if (ZCSG(interned_strings_top) + ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + str->len + 1) >=
     347     1636604 :             ZCSG(interned_strings_end)) {
     348             :             /* no memory, return the same non-interned string */
     349           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Interned string buffer overflow");
     350           0 :                 return str;
     351             :         }
     352             : 
     353             :         /* create new interning string in shared interned strings buffer */
     354             : 
     355     1636604 :         idx = ZCSG(interned_strings).nNumUsed++;
     356     1636604 :         ZCSG(interned_strings).nNumOfElements++;
     357     1636604 :         p = ZCSG(interned_strings).arData + idx;
     358     1636604 :         p->key = (zend_string*) ZCSG(interned_strings_top);
     359     1636604 :         ZCSG(interned_strings_top) += ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + str->len + 1);
     360     1636604 :         p->h = h;
     361     1636604 :         GC_REFCOUNT(p->key) = 1;
     362             : #if 1
     363             :         /* optimized single assignment */
     364     1636604 :         GC_TYPE_INFO(p->key) = IS_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << 8);
     365             : #else
     366             :         GC_TYPE(p->key) = IS_STRING;
     367             :         GC_FLAGS(p->key) = IS_STR_INTERNED | IS_STR_PERMANENT;
     368             : #endif
     369     1636604 :         p->key->h = str->h;
     370     1636604 :         p->key->len = str->len;
     371     1636604 :         memcpy(p->key->val, str->val, str->len);
     372     1636604 :         ZVAL_INTERNED_STR(&p->val, p->key);
     373     1636604 :         Z_NEXT(p->val) = HT_HASH(&ZCSG(interned_strings), nIndex);
     374     1636604 :         HT_HASH(&ZCSG(interned_strings), nIndex) = HT_IDX_TO_HASH(idx);
     375             :         zend_string_release(str);
     376     1636604 :         return p->key;
     377             : #else
     378             :         return str;
     379             : #endif
     380             : }
     381             : 
     382             : #ifndef ZTS
     383             : /* Copy PHP interned strings from PHP process memory into the shared memory */
     384         374 : static void accel_use_shm_interned_strings(void)
     385             : {
     386             :         uint idx, j;
     387             :         Bucket *p, *q;
     388             : 
     389             :         /* empty string */
     390         374 :         CG(empty_string) = accel_new_interned_string(CG(empty_string));
     391       96118 :         for (j = 0; j < 256; j++) {
     392             :                 char s[2];
     393       95744 :                 s[0] = j;
     394       95744 :                 s[1] = 0;
     395      191488 :                 CG(one_char_string)[j] = accel_new_interned_string(zend_string_init(s, 1, 0));
     396             :         }
     397             : 
     398             :         /* function table hash keys */
     399      775652 :         for (idx = 0; idx < CG(function_table)->nNumUsed; idx++) {
     400      775278 :                 p = CG(function_table)->arData + idx;
     401     1550556 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     402      775278 :                 if (p->key) {
     403      775278 :                         p->key = accel_new_interned_string(p->key);
     404             :                 }
     405      775278 :                 if (Z_FUNC(p->val)->common.function_name) {
     406      775278 :                         Z_FUNC(p->val)->common.function_name = accel_new_interned_string(Z_FUNC(p->val)->common.function_name);
     407             :                 }
     408             :         }
     409             : 
     410             :         /* class table hash keys, class names, properties, methods, constants, etc */
     411       70686 :         for (idx = 0; idx < CG(class_table)->nNumUsed; idx++) {
     412             :                 zend_class_entry *ce;
     413             : 
     414       70312 :                 p = CG(class_table)->arData + idx;
     415      140624 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     416       70312 :                 ce = (zend_class_entry*)Z_PTR(p->val);
     417             : 
     418       70312 :                 if (p->key) {
     419       70312 :                         p->key = accel_new_interned_string(p->key);
     420             :                 }
     421             : 
     422       70312 :                 if (ce->name) {
     423       70312 :                         ce->name = accel_new_interned_string(ce->name);
     424             :                 }
     425             : 
     426      168674 :                 for (j = 0; j < ce->properties_info.nNumUsed; j++) {
     427             :                         zend_property_info *info;
     428             : 
     429       98362 :                         q = ce->properties_info.arData + j;
     430      196724 :                         if (Z_TYPE(q->val) == IS_UNDEF) continue;
     431             : 
     432       97988 :                         info = (zend_property_info*)Z_PTR(q->val);
     433             : 
     434       97988 :                         if (q->key) {
     435       97988 :                                 q->key = accel_new_interned_string(q->key);
     436             :                         }
     437             : 
     438       97988 :                         if (info->name) {
     439       97988 :                                 info->name = accel_new_interned_string(info->name);
     440             :                         }
     441             :                 }
     442             : 
     443     1142570 :                 for (j = 0; j < ce->function_table.nNumUsed; j++) {
     444     1072258 :                         q = ce->function_table.arData + j;
     445     2144516 :                         if (Z_TYPE(q->val) == IS_UNDEF) continue;
     446     1072258 :                         if (q->key) {
     447     1072258 :                                 q->key = accel_new_interned_string(q->key);
     448             :                         }
     449     1072258 :                         if (Z_FUNC(q->val)->common.function_name) {
     450     1072258 :                                 Z_FUNC(q->val)->common.function_name = accel_new_interned_string(Z_FUNC(q->val)->common.function_name);
     451             :                         }
     452             :                 }
     453             : 
     454      510510 :                 for (j = 0; j < ce->constants_table.nNumUsed; j++) {
     455      440198 :                         q = ce->constants_table.arData + j;
     456      880396 :                         if (!Z_TYPE(q->val) == IS_UNDEF) continue;
     457           0 :                         if (q->key) {
     458           0 :                                 q->key = accel_new_interned_string(q->key);
     459             :                         }
     460             :                 }
     461             :         }
     462             : 
     463             :         /* constant hash keys */
     464      915926 :         for (idx = 0; idx < EG(zend_constants)->nNumUsed; idx++) {
     465      915552 :                 p = EG(zend_constants)->arData + idx;
     466     1831104 :                 if (!Z_TYPE(p->val) == IS_UNDEF) continue;
     467           0 :                 if (p->key) {
     468           0 :                         p->key = accel_new_interned_string(p->key);
     469             :                 }
     470             :         }
     471             : 
     472             :         /* auto globals hash keys and names */
     473        3740 :         for (idx = 0; idx < CG(auto_globals)->nNumUsed; idx++) {
     474             :                 zend_auto_global *auto_global;
     475             : 
     476        3366 :                 p = CG(auto_globals)->arData + idx;
     477        6732 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     478             : 
     479        3366 :                 auto_global = (zend_auto_global*)Z_PTR(p->val);;
     480             : 
     481        3366 :                 zend_string_addref(auto_global->name);
     482        3366 :                 auto_global->name = accel_new_interned_string(auto_global->name);
     483        3366 :                 if (p->key) {
     484        3366 :                         p->key = accel_new_interned_string(p->key);
     485             :                 }
     486             :         }
     487         374 : }
     488             : #endif
     489             : 
     490           0 : static inline void accel_restart_enter(void)
     491             : {
     492             : #ifdef ZEND_WIN32
     493             :         INCREMENT(restart_in);
     494             : #else
     495             :         static const FLOCK_STRUCTURE(restart_in_progress, F_WRLCK, SEEK_SET, 2, 1);
     496             : 
     497           0 :         if (fcntl(lock_file, F_SETLK, &restart_in_progress) == -1) {
     498           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(+1):  %s (%d)", strerror(errno), errno);
     499             :         }
     500             : #endif
     501           0 :         ZCSG(restart_in_progress) = 1;
     502           0 : }
     503             : 
     504           0 : static inline void accel_restart_leave(void)
     505             : {
     506             : #ifdef ZEND_WIN32
     507             :         ZCSG(restart_in_progress) = 0;
     508             :         DECREMENT(restart_in);
     509             : #else
     510             :         static const FLOCK_STRUCTURE(restart_finished, F_UNLCK, SEEK_SET, 2, 1);
     511             : 
     512           0 :         ZCSG(restart_in_progress) = 0;
     513           0 :         if (fcntl(lock_file, F_SETLK, &restart_finished) == -1) {
     514           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(-1):  %s (%d)", strerror(errno), errno);
     515             :         }
     516             : #endif
     517           0 : }
     518             : 
     519           0 : static inline int accel_restart_is_active(void)
     520             : {
     521           0 :         if (ZCSG(restart_in_progress)) {
     522             : #ifndef ZEND_WIN32
     523           0 :                 FLOCK_STRUCTURE(restart_check, F_WRLCK, SEEK_SET, 2, 1);
     524             : 
     525           0 :                 if (fcntl(lock_file, F_GETLK, &restart_check) == -1) {
     526           0 :                         zend_accel_error(ACCEL_LOG_DEBUG, "RestartC:  %s (%d)", strerror(errno), errno);
     527           0 :                         return FAILURE;
     528             :                 }
     529           0 :                 if (restart_check.l_type == F_UNLCK) {
     530           0 :                         ZCSG(restart_in_progress) = 0;
     531           0 :                         return 0;
     532             :                 } else {
     533           0 :                         return 1;
     534             :                 }
     535             : #else
     536             :                 return LOCKVAL(restart_in) != 0;
     537             : #endif
     538             :         }
     539           0 :         return 0;
     540             : }
     541             : 
     542             : /* Creates a read lock for SHM access */
     543         374 : static inline void accel_activate_add(void)
     544             : {
     545             : #ifdef ZEND_WIN32
     546             :         INCREMENT(mem_usage);
     547             : #else
     548             :         static const FLOCK_STRUCTURE(mem_usage_lock, F_RDLCK, SEEK_SET, 1, 1);
     549             : 
     550         374 :         if (fcntl(lock_file, F_SETLK, &mem_usage_lock) == -1) {
     551           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(+1):  %s (%d)", strerror(errno), errno);
     552             :         }
     553             : #endif
     554         374 : }
     555             : 
     556             : /* Releases a lock for SHM access */
     557           0 : static inline void accel_deactivate_sub(void)
     558             : {
     559             : #ifdef ZEND_WIN32
     560             :         if (ZCG(counted)) {
     561             :                 DECREMENT(mem_usage);
     562             :                 ZCG(counted) = 0;
     563             :         }
     564             : #else
     565             :         static const FLOCK_STRUCTURE(mem_usage_unlock, F_UNLCK, SEEK_SET, 1, 1);
     566             : 
     567           0 :         if (fcntl(lock_file, F_SETLK, &mem_usage_unlock) == -1) {
     568           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(-1):  %s (%d)", strerror(errno), errno);
     569             :         }
     570             : #endif
     571           0 : }
     572             : 
     573         374 : static inline void accel_unlock_all(void)
     574             : {
     575             : #ifdef ZEND_WIN32
     576             :         accel_deactivate_sub();
     577             : #else
     578             :         static const FLOCK_STRUCTURE(mem_usage_unlock_all, F_UNLCK, SEEK_SET, 0, 0);
     579             : 
     580         374 :         if (fcntl(lock_file, F_SETLK, &mem_usage_unlock_all) == -1) {
     581           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UnlockAll:  %s (%d)", strerror(errno), errno);
     582             :         }
     583             : #endif
     584         374 : }
     585             : 
     586             : #ifndef ZEND_WIN32
     587           0 : static inline void kill_all_lockers(struct flock *mem_usage_check)
     588             : {
     589           0 :         int tries = 10;
     590             : 
     591             :         /* so that other process won't try to force while we are busy cleaning up */
     592           0 :         ZCSG(force_restart_time) = 0;
     593           0 :         while (mem_usage_check->l_pid > 0) {
     594           0 :                 while (tries--) {
     595           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "Killed locker %d", mem_usage_check->l_pid);
     596           0 :                         if (kill(mem_usage_check->l_pid, SIGKILL)) {
     597           0 :                                 break;
     598             :                         }
     599             :                         /* give it a chance to die */
     600           0 :                         usleep(20000);
     601           0 :                         if (kill(mem_usage_check->l_pid, 0)) {
     602             :                                 /* can't kill it */
     603           0 :                                 break;
     604             :                         }
     605           0 :                         usleep(10000);
     606             :                 }
     607           0 :                 if (!tries) {
     608           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "Can't kill %d after 20 tries!", mem_usage_check->l_pid);
     609           0 :                         ZCSG(force_restart_time) = time(NULL); /* restore forced restart request */
     610             :                 }
     611             : 
     612           0 :                 mem_usage_check->l_type = F_WRLCK;
     613           0 :                 mem_usage_check->l_whence = SEEK_SET;
     614           0 :                 mem_usage_check->l_start = 1;
     615           0 :                 mem_usage_check->l_len = 1;
     616           0 :                 mem_usage_check->l_pid = -1;
     617           0 :                 if (fcntl(lock_file, F_GETLK, mem_usage_check) == -1) {
     618           0 :                         zend_accel_error(ACCEL_LOG_DEBUG, "KLockers:  %s (%d)", strerror(errno), errno);
     619           0 :                         break;
     620             :                 }
     621             : 
     622           0 :                 if (mem_usage_check->l_type == F_UNLCK || mem_usage_check->l_pid <= 0) {
     623             :                         break;
     624             :                 }
     625             :         }
     626           0 : }
     627             : #endif
     628             : 
     629           0 : static inline int accel_is_inactive(void)
     630             : {
     631             : #ifdef ZEND_WIN32
     632             :         if (LOCKVAL(mem_usage) == 0) {
     633             :                 return SUCCESS;
     634             :         }
     635             : #else
     636           0 :         FLOCK_STRUCTURE(mem_usage_check, F_WRLCK, SEEK_SET, 1, 1);
     637             : 
     638           0 :         mem_usage_check.l_pid = -1;
     639           0 :         if (fcntl(lock_file, F_GETLK, &mem_usage_check) == -1) {
     640           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC:  %s (%d)", strerror(errno), errno);
     641           0 :                 return FAILURE;
     642             :         }
     643           0 :         if (mem_usage_check.l_type == F_UNLCK) {
     644           0 :                 return SUCCESS;
     645             :         }
     646             : 
     647           0 :         if (ZCG(accel_directives).force_restart_timeout
     648           0 :                 && ZCSG(force_restart_time)
     649           0 :                 && time(NULL) >= ZCSG(force_restart_time)) {
     650           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Forced restart at %d (after %d seconds), locked by %d", time(NULL), ZCG(accel_directives).force_restart_timeout, mem_usage_check.l_pid);
     651           0 :                 kill_all_lockers(&mem_usage_check);
     652             : 
     653           0 :                 return FAILURE; /* next request should be able to restart it */
     654             :         }
     655             : #endif
     656             : 
     657           0 :         return FAILURE;
     658             : }
     659             : 
     660           0 : static int zend_get_stream_timestamp(const char *filename, zend_stat_t *statbuf)
     661             : {
     662             :         php_stream_wrapper *wrapper;
     663             :         php_stream_statbuf stream_statbuf;
     664             :         int ret, er;
     665             : 
     666           0 :         if (!filename) {
     667           0 :                 return FAILURE;
     668             :         }
     669             : 
     670           0 :         wrapper = php_stream_locate_url_wrapper(filename, NULL, STREAM_LOCATE_WRAPPERS_ONLY);
     671           0 :         if (!wrapper) {
     672           0 :                 return FAILURE;
     673             :         }
     674           0 :         if (!wrapper->wops || !wrapper->wops->url_stat) {
     675           0 :                 statbuf->st_mtime = 1;
     676           0 :                 return SUCCESS; /* anything other than 0 is considered to be a valid timestamp */
     677             :         }
     678             : 
     679           0 :         er = EG(error_reporting);
     680           0 :         EG(error_reporting) = 0;
     681           0 :         zend_try {
     682           0 :                 ret = wrapper->wops->url_stat(wrapper, (char*)filename, PHP_STREAM_URL_STAT_QUIET, &stream_statbuf, NULL);
     683           0 :         } zend_catch {
     684           0 :                 ret = -1;
     685           0 :         } zend_end_try();
     686           0 :         EG(error_reporting) = er;
     687             : 
     688           0 :         if (ret != 0) {
     689           0 :                 return FAILURE;
     690             :         }
     691             : 
     692           0 :         *statbuf = stream_statbuf.sb;
     693           0 :         return SUCCESS;
     694             : }
     695             : 
     696             : #if ZEND_WIN32
     697             : static accel_time_t zend_get_file_handle_timestamp_win(zend_file_handle *file_handle, size_t *size)
     698             : {
     699             :         static unsigned __int64 utc_base = 0;
     700             :         static FILETIME utc_base_ft;
     701             :         WIN32_FILE_ATTRIBUTE_DATA fdata;
     702             : 
     703             :         if (!file_handle->opened_path) {
     704             :                 return 0;
     705             :         }
     706             : 
     707             :         if (!utc_base) {
     708             :                 SYSTEMTIME st;
     709             : 
     710             :                 st.wYear = 1970;
     711             :                 st.wMonth = 1;
     712             :                 st.wDay = 1;
     713             :                 st.wHour = 0;
     714             :                 st.wMinute = 0;
     715             :                 st.wSecond = 0;
     716             :                 st.wMilliseconds = 0;
     717             : 
     718             :                 SystemTimeToFileTime (&st, &utc_base_ft);
     719             :                 utc_base = (((unsigned __int64)utc_base_ft.dwHighDateTime) << 32) + utc_base_ft.dwLowDateTime;
     720             :     }
     721             : 
     722             :         if (file_handle->opened_path && GetFileAttributesEx(file_handle->opened_path->val, GetFileExInfoStandard, &fdata) != 0) {
     723             :                 unsigned __int64 ftime;
     724             : 
     725             :                 if (CompareFileTime (&fdata.ftLastWriteTime, &utc_base_ft) < 0) {
     726             :                         return 0;
     727             :                 }
     728             : 
     729             :                 ftime = (((unsigned __int64)fdata.ftLastWriteTime.dwHighDateTime) << 32) + fdata.ftLastWriteTime.dwLowDateTime - utc_base;
     730             :                 ftime /= 10000000L;
     731             : 
     732             :                 if (size) {
     733             :                         *size = (size_t)((((unsigned __int64)fdata.nFileSizeHigh) << 32) + (unsigned __int64)fdata.nFileSizeLow);
     734             :                 }
     735             :                 return (accel_time_t)ftime;
     736             :         }
     737             :         return 0;
     738             : }
     739             : #endif
     740             : 
     741         430 : accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size)
     742             : {
     743             :         zend_stat_t statbuf;
     744             : #ifdef ZEND_WIN32
     745             :         accel_time_t res;
     746             : #endif
     747             : 
     748         430 :         if (sapi_module.get_stat &&
     749           0 :             !EG(current_execute_data) &&
     750           0 :             file_handle->filename == SG(request_info).path_translated) {
     751             : 
     752           0 :                 zend_stat_t *tmpbuf = sapi_module.get_stat();
     753             : 
     754           0 :                 if (tmpbuf) {
     755           0 :                         if (size) {
     756           0 :                                 *size = tmpbuf->st_size;
     757             :                         }
     758           0 :                         return tmpbuf->st_mtime;
     759             :                 }
     760             :         }
     761             : 
     762             : #ifdef ZEND_WIN32
     763             :         res = zend_get_file_handle_timestamp_win(file_handle, size);
     764             :         if (res) {
     765             :                 return res;
     766             :         }
     767             : #endif
     768             : 
     769         430 :         switch (file_handle->type) {
     770             :                 case ZEND_HANDLE_FD:
     771           0 :                         if (zend_fstat(file_handle->handle.fd, &statbuf) == -1) {
     772           0 :                                 return 0;
     773             :                         }
     774           0 :                         break;
     775             :                 case ZEND_HANDLE_FP:
     776           6 :                         if (zend_fstat(fileno(file_handle->handle.fp), &statbuf) == -1) {
     777           0 :                                 if (zend_get_stream_timestamp(file_handle->filename, &statbuf) != SUCCESS) {
     778           0 :                                         return 0;
     779             :                                 }
     780             :                         }
     781           6 :                         break;
     782             :                 case ZEND_HANDLE_FILENAME:
     783             :                 case ZEND_HANDLE_MAPPED:
     784         389 :                         if (file_handle->opened_path) {
     785         389 :                                 char *file_path = file_handle->opened_path->val;
     786             : 
     787         389 :                                 if (is_stream_path(file_path)) {
     788           0 :                                         if (zend_get_stream_timestamp(file_path, &statbuf) == SUCCESS) {
     789           0 :                                                 break;
     790             :                                         }
     791             :                                 }
     792         389 :                                 if (VCWD_STAT(file_path, &statbuf) != -1) {
     793         389 :                                         break;
     794             :                                 }
     795             :                         }
     796             : 
     797           0 :                         if (zend_get_stream_timestamp(file_handle->filename, &statbuf) != SUCCESS) {
     798           0 :                                 return 0;
     799             :                         }
     800           0 :                         break;
     801             :                 case ZEND_HANDLE_STREAM:
     802             :                         {
     803          35 :                                 php_stream *stream = (php_stream *)file_handle->handle.stream.handle;
     804             :                                 php_stream_statbuf sb;
     805             :                                 int ret, er;
     806             : 
     807         105 :                                 if (!stream ||
     808          35 :                                     !stream->ops ||
     809          35 :                                     !stream->ops->stat) {
     810           0 :                                         return 0;
     811             :                                 }
     812             : 
     813          35 :                                 er = EG(error_reporting);
     814          35 :                                 EG(error_reporting) = 0;
     815          35 :                                 zend_try {
     816          35 :                                         ret = stream->ops->stat(stream, &sb);
     817           0 :                                 } zend_catch {
     818           0 :                                         ret = -1;
     819          35 :                                 } zend_end_try();
     820          35 :                                 EG(error_reporting) = er;
     821          35 :                                 if (ret != 0) {
     822           0 :                                         return 0;
     823             :                                 }
     824             : 
     825          35 :                                 statbuf = sb.sb;
     826             :                         }
     827          35 :                         break;
     828             : 
     829             :                 default:
     830           0 :                         return 0;
     831             :         }
     832             : 
     833         430 :         if (size) {
     834           0 :                 *size = statbuf.st_size;
     835             :         }
     836         430 :         return statbuf.st_mtime;
     837             : }
     838             : 
     839           0 : static inline int do_validate_timestamps(zend_persistent_script *persistent_script, zend_file_handle *file_handle)
     840             : {
     841             :         zend_file_handle ps_handle;
     842           0 :         zend_string *full_path_ptr = NULL;
     843             : 
     844             :         /** check that the persistent script is indeed the same file we cached
     845             :          * (if part of the path is a symlink than it possible that the user will change it)
     846             :          * See bug #15140
     847             :          */
     848           0 :         if (file_handle->opened_path) {
     849           0 :                 if (persistent_script->full_path != file_handle->opened_path &&
     850           0 :                     (persistent_script->full_path->len != file_handle->opened_path->len ||
     851           0 :                      memcmp(persistent_script->full_path->val, file_handle->opened_path->val, file_handle->opened_path->len) != 0)) {
     852           0 :                         return FAILURE;
     853             :                 }
     854             :         } else {
     855           0 :                 full_path_ptr = accelerator_orig_zend_resolve_path(file_handle->filename, strlen(file_handle->filename));
     856           0 :                 if (full_path_ptr &&
     857           0 :                     persistent_script->full_path != full_path_ptr &&
     858           0 :                     (persistent_script->full_path->len != full_path_ptr->len ||
     859           0 :                      memcmp(persistent_script->full_path->val, full_path_ptr->val, full_path_ptr->len) != 0)) {
     860             :                         zend_string_release(full_path_ptr);
     861           0 :                         return FAILURE;
     862             :                 }
     863           0 :                 file_handle->opened_path = full_path_ptr;
     864             :         }
     865             : 
     866           0 :         if (persistent_script->timestamp == 0) {
     867           0 :                 if (full_path_ptr) {
     868             :                         zend_string_release(full_path_ptr);
     869           0 :                         file_handle->opened_path = NULL;
     870             :                 }
     871           0 :                 return FAILURE;
     872             :         }
     873             : 
     874           0 :         if (zend_get_file_handle_timestamp(file_handle, NULL) == persistent_script->timestamp) {
     875           0 :                 if (full_path_ptr) {
     876             :                         zend_string_release(full_path_ptr);
     877           0 :                         file_handle->opened_path = NULL;
     878             :                 }
     879           0 :                 return SUCCESS;
     880             :         }
     881           0 :         if (full_path_ptr) {
     882             :                 zend_string_release(full_path_ptr);
     883           0 :                 file_handle->opened_path = NULL;
     884             :         }
     885             : 
     886           0 :         ps_handle.type = ZEND_HANDLE_FILENAME;
     887           0 :         ps_handle.filename = persistent_script->full_path->val;
     888           0 :         ps_handle.opened_path = persistent_script->full_path;
     889             : 
     890           0 :         if (zend_get_file_handle_timestamp(&ps_handle, NULL) == persistent_script->timestamp) {
     891           0 :                 return SUCCESS;
     892             :         }
     893             : 
     894           0 :         return FAILURE;
     895             : }
     896             : 
     897           0 : int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle)
     898             : {
     899           0 :         if (ZCG(accel_directives).revalidate_freq &&
     900           0 :             persistent_script->dynamic_members.revalidate >= ZCG(request_time)) {
     901           0 :                 return SUCCESS;
     902           0 :         } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) {
     903           0 :                 return FAILURE;
     904             :         } else {
     905           0 :                 persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
     906           0 :                 return SUCCESS;
     907             :         }
     908             : }
     909             : 
     910             : /* Instead of resolving full real path name each time we need to identify file,
     911             :  * we create a key that consist from requested file name, current working
     912             :  * directory, current include_path, etc */
     913        1182 : char *accel_make_persistent_key(const char *path, int path_length, int *key_len)
     914             : {
     915             :         int key_length;
     916             : 
     917             :         /* CWD and include_path don't matter for absolute file names and streams */
     918        1182 :     if (IS_ABSOLUTE_PATH(path, path_length)) {
     919             :                 /* pass */
     920          57 :     } else if (UNEXPECTED(is_stream_path(path))) {
     921          28 :                 if (!is_cacheable_stream_path(path)) {
     922           0 :                         return NULL;
     923             :                 }
     924             :                 /* pass */
     925          29 :     } else if (UNEXPECTED(!ZCG(accel_directives).use_cwd)) {
     926             :                 /* pass */
     927             :     } else {
     928          29 :                 const char *include_path = NULL, *cwd = NULL;
     929          29 :                 int include_path_len = 0, cwd_len = 0;
     930          29 :                 zend_string *parent_script = NULL;
     931          29 :                 size_t parent_script_len = 0;
     932             : 
     933          29 :                 if (EXPECTED(ZCG(cwd_key_len))) {
     934           4 :                         cwd = ZCG(cwd_key);
     935           4 :                         cwd_len = ZCG(cwd_key_len);
     936             :                 } else {
     937          25 :                         zend_string *cwd_str = accel_getcwd();
     938             : 
     939          25 :                         if (UNEXPECTED(!cwd_str)) {
     940             :                                 /* we don't handle this well for now. */
     941           0 :                                 zend_accel_error(ACCEL_LOG_INFO, "getcwd() failed for '%s' (%d), please try to set opcache.use_cwd to 0 in ini file", path, errno);
     942           0 :                                 return NULL;
     943             :                         }
     944          25 :                         cwd = cwd_str->val;
     945          25 :                         cwd_len = cwd_str->len;
     946             : #ifndef ZTS
     947          25 :                         if (ZCG(cwd_check)) {
     948          25 :                                 ZCG(cwd_check) = 0;
     949          25 :                                 if ((ZCG(counted) || ZCSG(accelerator_enabled))) {
     950             : 
     951          25 :                                         zend_string *str = accel_find_interned_string(cwd_str);
     952          25 :                                         if (!str) {
     953          25 :                                                 SHM_UNPROTECT();
     954          25 :                                                 zend_shared_alloc_lock();
     955          25 :                                                 str = accel_new_interned_string(zend_string_copy(cwd_str));
     956          25 :                                                 if (str == cwd_str) {
     957           0 :                                                         str = NULL;
     958             :                                                 }
     959          25 :                                                 zend_shared_alloc_unlock();
     960          25 :                                                 SHM_PROTECT();
     961             :                                         }
     962          25 :                                         if (str) {
     963             :                                                 char buf[32];
     964          50 :                                                 char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, str->val - ZCSG(interned_strings_start));
     965             : 
     966          25 :                                                 cwd_len = ZCG(cwd_key_len) = buf + sizeof(buf) - 1 - res;
     967          25 :                                                 cwd = ZCG(cwd_key);
     968          25 :                                                 memcpy(ZCG(cwd_key), res, cwd_len + 1);
     969             :                                         }
     970             :                                 }
     971             :                         }
     972             : #endif
     973             :                 }
     974             : 
     975          29 :                 if (EXPECTED(ZCG(include_path_key_len))) {
     976           4 :                         include_path = ZCG(include_path_key);
     977           4 :                         include_path_len = ZCG(include_path_key_len);
     978          25 :                 } else if (!ZCG(include_path) || ZCG(include_path)->len == 0) {
     979           0 :                         include_path = "";
     980           0 :                         include_path_len = 0;
     981             :                 } else {
     982          25 :                         include_path = ZCG(include_path)->val;
     983          25 :                         include_path_len = ZCG(include_path)->len;
     984             : 
     985             : #ifndef ZTS
     986          25 :                         if (ZCG(include_path_check)) {
     987          25 :                                 ZCG(include_path_check) = 0;
     988          25 :                                 if ((ZCG(counted) || ZCSG(accelerator_enabled))) {
     989             : 
     990          25 :                                         zend_string *str = accel_find_interned_string(ZCG(include_path));
     991          25 :                                         if (!str) {
     992          23 :                                                 SHM_UNPROTECT();
     993          23 :                                                 zend_shared_alloc_lock();
     994          46 :                                                 str = accel_new_interned_string(zend_string_copy(ZCG(include_path)));
     995          23 :                                                 if (str == ZCG(include_path)) {
     996           0 :                                                         str = NULL;
     997             :                                                 }
     998          23 :                                                 zend_shared_alloc_unlock();
     999          23 :                                                 SHM_PROTECT();
    1000             :                                         }
    1001          25 :                                         if (str) {
    1002             :                                                 char buf[32];
    1003          50 :                                                 char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, str->val - ZCSG(interned_strings_start));
    1004             : 
    1005          25 :                                                 include_path_len = ZCG(include_path_key_len) = buf + sizeof(buf) - 1 - res;
    1006          25 :                                                 include_path = ZCG(include_path_key);
    1007          25 :                                                 memcpy(ZCG(include_path_key), res, include_path_len + 1);
    1008             :                                         }
    1009             :                                 }
    1010             :                         }
    1011             : #endif
    1012             :                 }
    1013             : 
    1014             :                 /* Calculate key length */
    1015          29 :                 if (UNEXPECTED((size_t)(cwd_len + path_length + include_path_len + 2) >= sizeof(ZCG(key)))) {
    1016           0 :                         return NULL;
    1017             :                 }
    1018             : 
    1019             :                 /* Generate key
    1020             :                  * Note - the include_path must be the last element in the key,
    1021             :                  * since in itself, it may include colons (which we use to separate
    1022             :                  * different components of the key)
    1023             :                  */
    1024          29 :                 memcpy(ZCG(key), path, path_length);
    1025          29 :                 ZCG(key)[path_length] = ':';
    1026          29 :                 key_length = path_length + 1;
    1027          29 :                 memcpy(ZCG(key) + key_length, cwd, cwd_len);
    1028          29 :                 key_length += cwd_len;
    1029             : 
    1030          29 :                 if (include_path_len) {
    1031          29 :                         ZCG(key)[key_length] = ':';
    1032          29 :                         key_length += 1;
    1033          29 :                         memcpy(ZCG(key) + key_length, include_path, include_path_len);
    1034          29 :                         key_length += include_path_len;
    1035             :                 }
    1036             : 
    1037             :                 /* Here we add to the key the parent script directory,
    1038             :                  * since fopen_wrappers from version 4.0.7 use current script's path
    1039             :                  * in include path too.
    1040             :                  */
    1041          87 :                 if (EXPECTED(EG(current_execute_data)) &&
    1042          58 :                     EXPECTED((parent_script = zend_get_executed_filename_ex()) != NULL)) {
    1043             : 
    1044          29 :                         parent_script_len = parent_script->len;
    1045         578 :                         while ((--parent_script_len > 0) && !IS_SLASH(parent_script->val[parent_script_len]));
    1046             : 
    1047          29 :                         if (UNEXPECTED((size_t)(key_length + parent_script_len + 1) >= sizeof(ZCG(key)))) {
    1048           0 :                                 return NULL;
    1049             :                         }
    1050          29 :                         ZCG(key)[key_length] = ':';
    1051          29 :                         key_length += 1;
    1052          29 :                         memcpy(ZCG(key) + key_length, parent_script->val, parent_script_len);
    1053          29 :                         key_length += parent_script_len;
    1054             :                 }
    1055          29 :                 ZCG(key)[key_length] = '\0';
    1056          29 :                 *key_len = ZCG(key_len) = key_length;
    1057          29 :                 return ZCG(key);
    1058             :         }
    1059             : 
    1060             :         /* not use_cwd */
    1061        1153 :         *key_len = path_length;
    1062        1153 :         return (char*)path;
    1063             : }
    1064             : 
    1065           0 : int zend_accel_invalidate(const char *filename, int filename_len, zend_bool force)
    1066             : {
    1067             :         zend_string *realpath;
    1068             :         zend_persistent_script *persistent_script;
    1069             : 
    1070           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock() != SUCCESS) {
    1071             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1072             :                 if (ZCG(accel_directives).file_cache) {
    1073             :                         realpath = accelerator_orig_zend_resolve_path(filename, filename_len);
    1074             :                         if (realpath) {
    1075             :                                 zend_file_cache_invalidate(realpath);
    1076             :                                 zend_string_release(realpath);
    1077             :                         }
    1078             :                 }
    1079             : #endif
    1080           0 :                 return FAILURE;
    1081             :         }
    1082             : 
    1083           0 :         realpath = accelerator_orig_zend_resolve_path(filename, filename_len);
    1084             : 
    1085           0 :         if (!realpath) {
    1086           0 :                 return FAILURE;
    1087             :         }
    1088             : 
    1089             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1090             :         if (ZCG(accel_directives).file_cache) {
    1091             :                 zend_file_cache_invalidate(realpath);
    1092             :         }
    1093             : #endif
    1094             : 
    1095           0 :         persistent_script = zend_accel_hash_find(&ZCSG(hash), realpath);
    1096           0 :         if (persistent_script && !persistent_script->corrupted) {
    1097             :                 zend_file_handle file_handle;
    1098             : 
    1099           0 :                 file_handle.type = ZEND_HANDLE_FILENAME;
    1100           0 :                 file_handle.filename = realpath->val;
    1101           0 :                 file_handle.opened_path = realpath;
    1102             : 
    1103           0 :                 if (force ||
    1104           0 :                         !ZCG(accel_directives).validate_timestamps ||
    1105           0 :                         do_validate_timestamps(persistent_script, &file_handle) == FAILURE) {
    1106           0 :                         SHM_UNPROTECT();
    1107           0 :                         zend_shared_alloc_lock();
    1108           0 :                         if (!persistent_script->corrupted) {
    1109           0 :                                 persistent_script->corrupted = 1;
    1110           0 :                                 persistent_script->timestamp = 0;
    1111           0 :                                 ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
    1112           0 :                                 if (ZSMMG(memory_exhausted)) {
    1113             :                                         zend_accel_restart_reason reason =
    1114           0 :                                                 zend_accel_hash_is_full(&ZCSG(hash)) ? ACCEL_RESTART_HASH : ACCEL_RESTART_OOM;
    1115           0 :                                         zend_accel_schedule_restart_if_necessary(reason);
    1116             :                                 }
    1117             :                         }
    1118           0 :                         zend_shared_alloc_unlock();
    1119           0 :                         SHM_PROTECT();
    1120             :                 }
    1121             :         }
    1122             : 
    1123           0 :         accelerator_shm_read_unlock();
    1124             :         zend_string_release(realpath);
    1125             : 
    1126           0 :         return SUCCESS;
    1127             : }
    1128             : 
    1129             : /* Adds another key for existing cached script */
    1130           0 : static void zend_accel_add_key(char *key, unsigned int key_length, zend_accel_hash_entry *bucket)
    1131             : {
    1132           0 :         if (!zend_accel_hash_str_find(&ZCSG(hash), key, key_length)) {
    1133           0 :                 if (zend_accel_hash_is_full(&ZCSG(hash))) {
    1134           0 :                         zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
    1135           0 :                         ZSMMG(memory_exhausted) = 1;
    1136           0 :                         zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
    1137             :                 } else {
    1138           0 :                         char *new_key = zend_shared_alloc(key_length + 1);
    1139           0 :                         if (new_key) {
    1140           0 :                                 memcpy(new_key, key, key_length + 1);
    1141           0 :                                 if (zend_accel_hash_update(&ZCSG(hash), new_key, key_length, 1, bucket)) {
    1142           0 :                                         zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", new_key);
    1143             :                                 }
    1144             :                         } else {
    1145           0 :                                 zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM);
    1146             :                         }
    1147             :                 }
    1148             :         }
    1149           0 : }
    1150             : 
    1151             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1152             : static zend_persistent_script *cache_script_in_file_cache(zend_persistent_script *new_persistent_script, int *from_shared_memory)
    1153             : {
    1154             :         uint memory_used;
    1155             : 
    1156             :         /* Check if script may be stored in shared memory */
    1157             :         if (!zend_accel_script_persistable(new_persistent_script)) {
    1158             :                 return new_persistent_script;
    1159             :         }
    1160             : 
    1161             :         if (!zend_accel_script_optimize(new_persistent_script)) {
    1162             :                 return new_persistent_script;
    1163             :         }
    1164             : 
    1165             :         zend_shared_alloc_init_xlat_table();
    1166             : 
    1167             :         /* Calculate the required memory size */
    1168             :         memory_used = zend_accel_script_persist_calc(new_persistent_script, NULL, 0);
    1169             : 
    1170             :         /* Allocate memory block */
    1171             : #ifdef __SSE2__
    1172             :         /* Align to 64-byte boundary */
    1173             :         ZCG(mem) = zend_arena_alloc(&CG(arena), memory_used + 64);
    1174             :         ZCG(mem) = (void*)(((zend_uintptr_t)ZCG(mem) + 63L) & ~63L);
    1175             : #else
    1176             :         ZCG(mem) = zend_arena_alloc(&CG(arena), memory_used);
    1177             : #endif
    1178             : 
    1179             :         /* Copy into shared memory */
    1180             :         new_persistent_script = zend_accel_script_persist(new_persistent_script, NULL, 0);
    1181             : 
    1182             :         zend_shared_alloc_destroy_xlat_table();
    1183             : 
    1184             :         new_persistent_script->is_phar =
    1185             :                 new_persistent_script->full_path &&
    1186             :                 strstr(new_persistent_script->full_path->val, ".phar") &&
    1187             :                 !strstr(new_persistent_script->full_path->val, "://");
    1188             : 
    1189             :         /* Consistency check */
    1190             :         if ((char*)new_persistent_script->mem + new_persistent_script->size != (char*)ZCG(mem)) {
    1191             :                 zend_accel_error(
    1192             :                         ((char*)new_persistent_script->mem + new_persistent_script->size < (char*)ZCG(mem)) ? ACCEL_LOG_ERROR : ACCEL_LOG_WARNING,
    1193             :                         "Internal error: wrong size calculation: %s start=0x%08x, end=0x%08x, real=0x%08x\n",
    1194             :                         new_persistent_script->full_path->val,
    1195             :                         new_persistent_script->mem,
    1196             :                         (char *)new_persistent_script->mem + new_persistent_script->size,
    1197             :                         ZCG(mem));
    1198             :         }
    1199             : 
    1200             :         new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
    1201             : 
    1202             :         zend_file_cache_script_store(new_persistent_script, 0);
    1203             : 
    1204             :         *from_shared_memory = 1;
    1205             :         return new_persistent_script;
    1206             : }
    1207             : #endif
    1208             : 
    1209         427 : static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_script *new_persistent_script, char *key, unsigned int key_length, int *from_shared_memory)
    1210             : {
    1211             :         zend_accel_hash_entry *bucket;
    1212             :         uint memory_used;
    1213             : 
    1214             :         /* Check if script may be stored in shared memory */
    1215         427 :         if (!zend_accel_script_persistable(new_persistent_script)) {
    1216           0 :                 return new_persistent_script;
    1217             :         }
    1218             : 
    1219         427 :         if (!zend_accel_script_optimize(new_persistent_script)) {
    1220           0 :                 return new_persistent_script;
    1221             :         }
    1222             : 
    1223             :         /* exclusive lock */
    1224         427 :         zend_shared_alloc_lock();
    1225             : 
    1226         427 :         if (zend_accel_hash_is_full(&ZCSG(hash))) {
    1227           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
    1228           0 :                 ZSMMG(memory_exhausted) = 1;
    1229           0 :                 zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
    1230           0 :                 zend_shared_alloc_unlock();
    1231           0 :                 return new_persistent_script;
    1232             :         }
    1233             : 
    1234             :         /* Check if we still need to put the file into the cache (may be it was
    1235             :          * already stored by another process. This final check is done under
    1236             :          * exclusive lock) */
    1237         427 :         bucket = zend_accel_hash_find_entry(&ZCSG(hash), new_persistent_script->full_path);
    1238         427 :         if (bucket) {
    1239           0 :                 zend_persistent_script *existing_persistent_script = (zend_persistent_script *)bucket->data;
    1240             : 
    1241           0 :                 if (!existing_persistent_script->corrupted) {
    1242           0 :                         if (key &&
    1243           0 :                             (!ZCG(accel_directives).validate_timestamps ||
    1244           0 :                              (new_persistent_script->timestamp == existing_persistent_script->timestamp))) {
    1245           0 :                                 zend_accel_add_key(key, key_length, bucket);
    1246             :                         }
    1247           0 :                         zend_shared_alloc_unlock();
    1248           0 :                         return new_persistent_script;
    1249             :                 }
    1250             :         }
    1251             : 
    1252             : 
    1253         427 :         zend_shared_alloc_init_xlat_table();
    1254             : 
    1255             :         /* Calculate the required memory size */
    1256         427 :         memory_used = zend_accel_script_persist_calc(new_persistent_script, key, key_length);
    1257             : 
    1258             :         /* Allocate shared memory */
    1259             : #ifdef __SSE2__
    1260             :         /* Align to 64-byte boundary */
    1261         427 :         ZCG(mem) = zend_shared_alloc(memory_used + 64);
    1262         427 :         ZCG(mem) = (void*)(((zend_uintptr_t)ZCG(mem) + 63L) & ~63L);
    1263             : #else
    1264             :         ZCG(mem) = zend_shared_alloc(memory_used);
    1265             : #endif
    1266         427 :         if (!ZCG(mem)) {
    1267           0 :                 zend_shared_alloc_destroy_xlat_table();
    1268           0 :                 zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM);
    1269           0 :                 zend_shared_alloc_unlock();
    1270           0 :                 return new_persistent_script;
    1271             :         }
    1272             : 
    1273             :         /* Copy into shared memory */
    1274         427 :         new_persistent_script = zend_accel_script_persist(new_persistent_script, &key, key_length);
    1275             : 
    1276         427 :         zend_shared_alloc_destroy_xlat_table();
    1277             : 
    1278         427 :         new_persistent_script->is_phar =
    1279         933 :                 new_persistent_script->full_path &&
    1280         427 :                 strstr(new_persistent_script->full_path->val, ".phar") &&
    1281          79 :                 !strstr(new_persistent_script->full_path->val, "://");
    1282             : 
    1283             :         /* Consistency check */
    1284         427 :         if ((char*)new_persistent_script->mem + new_persistent_script->size != (char*)ZCG(mem)) {
    1285           0 :                 zend_accel_error(
    1286           0 :                         ((char*)new_persistent_script->mem + new_persistent_script->size < (char*)ZCG(mem)) ? ACCEL_LOG_ERROR : ACCEL_LOG_WARNING,
    1287             :                         "Internal error: wrong size calculation: %s start=0x%08x, end=0x%08x, real=0x%08x\n",
    1288           0 :                         new_persistent_script->full_path->val,
    1289             :                         new_persistent_script->mem,
    1290             :                         (char *)new_persistent_script->mem + new_persistent_script->size,
    1291             :                         ZCG(mem));
    1292             :         }
    1293             : 
    1294         427 :         new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
    1295             : 
    1296             :         /* store script structure in the hash table */
    1297         427 :         bucket = zend_accel_hash_update(&ZCSG(hash), new_persistent_script->full_path->val, new_persistent_script->full_path->len, 0, new_persistent_script);
    1298         427 :         if (bucket) {
    1299         427 :                 zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", new_persistent_script->full_path);
    1300        1634 :                 if (key &&
    1301             :                     /* key may contain non-persistent PHAR aliases (see issues #115 and #149) */
    1302         427 :                     memcmp(key, "phar://", sizeof("phar://") - 1) != 0 &&
    1303         400 :                     (new_persistent_script->full_path->len != key_length ||
    1304         380 :                      memcmp(new_persistent_script->full_path->val, key, key_length) != 0)) {
    1305             :                         /* link key to the same persistent script in hash table */
    1306          20 :                         if (zend_accel_hash_update(&ZCSG(hash), key, key_length, 1, bucket)) {
    1307          20 :                                 zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", key);
    1308             :                         } else {
    1309           0 :                                 zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
    1310           0 :                                 ZSMMG(memory_exhausted) = 1;
    1311           0 :                                 zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
    1312             :                         }
    1313             :                 }
    1314             :         }
    1315             : 
    1316         427 :         new_persistent_script->dynamic_members.memory_consumption = ZEND_ALIGNED_SIZE(new_persistent_script->size);
    1317             : 
    1318         427 :         zend_shared_alloc_unlock();
    1319             : 
    1320             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1321             :         if (ZCG(accel_directives).file_cache) {
    1322             :                 SHM_PROTECT();
    1323             :                 zend_file_cache_script_store(new_persistent_script, 1);
    1324             :                 SHM_UNPROTECT();
    1325             :         }
    1326             : #endif
    1327             : 
    1328         427 :         *from_shared_memory = 1;
    1329         427 :         return new_persistent_script;
    1330             : }
    1331             : 
    1332             : static const struct jit_auto_global_info
    1333             : {
    1334             :     const char *name;
    1335             :     size_t len;
    1336             : } jit_auto_globals_info[] = {
    1337             :     { "_SERVER",  sizeof("_SERVER")-1},
    1338             :     { "_ENV",     sizeof("_ENV")-1},
    1339             :     { "_REQUEST", sizeof("_REQUEST")-1},
    1340             :     { "GLOBALS",  sizeof("GLOBALS")-1},
    1341             : };
    1342             : 
    1343             : static zend_string *jit_auto_globals_str[4];
    1344             : 
    1345         427 : static int zend_accel_get_auto_globals(void)
    1346             : {
    1347         427 :         int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
    1348         427 :         int n = 1;
    1349         427 :         int mask = 0;
    1350             : 
    1351        2135 :         for (i = 0; i < ag_size ; i++) {
    1352        1708 :                 if (zend_hash_exists(&EG(symbol_table), jit_auto_globals_str[i])) {
    1353         249 :                         mask |= n;
    1354             :                 }
    1355        1708 :                 n += n;
    1356             :         }
    1357         427 :         return mask;
    1358             : }
    1359             : 
    1360           0 : static int zend_accel_get_auto_globals_no_jit(void)
    1361             : {
    1362           0 :         if (zend_hash_exists(&EG(symbol_table), jit_auto_globals_str[3])) {
    1363           0 :                 return 8;
    1364             :         }
    1365           0 :         return 0;
    1366             : }
    1367             : 
    1368         133 : static void zend_accel_set_auto_globals(int mask)
    1369             : {
    1370         133 :         int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
    1371         133 :         int n = 1;
    1372             : 
    1373         665 :         for (i = 0; i < ag_size ; i++) {
    1374         532 :                 if ((mask & n) && !(ZCG(auto_globals_mask) & n)) {
    1375         195 :                         ZCG(auto_globals_mask) |= n;
    1376         195 :                         zend_is_auto_global(jit_auto_globals_str[i]);
    1377             :                 }
    1378         532 :                 n += n;
    1379             :         }
    1380         133 : }
    1381             : 
    1382         374 : static void zend_accel_init_auto_globals(void)
    1383             : {
    1384         374 :         int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
    1385             : 
    1386        1870 :         for (i = 0; i < ag_size ; i++) {
    1387        2992 :                 jit_auto_globals_str[i] = zend_string_init(jit_auto_globals_info[i].name, jit_auto_globals_info[i].len, 1);
    1388        1496 :                 zend_string_hash_val(jit_auto_globals_str[i]);
    1389        1496 :                 jit_auto_globals_str[i] = accel_new_interned_string(jit_auto_globals_str[i]);
    1390             :         }
    1391         374 : }
    1392             : 
    1393         430 : static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handle, int type, char *key, unsigned int key_length, zend_op_array **op_array_p)
    1394             : {
    1395             :         zend_persistent_script *new_persistent_script;
    1396             :         zend_op_array *orig_active_op_array;
    1397             :         HashTable *orig_function_table, *orig_class_table;
    1398             :         zval orig_user_error_handler;
    1399             :         zend_op_array *op_array;
    1400         430 :         int do_bailout = 0;
    1401         430 :         accel_time_t timestamp = 0;
    1402         430 :         uint32_t orig_compiler_options = 0;
    1403             : 
    1404             :     /* Try to open file */
    1405         430 :     if (file_handle->type == ZEND_HANDLE_FILENAME) {
    1406           0 :         if (accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) == SUCCESS) {
    1407             :                 /* key may be changed by zend_stream_open_function() */
    1408           0 :                 if (key == ZCG(key)) {
    1409           0 :                         key_length = ZCG(key_len);
    1410             :                 }
    1411             :         } else {
    1412           0 :                         *op_array_p = NULL;
    1413           0 :                         if (type == ZEND_REQUIRE) {
    1414           0 :                                 zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
    1415           0 :                                 zend_bailout();
    1416             :                         } else {
    1417           0 :                                 zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
    1418             :                         }
    1419           0 :                         return NULL;
    1420             :         }
    1421             :     }
    1422             : 
    1423             :         /* check blacklist right after ensuring that file was opened */
    1424         430 :         if (file_handle->opened_path && zend_accel_blacklist_is_blacklisted(&accel_blacklist, file_handle->opened_path->val)) {
    1425           0 :                 ZCSG(blacklist_misses)++;
    1426           0 :                 *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1427           0 :                 return NULL;
    1428             :         }
    1429             : 
    1430         430 :         if (ZCG(accel_directives).validate_timestamps ||
    1431           0 :             ZCG(accel_directives).file_update_protection ||
    1432           0 :             ZCG(accel_directives).max_file_size > 0) {
    1433         430 :                 size_t size = 0;
    1434             : 
    1435             :                 /* Obtain the file timestamps, *before* actually compiling them,
    1436             :                  * otherwise we have a race-condition.
    1437             :                  */
    1438         430 :                 timestamp = zend_get_file_handle_timestamp(file_handle, ZCG(accel_directives).max_file_size > 0 ? &size : NULL);
    1439             : 
    1440             :                 /* If we can't obtain a timestamp (that means file is possibly socket)
    1441             :                  *  we won't cache it
    1442             :                  */
    1443         430 :                 if (timestamp == 0) {
    1444           0 :                         *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1445           0 :                         return NULL;
    1446             :                 }
    1447             : 
    1448             :                 /* check if file is too new (may be it's not written completely yet) */
    1449         430 :                 if (ZCG(accel_directives).file_update_protection &&
    1450           0 :                     (ZCG(request_time) - ZCG(accel_directives).file_update_protection < timestamp)) {
    1451           0 :                         *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1452           0 :                         return NULL;
    1453             :                 }
    1454             : 
    1455         430 :                 if (ZCG(accel_directives).max_file_size > 0 && size > (size_t)ZCG(accel_directives).max_file_size) {
    1456           0 :                         ZCSG(blacklist_misses)++;
    1457           0 :                         *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1458           0 :                         return NULL;
    1459             :                 }
    1460             :         }
    1461             : 
    1462         430 :         new_persistent_script = create_persistent_script();
    1463             : 
    1464             :         /* Save the original values for the op_array, function table and class table */
    1465         430 :         orig_active_op_array = CG(active_op_array);
    1466         430 :         orig_function_table = CG(function_table);
    1467         430 :         orig_class_table = CG(class_table);
    1468         430 :         ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
    1469             : 
    1470             :         /* Override them with ours */
    1471         430 :         CG(function_table) = &ZCG(function_table);
    1472         430 :         EG(class_table) = CG(class_table) = &new_persistent_script->class_table;
    1473         430 :         ZVAL_UNDEF(&EG(user_error_handler));
    1474             : 
    1475         430 :         zend_try {
    1476         430 :                 orig_compiler_options = CG(compiler_options);
    1477         430 :                 CG(compiler_options) |= ZEND_COMPILE_HANDLE_OP_ARRAY;
    1478         430 :                 CG(compiler_options) |= ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
    1479         430 :                 CG(compiler_options) |= ZEND_COMPILE_DELAYED_BINDING;
    1480         430 :                 CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION;
    1481         430 :                 op_array = *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1482         428 :                 CG(compiler_options) = orig_compiler_options;
    1483           2 :         } zend_catch {
    1484           2 :                 op_array = NULL;
    1485           2 :                 do_bailout = 1;
    1486           2 :                 CG(compiler_options) = orig_compiler_options;
    1487         430 :         } zend_end_try();
    1488             : 
    1489             :         /* Restore originals */
    1490         430 :         CG(active_op_array) = orig_active_op_array;
    1491         430 :         CG(function_table) = orig_function_table;
    1492         430 :         EG(class_table) = CG(class_table) = orig_class_table;
    1493         430 :         EG(user_error_handler) = orig_user_error_handler;
    1494             : 
    1495         430 :         if (!op_array) {
    1496             :                 /* compilation failed */
    1497           3 :                 free_persistent_script(new_persistent_script, 1);
    1498           3 :                 zend_accel_free_user_functions(&ZCG(function_table));
    1499           3 :                 if (do_bailout) {
    1500           2 :                         zend_bailout();
    1501             :                 }
    1502           1 :                 return NULL;
    1503             :         }
    1504             : 
    1505             :         /* Build the persistent_script structure.
    1506             :            Here we aren't sure we would store it, but we will need it
    1507             :            further anyway.
    1508             :         */
    1509         427 :         zend_accel_move_user_functions(&ZCG(function_table), &new_persistent_script->function_table);
    1510         427 :         new_persistent_script->main_op_array = *op_array;
    1511             : 
    1512         427 :         efree(op_array); /* we have valid persistent_script, so it's safe to free op_array */
    1513             : 
    1514             :     /* Fill in the ping_auto_globals_mask for the new script. If jit for auto globals is enabled we
    1515             :        will have to ping the used auto global variables before execution */
    1516         427 :         if (PG(auto_globals_jit)) {
    1517         427 :                 new_persistent_script->ping_auto_globals_mask = zend_accel_get_auto_globals();
    1518             :         } else {
    1519           0 :                 new_persistent_script->ping_auto_globals_mask = zend_accel_get_auto_globals_no_jit();
    1520             :         }
    1521             : 
    1522         427 :         if (ZCG(accel_directives).validate_timestamps) {
    1523             :                 /* Obtain the file timestamps, *before* actually compiling them,
    1524             :                  * otherwise we have a race-condition.
    1525             :                  */
    1526         427 :                 new_persistent_script->timestamp = timestamp;
    1527         427 :                 new_persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
    1528             :         }
    1529             : 
    1530         427 :         if (file_handle->opened_path) {
    1531         854 :                 new_persistent_script->full_path = zend_string_copy(file_handle->opened_path);
    1532             :         } else {
    1533           0 :                 new_persistent_script->full_path = zend_string_init(file_handle->filename, strlen(file_handle->filename), 0);
    1534             :         }
    1535         427 :         zend_string_hash_val(new_persistent_script->full_path);
    1536             : 
    1537             :         /* Now persistent_script structure is ready in process memory */
    1538         427 :         return new_persistent_script;
    1539             : }
    1540             : 
    1541             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1542             : zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)
    1543             : {
    1544             :         zend_persistent_script *persistent_script;
    1545             :         zend_op_array *op_array = NULL;
    1546             :         int from_memory; /* if the script we've got is stored in SHM */
    1547             : 
    1548             :         if (is_stream_path(file_handle->filename) &&
    1549             :             !is_cacheable_stream_path(file_handle->filename)) {
    1550             :                 return accelerator_orig_compile_file(file_handle, type);
    1551             :         }
    1552             : 
    1553             :         if (!file_handle->opened_path) {
    1554             :                 if (file_handle->type == ZEND_HANDLE_FILENAME &&
    1555             :                     accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) == FAILURE) {
    1556             :                         if (type == ZEND_REQUIRE) {
    1557             :                                 zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
    1558             :                                 zend_bailout();
    1559             :                         } else {
    1560             :                                 zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
    1561             :                         }
    1562             :                         return NULL;
    1563             :             }
    1564             :         }
    1565             : 
    1566             :         persistent_script = zend_file_cache_script_load(file_handle);
    1567             :         if (persistent_script) {
    1568             :                 /* see bug #15471 (old BTS) */
    1569             :                 if (persistent_script->full_path) {
    1570             :                         if (!EG(current_execute_data) || !EG(current_execute_data)->opline ||
    1571             :                             !EG(current_execute_data)->func ||
    1572             :                             !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
    1573             :                             EG(current_execute_data)->opline->opcode != ZEND_INCLUDE_OR_EVAL ||
    1574             :                             (EG(current_execute_data)->opline->extended_value != ZEND_INCLUDE_ONCE &&
    1575             :                              EG(current_execute_data)->opline->extended_value != ZEND_REQUIRE_ONCE)) {
    1576             :                                 if (zend_hash_add_empty_element(&EG(included_files), persistent_script->full_path) != NULL) {
    1577             :                                         /* ext/phar has to load phar's metadata into memory */
    1578             :                                         if (persistent_script->is_phar) {
    1579             :                                                 php_stream_statbuf ssb;
    1580             :                                                 char *fname = emalloc(sizeof("phar://") + persistent_script->full_path->len);
    1581             : 
    1582             :                                                 memcpy(fname, "phar://", sizeof("phar://") - 1);
    1583             :                                                 memcpy(fname + sizeof("phar://") - 1, persistent_script->full_path->val, persistent_script->full_path->len + 1);
    1584             :                                                 php_stream_stat_path(fname, &ssb);
    1585             :                                                 efree(fname);
    1586             :                                         }
    1587             :                                 }
    1588             :                         }
    1589             :                 }
    1590             :                 zend_file_handle_dtor(file_handle);
    1591             : 
    1592             :                 persistent_script->dynamic_members.last_used = ZCG(request_time);
    1593             : 
    1594             :             if (persistent_script->ping_auto_globals_mask) {
    1595             :                         zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask);
    1596             :                 }
    1597             : 
    1598             :                 return zend_accel_load_script(persistent_script, 1);
    1599             :         }
    1600             : 
    1601             :         persistent_script = opcache_compile_file(file_handle, type, NULL, 0, &op_array);
    1602             : 
    1603             :         if (persistent_script) {
    1604             :                 from_memory = 0;
    1605             :                 persistent_script = cache_script_in_file_cache(persistent_script, &from_memory);
    1606             :                 return zend_accel_load_script(persistent_script, from_memory);
    1607             :         }
    1608             : 
    1609             :         return op_array;
    1610             : }
    1611             : #endif
    1612             : 
    1613             : /* zend_compile() replacement */
    1614         432 : zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
    1615             : {
    1616         432 :         zend_persistent_script *persistent_script = NULL;
    1617         432 :         char *key = NULL;
    1618             :         int key_length;
    1619             :         int from_shared_memory; /* if the script we've got is stored in SHM */
    1620             : 
    1621         432 :         if (!file_handle->filename || !ZCG(enabled) || !accel_startup_ok) {
    1622             :                 /* The Accelerator is disabled, act as if without the Accelerator */
    1623           0 :                 return accelerator_orig_compile_file(file_handle, type);
    1624             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1625             :         } else if (ZCG(accel_directives).file_cache_only) {
    1626             :                 return file_cache_compile_file(file_handle, type);
    1627             : #endif
    1628         864 :         } else if ((!ZCG(counted) && !ZCSG(accelerator_enabled)) ||
    1629         432 :                    (ZCSG(restart_in_progress) && accel_restart_is_active())) {
    1630             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1631             :                 if (ZCG(accel_directives).file_cache) {
    1632             :                         return file_cache_compile_file(file_handle, type);
    1633             :                 }
    1634             : #endif
    1635           0 :                 return accelerator_orig_compile_file(file_handle, type);
    1636             :         }
    1637             : 
    1638             :         /* In case this callback is called from include_once, require_once or it's
    1639             :          * a main FastCGI request, the key must be already calculated, and cached
    1640             :          * persistent script already found */
    1641         432 :         if (ZCG(cache_persistent_script) &&
    1642           0 :             ((!EG(current_execute_data) &&
    1643           0 :               file_handle->filename == SG(request_info).path_translated &&
    1644           0 :               ZCG(cache_opline) == NULL) ||
    1645           0 :              (EG(current_execute_data) &&
    1646           0 :               EG(current_execute_data)->func &&
    1647           0 :               ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
    1648           0 :               ZCG(cache_opline) == EG(current_execute_data)->opline))) {
    1649             : 
    1650           0 :                 persistent_script = ZCG(cache_persistent_script);
    1651           0 :                 if (ZCG(key_len)) {
    1652           0 :                         key = ZCG(key);
    1653           0 :                         key_length = ZCG(key_len);
    1654             :                 }
    1655             : 
    1656             :         } else {
    1657         432 :                 if (!ZCG(accel_directives).revalidate_path) {
    1658             :                         /* try to find cached script by key */
    1659         432 :                         key = accel_make_persistent_key(file_handle->filename, strlen(file_handle->filename), &key_length);
    1660         432 :                         if (!key) {
    1661           0 :                                 return accelerator_orig_compile_file(file_handle, type);
    1662             :                         }
    1663         432 :                         persistent_script = zend_accel_hash_str_find(&ZCSG(hash), key, key_length);
    1664             :                 }
    1665         432 :                 if (!persistent_script) {
    1666             :                         /* try to find cached script by full real path */
    1667             :                         zend_accel_hash_entry *bucket;
    1668             : 
    1669             :                         /* open file to resolve the path */
    1670         483 :                     if (file_handle->type == ZEND_HANDLE_FILENAME &&
    1671          51 :                         accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) == FAILURE) {
    1672           2 :                                 if (type == ZEND_REQUIRE) {
    1673           0 :                                         zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
    1674           0 :                                         zend_bailout();
    1675             :                                 } else {
    1676           2 :                                         zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
    1677             :                                 }
    1678           2 :                                 return NULL;
    1679             :                     }
    1680             : 
    1681         430 :                         if (file_handle->opened_path) {
    1682         430 :                                 bucket = zend_accel_hash_find_entry(&ZCSG(hash), file_handle->opened_path);
    1683             : 
    1684         430 :                                 if (bucket) {
    1685           0 :                                         persistent_script = (zend_persistent_script *)bucket->data;
    1686             : 
    1687           0 :                                         if (key && !persistent_script->corrupted) {
    1688           0 :                                                 SHM_UNPROTECT();
    1689           0 :                                                 zend_shared_alloc_lock();
    1690           0 :                                                 zend_accel_add_key(key, key_length, bucket);
    1691           0 :                                                 zend_shared_alloc_unlock();
    1692           0 :                                                 SHM_PROTECT();
    1693             :                                         }
    1694             :                                 }
    1695             :                         }
    1696             :                 }
    1697             :         }
    1698             : 
    1699             :         /* clear cache */
    1700         430 :         ZCG(cache_opline) = NULL;
    1701         430 :         ZCG(cache_persistent_script) = NULL;
    1702             : 
    1703         430 :         if (persistent_script && persistent_script->corrupted) {
    1704           0 :                 persistent_script = NULL;
    1705             :         }
    1706             : 
    1707             :         /* Make sure we only increase the currently running processes semaphore
    1708             :      * once each execution (this function can be called more than once on
    1709             :      * each execution)
    1710             :      */
    1711         430 :         if (!ZCG(counted)) {
    1712         374 :                 ZCG(counted) = 1;
    1713         374 :                 accel_activate_add();
    1714             :         }
    1715             : 
    1716         430 :         SHM_UNPROTECT();
    1717             : 
    1718             :         /* If script is found then validate_timestamps if option is enabled */
    1719         430 :         if (persistent_script && ZCG(accel_directives).validate_timestamps) {
    1720           0 :                 if (validate_timestamp_and_record(persistent_script, file_handle) == FAILURE) {
    1721           0 :                         zend_shared_alloc_lock();
    1722           0 :                         if (!persistent_script->corrupted) {
    1723           0 :                                 persistent_script->corrupted = 1;
    1724           0 :                                 persistent_script->timestamp = 0;
    1725           0 :                                 ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
    1726           0 :                                 if (ZSMMG(memory_exhausted)) {
    1727             :                                         zend_accel_restart_reason reason =
    1728           0 :                                                 zend_accel_hash_is_full(&ZCSG(hash)) ? ACCEL_RESTART_HASH : ACCEL_RESTART_OOM;
    1729           0 :                                         zend_accel_schedule_restart_if_necessary(reason);
    1730             :                                 }
    1731             :                         }
    1732           0 :                         zend_shared_alloc_unlock();
    1733           0 :                         persistent_script = NULL;
    1734             :                 }
    1735             :         }
    1736             : 
    1737             :         /* if turned on - check the compiled script ADLER32 checksum */
    1738         430 :         if (persistent_script && ZCG(accel_directives).consistency_checks
    1739           0 :                 && persistent_script->dynamic_members.hits % ZCG(accel_directives).consistency_checks == 0) {
    1740             : 
    1741           0 :                 unsigned int checksum = zend_accel_script_checksum(persistent_script);
    1742           0 :                 if (checksum != persistent_script->dynamic_members.checksum ) {
    1743             :                         /* The checksum is wrong */
    1744           0 :                         zend_accel_error(ACCEL_LOG_INFO, "Checksum failed for '%s':  expected=0x%0.8X, found=0x%0.8X",
    1745             :                                                          persistent_script->full_path, persistent_script->dynamic_members.checksum, checksum);
    1746           0 :                         zend_shared_alloc_lock();
    1747           0 :                         if (!persistent_script->corrupted) {
    1748           0 :                                 persistent_script->corrupted = 1;
    1749           0 :                                 persistent_script->timestamp = 0;
    1750           0 :                                 ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
    1751           0 :                                 if (ZSMMG(memory_exhausted)) {
    1752             :                                         zend_accel_restart_reason reason =
    1753           0 :                                                 zend_accel_hash_is_full(&ZCSG(hash)) ? ACCEL_RESTART_HASH : ACCEL_RESTART_OOM;
    1754           0 :                                         zend_accel_schedule_restart_if_necessary(reason);
    1755             :                                 }
    1756             :                         }
    1757           0 :                         zend_shared_alloc_unlock();
    1758           0 :                         persistent_script = NULL;
    1759             :                 }
    1760             :         }
    1761             : 
    1762             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1763             :         /* Check the second level cache */
    1764             :         if (!persistent_script && ZCG(accel_directives).file_cache) {
    1765             :                 persistent_script = zend_file_cache_script_load(file_handle);
    1766             :         }
    1767             : #endif
    1768             : 
    1769             :         /* If script was not found or invalidated by validate_timestamps */
    1770         430 :         if (!persistent_script) {
    1771         430 :                 uint32_t old_const_num = zend_hash_next_free_element(EG(zend_constants));
    1772             :                 zend_op_array *op_array;
    1773             : 
    1774             :                 /* Cache miss.. */
    1775         430 :                 ZCSG(misses)++;
    1776             : 
    1777             :                 /* No memory left. Behave like without the Accelerator */
    1778         430 :                 if (ZSMMG(memory_exhausted) || ZCSG(restart_pending)) {
    1779           0 :                         SHM_PROTECT();
    1780           0 :                         return accelerator_orig_compile_file(file_handle, type);
    1781             :                 }
    1782             : 
    1783             :                 /* Try and cache the script and assume that it is returned from_shared_memory.
    1784             :          * If it isn't compile_and_cache_file() changes the flag to 0
    1785             :          */
    1786         430 :         from_shared_memory = 0;
    1787         430 :                 persistent_script = opcache_compile_file(file_handle, type, key, key ? key_length : 0, &op_array);
    1788         428 :                 if (persistent_script) {
    1789         427 :                         persistent_script = cache_script_in_shared_memory(persistent_script, key, key ? key_length : 0, &from_shared_memory);
    1790             :                 }
    1791             : 
    1792             :                 /* Caching is disabled, returning op_array;
    1793             :                  * or something went wrong during compilation, returning NULL
    1794             :                  */
    1795         428 :                 if (!persistent_script) {
    1796           1 :                         SHM_PROTECT();
    1797           1 :                         return op_array;
    1798             :                 }
    1799         427 :                 if (from_shared_memory) {
    1800             :                         /* Delete immutable arrays moved into SHM */
    1801         427 :                         uint32_t new_const_num = zend_hash_next_free_element(EG(zend_constants));
    1802         854 :                         while (new_const_num > old_const_num) {
    1803           0 :                                 new_const_num--;
    1804           0 :                                 zend_hash_index_del(EG(zend_constants), new_const_num);
    1805             :                         }
    1806             :                 }
    1807             :         } else {
    1808             : 
    1809             : #if !ZEND_WIN32
    1810           0 :                 ZCSG(hits)++; /* TBFixed: may lose one hit */
    1811           0 :                 persistent_script->dynamic_members.hits++; /* see above */
    1812             : #else
    1813             :                 INCREMENT(hits);
    1814             :                 InterlockedIncrement64(&persistent_script->dynamic_members.hits);
    1815             : #endif
    1816             : 
    1817             :                 /* see bug #15471 (old BTS) */
    1818           0 :                 if (persistent_script->full_path) {
    1819           0 :                         if (!EG(current_execute_data) || !EG(current_execute_data)->opline ||
    1820           0 :                             !EG(current_execute_data)->func ||
    1821           0 :                             !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
    1822           0 :                             EG(current_execute_data)->opline->opcode != ZEND_INCLUDE_OR_EVAL ||
    1823           0 :                             (EG(current_execute_data)->opline->extended_value != ZEND_INCLUDE_ONCE &&
    1824           0 :                              EG(current_execute_data)->opline->extended_value != ZEND_REQUIRE_ONCE)) {
    1825           0 :                                 if (zend_hash_add_empty_element(&EG(included_files), persistent_script->full_path) != NULL) {
    1826             :                                         /* ext/phar has to load phar's metadata into memory */
    1827           0 :                                         if (persistent_script->is_phar) {
    1828             :                                                 php_stream_statbuf ssb;
    1829           0 :                                                 char *fname = emalloc(sizeof("phar://") + persistent_script->full_path->len);
    1830             : 
    1831           0 :                                                 memcpy(fname, "phar://", sizeof("phar://") - 1);
    1832           0 :                                                 memcpy(fname + sizeof("phar://") - 1, persistent_script->full_path->val, persistent_script->full_path->len + 1);
    1833           0 :                                                 php_stream_stat_path(fname, &ssb);
    1834           0 :                                                 efree(fname);
    1835             :                                         }
    1836             :                                 }
    1837             :                         }
    1838             :                 }
    1839           0 :                 zend_file_handle_dtor(file_handle);
    1840           0 :                 from_shared_memory = 1;
    1841             :         }
    1842             : 
    1843         427 :         persistent_script->dynamic_members.last_used = ZCG(request_time);
    1844             : 
    1845         427 :         SHM_PROTECT();
    1846             : 
    1847             :     /* Fetch jit auto globals used in the script before execution */
    1848         427 :     if (persistent_script->ping_auto_globals_mask) {
    1849         133 :                 zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask);
    1850             :         }
    1851             : 
    1852         427 :         return zend_accel_load_script(persistent_script, from_shared_memory);
    1853             : }
    1854             : 
    1855             : /* zend_stream_open_function() replacement for PHP 5.3 and above */
    1856         421 : static int persistent_stream_open_function(const char *filename, zend_file_handle *handle)
    1857             : {
    1858         421 :         if (ZCG(cache_persistent_script)) {
    1859             :                 /* check if callback is called from include_once or it's a main request */
    1860           0 :                 if ((!EG(current_execute_data) &&
    1861           0 :                      filename == SG(request_info).path_translated &&
    1862           0 :                      ZCG(cache_opline) == NULL) ||
    1863           0 :                     (EG(current_execute_data) &&
    1864           0 :                      EG(current_execute_data)->func &&
    1865           0 :                      ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
    1866           0 :                      ZCG(cache_opline) == EG(current_execute_data)->opline)) {
    1867             : 
    1868             :                         /* we are in include_once or FastCGI request */
    1869           0 :                         handle->filename = (char*)filename;
    1870           0 :                         handle->free_filename = 0;
    1871           0 :                         handle->opened_path = zend_string_copy(ZCG(cache_persistent_script)->full_path);
    1872           0 :                         handle->type = ZEND_HANDLE_FILENAME;
    1873           0 :                         return SUCCESS;
    1874             :                 }
    1875           0 :                 ZCG(cache_opline) = NULL;
    1876           0 :                 ZCG(cache_persistent_script) = NULL;
    1877             :         }
    1878         421 :         return accelerator_orig_zend_stream_open_function(filename, handle);
    1879             : }
    1880             : 
    1881             : /* zend_resolve_path() replacement for PHP 5.3 and above */
    1882         847 : static zend_string* persistent_zend_resolve_path(const char *filename, int filename_len)
    1883             : {
    1884        3277 :         if (ZCG(enabled) && accel_startup_ok &&
    1885        1583 :             (ZCG(counted) || ZCSG(accelerator_enabled)) &&
    1886         847 :             !ZCSG(restart_in_progress)) {
    1887             : 
    1888             :                 /* check if callback is called from include_once or it's a main request */
    1889        1996 :                 if ((!EG(current_execute_data) &&
    1890         770 :                      filename == SG(request_info).path_translated) ||
    1891         111 :                     (EG(current_execute_data) &&
    1892          77 :                      EG(current_execute_data)->func &&
    1893          77 :                      ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
    1894          38 :                      EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
    1895          38 :                      (EG(current_execute_data)->opline->extended_value == ZEND_INCLUDE_ONCE ||
    1896          38 :                       EG(current_execute_data)->opline->extended_value == ZEND_REQUIRE_ONCE))) {
    1897             : 
    1898             :                         /* we are in include_once or FastCGI request */
    1899             :                         zend_string *resolved_path;
    1900             :                         int key_length;
    1901         750 :                         char *key = NULL;
    1902             :                         
    1903         750 :                         if (!ZCG(accel_directives).revalidate_path) {
    1904             :                                 /* lookup by "not-real" path */
    1905         750 :                                 key = accel_make_persistent_key(filename, filename_len, &key_length);
    1906         750 :                                 if (key) {
    1907         750 :                                         zend_accel_hash_entry *bucket = zend_accel_hash_str_find_entry(&ZCSG(hash), key, key_length);
    1908         750 :                                         if (bucket != NULL) {
    1909           0 :                                                 zend_persistent_script *persistent_script = (zend_persistent_script *)bucket->data;
    1910           0 :                                                 if (!persistent_script->corrupted) {
    1911           0 :                                                         ZCG(cache_opline) = EG(current_execute_data) ? EG(current_execute_data)->opline : NULL;
    1912           0 :                                                         ZCG(cache_persistent_script) = persistent_script;
    1913           0 :                                                         return zend_string_copy(persistent_script->full_path);
    1914             :                                                 }
    1915             :                                         }
    1916             :                                 } else {
    1917           0 :                                         ZCG(cache_opline) = NULL;
    1918           0 :                                         ZCG(cache_persistent_script) = NULL;
    1919           0 :                                         return accelerator_orig_zend_resolve_path(filename, filename_len);
    1920             :                                 }
    1921             :                         }
    1922             : 
    1923             :                         /* find the full real path */
    1924         750 :                         resolved_path = accelerator_orig_zend_resolve_path(filename, filename_len);
    1925             : 
    1926         750 :                         if (resolved_path) {
    1927             :                                 /* lookup by real path */
    1928         750 :                                 zend_accel_hash_entry *bucket = zend_accel_hash_find_entry(&ZCSG(hash), resolved_path);
    1929         750 :                                 if (bucket) {
    1930           0 :                                         zend_persistent_script *persistent_script = (zend_persistent_script *)bucket->data;
    1931           0 :                                         if (!persistent_script->corrupted) {
    1932           0 :                                                 if (key) {
    1933             :                                                         /* add another "key" for the same bucket */
    1934           0 :                                                         SHM_UNPROTECT();
    1935           0 :                                                         zend_shared_alloc_lock();
    1936           0 :                                                         zend_accel_add_key(key, key_length, bucket);
    1937           0 :                                                         zend_shared_alloc_unlock();
    1938           0 :                                                         SHM_PROTECT();
    1939             :                                                 } else {
    1940           0 :                                                         ZCG(key_len) = 0;
    1941             :                                                 }
    1942           0 :                                                 ZCG(cache_opline) = EG(current_execute_data) ? EG(current_execute_data)->opline : NULL;
    1943           0 :                                                 ZCG(cache_persistent_script) = persistent_script;
    1944           0 :                                                 return resolved_path;
    1945             :                                         }
    1946             :                                 }
    1947             :                         }
    1948             : 
    1949         750 :                         ZCG(cache_opline) = NULL;
    1950         750 :                         ZCG(cache_persistent_script) = NULL;
    1951         750 :                         return resolved_path;
    1952             :                 }
    1953             :         }
    1954          97 :         ZCG(cache_opline) = NULL;
    1955          97 :         ZCG(cache_persistent_script) = NULL;
    1956          97 :         return accelerator_orig_zend_resolve_path(filename, filename_len);
    1957             : }
    1958             : 
    1959         374 : static void zend_reset_cache_vars(void)
    1960             : {
    1961         374 :         ZSMMG(memory_exhausted) = 0;
    1962         374 :         ZCSG(hits) = 0;
    1963         374 :         ZCSG(misses) = 0;
    1964         374 :         ZCSG(blacklist_misses) = 0;
    1965         374 :         ZSMMG(wasted_shared_memory) = 0;
    1966         374 :         ZCSG(restart_pending) = 0;
    1967         374 :         ZCSG(force_restart_time) = 0;
    1968         374 : }
    1969             : 
    1970         374 : static void accel_reset_pcre_cache(void)
    1971             : {
    1972             :         Bucket *p;
    1973             : 
    1974         374 :         ZEND_HASH_FOREACH_BUCKET(&PCRE_G(pcre_cache), p) {
    1975             :                 /* Remove PCRE cache entries with inconsistent keys */
    1976           0 :                 if (IS_INTERNED(p->key)) {
    1977           0 :                         p->key = NULL;
    1978           0 :                         zend_hash_del_bucket(&PCRE_G(pcre_cache), p);
    1979             :                 }
    1980             :         } ZEND_HASH_FOREACH_END();
    1981         374 : }
    1982             : 
    1983         374 : static void accel_activate(void)
    1984             : {
    1985         374 :         zend_bool reset_pcre = 0;
    1986             : 
    1987         374 :         if (!ZCG(enabled) || !accel_startup_ok) {
    1988           0 :                 return;
    1989             :         }
    1990             : 
    1991         374 :         if (!ZCG(function_table).nTableSize) {
    1992         374 :                 zend_hash_init(&ZCG(function_table), zend_hash_num_elements(CG(function_table)), NULL, ZEND_FUNCTION_DTOR, 1);
    1993         374 :                 zend_accel_copy_internal_functions();
    1994             :         }
    1995             : 
    1996             :         /* PHP-5.4 and above return "double", but we use 1 sec precision */
    1997         374 :         ZCG(auto_globals_mask) = 0;
    1998         374 :         ZCG(request_time) = (time_t)sapi_get_request_time();
    1999         374 :         ZCG(cache_opline) = NULL;
    2000         374 :         ZCG(cache_persistent_script) = NULL;
    2001         374 :         ZCG(include_path_key_len) = 0;
    2002         374 :         ZCG(include_path_check) = 1;
    2003             : 
    2004             :         /* check if ZCG(function_table) wasn't somehow polluted on the way */
    2005         374 :         if (ZCG(internal_functions_count) != zend_hash_num_elements(&ZCG(function_table))) {
    2006           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Internal functions count changed - was %d, now %d", ZCG(internal_functions_count), zend_hash_num_elements(&ZCG(function_table)));
    2007             :         }
    2008             : 
    2009         374 :         ZCG(cwd) = NULL;
    2010         374 :         ZCG(cwd_key_len) = 0;
    2011         374 :         ZCG(cwd_check) = 1;
    2012             : 
    2013             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2014             :         if (ZCG(accel_directives).file_cache_only) {
    2015             :                 return;
    2016             :         }
    2017             : #endif
    2018             : 
    2019         374 :         SHM_UNPROTECT();
    2020             : 
    2021         374 :         if (ZCG(counted)) {
    2022             : #ifdef ZTS
    2023             :                 zend_accel_error(ACCEL_LOG_WARNING, "Stuck count for thread id %d", tsrm_thread_id());
    2024             : #else
    2025           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Stuck count for pid %d", getpid());
    2026             : #endif
    2027           0 :                 accel_unlock_all();
    2028           0 :                 ZCG(counted) = 0;
    2029             :         }
    2030             : 
    2031         374 :         if (ZCSG(restart_pending)) {
    2032           0 :                 zend_shared_alloc_lock();
    2033           0 :                 if (ZCSG(restart_pending) != 0) { /* check again, to ensure that the cache wasn't already cleaned by another process */
    2034           0 :                         if (accel_is_inactive() == SUCCESS) {
    2035           0 :                                 zend_accel_error(ACCEL_LOG_DEBUG, "Restarting!");
    2036           0 :                                 ZCSG(restart_pending) = 0;
    2037           0 :                                 switch ZCSG(restart_reason) {
    2038             :                                         case ACCEL_RESTART_OOM:
    2039           0 :                                                 ZCSG(oom_restarts)++;
    2040           0 :                                                 break;
    2041             :                                         case ACCEL_RESTART_HASH:
    2042           0 :                                                 ZCSG(hash_restarts)++;
    2043           0 :                                                 break;
    2044             :                                         case ACCEL_RESTART_USER:
    2045           0 :                                                 ZCSG(manual_restarts)++;
    2046             :                                                 break;
    2047             :                                 }
    2048           0 :                                 accel_restart_enter();
    2049             : 
    2050           0 :                                 zend_reset_cache_vars();
    2051           0 :                                 zend_accel_hash_clean(&ZCSG(hash));
    2052             : 
    2053             : #if !defined(ZTS)
    2054           0 :                                 if (ZCG(accel_directives).interned_strings_buffer) {
    2055           0 :                                         accel_interned_strings_restore_state();
    2056             :                                 }
    2057             : #endif
    2058             : 
    2059           0 :                                 zend_shared_alloc_restore_state();
    2060           0 :                                 ZCSG(accelerator_enabled) = ZCSG(cache_status_before_restart);
    2061           0 :                                 if (ZCSG(last_restart_time) < ZCG(request_time)) {
    2062           0 :                                         ZCSG(last_restart_time) = ZCG(request_time);
    2063             :                                 } else {
    2064           0 :                                         ZCSG(last_restart_time)++;
    2065             :                                 }
    2066           0 :                                 accel_restart_leave();
    2067             :                         }
    2068             :                 } else {
    2069           0 :                         reset_pcre = 1;
    2070             :                 }
    2071           0 :                 zend_shared_alloc_unlock();
    2072             :         }
    2073             : 
    2074         374 :         SHM_PROTECT();
    2075             : 
    2076         374 :         if (ZCSG(last_restart_time) != ZCG(last_restart_time)) {
    2077             :                 /* SHM was reinitialized. */
    2078           0 :                 ZCG(last_restart_time) = ZCSG(last_restart_time);
    2079             : 
    2080             :                 /* Reset in-process realpath cache */
    2081           0 :                 realpath_cache_clean();
    2082             : 
    2083           0 :                 accel_reset_pcre_cache();
    2084         374 :         } else if (reset_pcre) {
    2085           0 :                 accel_reset_pcre_cache();
    2086             :         }
    2087             : }
    2088             : 
    2089             : #if !ZEND_DEBUG
    2090             : 
    2091             : /* Fast Request Shutdown
    2092             :  * =====================
    2093             :  * Zend Memory Manager frees memory by its own. We don't have to free each
    2094             :  * allocated block separately, but we like to call all the destructors and
    2095             :  * callbacks in exactly the same order.
    2096             :  */
    2097             : static void accel_fast_hash_destroy(HashTable *ht);
    2098             : 
    2099           0 : static void accel_fast_zval_dtor(zval *zvalue)
    2100             : {
    2101             : tail_call:
    2102           0 :         switch (Z_TYPE_P(zvalue)) {
    2103             :                 case IS_ARRAY:
    2104           0 :                         GC_REMOVE_FROM_BUFFER(Z_ARR_P(zvalue));
    2105           0 :                         if (Z_ARR_P(zvalue) != &EG(symbol_table)) {
    2106             :                                 /* break possible cycles */
    2107           0 :                                 ZVAL_NULL(zvalue);
    2108           0 :                                 accel_fast_hash_destroy(Z_ARRVAL_P(zvalue));
    2109             :                         }
    2110           0 :                         break;
    2111             :                 case IS_OBJECT:
    2112           0 :                         OBJ_RELEASE(Z_OBJ_P(zvalue));
    2113           0 :                         break;
    2114             :                 case IS_RESOURCE:
    2115           0 :                         zend_list_delete(Z_RES_P(zvalue));
    2116           0 :                         break;
    2117             :                 case IS_REFERENCE: {
    2118           0 :                                 zend_reference *ref = Z_REF_P(zvalue);
    2119             : 
    2120           0 :                                 if (--GC_REFCOUNT(ref) == 0) {
    2121           0 :                                         if (Z_REFCOUNTED(ref->val) && Z_DELREF(ref->val) == 0) {
    2122           0 :                                                 zvalue = &ref->val;
    2123           0 :                                                 goto tail_call;
    2124             :                                         }
    2125             :                                 }
    2126             :                         }
    2127             :                         break;
    2128             :         }
    2129           0 : }
    2130             : 
    2131           0 : static void accel_fast_hash_destroy(HashTable *ht)
    2132             : {
    2133           0 :         Bucket *p = ht->arData;
    2134           0 :         Bucket *end = p + ht->nNumUsed;
    2135             : 
    2136           0 :         while (p != end) {
    2137           0 :                 if (Z_REFCOUNTED(p->val) && Z_DELREF(p->val) == 0) {
    2138           0 :                         accel_fast_zval_dtor(&p->val);
    2139             :                 }
    2140           0 :                 p++;
    2141             :         }
    2142           0 : }
    2143             : 
    2144           0 : static inline void zend_accel_fast_del_bucket(HashTable *ht, uint32_t idx, Bucket *p)
    2145             : {
    2146           0 :         uint32_t nIndex = p->h | ht->nTableMask;
    2147           0 :         uint32_t i = HT_HASH(ht, nIndex);
    2148             : 
    2149           0 :         ht->nNumOfElements--;
    2150           0 :         if (idx != i) {
    2151           0 :                 Bucket *prev = HT_HASH_TO_BUCKET(ht, i);
    2152           0 :                 while (Z_NEXT(prev->val) != idx) {
    2153           0 :                         i = Z_NEXT(prev->val);
    2154           0 :                         prev = HT_HASH_TO_BUCKET(ht, i);
    2155             :                 }
    2156           0 :                 Z_NEXT(prev->val) = Z_NEXT(p->val);
    2157             :         } else {
    2158           0 :                 HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
    2159             :         }
    2160           0 : }
    2161             : 
    2162           0 : static void zend_accel_fast_shutdown(void)
    2163             : {
    2164           0 :         if (EG(full_tables_cleanup)) {
    2165           0 :                 return;
    2166             :         }
    2167             : 
    2168           0 :         if (EG(objects_store).top > 1 || zend_hash_num_elements(&EG(regular_list)) > 0) {
    2169             :                 /* We don't have to destroy all zvals if they cannot call any destructors */
    2170           0 :                 zend_try {
    2171           0 :                         ZEND_HASH_REVERSE_FOREACH(&EG(symbol_table), 0) {
    2172           0 :                                 if (Z_REFCOUNTED(_p->val) && Z_DELREF(_p->val) == 0) {
    2173           0 :                                         accel_fast_zval_dtor(&_p->val);
    2174             :                                 }
    2175           0 :                                 zend_accel_fast_del_bucket(&EG(symbol_table), HT_IDX_TO_HASH(_idx-1), _p);
    2176             :                         } ZEND_HASH_FOREACH_END();
    2177           0 :                 } zend_end_try();
    2178           0 :                 zend_hash_init(&EG(symbol_table), 8, NULL, NULL, 0);
    2179             : 
    2180           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(function_table), 0) {
    2181           0 :                         zend_function *func = Z_PTR(_p->val);
    2182             : 
    2183           0 :                         if (func->type == ZEND_INTERNAL_FUNCTION) {
    2184           0 :                                 break;
    2185             :                         } else {
    2186           0 :                                 if (func->op_array.static_variables) {
    2187           0 :                                         if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
    2188           0 :                                                 if (--GC_REFCOUNT(func->op_array.static_variables) == 0) {
    2189           0 :                                                         accel_fast_hash_destroy(func->op_array.static_variables);
    2190             :                                                 }
    2191             :                                         }
    2192             :                                 }
    2193           0 :                                 zend_accel_fast_del_bucket(EG(function_table), HT_IDX_TO_HASH(_idx-1), _p);
    2194             :                         }
    2195             :                 } ZEND_HASH_FOREACH_END();
    2196             : 
    2197           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(class_table), 0) {
    2198           0 :                         zend_class_entry *ce = Z_PTR(_p->val);
    2199             : 
    2200           0 :                         if (ce->type == ZEND_INTERNAL_CLASS) {
    2201           0 :                                 break;
    2202             :                         } else {
    2203           0 :                                 if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
    2204             :                                         zend_function *func;
    2205             : 
    2206           0 :                                         ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
    2207           0 :                                                 if (func->type == ZEND_USER_FUNCTION) {
    2208           0 :                                                         if (func->op_array.static_variables) {
    2209           0 :                                                                 if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
    2210           0 :                                                                         if (--GC_REFCOUNT(func->op_array.static_variables) == 0) {
    2211           0 :                                                                                 accel_fast_hash_destroy(func->op_array.static_variables);
    2212             :                                                                         }
    2213             :                                                                 }
    2214           0 :                                                                 func->op_array.static_variables = NULL;
    2215             :                                                         }
    2216             :                                                 }
    2217             :                                         } ZEND_HASH_FOREACH_END();
    2218             :                                 }
    2219           0 :                                 if (ce->static_members_table) {
    2220             :                                         int i;
    2221             : 
    2222           0 :                                         for (i = 0; i < ce->default_static_members_count; i++) {
    2223           0 :                                                 zval *zv = &ce->static_members_table[i];
    2224           0 :                                                 ZVAL_UNDEF(&ce->static_members_table[i]);
    2225           0 :                                                 if (Z_REFCOUNTED_P(zv) && Z_DELREF_P(zv) == 0) {
    2226           0 :                                                         accel_fast_zval_dtor(zv);
    2227             :                                                 }
    2228             :                                         }
    2229           0 :                                         ce->static_members_table = NULL;
    2230             :                                 }
    2231           0 :                                 zend_accel_fast_del_bucket(EG(class_table), HT_IDX_TO_HASH(_idx-1), _p);
    2232             :                         }
    2233             :                 } ZEND_HASH_FOREACH_END();
    2234             : 
    2235             :         } else {
    2236             : 
    2237           0 :                 zend_hash_init(&EG(symbol_table), 8, NULL, NULL, 0);
    2238             : 
    2239           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(function_table), 0) {
    2240           0 :                         zend_function *func = Z_PTR(_p->val);
    2241             : 
    2242           0 :                         if (func->type == ZEND_INTERNAL_FUNCTION) {
    2243           0 :                                 break;
    2244             :                         } else {
    2245           0 :                                 zend_accel_fast_del_bucket(EG(function_table), HT_IDX_TO_HASH(_idx-1), _p);
    2246             :                         }
    2247             :                 } ZEND_HASH_FOREACH_END();
    2248             : 
    2249           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(class_table), 0) {
    2250           0 :                         zend_class_entry *ce = Z_PTR(_p->val);
    2251             : 
    2252           0 :                         if (ce->type == ZEND_INTERNAL_CLASS) {
    2253           0 :                                 break;
    2254             :                         } else {
    2255           0 :                                 zend_accel_fast_del_bucket(EG(class_table), HT_IDX_TO_HASH(_idx-1), _p);
    2256             :                         }
    2257             :                 } ZEND_HASH_FOREACH_END();
    2258             :         }
    2259             : 
    2260           0 :         ZEND_HASH_REVERSE_FOREACH(EG(zend_constants), 0) {
    2261           0 :                 zend_constant *c = Z_PTR(_p->val);
    2262             : 
    2263           0 :                 if (c->flags & CONST_PERSISTENT) {
    2264           0 :                         break;
    2265             :                 } else {
    2266           0 :                         zend_accel_fast_del_bucket(EG(zend_constants), HT_IDX_TO_HASH(_idx-1), _p);
    2267             :                 }
    2268             :         } ZEND_HASH_FOREACH_END();
    2269           0 :         EG(function_table)->nNumUsed = EG(function_table)->nNumOfElements;
    2270           0 :         EG(class_table)->nNumUsed = EG(class_table)->nNumOfElements;
    2271           0 :         EG(zend_constants)->nNumUsed = EG(zend_constants)->nNumOfElements;
    2272             : 
    2273           0 :         CG(unclean_shutdown) = 1;
    2274             : }
    2275             : #endif
    2276             : 
    2277         374 : static void accel_deactivate(void)
    2278             : {
    2279             :         /* ensure that we restore function_table and class_table
    2280             :          * In general, they're restored by persistent_compile_file(), but in case
    2281             :          * the script is aborted abnormally, they may become messed up.
    2282             :          */
    2283             : 
    2284         374 :         if (!ZCG(enabled) || !accel_startup_ok) {
    2285           0 :                 return;
    2286             :         }
    2287             : 
    2288         374 :         zend_shared_alloc_safe_unlock(); /* be sure we didn't leave cache locked */
    2289         374 :         accel_unlock_all();
    2290         374 :         ZCG(counted) = 0;
    2291             : 
    2292             : #if !ZEND_DEBUG
    2293         374 :         if (ZCG(accel_directives).fast_shutdown) {
    2294           0 :                 zend_accel_fast_shutdown();
    2295             :         }
    2296             : #endif
    2297             : 
    2298         374 :         if (ZCG(cwd)) {
    2299          25 :                 zend_string_release(ZCG(cwd));
    2300          25 :                 ZCG(cwd) = NULL;
    2301             :         }
    2302             : 
    2303             : }
    2304             : 
    2305       20550 : static int accelerator_remove_cb(zend_extension *element1, zend_extension *element2)
    2306             : {
    2307             :         (void)element2; /* keep the compiler happy */
    2308             : 
    2309       20550 :         if (!strcmp(element1->name, ACCELERATOR_PRODUCT_NAME )) {
    2310       20550 :                 element1->startup = NULL;
    2311             : #if 0
    2312             :                 /* We have to call shutdown callback it to free TS resources */
    2313             :                 element1->shutdown = NULL;
    2314             : #endif
    2315       20550 :                 element1->activate = NULL;
    2316       20550 :                 element1->deactivate = NULL;
    2317       20550 :                 element1->op_array_handler = NULL;
    2318             : 
    2319             : #ifdef __DEBUG_MESSAGES__
    2320             :         fprintf(stderr, ACCELERATOR_PRODUCT_NAME " is disabled: %s\n", (zps_failure_reason ? zps_failure_reason : "unknown error"));
    2321             :         fflush(stderr);
    2322             : #endif
    2323             :         }
    2324             : 
    2325       20550 :         return 0;
    2326             : }
    2327             : 
    2328       20550 : static void zps_startup_failure(char *reason, char *api_reason, int (*cb)(zend_extension *, zend_extension *))
    2329             : {
    2330       20550 :         accel_startup_ok = 0;
    2331       20550 :         zps_failure_reason = reason;
    2332       20550 :         zps_api_failure_reason = api_reason?api_reason:reason;
    2333       20550 :         zend_llist_del_element(&zend_extensions, NULL, (int (*)(void *, void *))cb);
    2334       20550 : }
    2335             : 
    2336       20924 : static inline int accel_find_sapi(void)
    2337             : {
    2338             :         static const char *supported_sapis[] = {
    2339             :                 "apache",
    2340             :                 "fastcgi",
    2341             :                 "cli-server",
    2342             :                 "cgi-fcgi",
    2343             :                 "fpm-fcgi",
    2344             :                 "isapi",
    2345             :                 "apache2filter",
    2346             :                 "apache2handler",
    2347             :                 "litespeed",
    2348             :                 NULL
    2349             :         };
    2350             :         const char **sapi_name;
    2351             : 
    2352       20924 :         if (sapi_module.name) {
    2353      207032 :                 for (sapi_name = supported_sapis; *sapi_name; sapi_name++) {
    2354      186476 :                         if (strcmp(sapi_module.name, *sapi_name) == 0) {
    2355         368 :                                 return SUCCESS;
    2356             :                         }
    2357             :                 }
    2358       20562 :                 if (ZCG(accel_directives).enable_cli && (
    2359           6 :                     strcmp(sapi_module.name, "cli") == 0
    2360           6 :                   || strcmp(sapi_module.name, "phpdbg") == 0)) {
    2361           6 :                         return SUCCESS;
    2362             :                 }
    2363             :         }
    2364             : 
    2365       20550 :         return FAILURE;
    2366             : }
    2367             : 
    2368         374 : static int zend_accel_init_shm(void)
    2369             : {
    2370         374 :         zend_shared_alloc_lock();
    2371             : 
    2372         374 :         accel_shared_globals = zend_shared_alloc(sizeof(zend_accel_shared_globals));
    2373         374 :         if (!accel_shared_globals) {
    2374           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
    2375           0 :                 return FAILURE;
    2376             :         }
    2377         374 :         ZSMMG(app_shared_globals) = accel_shared_globals;
    2378             : 
    2379         374 :         zend_accel_hash_init(&ZCSG(hash), ZCG(accel_directives).max_accelerated_files);
    2380             : 
    2381         374 :         ZCSG(interned_strings_start) = ZCSG(interned_strings_end) = NULL;
    2382             : # ifndef ZTS
    2383         374 :         zend_hash_init(&ZCSG(interned_strings), (ZCG(accel_directives).interned_strings_buffer * 1024 * 1024) / (sizeof(Bucket) + sizeof(Bucket*) + 8 /* average string length */), NULL, NULL, 1);
    2384         374 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2385             :                 void *data;
    2386             : 
    2387         374 :                 ZCSG(interned_strings).nTableMask = -ZCSG(interned_strings).nTableSize;
    2388         374 :                 data = zend_shared_alloc(HT_SIZE(&ZCSG(interned_strings)));
    2389         374 :                 ZCSG(interned_strings_start) = zend_shared_alloc((ZCG(accel_directives).interned_strings_buffer * 1024 * 1024));
    2390         374 :                 if (!data || !ZCSG(interned_strings_start)) {
    2391           0 :                         zend_accel_error(ACCEL_LOG_FATAL, ACCELERATOR_PRODUCT_NAME " cannot allocate buffer for interned strings");
    2392           0 :                         return FAILURE;
    2393             :                 }
    2394         374 :                 HT_SET_DATA_ADDR(&ZCSG(interned_strings), data);
    2395         374 :                 HT_HASH_RESET(&ZCSG(interned_strings));
    2396         374 :                 ZCSG(interned_strings_end)   = ZCSG(interned_strings_start) + (ZCG(accel_directives).interned_strings_buffer * 1024 * 1024);
    2397         374 :                 ZCSG(interned_strings_top)   = ZCSG(interned_strings_start);
    2398             : 
    2399             : //              orig_interned_strings_start = CG(interned_strings_start);
    2400             : //              orig_interned_strings_end = CG(interned_strings_end);
    2401             : //              CG(interned_strings_start) = ZCSG(interned_strings_start);
    2402             : //              CG(interned_strings_end) = ZCSG(interned_strings_end);
    2403             :         }
    2404             : # endif
    2405             : 
    2406         374 :         orig_new_interned_string = zend_new_interned_string;
    2407         374 :         orig_interned_strings_snapshot = zend_interned_strings_snapshot;
    2408         374 :         orig_interned_strings_restore = zend_interned_strings_restore;
    2409         374 :         zend_new_interned_string = accel_new_interned_string_for_php;
    2410         374 :         zend_interned_strings_snapshot = accel_interned_strings_snapshot_for_php;
    2411         374 :         zend_interned_strings_restore = accel_interned_strings_restore_for_php;
    2412             : 
    2413             : # ifndef ZTS
    2414         374 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2415         374 :                 accel_use_shm_interned_strings();
    2416         374 :                 accel_interned_strings_save_state();
    2417             :         }
    2418             : # endif
    2419             : 
    2420         374 :         zend_reset_cache_vars();
    2421             : 
    2422         374 :         ZCSG(oom_restarts) = 0;
    2423         374 :         ZCSG(hash_restarts) = 0;
    2424         374 :         ZCSG(manual_restarts) = 0;
    2425             : 
    2426         374 :         ZCSG(accelerator_enabled) = 1;
    2427         374 :         ZCSG(start_time) = zend_accel_get_time();
    2428         374 :         ZCSG(last_restart_time) = 0;
    2429         374 :         ZCSG(restart_in_progress) = 0;
    2430             : 
    2431         374 :         zend_shared_alloc_unlock();
    2432             : 
    2433         374 :         return SUCCESS;
    2434             : }
    2435             : 
    2436       20924 : static void accel_globals_ctor(zend_accel_globals *accel_globals)
    2437             : {
    2438             : #if defined(COMPILE_DL_OPCACHE) && defined(ZTS)
    2439             :         ZEND_TSRMLS_CACHE_UPDATE();
    2440             : #endif
    2441       20924 :         memset(accel_globals, 0, sizeof(zend_accel_globals));
    2442       20924 : }
    2443             : 
    2444      775449 : static void accel_globals_internal_func_dtor(zval *zv)
    2445             : {
    2446      775449 :         free(Z_PTR_P(zv));
    2447      775449 : }
    2448             : 
    2449       20922 : static void accel_globals_dtor(zend_accel_globals *accel_globals)
    2450             : {
    2451       20922 :         if (accel_globals->function_table.nTableSize) {
    2452         374 :                 accel_globals->function_table.pDestructor = accel_globals_internal_func_dtor;
    2453         374 :                 zend_hash_destroy(&accel_globals->function_table);
    2454             :         }
    2455       20922 : }
    2456             : 
    2457             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2458             : 
    2459             : #define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_CHAR) ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
    2460             : 
    2461             : static void accel_gen_system_id(void)
    2462             : {
    2463             :         PHP_MD5_CTX context;
    2464             :         unsigned char digest[16], c;
    2465             :         char *md5str = ZCG(system_id);
    2466             :         int i;
    2467             : 
    2468             :         PHP_MD5Init(&context);
    2469             :         PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
    2470             :         PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
    2471             :         PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
    2472             :         if (strstr(PHP_VERSION, "-dev") != 0) {
    2473             :                 /* Development versions may be changed from build to build */
    2474             :                 PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
    2475             :                 PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
    2476             :         }
    2477             :         PHP_MD5Final(digest, &context);
    2478             :         for (i = 0; i < 16; i++) {
    2479             :                 c = digest[i] >> 4;
    2480             :                 c = (c <= 9) ? c + '0' : c - 10 + 'a';
    2481             :                 md5str[i * 2] = c;
    2482             :                 c = digest[i] &  0x0f;
    2483             :                 c = (c <= 9) ? c + '0' : c - 10 + 'a';
    2484             :                 md5str[(i * 2) + 1] = c;
    2485             :         }
    2486             : }
    2487             : #endif
    2488             : 
    2489       20924 : static int accel_startup(zend_extension *extension)
    2490             : {
    2491             :         zend_function *func;
    2492             :         zend_ini_entry *ini_entry;
    2493             : 
    2494             : #ifdef ZTS
    2495             :         accel_globals_id = ts_allocate_id(&accel_globals_id, sizeof(zend_accel_globals), (ts_allocate_ctor) accel_globals_ctor, (ts_allocate_dtor) accel_globals_dtor);
    2496             : #else
    2497       20924 :         accel_globals_ctor(&accel_globals);
    2498             : #endif
    2499             : 
    2500             : #ifdef ZEND_WIN32
    2501             :         _setmaxstdio(2048); /* The default configuration is limited to 512 stdio files */
    2502             : #endif
    2503             : 
    2504       20924 :         if (start_accel_module() == FAILURE) {
    2505           0 :                 accel_startup_ok = 0;
    2506           0 :                 zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME ": module registration failed!");
    2507           0 :                 return FAILURE;
    2508             :         }
    2509             : 
    2510             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2511             :         accel_gen_system_id();
    2512             : #endif
    2513             : 
    2514             :         /* no supported SAPI found - disable acceleration and stop initialization */
    2515       20924 :         if (accel_find_sapi() == FAILURE) {
    2516       20550 :                 accel_startup_ok = 0;
    2517       61650 :                 if (!ZCG(accel_directives).enable_cli &&
    2518       20550 :                     strcmp(sapi_module.name, "cli") == 0) {
    2519       20550 :                         zps_startup_failure("Opcode Caching is disabled for CLI", NULL, accelerator_remove_cb);
    2520             :                 } else {
    2521           0 :                         zps_startup_failure("Opcode Caching is only supported in Apache, ISAPI, FPM, FastCGI and LiteSpeed SAPIs", NULL, accelerator_remove_cb);
    2522             :                 }
    2523       20550 :                 return SUCCESS;
    2524             :         }
    2525             : 
    2526         374 :         if (ZCG(enabled) == 0) {
    2527           0 :                 return SUCCESS ;
    2528             :         }
    2529             : 
    2530             : /********************************************/
    2531             : /* End of non-SHM dependent initializations */
    2532             : /********************************************/
    2533             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2534             :         if (!ZCG(accel_directives).file_cache_only) {
    2535             : #else
    2536             :         if (1) {
    2537             : #endif
    2538         374 :                 switch (zend_shared_alloc_startup(ZCG(accel_directives).memory_consumption)) {
    2539             :                         case ALLOC_SUCCESS:
    2540         374 :                                 if (zend_accel_init_shm() == FAILURE) {
    2541           0 :                                         accel_startup_ok = 0;
    2542           0 :                                         return FAILURE;
    2543             :                                 }
    2544         374 :                                 break;
    2545             :                         case ALLOC_FAILURE:
    2546           0 :                                 accel_startup_ok = 0;
    2547           0 :                                 zend_accel_error(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - probably not enough shared memory.");
    2548           0 :                                 return SUCCESS;
    2549             :                         case SUCCESSFULLY_REATTACHED:
    2550           0 :                                 accel_shared_globals = (zend_accel_shared_globals *) ZSMMG(app_shared_globals);
    2551           0 :                                 zend_shared_alloc_lock();
    2552           0 :                                 orig_new_interned_string = zend_new_interned_string;
    2553           0 :                                 orig_interned_strings_snapshot = zend_interned_strings_snapshot;
    2554           0 :                                 orig_interned_strings_restore = zend_interned_strings_restore;
    2555             : 
    2556           0 :                                 zend_new_interned_string = accel_new_interned_string_for_php;
    2557           0 :                                 zend_interned_strings_snapshot = accel_interned_strings_snapshot_for_php;
    2558           0 :                                 zend_interned_strings_restore = accel_interned_strings_restore_for_php;
    2559             : #ifndef ZTS
    2560           0 :                                 accel_use_shm_interned_strings();
    2561             : #endif
    2562           0 :                                 zend_shared_alloc_unlock();
    2563           0 :                                 break;
    2564             :                         case FAILED_REATTACHED:
    2565           0 :                                 accel_startup_ok = 0;
    2566           0 :                                 zend_accel_error(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - can not reattach to exiting shared memory.");
    2567           0 :                                 return SUCCESS;
    2568             :                                 break;
    2569             :                 }
    2570             : 
    2571             :                 /* from this point further, shared memory is supposed to be OK */
    2572             : 
    2573             :                 /* remeber the last restart time in the process memory */
    2574         374 :                 ZCG(last_restart_time) = ZCSG(last_restart_time);
    2575             : 
    2576             :                 /* Init auto-global strings */
    2577         374 :                 zend_accel_init_auto_globals();
    2578             : 
    2579         374 :                 zend_shared_alloc_lock();
    2580         374 :                 zend_shared_alloc_save_state();
    2581         374 :                 zend_shared_alloc_unlock();
    2582             : 
    2583         374 :                 SHM_PROTECT();
    2584             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2585             :         } else if (!ZCG(accel_directives).file_cache) {
    2586             :                 accel_startup_ok = 0;
    2587             :                 zend_accel_error(ACCEL_LOG_FATAL, "opcache.file_cache_only is set without a proper setting of opcache.file_cache");
    2588             :                 return SUCCESS;
    2589             :         } else {
    2590             :                 accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));
    2591             : 
    2592             :                 /* Init auto-global strings */
    2593             :                 zend_accel_init_auto_globals();
    2594             : #endif
    2595             :         }
    2596             : 
    2597             :         /* Override compiler */
    2598         374 :         accelerator_orig_compile_file = zend_compile_file;
    2599         374 :         zend_compile_file = persistent_compile_file;
    2600             : 
    2601             :         /* Override stream opener function (to eliminate open() call caused by
    2602             :          * include/require statements ) */
    2603         374 :         accelerator_orig_zend_stream_open_function = zend_stream_open_function;
    2604         374 :         zend_stream_open_function = persistent_stream_open_function;
    2605             : 
    2606             :         /* Override path resolver function (to eliminate stat() calls caused by
    2607             :          * include_once/require_once statements */
    2608         374 :         accelerator_orig_zend_resolve_path = zend_resolve_path;
    2609         374 :         zend_resolve_path = persistent_zend_resolve_path;
    2610             : 
    2611             :         /* Override chdir() function */
    2612        1122 :         if ((func = zend_hash_str_find_ptr(CG(function_table), "chdir", sizeof("chdir")-1)) != NULL &&
    2613         374 :             func->type == ZEND_INTERNAL_FUNCTION) {
    2614         374 :                 orig_chdir = func->internal_function.handler;
    2615         374 :                 func->internal_function.handler = ZEND_FN(accel_chdir);
    2616             :         }
    2617         374 :         ZCG(cwd) = NULL;
    2618         374 :         ZCG(include_path) = NULL;
    2619             : 
    2620             :         /* Override "include_path" modifier callback */
    2621         748 :         if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), "include_path", sizeof("include_path")-1)) != NULL) {
    2622         374 :                 ZCG(include_path) = ini_entry->value;
    2623         374 :                 orig_include_path_on_modify = ini_entry->on_modify;
    2624         374 :                 ini_entry->on_modify = accel_include_path_on_modify;
    2625             :         }
    2626             : 
    2627         374 :         accel_startup_ok = 1;
    2628             : 
    2629             :         /* Override file_exists(), is_file() and is_readable() */
    2630         374 :         zend_accel_override_file_functions();
    2631             : 
    2632             :         /* Load black list */
    2633         374 :         accel_blacklist.entries = NULL;
    2634        1122 :         if (ZCG(enabled) && accel_startup_ok &&
    2635         374 :             ZCG(accel_directives).user_blacklist_filename &&
    2636         374 :             *ZCG(accel_directives.user_blacklist_filename)) {
    2637           0 :                 zend_accel_blacklist_init(&accel_blacklist);
    2638           0 :                 zend_accel_blacklist_load(&accel_blacklist, ZCG(accel_directives.user_blacklist_filename));
    2639             :         }
    2640             : 
    2641         374 :         return SUCCESS;
    2642             : }
    2643             : 
    2644       20922 : static void accel_free_ts_resources()
    2645             : {
    2646             : #ifndef ZTS
    2647       20922 :         accel_globals_dtor(&accel_globals);
    2648             : #else
    2649             :         ts_free_id(accel_globals_id);
    2650             : #endif
    2651       20922 : }
    2652             : 
    2653       20922 : void accel_shutdown(void)
    2654             : {
    2655             :         zend_ini_entry *ini_entry;
    2656       20922 :         zend_bool file_cache_only = 0;
    2657             : 
    2658       20922 :         zend_accel_blacklist_shutdown(&accel_blacklist);
    2659             : 
    2660       20922 :         if (!ZCG(enabled) || !accel_startup_ok) {
    2661       20548 :                 accel_free_ts_resources();
    2662       20548 :                 return;
    2663             :         }
    2664             : 
    2665         374 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2666             : #ifndef ZTS
    2667         374 :                 zend_hash_clean(CG(auto_globals));
    2668         374 :                 zend_hash_clean(CG(function_table));
    2669         374 :                 zend_hash_clean(CG(class_table));
    2670         374 :                 zend_hash_clean(EG(zend_constants));
    2671             : #endif
    2672             :         }
    2673             : 
    2674         374 :         accel_reset_pcre_cache();
    2675             : 
    2676         374 :         zend_new_interned_string = orig_new_interned_string;
    2677         374 :         zend_interned_strings_snapshot = orig_interned_strings_snapshot;
    2678         374 :         zend_interned_strings_restore = orig_interned_strings_restore;
    2679             : 
    2680             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2681             :         file_cache_only = ZCG(accel_directives).file_cache_only;
    2682             : #endif
    2683             : 
    2684         374 :         accel_free_ts_resources();
    2685             : 
    2686         374 :         if (!file_cache_only) {
    2687         374 :                 zend_shared_alloc_shutdown();
    2688             :         }
    2689         374 :         zend_compile_file = accelerator_orig_compile_file;
    2690             : 
    2691         748 :         if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), "include_path", sizeof("include_path")-1)) != NULL) {
    2692         374 :                 ini_entry->on_modify = orig_include_path_on_modify;
    2693             :         }
    2694             : }
    2695             : 
    2696           0 : void zend_accel_schedule_restart(zend_accel_restart_reason reason)
    2697             : {
    2698           0 :         if (ZCSG(restart_pending)) {
    2699             :                 /* don't schedule twice */
    2700           0 :                 return;
    2701             :         }
    2702           0 :         zend_accel_error(ACCEL_LOG_DEBUG, "Restart Scheduled!");
    2703             : 
    2704           0 :         SHM_UNPROTECT();
    2705           0 :         ZCSG(restart_pending) = 1;
    2706           0 :         ZCSG(restart_reason) = reason;
    2707           0 :         ZCSG(cache_status_before_restart) = ZCSG(accelerator_enabled);
    2708           0 :         ZCSG(accelerator_enabled) = 0;
    2709             : 
    2710           0 :         if (ZCG(accel_directives).force_restart_timeout) {
    2711           0 :                 ZCSG(force_restart_time) = zend_accel_get_time() + ZCG(accel_directives).force_restart_timeout;
    2712             :         } else {
    2713           0 :                 ZCSG(force_restart_time) = 0;
    2714             :         }
    2715           0 :         SHM_PROTECT();
    2716             : }
    2717             : 
    2718             : /* this is needed because on WIN32 lock is not decreased unless ZCG(counted) is set */
    2719             : #ifdef ZEND_WIN32
    2720             : #define accel_deactivate_now() ZCG(counted) = 1; accel_deactivate_sub()
    2721             : #else
    2722             : #define accel_deactivate_now() accel_deactivate_sub()
    2723             : #endif
    2724             : 
    2725             : /* ensures it is OK to read SHM
    2726             :         if it's not OK (restart in progress) returns FAILURE
    2727             :         if OK returns SUCCESS
    2728             :         MUST call accelerator_shm_read_unlock after done lock operations
    2729             : */
    2730           0 : int accelerator_shm_read_lock(void)
    2731             : {
    2732           0 :         if (ZCG(counted)) {
    2733             :                 /* counted means we are holding read lock for SHM, so that nothing bad can happen */
    2734           0 :                 return SUCCESS;
    2735             :         } else {
    2736             :                 /* here accelerator is active but we do not hold SHM lock. This means restart was scheduled
    2737             :                         or is in progress now */
    2738           0 :                 accel_activate_add(); /* acquire usage lock */
    2739             :                 /* Now if we weren't inside restart, restart would not begin until we remove usage lock */
    2740           0 :                 if (ZCSG(restart_in_progress)) {
    2741             :                         /* we already were inside restart this means it's not safe to touch shm */
    2742           0 :                         accel_deactivate_now(); /* drop usage lock */
    2743           0 :                         return FAILURE;
    2744             :                 }
    2745             :         }
    2746           0 :         return SUCCESS;
    2747             : }
    2748             : 
    2749             : /* must be called ONLY after SUCCESSFUL accelerator_shm_read_lock */
    2750           0 : void accelerator_shm_read_unlock(void)
    2751             : {
    2752           0 :         if (!ZCG(counted)) {
    2753             :                 /* counted is 0 - meaning we had to readlock manually, release readlock now */
    2754           0 :                 accel_deactivate_now();
    2755             :         }
    2756           0 : }
    2757             : 
    2758             : ZEND_EXT_API zend_extension zend_extension_entry = {
    2759             :         ACCELERATOR_PRODUCT_NAME,               /* name */
    2760             :         ACCELERATOR_VERSION,                                    /* version */
    2761             :         "Zend Technologies",                                  /* author */
    2762             :         "http://www.zend.com/",                                       /* URL */
    2763             :         "Copyright (c) 1999-2015",                            /* copyright */
    2764             :         accel_startup,                                                  /* startup */
    2765             :         NULL,                                                                   /* shutdown */
    2766             :         accel_activate,                                                 /* per-script activation */
    2767             :         accel_deactivate,                                               /* per-script deactivation */
    2768             :         NULL,                                                                   /* message handler */
    2769             :         NULL,                                                                   /* op_array handler */
    2770             :         NULL,                                                                   /* extended statement handler */
    2771             :         NULL,                                                                   /* extended fcall begin handler */
    2772             :         NULL,                                                                   /* extended fcall end handler */
    2773             :         NULL,                                                                   /* op_array ctor */
    2774             :         NULL,                                                                   /* op_array dtor */
    2775             :         STANDARD_ZEND_EXTENSION_PROPERTIES
    2776             : };

Generated by: LCOV version 1.10

Generated at Tue, 04 Aug 2015 09:11:07 +0000 (4 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.