cairo_vm/vm/errors/
memory_errors.rs

1// The `(*.0).0` syntax of thiserror falsely triggers this clippy warning
2#![allow(clippy::explicit_auto_deref)]
3
4use crate::stdlib::prelude::*;
5use crate::types::builtin_name::BuiltinName;
6
7use thiserror::Error;
8
9use crate::Felt252;
10
11use crate::types::{
12    errors::math_errors::MathError,
13    relocatable::{MaybeRelocatable, Relocatable},
14};
15
16#[derive(Debug, PartialEq, Error)]
17pub enum MemoryError {
18    #[error("Cell {0} has already been accessed; it cannot be removed.")]
19    UnsetAccessedCell(Relocatable),
20    #[error("Cell {0} is not allocated; it cannot be removed.")]
21    UnsetUnallocatedCell(Relocatable),
22    #[error(transparent)]
23    Math(#[from] MathError),
24    #[error(transparent)]
25    InsufficientAllocatedCells(#[from] InsufficientAllocatedCellsError),
26    #[error("Can't insert into segment #{}; memory only has {} segment", (*.0).0, (*.0).1)]
27    UnallocatedSegment(Box<(usize, usize)>),
28    #[error("Memory addresses must be relocatable")]
29    AddressNotRelocatable,
30    #[error("Range-check validation failed, number {} is out of valid range [0, {}]", (*.0).0, (*.0).1)]
31    RangeCheckNumOutOfBounds(Box<(Felt252, Felt252)>),
32    #[error("Range-check validation failed, encountered non-int value at address {0}")]
33    RangeCheckFoundNonInt(Box<Relocatable>),
34    #[error("Inconsistent memory assignment at address {:?}. {:?} != {:?}", (*.0).0, (*.0).1, (*.0).2)]
35    InconsistentMemory(Box<(Relocatable, MaybeRelocatable, MaybeRelocatable)>),
36    #[error("Inconsistent Relocation")]
37    Relocation,
38    #[error("Could not cast arguments")]
39    WriteArg,
40    #[error("Memory addresses mustn't be in a TemporarySegment, segment: {0}")]
41    AddressInTemporarySegment(isize),
42    #[error("Memory addresses must be in a TemporarySegment, segment: {0}")]
43    AddressNotInTemporarySegment(isize),
44    #[error("Temporary segment found while relocating (flattening), segment: {0}")]
45    TemporarySegmentInRelocation(isize),
46    #[error("The TemporarySegment: {0} doesn't have a relocation address")]
47    NonZeroOffset(usize),
48    #[error("Attempt to overwrite a relocation rule, segment: {0}")]
49    DuplicatedRelocation(isize),
50    #[error("Segment effective sizes haven't been calculated.")]
51    MissingSegmentUsedSizes,
52    #[error("Found a memory gap when calling get_continuous_range with base:{} and size: {}", (*.0).0, (*.0).1)]
53    GetRangeMemoryGap(Box<(Relocatable, usize)>),
54    #[error("Error calculating builtin memory units")]
55    ErrorCalculatingMemoryUnits,
56    #[error("Missing memory cells for {0}")]
57    MissingMemoryCells(Box<BuiltinName>),
58    #[error("Missing memory cells for {}: {:?}", (*.0).0, (*.0).1)]
59    MissingMemoryCellsWithOffsets(Box<(BuiltinName, Vec<usize>)>),
60    #[error("ErrorInitializing Verifying Key from public key: {0:?}")]
61    InitializingVerifyingKey(Box<Vec<u8>>),
62    #[error(
63        "Signature {}, is invalid, with respect to the public key {}, 
64    and the message hash {}.", (*.0).0, (*.0).1, (*.0).2
65    )]
66    InvalidSignature(Box<(String, Felt252, Felt252)>),
67    #[error(
68        "Signature hint is missing for ECDSA builtin at address {0}.
69    Add it using 'ecdsa_builtin.add_signature'."
70    )]
71    SignatureNotFound(Box<Relocatable>),
72    #[error("Could not create pubkey from: {0:?}")]
73    ErrorParsingPubKey(Box<str>),
74    #[error("Could not retrieve message from: {0:?}")]
75    ErrorRetrievingMessage(Box<str>),
76    #[error("Error verifying given signature")]
77    ErrorVerifyingSignature,
78    #[error("Couldn't obtain a mutable accessed offset")]
79    CantGetMutAccessedOffset,
80    #[error("ECDSA builtin: Expected public key at address {0} to be an integer")]
81    PubKeyNonInt(Box<Relocatable>),
82    #[error("ECDSA builtin: Expected message hash at address {0} to be an integer")]
83    MsgNonInt(Box<Relocatable>),
84    #[error("Failed to convert String: {0} to FieldElement")]
85    FailedStringToFieldElementConversion(Box<str>),
86    #[error("Failed to fetch {} return values, ap is only {}", (*.0).0, (*.0).1)]
87    FailedToGetReturnValues(Box<(usize, Relocatable)>),
88    #[error("Segment {} has {} amount of accessed addresses but its size is only {}.", (*.0).0, (*.0).1, (*.0).2)]
89    SegmentHasMoreAccessedAddressesThanSize(Box<(usize, usize, usize)>),
90    #[error("gen_arg: found argument of invalid type.")]
91    GenArgInvalidType,
92    // Memory.get() errors
93    #[error("Expected integer at address {0}")]
94    ExpectedInteger(Box<Relocatable>),
95    #[error("Expected relocatable at address {0}")]
96    ExpectedRelocatable(Box<Relocatable>),
97    #[error("Unknown memory cell at address {0}")]
98    UnknownMemoryCell(Box<Relocatable>),
99    // SegmentArenaBuiltin
100    #[error("segment_arena_builtin: assert used >= INITIAL_SEGMENT_SIZE")]
101    InvalidUsedSizeSegmentArena,
102    #[error("Vector capacity exceeded")]
103    VecCapacityExceeded,
104    #[error("Memory wasn't relocated")]
105    UnrelocatedMemory,
106    #[error("Malformed public memory")]
107    MalformedPublicMemory,
108}
109
110#[derive(Debug, PartialEq, Eq, Error)]
111pub enum InsufficientAllocatedCellsError {
112    #[error("Number of steps must be at least {} for the {}.", (*.0).0, (*.0).1)]
113    MinStepNotReached(Box<(usize, BuiltinName)>),
114    #[error("The {} used {} cells but the capacity is {}.", (*.0).0, (*.0).1, (*.0).2)]
115    BuiltinCells(Box<(BuiltinName, usize, usize)>),
116    #[error("There are only {} cells to fill the range checks holes, but potentially {} are required.", (*.0).0, (*.0).1)]
117    RangeCheckUnits(Box<(usize, usize)>),
118    #[error("There are only {} cells to fill the diluted check holes, but potentially {} are required.", (*.0).0, (*.0).1)]
119    DilutedCells(Box<(usize, usize)>),
120    #[error("There are only {} cells to fill the memory address holes, but {} are required.", (*.0).0, (*.0).1)]
121    MemoryAddresses(Box<(u32, usize)>),
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[test]
129    // Test to catch possible enum size regressions
130    fn test_memory_error_size() {
131        let size = crate::stdlib::mem::size_of::<MemoryError>();
132        assert!(size <= 24, "{size}")
133    }
134
135    #[test]
136    // Test to catch possible enum size regressions
137    fn test_insufficient_allocated_cells_error_size() {
138        let size = crate::stdlib::mem::size_of::<InsufficientAllocatedCellsError>();
139        assert!(size <= 16, "{size}")
140    }
141}