indexbus_core/
validate.rs

1//! Runtime layout validation helpers.
2//!
3//! These functions provide a single hardened validation path for shared-memory mappings:
4//! magic/version, capability flags, layout size, and (where applicable) initialization state.
5
6use indexbus_abi::layouts::{
7    JournalLayout4, SequencerLayout, SharedFanoutLayout, SharedLayout, StateLayout,
8};
9
10use crate::{internal, Error};
11
12/// Validate a mapped `SharedLayout`.
13///
14/// Checks:
15/// - v1 header compatibility (magic/version)
16/// - required capabilities
17/// - `layout_bytes` is large enough for the base layout
18/// - initialization state is `2`
19#[inline]
20pub fn validate_shared_layout(shared: &SharedLayout) -> Result<(), Error> {
21    internal::validate::validate_shared_layout(shared)
22}
23
24/// Validate a mapped `SharedFanoutLayout<N>`.
25///
26/// Checks:
27/// - v1 header compatibility (magic/version)
28/// - required capabilities (events + fanout)
29/// - `layout_bytes` is large enough for the base layout
30/// - initialization state is `2`
31#[inline]
32pub fn validate_fanout_layout<const N: usize>(shared: &SharedFanoutLayout<N>) -> Result<(), Error> {
33    internal::validate::validate_fanout_layout::<N>(shared)
34}
35
36/// Validate a mapped `StateLayout<STATE_MAX>`.
37///
38/// Checks:
39/// - v1 header compatibility (magic/version)
40/// - required capabilities (state)
41/// - `layout_bytes` is large enough for the base layout
42/// - NOTE: state regions do not use an `initialized: 0/1/2` field in v1; they rely on the
43///   `seq` parity protocol (even = stable, odd = writer in progress)
44/// - if `INDEXBUS_CAP_SUPPORTS_BLOCKING` is set, ensures the mapping is large enough to hold an
45///   appended `StateWakeSection` at the next 64B boundary
46#[inline]
47pub fn validate_state_layout<const STATE_MAX: usize>(
48    layout: &StateLayout<STATE_MAX>,
49) -> Result<(), Error> {
50    internal::validate::validate_state_layout::<STATE_MAX>(layout)
51}
52
53/// Validate a mapped `SequencerLayout<N>`.
54///
55/// Checks:
56/// - v1 header compatibility (magic/version)
57/// - required capabilities (sequencer)
58/// - `layout_bytes` is large enough for the base layout
59/// - initialization state is `2`
60/// - if `INDEXBUS_CAP_SUPPORTS_BLOCKING` is set, ensures the mapping is large enough to hold an
61///   appended `SequencerWakeSection<N>` at the next 64B boundary
62#[inline]
63pub fn validate_sequencer_layout<const N: usize>(layout: &SequencerLayout<N>) -> Result<(), Error> {
64    internal::validate::validate_sequencer_layout::<N>(layout)
65}
66
67/// Validate a mapped `JournalLayout4`.
68///
69/// Checks:
70/// - v1 header compatibility (magic/version)
71/// - required capabilities (journal)
72/// - `layout_bytes` is large enough for the base layout
73/// - initialization state is `2`
74/// - if `INDEXBUS_CAP_SUPPORTS_BLOCKING` is set, ensures the mapping is large enough to hold an
75///   appended `JournalWakeSection4` at the next 64B boundary
76#[inline]
77pub fn validate_journal_layout4(layout: &JournalLayout4) -> Result<(), Error> {
78    internal::validate::validate_journal_layout4(layout)
79}