Skip to content

Commit

Permalink
Remove shared globals, add string fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nanosonde committed May 17, 2018
1 parent d72fb8c commit a974429
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 67 deletions.
21 changes: 2 additions & 19 deletions ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,12 @@ ZEND_TSRMLS_CACHE_DEFINE()
#endif
#endif

/* Points to the structure shared across all PHP processes */
zend_accel_shared_globals *accel_shared_globals = NULL;

/* true globals, no need for thread safety */
zend_bool accel_startup_ok = 0;
static char *zps_failure_reason = NULL;
char *zps_api_failure_reason = NULL;

static zend_op_array *(*accelerator_orig_compile_file)(zend_file_handle *file_handle, int type);
static int (*accelerator_orig_zend_stream_open_function)(const char *filename, zend_file_handle *handle );

static void accel_gen_system_id(void);

Expand Down Expand Up @@ -243,7 +239,7 @@ static zend_persistent_script *bcgen_compile_file(zend_file_handle *file_handle,

/* Try to open file */
if (file_handle->type == ZEND_HANDLE_FILENAME) {
if (accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) != SUCCESS) {
if (zend_stream_open_function(file_handle->filename, file_handle) != SUCCESS) {
if (type == ZEND_REQUIRE) {
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
zend_bailout();
Expand Down Expand Up @@ -336,7 +332,7 @@ zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)

if (!file_handle->opened_path) {
if (file_handle->type == ZEND_HANDLE_FILENAME &&
accelerator_orig_zend_stream_open_function(file_handle->filename, file_handle) == FAILURE) {
zend_stream_open_function(file_handle->filename, file_handle) == FAILURE) {
if (type == ZEND_REQUIRE) {
zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, file_handle->filename);
zend_bailout();
Expand Down Expand Up @@ -388,12 +384,6 @@ void persistent_compile_file(zend_file_handle *file_handle, int type)
}
}

/* zend_stream_open_function() replacement for PHP 5.3 and above */
static int persistent_stream_open_function(const char *filename, zend_file_handle *handle)
{
return accelerator_orig_zend_stream_open_function(filename, handle);
}

static void accel_activate(void)
{
if (!ZCG(enabled) || !accel_startup_ok) {
Expand Down Expand Up @@ -694,20 +684,13 @@ static int accel_startup(zend_extension *extension)
/* End of non-SHM dependent initializations */
/********************************************/

accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));

/* Init auto-global strings */
zend_accel_init_auto_globals();

/* Override compiler */
accelerator_orig_compile_file = zend_compile_file;
zend_compile_file = persistent_load_file;

/* Override stream opener function (to eliminate open() call caused by
* include/require statements ) */
accelerator_orig_zend_stream_open_function = zend_stream_open_function;
zend_stream_open_function = persistent_stream_open_function;

accel_startup_ok = 1;

zend_optimizer_startup();
Expand Down
17 changes: 1 addition & 16 deletions ZendAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,8 @@ typedef struct _zend_accel_globals {
zend_persistent_script *current_persistent_script;
} zend_accel_globals;

typedef struct _zend_accel_shared_globals {
/* Interned Strings Support */
char *interned_strings_start;
char *interned_strings_top;
char *interned_strings_end;
char *interned_strings_saved_top;
HashTable interned_strings;
/* uninitialized HashTable Support */
uint32_t uninitialized_bucket[-HT_MIN_MASK];
} zend_accel_shared_globals;

extern zend_bool accel_startup_ok;

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;
Expand All @@ -171,7 +157,6 @@ int accel_post_deactivate(void);
zend_op_array *persistent_load_file(zend_file_handle *file_handle, int type);
void 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))
#define IS_ACCEL_INTERNED(str) (0)

#endif /* ZEND_ACCELERATOR_H */
3 changes: 2 additions & 1 deletion zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ static ZEND_FUNCTION(bcgen_compile_file)

ZCG(outfilename) = emalloc(outscript_name_len + 1);
memcpy(ZCG(outfilename), outscript_name, outscript_name_len);
ZCG(outfilename)[outscript_name_len + 1] = '\0';
ZCG(outfilename)[outscript_name_len] = '\0';
zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " len is %d, %d, %s", outscript_name_len, strlen (ZCG(outfilename)), ZCG(outfilename));

handle.filename = script_name;
handle.free_filename = 0;
Expand Down
50 changes: 23 additions & 27 deletions zend_file_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ static void zend_file_cache_serialize(zend_persistent_script *script,
{
zend_persistent_script *new_script;

memcpy(info->magic, "BCGEN", 6);
memcpy(info->magic, "BCGEN\0\0\0", 8);
memcpy(info->system_id, ZCG(system_id), 32);
info->mem_size = script->size;
info->str_size = 0;
Expand All @@ -647,30 +647,25 @@ static void zend_file_cache_serialize(zend_persistent_script *script,
int zend_file_cache_script_store(zend_persistent_script *script)
{
int fd;
char *filename;
zend_file_cache_metainfo info;
#ifdef HAVE_SYS_UIO_H
struct iovec vec[3];
#endif
void *mem, *buf;

size_t len;
len = strlen(ZCG(outfilename));
filename = emalloc(len);
memcpy(filename, ZCG(outfilename), len);

zend_accel_error(ACCEL_LOG_WARNING, "writing to file '%s'\n", filename);
zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " len is %d, %s", strlen (ZCG(outfilename)), ZCG(outfilename));
zend_accel_error(ACCEL_LOG_WARNING, "writing to file '%s'\n", ZCG(outfilename));

#ifndef ZEND_WIN32
fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
fd = open(ZCG(outfilename), O_CREAT | O_TRUNC | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
#else
fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_BINARY, _S_IREAD | _S_IWRITE);
fd = open(ZCG(outfilename), O_CREAT | O_TRUNC | O_RDWR | O_BINARY, _S_IREAD | _S_IWRITE);
#endif
if (fd < 0) {
if (errno != EEXIST) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot create file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot create file '%s'\n", ZCG(outfilename));
}
efree(filename);
efree(ZCG(outfilename));
return FAILURE;
}

Expand Down Expand Up @@ -700,12 +695,12 @@ int zend_file_cache_script_store(zend_persistent_script *script)
vec[2].iov_len = info.str_size;

if (writev(fd, vec, 3) != (ssize_t)(sizeof(info) + script->size + info.str_size)) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot write to file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot write to file '%s'\n", ZCG(outfilename));
zend_string_release((zend_string*)ZCG(mem));
close(fd);
efree(mem);
unlink(filename);
efree(filename);
unlink(ZCG(outfilename));
efree(ZCG(outfilename));
return FAILURE;
}
#else
Expand All @@ -714,20 +709,20 @@ int zend_file_cache_script_store(zend_persistent_script *script)
write(fd, buf, script->size) != script->size ||
write(fd, ((zend_string*)ZCG(mem))->val, info.str_size) != info.str_size
) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot write to file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot write to file '%s'\n", ZCG(outfilename));
zend_string_release((zend_string*)ZCG(mem));
close(fd);
efree(mem);
unlink(filename);
efree(filename);
unlink(ZCG(outfilename));
efree(ZCG(outfilename));
return FAILURE;
}
#endif

zend_string_release((zend_string*)ZCG(mem));
efree(mem);
close(fd);
efree(filename);
efree(ZCG(outfilename));

return SUCCESS;
}
Expand Down Expand Up @@ -1191,8 +1186,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl

size_t len;
len = ZSTR_LEN(full_path);
filename = emalloc(len);
memcpy(filename, ZSTR_VAL(full_path), len);
filename = emalloc(len + 1);
memcpy(filename, ZSTR_VAL(full_path), len);
filename[len] = '\0';
//filename = zend_file_cache_get_bin_file_path(full_path);

fd = open(filename, O_RDONLY | O_BINARY);
Expand All @@ -1202,21 +1198,21 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
}

if (read(fd, &info, sizeof(info)) != sizeof(info)) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot read from file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_DEBUG, "bcgen: '%s' is not a bcgen file (wrong meta info size)\n", filename);
close(fd);
efree(filename);
return NULL;
}

/* verify header */
if (memcmp(info.magic, "BCGEN", 6) != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot read from file '%s' (wrong header)\n", filename);
if (memcmp(info.magic, "BCGEN\0\0\0", 8) != 0) {
zend_accel_error(ACCEL_LOG_DEBUG, "bcgen: '%s' is not a bcgen file (wrong header)\n", filename);
close(fd);
efree(filename);
return NULL;
}
if (memcmp(info.system_id, ZCG(system_id), 32) != 0) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot read from file '%s' (wrong \"system_id\")\n", filename);
zend_accel_error(ACCEL_LOG_DEBUG, "bcgen '%s' is not a bcgen file (wrong \"system_id\")\n", filename);
close(fd);
efree(filename);
return NULL;
Expand All @@ -1232,7 +1228,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
#endif

if (read(fd, mem, info.mem_size + info.str_size) != (ssize_t)(info.mem_size + info.str_size)) {
zend_accel_error(ACCEL_LOG_WARNING, "bcgen cannot read from file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_DEBUG, "bcgen: cannot read from file '%s' (size mismatch)\n", filename);
close(fd);
zend_arena_release(&CG(arena), checkpoint);
efree(filename);
Expand All @@ -1243,7 +1239,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
/* verify checksum */
if (ZCG(accel_directives).file_consistency_checks &&
zend_adler32(ADLER32_INIT, mem, info.mem_size + info.str_size) != info.checksum) {
zend_accel_error(ACCEL_LOG_WARNING, "corrupted file '%s'\n", filename);
zend_accel_error(ACCEL_LOG_WARNING, "bcgen: corrupted file '%s' (checksum invalid)\n", filename);
zend_arena_release(&CG(arena), checkpoint);
efree(filename);
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions zend_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ static void zend_hash_persist(HashTable *ht, zend_persist_func_t pPersistElement
ht->pDestructor = NULL;

if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
return;
}
if (ht->nNumUsed == 0) {
efree(HT_GET_DATA_ADDR(ht));
ht->nTableMask = HT_MIN_MASK;
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
ht->u.flags &= ~HASH_FLAG_INITIALIZED;
return;
}
Expand Down Expand Up @@ -167,13 +167,13 @@ static void zend_hash_persist_immutable(HashTable *ht)
ht->pDestructor = NULL;

if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
return;
}
if (ht->nNumUsed == 0) {
efree(HT_GET_DATA_ADDR(ht));
ht->nTableMask = HT_MIN_MASK;
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
ht->u.flags &= ~HASH_FLAG_INITIALIZED;
return;
}
Expand Down

0 comments on commit a974429

Please sign in to comment.