Skip to content

Commit

Permalink
Add new field to OpcodeResolutionError::BrilligFunctionFailed
Browse files Browse the repository at this point in the history
  • Loading branch information
anaPerezGhiglia committed Jun 27, 2024
1 parent fc83d66 commit 54c45a3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
34 changes: 16 additions & 18 deletions acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,16 @@ impl<'b, B: BlackBoxFunctionSolver<F>, F: AcirField> BrilligSolver<'b, F, B> {
VMStatus::Finished { .. } => Ok(BrilligSolverStatus::Finished),
VMStatus::InProgress => Ok(BrilligSolverStatus::InProgress),
VMStatus::Failure { reason, call_stack } => {
match reason {
let call_stack = call_stack
.iter()
.map(|brillig_index| OpcodeLocation::Brillig {
acir_index: self.acir_index,
brillig_index: *brillig_index,
})
.collect();
let (payload, found_trap) = match reason {
FailureReason::RuntimeError { message } => {
let call_stack = call_stack
.iter()
.map(|brillig_index| OpcodeLocation::Brillig {
acir_index: self.acir_index,
brillig_index: *brillig_index,
})
.collect();
Err(OpcodeResolutionError::BrilligFunctionFailed {
payload: Some(ResolvedAssertionPayload::String(message)),
call_stack,
})
(Some(ResolvedAssertionPayload::String(message)), false)
}

FailureReason::Trap { revert_data_offset, revert_data_size } => {
Expand Down Expand Up @@ -218,13 +215,14 @@ impl<'b, B: BlackBoxFunctionSolver<F>, F: AcirField> BrilligSolver<'b, F, B> {
}
}
};

Err(OpcodeResolutionError::UnsatisfiedConstrain {
opcode_location: super::ErrorLocation::Unresolved,
payload,
})
(payload, true)
}
}
};
Err(OpcodeResolutionError::BrilligFunctionFailed {
payload,
call_stack,
found_trap,
})
}
VMStatus::ForeignCallWait { function, inputs } => {
Ok(BrilligSolverStatus::ForeignCallWait(ForeignCallWaitInfo { function, inputs }))
Expand Down
51 changes: 38 additions & 13 deletions acvm-repo/acvm/src/pwg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,53 @@ impl std::fmt::Display for ErrorLocation {
}
}

#[derive(Clone, PartialEq, Eq, Debug, Error)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum OpcodeResolutionError<F> {
#[error("Cannot solve opcode: {0}")]
OpcodeNotSolvable(#[from] OpcodeNotSolvable<F>),
#[error("Cannot satisfy constraint")]
OpcodeNotSolvable(OpcodeNotSolvable<F>),
UnsatisfiedConstrain {
opcode_location: ErrorLocation,
payload: Option<ResolvedAssertionPayload<F>>,
},
#[error("Index out of bounds, array has size {array_size:?}, but index was {index:?}")]
IndexOutOfBounds { opcode_location: ErrorLocation, index: u32, array_size: u32 },
#[error("Failed to solve blackbox function: {0}, reason: {1}")]
IndexOutOfBounds {
opcode_location: ErrorLocation,
index: u32,
array_size: u32,
},
BlackBoxFunctionFailed(BlackBoxFunc, String),
#[error("Failed to solve brillig function")]
BrilligFunctionFailed {
call_stack: Vec<OpcodeLocation>,
payload: Option<ResolvedAssertionPayload<F>>,
found_trap: bool,
},
AcirMainCallAttempted {
opcode_location: ErrorLocation,
},
AcirCallOutputsMismatch {
opcode_location: ErrorLocation,
results_size: u32,
outputs_size: u32,
},
#[error("Attempted to call `main` with a `Call` opcode")]
AcirMainCallAttempted { opcode_location: ErrorLocation },
#[error("{results_size:?} result values were provided for {outputs_size:?} call output witnesses, most likely due to bad ACIR codegen")]
AcirCallOutputsMismatch { opcode_location: ErrorLocation, results_size: u32, outputs_size: u32 },
}

impl<F: acir::AcirField> std::fmt::Display for OpcodeResolutionError<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OpcodeResolutionError::OpcodeNotSolvable(opcode_not_solvable) => write!(f, "Cannot solve opcode: {}", opcode_not_solvable),
OpcodeResolutionError::UnsatisfiedConstrain { .. } => write!(f, "Cannot satisfy constraint"),
OpcodeResolutionError::IndexOutOfBounds { array_size, index, .. } => write!(f, "Index out of bounds, array has size {}, but index was {}", array_size, index),
OpcodeResolutionError::BlackBoxFunctionFailed(func, reason) => write!(f,"Failed to solve blackbox function: {}, reason: {}", func, reason),
OpcodeResolutionError::BrilligFunctionFailed{found_trap: false, .. } => write!(f, "Failed to solve brillig function"),
OpcodeResolutionError::BrilligFunctionFailed{found_trap: true, .. } => write!(f, "Cannot satisfy constraint"),
OpcodeResolutionError::AcirMainCallAttempted{ .. } => write!(f, "Attempted to call `main` with a `Call` opcode"),
OpcodeResolutionError::AcirCallOutputsMismatch {results_size, outputs_size, .. } => write!(f, "{} result values were provided for {} call output witnesses, most likely due to bad ACIR codegen", results_size, outputs_size),
}
}
}

impl<F> From<OpcodeNotSolvable<F>> for OpcodeResolutionError<F> {
fn from(opcode: OpcodeNotSolvable<F>) -> Self {
OpcodeResolutionError::OpcodeNotSolvable(opcode)
}
}

impl<F> From<BlackBoxResolutionError> for OpcodeResolutionError<F> {
Expand Down Expand Up @@ -501,7 +526,7 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> ACVM<'a, F, B> {

fn map_brillig_error(&self, mut err: OpcodeResolutionError<F>) -> OpcodeResolutionError<F> {
match &mut err {
OpcodeResolutionError::BrilligFunctionFailed { call_stack, payload } => {
OpcodeResolutionError::BrilligFunctionFailed { call_stack, payload, .. } => {
// Some brillig errors have static strings as payloads, we can resolve them here
let last_location =
call_stack.last().expect("Call stacks should have at least one item");
Expand Down
3 changes: 2 additions & 1 deletion acvm-repo/acvm/tests/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ fn unsatisfied_opcode_resolved_brillig() {
solver_status,
ACVMStatus::Failure(OpcodeResolutionError::BrilligFunctionFailed {
payload: None,
call_stack: vec![OpcodeLocation::Brillig { acir_index: 0, brillig_index: 3 }]
call_stack: vec![OpcodeLocation::Brillig { acir_index: 0, brillig_index: 3 }],
found_trap: true,
}),
"The first opcode is not satisfiable, expected an error indicating this"
);
Expand Down
2 changes: 1 addition & 1 deletion tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl<'a, B: BlackBoxFunctionSolver<FieldElement>> DebugContext<'a, B> {
self.handle_foreign_call(foreign_call)
}
Err(err) => {
if let OpcodeResolutionError::UnsatisfiedConstrain { .. } = err {
if let OpcodeResolutionError::BrilligFunctionFailed { found_trap: true, .. } = err {
// return solver ownership so brillig_solver it has the right opcode location
self.brillig_solver = Some(solver);
}
Expand Down

0 comments on commit 54c45a3

Please sign in to comment.