Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ chronological order.
- 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.
- Fixed native lease projections discarding confirmed malformed lease controls
or bindings without marking the shared store corrupt. This is a patch-level
correction with no API or mapped protocol change.

## 3.0.0 - 2026-07-16

Expand Down
5 changes: 5 additions & 0 deletions docs/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ 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.

Native lease projection rejects released or reused tokens without corrupting
the store. If it instead confirms a malformed lease control or a stable Active
lease with an invalid or mismatched slot binding, it marks the shared store
corrupt before returning false validity or an empty span.

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
Expand Down
53 changes: 37 additions & 16 deletions src/cpp/src/lease_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,46 +560,67 @@ sms_status LeaseRegistry::try_cancel_claim(const LeaseToken& lease) noexcept {
return lease_status(expected, incarnation);
}

bool LeaseRegistry::try_get_active_slot_binding(
sms_status LeaseRegistry::validate_active_slot_binding(
const LeaseToken& lease,
std::uint64_t& slot_binding) const noexcept {
slot_binding = 0;
if (owner_status() != SMS_STATUS_SUCCESS) return false;
auto participant_status = owner_status();
if (participant_status != SMS_STATUS_SUCCESS) return participant_status;
std::int32_t index{};
std::int64_t incarnation{};
if (!try_decode_lease(lease, index, incarnation)) return false;
if (!try_decode_lease(lease, index, incarnation)) return SMS_STATUS_INVALID_LEASE;
auto* current = record(index);
if (current == nullptr) return false;
if (current == nullptr) return SMS_STATUS_CORRUPT_STORE;
std::uint64_t active{};
if (!encode_lease_control(
LeaseState::active,
incarnation,
lease.participant_token,
active)) {
return false;
return SMS_STATUS_INVALID_LEASE;
}
const auto confirm_corruption = [&](std::uint64_t observed) noexcept {
// Revalidate the exact observation before poisoning the shared store.
// A released/reused record has a different complete control word.
auto expected = observed;
return MappedAtomic64::compare_exchange(current->Control, expected, observed)
? SMS_STATUS_CORRUPT_STORE
: SMS_STATUS_INVALID_LEASE;
};
const auto control1 = MappedAtomic64::load_acquire(current->Control);
bool occupied{};
if (!try_classify_structural_control(
control1, layout_.participant_record_count, occupied) ||
control1 != active) {
return false;
control1, layout_.participant_record_count, occupied)) {
return confirm_corruption(control1);
}
if (control1 != active) return SMS_STATUS_INVALID_LEASE;
const auto observed_binding =
MappedAtomic64::load_acquire(current->SlotBinding);
const auto control2 = MappedAtomic64::load_acquire(current->Control);
if (!try_classify_structural_control(
control2, layout_.participant_record_count, occupied) ||
control2 != control1 || !valid_slot_binding(observed_binding) ||
observed_binding != lease.slot_binding) {
return false;
control2, layout_.participant_record_count, occupied)) {
return confirm_corruption(control2);
}
if (owner_status() != SMS_STATUS_SUCCESS ||
MappedAtomic64::load_acquire(current->Control) != control1) {
return false;
if (control2 != control1) return SMS_STATUS_INVALID_LEASE;
if (!valid_slot_binding(observed_binding) || observed_binding != lease.slot_binding) {
return confirm_corruption(control1);
}
participant_status = owner_status();
if (participant_status != SMS_STATUS_SUCCESS) return participant_status;
const auto control3 = MappedAtomic64::load_acquire(current->Control);
if (!try_classify_structural_control(
control3, layout_.participant_record_count, occupied)) {
return confirm_corruption(control3);
}
if (control3 != control1) return SMS_STATUS_INVALID_LEASE;
slot_binding = observed_binding;
return true;
return SMS_STATUS_SUCCESS;
}

bool LeaseRegistry::try_get_active_slot_binding(
const LeaseToken& lease,
std::uint64_t& slot_binding) const noexcept {
return validate_active_slot_binding(lease, slot_binding) == SMS_STATUS_SUCCESS;
}

bool LeaseRegistry::is_active(const LeaseToken& lease) const noexcept {
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/src/lease_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class LeaseRegistry {
// This is the registry half of immutable projection validation. The caller
// surrounds slot metadata with its own Published/RemoveRequested and exact
// directory-source revalidation before constructing a borrowed span.
// Preserve confirmed corruption for the owning Store to latch; stale or
// concurrently released/reused tokens return InvalidLease.
[[nodiscard]] sms_status validate_active_slot_binding(
const LeaseToken& lease,
std::uint64_t& slot_binding) const noexcept;
[[nodiscard]] bool try_get_active_slot_binding(
const LeaseToken& lease,
std::uint64_t& slot_binding) const noexcept;
Expand Down
20 changes: 15 additions & 5 deletions src/cpp/src/store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,12 @@ bool Store::project_lease(
return false;
}
std::uint64_t registry_binding{};
if (!state_->leases.try_get_active_slot_binding(
lease, registry_binding) ||
registry_binding != lease.slot_binding) {
const auto lease_status = state_->leases.validate_active_slot_binding(
lease, registry_binding);
if (lease_status != SMS_STATUS_SUCCESS || registry_binding != lease.slot_binding) {
if (lease_status == SMS_STATUS_CORRUPT_STORE) {
(void)state_->control.latch_corrupt();
}
return false;
}
IndexBinding binding{};
Expand Down Expand Up @@ -1110,8 +1113,15 @@ bool Store::project_lease(
!range_valid(
payload_offset,
observed_value,
static_cast<std::size_t>(state_->region->size())) ||
!state_->leases.is_active(lease)) {
static_cast<std::size_t>(state_->region->size()))) {
return false;
}
const auto confirmed_lease = state_->leases.validate_active_slot_binding(
lease, registry_binding);
if (confirmed_lease != SMS_STATUS_SUCCESS) {
if (confirmed_lease == SMS_STATUS_CORRUPT_STORE) {
(void)state_->control.latch_corrupt();
}
return false;
}
value_slot = current;
Expand Down
10 changes: 8 additions & 2 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set(
store_tests.cpp
c_api_tests.cpp
projection_control_tests.cpp
lease_corruption_tests.cpp
lease_projection_schedule_tests.cpp
lifecycle_tests.cpp
local_handle_tests.cpp
diagnostics_tests.cpp)
Expand Down Expand Up @@ -55,7 +57,8 @@ function(_sms_add_native_test source_name)

add_executable(${_target} "${_source}")
if(_test_stem STREQUAL "protocol_tests" OR
_test_stem STREQUAL "projection_control_tests")
_test_stem STREQUAL "projection_control_tests" OR
_test_stem STREQUAL "lease_corruption_tests")
target_sources(
${_target}
PRIVATE "${PROJECT_SOURCE_DIR}/src/cpp/src/protocol.cpp")
Expand Down Expand Up @@ -159,7 +162,10 @@ function(_sms_add_native_test source_name)
"${PROJECT_SOURCE_DIR}/src/cpp/include")
target_compile_definitions(
${_target} PRIVATE SMS_REPOSITORY_ROOT="${PROJECT_SOURCE_DIR}")
if(_test_stem STREQUAL "slot_reservation_tests" OR
if(_test_stem STREQUAL "lease_projection_schedule_tests")
target_link_libraries(
${_target} PRIVATE SharedMemoryStoreCheckpointRuntime Threads::Threads)
elseif(_test_stem STREQUAL "slot_reservation_tests" OR
_test_stem STREQUAL "lease_v2_tests" OR
_test_stem STREQUAL "recovery_v2_tests")
target_link_libraries(${_target} PRIVATE Threads::Threads)
Expand Down
59 changes: 59 additions & 0 deletions tests/cpp/lease_corruption_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "interop_faults.hpp"
#include "lease_registry.hpp"
#include "mapped_atomic.hpp"
#include "test_support.hpp"

int main() {
using namespace shared_memory_store;
using namespace sms::detail;

// Every public projection must preserve structural corruption, even
// though its public return type is only a boolean or a borrowed span.
for (int fault = 0; fault < 5; ++fault) {
for (int accessor = 0; accessor < 3; ++accessor) {
auto options = sms_test_options("lease-projection-corruption", 2, 1);
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);
SMS_CHECK(lease.valid());

sms::interop_test::raw_mapping mapping(options);
auto& header = sms::interop_test::validate_raw_mapping(mapping, options);
auto& record = *reinterpret_cast<LeaseRecordV2*>(
mapping.data() + header.LeaseRegistryOffset);
const auto active = MappedAtomic64::load_acquire(record.Control);
switch (fault) {
case 0: // Active without its required participant owner.
MappedAtomic64::store_release(record.Control, active & ((1ULL << 36) - 1));
break;
case 1: // Unknown lifecycle state.
MappedAtomic64::store_release(record.Control, (active & ~7ULL) | 6ULL);
break;
case 2: // Zero incarnation is never valid.
MappedAtomic64::store_release(record.Control, active & ~(((1ULL << 33) - 1) << 3));
break;
case 3:
MappedAtomic64::store_release(record.SlotBinding, 0);
break;
default: { // Validly encoded binding for a different slot.
IndexBinding original{};
SMS_CHECK(IndexBinding::try_decode(record.SlotBinding, original));
std::uint64_t different{};
SMS_CHECK(IndexBinding::try_encode(1 - original.slot_index, original.generation, different));
MappedAtomic64::store_release(record.SlotBinding, different);
break;
}
}
if (accessor == 0) SMS_CHECK(!lease.valid());
if (accessor == 1) SMS_CHECK(lease.value().empty());
if (accessor == 2) SMS_CHECK(lease.descriptor().empty());
SMS_CHECK(MappedAtomic64::load_acquire(header.Control) == sms2_store_corrupt);
SMS_CHECK(store.try_publish(sms_test_bytes("another"), sms_test_bytes("value")) ==
status::corrupt_store);
}
}
return 0;
}
81 changes: 81 additions & 0 deletions tests/cpp/lease_projection_schedule_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "checkpoint.hpp"
#include "interop_faults.hpp"
#include "mapped_atomic.hpp"
#include "test_support.hpp"

namespace {

class ProjectionObserver final : public sms::test_detail::CheckpointObserver {
public:
ProjectionObserver(shared_memory_store::memory_store& store,
shared_memory_store::value_lease& original,
sms::detail::LeaseRecordV2& record, int action)
: store_(store), original_(original), record_(record), action_(action) {}

void reach(sms::test_detail::CheckpointId checkpoint) noexcept override {
if (fired || checkpoint != sms::test_detail::CheckpointId::
ProjectAfterMetadataReadBeforeControlRevalidation) return;
fired = true;
if (action_ == 0) {
const auto active = sms::detail::MappedAtomic64::load_acquire(record_.Control);
sms::detail::MappedAtomic64::store_release(
record_.Control, active & ((1ULL << 36) - 1));
succeeded = true;
} else {
succeeded = original_.release() == shared_memory_store::status::success;
if (action_ == 2) {
succeeded = succeeded && store_.try_acquire(
sms_test_bytes("other"), replacement) == shared_memory_store::status::success;
}
}
}

bool fired{};
bool succeeded{};
shared_memory_store::value_lease replacement;

private:
shared_memory_store::memory_store& store_;
shared_memory_store::value_lease& original_;
sms::detail::LeaseRecordV2& record_;
int action_{};
};

} // namespace

int main() {
using namespace shared_memory_store;
using namespace sms::detail;

for (int action = 0; action < 3; ++action) {
for (int accessor = 0; accessor < 3; ++accessor) {
auto options = sms_test_options("lease-projection-schedule", 2, 1);
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);
SMS_CHECK(store.try_publish(sms_test_bytes("other"), sms_test_bytes("replacement")) ==
status::success);
value_lease original;
SMS_CHECK(store.try_acquire(sms_test_bytes("key"), original) == status::success);
sms::interop_test::raw_mapping mapping(options);
auto& header = sms::interop_test::validate_raw_mapping(mapping, options);
auto& record = *reinterpret_cast<LeaseRecordV2*>(mapping.data() + header.LeaseRegistryOffset);
ProjectionObserver observer(store, original, record, action);
{
sms::test_detail::ScopedCheckpointObserver scoped(observer);
if (accessor == 0) SMS_CHECK(!original.valid());
if (accessor == 1) SMS_CHECK(original.value().empty());
if (accessor == 2) SMS_CHECK(original.descriptor().empty());
}
SMS_CHECK(observer.fired && observer.succeeded);
SMS_CHECK(MappedAtomic64::load_acquire(header.Control) ==
(action == 0 ? sms2_store_corrupt : sms2_store_ready));
if (action == 2) {
SMS_CHECK(observer.replacement.valid());
SMS_CHECK(observer.replacement.value().size() == 11);
}
}
}
return 0;
}
Loading