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