Skip to content

Commit 8484741

Browse files
committed
[k2] fix constant initialization accessing RuntimeContext
1 parent 4564345 commit 8484741

5 files changed

Lines changed: 23 additions & 5 deletions

File tree

runtime-light/core/kphp-core-impl/kphp-core-context.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
// Distributed under the GPL v3 License, see LICENSE.notice.txt
44

55
#include "runtime-common/core/runtime-core.h"
6+
#include "runtime-light/k2-platform/k2-api.h"
67
#include "runtime-light/state/instance-state.h"
8+
#include "runtime-light/stdlib/diagnostics/logs.h"
9+
10+
namespace {
711

812
constexpr string_size_type initial_minimum_string_buffer_length = 1024;
913
constexpr string_size_type initial_maximum_string_buffer_length = (1 << 24);
1014

15+
} // namespace
16+
17+
// FIXME codegen for constants initialization should not access RuntimeContext by mutable reference
1118
RuntimeContext& RuntimeContext::get() noexcept {
12-
return InstanceState::get().runtime_context;
19+
if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] {
20+
return instance_state_ptr->runtime_context;
21+
} else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) [[unlikely]] {
22+
kphp::log::error("unexpected access to RuntimeContext");
23+
} else if (const auto* image_state_ptr{k2::image_state()}; image_state_ptr != nullptr) [[likely]] {
24+
return const_cast<RuntimeContext&>(image_state_ptr->runtime_context);
25+
}
26+
kphp::log::error("can't find suitable RuntimeContext");
1327
}
1428

1529
void RuntimeContext::init() noexcept {

runtime-light/state/image-state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
struct ImageState final : private vk::not_copyable {
3030
AllocatorState image_allocator_state{INIT_IMAGE_ALLOCATOR_SIZE, 0};
31+
RuntimeContext runtime_context;
3132

3233
uint32_t pid{k2::getpid()};
3334
uid_t uid{k2::getuid()};
@@ -48,6 +49,8 @@ struct ImageState final : private vk::not_copyable {
4849
RpcImageState rpc_image_state;
4950

5051
ImageState() noexcept {
52+
runtime_context.init();
53+
5154
if (const int64_t sysconf_max_buffer_size{k2::sysconf(_SC_GETPW_R_SIZE_MAX)}; sysconf_max_buffer_size != -1) {
5255
passwd_max_buffer_size.emplace(sysconf_max_buffer_size);
5356
}

runtime-light/state/instance-state.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ consteval std::string_view resolve_sapi_name() noexcept {
5959
// === initialization =============================================================================
6060

6161
void InstanceState::init_script_execution() noexcept {
62-
runtime_context.init();
6362
kphp::coro::task<> script_task;
6463
init_php_scripts_in_each_worker(php_script_mutable_globals_singleton, script_task);
6564

runtime-light/state/instance-state.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct InstanceState final : vk::not_copyable {
6767
// In the second case clang++ zeroes the whole structure.
6868
// It drastically ruins performance. Be careful!
6969
InstanceState() noexcept {
70+
runtime_context.init();
7071
kml_instance_state.init(ComponentState::get().kml_component_state.max_buffer_size());
7172
}
7273

@@ -89,16 +90,17 @@ struct InstanceState final : vk::not_copyable {
8990
}
9091

9192
AllocatorState instance_allocator_state{INIT_INSTANCE_ALLOCATOR_SIZE, 0};
92-
kphp::log::contextual_tags instance_tags;
9393

94+
kphp::log::contextual_tags instance_tags;
9495
kphp::coro::io_scheduler io_scheduler;
96+
97+
RuntimeContext runtime_context;
9598
CoroutineInstanceState coroutine_instance_state;
9699
ForkInstanceState fork_instance_state;
97100
WaitQueueInstanceState wait_queue_instance_state;
98101
RpcQueueInstanceState rpc_queue_instance_state;
99102
PhpScriptMutableGlobals php_script_mutable_globals_singleton;
100103

101-
RuntimeContext runtime_context;
102104
CLIInstanceInstance cli_instance_instate;
103105
OutputInstanceState output_instance_state;
104106
RpcClientInstanceState rpc_client_instance_state;

tests/phpt/pk/021_minmax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@ok k2_skip
1+
@ok
22
<?php
33

44
var_dump(min([1]));

0 commit comments

Comments
 (0)