Skip to content

Commit

Permalink
Added assertion that ptr is a fn ptr in build_call. Fixes rust-lang#67
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Apr 13, 2019
1 parent 4630dd2 commit 56e7d9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,19 @@ impl Builder {
{
let fn_val_ref = match function.into() {
Left(val) => val.as_value_ref(),
Right(val) => val.as_value_ref(),
Right(val) => {
// If using a pointer value, we must validate it's a valid function ptr
let value_ref = val.as_value_ref();

let is_a_fn_ptr = unsafe {
LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(value_ref))) == LLVMTypeKind::LLVMFunctionTypeKind
};

// REVIEW: We should probably turn this into a Result?
assert!(is_a_fn_ptr);

value_ref
},
};

// LLVM gets upset when void return calls are named because they don't return anything
Expand Down
20 changes: 20 additions & 0 deletions tests/all/test_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,3 +1176,23 @@ fn test_function_value_to_global_to_pointer() {
assert_eq!(*fn_ptr_value.get_name(), *CString::new("my_func").unwrap());
assert!(module.verify().is_ok());
}

#[test]
#[should_panic]
fn test_non_fn_ptr_called() {
let context = Context::create();
let builder = context.create_builder();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let void_ptr_type = void_type.ptr_type(AddressSpace::Generic);
let fn_type = void_type.fn_type(&[void_ptr_type.into()], false);
let fn_value = module.add_function("my_func", fn_type, None);
let bb = fn_value.append_basic_block("entry");
let void_ptr_param = fn_value .get_first_param().unwrap().into_pointer_value();

builder.position_at_end(&bb);
builder.build_call(void_ptr_param, &[], "call");
builder.build_return(None);

assert!(module.verify().is_ok());
}

0 comments on commit 56e7d9e

Please sign in to comment.