From 5107ce531c02cb1b2f037a256403c5caa9487be7 Mon Sep 17 00:00:00 2001 From: Alexander Polyakov Date: Thu, 26 Mar 2026 15:03:44 +0300 Subject: [PATCH 1/3] [k2] fix constant initialization accessing RuntimeContext --- .../core/kphp-core-impl/kphp-core-context.cpp | 16 +++++++++++++++- runtime-light/state/image-state.h | 3 +++ runtime-light/state/instance-state.cpp | 1 - runtime-light/state/instance-state.h | 6 ++++-- tests/phpt/pk/021_minmax.php | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp index 71c3ba8803..90540863db 100644 --- a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp +++ b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp @@ -3,13 +3,27 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime-common/core/runtime-core.h" +#include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/state/instance-state.h" +#include "runtime-light/stdlib/diagnostics/logs.h" + +namespace { constexpr string_size_type initial_minimum_string_buffer_length = 1024; constexpr string_size_type initial_maximum_string_buffer_length = (1 << 24); +} // namespace + +// FIXME codegen for constants initialization should not access RuntimeContext by mutable reference RuntimeContext& RuntimeContext::get() noexcept { - return InstanceState::get().runtime_context; + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->runtime_context; + } else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) [[unlikely]] { + kphp::log::error("unexpected access to RuntimeContext"); + } else if (const auto* image_state_ptr{k2::image_state()}; image_state_ptr != nullptr) [[likely]] { + return const_cast(image_state_ptr->runtime_context); + } + kphp::log::error("can't find suitable RuntimeContext"); } void RuntimeContext::init() noexcept { diff --git a/runtime-light/state/image-state.h b/runtime-light/state/image-state.h index 41e061dbe6..423a1ff6b6 100644 --- a/runtime-light/state/image-state.h +++ b/runtime-light/state/image-state.h @@ -29,6 +29,7 @@ struct ImageState final : private vk::not_copyable { AllocatorState image_allocator_state{INIT_IMAGE_ALLOCATOR_SIZE, 0}; + RuntimeContext runtime_context; uint32_t pid{k2::getpid()}; uid_t uid{k2::getuid()}; @@ -50,6 +51,8 @@ struct ImageState final : private vk::not_copyable { RpcImageState rpc_image_state; ImageState() noexcept { + runtime_context.init(); + if (const int64_t sysconf_max_buffer_size{k2::sysconf(_SC_GETPW_R_SIZE_MAX)}; sysconf_max_buffer_size != -1) { passwd_max_buffer_size.emplace(sysconf_max_buffer_size); } diff --git a/runtime-light/state/instance-state.cpp b/runtime-light/state/instance-state.cpp index 4b27210266..7c85e41015 100644 --- a/runtime-light/state/instance-state.cpp +++ b/runtime-light/state/instance-state.cpp @@ -59,7 +59,6 @@ consteval std::string_view resolve_sapi_name() noexcept { // === initialization ============================================================================= void InstanceState::init_script_execution() noexcept { - runtime_context.init(); kphp::coro::task<> script_task; init_php_scripts_in_each_worker(php_script_mutable_globals_singleton, script_task); diff --git a/runtime-light/state/instance-state.h b/runtime-light/state/instance-state.h index 1c91367c30..5f4ee1ebb2 100644 --- a/runtime-light/state/instance-state.h +++ b/runtime-light/state/instance-state.h @@ -67,6 +67,7 @@ struct InstanceState final : vk::not_copyable { // In the second case clang++ zeroes the whole structure. // It drastically ruins performance. Be careful! InstanceState() noexcept { + runtime_context.init(); kml_instance_state.init(ComponentState::get().kml_component_state.max_buffer_size()); } @@ -89,16 +90,17 @@ struct InstanceState final : vk::not_copyable { } AllocatorState instance_allocator_state{INIT_INSTANCE_ALLOCATOR_SIZE, 0}; - kphp::log::contextual_tags instance_tags; + kphp::log::contextual_tags instance_tags; kphp::coro::io_scheduler io_scheduler; + + RuntimeContext runtime_context; CoroutineInstanceState coroutine_instance_state; ForkInstanceState fork_instance_state; WaitQueueInstanceState wait_queue_instance_state; RpcQueueInstanceState rpc_queue_instance_state; PhpScriptMutableGlobals php_script_mutable_globals_singleton; - RuntimeContext runtime_context; CLIInstanceInstance cli_instance_instate; OutputInstanceState output_instance_state; RpcClientInstanceState rpc_client_instance_state; diff --git a/tests/phpt/pk/021_minmax.php b/tests/phpt/pk/021_minmax.php index ae089bc405..673975380d 100644 --- a/tests/phpt/pk/021_minmax.php +++ b/tests/phpt/pk/021_minmax.php @@ -1,4 +1,4 @@ -@ok k2_skip +@ok Date: Fri, 27 Mar 2026 13:28:37 +0300 Subject: [PATCH 2/3] Revert "[k2] fix constant initialization accessing RuntimeContext" This reverts commit 8484741dcffd596b664aa189d0bcc24511c40cd9. --- .../core/kphp-core-impl/kphp-core-context.cpp | 16 +--------------- runtime-light/state/image-state.h | 3 --- runtime-light/state/instance-state.cpp | 1 + runtime-light/state/instance-state.h | 6 ++---- tests/phpt/pk/021_minmax.php | 2 +- 5 files changed, 5 insertions(+), 23 deletions(-) diff --git a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp index 90540863db..71c3ba8803 100644 --- a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp +++ b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp @@ -3,27 +3,13 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime-common/core/runtime-core.h" -#include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/state/instance-state.h" -#include "runtime-light/stdlib/diagnostics/logs.h" - -namespace { constexpr string_size_type initial_minimum_string_buffer_length = 1024; constexpr string_size_type initial_maximum_string_buffer_length = (1 << 24); -} // namespace - -// FIXME codegen for constants initialization should not access RuntimeContext by mutable reference RuntimeContext& RuntimeContext::get() noexcept { - if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { - return instance_state_ptr->runtime_context; - } else if (const auto* component_state_ptr{k2::component_state()}; component_state_ptr != nullptr) [[unlikely]] { - kphp::log::error("unexpected access to RuntimeContext"); - } else if (const auto* image_state_ptr{k2::image_state()}; image_state_ptr != nullptr) [[likely]] { - return const_cast(image_state_ptr->runtime_context); - } - kphp::log::error("can't find suitable RuntimeContext"); + return InstanceState::get().runtime_context; } void RuntimeContext::init() noexcept { diff --git a/runtime-light/state/image-state.h b/runtime-light/state/image-state.h index 423a1ff6b6..41e061dbe6 100644 --- a/runtime-light/state/image-state.h +++ b/runtime-light/state/image-state.h @@ -29,7 +29,6 @@ struct ImageState final : private vk::not_copyable { AllocatorState image_allocator_state{INIT_IMAGE_ALLOCATOR_SIZE, 0}; - RuntimeContext runtime_context; uint32_t pid{k2::getpid()}; uid_t uid{k2::getuid()}; @@ -51,8 +50,6 @@ struct ImageState final : private vk::not_copyable { RpcImageState rpc_image_state; ImageState() noexcept { - runtime_context.init(); - if (const int64_t sysconf_max_buffer_size{k2::sysconf(_SC_GETPW_R_SIZE_MAX)}; sysconf_max_buffer_size != -1) { passwd_max_buffer_size.emplace(sysconf_max_buffer_size); } diff --git a/runtime-light/state/instance-state.cpp b/runtime-light/state/instance-state.cpp index 7c85e41015..4b27210266 100644 --- a/runtime-light/state/instance-state.cpp +++ b/runtime-light/state/instance-state.cpp @@ -59,6 +59,7 @@ consteval std::string_view resolve_sapi_name() noexcept { // === initialization ============================================================================= void InstanceState::init_script_execution() noexcept { + runtime_context.init(); kphp::coro::task<> script_task; init_php_scripts_in_each_worker(php_script_mutable_globals_singleton, script_task); diff --git a/runtime-light/state/instance-state.h b/runtime-light/state/instance-state.h index 5f4ee1ebb2..1c91367c30 100644 --- a/runtime-light/state/instance-state.h +++ b/runtime-light/state/instance-state.h @@ -67,7 +67,6 @@ struct InstanceState final : vk::not_copyable { // In the second case clang++ zeroes the whole structure. // It drastically ruins performance. Be careful! InstanceState() noexcept { - runtime_context.init(); kml_instance_state.init(ComponentState::get().kml_component_state.max_buffer_size()); } @@ -90,17 +89,16 @@ struct InstanceState final : vk::not_copyable { } AllocatorState instance_allocator_state{INIT_INSTANCE_ALLOCATOR_SIZE, 0}; - kphp::log::contextual_tags instance_tags; - kphp::coro::io_scheduler io_scheduler; - RuntimeContext runtime_context; + kphp::coro::io_scheduler io_scheduler; CoroutineInstanceState coroutine_instance_state; ForkInstanceState fork_instance_state; WaitQueueInstanceState wait_queue_instance_state; RpcQueueInstanceState rpc_queue_instance_state; PhpScriptMutableGlobals php_script_mutable_globals_singleton; + RuntimeContext runtime_context; CLIInstanceInstance cli_instance_instate; OutputInstanceState output_instance_state; RpcClientInstanceState rpc_client_instance_state; diff --git a/tests/phpt/pk/021_minmax.php b/tests/phpt/pk/021_minmax.php index 673975380d..ae089bc405 100644 --- a/tests/phpt/pk/021_minmax.php +++ b/tests/phpt/pk/021_minmax.php @@ -1,4 +1,4 @@ -@ok +@ok k2_skip Date: Fri, 27 Mar 2026 14:53:29 +0300 Subject: [PATCH 3/3] make RuntimeContext explicitly unavailable until InstanceState is initialized --- .../core/kphp-core-impl/kphp-core-context.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp index 71c3ba8803..ce79f0ee45 100644 --- a/runtime-light/core/kphp-core-impl/kphp-core-context.cpp +++ b/runtime-light/core/kphp-core-impl/kphp-core-context.cpp @@ -3,13 +3,22 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime-common/core/runtime-core.h" +#include "runtime-light/k2-platform/k2-api.h" #include "runtime-light/state/instance-state.h" +#include "runtime-light/stdlib/diagnostics/logs.h" + +namespace { constexpr string_size_type initial_minimum_string_buffer_length = 1024; constexpr string_size_type initial_maximum_string_buffer_length = (1 << 24); +} // namespace + RuntimeContext& RuntimeContext::get() noexcept { - return InstanceState::get().runtime_context; + if (auto* instance_state_ptr{k2::instance_state()}; instance_state_ptr != nullptr) [[likely]] { + return instance_state_ptr->runtime_context; + } + kphp::log::error("unexpected access to RuntimeContext"); } void RuntimeContext::init() noexcept {