Skip to content

Commit 37ce480

Browse files
committed
refactor and behavior changes
1 parent 431e39f commit 37ce480

9 files changed

Lines changed: 146 additions & 45 deletions

Zend/zend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ extern ZEND_API void (*zend_post_shutdown_cb)(void);
387387
extern ZEND_API void (*zend_accel_schedule_restart_hook)(int reason);
388388

389389
/* These hooks are used by OPcache Static Cache to restore, publish, and track
390-
* selected VolatileStatic and PersistentStatic state across requests. They remain
390+
* selected VolatileStatic and PinnedStatic state across requests. They remain
391391
* NULL when the static-cache subsystem is not active. */
392392
extern ZEND_API void (*zend_class_init_statics_hook)(zend_class_entry *ce);
393393
extern ZEND_API void (*zend_function_init_statics_hook)(zend_execute_data *execute_data);

ext/opcache/opcache.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ function volatile_clear(): void {}
108108

109109
function volatile_cache_info(): StaticCacheInfo {}
110110

111-
function pinned_store(string $key, null|bool|int|float|string|array|object $value): void {}
111+
function pinned_store(string $key, null|bool|int|float|string|array|object $value): bool {}
112112

113-
function pinned_store_array(array $values): void {}
113+
function pinned_store_array(array $values): bool {}
114114

115115
function pinned_fetch(string $key, null|bool|int|float|string|array|object $default = null): null|bool|int|float|string|array|object {}
116116

ext/opcache/opcache_arginfo.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
OPcache static cache disabled backends report unavailable and store APIs return false
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.static_cache.volatile_size_mb=0
9+
opcache.static_cache.pinned_size_mb=0
10+
--FILE--
11+
<?php
12+
13+
function dump_info(string $label, OPcache\StaticCacheInfo $info): void
14+
{
15+
echo $label, "\n";
16+
var_dump($info->enabled);
17+
var_dump($info->available);
18+
var_dump($info->startup_failed);
19+
var_dump($info->backend_initialized);
20+
var_dump($info->configured_memory);
21+
var_dump($info->failure_reason);
22+
}
23+
24+
dump_info('volatile', OPcache\volatile_cache_info());
25+
dump_info('pinned', OPcache\pinned_cache_info());
26+
27+
var_dump(OPcache\volatile_store('key', 'value'));
28+
var_dump(OPcache\volatile_store_array(['array-key' => 'value']));
29+
var_dump(OPcache\pinned_store('key', 'value'));
30+
var_dump(OPcache\pinned_store_array(['array-key' => 'value']));
31+
32+
?>
33+
--EXPECT--
34+
volatile
35+
bool(false)
36+
bool(false)
37+
bool(false)
38+
bool(false)
39+
int(0)
40+
NULL
41+
pinned
42+
bool(false)
43+
bool(false)
44+
bool(false)
45+
bool(false)
46+
int(0)
47+
NULL
48+
bool(false)
49+
bool(false)
50+
bool(false)
51+
bool(false)

ext/opcache/tests/static_cache_explicit_cache_lock_public_mutators_fork_001.phpt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ function cache_store(string $backend, string $key, mixed $value): mixed
5252
return OPcache\volatile_store($key, $value);
5353
}
5454

55-
OPcache\pinned_store($key, $value);
56-
return null;
55+
return OPcache\pinned_store($key, $value);
5756
}
5857

5958
function cache_store_array(string $backend, array $values): mixed
@@ -62,8 +61,7 @@ function cache_store_array(string $backend, array $values): mixed
6261
return OPcache\volatile_store_array($values);
6362
}
6463

65-
OPcache\pinned_store_array($values);
66-
return null;
64+
return OPcache\pinned_store_array($values);
6765
}
6866

6967
function cache_fetch(string $backend, string $key, mixed $default = null): mixed
@@ -256,11 +254,11 @@ clear values: owner,MISS
256254
pinned
257255
bool(true)
258256
store blocked: yes
259-
store result: null
257+
store result: true
260258
store value: child
261259
bool(true)
262260
store_array blocked: yes
263-
store_array result: null
261+
store_array result: true
264262
store_array value: child
265263
bool(true)
266264
delete blocked: no

ext/opcache/tests/static_cache_explicit_cache_signatures_001.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ function describe_type(?ReflectionType $type): string
3737

3838
foreach ([
3939
'OPcache\\volatile_store' => ['value'],
40+
'OPcache\\volatile_store_array' => [],
4041
'OPcache\\volatile_fetch' => ['default'],
4142
'OPcache\\volatile_fetch_array' => ['default'],
4243
'OPcache\\volatile_lock' => ['lease'],
4344
'OPcache\\volatile_unlock' => [],
4445
'OPcache\\volatile_cache_info' => [],
4546
'OPcache\\pinned_store' => ['value'],
47+
'OPcache\\pinned_store_array' => [],
4648
'OPcache\\pinned_fetch' => ['default'],
4749
'OPcache\\pinned_fetch_array' => ['default'],
4850
'OPcache\\pinned_lock' => ['lease'],
@@ -69,12 +71,14 @@ foreach ([
6971
?>
7072
--EXPECT--
7173
OPcache\volatile_store $value=null|bool|int|float|string|array|object params=2/3 return=bool
74+
OPcache\volatile_store_array params=1/2 return=bool
7275
OPcache\volatile_fetch $default=null|bool|int|float|string|array|object params=1/2 return=null|bool|int|float|string|array|object
7376
OPcache\volatile_fetch_array $default=?array params=1/2 return=?array
7477
OPcache\volatile_lock $lease=int params=1/2 return=bool
7578
OPcache\volatile_unlock params=1/1 return=bool
7679
OPcache\volatile_cache_info params=0/0 return=OPcache\StaticCacheInfo
77-
OPcache\pinned_store $value=null|bool|int|float|string|array|object params=2/2 return=void
80+
OPcache\pinned_store $value=null|bool|int|float|string|array|object params=2/2 return=bool
81+
OPcache\pinned_store_array params=1/1 return=bool
7882
OPcache\pinned_fetch $default=null|bool|int|float|string|array|object params=1/2 return=null|bool|int|float|string|array|object
7983
OPcache\pinned_fetch_array $default=?array params=1/2 return=?array
8084
OPcache\pinned_lock $lease=int params=1/2 return=bool

ext/opcache/tests/static_cache_startup_failure_001.phpt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,17 @@ var_dump($volatileInfo == $status['volatile_cache']);
3030
var_dump($pinnedInfo == $status['pinned_cache']);
3131
var_dump($volatileInfo->failure_reason);
3232
var_dump($pinnedInfo->failure_reason);
33-
34-
try {
35-
OPcache\volatile_store('key', 'value');
36-
} catch (Throwable $e) {
37-
echo get_class($e), ': ', $e->getMessage(), "\n";
38-
}
39-
40-
try {
41-
OPcache\pinned_store('key', 'value');
42-
} catch (Throwable $e) {
43-
echo get_class($e), ': ', $e->getMessage(), "\n";
44-
}
33+
var_dump(OPcache\volatile_store('key', 'value'));
34+
var_dump(OPcache\pinned_store('key', 'value'));
4535

4636
?>
4737
--EXPECT--
48-
bool(false)
38+
bool(true)
4939
bool(false)
5040
bool(true)
5141
bool(false)
5242
int(33554432)
53-
bool(false)
43+
bool(true)
5444
bool(false)
5545
bool(true)
5646
bool(false)
@@ -59,5 +49,5 @@ bool(true)
5949
bool(true)
6050
string(42) "Unable to initialize shared memory backend"
6151
string(42) "Unable to initialize shared memory backend"
62-
OPcache\StaticCacheException: Unable to initialize shared memory backend
63-
OPcache\StaticCacheException: Unable to initialize shared memory backend
52+
bool(false)
53+
bool(false)

ext/opcache/zend_static_cache.c

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,17 @@ static zend_always_inline bool zend_opcache_static_cache_validate_available_writ
591591
return true;
592592
}
593593

594+
static zend_always_inline bool zend_opcache_static_cache_store_backend_available(void)
595+
{
596+
if (!zend_opcache_validate_api_restriction()) {
597+
zend_throw_exception_ex(zend_opcache_static_cache_exception_ce, 0, ACCELERATOR_PRODUCT_NAME " API access is restricted");
598+
599+
return false;
600+
}
601+
602+
return zend_opcache_static_cache_active_runtime()->available;
603+
}
604+
594605
static zend_always_inline bool zend_opcache_static_cache_acquire_write_lock(void)
595606
{
596607
zend_opcache_static_cache_context *context = zend_opcache_static_cache_active_context();
@@ -1184,9 +1195,11 @@ ZEND_METHOD(OPcache_VolatileStatic, __construct)
11841195

11851196
ZEND_FUNCTION(OPcache_volatile_store)
11861197
{
1198+
zend_opcache_static_cache_context *previous_context;
11871199
zend_string *key;
11881200
zend_long ttl = 0;
11891201
zval *value;
1202+
bool stored;
11901203

11911204
ZEND_PARSE_PARAMETERS_START(2, 3)
11921205
Z_PARAM_STR(key)
@@ -1203,11 +1216,24 @@ ZEND_FUNCTION(OPcache_volatile_store)
12031216
RETURN_THROWS();
12041217
}
12051218

1206-
if (!zend_opcache_static_cache_parse_ttl(ttl, 3) || !zend_opcache_static_cache_validate_available_write()) {
1219+
if (!zend_opcache_static_cache_parse_ttl(ttl, 3)) {
12071220
RETURN_THROWS();
12081221
}
12091222

1210-
if (!zend_opcache_static_cache_explicit_store_prevalidated(key, value, ttl, false)) {
1223+
previous_context = zend_opcache_static_cache_activate_context(&zend_opcache_static_cache_volatile_context_state);
1224+
if (!zend_opcache_static_cache_store_backend_available()) {
1225+
zend_opcache_static_cache_restore_context(previous_context);
1226+
if (EG(exception)) {
1227+
RETURN_THROWS();
1228+
}
1229+
1230+
RETURN_FALSE;
1231+
}
1232+
1233+
stored = zend_opcache_static_cache_explicit_store_prevalidated(key, value, ttl, false);
1234+
zend_opcache_static_cache_restore_context(previous_context);
1235+
1236+
if (!stored) {
12111237
if (EG(exception)) {
12121238
RETURN_THROWS();
12131239
}
@@ -1220,29 +1246,43 @@ ZEND_FUNCTION(OPcache_volatile_store)
12201246

12211247
ZEND_FUNCTION(OPcache_volatile_store_array)
12221248
{
1249+
zend_opcache_static_cache_context *previous_context;
12231250
zend_string *key;
12241251
zend_long ttl = 0;
12251252
zval *value;
12261253
HashTable *values;
1254+
bool stored;
12271255

12281256
ZEND_PARSE_PARAMETERS_START(1, 2)
12291257
Z_PARAM_ARRAY_HT(values)
12301258
Z_PARAM_OPTIONAL
12311259
Z_PARAM_LONG(ttl)
12321260
ZEND_PARSE_PARAMETERS_END();
12331261

1234-
if (!zend_opcache_static_cache_parse_ttl(ttl, 2) || !zend_opcache_static_cache_validate_available_write()) {
1262+
if (!zend_opcache_static_cache_parse_ttl(ttl, 2)) {
12351263
RETURN_THROWS();
12361264
}
12371265

12381266
if (!zend_opcache_static_cache_validate_store_array_keys(values, 1)) {
12391267
RETURN_THROWS();
12401268
}
12411269

1270+
previous_context = zend_opcache_static_cache_activate_context(&zend_opcache_static_cache_volatile_context_state);
1271+
if (!zend_opcache_static_cache_store_backend_available()) {
1272+
zend_opcache_static_cache_restore_context(previous_context);
1273+
if (EG(exception)) {
1274+
RETURN_THROWS();
1275+
}
1276+
1277+
RETURN_FALSE;
1278+
}
1279+
12421280
ZEND_HASH_FOREACH_STR_KEY_VAL(values, key, value) {
12431281
ZEND_ASSERT(key != NULL);
12441282

1245-
if (!zend_opcache_static_cache_explicit_store_prevalidated(key, value, ttl, false)) {
1283+
stored = zend_opcache_static_cache_explicit_store_prevalidated(key, value, ttl, false);
1284+
if (!stored) {
1285+
zend_opcache_static_cache_restore_context(previous_context);
12461286
if (EG(exception)) {
12471287
RETURN_THROWS();
12481288
}
@@ -1251,6 +1291,8 @@ ZEND_FUNCTION(OPcache_volatile_store_array)
12511291
}
12521292
} ZEND_HASH_FOREACH_END();
12531293

1294+
zend_opcache_static_cache_restore_context(previous_context);
1295+
12541296
RETURN_TRUE;
12551297
}
12561298

@@ -1478,10 +1520,13 @@ ZEND_FUNCTION(OPcache_pinned_store)
14781520
}
14791521

14801522
previous_context = zend_opcache_static_cache_activate_context(&zend_opcache_static_cache_pinned_context_state);
1481-
if (!zend_opcache_static_cache_validate_available_write()) {
1523+
if (!zend_opcache_static_cache_store_backend_available()) {
14821524
zend_opcache_static_cache_restore_context(previous_context);
1525+
if (EG(exception)) {
1526+
RETURN_THROWS();
1527+
}
14831528

1484-
RETURN_THROWS();
1529+
RETURN_FALSE;
14851530
}
14861531

14871532
stored = zend_opcache_static_cache_explicit_store_prevalidated(key, value, 0, true);
@@ -1490,6 +1535,8 @@ ZEND_FUNCTION(OPcache_pinned_store)
14901535
if (!stored) {
14911536
RETURN_THROWS();
14921537
}
1538+
1539+
RETURN_TRUE;
14931540
}
14941541

14951542
ZEND_FUNCTION(OPcache_pinned_store_array)
@@ -1498,35 +1545,40 @@ ZEND_FUNCTION(OPcache_pinned_store_array)
14981545
zend_string *key;
14991546
zval *value;
15001547
HashTable *values;
1548+
bool stored;
15011549

15021550
ZEND_PARSE_PARAMETERS_START(1, 1)
15031551
Z_PARAM_ARRAY_HT(values)
15041552
ZEND_PARSE_PARAMETERS_END();
15051553

1506-
previous_context = zend_opcache_static_cache_activate_context(&zend_opcache_static_cache_pinned_context_state);
1507-
if (!zend_opcache_static_cache_validate_available_write()) {
1508-
zend_opcache_static_cache_restore_context(previous_context);
1509-
1554+
if (!zend_opcache_static_cache_validate_store_array_keys(values, 1)) {
15101555
RETURN_THROWS();
15111556
}
15121557

1513-
if (!zend_opcache_static_cache_validate_store_array_keys(values, 1)) {
1558+
previous_context = zend_opcache_static_cache_activate_context(&zend_opcache_static_cache_pinned_context_state);
1559+
if (!zend_opcache_static_cache_store_backend_available()) {
15141560
zend_opcache_static_cache_restore_context(previous_context);
1561+
if (EG(exception)) {
1562+
RETURN_THROWS();
1563+
}
15151564

1516-
RETURN_THROWS();
1565+
RETURN_FALSE;
15171566
}
15181567

15191568
ZEND_HASH_FOREACH_STR_KEY_VAL(values, key, value) {
15201569
ZEND_ASSERT(key != NULL);
15211570

1522-
if (!zend_opcache_static_cache_explicit_store_prevalidated(key, value, 0, true)) {
1571+
stored = zend_opcache_static_cache_explicit_store_prevalidated(key, value, 0, true);
1572+
if (!stored) {
15231573
zend_opcache_static_cache_restore_context(previous_context);
15241574

15251575
RETURN_THROWS();
15261576
}
15271577
} ZEND_HASH_FOREACH_END();
15281578

15291579
zend_opcache_static_cache_restore_context(previous_context);
1580+
1581+
RETURN_TRUE;
15301582
}
15311583

15321584
ZEND_FUNCTION(OPcache_pinned_fetch)

0 commit comments

Comments
 (0)