Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check overflow in cairo pie address calculation #1945

Merged
merged 8 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* fix: Check overflow in cairo pie address calculation [#1945](https://github.com/lambdaclass/cairo-vm/pull/1945)

#### [2.0.0-rc5] - 2025-02-24

* fix: Fix Cairo Pie limiting the number of segments to 2^16 [#1960](https://github.com/lambdaclass/cairo-vm/pull/1960)
Expand Down
23 changes: 22 additions & 1 deletion vm/src/vm/runners/cairo_pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,17 @@ pub(super) mod serde_impl {
let mut res = Vec::with_capacity(mem_cap);

for ((segment, offset), value) in values.iter() {
let mem_addr = ADDR_BASE + *segment as u64 * OFFSET_BASE + *offset as u64;
// mem_addr = ADDR_BASE + segment * OFFSET_BASE + offset
let mem_addr = (*segment as u64)
.checked_mul(OFFSET_BASE)
.and_then(|n| n.checked_add(ADDR_BASE))
.and_then(|n| n.checked_add(*offset as u64))
.ok_or_else(|| {
serde::ser::Error::custom(format!(
"failed to serialize address: {segment}:{offset}"
))
})?;

res.extend_from_slice(mem_addr.to_le_bytes().as_ref());
match value {
// Serializes RelocatableValue(little endian):
Expand Down Expand Up @@ -939,6 +949,17 @@ mod test {
);
}

#[test]
fn serialize_cairo_pie_memory_with_overflow() {
let memory = CairoPieMemory(vec![
((0, 0), MaybeRelocatable::Int(0.into())),
((0, 1), MaybeRelocatable::Int(1.into())),
((usize::MAX, 0), MaybeRelocatable::Int(2.into())),
]);

serde_json::to_value(memory).unwrap_err();
}

#[rstest]
#[cfg(feature = "std")]
#[case(include_bytes!("../../../../cairo_programs/fibonacci.json"), "fibonacci")]
Expand Down
Loading