hibana 0.2.0

Const-projected Affine Multiparty Session Types for choreography-first Rust protocols
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use core::{marker::PhantomData, ptr::NonNull};

use crate::global::{
    role_program::{LocalStep, PhaseRouteGuard, RoleFootprint, RoleLoweringInput},
    typestate::{RoleTypestateBuildScratch, StateIndex},
};

use super::super::images::{CompiledProgramFacts, CompiledRoleImage};
use super::super::lowering::{CompiledRoleImageInitError, LoweringSummary, ProgramStamp};
use super::super::lowering::{program_image_builder, role_image_builder};
#[cfg(test)]
use core::ptr;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LoweringLeaseMode {
    SummaryOnly,
    SummaryAndRoleScratch,
}

pub(crate) struct RoleLoweringScratch<'a> {
    typestate_build: RoleTypestateBuildScratch,
    steps: *mut LocalStep,
    steps_len: usize,
    eff_index_to_step: *mut u16,
    eff_index_to_step_len: usize,
    step_index_to_state: *mut StateIndex,
    step_index_to_state_len: usize,
    route_guards: *mut PhaseRouteGuard,
    route_guards_len: usize,
    parallel_ranges: *mut (usize, usize),
    parallel_ranges_len: usize,
    _marker: PhantomData<&'a mut ()>,
}

impl<'a> RoleLoweringScratch<'a> {
    #[inline(always)]
    fn ptr_or_dangling<T>(ptr: usize, len: usize) -> *mut T {
        if len == 0 {
            NonNull::<T>::dangling().as_ptr()
        } else {
            ptr as *mut T
        }
    }

    #[inline(always)]
    pub(crate) unsafe fn init_empty(&mut self) {
        unsafe {
            self.typestate_build.init_empty();
        }
    }

    #[inline(always)]
    pub(crate) fn typestate_build_mut(&mut self) -> &mut RoleTypestateBuildScratch {
        &mut self.typestate_build
    }

    pub(crate) fn local_step_build_slices_mut(&mut self) -> (&mut [LocalStep], &mut [u16]) {
        unsafe {
            (
                core::slice::from_raw_parts_mut(self.steps, self.steps_len),
                core::slice::from_raw_parts_mut(self.eff_index_to_step, self.eff_index_to_step_len),
            )
        }
    }

    pub(crate) fn eff_index_to_step(&self) -> &[u16] {
        unsafe { core::slice::from_raw_parts(self.eff_index_to_step, self.eff_index_to_step_len) }
    }

    #[inline(always)]
    pub(crate) fn step_state_build_slices_mut(
        &mut self,
    ) -> (&[LocalStep], &[u16], &mut [StateIndex]) {
        unsafe {
            (
                core::slice::from_raw_parts(self.steps, self.steps_len),
                core::slice::from_raw_parts(self.eff_index_to_step, self.eff_index_to_step_len),
                core::slice::from_raw_parts_mut(
                    self.step_index_to_state,
                    self.step_index_to_state_len,
                ),
            )
        }
    }

    #[inline(always)]
    pub(crate) fn step_index_to_state(&self) -> &[StateIndex] {
        unsafe {
            core::slice::from_raw_parts(self.step_index_to_state, self.step_index_to_state_len)
        }
    }

    #[inline(always)]
    pub(crate) fn phase_build_slices_mut(
        &mut self,
    ) -> (
        &[LocalStep],
        &[StateIndex],
        &mut [PhaseRouteGuard],
        &mut [(usize, usize)],
    ) {
        unsafe {
            (
                core::slice::from_raw_parts(self.steps, self.steps_len),
                core::slice::from_raw_parts(self.step_index_to_state, self.step_index_to_state_len),
                core::slice::from_raw_parts_mut(self.route_guards, self.route_guards_len),
                core::slice::from_raw_parts_mut(self.parallel_ranges, self.parallel_ranges_len),
            )
        }
    }
}

#[derive(Clone, Copy)]
struct RoleLoweringScratchLayout {
    typestate_build: usize,
    steps: usize,
    eff_index_to_step: usize,
    step_index_to_state: usize,
    route_guards: usize,
    parallel_ranges: usize,
    end: usize,
}

impl RoleLoweringScratchLayout {
    #[inline(always)]
    const fn align_up(value: usize, align: usize) -> usize {
        let mask = align.saturating_sub(1);
        (value + mask) & !mask
    }

    #[inline(always)]
    const fn eff_index_count(footprint: RoleFootprint) -> usize {
        footprint.eff_count
    }

    #[inline(always)]
    const fn step_count(footprint: RoleFootprint) -> usize {
        footprint.local_step_count
    }

    #[inline(always)]
    const fn parallel_range_count(footprint: RoleFootprint) -> usize {
        footprint.parallel_enter_count
    }

    #[inline(always)]
    const fn typestate_node_count(footprint: RoleFootprint) -> usize {
        let capped = footprint
            .local_step_count
            .saturating_add(footprint.scope_count)
            .saturating_add(footprint.passive_linger_route_scope_count)
            .saturating_add(1);
        if capped == 0 { 1 } else { capped }
    }

    #[inline(always)]
    const fn from_start(start: usize, footprint: RoleFootprint) -> Self {
        let eff_index_count = Self::eff_index_count(footprint);
        let step_count = Self::step_count(footprint);
        let parallel_range_count = Self::parallel_range_count(footprint);
        let typestate_node_count = Self::typestate_node_count(footprint);

        let typestate_build = Self::align_up(start, RoleTypestateBuildScratch::storage_align());
        let typestate_build_end = RoleTypestateBuildScratch::storage_end_from_start(
            typestate_build,
            typestate_node_count,
            footprint.scope_count,
            footprint.max_active_scope_depth,
        );
        let steps = Self::align_up(typestate_build_end, core::mem::align_of::<LocalStep>());
        let steps_end = steps + step_count.saturating_mul(core::mem::size_of::<LocalStep>());
        let eff_index_to_step = Self::align_up(steps_end, core::mem::align_of::<u16>());
        let eff_index_to_step_end =
            eff_index_to_step + eff_index_count.saturating_mul(core::mem::size_of::<u16>());
        let step_index_to_state =
            Self::align_up(eff_index_to_step_end, core::mem::align_of::<StateIndex>());
        let step_index_to_state_end =
            step_index_to_state + step_count.saturating_mul(core::mem::size_of::<StateIndex>());
        let route_guards = Self::align_up(
            step_index_to_state_end,
            core::mem::align_of::<PhaseRouteGuard>(),
        );
        let route_guards_end =
            route_guards + step_count.saturating_mul(core::mem::size_of::<PhaseRouteGuard>());
        let parallel_ranges =
            Self::align_up(route_guards_end, core::mem::align_of::<(usize, usize)>());
        let end = parallel_ranges
            + parallel_range_count.saturating_mul(core::mem::size_of::<(usize, usize)>());
        Self {
            typestate_build,
            steps,
            eff_index_to_step,
            step_index_to_state,
            route_guards,
            parallel_ranges,
            end,
        }
    }

    #[inline(always)]
    unsafe fn scratch<'a>(self, footprint: RoleFootprint) -> RoleLoweringScratch<'a> {
        let eff_index_count = Self::eff_index_count(footprint);
        let step_count = Self::step_count(footprint);
        let parallel_range_count = Self::parallel_range_count(footprint);
        let typestate_node_count = Self::typestate_node_count(footprint);
        RoleLoweringScratch {
            typestate_build: unsafe {
                RoleTypestateBuildScratch::from_storage(
                    self.typestate_build,
                    typestate_node_count,
                    footprint.scope_count,
                    footprint.max_active_scope_depth,
                )
            },
            steps: RoleLoweringScratch::ptr_or_dangling(self.steps, step_count),
            steps_len: step_count,
            eff_index_to_step: RoleLoweringScratch::ptr_or_dangling(
                self.eff_index_to_step,
                eff_index_count,
            ),
            eff_index_to_step_len: eff_index_count,
            step_index_to_state: RoleLoweringScratch::ptr_or_dangling(
                self.step_index_to_state,
                step_count,
            ),
            step_index_to_state_len: step_count,
            route_guards: RoleLoweringScratch::ptr_or_dangling(self.route_guards, step_count),
            route_guards_len: step_count,
            parallel_ranges: RoleLoweringScratch::ptr_or_dangling(
                self.parallel_ranges,
                parallel_range_count,
            ),
            parallel_ranges_len: parallel_range_count,
            _marker: PhantomData,
        }
    }
}

pub(crate) struct LoweringLease<'a> {
    summary: &'a LoweringSummary,
    role_lowering_scratch: Option<RoleLoweringScratch<'a>>,
}

impl<'a> LoweringLease<'a> {
    #[inline(always)]
    pub(crate) const fn summary(&self) -> &'a LoweringSummary {
        self.summary
    }

    #[inline(always)]
    pub(crate) fn into_parts(self) -> (&'a LoweringSummary, Option<RoleLoweringScratch<'a>>) {
        (self.summary, self.role_lowering_scratch)
    }
}

struct TransientLoweringLeaseStorage<'a> {
    role_lowering_scratch: Option<RoleLoweringScratch<'a>>,
}

impl<'a> TransientLoweringLeaseStorage<'a> {
    #[cfg(test)]
    #[inline(always)]
    const fn align_up(value: usize, align: usize) -> usize {
        let mask = align.saturating_sub(1);
        (value + mask) & !mask
    }

    #[inline(always)]
    const fn required_bytes_from_base(
        base: usize,
        mode: LoweringLeaseMode,
        footprint: RoleFootprint,
    ) -> usize {
        match mode {
            LoweringLeaseMode::SummaryOnly => 0,
            LoweringLeaseMode::SummaryAndRoleScratch => {
                RoleLoweringScratchLayout::from_start(base, footprint).end - base
            }
        }
    }

    #[inline]
    unsafe fn from_storage(
        storage: *mut u8,
        len: usize,
        mode: LoweringLeaseMode,
        footprint: RoleFootprint,
    ) -> Option<Self> {
        let base = storage as usize;
        let required = Self::required_bytes_from_base(base, mode, footprint);
        if required > len {
            return None;
        }
        Some(Self {
            role_lowering_scratch: match mode {
                LoweringLeaseMode::SummaryOnly => None,
                LoweringLeaseMode::SummaryAndRoleScratch => Some(unsafe {
                    RoleLoweringScratchLayout::from_start(base, footprint).scratch(footprint)
                }),
            },
        })
    }

    #[inline]
    unsafe fn init(
        self,
        source: crate::global::role_program::RoleImageSource,
        stamp: ProgramStamp,
    ) -> LoweringLease<'a> {
        unsafe {
            let summary = source.summary();
            debug_assert_eq!(summary.stamp(), stamp);
            let mut role_lowering_scratch = self.role_lowering_scratch;
            if let Some(scratch) = role_lowering_scratch.as_mut() {
                scratch.init_empty();
            }
            LoweringLease {
                summary,
                role_lowering_scratch,
            }
        }
    }
}

#[inline(always)]
#[cfg(test)]
const fn lowering_lease_storage_align() -> usize {
    let mut align = core::mem::align_of::<LoweringSummary>();
    if RoleTypestateBuildScratch::storage_align() > align {
        align = RoleTypestateBuildScratch::storage_align();
    }
    if core::mem::align_of::<LocalStep>() > align {
        align = core::mem::align_of::<LocalStep>();
    }
    if core::mem::align_of::<bool>() > align {
        align = core::mem::align_of::<bool>();
    }
    if core::mem::align_of::<u16>() > align {
        align = core::mem::align_of::<u16>();
    }
    if core::mem::align_of::<StateIndex>() > align {
        align = core::mem::align_of::<StateIndex>();
    }
    if core::mem::align_of::<PhaseRouteGuard>() > align {
        align = core::mem::align_of::<PhaseRouteGuard>();
    }
    if core::mem::align_of::<(usize, usize)>() > align {
        align = core::mem::align_of::<(usize, usize)>();
    }
    align
}

#[cfg(test)]
pub(crate) const fn lowering_lease_storage_bytes(
    footprint: RoleFootprint,
    mode: LoweringLeaseMode,
) -> usize {
    let max_align = lowering_lease_storage_align();
    let mut max_required = 0usize;
    let mut base = 0usize;
    while base < max_align {
        let required =
            TransientLoweringLeaseStorage::required_bytes_from_base(base, mode, footprint);
        if required > max_required {
            max_required = required;
        }
        base += 1;
    }
    max_required
}

#[cfg(test)]
pub(crate) const fn role_lowering_scratch_storage_bytes(footprint: RoleFootprint) -> usize {
    let max_align = lowering_lease_storage_align();
    let mut max_required = 0usize;
    let mut base = 0usize;
    while base < max_align {
        let required = RoleLoweringScratchLayout::from_start(base, footprint).end - base;
        if required > max_required {
            max_required = required;
        }
        base += 1;
    }
    max_required
}

#[inline]
pub(crate) unsafe fn with_lowering_lease<R>(
    input: RoleLoweringInput,
    storage: *mut u8,
    len: usize,
    mode: LoweringLeaseMode,
    f: impl FnOnce(LoweringLease<'_>) -> R,
) -> Option<R> {
    debug_assert_eq!(input.start(), crate::eff::EffIndex::ZERO);
    let storage = unsafe {
        TransientLoweringLeaseStorage::from_storage(storage, len, mode, input.footprint())
    }?;
    let lease = unsafe { storage.init(input.source(), input.stamp()) };
    Some(f(lease))
}

#[cfg(test)]
pub(crate) unsafe fn with_role_lowering_scratch_storage<R>(
    footprint: RoleFootprint,
    storage: *mut u8,
    len: usize,
    f: impl FnOnce(&mut RoleLoweringScratch<'_>) -> R,
) -> Option<R> {
    let base = storage as usize;
    let layout = RoleLoweringScratchLayout::from_start(base, footprint);
    let required = layout.end - base;
    if required > len {
        return None;
    }
    let mut scratch = unsafe { layout.scratch(footprint) };
    unsafe {
        scratch.init_empty();
    }
    Some(f(&mut scratch))
}

pub(crate) unsafe fn init_compiled_program_image_from_summary(
    dst: *mut CompiledProgramFacts,
    summary: &LoweringSummary,
) {
    unsafe {
        program_image_builder::init_compiled_program_image_from_summary(dst, summary);
    }
}

#[cfg(test)]
pub(crate) unsafe fn init_compiled_role_image_from_summary(
    dst: *mut CompiledRoleImage,
    role: u8,
    summary: &LoweringSummary,
    scratch: &mut RoleLoweringScratch<'_>,
    footprint: RoleFootprint,
) {
    unsafe {
        role_image_builder::init_compiled_role_image_from_summary(
            dst, role, summary, scratch, footprint,
        );
    }
}

pub(crate) unsafe fn try_init_compiled_role_image_from_summary(
    dst: *mut CompiledRoleImage,
    role: u8,
    summary: &LoweringSummary,
    scratch: &mut RoleLoweringScratch<'_>,
    footprint: RoleFootprint,
) -> Result<(), CompiledRoleImageInitError> {
    unsafe {
        role_image_builder::try_init_compiled_role_image_from_summary(
            dst, role, summary, scratch, footprint,
        )
    }
}

pub(crate) unsafe fn validate_compiled_role_image_init_from_summary(
    dst: *mut CompiledRoleImage,
    role: u8,
    summary: &LoweringSummary,
    footprint: RoleFootprint,
) -> Result<(), CompiledRoleImageInitError> {
    unsafe {
        role_image_builder::validate_compiled_role_image_init_from_summary(
            dst, role, summary, footprint,
        )
    }
}

pub(crate) unsafe fn init_compiled_role_image_from_prevalidated_summary(
    dst: *mut CompiledRoleImage,
    role: u8,
    summary: &LoweringSummary,
    scratch: &mut RoleLoweringScratch<'_>,
    footprint: RoleFootprint,
) -> usize {
    unsafe {
        role_image_builder::init_compiled_role_image_from_prevalidated_summary(
            dst, role, summary, scratch, footprint,
        )
    }
}

#[cfg(test)]
pub(crate) fn with_compiled_program<R>(
    input: RoleLoweringInput,
    f: impl FnOnce(&CompiledProgramFacts) -> R,
) -> R {
    const STORAGE_BYTES: usize =
        CompiledProgramFacts::max_persistent_bytes() + CompiledProgramFacts::persistent_align();
    static STORAGE: std::sync::Mutex<[u8; STORAGE_BYTES]> =
        std::sync::Mutex::new([0u8; STORAGE_BYTES]);

    let mut storage = STORAGE
        .lock()
        .expect("compiled program test storage lock poisoned");
    unsafe {
        let summary = input.source().summary();
        let base = storage.as_mut_ptr() as usize;
        let align = CompiledProgramFacts::persistent_align();
        let aligned =
            TransientLoweringLeaseStorage::align_up(base, align) as *mut CompiledProgramFacts;
        assert!(
            (aligned as usize) + CompiledProgramFacts::max_persistent_bytes()
                <= base + storage.len(),
            "compiled program test storage alignment exceeded backing storage"
        );
        init_compiled_program_image_from_summary(aligned, summary);
        let result = f(&*aligned);
        ptr::drop_in_place(aligned);
        result
    }
}

#[cfg(test)]
pub(crate) fn with_role_lowering_scratch<R>(
    input: RoleLoweringInput,
    f: impl FnOnce(&LoweringSummary, &mut RoleLoweringScratch<'_>) -> R,
) -> R {
    let footprint = input.footprint();
    let mut storage = std::vec::Vec::with_capacity(lowering_lease_storage_bytes(
        footprint,
        LoweringLeaseMode::SummaryAndRoleScratch,
    ));
    storage.resize(
        lowering_lease_storage_bytes(footprint, LoweringLeaseMode::SummaryAndRoleScratch),
        0u8,
    );
    unsafe {
        with_lowering_lease(
            input,
            storage.as_mut_ptr(),
            storage.len(),
            LoweringLeaseMode::SummaryAndRoleScratch,
            |lease| {
                let (summary, role_lowering_scratch) = lease.into_parts();
                let scratch = role_lowering_scratch
                    .expect("summary-and-scratch lowering lease must provide role scratch");
                f(summary, &mut { scratch })
            },
        )
        .expect("exact lowering scratch storage must fit derived footprint")
    }
}

#[cfg(test)]
pub(crate) fn with_compiled_role_image<const ROLE: u8, R>(
    input: RoleLoweringInput,
    f: impl FnOnce(&CompiledRoleImage) -> R,
) -> R {
    let footprint = input.footprint();
    let bytes = CompiledRoleImage::persistent_bytes_for_program(footprint);
    let align = CompiledRoleImage::persistent_align();
    let mut storage = std::vec::Vec::with_capacity(bytes + align);
    storage.resize(bytes + align, 0u8);
    let base = storage.as_mut_ptr() as usize;
    let aligned = TransientLoweringLeaseStorage::align_up(base, align) as *mut CompiledRoleImage;
    debug_assert!((aligned as usize) + bytes <= base + storage.len());
    with_role_lowering_scratch(input, |summary, scratch| unsafe {
        init_compiled_role_image_from_summary(aligned, ROLE, summary, scratch, footprint);
        let result = f(&*aligned);
        ptr::drop_in_place(aligned);
        result
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lowering_lease_role_scratch_bytes_follow_footprint() {
        let empty = RoleFootprint {
            scope_count: 0,
            max_active_scope_depth: 0,
            eff_count: 0,
            phase_count: 0,
            phase_lane_entry_count: 0,
            phase_lane_word_count: 0,
            parallel_enter_count: 0,
            route_scope_count: 0,
            local_step_count: 0,
            passive_linger_route_scope_count: 0,
            active_lane_count: 0,
            endpoint_lane_slot_count: 0,
            logical_lane_count: 0,
            logical_lane_word_count: 0,
            max_route_stack_depth: 0,
            scope_evidence_count: 0,
            frontier_entry_count: 0,
        };
        let compact = RoleFootprint {
            scope_count: 0,
            max_active_scope_depth: 0,
            eff_count: 3,
            phase_count: 2,
            phase_lane_entry_count: 2,
            phase_lane_word_count: 2,
            parallel_enter_count: 1,
            route_scope_count: 0,
            local_step_count: 2,
            passive_linger_route_scope_count: 0,
            active_lane_count: 0,
            endpoint_lane_slot_count: 0,
            logical_lane_count: 0,
            logical_lane_word_count: 0,
            max_route_stack_depth: 0,
            scope_evidence_count: 0,
            frontier_entry_count: 0,
        };
        let base = 0usize;
        let summary_only_required = TransientLoweringLeaseStorage::required_bytes_from_base(
            base,
            LoweringLeaseMode::SummaryOnly,
            compact,
        );
        let empty_required = TransientLoweringLeaseStorage::required_bytes_from_base(
            base,
            LoweringLeaseMode::SummaryAndRoleScratch,
            empty,
        );
        let compact_required = TransientLoweringLeaseStorage::required_bytes_from_base(
            base,
            LoweringLeaseMode::SummaryAndRoleScratch,
            compact,
        );

        assert_eq!(
            summary_only_required, 0,
            "lowering leases borrow the static summary instead of cloning it into the attach slab"
        );
        assert!(
            compact_required > empty_required,
            "footprint-driven lowering scratch must grow with used role-local facts"
        );
    }
}