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: 638 1353 47.2 %
Date: 2016-05-26 Functions: 39 65 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 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             : #include "ext/standard/md5.h"
      44             : 
      45             : #ifdef HAVE_OPCACHE_FILE_CACHE
      46             : # include "zend_file_cache.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             : #if ENABLE_FILE_CACHE_FALLBACK
     112             : zend_bool fallback_process = 0; /* process uses file cache fallback */
     113             : #endif
     114             : 
     115             : static zend_op_array *(*accelerator_orig_compile_file)(zend_file_handle *file_handle, int type);
     116             : static int (*accelerator_orig_zend_stream_open_function)(const char *filename, zend_file_handle *handle );
     117             : static zend_string *(*accelerator_orig_zend_resolve_path)(const char *filename, int filename_len);
     118             : static void (*orig_chdir)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
     119             : static ZEND_INI_MH((*orig_include_path_on_modify)) = NULL;
     120             : 
     121             : #ifdef ZEND_WIN32
     122             : # define INCREMENT(v) InterlockedIncrement64(&ZCSG(v))
     123             : # define DECREMENT(v) InterlockedDecrement64(&ZCSG(v))
     124             : # define LOCKVAL(v)   (ZCSG(v))
     125             : #endif
     126             : 
     127             : #ifdef ZEND_WIN32
     128             : static time_t zend_accel_get_time(void)
     129             : {
     130             :         FILETIME now;
     131             :         GetSystemTimeAsFileTime(&now);
     132             : 
     133             :         return (time_t) ((((((__int64)now.dwHighDateTime) << 32)|now.dwLowDateTime) - 116444736000000000L)/10000000);
     134             : }
     135             : #else
     136             : # define zend_accel_get_time() time(NULL)
     137             : #endif
     138             : 
     139         449 : static inline int is_stream_path(const char *filename)
     140             : {
     141             :         const char *p;
     142             : 
     143        3959 :         for (p = filename;
     144        1092 :              (*p >= 'a' && *p <= 'z') ||
     145         478 :              (*p >= 'A' && *p <= 'Z') ||
     146         506 :              (*p >= '0' && *p <= '9') ||
     147        1434 :              *p == '+' || *p == '-' || *p == '.';
     148         336 :              p++);
     149         449 :         return ((p != filename) && (p[0] == ':') && (p[1] == '/') && (p[2] == '/'));
     150             : }
     151             : 
     152          28 : static inline int is_cacheable_stream_path(const char *filename)
     153             : {
     154          56 :         return memcmp(filename, "file://", sizeof("file://") - 1) == 0 ||
     155          28 :                memcmp(filename, "phar://", sizeof("phar://") - 1) == 0;
     156             : }
     157             : 
     158             : /* O+ overrides PHP chdir() function and remembers the current working directory
     159             :  * in ZCG(cwd) and ZCG(cwd_len). Later accel_getcwd() can use stored value and
     160             :  * avoid getcwd() call.
     161             :  */
     162           0 : static ZEND_FUNCTION(accel_chdir)
     163             : {
     164             :         char cwd[MAXPATHLEN];
     165             : 
     166           0 :         orig_chdir(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     167           0 :         if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
     168           0 :                 if (ZCG(cwd)) {
     169           0 :                         zend_string_release(ZCG(cwd));
     170             :                 }
     171           0 :                 ZCG(cwd) = zend_string_init(cwd, strlen(cwd), 0);
     172             :         } else {
     173           0 :                 if (ZCG(cwd)) {
     174           0 :                         zend_string_release(ZCG(cwd));
     175           0 :                         ZCG(cwd) = NULL;
     176             :                 }
     177             :         }
     178           0 :         ZCG(cwd_key_len) = 0;
     179           0 :         ZCG(cwd_check) = 1;
     180           0 : }
     181             : 
     182          25 : static inline zend_string* accel_getcwd(void)
     183             : {
     184          25 :         if (ZCG(cwd)) {
     185           0 :                 return ZCG(cwd);
     186             :         } else {
     187             :                 char cwd[MAXPATHLEN + 1];
     188             : 
     189          25 :                 if (!VCWD_GETCWD(cwd, MAXPATHLEN)) {
     190           0 :                         return NULL;
     191             :                 }
     192          50 :                 ZCG(cwd) = zend_string_init(cwd, strlen(cwd), 0);
     193          25 :                 ZCG(cwd_key_len) = 0;
     194          25 :                 ZCG(cwd_check) = 1;
     195          25 :                 return ZCG(cwd);
     196             :         }
     197             : }
     198             : 
     199           0 : void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason)
     200             : {
     201           0 :         if ((((double) ZSMMG(wasted_shared_memory)) / ZCG(accel_directives).memory_consumption) >= ZCG(accel_directives).max_wasted_percentage) {
     202           0 :                 zend_accel_schedule_restart(reason);
     203             :         }
     204           0 : }
     205             : 
     206             : /* O+ tracks changes of "include_path" directive. It stores all the requested
     207             :  * values in ZCG(include_paths) shared hash table, current value in
     208             :  * ZCG(include_path)/ZCG(include_path_len) and one letter "path key" in
     209             :  * ZCG(include_path_key).
     210             :  */
     211           6 : static ZEND_INI_MH(accel_include_path_on_modify)
     212             : {
     213           6 :         int ret = orig_include_path_on_modify(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
     214             : 
     215           6 :         if (ret == SUCCESS) {
     216           6 :                 ZCG(include_path) = new_value;
     217           6 :                 ZCG(include_path_key_len) = 0;
     218           6 :                 ZCG(include_path_check) = 1;
     219             :         }
     220           6 :         return ret;
     221             : }
     222             : 
     223             : /* Interned strings support */
     224             : static zend_string *(*orig_new_interned_string)(zend_string *str);
     225             : static void (*orig_interned_strings_snapshot)(void);
     226             : static void (*orig_interned_strings_restore)(void);
     227             : 
     228             : /* O+ disables creation of interned strings by regular PHP compiler, instead,
     229             :  * it creates interned strings in shared memory when saves a script.
     230             :  * Such interned strings are shared across all PHP processes
     231             :  */
     232        4557 : static zend_string *accel_new_interned_string_for_php(zend_string *str)
     233             : {
     234        4557 :         return str;
     235             : }
     236             : 
     237         378 : static void accel_interned_strings_snapshot_for_php(void)
     238             : {
     239         378 : }
     240             : 
     241         378 : static void accel_interned_strings_restore_for_php(void)
     242             : {
     243         378 : }
     244             : 
     245             : #ifndef ZTS
     246           0 : static void accel_interned_strings_restore_state(void)
     247             : {
     248           0 :     uint idx = ZCSG(interned_strings).nNumUsed;
     249             :     uint nIndex;
     250             :     Bucket *p;
     251             : 
     252           0 :         memset(ZCSG(interned_strings_saved_top),
     253           0 :                         0, ZCSG(interned_strings_top) - ZCSG(interned_strings_saved_top));
     254           0 :         ZCSG(interned_strings_top) = ZCSG(interned_strings_saved_top);
     255           0 :     while (idx > 0) {
     256           0 :         idx--;
     257           0 :                 p = ZCSG(interned_strings).arData + idx;
     258           0 :                 if ((char*)p->key < ZCSG(interned_strings_top)) break;
     259           0 :                 ZCSG(interned_strings).nNumUsed--;
     260           0 :                 ZCSG(interned_strings).nNumOfElements--;
     261             : 
     262           0 :                 nIndex = p->h | ZCSG(interned_strings).nTableMask;
     263           0 :                 if (HT_HASH(&ZCSG(interned_strings), nIndex) == HT_IDX_TO_HASH(idx)) {
     264           0 :                         HT_HASH(&ZCSG(interned_strings), nIndex) = Z_NEXT(p->val);
     265             :                 } else {
     266           0 :                         uint32_t prev = HT_HASH(&ZCSG(interned_strings), nIndex);
     267           0 :                         while (Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val) != idx) {
     268           0 :                                 prev = Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val);
     269             :                         }
     270           0 :                         Z_NEXT(HT_HASH_TO_BUCKET(&ZCSG(interned_strings), prev)->val) = Z_NEXT(p->val);
     271             :                 }
     272             :         }
     273           0 : }
     274             : 
     275         378 : static void accel_interned_strings_save_state(void)
     276             : {
     277         378 :         ZCSG(interned_strings_saved_top) = ZCSG(interned_strings_top);
     278         378 : }
     279             : #endif
     280             : 
     281             : #ifndef ZTS
     282          50 : static zend_string *accel_find_interned_string(zend_string *str)
     283             : {
     284             : /* for now interned strings are supported only for non-ZTS build */
     285             :         zend_ulong h;
     286             :         uint nIndex;
     287             :         uint idx;
     288             :         Bucket *arData, *p;
     289             : 
     290          50 :         if (IS_ACCEL_INTERNED(str)) {
     291             :                 /* this is already an interned string */
     292           2 :                 return str;
     293             :         }
     294             : 
     295          48 :         h = zend_string_hash_val(str);
     296          48 :         nIndex = h | ZCSG(interned_strings).nTableMask;
     297             : 
     298             :         /* check for existing interned string */
     299          48 :         idx = HT_HASH(&ZCSG(interned_strings), nIndex);
     300          48 :         arData = ZCSG(interned_strings).arData;
     301          96 :         while (idx != HT_INVALID_IDX) {
     302           0 :                 p = HT_HASH_TO_BUCKET_EX(arData, idx);
     303           0 :                 if ((p->h == h) && (ZSTR_LEN(p->key) == ZSTR_LEN(str))) {
     304           0 :                         if (!memcmp(ZSTR_VAL(p->key), ZSTR_VAL(str), ZSTR_LEN(str))) {
     305           0 :                                 return p->key;
     306             :                         }
     307             :                 }
     308           0 :                 idx = Z_NEXT(p->val);
     309             :         }
     310             : 
     311          48 :         return NULL;
     312             : }
     313             : #endif
     314             : 
     315     4366046 : zend_string *accel_new_interned_string(zend_string *str)
     316             : {
     317             : /* for now interned strings are supported only for non-ZTS build */
     318             : #ifndef ZTS
     319             :         zend_ulong h;
     320             :         uint nIndex;
     321             :         uint idx;
     322             :         Bucket *p;
     323             : 
     324             : #ifdef HAVE_OPCACHE_FILE_CACHE
     325     4366046 :         if (ZCG(accel_directives).file_cache_only) {
     326           0 :                 return str;
     327             :         }
     328             : #endif
     329             : 
     330     4366046 :         if (IS_ACCEL_INTERNED(str)) {
     331             :                 /* this is already an interned string */
     332           0 :                 return str;
     333             :         }
     334             : 
     335     4366046 :         h = zend_string_hash_val(str);
     336     4366046 :         nIndex = h | ZCSG(interned_strings).nTableMask;
     337             : 
     338             :         /* check for existing interned string */
     339     4366046 :         idx = HT_HASH(&ZCSG(interned_strings), nIndex);
     340     8784711 :         while (idx != HT_INVALID_IDX) {
     341     2707139 :                 p = HT_HASH_TO_BUCKET(&ZCSG(interned_strings), idx);
     342     2707139 :                 if ((p->h == h) && (ZSTR_LEN(p->key) == ZSTR_LEN(str))) {
     343     2654520 :                         if (!memcmp(ZSTR_VAL(p->key), ZSTR_VAL(str), ZSTR_LEN(str))) {
     344             :                                 zend_string_release(str);
     345     2654520 :                                 return p->key;
     346             :                         }
     347             :                 }
     348       52619 :                 idx = Z_NEXT(p->val);
     349             :         }
     350             : 
     351     3423052 :         if (ZCSG(interned_strings_top) + ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(ZSTR_LEN(str))) >=
     352     1711526 :             ZCSG(interned_strings_end)) {
     353             :             /* no memory, return the same non-interned string */
     354           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Interned string buffer overflow");
     355           0 :                 return str;
     356             :         }
     357             : 
     358             :         /* create new interning string in shared interned strings buffer */
     359             : 
     360     1711526 :         idx = ZCSG(interned_strings).nNumUsed++;
     361     1711526 :         ZCSG(interned_strings).nNumOfElements++;
     362     1711526 :         p = ZCSG(interned_strings).arData + idx;
     363     1711526 :         p->key = (zend_string*) ZCSG(interned_strings_top);
     364     1711526 :         ZCSG(interned_strings_top) += ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(ZSTR_LEN(str)));
     365     1711526 :         p->h = h;
     366     1711526 :         GC_REFCOUNT(p->key) = 1;
     367             : #if 1
     368             :         /* optimized single assignment */
     369     1711526 :         GC_TYPE_INFO(p->key) = IS_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << 8);
     370             : #else
     371             :         GC_TYPE(p->key) = IS_STRING;
     372             :         GC_FLAGS(p->key) = IS_STR_INTERNED | IS_STR_PERMANENT;
     373             : #endif
     374     1711526 :         ZSTR_H(p->key) = ZSTR_H(str);
     375     1711526 :         ZSTR_LEN(p->key) = ZSTR_LEN(str);
     376     1711526 :         memcpy(ZSTR_VAL(p->key), ZSTR_VAL(str), ZSTR_LEN(str));
     377     1711526 :         ZVAL_INTERNED_STR(&p->val, p->key);
     378     1711526 :         Z_NEXT(p->val) = HT_HASH(&ZCSG(interned_strings), nIndex);
     379     1711526 :         HT_HASH(&ZCSG(interned_strings), nIndex) = HT_IDX_TO_HASH(idx);
     380             :         zend_string_release(str);
     381     1711526 :         return p->key;
     382             : #else
     383             :         return str;
     384             : #endif
     385             : }
     386             : 
     387             : #ifndef ZTS
     388             : /* Copy PHP interned strings from PHP process memory into the shared memory */
     389         378 : static void accel_use_shm_interned_strings(void)
     390             : {
     391             :         uint idx, j;
     392             :         Bucket *p, *q;
     393             : 
     394             :         /* empty string */
     395         378 :         CG(empty_string) = accel_new_interned_string(CG(empty_string));
     396       97146 :         for (j = 0; j < 256; j++) {
     397             :                 char s[2];
     398       96768 :                 s[0] = j;
     399       96768 :                 s[1] = 0;
     400      193536 :                 CG(one_char_string)[j] = accel_new_interned_string(zend_string_init(s, 1, 0));
     401             :         }
     402             : 
     403             :         /* function table hash keys */
     404      831198 :         for (idx = 0; idx < CG(function_table)->nNumUsed; idx++) {
     405      830820 :                 p = CG(function_table)->arData + idx;
     406     1661640 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     407      830820 :                 if (p->key) {
     408      830820 :                         p->key = accel_new_interned_string(p->key);
     409             :                 }
     410      830820 :                 if (Z_FUNC(p->val)->common.function_name) {
     411      830820 :                         Z_FUNC(p->val)->common.function_name = accel_new_interned_string(Z_FUNC(p->val)->common.function_name);
     412             :                 }
     413             :         }
     414             : 
     415             :         /* class table hash keys, class names, properties, methods, constants, etc */
     416       73332 :         for (idx = 0; idx < CG(class_table)->nNumUsed; idx++) {
     417             :                 zend_class_entry *ce;
     418             : 
     419       72954 :                 p = CG(class_table)->arData + idx;
     420      145908 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     421       72954 :                 ce = (zend_class_entry*)Z_PTR(p->val);
     422             : 
     423       72954 :                 if (p->key) {
     424       72954 :                         p->key = accel_new_interned_string(p->key);
     425             :                 }
     426             : 
     427       72954 :                 if (ce->name) {
     428       72954 :                         ce->name = accel_new_interned_string(ce->name);
     429             :                 }
     430             : 
     431      178416 :                 for (j = 0; j < ce->properties_info.nNumUsed; j++) {
     432             :                         zend_property_info *info;
     433             : 
     434      105462 :                         q = ce->properties_info.arData + j;
     435      210924 :                         if (Z_TYPE(q->val) == IS_UNDEF) continue;
     436             : 
     437      105084 :                         info = (zend_property_info*)Z_PTR(q->val);
     438             : 
     439      105084 :                         if (q->key) {
     440      105084 :                                 q->key = accel_new_interned_string(q->key);
     441             :                         }
     442             : 
     443      105084 :                         if (info->name) {
     444      105084 :                                 info->name = accel_new_interned_string(info->name);
     445             :                         }
     446             :                 }
     447             : 
     448     1192968 :                 for (j = 0; j < ce->function_table.nNumUsed; j++) {
     449     1120014 :                         q = ce->function_table.arData + j;
     450     2240028 :                         if (Z_TYPE(q->val) == IS_UNDEF) continue;
     451     1120014 :                         if (q->key) {
     452     1120014 :                                 q->key = accel_new_interned_string(q->key);
     453             :                         }
     454     1120014 :                         if (Z_FUNC(q->val)->common.function_name) {
     455     1120014 :                                 Z_FUNC(q->val)->common.function_name = accel_new_interned_string(Z_FUNC(q->val)->common.function_name);
     456             :                         }
     457             :                 }
     458             : 
     459      518616 :                 for (j = 0; j < ce->constants_table.nNumUsed; j++) {
     460      445662 :                         q = ce->constants_table.arData + j;
     461      891324 :                         if (!Z_TYPE(q->val) == IS_UNDEF) continue;
     462           0 :                         if (q->key) {
     463           0 :                                 q->key = accel_new_interned_string(q->key);
     464             :                         }
     465             :                 }
     466             :         }
     467             : 
     468             :         /* constant hash keys */
     469      959364 :         for (idx = 0; idx < EG(zend_constants)->nNumUsed; idx++) {
     470      958986 :                 p = EG(zend_constants)->arData + idx;
     471     1917972 :                 if (!Z_TYPE(p->val) == IS_UNDEF) continue;
     472           0 :                 if (p->key) {
     473           0 :                         p->key = accel_new_interned_string(p->key);
     474             :                 }
     475             :         }
     476             : 
     477             :         /* auto globals hash keys and names */
     478        3780 :         for (idx = 0; idx < CG(auto_globals)->nNumUsed; idx++) {
     479             :                 zend_auto_global *auto_global;
     480             : 
     481        3402 :                 p = CG(auto_globals)->arData + idx;
     482        6804 :                 if (Z_TYPE(p->val) == IS_UNDEF) continue;
     483             : 
     484        3402 :                 auto_global = (zend_auto_global*)Z_PTR(p->val);;
     485             : 
     486        3402 :                 zend_string_addref(auto_global->name);
     487        3402 :                 auto_global->name = accel_new_interned_string(auto_global->name);
     488        3402 :                 if (p->key) {
     489        3402 :                         p->key = accel_new_interned_string(p->key);
     490             :                 }
     491             :         }
     492         378 : }
     493             : #endif
     494             : 
     495           0 : static inline void accel_restart_enter(void)
     496             : {
     497             : #ifdef ZEND_WIN32
     498             :         INCREMENT(restart_in);
     499             : #else
     500             :         static const FLOCK_STRUCTURE(restart_in_progress, F_WRLCK, SEEK_SET, 2, 1);
     501             : 
     502           0 :         if (fcntl(lock_file, F_SETLK, &restart_in_progress) == -1) {
     503           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(+1):  %s (%d)", strerror(errno), errno);
     504             :         }
     505             : #endif
     506           0 :         ZCSG(restart_in_progress) = 1;
     507           0 : }
     508             : 
     509           0 : static inline void accel_restart_leave(void)
     510             : {
     511             : #ifdef ZEND_WIN32
     512             :         ZCSG(restart_in_progress) = 0;
     513             :         DECREMENT(restart_in);
     514             : #else
     515             :         static const FLOCK_STRUCTURE(restart_finished, F_UNLCK, SEEK_SET, 2, 1);
     516             : 
     517           0 :         ZCSG(restart_in_progress) = 0;
     518           0 :         if (fcntl(lock_file, F_SETLK, &restart_finished) == -1) {
     519           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(-1):  %s (%d)", strerror(errno), errno);
     520             :         }
     521             : #endif
     522           0 : }
     523             : 
     524           0 : static inline int accel_restart_is_active(void)
     525             : {
     526           0 :         if (ZCSG(restart_in_progress)) {
     527             : #ifndef ZEND_WIN32
     528           0 :                 FLOCK_STRUCTURE(restart_check, F_WRLCK, SEEK_SET, 2, 1);
     529             : 
     530           0 :                 if (fcntl(lock_file, F_GETLK, &restart_check) == -1) {
     531           0 :                         zend_accel_error(ACCEL_LOG_DEBUG, "RestartC:  %s (%d)", strerror(errno), errno);
     532           0 :                         return FAILURE;
     533             :                 }
     534           0 :                 if (restart_check.l_type == F_UNLCK) {
     535           0 :                         ZCSG(restart_in_progress) = 0;
     536           0 :                         return 0;
     537             :                 } else {
     538           0 :                         return 1;
     539             :                 }
     540             : #else
     541             :                 return LOCKVAL(restart_in) != 0;
     542             : #endif
     543             :         }
     544           0 :         return 0;
     545             : }
     546             : 
     547             : /* Creates a read lock for SHM access */
     548         377 : static inline void accel_activate_add(void)
     549             : {
     550             : #ifdef ZEND_WIN32
     551             :         INCREMENT(mem_usage);
     552             : #else
     553             :         static const FLOCK_STRUCTURE(mem_usage_lock, F_RDLCK, SEEK_SET, 1, 1);
     554             : 
     555         377 :         if (fcntl(lock_file, F_SETLK, &mem_usage_lock) == -1) {
     556           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(+1):  %s (%d)", strerror(errno), errno);
     557             :         }
     558             : #endif
     559         377 : }
     560             : 
     561             : /* Releases a lock for SHM access */
     562           0 : static inline void accel_deactivate_sub(void)
     563             : {
     564             : #ifdef ZEND_WIN32
     565             :         if (ZCG(counted)) {
     566             :                 DECREMENT(mem_usage);
     567             :                 ZCG(counted) = 0;
     568             :         }
     569             : #else
     570             :         static const FLOCK_STRUCTURE(mem_usage_unlock, F_UNLCK, SEEK_SET, 1, 1);
     571             : 
     572           0 :         if (fcntl(lock_file, F_SETLK, &mem_usage_unlock) == -1) {
     573           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(-1):  %s (%d)", strerror(errno), errno);
     574             :         }
     575             : #endif
     576           0 : }
     577             : 
     578         378 : static inline void accel_unlock_all(void)
     579             : {
     580             : #ifdef ZEND_WIN32
     581             :         accel_deactivate_sub();
     582             : #else
     583             :         static const FLOCK_STRUCTURE(mem_usage_unlock_all, F_UNLCK, SEEK_SET, 0, 0);
     584             : 
     585         378 :         if (fcntl(lock_file, F_SETLK, &mem_usage_unlock_all) == -1) {
     586           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UnlockAll:  %s (%d)", strerror(errno), errno);
     587             :         }
     588             : #endif
     589         378 : }
     590             : 
     591             : #ifndef ZEND_WIN32
     592           0 : static inline void kill_all_lockers(struct flock *mem_usage_check)
     593             : {
     594           0 :         int tries = 10;
     595             : 
     596             :         /* so that other process won't try to force while we are busy cleaning up */
     597           0 :         ZCSG(force_restart_time) = 0;
     598           0 :         while (mem_usage_check->l_pid > 0) {
     599           0 :                 while (tries--) {
     600           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "Killed locker %d", mem_usage_check->l_pid);
     601           0 :                         if (kill(mem_usage_check->l_pid, SIGKILL)) {
     602           0 :                                 break;
     603             :                         }
     604             :                         /* give it a chance to die */
     605           0 :                         usleep(20000);
     606           0 :                         if (kill(mem_usage_check->l_pid, 0)) {
     607             :                                 /* can't kill it */
     608           0 :                                 break;
     609             :                         }
     610           0 :                         usleep(10000);
     611             :                 }
     612           0 :                 if (!tries) {
     613           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "Can't kill %d after 20 tries!", mem_usage_check->l_pid);
     614           0 :                         ZCSG(force_restart_time) = time(NULL); /* restore forced restart request */
     615             :                 }
     616             : 
     617           0 :                 mem_usage_check->l_type = F_WRLCK;
     618           0 :                 mem_usage_check->l_whence = SEEK_SET;
     619           0 :                 mem_usage_check->l_start = 1;
     620           0 :                 mem_usage_check->l_len = 1;
     621           0 :                 mem_usage_check->l_pid = -1;
     622           0 :                 if (fcntl(lock_file, F_GETLK, mem_usage_check) == -1) {
     623           0 :                         zend_accel_error(ACCEL_LOG_DEBUG, "KLockers:  %s (%d)", strerror(errno), errno);
     624           0 :                         break;
     625             :                 }
     626             : 
     627           0 :                 if (mem_usage_check->l_type == F_UNLCK || mem_usage_check->l_pid <= 0) {
     628             :                         break;
     629             :                 }
     630             :         }
     631           0 : }
     632             : #endif
     633             : 
     634           0 : static inline int accel_is_inactive(void)
     635             : {
     636             : #ifdef ZEND_WIN32
     637             :         if (LOCKVAL(mem_usage) == 0) {
     638             :                 return SUCCESS;
     639             :         }
     640             : #else
     641           0 :         FLOCK_STRUCTURE(mem_usage_check, F_WRLCK, SEEK_SET, 1, 1);
     642             : 
     643           0 :         mem_usage_check.l_pid = -1;
     644           0 :         if (fcntl(lock_file, F_GETLK, &mem_usage_check) == -1) {
     645           0 :                 zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC:  %s (%d)", strerror(errno), errno);
     646           0 :                 return FAILURE;
     647             :         }
     648           0 :         if (mem_usage_check.l_type == F_UNLCK) {
     649           0 :                 return SUCCESS;
     650             :         }
     651             : 
     652           0 :         if (ZCG(accel_directives).force_restart_timeout
     653           0 :                 && ZCSG(force_restart_time)
     654           0 :                 && time(NULL) >= ZCSG(force_restart_time)) {
     655           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);
     656           0 :                 kill_all_lockers(&mem_usage_check);
     657             : 
     658           0 :                 return FAILURE; /* next request should be able to restart it */
     659             :         }
     660             : #endif
     661             : 
     662           0 :         return FAILURE;
     663             : }
     664             : 
     665           0 : static int zend_get_stream_timestamp(const char *filename, zend_stat_t *statbuf)
     666             : {
     667             :         php_stream_wrapper *wrapper;
     668             :         php_stream_statbuf stream_statbuf;
     669             :         int ret, er;
     670             : 
     671           0 :         if (!filename) {
     672           0 :                 return FAILURE;
     673             :         }
     674             : 
     675           0 :         wrapper = php_stream_locate_url_wrapper(filename, NULL, STREAM_LOCATE_WRAPPERS_ONLY);
     676           0 :         if (!wrapper) {
     677           0 :                 return FAILURE;
     678             :         }
     679           0 :         if (!wrapper->wops || !wrapper->wops->url_stat) {
     680           0 :                 statbuf->st_mtime = 1;
     681           0 :                 return SUCCESS; /* anything other than 0 is considered to be a valid timestamp */
     682             :         }
     683             : 
     684           0 :         er = EG(error_reporting);
     685           0 :         EG(error_reporting) = 0;
     686           0 :         zend_try {
     687           0 :                 ret = wrapper->wops->url_stat(wrapper, (char*)filename, PHP_STREAM_URL_STAT_QUIET, &stream_statbuf, NULL);
     688           0 :         } zend_catch {
     689           0 :                 ret = -1;
     690           0 :         } zend_end_try();
     691           0 :         EG(error_reporting) = er;
     692             : 
     693           0 :         if (ret != 0) {
     694           0 :                 return FAILURE;
     695             :         }
     696             : 
     697           0 :         *statbuf = stream_statbuf.sb;
     698           0 :         return SUCCESS;
     699             : }
     700             : 
     701             : #if ZEND_WIN32
     702             : static accel_time_t zend_get_file_handle_timestamp_win(zend_file_handle *file_handle, size_t *size)
     703             : {
     704             :         static unsigned __int64 utc_base = 0;
     705             :         static FILETIME utc_base_ft;
     706             :         WIN32_FILE_ATTRIBUTE_DATA fdata;
     707             : 
     708             :         if (!file_handle->opened_path) {
     709             :                 return 0;
     710             :         }
     711             : 
     712             :         if (!utc_base) {
     713             :                 SYSTEMTIME st;
     714             : 
     715             :                 st.wYear = 1970;
     716             :                 st.wMonth = 1;
     717             :                 st.wDay = 1;
     718             :                 st.wHour = 0;
     719             :                 st.wMinute = 0;
     720             :                 st.wSecond = 0;
     721             :                 st.wMilliseconds = 0;
     722             : 
     723             :                 SystemTimeToFileTime (&st, &utc_base_ft);
     724             :                 utc_base = (((unsigned __int64)utc_base_ft.dwHighDateTime) << 32) + utc_base_ft.dwLowDateTime;
     725             :     }
     726             : 
     727             :         if (file_handle->opened_path && GetFileAttributesEx(file_handle->opened_path->val, GetFileExInfoStandard, &fdata) != 0) {
     728             :                 unsigned __int64 ftime;
     729             : 
     730             :                 if (CompareFileTime (&fdata.ftLastWriteTime, &utc_base_ft) < 0) {
     731             :                         return 0;
     732             :                 }
     733             : 
     734             :                 ftime = (((unsigned __int64)fdata.ftLastWriteTime.dwHighDateTime) << 32) + fdata.ftLastWriteTime.dwLowDateTime - utc_base;
     735             :                 ftime /= 10000000L;
     736             : 
     737             :                 if (size) {
     738             :                         *size = (size_t)((((unsigned __int64)fdata.nFileSizeHigh) << 32) + (unsigned __int64)fdata.nFileSizeLow);
     739             :                 }
     740             :                 return (accel_time_t)ftime;
     741             :         }
     742             :         return 0;
     743             : }
     744             : #endif
     745             : 
     746         433 : accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size)
     747             : {
     748             :         zend_stat_t statbuf;
     749             : #ifdef ZEND_WIN32
     750             :         accel_time_t res;
     751             : #endif
     752             : 
     753         433 :         if (sapi_module.get_stat &&
     754           0 :             !EG(current_execute_data) &&
     755           0 :             file_handle->filename == SG(request_info).path_translated) {
     756             : 
     757           0 :                 zend_stat_t *tmpbuf = sapi_module.get_stat();
     758             : 
     759           0 :                 if (tmpbuf) {
     760           0 :                         if (size) {
     761           0 :                                 *size = tmpbuf->st_size;
     762             :                         }
     763           0 :                         return tmpbuf->st_mtime;
     764             :                 }
     765             :         }
     766             : 
     767             : #ifdef ZEND_WIN32
     768             :         res = zend_get_file_handle_timestamp_win(file_handle, size);
     769             :         if (res) {
     770             :                 return res;
     771             :         }
     772             : #endif
     773             : 
     774         433 :         switch (file_handle->type) {
     775             :                 case ZEND_HANDLE_FD:
     776           0 :                         if (zend_fstat(file_handle->handle.fd, &statbuf) == -1) {
     777           0 :                                 return 0;
     778             :                         }
     779           0 :                         break;
     780             :                 case ZEND_HANDLE_FP:
     781           6 :                         if (zend_fstat(fileno(file_handle->handle.fp), &statbuf) == -1) {
     782           0 :                                 if (zend_get_stream_timestamp(file_handle->filename, &statbuf) != SUCCESS) {
     783           0 :                                         return 0;
     784             :                                 }
     785             :                         }
     786           6 :                         break;
     787             :                 case ZEND_HANDLE_FILENAME:
     788             :                 case ZEND_HANDLE_MAPPED:
     789         392 :                         if (file_handle->opened_path) {
     790         392 :                                 char *file_path = ZSTR_VAL(file_handle->opened_path);
     791             : 
     792         392 :                                 if (is_stream_path(file_path)) {
     793           0 :                                         if (zend_get_stream_timestamp(file_path, &statbuf) == SUCCESS) {
     794           0 :                                                 break;
     795             :                                         }
     796             :                                 }
     797         392 :                                 if (VCWD_STAT(file_path, &statbuf) != -1) {
     798         392 :                                         break;
     799             :                                 }
     800             :                         }
     801             : 
     802           0 :                         if (zend_get_stream_timestamp(file_handle->filename, &statbuf) != SUCCESS) {
     803           0 :                                 return 0;
     804             :                         }
     805           0 :                         break;
     806             :                 case ZEND_HANDLE_STREAM:
     807             :                         {
     808          35 :                                 php_stream *stream = (php_stream *)file_handle->handle.stream.handle;
     809             :                                 php_stream_statbuf sb;
     810             :                                 int ret, er;
     811             : 
     812         105 :                                 if (!stream ||
     813          35 :                                     !stream->ops ||
     814          35 :                                     !stream->ops->stat) {
     815           0 :                                         return 0;
     816             :                                 }
     817             : 
     818          35 :                                 er = EG(error_reporting);
     819          35 :                                 EG(error_reporting) = 0;
     820          35 :                                 zend_try {
     821          35 :                                         ret = stream->ops->stat(stream, &sb);
     822           0 :                                 } zend_catch {
     823           0 :                                         ret = -1;
     824          35 :                                 } zend_end_try();
     825          35 :                                 EG(error_reporting) = er;
     826          35 :                                 if (ret != 0) {
     827           0 :                                         return 0;
     828             :                                 }
     829             : 
     830          35 :                                 statbuf = sb.sb;
     831             :                         }
     832          35 :                         break;
     833             : 
     834             :                 default:
     835           0 :                         return 0;
     836             :         }
     837             : 
     838         433 :         if (size) {
     839           0 :                 *size = statbuf.st_size;
     840             :         }
     841         433 :         return statbuf.st_mtime;
     842             : }
     843             : 
     844           0 : static inline int do_validate_timestamps(zend_persistent_script *persistent_script, zend_file_handle *file_handle)
     845             : {
     846             :         zend_file_handle ps_handle;
     847           0 :         zend_string *full_path_ptr = NULL;
     848             : 
     849             :         /** check that the persistent script is indeed the same file we cached
     850             :          * (if part of the path is a symlink than it possible that the user will change it)
     851             :          * See bug #15140
     852             :          */
     853           0 :         if (file_handle->opened_path) {
     854           0 :                 if (persistent_script->script.filename != file_handle->opened_path &&
     855           0 :                     (ZSTR_LEN(persistent_script->script.filename) != ZSTR_LEN(file_handle->opened_path) ||
     856           0 :                      memcmp(ZSTR_VAL(persistent_script->script.filename), ZSTR_VAL(file_handle->opened_path), ZSTR_LEN(file_handle->opened_path)) != 0)) {
     857           0 :                         return FAILURE;
     858             :                 }
     859             :         } else {
     860           0 :                 full_path_ptr = accelerator_orig_zend_resolve_path(file_handle->filename, strlen(file_handle->filename));
     861           0 :                 if (full_path_ptr &&
     862           0 :                     persistent_script->script.filename != full_path_ptr &&
     863           0 :                     (ZSTR_LEN(persistent_script->script.filename) != ZSTR_LEN(full_path_ptr) ||
     864           0 :                      memcmp(ZSTR_VAL(persistent_script->script.filename), ZSTR_VAL(full_path_ptr), ZSTR_LEN(full_path_ptr)) != 0)) {
     865             :                         zend_string_release(full_path_ptr);
     866           0 :                         return FAILURE;
     867             :                 }
     868           0 :                 file_handle->opened_path = full_path_ptr;
     869             :         }
     870             : 
     871           0 :         if (persistent_script->timestamp == 0) {
     872           0 :                 if (full_path_ptr) {
     873             :                         zend_string_release(full_path_ptr);
     874           0 :                         file_handle->opened_path = NULL;
     875             :                 }
     876           0 :                 return FAILURE;
     877             :         }
     878             : 
     879           0 :         if (zend_get_file_handle_timestamp(file_handle, NULL) == persistent_script->timestamp) {
     880           0 :                 if (full_path_ptr) {
     881             :                         zend_string_release(full_path_ptr);
     882           0 :                         file_handle->opened_path = NULL;
     883             :                 }
     884           0 :                 return SUCCESS;
     885             :         }
     886           0 :         if (full_path_ptr) {
     887             :                 zend_string_release(full_path_ptr);
     888           0 :                 file_handle->opened_path = NULL;
     889             :         }
     890             : 
     891           0 :         ps_handle.type = ZEND_HANDLE_FILENAME;
     892           0 :         ps_handle.filename = ZSTR_VAL(persistent_script->script.filename);
     893           0 :         ps_handle.opened_path = persistent_script->script.filename;
     894             : 
     895           0 :         if (zend_get_file_handle_timestamp(&ps_handle, NULL) == persistent_script->timestamp) {
     896           0 :                 return SUCCESS;
     897             :         }
     898             : 
     899           0 :         return FAILURE;
     900             : }
     901             : 
     902           0 : int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle)
     903             : {
     904           0 :         if (ZCG(accel_directives).revalidate_freq &&
     905           0 :             persistent_script->dynamic_members.revalidate >= ZCG(request_time)) {
     906           0 :                 return SUCCESS;
     907           0 :         } else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) {
     908           0 :                 return FAILURE;
     909             :         } else {
     910           0 :                 persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
     911           0 :                 return SUCCESS;
     912             :         }
     913             : }
     914             : 
     915             : /* Instead of resolving full real path name each time we need to identify file,
     916             :  * we create a key that consist from requested file name, current working
     917             :  * directory, current include_path, etc */
     918        1191 : char *accel_make_persistent_key(const char *path, int path_length, int *key_len)
     919             : {
     920             :         int key_length;
     921             : 
     922             :         /* CWD and include_path don't matter for absolute file names and streams */
     923        1191 :     if (IS_ABSOLUTE_PATH(path, path_length)) {
     924             :                 /* pass */
     925        1134 :                 ZCG(key_len) = 0;
     926          57 :     } else if (UNEXPECTED(is_stream_path(path))) {
     927          28 :                 if (!is_cacheable_stream_path(path)) {
     928           0 :                         return NULL;
     929             :                 }
     930             :                 /* pass */
     931          28 :                 ZCG(key_len) = 0;
     932          29 :     } else if (UNEXPECTED(!ZCG(accel_directives).use_cwd)) {
     933             :                 /* pass */
     934           0 :                 ZCG(key_len) = 0;
     935             :     } else {
     936          29 :                 const char *include_path = NULL, *cwd = NULL;
     937          29 :                 int include_path_len = 0, cwd_len = 0;
     938          29 :                 zend_string *parent_script = NULL;
     939          29 :                 size_t parent_script_len = 0;
     940             : 
     941          29 :                 if (EXPECTED(ZCG(cwd_key_len))) {
     942           4 :                         cwd = ZCG(cwd_key);
     943           4 :                         cwd_len = ZCG(cwd_key_len);
     944             :                 } else {
     945          25 :                         zend_string *cwd_str = accel_getcwd();
     946             : 
     947          25 :                         if (UNEXPECTED(!cwd_str)) {
     948             :                                 /* we don't handle this well for now. */
     949           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);
     950           0 :                                 return NULL;
     951             :                         }
     952          25 :                         cwd = ZSTR_VAL(cwd_str);
     953          25 :                         cwd_len = ZSTR_LEN(cwd_str);
     954             : #ifndef ZTS
     955          25 :                         if (ZCG(cwd_check)) {
     956          25 :                                 ZCG(cwd_check) = 0;
     957          25 :                                 if ((ZCG(counted) || ZCSG(accelerator_enabled))) {
     958             : 
     959          25 :                                         zend_string *str = accel_find_interned_string(cwd_str);
     960          25 :                                         if (!str) {
     961          25 :                                                 SHM_UNPROTECT();
     962          25 :                                                 zend_shared_alloc_lock();
     963          25 :                                                 str = accel_new_interned_string(zend_string_copy(cwd_str));
     964          25 :                                                 if (str == cwd_str) {
     965             :                                                         zend_string_release(str);
     966           0 :                                                         str = NULL;
     967             :                                                 }
     968          25 :                                                 zend_shared_alloc_unlock();
     969          25 :                                                 SHM_PROTECT();
     970             :                                         }
     971          25 :                                         if (str) {
     972             :                                                 char buf[32];
     973          50 :                                                 char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, ZSTR_VAL(str) - ZCSG(interned_strings_start));
     974             : 
     975          25 :                                                 cwd_len = ZCG(cwd_key_len) = buf + sizeof(buf) - 1 - res;
     976          25 :                                                 cwd = ZCG(cwd_key);
     977          25 :                                                 memcpy(ZCG(cwd_key), res, cwd_len + 1);
     978             :                                         }
     979             :                                 }
     980             :                         }
     981             : #endif
     982             :                 }
     983             : 
     984          29 :                 if (EXPECTED(ZCG(include_path_key_len))) {
     985           4 :                         include_path = ZCG(include_path_key);
     986           4 :                         include_path_len = ZCG(include_path_key_len);
     987          25 :                 } else if (!ZCG(include_path) || ZSTR_LEN(ZCG(include_path)) == 0) {
     988           0 :                         include_path = "";
     989           0 :                         include_path_len = 0;
     990             :                 } else {
     991          25 :                         include_path = ZSTR_VAL(ZCG(include_path));
     992          25 :                         include_path_len = ZSTR_LEN(ZCG(include_path));
     993             : 
     994             : #ifndef ZTS
     995          25 :                         if (ZCG(include_path_check)) {
     996          25 :                                 ZCG(include_path_check) = 0;
     997          25 :                                 if ((ZCG(counted) || ZCSG(accelerator_enabled))) {
     998             : 
     999          25 :                                         zend_string *str = accel_find_interned_string(ZCG(include_path));
    1000          25 :                                         if (!str) {
    1001          23 :                                                 SHM_UNPROTECT();
    1002          23 :                                                 zend_shared_alloc_lock();
    1003          46 :                                                 str = accel_new_interned_string(zend_string_copy(ZCG(include_path)));
    1004          23 :                                                 if (str == ZCG(include_path)) {
    1005           0 :                                                         str = NULL;
    1006             :                                                 }
    1007          23 :                                                 zend_shared_alloc_unlock();
    1008          23 :                                                 SHM_PROTECT();
    1009             :                                         }
    1010          25 :                                         if (str) {
    1011             :                                                 char buf[32];
    1012          50 :                                                 char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, ZSTR_VAL(str) - ZCSG(interned_strings_start));
    1013             : 
    1014          25 :                                                 include_path_len = ZCG(include_path_key_len) = buf + sizeof(buf) - 1 - res;
    1015          25 :                                                 include_path = ZCG(include_path_key);
    1016          25 :                                                 memcpy(ZCG(include_path_key), res, include_path_len + 1);
    1017             :                                         }
    1018             :                                 }
    1019             :                         }
    1020             : #endif
    1021             :                 }
    1022             : 
    1023             :                 /* Calculate key length */
    1024          29 :                 if (UNEXPECTED((size_t)(cwd_len + path_length + include_path_len + 2) >= sizeof(ZCG(key)))) {
    1025           0 :                         return NULL;
    1026             :                 }
    1027             : 
    1028             :                 /* Generate key
    1029             :                  * Note - the include_path must be the last element in the key,
    1030             :                  * since in itself, it may include colons (which we use to separate
    1031             :                  * different components of the key)
    1032             :                  */
    1033          29 :                 memcpy(ZCG(key), path, path_length);
    1034          29 :                 ZCG(key)[path_length] = ':';
    1035          29 :                 key_length = path_length + 1;
    1036          29 :                 memcpy(ZCG(key) + key_length, cwd, cwd_len);
    1037          29 :                 key_length += cwd_len;
    1038             : 
    1039          29 :                 if (include_path_len) {
    1040          29 :                         ZCG(key)[key_length] = ':';
    1041          29 :                         key_length += 1;
    1042          29 :                         memcpy(ZCG(key) + key_length, include_path, include_path_len);
    1043          29 :                         key_length += include_path_len;
    1044             :                 }
    1045             : 
    1046             :                 /* Here we add to the key the parent script directory,
    1047             :                  * since fopen_wrappers from version 4.0.7 use current script's path
    1048             :                  * in include path too.
    1049             :                  */
    1050          87 :                 if (EXPECTED(EG(current_execute_data)) &&
    1051          58 :                     EXPECTED((parent_script = zend_get_executed_filename_ex()) != NULL)) {
    1052             : 
    1053          29 :                         parent_script_len = ZSTR_LEN(parent_script);
    1054         578 :                         while ((--parent_script_len > 0) && !IS_SLASH(ZSTR_VAL(parent_script)[parent_script_len]));
    1055             : 
    1056          29 :                         if (UNEXPECTED((size_t)(key_length + parent_script_len + 1) >= sizeof(ZCG(key)))) {
    1057           0 :                                 return NULL;
    1058             :                         }
    1059          29 :                         ZCG(key)[key_length] = ':';
    1060          29 :                         key_length += 1;
    1061          29 :                         memcpy(ZCG(key) + key_length, ZSTR_VAL(parent_script), parent_script_len);
    1062          29 :                         key_length += parent_script_len;
    1063             :                 }
    1064          29 :                 ZCG(key)[key_length] = '\0';
    1065          29 :                 *key_len = ZCG(key_len) = key_length;
    1066          29 :                 return ZCG(key);
    1067             :         }
    1068             : 
    1069             :         /* not use_cwd */
    1070        1162 :         *key_len = path_length;
    1071        1162 :         return (char*)path;
    1072             : }
    1073             : 
    1074           0 : int zend_accel_invalidate(const char *filename, int filename_len, zend_bool force)
    1075             : {
    1076             :         zend_string *realpath;
    1077             :         zend_persistent_script *persistent_script;
    1078             : 
    1079           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock() != SUCCESS) {
    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           0 :         if (ZCG(accel_directives).file_cache) {
    1091           0 :                 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 = ZSTR_VAL(realpath);
    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           0 : 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           0 :         if (!zend_accel_script_persistable(new_persistent_script)) {
    1158           0 :                 return new_persistent_script;
    1159             :         }
    1160             : 
    1161           0 :         if (!zend_optimize_script(&new_persistent_script->script, ZCG(accel_directives).optimization_level, ZCG(accel_directives).opt_debug_level)) {
    1162           0 :                 return new_persistent_script;
    1163             :         }
    1164             : 
    1165           0 :         zend_shared_alloc_init_xlat_table();
    1166             : 
    1167             :         /* Calculate the required memory size */
    1168           0 :         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           0 :         ZCG(mem) = zend_arena_alloc(&CG(arena), memory_used + 64);
    1174           0 :         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           0 :         new_persistent_script = zend_accel_script_persist(new_persistent_script, NULL, 0);
    1181             : 
    1182           0 :         zend_shared_alloc_destroy_xlat_table();
    1183             : 
    1184           0 :         new_persistent_script->is_phar =
    1185           0 :                 new_persistent_script->script.filename &&
    1186           0 :                 strstr(ZSTR_VAL(new_persistent_script->script.filename), ".phar") &&
    1187           0 :                 !strstr(ZSTR_VAL(new_persistent_script->script.filename), "://");
    1188             : 
    1189             :         /* Consistency check */
    1190           0 :         if ((char*)new_persistent_script->mem + new_persistent_script->size != (char*)ZCG(mem)) {
    1191           0 :                 zend_accel_error(
    1192           0 :                         ((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           0 :                         ZSTR_VAL(new_persistent_script->script.filename),
    1195             :                         new_persistent_script->mem,
    1196             :                         (char *)new_persistent_script->mem + new_persistent_script->size,
    1197             :                         ZCG(mem));
    1198             :         }
    1199             : 
    1200           0 :         new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
    1201             : 
    1202           0 :         zend_file_cache_script_store(new_persistent_script, 0);
    1203             : 
    1204           0 :         *from_shared_memory = 1;
    1205           0 :         return new_persistent_script;
    1206             : }
    1207             : #endif
    1208             : 
    1209         430 : 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         430 :         if (!zend_accel_script_persistable(new_persistent_script)) {
    1216           0 :                 return new_persistent_script;
    1217             :         }
    1218             : 
    1219         430 :         if (!zend_optimize_script(&new_persistent_script->script, ZCG(accel_directives).optimization_level, ZCG(accel_directives).opt_debug_level)) {
    1220           0 :                 return new_persistent_script;
    1221             :         }
    1222             : 
    1223             :         /* exclusive lock */
    1224         430 :         zend_shared_alloc_lock();
    1225             : 
    1226         430 :         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         430 :         bucket = zend_accel_hash_find_entry(&ZCSG(hash), new_persistent_script->script.filename);
    1238         430 :         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         430 :         zend_shared_alloc_init_xlat_table();
    1254             : 
    1255             :         /* Calculate the required memory size */
    1256         430 :         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         430 :         ZCG(mem) = zend_shared_alloc(memory_used + 64);
    1262         430 :         ZCG(mem) = (void*)(((zend_uintptr_t)ZCG(mem) + 63L) & ~63L);
    1263             : #else
    1264             :         ZCG(mem) = zend_shared_alloc(memory_used);
    1265             : #endif
    1266         430 :         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         430 :         new_persistent_script = zend_accel_script_persist(new_persistent_script, &key, key_length);
    1275             : 
    1276         430 :         zend_shared_alloc_destroy_xlat_table();
    1277             : 
    1278         430 :         new_persistent_script->is_phar =
    1279         939 :                 new_persistent_script->script.filename &&
    1280         430 :                 strstr(ZSTR_VAL(new_persistent_script->script.filename), ".phar") &&
    1281          79 :                 !strstr(ZSTR_VAL(new_persistent_script->script.filename), "://");
    1282             : 
    1283             :         /* Consistency check */
    1284         430 :         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 :                         ZSTR_VAL(new_persistent_script->script.filename),
    1289             :                         new_persistent_script->mem,
    1290             :                         (char *)new_persistent_script->mem + new_persistent_script->size,
    1291             :                         ZCG(mem));
    1292             :         }
    1293             : 
    1294         430 :         new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
    1295             : 
    1296             :         /* store script structure in the hash table */
    1297         430 :         bucket = zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(new_persistent_script->script.filename), ZSTR_LEN(new_persistent_script->script.filename), 0, new_persistent_script);
    1298         430 :         if (bucket) {
    1299         430 :                 zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", new_persistent_script->script.filename);
    1300        1646 :                 if (key &&
    1301             :                     /* key may contain non-persistent PHAR aliases (see issues #115 and #149) */
    1302         430 :                     memcmp(key, "phar://", sizeof("phar://") - 1) != 0 &&
    1303         403 :                     (ZSTR_LEN(new_persistent_script->script.filename) != key_length ||
    1304         383 :                      memcmp(ZSTR_VAL(new_persistent_script->script.filename), 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         430 :         new_persistent_script->dynamic_members.memory_consumption = ZEND_ALIGNED_SIZE(new_persistent_script->size);
    1317             : 
    1318         430 :         zend_shared_alloc_unlock();
    1319             : 
    1320             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1321         430 :         if (ZCG(accel_directives).file_cache) {
    1322           0 :                 SHM_PROTECT();
    1323           0 :                 zend_file_cache_script_store(new_persistent_script, 1);
    1324           0 :                 SHM_UNPROTECT();
    1325             :         }
    1326             : #endif
    1327             : 
    1328         430 :         *from_shared_memory = 1;
    1329         430 :         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         430 : static int zend_accel_get_auto_globals(void)
    1346             : {
    1347         430 :         int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
    1348         430 :         int n = 1;
    1349         430 :         int mask = 0;
    1350             : 
    1351        2150 :         for (i = 0; i < ag_size ; i++) {
    1352        1720 :                 if (zend_hash_exists(&EG(symbol_table), jit_auto_globals_str[i])) {
    1353         249 :                         mask |= n;
    1354             :                 }
    1355        1720 :                 n += n;
    1356             :         }
    1357         430 :         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         378 : static void zend_accel_init_auto_globals(void)
    1383             : {
    1384         378 :         int i, ag_size = (sizeof(jit_auto_globals_info) / sizeof(jit_auto_globals_info[0]));
    1385             : 
    1386        1890 :         for (i = 0; i < ag_size ; i++) {
    1387        3024 :                 jit_auto_globals_str[i] = zend_string_init(jit_auto_globals_info[i].name, jit_auto_globals_info[i].len, 1);
    1388        1512 :                 zend_string_hash_val(jit_auto_globals_str[i]);
    1389        1512 :                 jit_auto_globals_str[i] = accel_new_interned_string(jit_auto_globals_str[i]);
    1390             :         }
    1391         378 : }
    1392             : 
    1393         433 : 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         433 :         int do_bailout = 0;
    1401         433 :         accel_time_t timestamp = 0;
    1402         433 :         uint32_t orig_compiler_options = 0;
    1403             : 
    1404             :     /* Try to open file */
    1405         433 :     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         433 :         if (file_handle->opened_path && zend_accel_blacklist_is_blacklisted(&accel_blacklist, ZSTR_VAL(file_handle->opened_path))) {
    1425           0 :                 ZCSG(blacklist_misses)++;
    1426           0 :                 *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1427           0 :                 return NULL;
    1428             :         }
    1429             : 
    1430         433 :         if (ZCG(accel_directives).validate_timestamps ||
    1431           0 :             ZCG(accel_directives).file_update_protection ||
    1432           0 :             ZCG(accel_directives).max_file_size > 0) {
    1433         433 :                 size_t size = 0;
    1434             : 
    1435             :                 /* Obtain the file timestamps, *before* actually compiling them,
    1436             :                  * otherwise we have a race-condition.
    1437             :                  */
    1438         433 :                 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         433 :                 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         433 :                 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         433 :                 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         433 :         new_persistent_script = create_persistent_script();
    1463             : 
    1464             :         /* Save the original values for the op_array, function table and class table */
    1465         433 :         orig_active_op_array = CG(active_op_array);
    1466         433 :         orig_function_table = CG(function_table);
    1467         433 :         orig_class_table = CG(class_table);
    1468         433 :         ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
    1469             : 
    1470             :         /* Override them with ours */
    1471         433 :         CG(function_table) = &ZCG(function_table);
    1472         433 :         EG(class_table) = CG(class_table) = &new_persistent_script->script.class_table;
    1473         433 :         ZVAL_UNDEF(&EG(user_error_handler));
    1474             : 
    1475         433 :         zend_try {
    1476         433 :                 orig_compiler_options = CG(compiler_options);
    1477         433 :                 CG(compiler_options) |= ZEND_COMPILE_HANDLE_OP_ARRAY;
    1478         433 :                 CG(compiler_options) |= ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
    1479         433 :                 CG(compiler_options) |= ZEND_COMPILE_DELAYED_BINDING;
    1480         433 :                 CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION;
    1481         433 :                 op_array = *op_array_p = accelerator_orig_compile_file(file_handle, type);
    1482         431 :                 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         433 :         } zend_end_try();
    1488             : 
    1489             :         /* Restore originals */
    1490         433 :         CG(active_op_array) = orig_active_op_array;
    1491         433 :         CG(function_table) = orig_function_table;
    1492         433 :         EG(class_table) = CG(class_table) = orig_class_table;
    1493         433 :         EG(user_error_handler) = orig_user_error_handler;
    1494             : 
    1495         433 :         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         430 :         zend_accel_move_user_functions(&ZCG(function_table), &new_persistent_script->script.function_table);
    1510         430 :         new_persistent_script->script.main_op_array = *op_array;
    1511             : 
    1512         430 :         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         430 :         if (PG(auto_globals_jit)) {
    1517         430 :                 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         430 :         if (ZCG(accel_directives).validate_timestamps) {
    1523             :                 /* Obtain the file timestamps, *before* actually compiling them,
    1524             :                  * otherwise we have a race-condition.
    1525             :                  */
    1526         430 :                 new_persistent_script->timestamp = timestamp;
    1527         430 :                 new_persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
    1528             :         }
    1529             : 
    1530         430 :         if (file_handle->opened_path) {
    1531         860 :                 new_persistent_script->script.filename = zend_string_copy(file_handle->opened_path);
    1532             :         } else {
    1533           0 :                 new_persistent_script->script.filename = zend_string_init(file_handle->filename, strlen(file_handle->filename), 0);
    1534             :         }
    1535         430 :         zend_string_hash_val(new_persistent_script->script.filename);
    1536             : 
    1537             :         /* Now persistent_script structure is ready in process memory */
    1538         430 :         return new_persistent_script;
    1539             : }
    1540             : 
    1541             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1542           0 : zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)
    1543             : {
    1544             :         zend_persistent_script *persistent_script;
    1545           0 :         zend_op_array *op_array = NULL;
    1546             :         int from_memory; /* if the script we've got is stored in SHM */
    1547             : 
    1548           0 :         if (is_stream_path(file_handle->filename) &&
    1549           0 :             !is_cacheable_stream_path(file_handle->filename)) {
    1550           0 :                 return accelerator_orig_compile_file(file_handle, type);
    1551             :         }
    1552             : 
    1553           0 :         if (!file_handle->opened_path) {
    1554           0 :                 if (file_handle->type == ZEND_HANDLE_FILENAME &&
    1555           0 :                     accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) == FAILURE) {
    1556           0 :                         if (type == ZEND_REQUIRE) {
    1557           0 :                                 zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
    1558           0 :                                 zend_bailout();
    1559             :                         } else {
    1560           0 :                                 zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, file_handle->filename);
    1561             :                         }
    1562           0 :                         return NULL;
    1563             :             }
    1564             :         }
    1565             : 
    1566           0 :         persistent_script = zend_file_cache_script_load(file_handle);
    1567           0 :         if (persistent_script) {
    1568             :                 /* see bug #15471 (old BTS) */
    1569           0 :                 if (persistent_script->script.filename) {
    1570           0 :                         if (!EG(current_execute_data) || !EG(current_execute_data)->opline ||
    1571           0 :                             !EG(current_execute_data)->func ||
    1572           0 :                             !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
    1573           0 :                             EG(current_execute_data)->opline->opcode != ZEND_INCLUDE_OR_EVAL ||
    1574           0 :                             (EG(current_execute_data)->opline->extended_value != ZEND_INCLUDE_ONCE &&
    1575           0 :                              EG(current_execute_data)->opline->extended_value != ZEND_REQUIRE_ONCE)) {
    1576           0 :                                 if (zend_hash_add_empty_element(&EG(included_files), persistent_script->script.filename) != NULL) {
    1577             :                                         /* ext/phar has to load phar's metadata into memory */
    1578           0 :                                         if (persistent_script->is_phar) {
    1579             :                                                 php_stream_statbuf ssb;
    1580           0 :                                                 char *fname = emalloc(sizeof("phar://") + ZSTR_LEN(persistent_script->script.filename));
    1581             : 
    1582           0 :                                                 memcpy(fname, "phar://", sizeof("phar://") - 1);
    1583           0 :                                                 memcpy(fname + sizeof("phar://") - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename) + 1);
    1584           0 :                                                 php_stream_stat_path(fname, &ssb);
    1585           0 :                                                 efree(fname);
    1586             :                                         }
    1587             :                                 }
    1588             :                         }
    1589             :                 }
    1590           0 :                 zend_file_handle_dtor(file_handle);
    1591             : 
    1592           0 :                 persistent_script->dynamic_members.last_used = ZCG(request_time);
    1593             : 
    1594           0 :             if (persistent_script->ping_auto_globals_mask) {
    1595           0 :                         zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask);
    1596             :                 }
    1597             : 
    1598           0 :                 return zend_accel_load_script(persistent_script, 1);
    1599             :         }
    1600             : 
    1601           0 :         persistent_script = opcache_compile_file(file_handle, type, NULL, 0, &op_array);
    1602             : 
    1603           0 :         if (persistent_script) {
    1604           0 :                 from_memory = 0;
    1605           0 :                 persistent_script = cache_script_in_file_cache(persistent_script, &from_memory);
    1606           0 :                 return zend_accel_load_script(persistent_script, from_memory);
    1607             :         }
    1608             : 
    1609           0 :         return op_array;
    1610             : }
    1611             : #endif
    1612             : 
    1613             : /* zend_compile() replacement */
    1614         435 : zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
    1615             : {
    1616         435 :         zend_persistent_script *persistent_script = NULL;
    1617         435 :         char *key = NULL;
    1618             :         int key_length;
    1619             :         int from_shared_memory; /* if the script we've got is stored in SHM */
    1620             : 
    1621         435 :         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         435 :         } else if (ZCG(accel_directives).file_cache_only) {
    1626           0 :                 return file_cache_compile_file(file_handle, type);
    1627             : #endif
    1628         870 :         } else if ((!ZCG(counted) && !ZCSG(accelerator_enabled)) ||
    1629         435 :                    (ZCSG(restart_in_progress) && accel_restart_is_active())) {
    1630             : #ifdef HAVE_OPCACHE_FILE_CACHE
    1631           0 :                 if (ZCG(accel_directives).file_cache) {
    1632           0 :                         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         435 :         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         435 :                 if (!ZCG(accel_directives).revalidate_path) {
    1658             :                         /* try to find cached script by key */
    1659         435 :                         key = accel_make_persistent_key(file_handle->filename, strlen(file_handle->filename), &key_length);
    1660         435 :                         if (!key) {
    1661           0 :                                 return accelerator_orig_compile_file(file_handle, type);
    1662             :                         }
    1663         435 :                         persistent_script = zend_accel_hash_str_find(&ZCSG(hash), key, key_length);
    1664             :                 }
    1665         435 :                 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         486 :                     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         433 :                         if (file_handle->opened_path) {
    1682         433 :                                 bucket = zend_accel_hash_find_entry(&ZCSG(hash), file_handle->opened_path);
    1683             : 
    1684         433 :                                 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         433 :         ZCG(cache_opline) = NULL;
    1701         433 :         ZCG(cache_persistent_script) = NULL;
    1702             : 
    1703         433 :         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         433 :         if (!ZCG(counted)) {
    1712         377 :                 ZCG(counted) = 1;
    1713         377 :                 accel_activate_add();
    1714             :         }
    1715             : 
    1716         433 :         SHM_UNPROTECT();
    1717             : 
    1718             :         /* If script is found then validate_timestamps if option is enabled */
    1719         433 :         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         433 :         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->script.filename, 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         433 :         if (!persistent_script && ZCG(accel_directives).file_cache) {
    1765           0 :                 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         433 :         if (!persistent_script) {
    1771         433 :                 uint32_t old_const_num = zend_hash_next_free_element(EG(zend_constants));
    1772             :                 zend_op_array *op_array;
    1773             : 
    1774             :                 /* Cache miss.. */
    1775         433 :                 ZCSG(misses)++;
    1776             : 
    1777             :                 /* No memory left. Behave like without the Accelerator */
    1778         433 :                 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         433 :         from_shared_memory = 0;
    1787         433 :                 persistent_script = opcache_compile_file(file_handle, type, key, key ? key_length : 0, &op_array);
    1788         431 :                 if (persistent_script) {
    1789         430 :                         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         431 :                 if (!persistent_script) {
    1796           1 :                         SHM_PROTECT();
    1797           1 :                         return op_array;
    1798             :                 }
    1799         430 :                 if (from_shared_memory) {
    1800             :                         /* Delete immutable arrays moved into SHM */
    1801         430 :                         uint32_t new_const_num = zend_hash_next_free_element(EG(zend_constants));
    1802         860 :                         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             : #ifdef _M_X64
    1814             :                 InterlockedIncrement64(&ZCSG(hits));
    1815             : #else
    1816             :                 InterlockedIncrement(&ZCSG(hits));
    1817             : #endif
    1818             :                 InterlockedIncrement64(&persistent_script->dynamic_members.hits);
    1819             : #endif
    1820             : 
    1821             :                 /* see bug #15471 (old BTS) */
    1822           0 :                 if (persistent_script->script.filename) {
    1823           0 :                         if (!EG(current_execute_data) || !EG(current_execute_data)->opline ||
    1824           0 :                             !EG(current_execute_data)->func ||
    1825           0 :                             !ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
    1826           0 :                             EG(current_execute_data)->opline->opcode != ZEND_INCLUDE_OR_EVAL ||
    1827           0 :                             (EG(current_execute_data)->opline->extended_value != ZEND_INCLUDE_ONCE &&
    1828           0 :                              EG(current_execute_data)->opline->extended_value != ZEND_REQUIRE_ONCE)) {
    1829           0 :                                 if (zend_hash_add_empty_element(&EG(included_files), persistent_script->script.filename) != NULL) {
    1830             :                                         /* ext/phar has to load phar's metadata into memory */
    1831           0 :                                         if (persistent_script->is_phar) {
    1832             :                                                 php_stream_statbuf ssb;
    1833           0 :                                                 char *fname = emalloc(sizeof("phar://") + ZSTR_LEN(persistent_script->script.filename));
    1834             : 
    1835           0 :                                                 memcpy(fname, "phar://", sizeof("phar://") - 1);
    1836           0 :                                                 memcpy(fname + sizeof("phar://") - 1, ZSTR_VAL(persistent_script->script.filename), ZSTR_LEN(persistent_script->script.filename) + 1);
    1837           0 :                                                 php_stream_stat_path(fname, &ssb);
    1838           0 :                                                 efree(fname);
    1839             :                                         }
    1840             :                                 }
    1841             :                         }
    1842             :                 }
    1843           0 :                 zend_file_handle_dtor(file_handle);
    1844           0 :                 from_shared_memory = 1;
    1845             :         }
    1846             : 
    1847         430 :         persistent_script->dynamic_members.last_used = ZCG(request_time);
    1848             : 
    1849         430 :         SHM_PROTECT();
    1850             : 
    1851             :     /* Fetch jit auto globals used in the script before execution */
    1852         430 :     if (persistent_script->ping_auto_globals_mask) {
    1853         133 :                 zend_accel_set_auto_globals(persistent_script->ping_auto_globals_mask);
    1854             :         }
    1855             : 
    1856         430 :         return zend_accel_load_script(persistent_script, from_shared_memory);
    1857             : }
    1858             : 
    1859             : /* zend_stream_open_function() replacement for PHP 5.3 and above */
    1860         424 : static int persistent_stream_open_function(const char *filename, zend_file_handle *handle)
    1861             : {
    1862         424 :         if (ZCG(cache_persistent_script)) {
    1863             :                 /* check if callback is called from include_once or it's a main request */
    1864           0 :                 if ((!EG(current_execute_data) &&
    1865           0 :                      filename == SG(request_info).path_translated &&
    1866           0 :                      ZCG(cache_opline) == NULL) ||
    1867           0 :                     (EG(current_execute_data) &&
    1868           0 :                      EG(current_execute_data)->func &&
    1869           0 :                      ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
    1870           0 :                      ZCG(cache_opline) == EG(current_execute_data)->opline)) {
    1871             : 
    1872             :                         /* we are in include_once or FastCGI request */
    1873           0 :                         handle->filename = (char*)filename;
    1874           0 :                         handle->free_filename = 0;
    1875           0 :                         handle->opened_path = zend_string_copy(ZCG(cache_persistent_script)->script.filename);
    1876           0 :                         handle->type = ZEND_HANDLE_FILENAME;
    1877           0 :                         return SUCCESS;
    1878             :                 }
    1879           0 :                 ZCG(cache_opline) = NULL;
    1880           0 :                 ZCG(cache_persistent_script) = NULL;
    1881             :         }
    1882         424 :         return accelerator_orig_zend_stream_open_function(filename, handle);
    1883             : }
    1884             : 
    1885             : /* zend_resolve_path() replacement for PHP 5.3 and above */
    1886         853 : static zend_string* persistent_zend_resolve_path(const char *filename, int filename_len)
    1887             : {
    1888        3301 :         if (ZCG(enabled) && accel_startup_ok &&
    1889        1595 :             (ZCG(counted) || ZCSG(accelerator_enabled)) &&
    1890         853 :             !ZCSG(restart_in_progress)) {
    1891             : 
    1892             :                 /* check if callback is called from include_once or it's a main request */
    1893        2008 :                 if ((!EG(current_execute_data) &&
    1894         776 :                      filename == SG(request_info).path_translated) ||
    1895         111 :                     (EG(current_execute_data) &&
    1896          77 :                      EG(current_execute_data)->func &&
    1897          77 :                      ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
    1898          38 :                      EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
    1899          38 :                      (EG(current_execute_data)->opline->extended_value == ZEND_INCLUDE_ONCE ||
    1900          38 :                       EG(current_execute_data)->opline->extended_value == ZEND_REQUIRE_ONCE))) {
    1901             : 
    1902             :                         /* we are in include_once or FastCGI request */
    1903             :                         zend_string *resolved_path;
    1904             :                         int key_length;
    1905         756 :                         char *key = NULL;
    1906             :                         
    1907         756 :                         if (!ZCG(accel_directives).revalidate_path) {
    1908             :                                 /* lookup by "not-real" path */
    1909         756 :                                 key = accel_make_persistent_key(filename, filename_len, &key_length);
    1910         756 :                                 if (key) {
    1911         756 :                                         zend_accel_hash_entry *bucket = zend_accel_hash_str_find_entry(&ZCSG(hash), key, key_length);
    1912         756 :                                         if (bucket != NULL) {
    1913           0 :                                                 zend_persistent_script *persistent_script = (zend_persistent_script *)bucket->data;
    1914           0 :                                                 if (!persistent_script->corrupted) {
    1915           0 :                                                         ZCG(cache_opline) = EG(current_execute_data) ? EG(current_execute_data)->opline : NULL;
    1916           0 :                                                         ZCG(cache_persistent_script) = persistent_script;
    1917           0 :                                                         return zend_string_copy(persistent_script->script.filename);
    1918             :                                                 }
    1919             :                                         }
    1920             :                                 } else {
    1921           0 :                                         ZCG(cache_opline) = NULL;
    1922           0 :                                         ZCG(cache_persistent_script) = NULL;
    1923           0 :                                         return accelerator_orig_zend_resolve_path(filename, filename_len);
    1924             :                                 }
    1925             :                         }
    1926             : 
    1927             :                         /* find the full real path */
    1928         756 :                         resolved_path = accelerator_orig_zend_resolve_path(filename, filename_len);
    1929             : 
    1930         756 :                         if (resolved_path) {
    1931             :                                 /* lookup by real path */
    1932         756 :                                 zend_accel_hash_entry *bucket = zend_accel_hash_find_entry(&ZCSG(hash), resolved_path);
    1933         756 :                                 if (bucket) {
    1934           0 :                                         zend_persistent_script *persistent_script = (zend_persistent_script *)bucket->data;
    1935           0 :                                         if (!persistent_script->corrupted) {
    1936           0 :                                                 if (key) {
    1937             :                                                         /* add another "key" for the same bucket */
    1938           0 :                                                         SHM_UNPROTECT();
    1939           0 :                                                         zend_shared_alloc_lock();
    1940           0 :                                                         zend_accel_add_key(key, key_length, bucket);
    1941           0 :                                                         zend_shared_alloc_unlock();
    1942           0 :                                                         SHM_PROTECT();
    1943             :                                                 } else {
    1944           0 :                                                         ZCG(key_len) = 0;
    1945             :                                                 }
    1946           0 :                                                 ZCG(cache_opline) = EG(current_execute_data) ? EG(current_execute_data)->opline : NULL;
    1947           0 :                                                 ZCG(cache_persistent_script) = persistent_script;
    1948           0 :                                                 return resolved_path;
    1949             :                                         }
    1950             :                                 }
    1951             :                         }
    1952             : 
    1953         756 :                         ZCG(cache_opline) = NULL;
    1954         756 :                         ZCG(cache_persistent_script) = NULL;
    1955         756 :                         return resolved_path;
    1956             :                 }
    1957             :         }
    1958          97 :         ZCG(cache_opline) = NULL;
    1959          97 :         ZCG(cache_persistent_script) = NULL;
    1960          97 :         return accelerator_orig_zend_resolve_path(filename, filename_len);
    1961             : }
    1962             : 
    1963         378 : static void zend_reset_cache_vars(void)
    1964             : {
    1965         378 :         ZSMMG(memory_exhausted) = 0;
    1966         378 :         ZCSG(hits) = 0;
    1967         378 :         ZCSG(misses) = 0;
    1968         378 :         ZCSG(blacklist_misses) = 0;
    1969         378 :         ZSMMG(wasted_shared_memory) = 0;
    1970         378 :         ZCSG(restart_pending) = 0;
    1971         378 :         ZCSG(force_restart_time) = 0;
    1972         378 : }
    1973             : 
    1974         378 : static void accel_reset_pcre_cache(void)
    1975             : {
    1976             :         Bucket *p;
    1977             : 
    1978         378 :         ZEND_HASH_FOREACH_BUCKET(&PCRE_G(pcre_cache), p) {
    1979             :                 /* Remove PCRE cache entries with inconsistent keys */
    1980           0 :                 if (ZSTR_IS_INTERNED(p->key)) {
    1981           0 :                         p->key = NULL;
    1982           0 :                         zend_hash_del_bucket(&PCRE_G(pcre_cache), p);
    1983             :                 }
    1984             :         } ZEND_HASH_FOREACH_END();
    1985         378 : }
    1986             : 
    1987         378 : static void accel_activate(void)
    1988             : {
    1989         378 :         zend_bool reset_pcre = 0;
    1990             : 
    1991         378 :         if (!ZCG(enabled) || !accel_startup_ok) {
    1992           0 :                 return;
    1993             :         }
    1994             : 
    1995         378 :         if (!ZCG(function_table).nTableSize) {
    1996         378 :                 zend_hash_init(&ZCG(function_table), zend_hash_num_elements(CG(function_table)), NULL, ZEND_FUNCTION_DTOR, 1);
    1997         378 :                 zend_accel_copy_internal_functions();
    1998             :         }
    1999             : 
    2000             :         /* PHP-5.4 and above return "double", but we use 1 sec precision */
    2001         378 :         ZCG(auto_globals_mask) = 0;
    2002         378 :         ZCG(request_time) = (time_t)sapi_get_request_time();
    2003         378 :         ZCG(cache_opline) = NULL;
    2004         378 :         ZCG(cache_persistent_script) = NULL;
    2005         378 :         ZCG(include_path_key_len) = 0;
    2006         378 :         ZCG(include_path_check) = 1;
    2007             : 
    2008             :         /* check if ZCG(function_table) wasn't somehow polluted on the way */
    2009         378 :         if (ZCG(internal_functions_count) != zend_hash_num_elements(&ZCG(function_table))) {
    2010           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)));
    2011             :         }
    2012             : 
    2013         378 :         ZCG(cwd) = NULL;
    2014         378 :         ZCG(cwd_key_len) = 0;
    2015         378 :         ZCG(cwd_check) = 1;
    2016             : 
    2017             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2018         378 :         if (ZCG(accel_directives).file_cache_only) {
    2019           0 :                 return;
    2020             :         }
    2021             : #endif
    2022             : 
    2023         378 :         SHM_UNPROTECT();
    2024             : 
    2025         378 :         if (ZCG(counted)) {
    2026             : #ifdef ZTS
    2027             :                 zend_accel_error(ACCEL_LOG_WARNING, "Stuck count for thread id %d", tsrm_thread_id());
    2028             : #else
    2029           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Stuck count for pid %d", getpid());
    2030             : #endif
    2031           0 :                 accel_unlock_all();
    2032           0 :                 ZCG(counted) = 0;
    2033             :         }
    2034             : 
    2035         378 :         if (ZCSG(restart_pending)) {
    2036           0 :                 zend_shared_alloc_lock();
    2037           0 :                 if (ZCSG(restart_pending) != 0) { /* check again, to ensure that the cache wasn't already cleaned by another process */
    2038           0 :                         if (accel_is_inactive() == SUCCESS) {
    2039           0 :                                 zend_accel_error(ACCEL_LOG_DEBUG, "Restarting!");
    2040           0 :                                 ZCSG(restart_pending) = 0;
    2041           0 :                                 switch ZCSG(restart_reason) {
    2042             :                                         case ACCEL_RESTART_OOM:
    2043           0 :                                                 ZCSG(oom_restarts)++;
    2044           0 :                                                 break;
    2045             :                                         case ACCEL_RESTART_HASH:
    2046           0 :                                                 ZCSG(hash_restarts)++;
    2047           0 :                                                 break;
    2048             :                                         case ACCEL_RESTART_USER:
    2049           0 :                                                 ZCSG(manual_restarts)++;
    2050             :                                                 break;
    2051             :                                 }
    2052           0 :                                 accel_restart_enter();
    2053             : 
    2054           0 :                                 zend_reset_cache_vars();
    2055           0 :                                 zend_accel_hash_clean(&ZCSG(hash));
    2056             : 
    2057             : #if !defined(ZTS)
    2058           0 :                                 if (ZCG(accel_directives).interned_strings_buffer) {
    2059           0 :                                         accel_interned_strings_restore_state();
    2060             :                                 }
    2061             : #endif
    2062             : 
    2063           0 :                                 zend_shared_alloc_restore_state();
    2064           0 :                                 ZCSG(accelerator_enabled) = ZCSG(cache_status_before_restart);
    2065           0 :                                 if (ZCSG(last_restart_time) < ZCG(request_time)) {
    2066           0 :                                         ZCSG(last_restart_time) = ZCG(request_time);
    2067             :                                 } else {
    2068           0 :                                         ZCSG(last_restart_time)++;
    2069             :                                 }
    2070           0 :                                 accel_restart_leave();
    2071             :                         }
    2072             :                 } else {
    2073           0 :                         reset_pcre = 1;
    2074             :                 }
    2075           0 :                 zend_shared_alloc_unlock();
    2076             :         }
    2077             : 
    2078         378 :         SHM_PROTECT();
    2079             : 
    2080         378 :         if (ZCSG(last_restart_time) != ZCG(last_restart_time)) {
    2081             :                 /* SHM was reinitialized. */
    2082           0 :                 ZCG(last_restart_time) = ZCSG(last_restart_time);
    2083             : 
    2084             :                 /* Reset in-process realpath cache */
    2085           0 :                 realpath_cache_clean();
    2086             : 
    2087           0 :                 accel_reset_pcre_cache();
    2088         378 :         } else if (reset_pcre) {
    2089           0 :                 accel_reset_pcre_cache();
    2090             :         }
    2091             : }
    2092             : 
    2093             : #if !ZEND_DEBUG
    2094             : 
    2095             : /* Fast Request Shutdown
    2096             :  * =====================
    2097             :  * Zend Memory Manager frees memory by its own. We don't have to free each
    2098             :  * allocated block separately, but we like to call all the destructors and
    2099             :  * callbacks in exactly the same order.
    2100             :  */
    2101             : static void accel_fast_hash_destroy(HashTable *ht);
    2102             : 
    2103           0 : static void accel_fast_zval_dtor(zval *zvalue)
    2104             : {
    2105             : tail_call:
    2106           0 :         switch (Z_TYPE_P(zvalue)) {
    2107             :                 case IS_ARRAY:
    2108           0 :                         GC_REMOVE_FROM_BUFFER(Z_ARR_P(zvalue));
    2109           0 :                         if (Z_ARR_P(zvalue) != &EG(symbol_table)) {
    2110             :                                 /* break possible cycles */
    2111           0 :                                 ZVAL_NULL(zvalue);
    2112           0 :                                 accel_fast_hash_destroy(Z_ARRVAL_P(zvalue));
    2113             :                         }
    2114           0 :                         break;
    2115             :                 case IS_OBJECT:
    2116           0 :                         OBJ_RELEASE(Z_OBJ_P(zvalue));
    2117           0 :                         break;
    2118             :                 case IS_RESOURCE:
    2119           0 :                         zend_list_delete(Z_RES_P(zvalue));
    2120           0 :                         break;
    2121             :                 case IS_REFERENCE: {
    2122           0 :                                 zend_reference *ref = Z_REF_P(zvalue);
    2123             : 
    2124           0 :                                 if (--GC_REFCOUNT(ref) == 0) {
    2125           0 :                                         if (Z_REFCOUNTED(ref->val) && Z_DELREF(ref->val) == 0) {
    2126           0 :                                                 zvalue = &ref->val;
    2127           0 :                                                 goto tail_call;
    2128             :                                         }
    2129             :                                 }
    2130             :                         }
    2131             :                         break;
    2132             :         }
    2133           0 : }
    2134             : 
    2135           0 : static void accel_fast_hash_destroy(HashTable *ht)
    2136             : {
    2137           0 :         Bucket *p = ht->arData;
    2138           0 :         Bucket *end = p + ht->nNumUsed;
    2139             : 
    2140           0 :         while (p != end) {
    2141           0 :                 if (Z_REFCOUNTED(p->val) && Z_DELREF(p->val) == 0) {
    2142           0 :                         accel_fast_zval_dtor(&p->val);
    2143             :                 }
    2144           0 :                 p++;
    2145             :         }
    2146           0 : }
    2147             : 
    2148           0 : static inline void zend_accel_fast_del_bucket(HashTable *ht, uint32_t idx, Bucket *p)
    2149             : {
    2150           0 :         uint32_t nIndex = p->h | ht->nTableMask;
    2151           0 :         uint32_t i = HT_HASH(ht, nIndex);
    2152             : 
    2153           0 :         ht->nNumOfElements--;
    2154           0 :         if (idx != i) {
    2155           0 :                 Bucket *prev = HT_HASH_TO_BUCKET(ht, i);
    2156           0 :                 while (Z_NEXT(prev->val) != idx) {
    2157           0 :                         i = Z_NEXT(prev->val);
    2158           0 :                         prev = HT_HASH_TO_BUCKET(ht, i);
    2159             :                 }
    2160           0 :                 Z_NEXT(prev->val) = Z_NEXT(p->val);
    2161             :         } else {
    2162           0 :                 HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
    2163             :         }
    2164           0 : }
    2165             : 
    2166           0 : static void zend_accel_fast_shutdown(void)
    2167             : {
    2168           0 :         if (EG(full_tables_cleanup)) {
    2169           0 :                 return;
    2170             :         }
    2171             : 
    2172           0 :         if (EG(objects_store).top > 1 || zend_hash_num_elements(&EG(regular_list)) > 0) {
    2173             :                 /* We don't have to destroy all zvals if they cannot call any destructors */
    2174           0 :                 zend_try {
    2175           0 :                         ZEND_HASH_REVERSE_FOREACH(&EG(symbol_table), 0) {
    2176           0 :                                 if (Z_REFCOUNTED(_p->val) && Z_DELREF(_p->val) == 0) {
    2177           0 :                                         accel_fast_zval_dtor(&_p->val);
    2178             :                                 }
    2179           0 :                                 zend_accel_fast_del_bucket(&EG(symbol_table), HT_IDX_TO_HASH(_idx-1), _p);
    2180             :                         } ZEND_HASH_FOREACH_END();
    2181           0 :                 } zend_end_try();
    2182           0 :                 zend_hash_init(&EG(symbol_table), 8, NULL, NULL, 0);
    2183             : 
    2184           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(function_table), 0) {
    2185           0 :                         zend_function *func = Z_PTR(_p->val);
    2186             : 
    2187           0 :                         if (func->type == ZEND_INTERNAL_FUNCTION) {
    2188           0 :                                 break;
    2189             :                         } else {
    2190           0 :                                 if (func->op_array.static_variables) {
    2191           0 :                                         if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
    2192           0 :                                                 if (--GC_REFCOUNT(func->op_array.static_variables) == 0) {
    2193           0 :                                                         accel_fast_hash_destroy(func->op_array.static_variables);
    2194             :                                                 }
    2195             :                                         }
    2196             :                                 }
    2197           0 :                                 zend_accel_fast_del_bucket(EG(function_table), HT_IDX_TO_HASH(_idx-1), _p);
    2198             :                         }
    2199             :                 } ZEND_HASH_FOREACH_END();
    2200             : 
    2201           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(class_table), 0) {
    2202           0 :                         zend_class_entry *ce = Z_PTR(_p->val);
    2203             : 
    2204           0 :                         if (ce->type == ZEND_INTERNAL_CLASS) {
    2205           0 :                                 break;
    2206             :                         } else {
    2207           0 :                                 if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
    2208             :                                         zend_function *func;
    2209             : 
    2210           0 :                                         ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
    2211           0 :                                                 if (func->type == ZEND_USER_FUNCTION) {
    2212           0 :                                                         if (func->op_array.static_variables) {
    2213           0 :                                                                 if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
    2214           0 :                                                                         if (--GC_REFCOUNT(func->op_array.static_variables) == 0) {
    2215           0 :                                                                                 accel_fast_hash_destroy(func->op_array.static_variables);
    2216             :                                                                         }
    2217             :                                                                 }
    2218           0 :                                                                 func->op_array.static_variables = NULL;
    2219             :                                                         }
    2220             :                                                 }
    2221             :                                         } ZEND_HASH_FOREACH_END();
    2222             :                                 }
    2223           0 :                                 if (ce->static_members_table) {
    2224             :                                         int i;
    2225             : 
    2226           0 :                                         for (i = 0; i < ce->default_static_members_count; i++) {
    2227           0 :                                                 zval *zv = &ce->static_members_table[i];
    2228           0 :                                                 ZVAL_UNDEF(&ce->static_members_table[i]);
    2229           0 :                                                 if (Z_REFCOUNTED_P(zv) && Z_DELREF_P(zv) == 0) {
    2230           0 :                                                         accel_fast_zval_dtor(zv);
    2231             :                                                 }
    2232             :                                         }
    2233           0 :                                         ce->static_members_table = NULL;
    2234             :                                 }
    2235           0 :                                 zend_accel_fast_del_bucket(EG(class_table), HT_IDX_TO_HASH(_idx-1), _p);
    2236             :                         }
    2237             :                 } ZEND_HASH_FOREACH_END();
    2238             : 
    2239             :         } else {
    2240             : 
    2241           0 :                 zend_hash_init(&EG(symbol_table), 8, NULL, NULL, 0);
    2242             : 
    2243           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(function_table), 0) {
    2244           0 :                         zend_function *func = Z_PTR(_p->val);
    2245             : 
    2246           0 :                         if (func->type == ZEND_INTERNAL_FUNCTION) {
    2247           0 :                                 break;
    2248             :                         } else {
    2249           0 :                                 zend_accel_fast_del_bucket(EG(function_table), HT_IDX_TO_HASH(_idx-1), _p);
    2250             :                         }
    2251             :                 } ZEND_HASH_FOREACH_END();
    2252             : 
    2253           0 :                 ZEND_HASH_REVERSE_FOREACH(EG(class_table), 0) {
    2254           0 :                         zend_class_entry *ce = Z_PTR(_p->val);
    2255             : 
    2256           0 :                         if (ce->type == ZEND_INTERNAL_CLASS) {
    2257           0 :                                 break;
    2258             :                         } else {
    2259           0 :                                 zend_accel_fast_del_bucket(EG(class_table), HT_IDX_TO_HASH(_idx-1), _p);
    2260             :                         }
    2261             :                 } ZEND_HASH_FOREACH_END();
    2262             :         }
    2263             : 
    2264           0 :         ZEND_HASH_REVERSE_FOREACH(EG(zend_constants), 0) {
    2265           0 :                 zend_constant *c = Z_PTR(_p->val);
    2266             : 
    2267           0 :                 if (c->flags & CONST_PERSISTENT) {
    2268           0 :                         break;
    2269             :                 } else {
    2270           0 :                         zend_accel_fast_del_bucket(EG(zend_constants), HT_IDX_TO_HASH(_idx-1), _p);
    2271             :                 }
    2272             :         } ZEND_HASH_FOREACH_END();
    2273           0 :         EG(function_table)->nNumUsed = EG(function_table)->nNumOfElements;
    2274           0 :         EG(class_table)->nNumUsed = EG(class_table)->nNumOfElements;
    2275           0 :         EG(zend_constants)->nNumUsed = EG(zend_constants)->nNumOfElements;
    2276             : 
    2277           0 :         CG(unclean_shutdown) = 1;
    2278             : }
    2279             : #endif
    2280             : 
    2281       22453 : int accel_post_deactivate(void)
    2282             : {
    2283       22453 :         if (!ZCG(enabled) || !accel_startup_ok) {
    2284       22075 :                 return SUCCESS;
    2285             :         }
    2286             : 
    2287         378 :         zend_shared_alloc_safe_unlock(); /* be sure we didn't leave cache locked */
    2288         378 :         accel_unlock_all();
    2289         378 :         ZCG(counted) = 0;
    2290             : 
    2291         378 :         return SUCCESS;
    2292             : }
    2293             : 
    2294         378 : static void accel_deactivate(void)
    2295             : {
    2296             :         /* ensure that we restore function_table and class_table
    2297             :          * In general, they're restored by persistent_compile_file(), but in case
    2298             :          * the script is aborted abnormally, they may become messed up.
    2299             :          */
    2300             : 
    2301         378 :         if (ZCG(cwd)) {
    2302          25 :                 zend_string_release(ZCG(cwd));
    2303          25 :                 ZCG(cwd) = NULL;
    2304             :         }
    2305             : 
    2306         378 :         if (!ZCG(enabled) || !accel_startup_ok) {
    2307           0 :                 return;
    2308             :         }
    2309             : 
    2310             : #if !ZEND_DEBUG
    2311         378 :         if (ZCG(accel_directives).fast_shutdown && is_zend_mm()) {
    2312           0 :                 zend_accel_fast_shutdown();
    2313             :         }
    2314             : #endif
    2315             : }
    2316             : 
    2317       22077 : static int accelerator_remove_cb(zend_extension *element1, zend_extension *element2)
    2318             : {
    2319             :         (void)element2; /* keep the compiler happy */
    2320             : 
    2321       22077 :         if (!strcmp(element1->name, ACCELERATOR_PRODUCT_NAME )) {
    2322       22077 :                 element1->startup = NULL;
    2323             : #if 0
    2324             :                 /* We have to call shutdown callback it to free TS resources */
    2325             :                 element1->shutdown = NULL;
    2326             : #endif
    2327       22077 :                 element1->activate = NULL;
    2328       22077 :                 element1->deactivate = NULL;
    2329       22077 :                 element1->op_array_handler = NULL;
    2330             : 
    2331             : #ifdef __DEBUG_MESSAGES__
    2332             :         fprintf(stderr, ACCELERATOR_PRODUCT_NAME " is disabled: %s\n", (zps_failure_reason ? zps_failure_reason : "unknown error"));
    2333             :         fflush(stderr);
    2334             : #endif
    2335             :         }
    2336             : 
    2337       22077 :         return 0;
    2338             : }
    2339             : 
    2340       22077 : static void zps_startup_failure(char *reason, char *api_reason, int (*cb)(zend_extension *, zend_extension *))
    2341             : {
    2342       22077 :         accel_startup_ok = 0;
    2343       22077 :         zps_failure_reason = reason;
    2344       22077 :         zps_api_failure_reason = api_reason?api_reason:reason;
    2345       22077 :         zend_llist_del_element(&zend_extensions, NULL, (int (*)(void *, void *))cb);
    2346       22077 : }
    2347             : 
    2348       22455 : static inline int accel_find_sapi(void)
    2349             : {
    2350             :         static const char *supported_sapis[] = {
    2351             :                 "apache",
    2352             :                 "fastcgi",
    2353             :                 "cli-server",
    2354             :                 "cgi-fcgi",
    2355             :                 "fpm-fcgi",
    2356             :                 "isapi",
    2357             :                 "apache2filter",
    2358             :                 "apache2handler",
    2359             :                 "litespeed",
    2360             :                 "uwsgi",
    2361             :                 NULL
    2362             :         };
    2363             :         const char **sapi_name;
    2364             : 
    2365       22455 :         if (sapi_module.name) {
    2366      244401 :                 for (sapi_name = supported_sapis; *sapi_name; sapi_name++) {
    2367      222318 :                         if (strcmp(sapi_module.name, *sapi_name) == 0) {
    2368         372 :                                 return SUCCESS;
    2369             :                         }
    2370             :                 }
    2371       22089 :                 if (ZCG(accel_directives).enable_cli && (
    2372           6 :                     strcmp(sapi_module.name, "cli") == 0
    2373           6 :                   || strcmp(sapi_module.name, "phpdbg") == 0)) {
    2374           6 :                         return SUCCESS;
    2375             :                 }
    2376             :         }
    2377             : 
    2378       22077 :         return FAILURE;
    2379             : }
    2380             : 
    2381         378 : static int zend_accel_init_shm(void)
    2382             : {
    2383         378 :         zend_shared_alloc_lock();
    2384             : 
    2385         378 :         accel_shared_globals = zend_shared_alloc(sizeof(zend_accel_shared_globals));
    2386         378 :         if (!accel_shared_globals) {
    2387           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
    2388           0 :                 zend_shared_alloc_unlock();
    2389           0 :                 return FAILURE;
    2390             :         }
    2391         378 :         ZSMMG(app_shared_globals) = accel_shared_globals;
    2392             : 
    2393         378 :         zend_accel_hash_init(&ZCSG(hash), ZCG(accel_directives).max_accelerated_files);
    2394             : 
    2395         378 :         ZCSG(interned_strings_start) = ZCSG(interned_strings_end) = NULL;
    2396             : # ifndef ZTS
    2397         378 :         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);
    2398         378 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2399             :                 void *data;
    2400             : 
    2401         378 :                 ZCSG(interned_strings).nTableMask = -ZCSG(interned_strings).nTableSize;
    2402         378 :                 data = zend_shared_alloc(HT_SIZE(&ZCSG(interned_strings)));
    2403         378 :                 ZCSG(interned_strings_start) = zend_shared_alloc((ZCG(accel_directives).interned_strings_buffer * 1024 * 1024));
    2404         378 :                 if (!data || !ZCSG(interned_strings_start)) {
    2405           0 :                         zend_accel_error(ACCEL_LOG_FATAL, ACCELERATOR_PRODUCT_NAME " cannot allocate buffer for interned strings");
    2406           0 :                         zend_shared_alloc_unlock();
    2407           0 :                         return FAILURE;
    2408             :                 }
    2409         378 :                 HT_SET_DATA_ADDR(&ZCSG(interned_strings), data);
    2410         378 :                 HT_HASH_RESET(&ZCSG(interned_strings));
    2411         378 :                 ZCSG(interned_strings_end)   = ZCSG(interned_strings_start) + (ZCG(accel_directives).interned_strings_buffer * 1024 * 1024);
    2412         378 :                 ZCSG(interned_strings_top)   = ZCSG(interned_strings_start);
    2413             : 
    2414             : //              orig_interned_strings_start = CG(interned_strings_start);
    2415             : //              orig_interned_strings_end = CG(interned_strings_end);
    2416             : //              CG(interned_strings_start) = ZCSG(interned_strings_start);
    2417             : //              CG(interned_strings_end) = ZCSG(interned_strings_end);
    2418             :         }
    2419             : # endif
    2420             : 
    2421         378 :         orig_new_interned_string = zend_new_interned_string;
    2422         378 :         orig_interned_strings_snapshot = zend_interned_strings_snapshot;
    2423         378 :         orig_interned_strings_restore = zend_interned_strings_restore;
    2424         378 :         zend_new_interned_string = accel_new_interned_string_for_php;
    2425         378 :         zend_interned_strings_snapshot = accel_interned_strings_snapshot_for_php;
    2426         378 :         zend_interned_strings_restore = accel_interned_strings_restore_for_php;
    2427             : 
    2428             : # ifndef ZTS
    2429         378 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2430         378 :                 accel_use_shm_interned_strings();
    2431         378 :                 accel_interned_strings_save_state();
    2432             :         }
    2433             : # endif
    2434             : 
    2435         378 :         zend_reset_cache_vars();
    2436             : 
    2437         378 :         ZCSG(oom_restarts) = 0;
    2438         378 :         ZCSG(hash_restarts) = 0;
    2439         378 :         ZCSG(manual_restarts) = 0;
    2440             : 
    2441         378 :         ZCSG(accelerator_enabled) = 1;
    2442         378 :         ZCSG(start_time) = zend_accel_get_time();
    2443         378 :         ZCSG(last_restart_time) = 0;
    2444         378 :         ZCSG(restart_in_progress) = 0;
    2445             : 
    2446         378 :         zend_shared_alloc_unlock();
    2447             : 
    2448         378 :         return SUCCESS;
    2449             : }
    2450             : 
    2451       22455 : static void accel_globals_ctor(zend_accel_globals *accel_globals)
    2452             : {
    2453             : #if defined(COMPILE_DL_OPCACHE) && defined(ZTS)
    2454             :         ZEND_TSRMLS_CACHE_UPDATE();
    2455             : #endif
    2456       22455 :         memset(accel_globals, 0, sizeof(zend_accel_globals));
    2457       22455 : }
    2458             : 
    2459      830991 : static void accel_globals_internal_func_dtor(zval *zv)
    2460             : {
    2461      830991 :         free(Z_PTR_P(zv));
    2462      830991 : }
    2463             : 
    2464       22453 : static void accel_globals_dtor(zend_accel_globals *accel_globals)
    2465             : {
    2466       22453 :         if (accel_globals->function_table.nTableSize) {
    2467         378 :                 accel_globals->function_table.pDestructor = accel_globals_internal_func_dtor;
    2468         378 :                 zend_hash_destroy(&accel_globals->function_table);
    2469             :         }
    2470       22453 : }
    2471             : 
    2472             : #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)
    2473             : 
    2474       22455 : static void accel_gen_system_id(void)
    2475             : {
    2476             :         PHP_MD5_CTX context;
    2477             :         unsigned char digest[16], c;
    2478       22455 :         char *md5str = ZCG(system_id);
    2479             :         int i;
    2480             : 
    2481       22455 :         PHP_MD5Init(&context);
    2482       22455 :         PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
    2483       22455 :         PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
    2484       22455 :         PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
    2485             :         if (strstr(PHP_VERSION, "-dev") != 0) {
    2486             :                 /* Development versions may be changed from build to build */
    2487       22455 :                 PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
    2488       22455 :                 PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
    2489             :         }
    2490       22455 :         PHP_MD5Final(digest, &context);
    2491      381735 :         for (i = 0; i < 16; i++) {
    2492      359280 :                 c = digest[i] >> 4;
    2493      359280 :                 c = (c <= 9) ? c + '0' : c - 10 + 'a';
    2494      359280 :                 md5str[i * 2] = c;
    2495      359280 :                 c = digest[i] &  0x0f;
    2496      359280 :                 c = (c <= 9) ? c + '0' : c - 10 + 'a';
    2497      359280 :                 md5str[(i * 2) + 1] = c;
    2498             :         }
    2499       22455 : }
    2500             : 
    2501             : #ifdef HAVE_HUGE_CODE_PAGES
    2502             : # ifndef _WIN32
    2503             : #  include <sys/mman.h>
    2504             : #  ifndef MAP_ANON
    2505             : #   ifdef MAP_ANONYMOUS
    2506             : #    define MAP_ANON MAP_ANONYMOUS
    2507             : #   endif
    2508             : #  endif
    2509             : #  ifndef MAP_FAILED
    2510             : #   define MAP_FAILED ((void*)-1)
    2511             : #  endif
    2512             : # endif
    2513             : 
    2514             : # if defined(MAP_HUGETLB) || defined(MADV_HUGEPAGE)
    2515           0 : static int accel_remap_huge_pages(void *start, size_t size, const char *name, size_t offset)
    2516             : {
    2517           0 :         void *ret = MAP_FAILED;
    2518             :         void *mem;
    2519             : 
    2520           0 :         mem = mmap(NULL, size,
    2521             :                 PROT_READ | PROT_WRITE,
    2522             :                 MAP_PRIVATE | MAP_ANONYMOUS,
    2523             :                 -1, 0);
    2524           0 :         if (mem == MAP_FAILED) {
    2525           0 :                 zend_error(E_WARNING,
    2526             :                         ACCELERATOR_PRODUCT_NAME " huge_code_pages: mmap failed: %s (%d)",
    2527             :                         strerror(errno), errno);
    2528           0 :                 return -1;
    2529             :         }
    2530           0 :         memcpy(mem, start, size);
    2531             : 
    2532             : #  ifdef MAP_HUGETLB
    2533           0 :         ret = mmap(start, size,
    2534             :                 PROT_READ | PROT_WRITE | PROT_EXEC,
    2535             :                 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_HUGETLB,
    2536             :                 -1, 0);
    2537             : #  endif
    2538           0 :         if (ret == MAP_FAILED) {
    2539           0 :                 ret = mmap(start, size,
    2540             :                         PROT_READ | PROT_WRITE | PROT_EXEC,
    2541             :                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
    2542             :                         -1, 0);
    2543             :                 /* this should never happen? */
    2544             :                 ZEND_ASSERT(ret != MAP_FAILED);
    2545             : #  ifdef MADV_HUGEPAGE
    2546             :                 if (-1 == madvise(start, size, MADV_HUGEPAGE)) {
    2547             :                         memcpy(start, mem, size);
    2548             :                         mprotect(start, size, PROT_READ | PROT_EXEC);
    2549             :                         munmap(mem, size);
    2550             :                         zend_error(E_WARNING,
    2551             :                                 ACCELERATOR_PRODUCT_NAME " huge_code_pages: madvise(HUGEPAGE) failed: %s (%d)",
    2552             :                                 strerror(errno), errno);
    2553             :                         return -1;
    2554             :                 }
    2555             : #  else
    2556           0 :                 memcpy(start, mem, size);
    2557           0 :                 mprotect(start, size, PROT_READ | PROT_EXEC);
    2558           0 :                 munmap(mem, size);
    2559           0 :                 zend_error(E_WARNING,
    2560             :                         ACCELERATOR_PRODUCT_NAME " huge_code_pages: mmap(HUGETLB) failed: %s (%d)",
    2561             :                         strerror(errno), errno);
    2562           0 :                 return -1;
    2563             : #  endif
    2564             :         }
    2565             : 
    2566           0 :         if (ret == start) {
    2567           0 :                 memcpy(start, mem, size);
    2568           0 :                 mprotect(start, size, PROT_READ | PROT_EXEC);
    2569             :         }
    2570           0 :         munmap(mem, size);
    2571             : 
    2572           0 :         return (ret == start) ? 0 : -1;
    2573             : }
    2574             : 
    2575           0 : static void accel_move_code_to_huge_pages(void)
    2576             : {
    2577             :         FILE *f;
    2578           0 :         long unsigned int huge_page_size = 2 * 1024 * 1024;
    2579             : 
    2580           0 :         f = fopen("/proc/self/maps", "r");
    2581           0 :         if (f) {
    2582             :                 long unsigned int  start, end, offset, inode;
    2583             :                 char perm[5], dev[6], name[MAXPATHLEN];
    2584             :                 int ret;
    2585             : 
    2586           0 :                 ret = fscanf(f, "%lx-%lx %4s %lx %5s %ld %s\n", &start, &end, perm, &offset, dev, &inode, name);
    2587           0 :                 if (ret == 7 && perm[0] == 'r' && perm[1] == '-' && perm[2] == 'x' && name[0] == '/') {
    2588           0 :                         long unsigned int  seg_start = ZEND_MM_ALIGNED_SIZE_EX(start, huge_page_size);
    2589           0 :                         long unsigned int  seg_end = (end & ~(huge_page_size-1L));
    2590             : 
    2591           0 :                         if (seg_end > seg_start) {
    2592           0 :                                 zend_accel_error(ACCEL_LOG_DEBUG, "remap to huge page %lx-%lx %s \n", seg_start, seg_end, name);
    2593           0 :                                 accel_remap_huge_pages((void*)seg_start, seg_end - seg_start, name, offset + seg_start - start);
    2594             :                         }
    2595             :                 }
    2596           0 :                 fclose(f);
    2597             :         }
    2598           0 : }
    2599             : # else
    2600             : static void accel_move_code_to_huge_pages(void)
    2601             : {
    2602             :         zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME ": opcache.huge_code_pages has no affect as huge page is not supported");
    2603             :         return;
    2604             : }
    2605             : # endif /* defined(MAP_HUGETLB) || defined(MADV_HUGEPAGE) */
    2606             : #endif /* HAVE_HUGE_CODE_PAGES */
    2607             : 
    2608       22455 : static int accel_startup(zend_extension *extension)
    2609             : {
    2610             :         zend_function *func;
    2611             :         zend_ini_entry *ini_entry;
    2612             : 
    2613             : #ifdef ZTS
    2614             :         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);
    2615             : #else
    2616       22455 :         accel_globals_ctor(&accel_globals);
    2617             : #endif
    2618             : 
    2619             : #ifdef ZEND_WIN32
    2620             :         _setmaxstdio(2048); /* The default configuration is limited to 512 stdio files */
    2621             : #endif
    2622             : 
    2623       22455 :         if (start_accel_module() == FAILURE) {
    2624           0 :                 accel_startup_ok = 0;
    2625           0 :                 zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME ": module registration failed!");
    2626           0 :                 return FAILURE;
    2627             :         }
    2628             : 
    2629       22455 :         accel_gen_system_id();
    2630             : 
    2631             : #ifdef HAVE_HUGE_CODE_PAGES
    2632       22455 :         if (ZCG(accel_directives).huge_code_pages &&
    2633           0 :             (strcmp(sapi_module.name, "cli") == 0 ||
    2634           0 :              strcmp(sapi_module.name, "cli-server") == 0 ||
    2635           0 :                  strcmp(sapi_module.name, "cgi-fcgi") == 0 ||
    2636           0 :                  strcmp(sapi_module.name, "fpm-fcgi") == 0)) {
    2637           0 :                 accel_move_code_to_huge_pages();
    2638             :         }
    2639             : #endif
    2640             : 
    2641             :         /* no supported SAPI found - disable acceleration and stop initialization */
    2642       22455 :         if (accel_find_sapi() == FAILURE) {
    2643       22077 :                 accel_startup_ok = 0;
    2644       66178 :                 if (!ZCG(accel_directives).enable_cli &&
    2645       22077 :                     strcmp(sapi_module.name, "cli") == 0) {
    2646       22024 :                         zps_startup_failure("Opcode Caching is disabled for CLI", NULL, accelerator_remove_cb);
    2647             :                 } else {
    2648          53 :                         zps_startup_failure("Opcode Caching is only supported in Apache, ISAPI, FPM, FastCGI and LiteSpeed SAPIs", NULL, accelerator_remove_cb);
    2649             :                 }
    2650       22077 :                 return SUCCESS;
    2651             :         }
    2652             : 
    2653         378 :         if (ZCG(enabled) == 0) {
    2654           0 :                 return SUCCESS ;
    2655             :         }
    2656             : 
    2657             : /********************************************/
    2658             : /* End of non-SHM dependent initializations */
    2659             : /********************************************/
    2660             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2661         378 :         if (!ZCG(accel_directives).file_cache_only) {
    2662             : #else
    2663             :         if (1) {
    2664             : #endif
    2665         378 :                 switch (zend_shared_alloc_startup(ZCG(accel_directives).memory_consumption)) {
    2666             :                         case ALLOC_SUCCESS:
    2667         378 :                                 if (zend_accel_init_shm() == FAILURE) {
    2668           0 :                                         accel_startup_ok = 0;
    2669           0 :                                         return FAILURE;
    2670             :                                 }
    2671         378 :                                 break;
    2672             :                         case ALLOC_FAILURE:
    2673           0 :                                 accel_startup_ok = 0;
    2674           0 :                                 zend_accel_error(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - probably not enough shared memory.");
    2675           0 :                                 return SUCCESS;
    2676             :                         case SUCCESSFULLY_REATTACHED:
    2677           0 :                                 zend_shared_alloc_lock();
    2678           0 :                                 accel_shared_globals = (zend_accel_shared_globals *) ZSMMG(app_shared_globals);
    2679           0 :                                 orig_new_interned_string = zend_new_interned_string;
    2680           0 :                                 orig_interned_strings_snapshot = zend_interned_strings_snapshot;
    2681           0 :                                 orig_interned_strings_restore = zend_interned_strings_restore;
    2682             : 
    2683           0 :                                 zend_new_interned_string = accel_new_interned_string_for_php;
    2684           0 :                                 zend_interned_strings_snapshot = accel_interned_strings_snapshot_for_php;
    2685           0 :                                 zend_interned_strings_restore = accel_interned_strings_restore_for_php;
    2686             : #ifndef ZTS
    2687           0 :                                 accel_use_shm_interned_strings();
    2688             : #endif
    2689           0 :                                 zend_shared_alloc_unlock();
    2690           0 :                                 break;
    2691             :                         case FAILED_REATTACHED:
    2692           0 :                                 accel_startup_ok = 0;
    2693           0 :                                 zend_accel_error(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - can not reattach to exiting shared memory.");
    2694           0 :                                 return SUCCESS;
    2695             :                                 break;
    2696             : #if ENABLE_FILE_CACHE_FALLBACK
    2697             :                         case ALLOC_FALLBACK:
    2698             :                                 zend_shared_alloc_lock();
    2699             :                                 fallback_process = 1;
    2700             :                                 zend_accel_init_auto_globals();
    2701             :                                 zend_shared_alloc_unlock();
    2702             :                                 goto file_cache_fallback;
    2703             :                                 break;
    2704             : #endif
    2705             :                 }
    2706             : 
    2707             :                 /* from this point further, shared memory is supposed to be OK */
    2708             : 
    2709             :                 /* remeber the last restart time in the process memory */
    2710         378 :                 ZCG(last_restart_time) = ZCSG(last_restart_time);
    2711             : 
    2712             :                 /* Init auto-global strings */
    2713         378 :                 zend_accel_init_auto_globals();
    2714             : 
    2715         378 :                 zend_shared_alloc_lock();
    2716         378 :                 zend_shared_alloc_save_state();
    2717         378 :                 zend_shared_alloc_unlock();
    2718             : 
    2719         378 :                 SHM_PROTECT();
    2720             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2721           0 :         } else if (!ZCG(accel_directives).file_cache) {
    2722           0 :                 accel_startup_ok = 0;
    2723           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "opcache.file_cache_only is set without a proper setting of opcache.file_cache");
    2724           0 :                 return SUCCESS;
    2725             :         } else {
    2726           0 :                 accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));
    2727             : 
    2728             :                 /* Init auto-global strings */
    2729           0 :                 zend_accel_init_auto_globals();
    2730             : #endif
    2731             :         }
    2732             : #if ENABLE_FILE_CACHE_FALLBACK
    2733             : file_cache_fallback:
    2734             : #endif
    2735             : 
    2736             :         /* Override compiler */
    2737         378 :         accelerator_orig_compile_file = zend_compile_file;
    2738         378 :         zend_compile_file = persistent_compile_file;
    2739             : 
    2740             :         /* Override stream opener function (to eliminate open() call caused by
    2741             :          * include/require statements ) */
    2742         378 :         accelerator_orig_zend_stream_open_function = zend_stream_open_function;
    2743         378 :         zend_stream_open_function = persistent_stream_open_function;
    2744             : 
    2745             :         /* Override path resolver function (to eliminate stat() calls caused by
    2746             :          * include_once/require_once statements */
    2747         378 :         accelerator_orig_zend_resolve_path = zend_resolve_path;
    2748         378 :         zend_resolve_path = persistent_zend_resolve_path;
    2749             : 
    2750             :         /* Override chdir() function */
    2751        1134 :         if ((func = zend_hash_str_find_ptr(CG(function_table), "chdir", sizeof("chdir")-1)) != NULL &&
    2752         378 :             func->type == ZEND_INTERNAL_FUNCTION) {
    2753         378 :                 orig_chdir = func->internal_function.handler;
    2754         378 :                 func->internal_function.handler = ZEND_FN(accel_chdir);
    2755             :         }
    2756         378 :         ZCG(cwd) = NULL;
    2757         378 :         ZCG(include_path) = NULL;
    2758             : 
    2759             :         /* Override "include_path" modifier callback */
    2760         756 :         if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), "include_path", sizeof("include_path")-1)) != NULL) {
    2761         378 :                 ZCG(include_path) = ini_entry->value;
    2762         378 :                 orig_include_path_on_modify = ini_entry->on_modify;
    2763         378 :                 ini_entry->on_modify = accel_include_path_on_modify;
    2764             :         }
    2765             : 
    2766         378 :         accel_startup_ok = 1;
    2767             : 
    2768             :         /* Override file_exists(), is_file() and is_readable() */
    2769         378 :         zend_accel_override_file_functions();
    2770             : 
    2771             :         /* Load black list */
    2772         378 :         accel_blacklist.entries = NULL;
    2773        1134 :         if (ZCG(enabled) && accel_startup_ok &&
    2774         378 :             ZCG(accel_directives).user_blacklist_filename &&
    2775         378 :             *ZCG(accel_directives.user_blacklist_filename)) {
    2776           0 :                 zend_accel_blacklist_init(&accel_blacklist);
    2777           0 :                 zend_accel_blacklist_load(&accel_blacklist, ZCG(accel_directives.user_blacklist_filename));
    2778             :         }
    2779             : 
    2780         378 :         zend_optimizer_startup();
    2781             : 
    2782         378 :         return SUCCESS;
    2783             : }
    2784             : 
    2785       22453 : static void accel_free_ts_resources()
    2786             : {
    2787             : #ifndef ZTS
    2788       22453 :         accel_globals_dtor(&accel_globals);
    2789             : #else
    2790             :         ts_free_id(accel_globals_id);
    2791             : #endif
    2792       22453 : }
    2793             : 
    2794       22453 : void accel_shutdown(void)
    2795             : {
    2796             :         zend_ini_entry *ini_entry;
    2797       22453 :         zend_bool file_cache_only = 0;
    2798             : 
    2799       22453 :         zend_optimizer_shutdown();
    2800             : 
    2801       22453 :         zend_accel_blacklist_shutdown(&accel_blacklist);
    2802             : 
    2803       22453 :         if (!ZCG(enabled) || !accel_startup_ok) {
    2804       22075 :                 accel_free_ts_resources();
    2805       22075 :                 return;
    2806             :         }
    2807             : 
    2808         378 :         if (ZCG(accel_directives).interned_strings_buffer) {
    2809             : #ifndef ZTS
    2810         378 :                 zend_hash_clean(CG(auto_globals));
    2811         378 :                 zend_hash_clean(CG(function_table));
    2812         378 :                 zend_hash_clean(CG(class_table));
    2813         378 :                 zend_hash_clean(EG(zend_constants));
    2814             : #endif
    2815             :         }
    2816             : 
    2817         378 :         accel_reset_pcre_cache();
    2818             : 
    2819         378 :         zend_new_interned_string = orig_new_interned_string;
    2820         378 :         zend_interned_strings_snapshot = orig_interned_strings_snapshot;
    2821         378 :         zend_interned_strings_restore = orig_interned_strings_restore;
    2822             : 
    2823             : #ifdef HAVE_OPCACHE_FILE_CACHE
    2824         378 :         file_cache_only = ZCG(accel_directives).file_cache_only;
    2825             : #endif
    2826             : 
    2827         378 :         accel_free_ts_resources();
    2828             : 
    2829         378 :         if (!file_cache_only) {
    2830         378 :                 zend_shared_alloc_shutdown();
    2831             :         }
    2832         378 :         zend_compile_file = accelerator_orig_compile_file;
    2833             : 
    2834         756 :         if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), "include_path", sizeof("include_path")-1)) != NULL) {
    2835         378 :                 ini_entry->on_modify = orig_include_path_on_modify;
    2836             :         }
    2837             : }
    2838             : 
    2839           0 : void zend_accel_schedule_restart(zend_accel_restart_reason reason)
    2840             : {
    2841           0 :         if (ZCSG(restart_pending)) {
    2842             :                 /* don't schedule twice */
    2843           0 :                 return;
    2844             :         }
    2845           0 :         zend_accel_error(ACCEL_LOG_DEBUG, "Restart Scheduled!");
    2846             : 
    2847           0 :         SHM_UNPROTECT();
    2848           0 :         ZCSG(restart_pending) = 1;
    2849           0 :         ZCSG(restart_reason) = reason;
    2850           0 :         ZCSG(cache_status_before_restart) = ZCSG(accelerator_enabled);
    2851           0 :         ZCSG(accelerator_enabled) = 0;
    2852             : 
    2853           0 :         if (ZCG(accel_directives).force_restart_timeout) {
    2854           0 :                 ZCSG(force_restart_time) = zend_accel_get_time() + ZCG(accel_directives).force_restart_timeout;
    2855             :         } else {
    2856           0 :                 ZCSG(force_restart_time) = 0;
    2857             :         }
    2858           0 :         SHM_PROTECT();
    2859             : }
    2860             : 
    2861             : /* this is needed because on WIN32 lock is not decreased unless ZCG(counted) is set */
    2862             : #ifdef ZEND_WIN32
    2863             : #define accel_deactivate_now() ZCG(counted) = 1; accel_deactivate_sub()
    2864             : #else
    2865             : #define accel_deactivate_now() accel_deactivate_sub()
    2866             : #endif
    2867             : 
    2868             : /* ensures it is OK to read SHM
    2869             :         if it's not OK (restart in progress) returns FAILURE
    2870             :         if OK returns SUCCESS
    2871             :         MUST call accelerator_shm_read_unlock after done lock operations
    2872             : */
    2873           0 : int accelerator_shm_read_lock(void)
    2874             : {
    2875           0 :         if (ZCG(counted)) {
    2876             :                 /* counted means we are holding read lock for SHM, so that nothing bad can happen */
    2877           0 :                 return SUCCESS;
    2878             :         } else {
    2879             :                 /* here accelerator is active but we do not hold SHM lock. This means restart was scheduled
    2880             :                         or is in progress now */
    2881           0 :                 accel_activate_add(); /* acquire usage lock */
    2882             :                 /* Now if we weren't inside restart, restart would not begin until we remove usage lock */
    2883           0 :                 if (ZCSG(restart_in_progress)) {
    2884             :                         /* we already were inside restart this means it's not safe to touch shm */
    2885           0 :                         accel_deactivate_now(); /* drop usage lock */
    2886           0 :                         return FAILURE;
    2887             :                 }
    2888             :         }
    2889           0 :         return SUCCESS;
    2890             : }
    2891             : 
    2892             : /* must be called ONLY after SUCCESSFUL accelerator_shm_read_lock */
    2893           0 : void accelerator_shm_read_unlock(void)
    2894             : {
    2895           0 :         if (!ZCG(counted)) {
    2896             :                 /* counted is 0 - meaning we had to readlock manually, release readlock now */
    2897           0 :                 accel_deactivate_now();
    2898             :         }
    2899           0 : }
    2900             : 
    2901             : ZEND_EXT_API zend_extension zend_extension_entry = {
    2902             :         ACCELERATOR_PRODUCT_NAME,               /* name */
    2903             :         ACCELERATOR_VERSION,                                    /* version */
    2904             :         "Zend Technologies",                                  /* author */
    2905             :         "http://www.zend.com/",                                       /* URL */
    2906             :         "Copyright (c) 1999-2016",                            /* copyright */
    2907             :         accel_startup,                                                  /* startup */
    2908             :         NULL,                                                                   /* shutdown */
    2909             :         accel_activate,                                                 /* per-script activation */
    2910             :         accel_deactivate,                                               /* per-script deactivation */
    2911             :         NULL,                                                                   /* message handler */
    2912             :         NULL,                                                                   /* op_array handler */
    2913             :         NULL,                                                                   /* extended statement handler */
    2914             :         NULL,                                                                   /* extended fcall begin handler */
    2915             :         NULL,                                                                   /* extended fcall end handler */
    2916             :         NULL,                                                                   /* op_array ctor */
    2917             :         NULL,                                                                   /* op_array dtor */
    2918             :         STANDARD_ZEND_EXTENSION_PROPERTIES
    2919             : };

Generated by: LCOV version 1.10

Generated at Thu, 26 May 2016 12:48:01 +0000 (39 hours ago)

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