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

fix!: Only decrement the counter of an array if its address has not changed #7297

Merged
merged 21 commits into from
Feb 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Add Brillig check that after decrease RC it's at least 1
aakoshh committed Feb 5, 2025

Verified

This commit was signed with the committer’s verified signature.
aakoshh Akosh Farkash
commit 5d4c4298c98b0f1f358a79248d822b4cfac4735d
33 changes: 33 additions & 0 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
@@ -846,6 +846,13 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
BrilligBinaryOp::Sub,
1,
);

// Check that the refcount doesn't go below 1. If we allow it to underflow
// and become 0 or usize::MAX, and then return to 1, then it will indicate
// an array as mutable when it probably shouldn't be.
// We could use `add_overflow_check` here but going to 0 is the first indication of a problem.
self.add_usize_minimum_check(rc_register, 1, "array ref-count went to zero");

self.brillig_context
.store_instruction(array_or_vector.extract_register(), rc_register);
self.brillig_context.deallocate_register(rc_register);
@@ -1528,6 +1535,32 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
self.brillig_context.deallocate_single_addr(left_is_negative);
}

/// Check that some value did not go below a minimum.
fn add_usize_minimum_check(&mut self, addr: MemoryAddress, minimum: usize, msg: &str) {
let min_addr = if minimum == 1 {
ReservedRegisters::usize_one()
} else {
let min_var =
self.brillig_context.make_usize_constant_instruction(FieldElement::from(minimum));
min_var.address
};
let condition = SingleAddrVariable::new(self.brillig_context.allocate_register(), 1);

self.brillig_context.memory_op_instruction(
min_addr,
addr,
condition.address,
BrilligBinaryOp::LessThanEquals,
);

self.brillig_context.codegen_constrain(condition, Some(msg.to_owned()));

self.brillig_context.deallocate_single_addr(condition);
if minimum != 1 {
self.brillig_context.deallocate_register(min_addr);
}
}

#[allow(clippy::too_many_arguments)]
fn add_overflow_check(
&mut self,