Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
chore: set Brillig VM state to Failure rather than panicking on inv…
Browse files Browse the repository at this point in the history
…alid foreign call response (#375)
  • Loading branch information
TomAFrench authored Jun 14, 2023
1 parent 26ff206 commit c49d82c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,23 @@ impl VM {
let ForeignCallResult { values } =
&self.foreign_call_results[self.foreign_call_counter];

let mut invalid_foreign_call_result = false;
for (destination, values) in destinations.iter().zip(values) {
match destination {
RegisterValueOrArray::RegisterIndex(index) => {
assert_eq!(
values.len(),
1,
"Function result size does not match brillig bytecode"
);
if values.len() != 1 {
invalid_foreign_call_result = true;
break;
}

self.registers.set(*index, values[0])
}
RegisterValueOrArray::HeapArray(index, size) => {
assert_eq!(
values.len(),
*size,
"Function result size does not match brillig bytecode"
);
if values.len() != *size {
invalid_foreign_call_result = true;
break;
}

// Convert the destination pointer to a usize
let destination = self.registers.get(*index).to_usize();
// Expand memory if the array to be written
Expand All @@ -238,7 +239,12 @@ impl VM {
}
}

// This check must come after resolving the foreign call outputs as `fail` uses a mutable reference
// These checks must come after resolving the foreign call outputs as `fail` uses a mutable reference
if invalid_foreign_call_result {
return VMStatus::Failure {
message: "Function result size does not match brillig bytecode".to_owned(),
};
}
if destinations.len() != values.len() {
self.fail(format!("{} output values were provided as a foreign call result for {} destination slots", values.len(), destinations.len()));
}
Expand Down

0 comments on commit c49d82c

Please sign in to comment.