-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct result when assigning shared arrays in unconstrained code (
#4210) # Description ## Problem\* Resolves #3795 ## Summary\* `Instruction::IncrementRc` in Brillig actually only increments the reference count on a copy of an array's metadata if the array was loaded from a reference (while the loaded array is a reference rather than a copy). This is suboptimal since it was meant to be shared across references to the array. This is what caused the original issue where mutating the reference saw a different reference count and thought it was safe to mutate without copying first. I've added a somewhat hacky check in the method to increment reference counts to check if the array was loaded from a reference. If so, we load a fresh value, increment rc on that, and re-store it to update the metadata in the original array. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
- Loading branch information
Showing
6 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/brillig_cow_assign/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "brillig_cow_assign" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.23.0" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
test_programs/execution_success/brillig_cow_assign/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
items_to_update = 10 | ||
index = 6 |
23 changes: 23 additions & 0 deletions
23
test_programs/execution_success/brillig_cow_assign/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
global N = 10; | ||
|
||
unconstrained fn main() { | ||
let mut arr = [0; N]; | ||
let mut mid_change = arr; | ||
|
||
for i in 0..N { | ||
if i == N / 2 { | ||
mid_change = arr; | ||
} | ||
arr[i] = 27; | ||
} | ||
|
||
// Expect: | ||
// arr = [27, 27, 27, 27, 27, 27, 27, 27, 27, 27] | ||
// mid_change = [27, 27, 27, 27, 27, 0, 0, 0, 0, 0] | ||
|
||
let modified_i = N / 2 + 1; | ||
assert_eq(arr[modified_i], 27); | ||
|
||
// Fail here! | ||
assert(mid_change[modified_i] != 27); | ||
} |