tk-axcbpf 0.1.0

Verified classic-BPF mechanism for no_std operating systems
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
use alloc::vec::Vec;
use core::{fmt, ops::Range};

use crate::{Instruction, opcode};

/// Maximum number of instructions in one verified classic-BPF program.
pub const MAX_INSTRUCTIONS: usize = 4096;

/// Number of 32-bit scratch words exposed by classic BPF.
pub const SCRATCH_WORDS: usize = 16;

/// Width requested from a classic-BPF input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LoadWidth {
    /// One byte.
    Byte,
    /// Two bytes.
    Half,
    /// Four bytes.
    Word,
}

impl LoadWidth {
    const fn bytes(self) -> usize {
        match self {
            Self::Byte => 1,
            Self::Half => 2,
            Self::Word => 4,
        }
    }
}

/// Read-only data source evaluated by a classic-BPF program.
///
/// The input owns load endianness and any domain-specific offset validation.
/// Returning `None` terminates evaluation with the classic-BPF failure value
/// zero. The interpreter never exposes A, X, scratch memory, or mutable state
/// to the input implementation.
pub trait Input {
    /// Returns the length value observed by `LD_LEN` and `LDX_LEN`.
    fn len(&self) -> u32;

    /// Returns whether the input length is zero.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Loads one value at an already resolved absolute offset.
    fn load(&self, offset: u32, width: LoadWidth) -> Option<u32>;
}

/// Slice adapters and native images share a u32 input-length ABI. A larger
/// backing slice must not expose bytes beyond its advertised saturated length.
pub(crate) fn input_range(length: usize, offset: u32, width: LoadWidth) -> Option<Range<usize>> {
    let end = offset.checked_add(width.bytes() as u32)?;
    if end > u32::try_from(length).unwrap_or(u32::MAX) {
        return None;
    }
    Some(usize::try_from(offset).ok()?..usize::try_from(end).ok()?)
}

impl Input for [u8] {
    fn len(&self) -> u32 {
        u32::try_from(<[u8]>::len(self)).unwrap_or(u32::MAX)
    }

    fn load(&self, offset: u32, width: LoadWidth) -> Option<u32> {
        let bytes = self.get(input_range(<[u8]>::len(self), offset, width)?)?;
        match width {
            LoadWidth::Byte => Some(u32::from(bytes[0])),
            LoadWidth::Half => Some(u32::from(u16::from_be_bytes([bytes[0], bytes[1]]))),
            LoadWidth::Word => Some(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
        }
    }
}

/// Failure while validating and owning one classic-BPF program.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum VerifyError {
    /// A program must contain at least one instruction.
    Empty,
    /// The program exceeds [`MAX_INSTRUCTIONS`].
    TooLong {
        /// Supplied instruction count.
        length: usize,
    },
    /// Storage needed for validation or the owned program could not be reserved.
    NoMemory,
    /// The opcode is not part of the supported ordinary classic-BPF set.
    UnsupportedOpcode {
        /// Instruction index.
        pc: usize,
        /// Rejected raw opcode.
        code: u16,
    },
    /// An immediate divisor or modulus was zero.
    ImmediateDivisionByZero {
        /// Instruction index.
        pc: usize,
    },
    /// An immediate shift count was outside `0..32`.
    ImmediateShiftOutOfRange {
        /// Instruction index.
        pc: usize,
        /// Rejected shift count.
        shift: u32,
    },
    /// A scratch-memory index was outside the sixteen-word store.
    ScratchOutOfRange {
        /// Instruction index.
        pc: usize,
        /// Rejected scratch index.
        index: u32,
    },
    /// A reachable path loads a scratch word not initialized on every path.
    ScratchUninitialized {
        /// Instruction index.
        pc: usize,
        /// Uninitialized scratch index.
        index: u32,
    },
    /// A jump target was outside the program.
    JumpOutOfRange {
        /// Instruction index.
        pc: usize,
    },
    /// The final instruction was not a return.
    MissingFinalReturn,
}

impl fmt::Display for VerifyError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => formatter.write_str("classic-BPF program is empty"),
            Self::TooLong { length } => write!(
                formatter,
                "classic-BPF program has {length} instructions, limit is {MAX_INSTRUCTIONS}"
            ),
            Self::NoMemory => formatter.write_str("classic-BPF allocation failed"),
            Self::UnsupportedOpcode { pc, code } => {
                write!(
                    formatter,
                    "unsupported classic-BPF opcode {code:#x} at {pc}"
                )
            }
            Self::ImmediateDivisionByZero { pc } => {
                write!(formatter, "zero classic-BPF immediate divisor at {pc}")
            }
            Self::ImmediateShiftOutOfRange { pc, shift } => write!(
                formatter,
                "classic-BPF immediate shift {shift} is out of range at {pc}"
            ),
            Self::ScratchOutOfRange { pc, index } => write!(
                formatter,
                "classic-BPF scratch index {index} is out of range at {pc}"
            ),
            Self::ScratchUninitialized { pc, index } => write!(
                formatter,
                "classic-BPF scratch index {index} is not initialized on every path at {pc}"
            ),
            Self::JumpOutOfRange { pc } => {
                write!(formatter, "classic-BPF jump leaves the program at {pc}")
            }
            Self::MissingFinalReturn => {
                formatter.write_str("classic-BPF program does not end in a return")
            }
        }
    }
}

/// Immutable verified classic-BPF program.
///
/// Construction performs all structural checks and fallibly copies the raw
/// instructions. Evaluation allocates nothing, initializes A and X to zero,
/// and executes at most `len()` instructions because every accepted jump is
/// forward-only.
#[derive(Debug)]
pub struct Program {
    instructions: Vec<Instruction>,
}

impl Program {
    /// Verifies and fallibly takes an immutable copy of raw instructions.
    pub fn verify(instructions: &[Instruction]) -> Result<Self, VerifyError> {
        verify_program(instructions)?;

        let mut owned = Vec::new();
        owned
            .try_reserve_exact(instructions.len())
            .map_err(|_| VerifyError::NoMemory)?;
        owned.extend_from_slice(instructions);
        Ok(Self {
            instructions: owned,
        })
    }

    /// Verifies and takes ownership of an existing instruction vector.
    ///
    /// This path performs no second instruction-buffer allocation or copy. It
    /// is intended for adapters that already copied an untrusted wire program
    /// into a fallibly allocated kernel vector. Validation still uses bounded,
    /// fallible temporary storage for all-path scratch analysis.
    pub fn try_from_vec(instructions: Vec<Instruction>) -> Result<Self, VerifyError> {
        verify_program(&instructions)?;
        Ok(Self { instructions })
    }

    /// Returns the verified raw instructions.
    pub fn instructions(&self) -> &[Instruction] {
        &self.instructions
    }

    /// Returns the instruction count and maximum evaluation step count.
    pub fn len(&self) -> usize {
        self.instructions.len()
    }

    /// Returns whether the program contains no instructions.
    ///
    /// Verified programs are never empty, so this always returns `false`.
    pub fn is_empty(&self) -> bool {
        self.instructions.is_empty()
    }

    /// Translates this verified program into an immutable x86_64 code image.
    ///
    /// Translation only produces bytes and metadata. It does not make pages
    /// executable, patch relocations, or invoke an external function. A
    /// publisher is responsible for resolving the returned relocations under
    /// its own W^X and lifetime policy.
    pub fn translate(&self) -> Result<crate::CodeImage, crate::TranslationError> {
        self.translate_with_profile(crate::InputProfile::BigEndianBytes)
    }

    /// Translates using an explicit input ABI profile.
    pub fn translate_with_profile(
        &self,
        profile: crate::InputProfile,
    ) -> Result<crate::CodeImage, crate::TranslationError> {
        crate::translate::translate_program(self.instructions(), profile)
    }

    /// Evaluates the program without allocation.
    ///
    /// A failed input load, an indirect-offset overflow, or a run-time X
    /// divisor of zero terminates evaluation with zero, matching classic-BPF
    /// failure behavior. A and X start at zero for every call.
    pub fn evaluate<I: Input + ?Sized>(&self, input: &I) -> u32 {
        self.evaluate_counted(input).0
    }

    fn evaluate_counted<I: Input + ?Sized>(&self, input: &I) -> (u32, usize) {
        let mut accumulator = 0_u32;
        let mut index = 0_u32;
        let mut scratch = [0_u32; SCRATCH_WORDS];
        let mut pc = 0_usize;
        let mut steps = 0_usize;

        while steps < self.instructions.len() {
            let instruction = self.instructions[pc];
            steps += 1;

            match instruction.code {
                opcode::LD_IMM => accumulator = instruction.k,
                opcode::LD_W_ABS => {
                    let Some(value) = input.load(instruction.k, LoadWidth::Word) else {
                        return (0, steps);
                    };
                    accumulator = value;
                }
                opcode::LD_H_ABS => {
                    let Some(value) = input.load(instruction.k, LoadWidth::Half) else {
                        return (0, steps);
                    };
                    accumulator = value;
                }
                opcode::LD_B_ABS => {
                    let Some(value) = input.load(instruction.k, LoadWidth::Byte) else {
                        return (0, steps);
                    };
                    accumulator = value;
                }
                opcode::LD_W_IND | opcode::LD_H_IND | opcode::LD_B_IND => {
                    let Some(offset) = index.checked_add(instruction.k) else {
                        return (0, steps);
                    };
                    let width = match instruction.code {
                        opcode::LD_W_IND => LoadWidth::Word,
                        opcode::LD_H_IND => LoadWidth::Half,
                        opcode::LD_B_IND => LoadWidth::Byte,
                        _ => unreachable!(),
                    };
                    let Some(value) = input.load(offset, width) else {
                        return (0, steps);
                    };
                    accumulator = value;
                }
                opcode::LD_MEM => accumulator = scratch[instruction.k as usize],
                opcode::LD_LEN => accumulator = input.len(),
                opcode::LDX_IMM => index = instruction.k,
                opcode::LDX_MEM => index = scratch[instruction.k as usize],
                opcode::LDX_LEN => index = input.len(),
                opcode::LDX_B_MSH => {
                    let Some(value) = input.load(instruction.k, LoadWidth::Byte) else {
                        return (0, steps);
                    };
                    index = (value & 0x0f) << 2;
                }
                opcode::ST => scratch[instruction.k as usize] = accumulator,
                opcode::STX => scratch[instruction.k as usize] = index,
                opcode::ALU_ADD_K => accumulator = accumulator.wrapping_add(instruction.k),
                opcode::ALU_ADD_X => accumulator = accumulator.wrapping_add(index),
                opcode::ALU_SUB_K => accumulator = accumulator.wrapping_sub(instruction.k),
                opcode::ALU_SUB_X => accumulator = accumulator.wrapping_sub(index),
                opcode::ALU_MUL_K => accumulator = accumulator.wrapping_mul(instruction.k),
                opcode::ALU_MUL_X => accumulator = accumulator.wrapping_mul(index),
                opcode::ALU_DIV_K => accumulator /= instruction.k,
                opcode::ALU_DIV_X => {
                    if index == 0 {
                        return (0, steps);
                    }
                    accumulator /= index;
                }
                opcode::ALU_OR_K => accumulator |= instruction.k,
                opcode::ALU_OR_X => accumulator |= index,
                opcode::ALU_AND_K => accumulator &= instruction.k,
                opcode::ALU_AND_X => accumulator &= index,
                opcode::ALU_LSH_K => accumulator = accumulator.wrapping_shl(instruction.k),
                opcode::ALU_LSH_X => accumulator = accumulator.wrapping_shl(index & 31),
                opcode::ALU_RSH_K => accumulator = accumulator.wrapping_shr(instruction.k),
                opcode::ALU_RSH_X => accumulator = accumulator.wrapping_shr(index & 31),
                opcode::ALU_NEG => accumulator = accumulator.wrapping_neg(),
                opcode::ALU_MOD_K => accumulator %= instruction.k,
                opcode::ALU_MOD_X => {
                    if index == 0 {
                        return (0, steps);
                    }
                    accumulator %= index;
                }
                opcode::ALU_XOR_K => accumulator ^= instruction.k,
                opcode::ALU_XOR_X => accumulator ^= index,
                opcode::JMP_JA => {
                    pc += 1 + instruction.k as usize;
                    continue;
                }
                opcode::JMP_JEQ_K
                | opcode::JMP_JEQ_X
                | opcode::JMP_JGT_K
                | opcode::JMP_JGT_X
                | opcode::JMP_JGE_K
                | opcode::JMP_JGE_X
                | opcode::JMP_JSET_K
                | opcode::JMP_JSET_X => {
                    let operand = if matches!(
                        instruction.code,
                        opcode::JMP_JEQ_X
                            | opcode::JMP_JGT_X
                            | opcode::JMP_JGE_X
                            | opcode::JMP_JSET_X
                    ) {
                        index
                    } else {
                        instruction.k
                    };
                    let condition = match instruction.code {
                        opcode::JMP_JEQ_K | opcode::JMP_JEQ_X => accumulator == operand,
                        opcode::JMP_JGT_K | opcode::JMP_JGT_X => accumulator > operand,
                        opcode::JMP_JGE_K | opcode::JMP_JGE_X => accumulator >= operand,
                        opcode::JMP_JSET_K | opcode::JMP_JSET_X => accumulator & operand != 0,
                        _ => unreachable!(),
                    };
                    pc += 1 + usize::from(if condition {
                        instruction.jt
                    } else {
                        instruction.jf
                    });
                    continue;
                }
                opcode::RET_K => return (instruction.k, steps),
                opcode::RET_A => return (accumulator, steps),
                opcode::MISC_TAX => index = accumulator,
                opcode::MISC_TXA => accumulator = index,
                _ => unreachable!("verified program contains an unsupported opcode"),
            }

            pc += 1;
        }

        unreachable!("verified program exhausted its step bound without returning")
    }
}

fn verify_program(instructions: &[Instruction]) -> Result<(), VerifyError> {
    verify_structure(instructions)?;
    verify_scratch_initialization(instructions)
}

fn verify_structure(instructions: &[Instruction]) -> Result<(), VerifyError> {
    if instructions.is_empty() {
        return Err(VerifyError::Empty);
    }
    if instructions.len() > MAX_INSTRUCTIONS {
        return Err(VerifyError::TooLong {
            length: instructions.len(),
        });
    }

    for (pc, instruction) in instructions.iter().copied().enumerate() {
        if !is_supported(instruction.code) {
            return Err(VerifyError::UnsupportedOpcode {
                pc,
                code: instruction.code,
            });
        }

        match instruction.code {
            opcode::ALU_DIV_K | opcode::ALU_MOD_K if instruction.k == 0 => {
                return Err(VerifyError::ImmediateDivisionByZero { pc });
            }
            opcode::ALU_LSH_K | opcode::ALU_RSH_K if instruction.k >= 32 => {
                return Err(VerifyError::ImmediateShiftOutOfRange {
                    pc,
                    shift: instruction.k,
                });
            }
            opcode::LD_MEM | opcode::LDX_MEM | opcode::ST | opcode::STX
                if instruction.k >= SCRATCH_WORDS as u32 =>
            {
                return Err(VerifyError::ScratchOutOfRange {
                    pc,
                    index: instruction.k,
                });
            }
            opcode::JMP_JA => {
                let Some(target) = pc
                    .checked_add(1)
                    .and_then(|next| next.checked_add(instruction.k as usize))
                else {
                    return Err(VerifyError::JumpOutOfRange { pc });
                };
                if target >= instructions.len() {
                    return Err(VerifyError::JumpOutOfRange { pc });
                }
            }
            opcode::JMP_JEQ_K
            | opcode::JMP_JEQ_X
            | opcode::JMP_JGT_K
            | opcode::JMP_JGT_X
            | opcode::JMP_JGE_K
            | opcode::JMP_JGE_X
            | opcode::JMP_JSET_K
            | opcode::JMP_JSET_X => {
                let true_target = pc + 1 + usize::from(instruction.jt);
                let false_target = pc + 1 + usize::from(instruction.jf);
                if true_target >= instructions.len() || false_target >= instructions.len() {
                    return Err(VerifyError::JumpOutOfRange { pc });
                }
            }
            _ => {}
        }
    }

    if !matches!(
        instructions.last().map(|instruction| instruction.code),
        Some(opcode::RET_K | opcode::RET_A)
    ) {
        return Err(VerifyError::MissingFinalReturn);
    }

    Ok(())
}

fn verify_scratch_initialization(instructions: &[Instruction]) -> Result<(), VerifyError> {
    let mut incoming = Vec::new();
    incoming
        .try_reserve_exact(instructions.len())
        .map_err(|_| VerifyError::NoMemory)?;
    incoming.resize(instructions.len(), None);
    incoming[0] = Some(0_u16);

    for (pc, instruction) in instructions.iter().copied().enumerate() {
        let Some(mut initialized) = incoming[pc] else {
            continue;
        };

        match instruction.code {
            opcode::LD_MEM | opcode::LDX_MEM => {
                let bit = 1_u16 << instruction.k;
                if initialized & bit == 0 {
                    return Err(VerifyError::ScratchUninitialized {
                        pc,
                        index: instruction.k,
                    });
                }
            }
            opcode::ST | opcode::STX => initialized |= 1_u16 << instruction.k,
            _ => {}
        }

        match instruction.code {
            opcode::RET_K | opcode::RET_A => {}
            opcode::JMP_JA => {
                let target = pc + 1 + instruction.k as usize;
                merge_initialized(&mut incoming[target], initialized);
            }
            opcode::JMP_JEQ_K
            | opcode::JMP_JEQ_X
            | opcode::JMP_JGT_K
            | opcode::JMP_JGT_X
            | opcode::JMP_JGE_K
            | opcode::JMP_JGE_X
            | opcode::JMP_JSET_K
            | opcode::JMP_JSET_X => {
                let true_target = pc + 1 + usize::from(instruction.jt);
                let false_target = pc + 1 + usize::from(instruction.jf);
                merge_initialized(&mut incoming[true_target], initialized);
                merge_initialized(&mut incoming[false_target], initialized);
            }
            _ => merge_initialized(&mut incoming[pc + 1], initialized),
        }
    }

    Ok(())
}

fn merge_initialized(slot: &mut Option<u16>, incoming: u16) {
    *slot = Some(match *slot {
        Some(existing) => existing & incoming,
        None => incoming,
    });
}

const fn is_supported(code: u16) -> bool {
    matches!(
        code,
        opcode::LD_IMM
            | opcode::LD_W_ABS
            | opcode::LD_H_ABS
            | opcode::LD_B_ABS
            | opcode::LD_W_IND
            | opcode::LD_H_IND
            | opcode::LD_B_IND
            | opcode::LD_MEM
            | opcode::LD_LEN
            | opcode::LDX_IMM
            | opcode::LDX_MEM
            | opcode::LDX_LEN
            | opcode::LDX_B_MSH
            | opcode::ST
            | opcode::STX
            | opcode::ALU_ADD_K
            | opcode::ALU_ADD_X
            | opcode::ALU_SUB_K
            | opcode::ALU_SUB_X
            | opcode::ALU_MUL_K
            | opcode::ALU_MUL_X
            | opcode::ALU_DIV_K
            | opcode::ALU_DIV_X
            | opcode::ALU_OR_K
            | opcode::ALU_OR_X
            | opcode::ALU_AND_K
            | opcode::ALU_AND_X
            | opcode::ALU_LSH_K
            | opcode::ALU_LSH_X
            | opcode::ALU_RSH_K
            | opcode::ALU_RSH_X
            | opcode::ALU_NEG
            | opcode::ALU_MOD_K
            | opcode::ALU_MOD_X
            | opcode::ALU_XOR_K
            | opcode::ALU_XOR_X
            | opcode::JMP_JA
            | opcode::JMP_JEQ_K
            | opcode::JMP_JEQ_X
            | opcode::JMP_JGT_K
            | opcode::JMP_JGT_X
            | opcode::JMP_JGE_K
            | opcode::JMP_JGE_X
            | opcode::JMP_JSET_K
            | opcode::JMP_JSET_X
            | opcode::RET_K
            | opcode::RET_A
            | opcode::MISC_TAX
            | opcode::MISC_TXA
    )
}

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

    #[test]
    fn slice_load_ranges_never_exceed_the_advertised_u32_length() {
        let limit = u32::MAX as usize;
        for length in [limit, usize::MAX] {
            assert_eq!(
                input_range(length, u32::MAX - 1, LoadWidth::Byte),
                Some(limit - 1..limit)
            );
            assert_eq!(
                input_range(length, u32::MAX - 4, LoadWidth::Word),
                Some(limit - 4..limit)
            );
            assert_eq!(input_range(length, u32::MAX, LoadWidth::Byte), None);
            assert_eq!(input_range(length, u32::MAX - 1, LoadWidth::Half), None);
            assert_eq!(input_range(length, u32::MAX - 3, LoadWidth::Word), None);
        }
        assert_eq!(input_range(3, 0, LoadWidth::Word), None);
        assert_eq!(input_range(4, 0, LoadWidth::Word), Some(0..4));
    }

    #[test]
    fn verified_execution_never_exceeds_program_length() {
        let program = Program::verify(&[
            Instruction::statement(opcode::LD_IMM, 1),
            Instruction::jump(opcode::JMP_JEQ_K, 1, 1, 0),
            Instruction::statement(opcode::RET_K, 0),
            Instruction::statement(opcode::RET_K, 1),
        ])
        .unwrap();
        let (_, steps) = program.evaluate_counted(&[][..]);
        assert!(steps <= program.len());
        assert_eq!(steps, 3);
    }
}