From ac6b77ea15cb0861f8e38b0551775977c4f297eb Mon Sep 17 00:00:00 2001 From: Ran Trifon Date: Tue, 22 Sep 2026 20:32:15 +0300 Subject: [PATCH] Reject native projections when shared store is not ready --- CHANGELOG.md | 4 +++ docs/lifecycle.md | 6 ++++ src/cpp/src/store.cpp | 14 ++++++--- tests/cpp/CMakeLists.txt | 4 ++- tests/cpp/projection_control_tests.cpp | 40 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/cpp/projection_control_tests.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d1ca2..d692112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ chronological order. ## Unreleased +- Fixed native lease and reservation projections remaining available after the + shared store leaves Ready. This is a patch-level correction with no API or + mapped protocol change. + ## 3.0.0 - 2026-07-16 ### Breaking changes diff --git a/docs/lifecycle.md b/docs/lifecycle.md index 2d1b3de..243d5ae 100644 --- a/docs/lifecycle.md +++ b/docs/lifecycle.md @@ -120,6 +120,12 @@ their local operation drain. ## Generations and Incarnations +Native lease and reservation accessors check the shared store's Ready state +before projecting memory. After shared corruption or another non-Ready state +is observed, validity is false, lengths are zero, and new borrowed spans are +empty. An already returned span cannot be revoked; its caller remains +responsible for observing the store and token lifetime. + Slots, leases, participants, directory operations, and their public tokens use generation/incarnation identity. A transition compares the complete encoded identity before exposing memory, helping, releasing, or reclaiming. Terminal diff --git a/src/cpp/src/store.cpp b/src/cpp/src/store.cpp index 2a1f0de..b707030 100644 --- a/src/cpp/src/store.cpp +++ b/src/cpp/src/store.cpp @@ -1038,7 +1038,10 @@ bool Store::project_lease( descriptor_length = 0; sms::test_detail::reach_checkpoint( sms::test_detail::CheckpointId::ProjectBeforeHandleValidation); - if (state_ == nullptr || !lease.valid()) return false; + if (state_ == nullptr || !lease.valid() || + state_->control.ensure_ready() != SMS_STATUS_SUCCESS) { + return false; + } std::uint64_t registry_binding{}; if (!state_->leases.try_get_active_slot_binding( lease, registry_binding) || @@ -1309,7 +1312,8 @@ bool Store::reservation_valid( LifecycleId lifecycle) noexcept { LifecycleGate::Operation operation; if (lifecycle_.try_enter(operation) != SMS_STATUS_SUCCESS || - state_ == nullptr || !lifecycle.reservation_valid()) { + state_ == nullptr || !lifecycle.reservation_valid() || + state_->control.ensure_ready() != SMS_STATUS_SUCCESS) { return false; } IndexBinding binding{}; @@ -1331,7 +1335,8 @@ std::int32_t Store::reservation_bytes_written( LifecycleId lifecycle) noexcept { LifecycleGate::Operation operation; if (lifecycle_.try_enter(operation) != SMS_STATUS_SUCCESS || - state_ == nullptr || !lifecycle.reservation_valid()) { + state_ == nullptr || !lifecycle.reservation_valid() || + state_->control.ensure_ready() != SMS_STATUS_SUCCESS) { return 0; } IndexBinding binding{}; @@ -1347,7 +1352,8 @@ std::span Store::reservation_buffer( std::int32_t size_hint) noexcept { LifecycleGate::Operation operation; if (lifecycle_.try_enter(operation) != SMS_STATUS_SUCCESS || - state_ == nullptr || !lifecycle.reservation_valid()) { + state_ == nullptr || !lifecycle.reservation_valid() || + state_->control.ensure_ready() != SMS_STATUS_SUCCESS) { return {}; } IndexBinding binding{}; diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index 1b0ed1b..8a51540 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -3,6 +3,7 @@ set( protocol_tests.cpp store_tests.cpp c_api_tests.cpp + projection_control_tests.cpp lifecycle_tests.cpp diagnostics_tests.cpp) @@ -52,7 +53,8 @@ function(_sms_add_native_test source_name) set(_target "sms_${_test_stem}") add_executable(${_target} "${_source}") - if(_test_stem STREQUAL "protocol_tests") + if(_test_stem STREQUAL "protocol_tests" OR + _test_stem STREQUAL "projection_control_tests") target_sources( ${_target} PRIVATE "${PROJECT_SOURCE_DIR}/src/cpp/src/protocol.cpp") diff --git a/tests/cpp/projection_control_tests.cpp b/tests/cpp/projection_control_tests.cpp new file mode 100644 index 0000000..0063f8d --- /dev/null +++ b/tests/cpp/projection_control_tests.cpp @@ -0,0 +1,40 @@ +#include "interop_faults.hpp" +#include "mapped_atomic.hpp" +#include "test_support.hpp" + +int main() { + using namespace shared_memory_store; + using namespace sms::detail; + + for (const auto control : { + sms2_store_initializing, sms2_store_corrupt, sms2_store_unsupported, + std::uint64_t{0xffff}}) { + auto options = sms_test_options("projection-control", 2, 2); + memory_store store; + SMS_CHECK(memory_store::try_create_or_open(options, store) == open_status::success); + SMS_CHECK(store.try_publish(sms_test_bytes("key"), sms_test_bytes("payload"), + sms_test_bytes("descriptor")) == status::success); + value_lease lease; + SMS_CHECK(store.try_acquire(sms_test_bytes("key"), lease) == status::success); + value_reservation reservation; + SMS_CHECK(store.try_reserve(sms_test_bytes("reserved"), 4, {}, reservation) == + status::success); + SMS_CHECK(lease.valid() && lease.value().size() == 7 && lease.descriptor().size() == 10); + SMS_CHECK(reservation.valid() && reservation.buffer().size() == 4); + SMS_CHECK(reservation.advance(1) == status::success); + SMS_CHECK(reservation.payload_length() == 4 && reservation.bytes_written() == 1); + + // Change the actual shared control after valid tokens have escaped. + sms::interop_test::raw_mapping mapping(options); + auto& header = sms::interop_test::validate_raw_mapping(mapping, options); + MappedAtomic64::store_release(header.Control, control); + SMS_CHECK(!lease.valid()); + SMS_CHECK(lease.value().empty()); + SMS_CHECK(lease.descriptor().empty()); + SMS_CHECK(!reservation.valid()); + SMS_CHECK(reservation.buffer().empty()); + SMS_CHECK(reservation.payload_length() == 0 && reservation.bytes_written() == 0); + SMS_CHECK(MappedAtomic64::load_acquire(header.Control) == control); + } + return 0; +}