Skip to content

Commit

Permalink
fix: add check against cache overflow (#1285)
Browse files Browse the repository at this point in the history
* Add check against cache overflow

* Fix off-by-one error

* Update changelog
  • Loading branch information
MegaRedHand committed Jun 29, 2023
1 parent d46d2b6 commit 7e1df87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#### Upcoming Changes

* fix(security): avoid OOM crashes when programs jump to very high invalid addresses.

* fix: add `to_bytes_be` to the felt when `lambdaworks-felt` feature is active [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290)

* chore: mark `modpow` and `to_signed_bytes_le` as *deprecated* [#1290](https://github.com/lambdaclass/cairo-vm/pull/1290)

* fix: bump *lambdaworks-math* to latest version, that fixes no-std support [#1293](https://github.com/lambdaclass/cairo-vm/pull/1293)

* build: remove dependecy to `thiserror` (use `thiserror-no-std/std` instead)
* build: remove dependency to `thiserror` (use `thiserror-no-std/std` instead)

* chore: use LambdaWorks' implementation of bit operations for `Felt252` [#1291](https://github.com/lambdaclass/cairo-rs/pull/1291)

Expand Down
22 changes: 22 additions & 0 deletions cairo_programs/manually_compiled/overflowing_dict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"attributes": [],
"builtins": [],
"compiler_version": "0.11.0",
"data": [
"0x1104800180018000",
"0x80000000000001"
],
"hints": {},
"identifiers": {
"__main__.main": {
"decorators": [],
"pc": 0,
"type": "function"
}
},
"main_scope": "__main__",
"prime": "0x800000000000011000000000000000000000000000000000000000000000001",
"reference_manager": {
"references": []
}
}
7 changes: 7 additions & 0 deletions vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,3 +975,10 @@ fn cairo_run_if_reloc_equal() {
let program_data = include_bytes!("../../../cairo_programs/if_reloc_equal.json");
run_program_simple_with_memory_holes(program_data, 4);
}

#[test]
fn cairo_run_overflowing_dict() {
let program_data =
include_bytes!("../../../cairo_programs/manually_compiled/overflowing_dict.json");
run_program_with_error(program_data, "Unknown memory cell at address");
}
4 changes: 4 additions & 0 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ impl VirtualMachine {
pub fn step_instruction(&mut self) -> Result<(), VirtualMachineError> {
let pc = self.run_context.pc.offset;

if self.segments.memory.data[0].len() <= pc {
return Err(MemoryError::UnknownMemoryCell(Box::new((0, pc).into())))?;
}

let mut inst_cache = core::mem::take(&mut self.instruction_cache);
inst_cache.resize((pc + 1).max(inst_cache.len()), None);

Expand Down

1 comment on commit 7e1df87

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 7e1df87 Previous: d46d2b6 Ratio
add_u64_with_felt/3 2 ns/iter (± 0) 1 ns/iter (± 0) 2
add_u64_with_felt/4 2 ns/iter (± 0) 1 ns/iter (± 0) 2
add_u64_with_felt/6 4 ns/iter (± 0) 3 ns/iter (± 0) 1.33
add_u64_with_felt/7 4 ns/iter (± 0) 3 ns/iter (± 0) 1.33

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.