-
Notifications
You must be signed in to change notification settings - Fork 387
chore: Replace all SSA interpreter panics with error variants #8311
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5ebba0
Start adding error variants
fd5d66a
Replace all reachable errors with error variants
jfecher 5aca182
Remove some last asserts
jfecher b2119d7
PR feedback
3eaa04a
Add forgotten error handling in intrinsics.rs
jfecher 00e75fd
Clippy needs to run with '--all-targets'
jfecher a216205
Clippy needs to run with that argument and in the exact crate
jfecher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,139 @@ | ||
| use crate::ssa::ir::{ | ||
| basic_block::BasicBlockId, | ||
| instruction::{BinaryOp, Intrinsic}, | ||
| value::ValueId, | ||
| }; | ||
| use acvm::FieldElement; | ||
| use thiserror::Error; | ||
|
|
||
| pub(super) const MAX_SIGNED_BIT_SIZE: u32 = 64; | ||
| pub(super) const MAX_UNSIGNED_BIT_SIZE: u32 = 128; | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub(crate) enum InterpreterError { | ||
| /// These errors are all the result from malformed input SSA | ||
| #[error("{0}")] | ||
| Internal(InternalError), | ||
| #[error("constrain {lhs_id} == {rhs_id} failed:\n {lhs} != {rhs}")] | ||
| ConstrainEqFailed { lhs: String, lhs_id: ValueId, rhs: String, rhs_id: ValueId }, | ||
| #[error("constrain {lhs_id} != {rhs_id} failed:\n {lhs} == {rhs}")] | ||
| ConstrainNeFailed { lhs: String, lhs_id: ValueId, rhs: String, rhs_id: ValueId }, | ||
| #[error("static_assert `{condition}` failed: {message}")] | ||
| StaticAssertFailed { condition: ValueId, message: String }, | ||
| #[error( | ||
| "Range check of {value_id} = {value} failed.\n Max bits allowed by range check = {max_bits}\n Actual bit count = {actual_bits}" | ||
| )] | ||
| RangeCheckFailed { value: String, value_id: ValueId, actual_bits: u32, max_bits: u32 }, | ||
| #[error( | ||
| "Range check of {value_id} = {value} failed.\n Max bits allowed by range check = {max_bits}\n Actual bit count = {actual_bits}\n {message}" | ||
| )] | ||
| RangeCheckFailedWithMessage { | ||
| value: String, | ||
| value_id: ValueId, | ||
| actual_bits: u32, | ||
| max_bits: u32, | ||
| message: String, | ||
| }, | ||
| /// This is not an internal error since the SSA is still valid. We're just not able to | ||
| /// interpret it since we lack the context of what the external function is. | ||
| #[error("Call to unknown foreign function {name}")] | ||
| UnknownForeignFunctionCall { name: String }, | ||
| #[error("Division by zero: `div {lhs_id}, {rhs_id}` ({lhs} / {rhs})")] | ||
| DivisionByZero { lhs_id: ValueId, lhs: String, rhs_id: ValueId, rhs: String }, | ||
| #[error("Underflow in dec_rc when decrementing reference count of `{value_id} = {value}`")] | ||
| DecRcUnderflow { value_id: ValueId, value: String }, | ||
| #[error( | ||
| "Erroneously incremented reference count of value `{value_id} = {value}` from 0 back to 1" | ||
| )] | ||
| IncRcRevive { value_id: ValueId, value: String }, | ||
| #[error("An overflow occurred while evaluating {instruction}")] | ||
| Overflow { instruction: String }, | ||
| #[error( | ||
| "if-else instruction with then condition `{then_condition_id}` and else condition `{else_condition_id}` has both branches as true. This should be impossible except for malformed SSA code" | ||
| )] | ||
| DoubleTrueIfElse { then_condition_id: ValueId, else_condition_id: ValueId }, | ||
| #[error("Tried to pop from empty slice `{slice}` in `{instruction}`")] | ||
| PoppedFromEmptySlice { slice: ValueId, instruction: &'static str }, | ||
| #[error("Unable to convert `{field_id} = {field}` to radix {radix}")] | ||
| ToRadixFailed { field_id: ValueId, field: FieldElement, radix: u32 }, | ||
| } | ||
|
|
||
| /// These errors can only result from interpreting malformed SSA | ||
| #[derive(Debug, Error)] | ||
| pub(crate) enum InternalError { | ||
| #[error( | ||
| "Argument count {arguments} to block {block} does not match the expected parameter count {parameters}" | ||
| )] | ||
| BlockArgumentCountMismatch { block: BasicBlockId, arguments: usize, parameters: usize }, | ||
| #[error( | ||
| "Argument count {arguments} to `{intrinsic}` does not match the expected parameter count {parameters}" | ||
| )] | ||
| IntrinsicArgumentCountMismatch { intrinsic: Intrinsic, arguments: usize, parameters: usize }, | ||
| #[error("Block {block} is missing the terminator instruction")] | ||
| BlockMissingTerminator { block: BasicBlockId }, | ||
| #[error("Cannot call non-function value {value_id} = {value}")] | ||
| CalledNonFunction { value: String, value_id: ValueId }, | ||
| // Note that we don't need to display the value_id because displaying a reference | ||
| // value shows the original value id anyway | ||
| #[error("Reference value `{value}` passed from a constrained to an unconstrained function")] | ||
| ReferenceValueCrossedUnconstrainedBoundary { value: String }, | ||
| #[error("Reference value `{value}` loaded before it was first stored to")] | ||
| UninitializedReferenceValueLoaded { value: String }, | ||
| #[error( | ||
| "Mismatched types in binary operator: `{operator} {lhs_id}, {rhs_id}` ({operator} {lhs}, {rhs})" | ||
| )] | ||
| MismatchedTypesInBinaryOperator { | ||
| lhs_id: ValueId, | ||
| lhs: String, | ||
| operator: BinaryOp, | ||
| rhs_id: ValueId, | ||
| rhs: String, | ||
| }, | ||
| #[error("Unsupported operator `{operator}` for type `{typ}`")] | ||
| UnsupportedOperatorForType { operator: &'static str, typ: &'static str }, | ||
| #[error( | ||
| "Invalid bit size of `{bit_size}` given to truncate, maximum size allowed for unsigned values is {MAX_UNSIGNED_BIT_SIZE}" | ||
| )] | ||
| InvalidUnsignedTruncateBitSize { bit_size: u32 }, | ||
| #[error( | ||
| "Invalid bit size of `{bit_size}` given to truncate, maximum size allowed for signed values is {MAX_SIGNED_BIT_SIZE}" | ||
| )] | ||
| InvalidSignedTruncateBitSize { bit_size: u32 }, | ||
| #[error("Rhs of `{operator}` should be a u8 but found `{rhs_id} = {rhs}`")] | ||
| RhsOfBitShiftShouldBeU8 { operator: &'static str, rhs_id: ValueId, rhs: String }, | ||
| #[error( | ||
| "Expected {expected_type} value in {instruction} but instead found `{value_id} = {value}`" | ||
| )] | ||
| TypeError { | ||
| value_id: ValueId, | ||
| value: String, | ||
| expected_type: &'static str, | ||
| instruction: &'static str, | ||
| }, | ||
| #[error( | ||
| "Function {function} ({function_name}) returned {actual} argument(s) but it was expected to return {expected}" | ||
| )] | ||
| FunctionReturnedIncorrectArgCount { | ||
| function: ValueId, | ||
| function_name: String, | ||
| expected: usize, | ||
| actual: usize, | ||
| }, | ||
| #[error( | ||
| "`truncate {value_id} to 0 bits, max_bit_size: {max_bit_size}` has invalid bit size 0. This should only be possible in malformed SSA." | ||
| )] | ||
| TruncateToZeroBits { value_id: ValueId, max_bit_size: u32 }, | ||
| #[error( | ||
| "`range_check {value_id} to 0 bits` has invalid bit size 0. This should only be possible in malformed SSA." | ||
| )] | ||
| RangeCheckToZeroBits { value_id: ValueId }, | ||
| #[error("`field_less_than` can only be called in unconstrained contexts")] | ||
| FieldLessThanCalledInConstrainedContext, | ||
| #[error("Slice `{slice_id} = {slice}` contains struct/tuple elements of types `({})` and thus needs a minimum length of {} to pop 1 struct/tuple, but it is only of length {actual_length}", element_types.join(", "), element_types.len())] | ||
| NotEnoughElementsToPopSliceOfStructs { | ||
| slice_id: ValueId, | ||
| slice: String, | ||
| actual_length: usize, | ||
| element_types: Vec<String>, | ||
| }, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.