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: prevent compiler panic when popping from empty slices #6274

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ impl Intrinsic {
// These apply a constraint that the input must fit into a specified number of limbs.
Intrinsic::ToBits(_) | Intrinsic::ToRadix(_) => true,

// These imply a check that the slice is non-empty and should fail otherwise.
Intrinsic::SlicePopBack | Intrinsic::SlicePopFront | Intrinsic::SliceRemove => true,

Intrinsic::ArrayLen
| Intrinsic::ArrayAsStrUnchecked
| Intrinsic::AsSlice
| Intrinsic::SlicePushBack
| Intrinsic::SlicePushFront
| Intrinsic::SlicePopBack
| Intrinsic::SlicePopFront
| Intrinsic::SliceInsert
| Intrinsic::SliceRemove
| Intrinsic::StrAsBytes
| Intrinsic::FromField
| Intrinsic::AsField
Expand Down
21 changes: 21 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SlicePopBack => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to pop the last element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
if let Some((_, typ)) = slice {
simplify_slice_pop_back(typ, arguments, dfg, block, call_stack.clone())
Expand All @@ -174,6 +181,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SlicePopFront => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to pop the first element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
if let Some((mut slice, typ)) = slice {
let element_count = typ.element_size();
Expand Down Expand Up @@ -225,6 +239,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SliceRemove => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to remove an element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
let index = dfg.get_numeric_constant(arguments[2]);
if let (Some((mut slice, typ)), Some(index)) = (slice, index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ impl<'a> SliceCapacityTracker<'a> {
let slice_contents = arguments[argument_index];

if let Some(contents_capacity) = slice_sizes.get(&slice_contents) {
let new_capacity = *contents_capacity - 1;
// We use a saturating sub here as calling `pop_front` or `pop_back`
// on a zero-length slice would otherwise underflow.
let new_capacity = contents_capacity.saturating_sub(1);
slice_sizes.insert(result_slice, new_capacity);
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/remove_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ impl Context {
}
SizeChange::Dec { old, new } => {
let old_capacity = self.get_or_find_capacity(&function.dfg, old);
self.slice_sizes.insert(new, old_capacity - 1);
// We use a saturating sub here as calling `pop_front` or `pop_back` on a zero-length slice
// would otherwise underflow.
self.slice_sizes.insert(new, old_capacity.saturating_sub(1));
}
}
}
Expand Down
Loading