miden_processor/chiplets/memory/
errors.rs

1use alloc::sync::Arc;
2
3use miden_core::Felt;
4use miden_debug_types::{SourceFile, SourceSpan};
5use miden_utils_diagnostics::{Diagnostic, miette};
6
7use crate::{ContextId, ErrorContext};
8
9#[derive(Debug, thiserror::Error, Diagnostic)]
10pub enum MemoryError {
11    #[error("memory address cannot exceed 2^32 but was {addr}")]
12    #[diagnostic()]
13    AddressOutOfBounds {
14        #[label]
15        label: SourceSpan,
16        #[source_code]
17        source_file: Option<Arc<SourceFile>>,
18        addr: u64,
19    },
20    #[error(
21        "memory address {addr} in context {ctx} was read and written, or written twice, in the same clock cycle {clk}"
22    )]
23    IllegalMemoryAccess { ctx: ContextId, addr: u32, clk: Felt },
24    #[error(
25        "memory range start address cannot exceed end address, but was ({start_addr}, {end_addr})"
26    )]
27    InvalidMemoryRange { start_addr: u64, end_addr: u64 },
28    #[error(
29        "word memory access at address {addr} in context {ctx} is unaligned at clock cycle {clk}"
30    )]
31    #[diagnostic(help(
32        "ensure that the memory address accessed is aligned to a word boundary (it is a multiple of 4)"
33    ))]
34    UnalignedWordAccess {
35        #[label("tried to access memory address {addr}")]
36        label: SourceSpan,
37        #[source_code]
38        source_file: Option<Arc<SourceFile>>,
39        addr: u32,
40        ctx: ContextId,
41        clk: Felt,
42    },
43    // Note: we need this version as well because to handle advice provider calls, which don't
44    // have access to the clock.
45    #[error("word access at memory address {addr} in context {ctx} is unaligned")]
46    UnalignedWordAccessNoClk { addr: u32, ctx: ContextId },
47}
48
49impl MemoryError {
50    pub fn unaligned_word_access(
51        addr: u32,
52        ctx: ContextId,
53        clk: Felt,
54        err_ctx: &impl ErrorContext,
55    ) -> Self {
56        let (label, source_file) = err_ctx.label_and_source_file();
57        MemoryError::UnalignedWordAccess { addr, ctx, clk, label, source_file }
58    }
59
60    pub fn address_out_of_bounds(addr: u64, err_ctx: &impl ErrorContext) -> Self {
61        let (label, source_file) = err_ctx.label_and_source_file();
62        MemoryError::AddressOutOfBounds { label, source_file, addr }
63    }
64}