hibana 0.9.3

Session-typed choreographic programming for no_std Rust protocols, inspired by affine MPST
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Endpoint arena initialization.
//!
//! # Unsafe Owner Contract
//!
//! This module owns construction of a `CursorEndpoint` inside a rendezvous
//! lease arena. Unsafe operations write each arena section exactly once using
//! offsets computed by `CursorEndpointStorageLayout`; the caller supplies an
//! exclusive, aligned arena and keeps it resident for the endpoint lifetime.

use crate::endpoint::affine::LaneGuard;
use crate::endpoint::carrier::EndpointOps;
use crate::endpoint::kernel::decision_state::{
    RouteCommitRowSetBuilder, RouteState, RouteStateCapacity, RouteStateStorage,
};
use crate::endpoint::kernel::frontier_state::{
    FrontierState, FrontierStateCapacity, FrontierStateStorage, RootFrontierCapacity,
    RootFrontierStorage,
};
use crate::endpoint::session::SessionCtx;
use crate::global::compiled::images::RoleDescriptorRef;
use crate::global::role_program::DenseLaneOrdinal;
use crate::global::typestate::EventCursor;
use crate::rendezvous::core::EndpointLeaseId;
use crate::rendezvous::port::Port;
use crate::session::brand::Owner;
use crate::session::types::{RendezvousId, SessionId};
use crate::transport::Transport;

use super::CursorEndpoint;
use super::CursorEndpointStorageLayout;
use super::evidence_store::ScopeEvidenceSlot;
use super::layout::EndpointArenaSection;
use super::layout::LeasedState;

#[inline(always)]
unsafe fn section_ptr<T>(base: *mut u8, section: EndpointArenaSection) -> *mut T {
    /* SAFETY: `CursorEndpointStorageLayout` produced `section` for the endpoint
    arena passed by the rendezvous lease. The section offset/alignment/size were
    checked when the arena layout was built. */
    unsafe { base.add(section.offset()).cast::<T>() }
}

struct EndpointHeaderInit<'r, const ROLE: u8, T>
where
    T: Transport + 'r,
{
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    storage_base: *mut u8,
    storage_layout: CursorEndpointStorageLayout,
    logical_lane_count: usize,
    primary_lane: usize,
    sid: SessionId,
    owner: Owner<'r>,
    public_rv: RendezvousId,
    public_slot: EndpointLeaseId,
    public_generation: u32,
    public_ops: EndpointOps<'r>,
    public_slot_ownership: super::core::PublicSlotOwnership,
    session: SessionCtx<'r, T>,
}

#[inline(never)]
unsafe fn init_endpoint_header<'r, const ROLE: u8, T>(init: EndpointHeaderInit<'r, ROLE, T>)
where
    T: Transport + 'r,
{
    let EndpointHeaderInit {
        dst,
        storage_base,
        storage_layout,
        logical_lane_count,
        primary_lane,
        sid,
        owner,
        public_rv,
        public_slot,
        public_generation,
        public_ops,
        public_slot_ownership,
        session,
    } = init;
    /* SAFETY: `init_empty_from_compiled` passes the unpublished
    `CursorEndpoint` header field set and the endpoint arena base. This block
    initializes public state, lane slots, and session context before returning
    the endpoint to the rendezvous lease. */
    unsafe {
        super::lane_slots::LaneSlotArray::init_from_parts(
            ::core::ptr::addr_of_mut!((*dst).ports),
            storage_base
                .add(storage_layout.port_slots_offset())
                .cast::<Option<Port<'r, T>>>(),
            logical_lane_count,
        );
        super::lane_slots::LaneSlotArray::init_from_parts(
            ::core::ptr::addr_of_mut!((*dst).guards),
            storage_base
                .add(storage_layout.guard_slots_offset())
                .cast::<Option<LaneGuard<'r, T>>>(),
            logical_lane_count,
        );
        ::core::ptr::addr_of_mut!((*dst).public_header).write(
            crate::endpoint::carrier::KernelEndpointHeader::new(
                public_ops,
                public_generation,
                ROLE,
            ),
        );
        ::core::ptr::addr_of_mut!((*dst).primary_lane).write(primary_lane);
        ::core::ptr::addr_of_mut!((*dst).sid).write(sid);
        ::core::ptr::addr_of_mut!((*dst)._owner).write(owner);
        ::core::ptr::addr_of_mut!((*dst).public_rv).write(public_rv);
        ::core::ptr::addr_of_mut!((*dst).public_slot).write(public_slot);
        ::core::ptr::addr_of_mut!((*dst).public_generation).write(public_generation);
        ::core::ptr::addr_of_mut!((*dst).public_slot_ownership).write(public_slot_ownership);
        ::core::ptr::addr_of_mut!((*dst).public_active_op).write(super::core::PublicActiveOp::Idle);
        ::core::ptr::addr_of_mut!((*dst).public_offer_state).write(super::offer::OfferState::new());
        ::core::ptr::addr_of_mut!((*dst).public_route_branch).write(None);
        ::core::ptr::addr_of_mut!((*dst).public_recv_state).write(super::recv::RecvState::new());
        ::core::ptr::addr_of_mut!((*dst).public_branch_recv_state)
            .write(super::branch_recv::BranchRecvState::empty());
        ::core::ptr::addr_of_mut!((*dst).public_send_state).write(super::core::SendState::Done);
        ::core::ptr::addr_of_mut!((*dst).session).write(session);
    }
}

#[inline(never)]
unsafe fn init_endpoint_cursor<'r, const ROLE: u8, T>(
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    arena_storage: *mut u8,
    arena_layout: &crate::endpoint::kernel::layout::EndpointArenaLayout,
    role_descriptor: RoleDescriptorRef,
) where
    T: Transport + 'r,
{
    /* SAFETY: `dst.cursor` is still unpublished, and all cursor backing
    columns come from the endpoint arena sections selected by the compiled role
    descriptor. `EventCursor::init_from_compiled` initializes the cursor before
    endpoint publication. */
    unsafe {
        EventCursor::init_from_compiled(
            ::core::ptr::addr_of_mut!((*dst).cursor),
            section_ptr::<crate::global::typestate::EventCursorState>(
                arena_storage,
                arena_layout.event_cursor_state(),
            ),
            section_ptr::<u16>(arena_storage, arena_layout.event_cursor_lane_cursors()),
            section_ptr::<u16>(
                arena_storage,
                arena_layout.event_cursor_current_step_label_codes(),
            ),
            section_ptr::<u32>(
                arena_storage,
                arena_layout.event_cursor_completed_event_words(),
            ),
            role_descriptor,
        );
    }
}

#[inline(never)]
unsafe fn init_endpoint_route<'r, const ROLE: u8, T>(
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    arena_storage: *mut u8,
    arena_layout: &crate::endpoint::kernel::layout::EndpointArenaLayout,
    role_descriptor: RoleDescriptorRef,
) where
    T: Transport + 'r,
{
    /* SAFETY: route-state initialization owns the unpublished endpoint route
    fields. Every pointer passed to `RouteState::init_empty` is a section of the
    endpoint arena, and active-lane density is filled before route state reads it. */
    unsafe {
        let active_lane_dense_by_lane = section_ptr::<DenseLaneOrdinal>(
            arena_storage,
            arena_layout.route_state_lane_dense_by_lane(),
        );
        let active_lane_count =
            role_descriptor.fill_active_lane_dense_by_lane(core::slice::from_raw_parts_mut(
                active_lane_dense_by_lane,
                arena_layout.route_state_lane_dense_by_lane().count(),
            ));
        let decision_state =
            section_ptr::<RouteState>(arena_storage, arena_layout.decision_state());
        LeasedState::init_from_ptr(
            ::core::ptr::addr_of_mut!((*dst).decision_state),
            decision_state,
        );
        let route_commit_row_set_builder = section_ptr::<RouteCommitRowSetBuilder>(
            arena_storage,
            arena_layout.route_commit_row_set_builder(),
        );
        LeasedState::init_from_ptr(
            ::core::ptr::addr_of_mut!((*dst).route_commit_rows),
            route_commit_row_set_builder,
        );
        RouteCommitRowSetBuilder::init(
            route_commit_row_set_builder,
            role_descriptor.max_route_stack_depth().max(1),
        );
        RouteState::init_empty(
            decision_state,
            RouteStateStorage {
                route_arm_storage: section_ptr::<crate::endpoint::kernel::evidence::RouteArmState>(
                    arena_storage,
                    arena_layout.route_arm_stack(),
                ),
                lane_offer_state_storage: section_ptr::<
                    crate::endpoint::kernel::frontier::LaneOfferState,
                >(
                    arena_storage, arena_layout.lane_offer_state_slots()
                ),
                scope_evidence_slots: section_ptr::<ScopeEvidenceSlot>(
                    arena_storage,
                    arena_layout.scope_evidence_slots(),
                ),
                scope_selected_arms: section_ptr::<
                    crate::endpoint::kernel::decision_state::RouteScopeSelectedArmSlot,
                >(
                    arena_storage,
                    arena_layout.route_state_scope_selected_arms(),
                ),
                lane_dense_by_lane: active_lane_dense_by_lane,
                lane_route_arm_lens: section_ptr::<u8>(
                    arena_storage,
                    arena_layout.route_state_lane_route_arm_lens(),
                ),
                lane_reentry_counts: section_ptr::<u8>(
                    arena_storage,
                    arena_layout.route_state_lane_reentry_counts(),
                ),
                lane_reentry_words: section_ptr::<crate::global::role_program::LaneWord>(
                    arena_storage,
                    arena_layout.route_state_lane_reentry_lanes(),
                ),
                lane_offer_reentry_words: section_ptr::<crate::global::role_program::LaneWord>(
                    arena_storage,
                    arena_layout.route_state_lane_offer_reentry_lanes(),
                ),
                active_offer_lane_words: section_ptr::<crate::global::role_program::LaneWord>(
                    arena_storage,
                    arena_layout.route_state_active_offer_lanes(),
                ),
            },
            RouteStateCapacity {
                lane_slot_count: arena_layout.route_state_lane_dense_by_lane().count(),
                active_lane_count,
                lane_word_count: arena_layout.route_state_lane_reentry_lanes().count(),
                lane_offer_state_count: arena_layout.lane_offer_state_slots().count(),
                route_frame_depth: role_descriptor.max_route_stack_depth(),
                scope_evidence_count: arena_layout.scope_evidence_slots().count(),
                scope_selected_arm_count: arena_layout.route_state_scope_selected_arms().count(),
            },
        );
    }
}

#[inline(never)]
unsafe fn init_endpoint_frontier<'r, const ROLE: u8, T>(
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    arena_storage: *mut u8,
    arena_layout: &crate::endpoint::kernel::layout::EndpointArenaLayout,
) where
    T: Transport + 'r,
{
    /* SAFETY: frontier initialization owns the unpublished endpoint frontier
    field. The root rows, active slots, observation slots, and offer-entry slots
    are disjoint arena sections selected by the compiled endpoint layout. */
    unsafe {
        let frontier_state =
            section_ptr::<FrontierState>(arena_storage, arena_layout.frontier_state());
        LeasedState::init_from_ptr(
            ::core::ptr::addr_of_mut!((*dst).frontier_state),
            frontier_state,
        );
        FrontierState::init_empty(
            frontier_state,
            FrontierStateStorage {
                root: RootFrontierStorage {
                    rows: section_ptr::<crate::endpoint::kernel::frontier::RootFrontierState>(
                        arena_storage,
                        arena_layout.frontier_root_rows(),
                    ),
                    active_entries: section_ptr::<crate::endpoint::kernel::frontier::ActiveEntrySlot>(
                        arena_storage,
                        arena_layout.frontier_root_active_slots(),
                    ),
                },
                offer_entry_slots: section_ptr::<crate::endpoint::kernel::frontier::OfferEntrySlot>(
                    arena_storage,
                    arena_layout.frontier_offer_entry_slots(),
                ),
            },
            FrontierStateCapacity {
                root: RootFrontierCapacity {
                    row_count: arena_layout.frontier_root_rows().count(),
                    pool_capacity: arena_layout.frontier_root_active_slots().count(),
                },
                max_offer_entries: arena_layout.frontier_offer_entry_slots().count(),
            },
        );
    }
}

pub(crate) struct CompiledEndpointInit<'r, const ROLE: u8, T: Transport + 'r> {
    pub(crate) dst: *mut CursorEndpoint<'r, ROLE, T>,
    pub(crate) arena_storage: *mut u8,
    pub(crate) primary_lane: usize,
    pub(crate) sid: SessionId,
    pub(crate) owner: Owner<'r>,
    pub(crate) role_descriptor: RoleDescriptorRef,
    pub(crate) public_rv: RendezvousId,
    pub(crate) public_slot: EndpointLeaseId,
    pub(crate) public_generation: u32,
    pub(crate) public_ops: EndpointOps<'r>,
    pub(crate) public_slot_ownership: super::core::PublicSlotOwnership,
    pub(crate) session: SessionCtx<'r, T>,
}

pub(crate) unsafe fn init_empty_from_compiled<'r, const ROLE: u8, T>(
    init: CompiledEndpointInit<'r, ROLE, T>,
) where
    T: Transport + 'r,
{
    let CompiledEndpointInit {
        dst,
        arena_storage,
        primary_lane,
        sid,
        owner,
        role_descriptor,
        public_rv,
        public_slot,
        public_generation,
        public_ops,
        public_slot_ownership,
        session,
    } = init;
    let arena_layout = role_descriptor.endpoint_arena_layout();
    let lane_slot_count = role_descriptor.endpoint_lane_slot_count();
    let storage_layout =
        super::cursor_endpoint_storage_layout::<ROLE, T>(&arena_layout, lane_slot_count);
    let storage_base = dst.cast::<u8>();
    /* SAFETY: the rendezvous lease passes the unpublished endpoint allocation.
    Header, cursor, route, and frontier owners initialize disjoint fields and
    arena sections before the endpoint is made reachable. */
    unsafe {
        init_endpoint_header(EndpointHeaderInit {
            dst,
            storage_base,
            storage_layout,
            logical_lane_count: lane_slot_count,
            primary_lane,
            sid,
            owner,
            public_rv,
            public_slot,
            public_generation,
            public_ops,
            public_slot_ownership,
            session,
        });
        init_endpoint_cursor(dst, arena_storage, &arena_layout, role_descriptor);
        init_endpoint_route(dst, arena_storage, &arena_layout, role_descriptor);
        init_endpoint_frontier(dst, arena_storage, &arena_layout);
    }
}

pub(crate) unsafe fn write_port_slot<'r, const ROLE: u8, T>(
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    logical_lane: usize,
    port: Port<'r, T>,
) where
    T: Transport + 'r,
{
    /* SAFETY: endpoint attachment owns `dst` while installing lane resources.
    `logical_lane` indexes the initialized port-slot array, and the write occurs
    before a public endpoint borrow can access that lane. */
    unsafe {
        (&mut *dst).ports[logical_lane] = Some(port);
    }
}

pub(crate) unsafe fn write_guard_slot<'r, const ROLE: u8, T>(
    dst: *mut CursorEndpoint<'r, ROLE, T>,
    logical_lane: usize,
    guard: LaneGuard<'r, T>,
) where
    T: Transport + 'r,
{
    /* SAFETY: endpoint attachment owns `dst` while installing lane resources.
    `logical_lane` indexes the initialized guard-slot array, and this consumes
    the guard into that one slot. */
    unsafe {
        (&mut *dst).guards[logical_lane] = Some(guard);
    }
}

pub(crate) unsafe fn finish_init<'r, const ROLE: u8, T>(dst: *mut CursorEndpoint<'r, ROLE, T>)
where
    T: Transport + 'r,
{
    /* SAFETY: endpoint initialization still owns `dst`; lane offer state is
    synchronized after all lane slots and route/frontier sections have been
    initialized and before endpoint publication. */
    unsafe {
        (&mut *dst).sync_lane_offer_state();
    }
}