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

[T]::pop_front() panics when inside non-executed if #5462

Closed
michaeljklein opened this issue Jul 9, 2024 · 1 comment · Fixed by #6274
Closed

[T]::pop_front() panics when inside non-executed if #5462

michaeljklein opened this issue Jul 9, 2024 · 1 comment · Fixed by #6274
Labels
bug Something isn't working

Comments

@michaeljklein
Copy link
Contributor

michaeljklein commented Jul 9, 2024

Aim

Attempted to conditionally pop_front from a slice, depending on whether it's empty:

fn main() {
    let empty_slice: [u8] = &[];

    // Also panics:
    // if empty_slice != &[] {
    if empty_slice.len() != 0 {
        // Works:
        // let _ = empty_slice.pop_back();

        // Panics:
        let _ = empty_slice.pop_front();
    }
}

Expected Behavior

I expected the predicate to prevent pop_front from executing and failing when the slice is empty.

Bug

The application panicked (crashed).
Message:  There are no elements in this slice to be removed
Location: compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs:162

To Reproduce

Project Impact

Nice-to-have

Impact Context

NOTE: this is required to finish the slice control flow test here.

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

Binary (noirup default)

Nargo Version

nargo version = 0.31.0 noirc version = 0.31.0+d44f882be094bf492b1742370fd3896b0c371f59 (git version hash: d44f882, is dirty: false)

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@michaeljklein michaeljklein added the bug Something isn't working label Jul 9, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 9, 2024
@jfecher
Copy link
Contributor

jfecher commented Jul 10, 2024

Looks like pop_back is broken as well but in a different way.

Instead of calling pop_back on its slice internally it repeatedly calls push_front to build a new slice. I believe this is to avoid logic on guessing how many times to pop when the element is a struct type which is represented as multiple SSA values.

Anyway, neither pop_back nor pop_front have checks for if the slice is empty. pop_front leads to the error you found while pop_back will silently "optimize" to an empty array result. Execution will still fail though because it will leave behind a v0 = sub u32 0, u32 1 instruction. When this happens the program just exits with "Constraint Failed" and no explanation.

It looks like there is a length check on SliceRemove and SliceInsert at least.

github-merge-queue bot pushed a commit that referenced this issue Jul 25, 2024
# Description

TODO
- [x] Make note to finish slice test after `pop_front` issue:
#5462
- [x] Refactor `any::<T>()`
- [x] Make issues for failing tests

## Problem\*

Part of #5362

## Summary\*

Simple combinator-based parsing tests for control flow.
- Originally designed for slices, but adapted to arrays by emulating
slices as bounded vectors

```rust
struct Bvec<T, let N: u32> {
    inner: [T; N],

    // elements at indices < offset are zero
    offset: u32, 

    // elements at indices >= len are zero
    len: u32,
}
```

## Additional Context

This code is hacky, but let me know if any parts are worth cleaning up
for the stdlib, e.g. `reverse_array`.

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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: jfecher <[email protected]>
github-merge-queue bot pushed a commit that referenced this issue Oct 11, 2024
# Description

## Problem\*

Partial fix to #5462

## Summary\*

This PR doesn't fully fix #5462
but allows full compilation of a program without panicking. The test
program from #5462 now successfully compiles (due to us being able to
remove the inactive branch during inlining)


We still fail to properly handle branches which are conditional on
runtime values such as the below:
```
fn main(active: bool) {
    let empty_slice: [u8] = &[];

    if !active {
        let _ = empty_slice.pop_front();
    }
}
```

This compiles to the below program

```
Compiled ACIR for main (unoptimized):
func 0
current witness index : 2
private parameters indices : [0]
public parameters indices : []
return value indices : []
BLACKBOX::RANGE [(0)] [ ]
INIT (id: 0, len: 0) 
EXPR [ (-1, _1) 0 ]
MEM (id: 0, read at: x1, value: x2) 
```

This will then fail to execute with the below error:
```

error: Index out of bounds, array has size 0, but index was 0
   ┌─ /mnt/user-data/tom/noir/test_programs/execution_success/inactive_slice_popping/src/main.nr:11:17
   │
11 │         let _ = empty_slice.pop_front();
   │                 ---------------------
   │
   = Call stack:
     1. /mnt/user-data/tom/noir/test_programs/execution_success/inactive_slice_popping/src/main.nr:11:17

```

Note the memory block has been allocated with a length of 0 so even
applying a predicate to the array get will fail.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Oct 11, 2024
michaeljklein added a commit that referenced this issue Oct 11, 2024
github-merge-queue bot pushed a commit that referenced this issue Oct 11, 2024
# Description

## Problem\*

Regression test for github.com//issues/5462

## Summary\*

- Added the test from [5462](github.com//issues/5462)
- Updated the issue needed to re-enable the `array_regex` execution test

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
TomAFrench added a commit that referenced this issue Oct 22, 2024
* master: (202 commits)
  chore: remove usage of slices in pedersen hash (#6295)
  chore: remove dead function (#6308)
  feat: new formatter (#6300)
  feat: Sync from aztec-packages (#6301)
  fix: Allow array map on empty arrays (#6305)
  fix: Display function name and body when inlining recursion limit hit (#6291)
  feat(interpreter): Comptime derive generators (#6303)
  fix: enforce correctness of decompositions performed at compile time (#6278)
  feat: Warn about private types leaking in public functions and struct fields (#6296)
  chore(docs): refactoring guides and some other nits (#6175)
  fix: Do not warn on unused self in traits (#6298)
  fix: Reject invalid expression with in CLI parser (#6287)
  chore: regression test for #5462 (#6286)
  feat(improve): Remove scan through globals (#6282)
  feat: show LSP diagnostic related information (#6277)
  feat: trait inheritance (#6252)
  feat: optimize reading a workspace's files (#6281)
  fix: prevent compiler panic when popping from empty slices (#6274)
  feat(test): Include the PoseidonHasher in the fuzzing (#6280)
  feat: slightly improve "unexpected token" error message (#6279)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants