mirror of
https://github.com/php/php-src.git
synced 2026-03-30 04:02:19 +02:00
Since long the default PHP charset is UTF-8, however the Windows part is out of step with this important point. The current implementation in PHP doesn't technically permit to handle UTF-8 filepath and several other things. Till now, only the ANSI compatible APIs are being used. Here is more about it https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx The patch fixes not only issues with multibyte filenames under incompatible codepages, but indirectly also issues with some other multibyte encodings like BIG5, Shift-JIS, etc. by providing a clean way to access filenames in UTF-8. Below is a small list of issues from the bug tracker, that are getting fixed: https://bugs.php.net/63401 https://bugs.php.net/41199 https://bugs.php.net/50203 https://bugs.php.net/71509 https://bugs.php.net/64699 https://bugs.php.net/64506 https://bugs.php.net/30195 https://bugs.php.net/65358 https://bugs.php.net/61315 https://bugs.php.net/70943 https://bugs.php.net/70903 https://bugs.php.net/63593 https://bugs.php.net/54977 https://bugs.php.net/54028 https://bugs.php.net/43148 https://bugs.php.net/30730 https://bugs.php.net/33350 https://bugs.php.net/35300 https://bugs.php.net/46990 https://bugs.php.net/61309 https://bugs.php.net/69333 https://bugs.php.net/45517 https://bugs.php.net/70551 https://bugs.php.net/50197 https://bugs.php.net/72200 https://bugs.php.net/37672 Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow and Github. Some of the bugs are pretty recent, some descend to early 2000th, but the user comments in there last even till today. Just for example, bug #30195 was opened in 2004, the latest comment in there was made in 2014. It is certain, that these bugs descend not only to pure PHP use cases, but get also redirected from the popular PHP based projects. Given the modern systems (and those supported by PHP) are always based on NTFS, there is no excuse to keep these issues unresolved. The internalization approach on Windows is in many ways different from UNIX and Linux, while it supports and is based on Unicode. It depends on the current system code page, APIs used and exact kind how the binary was compiled The locale doesn't affect the way Unicode or ANSI API work. PHP in particular is being compiled without _UNICODE defined and this is conditioned by the way we handle strings. Here is more about it https://msdn.microsoft.com/en-us/library/tsbaswba.aspx However, with any system code page ANSI functions automatically convert paths to UTF-16. Paths in some encodings incompatible with the current system code page, won't work correctly with ANSI APIs. PHP till now only uses the ANSI Windows APIs. For example, on a system with the current code page 1252, the paths in cp1252 are supported and transparently converted to UTF-16 by the ANSI functions. Once one wants to handle a filepath encoded with cp932 on that particular system, an ANSI or a POSIX compatible function used in PHP will produce an erroneous result. When trying to convert that cp932 path to UTF-8 and passing to the ANSI functions, an ANSI function would likely interpret the UTF-8 string as some string in the current code page and create a filepath that represents every single byte of the UTF-8 string. These behaviors are not only broken but also disregard the documented INI settings. This patch solves the issies with the multibyte paths on Windows by intelligently enforcing the usage of the Unicode aware APIs. For functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments will be converted to UTF-16 wide chars. For functions returning Unicode aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is converted back to char's depending on the current PHP charset settings, either to the current ANSI codepage (this is the behavior prior to this patch) or to UTF-8 (the default behavior). In a particular case, users might have to explicitly set internal_encoding or default_charset, if filenames in ANSI codepage are necessary. Current tests show no regressions and witness that this will be an exotic case, the current default UTF-8 encoding is compatible with any supported system. The dependency libraries are long switching to Unicode APIs, so some tests were also added for extensions not directly related to streams. At large, the patch brings over 150 related tests into the core. Those target and was run on various environments with European, Asian, etc. codepages. General PHP frameworks was tested and showed no regressions. The impact on the current C code base is low, the most places affected are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c and plain_wrapper.c. The actual implementation of the most of the wide char supporting functionality is in win32/ioutil.* and win32/codepage.*, several low level functionsare extended in place to avoid reimplementation for now. No performance impact was sighted. As previously mentioned, the ANSI APIs used prior the patch perform Unicode conversions internally. Using the Unicode APIs directly while doing custom conversions just retains the status quo. The ways to optimize it are open (fe. by implementing caching for the strings converted to wide variants). The long path implementation is user transparent. If a path exceeds the length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN is set to 2048 bytes. Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas and testing. Thanks.
319 lines
11 KiB
C
319 lines
11 KiB
C
/*
|
|
+----------------------------------------------------------------------+
|
|
| Zend OPcache |
|
|
+----------------------------------------------------------------------+
|
|
| Copyright (c) 1998-2016 The PHP Group |
|
|
+----------------------------------------------------------------------+
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
| available through the world-wide-web at the following url: |
|
|
| http://www.php.net/license/3_01.txt |
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
+----------------------------------------------------------------------+
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
| Stanislav Malyshev <stas@zend.com> |
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
|
+----------------------------------------------------------------------+
|
|
*/
|
|
|
|
#ifndef ZEND_ACCELERATOR_H
|
|
#define ZEND_ACCELERATOR_H
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#define ACCELERATOR_PRODUCT_NAME "Zend OPcache"
|
|
/* 2 - added Profiler support, on 20010712 */
|
|
/* 3 - added support for Optimizer's encoded-only-files mode */
|
|
/* 4 - works with the new Optimizer, that supports the file format with licenses */
|
|
/* 5 - API 4 didn't really work with the license-enabled file format. v5 does. */
|
|
/* 6 - Monitor was removed from ZendPlatform.so, to a module of its own */
|
|
/* 7 - Optimizer was embedded into Accelerator */
|
|
/* 8 - Standalone Open Source Zend OPcache */
|
|
#define ACCELERATOR_API_NO 8
|
|
|
|
#if ZEND_WIN32
|
|
# include "zend_config.w32.h"
|
|
#else
|
|
#include "zend_config.h"
|
|
# include <sys/time.h>
|
|
# include <sys/resource.h>
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
|
# include "unistd.h"
|
|
#endif
|
|
|
|
#include "zend_extensions.h"
|
|
#include "zend_compile.h"
|
|
|
|
#include "Optimizer/zend_optimizer.h"
|
|
#include "zend_accelerator_hash.h"
|
|
#include "zend_accelerator_debug.h"
|
|
|
|
#ifndef PHPAPI
|
|
# ifdef ZEND_WIN32
|
|
# define PHPAPI __declspec(dllimport)
|
|
# else
|
|
# define PHPAPI
|
|
# endif
|
|
#endif
|
|
|
|
#ifndef ZEND_EXT_API
|
|
# if WIN32|WINNT
|
|
# define ZEND_EXT_API __declspec(dllexport)
|
|
# elif defined(__GNUC__) && __GNUC__ >= 4
|
|
# define ZEND_EXT_API __attribute__ ((visibility("default")))
|
|
# else
|
|
# define ZEND_EXT_API
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef ZEND_WIN32
|
|
# ifndef MAXPATHLEN
|
|
# include "win32/ioutil.h"
|
|
# define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
|
|
# endif
|
|
# include <direct.h>
|
|
#else
|
|
# ifndef MAXPATHLEN
|
|
# define MAXPATHLEN 4096
|
|
# endif
|
|
# include <sys/param.h>
|
|
#endif
|
|
|
|
/*** file locking ***/
|
|
#ifndef ZEND_WIN32
|
|
extern int lock_file;
|
|
|
|
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (defined(__APPLE__) && defined(__MACH__)/* Darwin */) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {start, len, -1, type, whence}
|
|
# elif defined(__svr4__)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {type, whence, start, len}
|
|
# elif defined(__linux__) || defined(__hpux) || defined(__GNU__)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {type, whence, start, len, 0}
|
|
# elif defined(_AIX)
|
|
# if defined(_LARGE_FILES) || defined(__64BIT__)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {type, whence, 0, 0, 0, start, len }
|
|
# else
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {type, whence, start, len}
|
|
# endif
|
|
# elif defined(HAVE_FLOCK_BSD)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {start, len, -1, type, whence}
|
|
# elif defined(HAVE_FLOCK_LINUX)
|
|
# define FLOCK_STRUCTURE(name, type, whence, start, len) \
|
|
struct flock name = {type, whence, start, len}
|
|
# else
|
|
# error "Don't know how to define struct flock"
|
|
# endif
|
|
#endif
|
|
|
|
#if defined(HAVE_OPCACHE_FILE_CACHE) && defined(ZEND_WIN32)
|
|
# define ENABLE_FILE_CACHE_FALLBACK 1
|
|
#endif
|
|
|
|
#if ZEND_WIN32
|
|
typedef unsigned __int64 accel_time_t;
|
|
#else
|
|
typedef time_t accel_time_t;
|
|
#endif
|
|
|
|
typedef enum _zend_accel_restart_reason {
|
|
ACCEL_RESTART_OOM, /* restart because of out of memory */
|
|
ACCEL_RESTART_HASH, /* restart because of hash overflow */
|
|
ACCEL_RESTART_USER /* restart scheduled by opcache_reset() */
|
|
} zend_accel_restart_reason;
|
|
|
|
typedef struct _zend_persistent_script {
|
|
zend_script script;
|
|
zend_long compiler_halt_offset; /* position of __HALT_COMPILER or -1 */
|
|
int ping_auto_globals_mask; /* which autoglobals are used by the script */
|
|
accel_time_t timestamp; /* the script modification time */
|
|
zend_bool corrupted;
|
|
zend_bool is_phar;
|
|
|
|
void *mem; /* shared memory area used by script structures */
|
|
size_t size; /* size of used shared memory */
|
|
void *arena_mem; /* part that should be copied into process */
|
|
size_t arena_size;
|
|
|
|
/* All entries that shouldn't be counted in the ADLER32
|
|
* checksum must be declared in this struct
|
|
*/
|
|
struct zend_persistent_script_dynamic_members {
|
|
time_t last_used;
|
|
#ifdef ZEND_WIN32
|
|
LONGLONG hits;
|
|
#else
|
|
zend_ulong hits;
|
|
#endif
|
|
unsigned int memory_consumption;
|
|
unsigned int checksum;
|
|
time_t revalidate;
|
|
} dynamic_members;
|
|
} zend_persistent_script;
|
|
|
|
typedef struct _zend_accel_directives {
|
|
zend_long memory_consumption;
|
|
zend_long max_accelerated_files;
|
|
double max_wasted_percentage;
|
|
char *user_blacklist_filename;
|
|
zend_long consistency_checks;
|
|
zend_long force_restart_timeout;
|
|
zend_bool use_cwd;
|
|
zend_bool ignore_dups;
|
|
zend_bool validate_timestamps;
|
|
zend_bool revalidate_path;
|
|
zend_bool save_comments;
|
|
zend_bool fast_shutdown;
|
|
zend_bool protect_memory;
|
|
zend_bool file_override_enabled;
|
|
zend_bool inherited_hack;
|
|
zend_bool enable_cli;
|
|
zend_ulong revalidate_freq;
|
|
zend_ulong file_update_protection;
|
|
char *error_log;
|
|
#ifdef ZEND_WIN32
|
|
char *mmap_base;
|
|
#endif
|
|
char *memory_model;
|
|
zend_long log_verbosity_level;
|
|
|
|
zend_long optimization_level;
|
|
zend_long opt_debug_level;
|
|
zend_long max_file_size;
|
|
zend_long interned_strings_buffer;
|
|
char *restrict_api;
|
|
#ifndef ZEND_WIN32
|
|
char *lockfile_path;
|
|
#endif
|
|
#ifdef HAVE_OPCACHE_FILE_CACHE
|
|
char *file_cache;
|
|
zend_bool file_cache_only;
|
|
zend_bool file_cache_consistency_checks;
|
|
#endif
|
|
#if ENABLE_FILE_CACHE_FALLBACK
|
|
zend_bool file_cache_fallback;
|
|
#endif
|
|
#ifdef HAVE_HUGE_CODE_PAGES
|
|
zend_bool huge_code_pages;
|
|
#endif
|
|
} zend_accel_directives;
|
|
|
|
typedef struct _zend_accel_globals {
|
|
/* copy of CG(function_table) used for compilation scripts into cache */
|
|
/* initially it contains only internal functions */
|
|
HashTable function_table;
|
|
int internal_functions_count;
|
|
int counted; /* the process uses shared memory */
|
|
zend_bool enabled;
|
|
zend_bool locked; /* thread obtained exclusive lock */
|
|
HashTable bind_hash; /* prototype and zval lookup table */
|
|
zend_accel_directives accel_directives;
|
|
zend_string *cwd; /* current working directory or NULL */
|
|
zend_string *include_path; /* current value of "include_path" directive */
|
|
char include_path_key[32]; /* key of current "include_path" */
|
|
char cwd_key[32]; /* key of current working directory */
|
|
int include_path_key_len;
|
|
int include_path_check;
|
|
int cwd_key_len;
|
|
int cwd_check;
|
|
int auto_globals_mask;
|
|
time_t request_time;
|
|
time_t last_restart_time; /* used to synchronize SHM and in-process caches */
|
|
char system_id[32];
|
|
HashTable xlat_table;
|
|
/* preallocated shared-memory block to save current script */
|
|
void *mem;
|
|
void *arena_mem;
|
|
zend_persistent_script *current_persistent_script;
|
|
/* cache to save hash lookup on the same INCLUDE opcode */
|
|
const zend_op *cache_opline;
|
|
zend_persistent_script *cache_persistent_script;
|
|
/* preallocated buffer for keys */
|
|
int key_len;
|
|
char key[MAXPATHLEN * 8];
|
|
} zend_accel_globals;
|
|
|
|
typedef struct _zend_accel_shared_globals {
|
|
/* Cache Data Structures */
|
|
zend_ulong hits;
|
|
zend_ulong misses;
|
|
zend_ulong blacklist_misses;
|
|
zend_ulong oom_restarts; /* number of restarts because of out of memory */
|
|
zend_ulong hash_restarts; /* number of restarts because of hash overflow */
|
|
zend_ulong manual_restarts; /* number of restarts scheduled by opcache_reset() */
|
|
zend_accel_hash hash; /* hash table for cached scripts */
|
|
|
|
/* Directives & Maintenance */
|
|
time_t start_time;
|
|
time_t last_restart_time;
|
|
time_t force_restart_time;
|
|
zend_bool accelerator_enabled;
|
|
zend_bool restart_pending;
|
|
zend_accel_restart_reason restart_reason;
|
|
zend_bool cache_status_before_restart;
|
|
#ifdef ZEND_WIN32
|
|
LONGLONG mem_usage;
|
|
LONGLONG restart_in;
|
|
#endif
|
|
zend_bool restart_in_progress;
|
|
/* Interned Strings Support */
|
|
char *interned_strings_start;
|
|
char *interned_strings_top;
|
|
char *interned_strings_end;
|
|
char *interned_strings_saved_top;
|
|
HashTable interned_strings;
|
|
} zend_accel_shared_globals;
|
|
|
|
extern zend_bool accel_startup_ok;
|
|
#if ENABLE_FILE_CACHE_FALLBACK
|
|
extern zend_bool fallback_process;
|
|
#endif
|
|
|
|
extern zend_accel_shared_globals *accel_shared_globals;
|
|
#define ZCSG(element) (accel_shared_globals->element)
|
|
|
|
#ifdef ZTS
|
|
# define ZCG(v) ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v)
|
|
extern int accel_globals_id;
|
|
# ifdef COMPILE_DL_OPCACHE
|
|
ZEND_TSRMLS_CACHE_EXTERN()
|
|
# endif
|
|
#else
|
|
# define ZCG(v) (accel_globals.v)
|
|
extern zend_accel_globals accel_globals;
|
|
#endif
|
|
|
|
extern char *zps_api_failure_reason;
|
|
|
|
void accel_shutdown(void);
|
|
int accel_post_deactivate(void);
|
|
void zend_accel_schedule_restart(zend_accel_restart_reason reason);
|
|
void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason);
|
|
accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);
|
|
int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
|
|
int zend_accel_invalidate(const char *filename, int filename_len, zend_bool force);
|
|
int accelerator_shm_read_lock(void);
|
|
void accelerator_shm_read_unlock(void);
|
|
|
|
char *accel_make_persistent_key(const char *path, int path_length, int *key_len);
|
|
zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);
|
|
|
|
#define IS_ACCEL_INTERNED(str) \
|
|
((char*)(str) >= ZCSG(interned_strings_start) && (char*)(str) < ZCSG(interned_strings_end))
|
|
|
|
zend_string *accel_new_interned_string(zend_string *str);
|
|
|
|
#endif /* ZEND_ACCELERATOR_H */
|