lion-core 0.3.0

Lion microkernel — production types, state machine, and kernel API
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
// Copyright (C) 2026 HaiyangLi
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Lion Step Host Call (EROS-style 6 operations)
//!
//! Corresponds to: Lion/Step/HostCall/Core.lean
//!
//! Host function calls that cross the trust boundary between
//! untrusted plugin code and the trusted kernel.
//!
//! EROS consolidation (13 -> 6):
//! - CapCall: Synchronous capability invocation (was cap_invoke)
//! - CapSend: Async message send (was ipc_send; delegation is user-space)
//! - KernelAlloc: Resource allocation (was mem_alloc, resource_create)
//! - MemFree: Memory deallocation (keep separate for safety)
//! - CapRevoke: Capability revocation
//! - Declassify: Security level change

use crate::state::{Message, State};
use crate::types::{Action, Capability, MemAddr, PluginId, Right, Rights, SecurityLevel, Size};

use super::{HostCallPrecondition, StepError};

/// All host functions that cross the trust boundary (EROS-style 6 operations)
///
/// Corresponds to Lean: `inductive HostFunctionId`
///
/// EROS consolidation (13 -> 6):
/// - CapInvoke -> CapCall (synchronous capability invocation)
/// - CapDelegate, CapAccept -> User-space protocol over CapSend
/// - IpcSend -> CapSend (async message send)
/// - IpcReceive -> Removed (mailbox read via CapCall)
/// - MemAlloc, ResourceCreate, ResourceAccess -> KernelAlloc
/// - MemFree -> MemFree (keep separate for safety)
/// - CapRevoke -> CapRevoke (kernel operation)
/// - WorkflowStart, WorkflowStep -> Removed (user-space library)
/// - Declassify -> Declassify (IFC operation)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HostFunction {
    /// Synchronous capability invocation (replaces cap_invoke)
    ///
    /// Corresponds to Lean: `| cap_call`
    CapCall,

    /// Async message send (replaces ipc_send; delegation is user-space protocol)
    ///
    /// Corresponds to Lean: `| cap_send`
    CapSend,

    /// Kernel resource allocation (replaces mem_alloc, resource_create)
    ///
    /// Corresponds to Lean: `| kernel_alloc`
    KernelAlloc,

    /// Memory deallocation (keep separate for safety)
    ///
    /// Corresponds to Lean: `| mem_free`
    MemFree,

    /// Capability revocation
    ///
    /// Corresponds to Lean: `| cap_revoke`
    CapRevoke,

    /// Controlled information declassification (IFC operation)
    ///
    /// Corresponds to Lean: `| declassify`
    Declassify,

    /// Create new thread (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: `| thread_create`
    ThreadCreate,

    /// Configure thread bindings (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: `| thread_configure`
    ThreadConfigure,

    /// Resume suspended thread (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: `| thread_resume`
    ThreadResume,

    /// Suspend running thread (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: `| thread_suspend`
    ThreadSuspend,

    /// Terminate thread (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: `| thread_destroy`
    ThreadDestroy,
}

/// Resource type for KernelAlloc operation
///
/// Corresponds to Lean: `inductive ResourceType`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResourceType {
    /// Allocate memory region
    Memory {
        /// Size of the memory region to allocate
        size: u64,
    },
    /// Create capability (simplified payload)
    Capability {
        /// Capability payload identifier
        payload: u64,
    },
    /// Create actor (simplified config)
    Actor {
        /// Actor configuration identifier
        config: u64,
    },
}

// ============== LEGACY COMPATIBILITY ALIASES ==============

impl HostFunction {
    /// Legacy alias: CapInvoke -> CapCall
    #[deprecated(note = "Use HostFunction::CapCall instead (EROS consolidation)")]
    pub const CAP_INVOKE: HostFunction = HostFunction::CapCall;

    /// Legacy alias: IpcSend -> CapSend
    #[deprecated(note = "Use HostFunction::CapSend instead (EROS consolidation)")]
    pub const IPC_SEND: HostFunction = HostFunction::CapSend;

    /// Legacy alias: MemAlloc -> KernelAlloc
    #[deprecated(note = "Use HostFunction::KernelAlloc instead (EROS consolidation)")]
    pub const MEM_ALLOC: HostFunction = HostFunction::KernelAlloc;
}

impl HostFunction {
    /// Check if this host function is effectful
    ///
    /// All host functions cross the trust boundary, so all are effectful.
    ///
    /// Corresponds to Lean: `def is_effectful_host_call`
    #[inline]
    pub fn is_effectful(&self) -> bool {
        true
    }

    /// Check if this is a declassify operation
    ///
    /// Corresponds to Lean: `def is_declassify`
    #[inline]
    pub fn is_declassify(&self) -> bool {
        matches!(self, HostFunction::Declassify)
    }

    /// Check if this is a capability operation (may modify kernel.revocation)
    ///
    /// Corresponds to Lean: `def is_cap_operation`
    #[inline]
    pub fn is_cap_operation(&self) -> bool {
        matches!(self, HostFunction::CapCall | HostFunction::CapRevoke)
    }

    /// Check if this is a memory operation (modifies ghost state)
    ///
    /// Corresponds to Lean: `def is_mem_operation`
    #[inline]
    pub fn is_mem_operation(&self) -> bool {
        matches!(self, HostFunction::KernelAlloc | HostFunction::MemFree)
    }

    /// Execute the host function
    ///
    /// Corresponds to Lean: `def KernelExecHost`
    ///
    /// # Errors
    ///
    /// Returns `StepError::PluginNotFound` if the caller plugin does not exist.
    /// Returns `StepError::HostCallPreconditionFailed` if function-specific preconditions are not met.
    /// Returns `StepError::ActorNotFound` if a destination actor does not exist (CapSend).
    /// Returns `StepError::QuotaExceeded` if the allocation would exceed the plugin's memory quota (KernelAlloc).
    /// Returns `StepError::AddressAlreadyFreed` if the address is already freed (MemFree).
    /// Returns `StepError::CapabilityTargetsAddress` if a valid capability targets the address (MemFree).
    /// Returns `StepError::CapabilityNotFound` if the capability does not exist (CapRevoke).
    pub fn execute(
        &self,
        state: &State,
        hc: &HostCall,
        result: &HostResult,
    ) -> Result<State, StepError> {
        match self {
            HostFunction::CapCall => execute_cap_call(state, hc, result),
            HostFunction::CapSend => execute_cap_send(state, hc, result),
            HostFunction::KernelAlloc => execute_kernel_alloc(state, hc),
            HostFunction::MemFree => execute_mem_free(state, hc),
            HostFunction::CapRevoke => execute_cap_revoke(state, hc),
            HostFunction::Declassify => execute_declassify(state, hc),
            // Thread operations — Phase 3 stubs (require State extension)
            HostFunction::ThreadCreate
            | HostFunction::ThreadConfigure
            | HostFunction::ThreadResume
            | HostFunction::ThreadSuspend
            | HostFunction::ThreadDestroy => Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::NotImplemented {
                    operation: "thread operations",
                },
            )),
        }
    }

    /// Execute the host function (mutating version).
    ///
    /// Same validation as `execute` but modifies `&mut State` in place.
    pub fn execute_mut(
        &self,
        state: &mut State,
        hc: &HostCall,
        result: &HostResult,
    ) -> Result<(), StepError> {
        match self {
            HostFunction::CapCall => execute_cap_call_mut(state, hc, result),
            HostFunction::CapSend => execute_cap_send_mut(state, hc, result),
            HostFunction::KernelAlloc => execute_kernel_alloc_mut(state, hc),
            HostFunction::MemFree => execute_mem_free_mut(state, hc),
            HostFunction::CapRevoke => execute_cap_revoke_mut(state, hc),
            HostFunction::Declassify => execute_declassify_mut(state, hc),
            HostFunction::ThreadCreate
            | HostFunction::ThreadConfigure
            | HostFunction::ThreadResume
            | HostFunction::ThreadSuspend
            | HostFunction::ThreadDestroy => Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::NotImplemented {
                    operation: "thread operations",
                },
            )),
        }
    }

    /// Check if this is a thread operation (requires ThreadControl capability)
    ///
    /// Corresponds to Lean: thread_create/configure/resume/suspend/destroy
    #[inline]
    pub fn is_thread_operation(&self) -> bool {
        matches!(
            self,
            HostFunction::ThreadCreate
                | HostFunction::ThreadConfigure
                | HostFunction::ThreadResume
                | HostFunction::ThreadSuspend
                | HostFunction::ThreadDestroy
        )
    }
}

/// Host call request from plugin
///
/// Corresponds to Lean: `structure HostCall`
#[derive(Debug, Clone)]
pub struct HostCall {
    /// The plugin making the call
    ///
    /// Corresponds to Lean: `caller : PluginId`
    pub(crate) caller: PluginId,

    /// The function to invoke
    ///
    /// Corresponds to Lean: `function : HostFunctionId`
    pub(crate) function: HostFunction,

    /// Arguments to the function (simplified from WasmValue)
    ///
    /// Corresponds to Lean: `args : List Nat`
    pub(crate) args: Vec<u64>,

    /// Memory regions to read
    ///
    /// Corresponds to Lean: `reads : List (MemAddr x Size)`
    pub(crate) reads: Vec<(MemAddr, Size)>,

    /// Memory regions to write
    ///
    /// Corresponds to Lean: `writes : List (MemAddr x Size)`
    pub(crate) writes: Vec<(MemAddr, Size)>,
}

impl HostCall {
    /// Create a new host call
    pub fn new(
        caller: PluginId,
        function: HostFunction,
        args: Vec<u64>,
        reads: Vec<(MemAddr, Size)>,
        writes: Vec<(MemAddr, Size)>,
    ) -> Self {
        HostCall {
            caller,
            function,
            args,
            reads,
            writes,
        }
    }

    /// Get the caller plugin ID
    #[inline]
    pub fn caller(&self) -> PluginId {
        self.caller
    }

    /// Get the host function to invoke
    #[inline]
    pub fn function(&self) -> HostFunction {
        self.function
    }

    /// Get the arguments
    #[inline]
    pub fn args(&self) -> &[u64] {
        &self.args
    }

    /// Get the read regions
    #[inline]
    pub fn reads(&self) -> &[(MemAddr, Size)] {
        &self.reads
    }

    /// Get the write regions
    #[inline]
    pub fn writes(&self) -> &[(MemAddr, Size)] {
        &self.writes
    }

    /// Derive the required authorization action from the host call semantics.
    ///
    /// This binds authorization to the exact operation being performed,
    /// preventing the vulnerability where a valid `Authorized` token for one
    /// action could be reused to execute a different `HostCall`.
    ///
    /// # Errors
    ///
    /// Returns `StepError::HostCallPreconditionFailed` if required arguments are missing
    /// or the operation is not yet implemented (thread operations).
    pub fn required_action(&self) -> Result<Action, StepError> {
        // Helper to convert PolicyError to StepError
        let map_policy_err =
            |_| StepError::HostCallPreconditionFailed(HostCallPrecondition::NoCapabilityId);

        match self.function {
            HostFunction::CapCall => {
                let target: u64 =
                    self.args
                        .first()
                        .copied()
                        .ok_or(StepError::HostCallPreconditionFailed(
                            HostCallPrecondition::NoCapabilityId,
                        ))?;

                Action::new(
                    self.caller,
                    target.into(),
                    Rights::singleton(Right::Read),
                    "cap_call".to_string(),
                )
                .map_err(map_policy_err)
            }
            HostFunction::KernelAlloc => Action::new(
                self.caller,
                0,
                Rights::singleton(Right::Create),
                "kernel_alloc".to_string(),
            )
            .map_err(map_policy_err),
            HostFunction::MemFree => {
                let addr: u64 =
                    self.args
                        .first()
                        .copied()
                        .ok_or(StepError::HostCallPreconditionFailed(
                            HostCallPrecondition::NoAddress,
                        ))?;

                Action::new(
                    self.caller,
                    addr.into(),
                    Rights::singleton(Right::Delete),
                    "mem_free".to_string(),
                )
                .map_err(map_policy_err)
            }
            HostFunction::CapRevoke => {
                let cap_id: u64 =
                    self.args
                        .first()
                        .copied()
                        .ok_or(StepError::HostCallPreconditionFailed(
                            HostCallPrecondition::NoCapabilityId,
                        ))?;

                Action::new(
                    self.caller,
                    cap_id.into(),
                    Rights::singleton(Right::Revoke),
                    "cap_revoke".to_string(),
                )
                .map_err(map_policy_err)
            }
            HostFunction::Declassify => Action::new(
                self.caller,
                0,
                Rights::singleton(Right::Declassify),
                "declassify".to_string(),
            )
            .map_err(map_policy_err),
            HostFunction::CapSend => Action::new(
                self.caller,
                0,
                Rights::singleton(Right::Send),
                "cap_send".to_string(),
            )
            .map_err(map_policy_err),
            _ => Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::NotImplemented {
                    operation: "thread operations",
                },
            )),
        }
    }

    /// Check preconditions for the host call
    ///
    /// Corresponds to Lean: `def host_call_pre`
    ///
    /// # Errors
    ///
    /// Returns `StepError::PluginNotFound` if the caller plugin does not exist.
    /// Returns `StepError::HostCallPreconditionFailed` if a read or write region is out of bounds.
    pub fn check_preconditions(&self, state: &State) -> Result<(), StepError> {
        // Get the caller plugin
        let plugin = state
            .get_plugin(self.caller)
            .ok_or(StepError::PluginNotFound(self.caller))?;

        // Check read regions are in bounds
        if let Some(err) = self.find_oob_read(plugin) {
            return Err(err);
        }

        // Check write regions are in bounds
        if let Some(err) = self.find_oob_write(plugin) {
            return Err(err);
        }

        Ok(())
    }

    /// Helper: find first out-of-bounds read region
    fn find_oob_read(&self, plugin: &crate::state::PluginState) -> Option<StepError> {
        self.reads.iter().find_map(|&(addr, size)| {
            if plugin.memory().addr_in_bounds(addr, size) {
                None
            } else {
                Some(StepError::HostCallPreconditionFailed(
                    HostCallPrecondition::ReadOutOfBounds { addr, size },
                ))
            }
        })
    }

    /// Helper: find first out-of-bounds write region
    fn find_oob_write(&self, plugin: &crate::state::PluginState) -> Option<StepError> {
        self.writes.iter().find_map(|&(addr, size)| {
            if plugin.memory().addr_in_bounds(addr, size) {
                None
            } else {
                Some(StepError::HostCallPreconditionFailed(
                    HostCallPrecondition::WriteOutOfBounds { addr, size },
                ))
            }
        })
    }
}

/// Result of host call execution
///
/// Corresponds to Lean: `structure HostResult`
#[derive(Debug, Clone, Default)]
#[must_use = "host call results must be consumed"]
pub struct HostResult {
    /// Capabilities created/delegated
    ///
    /// Corresponds to Lean: `newCaps : List Capability`
    pub(crate) new_caps: Vec<Capability>,

    /// Messages queued
    ///
    /// Corresponds to Lean: `newMsgs : List Message`
    pub(crate) new_msgs: Vec<Message>,
}

impl HostResult {
    /// Create an empty result
    pub fn empty() -> Self {
        HostResult {
            new_caps: Vec::new(),
            new_msgs: Vec::new(),
        }
    }

    /// Create a result with new capabilities
    pub fn with_caps(caps: Vec<Capability>) -> Self {
        HostResult {
            new_caps: caps,
            new_msgs: Vec::new(),
        }
    }

    /// Create a result with new messages
    pub fn with_msgs(msgs: Vec<Message>) -> Self {
        HostResult {
            new_caps: Vec::new(),
            new_msgs: msgs,
        }
    }

    /// Get new capabilities
    #[inline]
    pub fn new_caps(&self) -> &[Capability] {
        &self.new_caps
    }

    /// Get new messages
    #[inline]
    pub fn new_msgs(&self) -> &[Message] {
        &self.new_msgs
    }
}

// ============== HOST FUNCTION EXECUTION ==============

/// Execute cap_call (was cap_invoke)
///
/// Synchronous capability invocation: validate and use capability.
/// State unchanged (call just validates and uses the cap).
///
/// PERF NOTE: Returns `state.clone()` because the API takes `&State` (callers
/// need to retain the original for error paths and multi-step sequences).
/// The clone is architecturally necessary for the immutable-state-transition
/// model that mirrors the Lean spec (`Step s s'`). A by-value `State` API
/// would eliminate this clone but would require the caller to re-create state
/// on error paths, trading clone cost for ergonomic complexity.
fn execute_cap_call(
    state: &State,
    hc: &HostCall,
    _result: &HostResult,
) -> Result<State, StepError> {
    // Check caller holds a valid capability
    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    // At least one held cap must be valid
    let has_valid_cap = plugin
        .held_caps_ref()
        .iter()
        .any(|&id| state.kernel().revocation().is_valid(id));

    if !has_valid_cap {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoValidCapability,
        ));
    }

    // State unchanged for call — clone required by &State API contract
    Ok(state.clone())
}

/// Execute cap_send (was ipc_send)
///
/// Async message send. Capability delegation is now user-space protocol:
/// 1. Holder creates delegation message with cap payload
/// 2. Sends via cap_send to delegatee
/// 3. Delegatee receives and calls kernel_alloc to register
///
/// PERF NOTE: See `execute_cap_call` for why `state.clone()` is required here.
/// State is unchanged — the kernel delivers messages asynchronously.
fn execute_cap_send(state: &State, hc: &HostCall, result: &HostResult) -> Result<State, StepError> {
    // Check message in result
    if result.new_msgs.is_empty() {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoMessage,
        ));
    }

    // Validate message source matches caller
    for msg in &result.new_msgs {
        if msg.src() != hc.caller {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::SourceMismatch,
            ));
        }

        // Check destination actor exists and has capacity
        let dst_actor = state
            .get_actor(msg.dst())
            .ok_or(StepError::ActorNotFound(msg.dst()))?;

        if dst_actor.mailbox_len() >= dst_actor.capacity() {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::MailboxFull,
            ));
        }
    }

    // State unchanged — clone required by &State API contract
    Ok(state.clone())
}

/// Execute kernel_alloc (was mem_alloc, resource_create)
///
/// Unified kernel resource allocation.
/// Enforces memory quota to prevent DoS attacks.
fn execute_kernel_alloc(state: &State, hc: &HostCall) -> Result<State, StepError> {
    // Get size from args (first arg is size for memory allocation)
    let size = hc.args.first().copied().unwrap_or(0);

    // QUOTA CHECK: Verify memory quota not exceeded
    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    if !plugin.can_alloc_memory(size) {
        return Err(StepError::QuotaExceeded(
            0, // memory
            size,
            plugin.memory_used(),
            plugin.memory_quota(),
        ));
    }

    // Apply allocation and update quota tracking
    let mut new_state = state.apply_alloc(hc.caller, size);

    // Update memory used counter in plugin
    if let Some(p) = new_state.get_plugin_mut(hc.caller) {
        p.memory_used = p.memory_used.saturating_add(size);
    }

    Ok(new_state)
}

/// Execute mem_free
///
/// Free kernel-managed memory.
fn execute_mem_free(state: &State, hc: &HostCall) -> Result<State, StepError> {
    // Get addr from args
    let addr: MemAddr = hc
        .args
        .first()
        .copied()
        .ok_or(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoAddress,
        ))?;

    // Check not already freed
    if state.ghost().is_freed(addr) {
        return Err(StepError::AddressAlreadyFreed(addr));
    }

    // Check no valid capability targets addr (ResourceId = u128, addr = u64)
    if let Some(&(cap_id, _)) = state
        .kernel()
        .revocation()
        .iter()
        .find(|(_, cap)| cap.is_valid() && cap.target() == u128::from(addr))
    {
        return Err(StepError::CapabilityTargetsAddress(cap_id, addr));
    }

    state.apply_free(addr).map_err(|e| {
        StepError::HostCallPreconditionFailed(HostCallPrecondition::FreeFailed { source: e })
    })
}

/// Execute cap_revoke
///
/// Revoke capability transitively.
fn execute_cap_revoke(state: &State, hc: &HostCall) -> Result<State, StepError> {
    // Get the cap_id from args (stored as u64, widened to CapId = u128)
    let cap_id: crate::types::CapId =
        hc.args
            .first()
            .copied()
            .map(u128::from)
            .ok_or(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::NoCapabilityId,
            ))?;

    // Check cap exists
    if state.kernel().revocation().get(cap_id).is_none() {
        return Err(StepError::CapabilityNotFound(cap_id));
    }

    // Apply transitive revocation
    Ok(state.apply_cap_revoke(cap_id))
}

/// Execute declassify
///
/// Controlled information declassification.
fn execute_declassify(state: &State, hc: &HostCall) -> Result<State, StepError> {
    // Get new level from args (encoded as u64)
    let new_level_val = hc.args.first().copied().unwrap_or(0);
    let new_level = match new_level_val {
        0 => SecurityLevel::Public,
        1 => SecurityLevel::Internal,
        2 => SecurityLevel::Confidential,
        3 => SecurityLevel::Secret,
        other => {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::InvalidSecurityLevel { value: other },
            ));
        }
    };

    // Check new level <= current level (can only declassify down)
    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    if new_level > plugin.level() {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::CannotClassifyUp,
        ));
    }

    // Update plugin level
    let mut new_state = state.clone();
    if let Some(plugin) = new_state.get_plugin_mut(hc.caller) {
        plugin.level = new_level;
    }

    Ok(new_state)
}

// ============== MUTATING HOST FUNCTION VARIANTS ==============

/// Execute cap_call (mutating) -- state-unchanged operation
fn execute_cap_call_mut(
    state: &mut State,
    hc: &HostCall,
    _result: &HostResult,
) -> Result<(), StepError> {
    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    let has_valid_cap = plugin
        .held_caps_ref()
        .iter()
        .any(|&id| state.kernel().revocation().is_valid(id));

    if !has_valid_cap {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoValidCapability,
        ));
    }

    // State unchanged for call
    Ok(())
}

/// Execute cap_send (mutating) -- state-unchanged operation
fn execute_cap_send_mut(
    state: &mut State,
    hc: &HostCall,
    result: &HostResult,
) -> Result<(), StepError> {
    if result.new_msgs.is_empty() {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoMessage,
        ));
    }

    for msg in &result.new_msgs {
        if msg.src() != hc.caller {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::SourceMismatch,
            ));
        }

        let dst_actor = state
            .get_actor(msg.dst())
            .ok_or(StepError::ActorNotFound(msg.dst()))?;

        if dst_actor.mailbox_len() >= dst_actor.capacity() {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::MailboxFull,
            ));
        }
    }

    // State unchanged
    Ok(())
}

/// Execute kernel_alloc (mutating)
fn execute_kernel_alloc_mut(state: &mut State, hc: &HostCall) -> Result<(), StepError> {
    let size = hc.args.first().copied().unwrap_or(0);

    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    if !plugin.can_alloc_memory(size) {
        return Err(StepError::QuotaExceeded(
            0, // memory
            size,
            plugin.memory_used(),
            plugin.memory_quota(),
        ));
    }

    // Apply allocation in place
    let _addr = state.apply_alloc_mut(hc.caller, size);

    // Update memory used counter in plugin
    if let Some(p) = state.get_plugin_mut(hc.caller) {
        p.memory_used = p.memory_used.saturating_add(size);
    }

    Ok(())
}

/// Execute mem_free (mutating)
fn execute_mem_free_mut(state: &mut State, hc: &HostCall) -> Result<(), StepError> {
    let addr: MemAddr = hc
        .args
        .first()
        .copied()
        .ok_or(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::NoAddress,
        ))?;

    if state.ghost().is_freed(addr) {
        return Err(StepError::AddressAlreadyFreed(addr));
    }

    // ResourceId = u128, addr = u64
    if let Some(&(cap_id, _)) = state
        .kernel()
        .revocation()
        .iter()
        .find(|(_, cap)| cap.is_valid() && cap.target() == u128::from(addr))
    {
        return Err(StepError::CapabilityTargetsAddress(cap_id, addr));
    }

    state.apply_free_mut(addr).map_err(|e| {
        StepError::HostCallPreconditionFailed(HostCallPrecondition::FreeFailed { source: e })
    })
}

/// Execute cap_revoke (mutating)
fn execute_cap_revoke_mut(state: &mut State, hc: &HostCall) -> Result<(), StepError> {
    // Stored as u64, widened to CapId = u128
    let cap_id: crate::types::CapId =
        hc.args
            .first()
            .copied()
            .map(u128::from)
            .ok_or(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::NoCapabilityId,
            ))?;

    if state.kernel().revocation().get(cap_id).is_none() {
        return Err(StepError::CapabilityNotFound(cap_id));
    }

    state
        .apply_cap_revoke_mut(cap_id)
        .map_err(|_| StepError::CapabilityNotFound(cap_id))?;

    Ok(())
}

/// Execute declassify (mutating)
fn execute_declassify_mut(state: &mut State, hc: &HostCall) -> Result<(), StepError> {
    let new_level_val = hc.args.first().copied().unwrap_or(0);
    let new_level = match new_level_val {
        0 => SecurityLevel::Public,
        1 => SecurityLevel::Internal,
        2 => SecurityLevel::Confidential,
        3 => SecurityLevel::Secret,
        other => {
            return Err(StepError::HostCallPreconditionFailed(
                HostCallPrecondition::InvalidSecurityLevel { value: other },
            ));
        }
    };

    let plugin = state
        .get_plugin(hc.caller)
        .ok_or(StepError::PluginNotFound(hc.caller))?;

    if new_level > plugin.level() {
        return Err(StepError::HostCallPreconditionFailed(
            HostCallPrecondition::CannotClassifyUp,
        ));
    }

    if let Some(plugin) = state.get_plugin_mut(hc.caller) {
        plugin.level = new_level;
    }

    Ok(())
}

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

    #[test]
    fn test_host_function_is_effectful() {
        // All host functions are effectful
        assert!(HostFunction::CapCall.is_effectful());
        assert!(HostFunction::KernelAlloc.is_effectful());
        assert!(HostFunction::Declassify.is_effectful());
    }

    #[test]
    fn test_host_function_is_declassify() {
        assert!(!HostFunction::CapCall.is_declassify());
        assert!(!HostFunction::KernelAlloc.is_declassify());
        assert!(HostFunction::Declassify.is_declassify());
    }

    #[test]
    fn test_host_function_is_cap_operation() {
        assert!(HostFunction::CapCall.is_cap_operation());
        assert!(HostFunction::CapRevoke.is_cap_operation());
        assert!(!HostFunction::CapSend.is_cap_operation());
        assert!(!HostFunction::KernelAlloc.is_cap_operation());
    }

    #[test]
    fn test_host_function_is_mem_operation() {
        assert!(HostFunction::KernelAlloc.is_mem_operation());
        assert!(HostFunction::MemFree.is_mem_operation());
        assert!(!HostFunction::CapCall.is_mem_operation());
    }

    #[test]
    fn test_host_call_check_preconditions_no_plugin() {
        let state = State::empty();
        let hc = HostCall::new(1, HostFunction::CapCall, vec![], vec![], vec![]);

        let result = hc.check_preconditions(&state);
        assert!(matches!(result, Err(StepError::PluginNotFound(1))));
    }

    #[test]
    fn test_host_call_check_preconditions_out_of_bounds() {
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Public, 100));

        // Read region beyond bounds
        let hc = HostCall::new(1, HostFunction::CapCall, vec![], vec![(200, 50)], vec![]);

        let result = hc.check_preconditions(&state);
        assert!(matches!(
            result,
            Err(StepError::HostCallPreconditionFailed(_))
        ));
    }

    #[test]
    fn test_execute_kernel_alloc() {
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Public, 1024));

        let hc = HostCall::new(1, HostFunction::KernelAlloc, vec![256], vec![], vec![]);
        let result = HostResult::empty();

        let new_state = HostFunction::KernelAlloc
            .execute(&state, &hc, &result)
            .expect("kernel_alloc should succeed");

        // Ghost state should show allocation
        assert!(new_state.ghost().resource_count() > state.ghost().resource_count());
    }

    #[test]
    fn test_execute_declassify() {
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Secret, 0));

        // Declassify to Public (level 0)
        let hc = HostCall::new(1, HostFunction::Declassify, vec![0], vec![], vec![]);
        let result = HostResult::empty();

        let new_state = HostFunction::Declassify
            .execute(&state, &hc, &result)
            .expect("declassify should succeed");

        assert_eq!(new_state.plugin_level(1), Some(SecurityLevel::Public));
    }

    #[test]
    fn test_execute_declassify_up_fails() {
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Public, 0));

        // Try to classify up to Secret (level 3) - should fail
        let hc = HostCall::new(1, HostFunction::Declassify, vec![3], vec![], vec![]);
        let result = HostResult::empty();

        let err = HostFunction::Declassify
            .execute(&state, &hc, &result)
            .expect_err("classify up should fail");

        assert!(matches!(err, StepError::HostCallPreconditionFailed(_)));
    }

    #[test]
    fn test_execute_declassify_invalid_level_rejected() {
        // SEC-002: values >= 4 must be rejected, not silently mapped to Secret
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Secret, 0));

        for invalid_level in [4, 5, 100, u64::MAX] {
            let hc = HostCall::new(
                1,
                HostFunction::Declassify,
                vec![invalid_level],
                vec![],
                vec![],
            );
            let result = HostResult::empty();

            let err = HostFunction::Declassify
                .execute(&state, &hc, &result)
                .expect_err(&format!("level {invalid_level} should be rejected"));

            assert!(
                matches!(
                    err,
                    StepError::HostCallPreconditionFailed(
                        HostCallPrecondition::InvalidSecurityLevel { .. }
                    )
                ),
                "level {invalid_level}: expected InvalidSecurityLevel error, got {err:?}"
            );
        }
    }

    #[test]
    fn test_execute_declassify_all_valid_levels() {
        // All 4 valid levels (0-3) should work for a Secret plugin declassifying down
        let mut state = State::empty();
        let _ = state.insert_plugin(1, PluginState::empty(SecurityLevel::Secret, 0));

        let expected = [
            (0, SecurityLevel::Public),
            (1, SecurityLevel::Internal),
            (2, SecurityLevel::Confidential),
            (3, SecurityLevel::Secret),
        ];

        for (level_val, expected_level) in expected {
            let hc = HostCall::new(1, HostFunction::Declassify, vec![level_val], vec![], vec![]);
            let result = HostResult::empty();

            let new_state = HostFunction::Declassify
                .execute(&state, &hc, &result)
                .unwrap_or_else(|e| panic!("level {level_val} should succeed: {e:?}"));

            assert_eq!(new_state.plugin_level(1), Some(expected_level));
        }
    }
}