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

feat: set Brillig VM state to Failure rather than panicking on invalid foreign call response #375

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Changes from all commits
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
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