Skip to content
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
35 changes: 35 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/remove_unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn used_functions(func: &Function) -> BTreeSet<FunctionId> {
Instruction::Store { .. }
| Instruction::Call { .. }
| Instruction::MakeArray { .. }
| Instruction::ArraySet { .. }
) {
instruction.for_each_value(&mut find_functions);
}
Expand Down Expand Up @@ -211,4 +212,38 @@ mod tests {

assert_normalized_ssa_equals(ssa, src);
}

#[test]
fn keep_functions_used_in_array_set() {
// Regression test for issue "V-NSCA-VUL-003: Missing ArraySet case in Removing Unreachable Functions pass"
// found in Veridise Audit. https://github.com/noir-lang/noir/issues/8890

// f2 is written to an array using an `array_set` instruction. Thus, we do not want to remove it.
let src = r#"
acir(inline) fn main f0 {
b0(v0: Field, v1: Field):
v2 = make_array [f1] : [function; 1]
v3 = array_set v2, index u32 0, value f2
v4 = array_get v3, index u32 0 -> function
v5 = call v4(v0) -> Field
return
}

acir(inline) fn my_fun f1 {
b0(v0: Field):
v2 = add v0, Field 1
return v2
}

acir(inline) fn my_fun2 f2 {
b0(v0: Field):
v2 = add v0, Field 2
return v2
}"#;

let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.remove_unreachable_functions();

assert_normalized_ssa_equals(ssa, src);
}
}
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"nomicfoundation",
"noncanonical",
"nouner",
"NSCA",
"oneof",
"oneshot",
"Ottenstein",
Expand Down Expand Up @@ -288,6 +289,7 @@
"vecmap",
"vecs",
"Vecs",
"Veridise",
"vitkov",
"VM",
"walkdir",
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_8890/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_8890"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
4 changes: 4 additions & 0 deletions test_programs/execution_success/regression_8890/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x = "3"
y = "4"

return = 5
17 changes: 17 additions & 0 deletions test_programs/execution_success/regression_8890/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Regression test for issue "V-NSCA-VUL-003: Missing ArraySet case in Removing Unreachable Functions pass"
// found in Veridise Audit.

fn main(x: Field, y: pub Field) -> pub Field {
assert(x != y);
let mut f = [my_fun];
f[0] = my_fun2;
(f[0])(x)
}

fn my_fun(x: Field) -> Field {
x + 1
}

fn my_fun2(x: Field) -> Field {
x + 2
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading