xref: /PHP-8.2/ext/opcache/ZendAccelerator.h (revision 9313cd89)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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    | https://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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_ACCELERATOR_H
23 #define ZEND_ACCELERATOR_H
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define ACCELERATOR_PRODUCT_NAME	"Zend OPcache"
30 /* 2 - added Profiler support, on 20010712 */
31 /* 3 - added support for Optimizer's encoded-only-files mode */
32 /* 4 - works with the new Optimizer, that supports the file format with licenses */
33 /* 5 - API 4 didn't really work with the license-enabled file format.  v5 does. */
34 /* 6 - Monitor was removed from ZendPlatform.so, to a module of its own */
35 /* 7 - Optimizer was embedded into Accelerator */
36 /* 8 - Standalone Open Source Zend OPcache */
37 #define ACCELERATOR_API_NO 8
38 
39 #if ZEND_WIN32
40 # include "zend_config.w32.h"
41 #else
42 #include "zend_config.h"
43 # include <sys/time.h>
44 # include <sys/resource.h>
45 #endif
46 
47 #if HAVE_UNISTD_H
48 # include "unistd.h"
49 #endif
50 
51 #include "zend_extensions.h"
52 #include "zend_compile.h"
53 
54 #include "Optimizer/zend_optimizer.h"
55 #include "zend_accelerator_hash.h"
56 #include "zend_accelerator_debug.h"
57 
58 #ifndef PHPAPI
59 # ifdef ZEND_WIN32
60 #  define PHPAPI __declspec(dllimport)
61 # else
62 #  define PHPAPI
63 # endif
64 #endif
65 
66 #ifndef ZEND_EXT_API
67 # ifdef ZEND_WIN32
68 #  define ZEND_EXT_API __declspec(dllexport)
69 # elif defined(__GNUC__) && __GNUC__ >= 4
70 #  define ZEND_EXT_API __attribute__ ((visibility("default")))
71 # else
72 #  define ZEND_EXT_API
73 # endif
74 #endif
75 
76 #ifdef ZEND_WIN32
77 # ifndef MAXPATHLEN
78 #  include "win32/ioutil.h"
79 #  define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
80 # endif
81 # include <direct.h>
82 #else
83 # ifndef MAXPATHLEN
84 #  define MAXPATHLEN     4096
85 # endif
86 # include <sys/param.h>
87 #endif
88 
89 /*** file locking ***/
90 #ifndef ZEND_WIN32
91 extern int lock_file;
92 #endif
93 
94 #if defined(ZEND_WIN32)
95 # define ENABLE_FILE_CACHE_FALLBACK 1
96 #else
97 # define ENABLE_FILE_CACHE_FALLBACK 0
98 #endif
99 
100 #if ZEND_WIN32
101 typedef unsigned __int64 accel_time_t;
102 #else
103 typedef time_t accel_time_t;
104 #endif
105 
106 typedef enum _zend_accel_restart_reason {
107 	ACCEL_RESTART_OOM,    /* restart because of out of memory */
108 	ACCEL_RESTART_HASH,   /* restart because of hash overflow */
109 	ACCEL_RESTART_USER    /* restart scheduled by opcache_reset() */
110 } zend_accel_restart_reason;
111 
112 typedef struct _zend_early_binding {
113 	zend_string *lcname;
114 	zend_string *rtd_key;
115 	zend_string *lc_parent_name;
116 	uint32_t cache_slot;
117 } zend_early_binding;
118 
119 typedef struct _zend_persistent_script {
120 	zend_script    script;
121 	zend_long      compiler_halt_offset;   /* position of __HALT_COMPILER or -1 */
122 	int            ping_auto_globals_mask; /* which autoglobals are used by the script */
123 	accel_time_t   timestamp;              /* the script modification time */
124 	bool      corrupted;
125 	bool      is_phar;
126 	bool      empty;
127 	uint32_t       num_warnings;
128 	uint32_t       num_early_bindings;
129 	zend_error_info **warnings;
130 	zend_early_binding *early_bindings;
131 
132 	void          *mem;                    /* shared memory area used by script structures */
133 	size_t         size;                   /* size of used shared memory */
134 
135 	/* All entries that shouldn't be counted in the ADLER32
136 	 * checksum must be declared in this struct
137 	 */
138 	struct zend_persistent_script_dynamic_members {
139 		time_t       last_used;
140 		zend_ulong   hits;
141 		unsigned int memory_consumption;
142 		unsigned int checksum;
143 		time_t       revalidate;
144 	} dynamic_members;
145 } zend_persistent_script;
146 
147 typedef struct _zend_accel_directives {
148 	zend_long           memory_consumption;
149 	zend_long           max_accelerated_files;
150 	double         max_wasted_percentage;
151 	char          *user_blacklist_filename;
152 	zend_long           consistency_checks;
153 	zend_long           force_restart_timeout;
154 	bool      use_cwd;
155 	bool      ignore_dups;
156 	bool      validate_timestamps;
157 	bool      revalidate_path;
158 	bool      save_comments;
159 	bool      record_warnings;
160 	bool      protect_memory;
161 	bool      file_override_enabled;
162 	bool      enable_cli;
163 	bool      validate_permission;
164 #ifndef ZEND_WIN32
165 	bool      validate_root;
166 #endif
167 	zend_ulong     revalidate_freq;
168 	zend_ulong     file_update_protection;
169 	char          *error_log;
170 #ifdef ZEND_WIN32
171 	char          *mmap_base;
172 #endif
173 	char          *memory_model;
174 	zend_long           log_verbosity_level;
175 
176 	zend_long           optimization_level;
177 	zend_long           opt_debug_level;
178 	zend_long           max_file_size;
179 	zend_long           interned_strings_buffer;
180 	char          *restrict_api;
181 #ifndef ZEND_WIN32
182 	char          *lockfile_path;
183 #endif
184 	char          *file_cache;
185 	bool      file_cache_only;
186 	bool      file_cache_consistency_checks;
187 #if ENABLE_FILE_CACHE_FALLBACK
188 	bool      file_cache_fallback;
189 #endif
190 #ifdef HAVE_HUGE_CODE_PAGES
191 	bool      huge_code_pages;
192 #endif
193 	char *preload;
194 #ifndef ZEND_WIN32
195 	char *preload_user;
196 #endif
197 #ifdef ZEND_WIN32
198 	char *cache_id;
199 #endif
200 } zend_accel_directives;
201 
202 typedef struct _zend_accel_globals {
203 	bool               counted;   /* the process uses shared memory */
204 	bool               enabled;
205 	bool               locked;    /* thread obtained exclusive lock */
206 	bool               accelerator_enabled; /* accelerator enabled for current request */
207 	bool               pcre_reseted;
208 	zend_accel_directives   accel_directives;
209 	zend_string            *cwd;                  /* current working directory or NULL */
210 	zend_string            *include_path;         /* current value of "include_path" directive */
211 	char                    include_path_key[32]; /* key of current "include_path" */
212 	char                    cwd_key[32];          /* key of current working directory */
213 	int                     include_path_key_len;
214 	bool                    include_path_check;
215 	int                     cwd_key_len;
216 	bool                    cwd_check;
217 	int                     auto_globals_mask;
218 	time_t                  request_time;
219 	time_t                  last_restart_time; /* used to synchronize SHM and in-process caches */
220 	HashTable               xlat_table;
221 #ifndef ZEND_WIN32
222 	zend_ulong              root_hash;
223 #endif
224 	/* preallocated shared-memory block to save current script */
225 	void                   *mem;
226 	zend_persistent_script *current_persistent_script;
227 	/* cache to save hash lookup on the same INCLUDE opcode */
228 	const zend_op          *cache_opline;
229 	zend_persistent_script *cache_persistent_script;
230 	/* preallocated buffer for keys */
231 	zend_string             key;
232 	char                    _key[MAXPATHLEN * 8];
233 } zend_accel_globals;
234 
235 typedef struct _zend_string_table {
236 	uint32_t     nTableMask;
237 	uint32_t     nNumOfElements;
238 	zend_string *start;
239 	zend_string *top;
240 	zend_string *end;
241 	zend_string *saved_top;
242 } zend_string_table;
243 
244 typedef struct _zend_accel_shared_globals {
245 	/* Cache Data Structures */
246 	zend_ulong   hits;
247 	zend_ulong   misses;
248 	zend_ulong   blacklist_misses;
249 	zend_ulong   oom_restarts;     /* number of restarts because of out of memory */
250 	zend_ulong   hash_restarts;    /* number of restarts because of hash overflow */
251 	zend_ulong   manual_restarts;  /* number of restarts scheduled by opcache_reset() */
252 	zend_accel_hash hash;             /* hash table for cached scripts */
253 
254 	size_t map_ptr_last;
255 
256 	/* Directives & Maintenance */
257 	time_t          start_time;
258 	time_t          last_restart_time;
259 	time_t          force_restart_time;
260 	bool       accelerator_enabled;
261 	bool       restart_pending;
262 	zend_accel_restart_reason restart_reason;
263 	bool       cache_status_before_restart;
264 #ifdef ZEND_WIN32
265 	LONGLONG   mem_usage;
266 	LONGLONG   restart_in;
267 #endif
268 	bool       restart_in_progress;
269 
270 	/* Preloading */
271 	zend_persistent_script *preload_script;
272 	zend_persistent_script **saved_scripts;
273 
274 	/* uninitialized HashTable Support */
275 	uint32_t uninitialized_bucket[-HT_MIN_MASK];
276 
277 	/* Tracing JIT */
278 	void *jit_traces;
279 	const void **jit_exit_groups;
280 
281 	/* Interned Strings Support (must be the last element) */
282 	zend_string_table interned_strings;
283 } zend_accel_shared_globals;
284 
285 #ifdef ZEND_WIN32
286 extern char accel_uname_id[32];
287 #endif
288 extern bool accel_startup_ok;
289 extern bool file_cache_only;
290 #if ENABLE_FILE_CACHE_FALLBACK
291 extern bool fallback_process;
292 #endif
293 
294 extern zend_accel_shared_globals *accel_shared_globals;
295 #define ZCSG(element)   (accel_shared_globals->element)
296 
297 #ifdef ZTS
298 # define ZCG(v)	ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v)
299 extern int accel_globals_id;
300 # ifdef COMPILE_DL_OPCACHE
301 ZEND_TSRMLS_CACHE_EXTERN()
302 # endif
303 #else
304 # define ZCG(v) (accel_globals.v)
305 extern zend_accel_globals accel_globals;
306 #endif
307 
308 extern char *zps_api_failure_reason;
309 
310 BEGIN_EXTERN_C()
311 
312 void accel_shutdown(void);
313 zend_result  accel_activate(INIT_FUNC_ARGS);
314 zend_result accel_post_deactivate(void);
315 void zend_accel_schedule_restart(zend_accel_restart_reason reason);
316 void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason);
317 accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);
318 int  validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
319 int  validate_timestamp_and_record_ex(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
320 int  zend_accel_invalidate(zend_string *filename, bool force);
321 int  accelerator_shm_read_lock(void);
322 void accelerator_shm_read_unlock(void);
323 
324 zend_string *accel_make_persistent_key(zend_string *path);
325 zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);
326 
327 #define IS_ACCEL_INTERNED(str) \
328 	((char*)(str) >= (char*)ZCSG(interned_strings).start && (char*)(str) < (char*)ZCSG(interned_strings).top)
329 
330 zend_string* ZEND_FASTCALL accel_new_interned_string(zend_string *str);
331 
332 uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name);
333 
334 END_EXTERN_C()
335 
336 /* memory write protection */
337 #define SHM_PROTECT() \
338 	do { \
339 		if (ZCG(accel_directives).protect_memory) { \
340 			zend_accel_shared_protect(1); \
341 		} \
342 	} while (0)
343 
344 #define SHM_UNPROTECT() \
345 	do { \
346 		if (ZCG(accel_directives).protect_memory) { \
347 			zend_accel_shared_protect(0); \
348 		} \
349 	} while (0)
350 
351 #endif /* ZEND_ACCELERATOR_H */
352