Check signature compatibility in declare_function (fixes #427)

This commit is contained in:
bjorn3
2018-07-29 17:58:53 +02:00
committed by Dan Gohman
parent a8ded3a6f1
commit 5389b7784e
2 changed files with 28 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
extern crate cranelift_codegen;
extern crate cranelift_module;
extern crate cranelift_simplejit;
use cranelift_codegen::settings::*;
use cranelift_codegen::ir::*;
use cranelift_module::*;
use cranelift_simplejit::*;
#[test]
fn error_on_incompatible_sig_in_declare_function() {
let mut module: Module<SimpleJITBackend> = Module::new(SimpleJITBuilder::new());
let mut sig = Signature {
params: vec![AbiParam::new(types::I64)],
returns: vec![],
call_conv: CallConv::SystemV,
argument_bytes: None,
};
module.declare_function("abc", Linkage::Local, &sig).unwrap();
sig.params[0] = AbiParam::new(types::I32);
module.declare_function("abc", Linkage::Local, &sig).err().unwrap(); // Make sure this is an error
}

View File

@@ -159,8 +159,12 @@ impl<B> ModuleFunction<B>
where where
B: Backend, B: Backend,
{ {
fn merge(&mut self, linkage: Linkage) { fn merge(&mut self, linkage: Linkage, sig: &ir::Signature) -> Result<(), ModuleError> {
self.decl.linkage = Linkage::merge(self.decl.linkage, linkage); self.decl.linkage = Linkage::merge(self.decl.linkage, linkage);
if &self.decl.signature != sig {
return Err(ModuleError::IncompatibleDeclaration(self.decl.name.clone()));
}
Ok(())
} }
} }
@@ -357,7 +361,7 @@ where
Occupied(entry) => match *entry.get() { Occupied(entry) => match *entry.get() {
FuncOrDataId::Func(id) => { FuncOrDataId::Func(id) => {
let existing = &mut self.contents.functions[id]; let existing = &mut self.contents.functions[id];
existing.merge(linkage); existing.merge(linkage, signature)?;
self.backend.declare_function(name, existing.decl.linkage); self.backend.declare_function(name, existing.decl.linkage);
Ok(id) Ok(id)
} }