diff --git a/CHANGELOG.md b/CHANGELOG.md index 478cc4d..f868ab7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ chronological order. Recovery removes only validated older descriptors and preserves current ownership and corruption checks. This is a compatible bug fix; public APIs, ABI 2.0, and the SMS2 layout are unchanged. +- 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 diff --git a/docs/lifecycle.md b/docs/lifecycle.md index 6672de7..c441e47 100644 --- a/docs/lifecycle.md +++ b/docs/lifecycle.md @@ -126,6 +126,12 @@ Synchronize concurrent attempts to reopen or replace the same wrapper. ## 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 ad69079..568a746 100644 --- a/src/cpp/src/store.cpp +++ b/src/cpp/src/store.cpp @@ -1046,7 +1046,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) || @@ -1317,7 +1320,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{}; @@ -1339,7 +1343,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{}; @@ -1355,7 +1360,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 28d4ee3..fb0930c 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 local_handle_tests.cpp diagnostics_tests.cpp) @@ -53,7 +54,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; +}