Skip to content

Commit

Permalink
Merge pull request #40 from entropyxyz/add-empty-bytecode
Browse files Browse the repository at this point in the history
added EmptyBytecode variant to RuntimeError
  • Loading branch information
jakehemmerle authored Nov 27, 2023
2 parents 008fd64 + 0c2a673 commit 52c1c84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ pub use bindgen::{Error as ProgramError, Program, SignatureRequest};
/// Runtime `Error` type
#[derive(Debug, Error)]
pub enum RuntimeError {
/// Program bytecode is invalid.
/// Program bytecode is of zero length (core-side runtime error; Programs should probably not return this)
#[error("Bytecode length is zero")]
EmptyBytecode,
/// Program bytecode is not a valid WebAssembly component.
#[error("Invalid bytecode")]
InvalidBytecode,
/// Runtime error during execution.
Expand Down Expand Up @@ -63,6 +66,10 @@ impl Runtime {
program: &[u8],
signature_request: &SignatureRequest,
) -> Result<(), RuntimeError> {
if program.len() == 0 {
return Err(RuntimeError::EmptyBytecode);
}

let component = Component::from_binary(&self.engine, program)
.map_err(|_| RuntimeError::InvalidBytecode)?;

Expand Down
13 changes: 13 additions & 0 deletions runtime/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ fn test_barebones_component_fails_with_data_length_less_than_10() {
let res = runtime.evaluate(BAREBONES_COMPONENT_WASM, &signature_request);
assert!(res.is_err());
}

#[test]
fn test_empty_bytecode_fails() {
let mut runtime = Runtime::new();

let signature_request = SignatureRequest {
message: vec![],
auxilary_data: None,
};

let res = runtime.evaluate(&[], &signature_request);
assert_eq!(res.unwrap_err().to_string(), "Bytecode length is zero");
}

0 comments on commit 52c1c84

Please sign in to comment.