Skip to content

Commit

Permalink
Check for types::INVALID in the function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
walac committed Sep 18, 2019
1 parent ddd4790 commit fda7487
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion cranelift-codegen/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1837,12 +1837,41 @@ impl<'a> Verifier<'a> {
Ok(())
}

fn verify_function_invalid_types(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
self.func
.signature
.params
.iter()
.filter(|&param| param.value_type == types::INVALID)
.for_each(|_| report!(errors, AnyEntity::Function, "Parameter has an invalid type"));

self.func
.signature
.returns
.iter()
.filter(|&ret| ret.value_type == types::INVALID)
.for_each(|_| {
report!(
errors,
AnyEntity::Function,
"Return value has an invalid type"
)
});

if errors.has_error() {
Err(())
} else {
Ok(())
}
}

pub fn run(&self, errors: &mut VerifierErrors) -> VerifierStepResult<()> {
self.verify_global_values(errors)?;
self.verify_heaps(errors)?;
self.verify_tables(errors)?;
self.verify_jump_tables(errors)?;
self.typecheck_entry_block_params(errors)?;
self.verify_function_invalid_types(errors)?;

for ebb in self.func.layout.ebbs() {
for inst in self.func.layout.ebb_insts(ebb) {
Expand All @@ -1869,7 +1898,7 @@ mod tests {
use super::{Verifier, VerifierError, VerifierErrors};
use crate::entity::EntityList;
use crate::ir::instructions::{InstructionData, Opcode};
use crate::ir::Function;
use crate::ir::{types, AbiParam, Function};
use crate::settings;

macro_rules! assert_err_with_msg {
Expand Down Expand Up @@ -1928,4 +1957,30 @@ mod tests {

assert_err_with_msg!(errors, "instruction format");
}

#[test]
fn test_function_invalid_param() {
let mut func = Function::new();
func.signature.params.push(AbiParam::new(types::INVALID));

let mut errors = VerifierErrors::default();
let flags = &settings::Flags::new(settings::builder());
let verifier = Verifier::new(&func, flags.into());

let _ = verifier.verify_function_invalid_types(&mut errors);
assert_err_with_msg!(errors, "Parameter has an invalid type");
}

#[test]
fn test_function_invalid_return_value() {
let mut func = Function::new();
func.signature.returns.push(AbiParam::new(types::INVALID));

let mut errors = VerifierErrors::default();
let flags = &settings::Flags::new(settings::builder());
let verifier = Verifier::new(&func, flags.into());

let _ = verifier.verify_function_invalid_types(&mut errors);
assert_err_with_msg!(errors, "Return value has an invalid type");
}
}

0 comments on commit fda7487

Please sign in to comment.