Skip to content

Commit 16c9d93

Browse files
JulianGCalderongabrielbosio
authored andcommitted
Replace security anyhow errors with enum variants (#1946)
* Replace anyhow error with variants * Update changelog * Update tests * Use box to keep error enum small
1 parent 7ca79de commit 16c9d93

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* refactor: Replaces security anyhow errors with enum variants [#1946](https://github.com/lambdaclass/cairo-vm/pull/1946)
6+
57
* fix: `mod_builtin_fill_memory` could be stuck in an infinite loop [#1975](https://github.com/lambdaclass/cairo-vm/issues/1975)
68

79
* feat: replace `thiserror-no-std` with `thiserror 2` [#1919](https://github.com/lambdaclass/cairo-vm/pull/1919)

vm/src/vm/errors/vm_errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ pub enum VirtualMachineError {
140140
RelocationNotFound(usize),
141141
#[error("{} batch size is not {}", (*.0).0, (*.0).1)]
142142
ModBuiltinBatchSize(Box<(BuiltinName, usize)>),
143+
#[error("Initial FP should have been initialized")]
144+
MissingInitialFp,
145+
#[error("Return FP address should be in memory: {0}")]
146+
MissingReturnFp(Box<Relocatable>),
147+
#[error("Return FP { } should equal expected final FP { }", (*.0).0, (*.0).1)]
148+
MismatchReturnFP(Box<(Relocatable, Relocatable)>),
149+
#[error("Return FP { } offset should equal expected final FP { } offset", (*.0).0, (*.0).1)]
150+
MismatchReturnFPOffset(Box<(Relocatable, Relocatable)>),
151+
#[error("Return FP felt { } should equal expected final FP { } offset", (*.0).0, (*.0).1)]
152+
MismatchReturnFPFelt(Box<(Felt252, Relocatable)>),
143153
#[error("Blake2s opcode invalid operand: op{0} does not point to {1} u32 numbers.")]
144154
Blake2sInvalidOperand(u8, u8),
145155
#[error("Blake2s opcode invalid flags {0}")]

vm/src/vm/security.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,37 +81,33 @@ pub fn verify_secure_runner(
8181
}
8282

8383
// Validate ret FP.
84-
let initial_fp = runner.get_initial_fp().ok_or_else(|| {
85-
VirtualMachineError::Other(anyhow::anyhow!(
86-
"Failed to retrieve the initial_fp: it is None. \
87-
The initial_fp field should be initialized after running the entry point."
88-
))
89-
})?;
84+
let initial_fp = runner
85+
.get_initial_fp()
86+
.ok_or(VirtualMachineError::MissingInitialFp)?;
9087
let ret_fp_addr = (initial_fp - 2).map_err(VirtualMachineError::Math)?;
91-
let ret_fp = runner.vm.get_maybe(&ret_fp_addr).ok_or_else(|| {
92-
VirtualMachineError::Other(anyhow::anyhow!(
93-
"Ret FP address is not in memory: {ret_fp_addr}"
94-
))
95-
})?;
88+
let ret_fp = runner
89+
.vm
90+
.get_maybe(&ret_fp_addr)
91+
.ok_or(VirtualMachineError::MissingReturnFp(Box::new(ret_fp_addr)))?;
9692
let final_fp = runner.vm.get_fp();
9793
match ret_fp {
9894
MaybeRelocatable::RelocatableValue(value) => {
9995
if runner.runner_mode == RunnerMode::ProofModeCanonical && value != final_fp {
100-
return Err(VirtualMachineError::Other(anyhow::anyhow!(
101-
"Return FP is not equal to final FP: ret_f={ret_fp}, final_fp={final_fp}"
102-
)));
96+
return Err(VirtualMachineError::MismatchReturnFP(Box::new((
97+
value, final_fp,
98+
))));
10399
}
104100
if runner.runner_mode == RunnerMode::ExecutionMode && value.offset != final_fp.offset {
105-
return Err(VirtualMachineError::Other(anyhow::anyhow!(
106-
"Return FP offset is not equal to final FP offset: ret_f={ret_fp}, final_fp={final_fp}"
107-
)));
101+
return Err(VirtualMachineError::MismatchReturnFPOffset(Box::new((
102+
value, final_fp,
103+
))));
108104
}
109105
}
110106
MaybeRelocatable::Int(value) => {
111107
if Felt252::from(final_fp.offset) != value {
112-
return Err(VirtualMachineError::Other(anyhow::anyhow!(
113-
"Return FP felt value is not equal to final FP offset: ret_fp={ret_fp}, final_fp={final_fp}"
114-
)));
108+
return Err(VirtualMachineError::MismatchReturnFPFelt(Box::new((
109+
value, final_fp,
110+
))));
115111
}
116112
}
117113
}
@@ -340,7 +336,7 @@ mod test {
340336

341337
assert_matches!(
342338
verify_secure_runner(&runner, true, None),
343-
Err(VirtualMachineError::Other(ref err)) if err.to_string().contains("Failed to retrieve the initial_fp: it is None")
339+
Err(VirtualMachineError::MissingInitialFp)
344340
);
345341
}
346342

@@ -354,8 +350,7 @@ mod test {
354350
runner.vm.segments.memory = crate::vm::vm_memory::memory::Memory::new();
355351
assert_matches!(
356352
verify_secure_runner(&runner, true, None),
357-
Err(VirtualMachineError::Other(ref err))
358-
if err.to_string().contains("Ret FP address is not in memory")
353+
Err(VirtualMachineError::MissingReturnFp(..))
359354
);
360355
}
361356

@@ -372,8 +367,7 @@ mod test {
372367

373368
assert_matches!(
374369
verify_secure_runner(&runner, true, None),
375-
Err(VirtualMachineError::Other(ref err))
376-
if err.to_string().contains("Return FP is not equal to final FP")
370+
Err(VirtualMachineError::MismatchReturnFP(..))
377371
);
378372
}
379373

@@ -387,8 +381,7 @@ mod test {
387381
// ExecutionMode only requires offset equality, not the entire relocatable.
388382
assert_matches!(
389383
verify_secure_runner(&runner, true, None),
390-
Err(VirtualMachineError::Other(ref err))
391-
if err.to_string().contains("Return FP offset is not equal to final FP offset")
384+
Err(VirtualMachineError::MismatchReturnFPOffset(..))
392385
);
393386
}
394387

@@ -404,8 +397,7 @@ mod test {
404397
// ExecutionMode only requires offset equality, not the entire relocatable.
405398
assert_matches!(
406399
verify_secure_runner(&runner, true, None),
407-
Err(VirtualMachineError::Other(ref err))
408-
if err.to_string().contains("Return FP felt value is not equal to final FP offset")
400+
Err(VirtualMachineError::MismatchReturnFPFelt(..))
409401
);
410402
}
411403
}

0 commit comments

Comments
 (0)