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
45 changes: 45 additions & 0 deletions compiler/noirc_evaluator/src/acir/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ impl Context<'_> {
unreachable!("ICE: expected array or slice type");
};

// For 0-length arrays and slices, even the disabled memory operations would cause runtime failures.
// Set rhe result to a zero value that matches the type then bypass the rest of the operation,
// leaving an assertion that the side effect variable must be false.
if self.flattened_size(array, dfg) == 0 {
// Zero result.
let results = dfg.instruction_results(instruction);
let res_typ = dfg.type_of_value(results[0]);
let zero = self.array_zero_value(&res_typ)?;
self.define_result(dfg, instruction, zero);
// Make sure this code is disabled, or fail with "Index out of bounds".
let msg = "Index out of bounds, array has size 0".to_string();
let msg = self.acir_context.generate_assertion_message_payload(msg);
let zero = self.acir_context.add_constant(FieldElement::zero());
return self.acir_context.assert_eq_var(
self.current_side_effects_enabled_var,
zero,
Some(msg),
);
}

if self.handle_constant_index_wrapper(instruction, dfg, array, index, store_value)? {
return Ok(());
}
Expand Down Expand Up @@ -472,6 +492,31 @@ impl Context<'_> {
}
}

/// Construct a value with all zero values, which we can use to provide a default value
/// when we cannot use `array_get_value` because the array length itself is zero, yet
/// we also don't want a memory operation to fail, because the operation will never
/// actually run, because we know that the side effect variable is false.
pub(super) fn array_zero_value(&mut self, ssa_type: &Type) -> Result<AcirValue, RuntimeError> {
match ssa_type.clone() {
Type::Numeric(numeric_type) => {
let zero = self.acir_context.add_constant(FieldElement::zero());
let typ = AcirType::NumericType(numeric_type);
Ok(AcirValue::Var(zero, typ))
}
Type::Array(element_types, len) => {
let mut values = im::Vector::new();
for _ in 0..len {
for typ in element_types.as_ref() {
values.push_back(self.array_zero_value(typ)?);
}
}
Ok(AcirValue::Array(values))
}
Type::Reference(reference_type) => self.array_zero_value(reference_type.as_ref()),
_ => unreachable!("ICE: Expected an array or numeric but got {ssa_type:?}"),
}
}

/// If `mutate_array` is:
/// - `true`: Mutate the array directly
/// - `false`: Copy the array and generates a write opcode on the new array. This is
Expand Down
8 changes: 8 additions & 0 deletions test_programs/execution_success/regression_9193/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "regression_9193"
type = "bin"
authors = [""]

[dependencies]


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = false
3 changes: 3 additions & 0 deletions test_programs/execution_success/regression_9193/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(a: bool) {
if a { 1 / [][0]; };
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This program expects the non-deadbeef failure message, so the deadbeef case is treated as an actual failure
#[fuzz(should_fail_with = "input is not 0xdeadbeef")]
#[fuzz(only_fail_with = "input is not 0xdeadbeef")]
fn fuzz(input: u64) {
assert(input == 0xdeadbeef, "input is not 0xdeadbeef");
assert(input != 0xdeadbeef, "input is 0xdeadbeef");
Expand Down
6 changes: 6 additions & 0 deletions test_programs/noir_test_success/regression_9174/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_9174"
type = "bin"
authors = [""]

[dependencies]
45 changes: 45 additions & 0 deletions test_programs/noir_test_success/regression_9174/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
global G_A: [Field] = &[];
fn main(a: bool, b: bool, c: Field) -> pub (Field, Field) {
if a {
(
{
let i = G_A;
i[(2050918985_u32 % i.len())]
},
// Safety: test
unsafe {
func_1_proxy(
if a {
if b {
func_1_proxy(0) as u32
} else {
766482358_u32
}
} else {
1568313658_u32
},
)
},
)
} else {
(
{
let k = G_A;
k[(779011912_u32 % k.len())]
}, c,
)
}
}

unconstrained fn func_1_proxy(_a: u32) -> Field {
0
}

#[test(should_fail_with = "Cannot satisfy constraint")]
fn fuzz_main(a: bool, b: bool) -> (Field, Field) {
main(
a,
b,
0x08c9c54133258070e36902247f2c89f746736c2abeaba3283c66587b49e9b512,
)
}
5 changes: 4 additions & 1 deletion tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ fn generate_fuzzing_failure_tests(test_file: &mut File, test_data_dir: &Path) {
&test_name,
&test_dir,
r#"
nargo.assert().failure().stderr(predicate::str::contains("Failing input"));
nargo.assert().failure().stderr(
predicate::str::contains("Failing input").and(
predicate::str::contains("got a different failing assertion").not())
);
"#,
240,
);
Expand Down

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