Skip to content

Commit 50949c9

Browse files
committed
Replaced usafe reference from SHM to process memory with SHM to SHM reference.
1 parent b711a96 commit 50949c9

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,8 @@ static zend_persistent_script *store_script_in_file_cache(zend_persistent_script
12351235
ZCG(mem) = zend_arena_alloc(&CG(arena), memory_used);
12361236
#endif
12371237

1238-
/* Copy into shared memory */
1239-
new_persistent_script = zend_accel_script_persist(new_persistent_script, NULL, 0);
1238+
/* Copy into memory block */
1239+
new_persistent_script = zend_accel_script_persist(new_persistent_script, NULL, 0, 0);
12401240

12411241
zend_shared_alloc_destroy_xlat_table();
12421242

@@ -1355,7 +1355,7 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
13551355
}
13561356

13571357
/* Copy into shared memory */
1358-
new_persistent_script = zend_accel_script_persist(new_persistent_script, &key, key_length);
1358+
new_persistent_script = zend_accel_script_persist(new_persistent_script, &key, key_length, 1);
13591359

13601360
zend_shared_alloc_destroy_xlat_table();
13611361

@@ -2526,6 +2526,8 @@ static inline int accel_find_sapi(void)
25262526

25272527
static int zend_accel_init_shm(void)
25282528
{
2529+
int i;
2530+
25292531
zend_shared_alloc_lock();
25302532

25312533
accel_shared_globals = zend_shared_alloc(sizeof(zend_accel_shared_globals));
@@ -2589,6 +2591,11 @@ static int zend_accel_init_shm(void)
25892591
ZCSG(last_restart_time) = 0;
25902592
ZCSG(restart_in_progress) = 0;
25912593

2594+
2595+
for (i = 0; i < -HT_MIN_MASK; i++) {
2596+
ZCSG(uninitialized_bucket)[i] = HT_INVALID_IDX;
2597+
}
2598+
25922599
zend_shared_alloc_unlock();
25932600

25942601
return SUCCESS;

ext/opcache/ZendAccelerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ typedef struct _zend_accel_shared_globals {
283283
char *interned_strings_end;
284284
char *interned_strings_saved_top;
285285
HashTable interned_strings;
286+
/* uninitialized HashTable Support */
287+
uint32_t uninitialized_bucket[-HT_MIN_MASK];
286288
} zend_accel_shared_globals;
287289

288290
extern zend_bool accel_startup_ok;

ext/opcache/zend_persist.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,21 @@ static void zend_hash_persist(HashTable *ht, zend_persist_func_t pPersistElement
8989
ht->pDestructor = NULL;
9090

9191
if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
92-
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
92+
if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
93+
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
94+
} else {
95+
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
96+
}
9397
return;
9498
}
9599
if (ht->nNumUsed == 0) {
96100
efree(HT_GET_DATA_ADDR(ht));
97101
ht->nTableMask = HT_MIN_MASK;
98-
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
102+
if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
103+
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
104+
} else {
105+
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
106+
}
99107
ht->u.flags &= ~HASH_FLAG_INITIALIZED;
100108
return;
101109
}
@@ -175,13 +183,21 @@ static void zend_hash_persist_immutable(HashTable *ht)
175183
ht->pDestructor = NULL;
176184

177185
if (!(ht->u.flags & HASH_FLAG_INITIALIZED)) {
178-
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
186+
if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
187+
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
188+
} else {
189+
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
190+
}
179191
return;
180192
}
181193
if (ht->nNumUsed == 0) {
182194
efree(HT_GET_DATA_ADDR(ht));
183195
ht->nTableMask = HT_MIN_MASK;
184-
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
196+
if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
197+
HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
198+
} else {
199+
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
200+
}
185201
ht->u.flags &= ~HASH_FLAG_INITIALIZED;
186202
return;
187203
}
@@ -833,9 +849,16 @@ static void zend_accel_persist_class_table(HashTable *class_table)
833849
zend_hash_apply(class_table, (apply_func_t) zend_update_parent_ce);
834850
}
835851

836-
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, char **key, unsigned int key_length)
852+
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, char **key, unsigned int key_length, int for_shm)
837853
{
838854
script->mem = ZCG(mem);
855+
script->corrupted = 0;
856+
ZCG(current_persistent_script) = script;
857+
858+
if (!for_shm) {
859+
/* script is not going to be saved in SHM */
860+
script->corrupted = 1;
861+
}
839862

840863
ZEND_ASSERT(((zend_uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
841864
zend_shared_alloc_clear_xlat_table();
@@ -860,6 +883,9 @@ zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script
860883
zend_hash_persist(&script->script.function_table, zend_persist_op_array);
861884
zend_persist_op_array_ex(&script->script.main_op_array, script);
862885

886+
script->corrupted = 0;
887+
ZCG(current_persistent_script) = NULL;
888+
863889
return script;
864890
}
865891

ext/opcache/zend_persist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424

2525
int zend_accel_script_persistable(zend_persistent_script *script);
2626
uint zend_accel_script_persist_calc(zend_persistent_script *script, char *key, unsigned int key_length, int for_shm);
27-
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, char **key, unsigned int key_length);
27+
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, char **key, unsigned int key_length, int for_shm);
2828

2929
#endif /* ZEND_PERSIST_H */

0 commit comments

Comments
 (0)