Skip to content

Commit 56e7d9e

Browse files
committed
Added assertion that ptr is a fn ptr in build_call. Fixes rust-lang#67
1 parent 4630dd2 commit 56e7d9e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/builder.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,19 @@ impl Builder {
7070
{
7171
let fn_val_ref = match function.into() {
7272
Left(val) => val.as_value_ref(),
73-
Right(val) => val.as_value_ref(),
73+
Right(val) => {
74+
// If using a pointer value, we must validate it's a valid function ptr
75+
let value_ref = val.as_value_ref();
76+
77+
let is_a_fn_ptr = unsafe {
78+
LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(value_ref))) == LLVMTypeKind::LLVMFunctionTypeKind
79+
};
80+
81+
// REVIEW: We should probably turn this into a Result?
82+
assert!(is_a_fn_ptr);
83+
84+
value_ref
85+
},
7486
};
7587

7688
// LLVM gets upset when void return calls are named because they don't return anything

tests/all/test_values.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1176,3 +1176,23 @@ fn test_function_value_to_global_to_pointer() {
11761176
assert_eq!(*fn_ptr_value.get_name(), *CString::new("my_func").unwrap());
11771177
assert!(module.verify().is_ok());
11781178
}
1179+
1180+
#[test]
1181+
#[should_panic]
1182+
fn test_non_fn_ptr_called() {
1183+
let context = Context::create();
1184+
let builder = context.create_builder();
1185+
let module = context.create_module("my_mod");
1186+
let void_type = context.void_type();
1187+
let void_ptr_type = void_type.ptr_type(AddressSpace::Generic);
1188+
let fn_type = void_type.fn_type(&[void_ptr_type.into()], false);
1189+
let fn_value = module.add_function("my_func", fn_type, None);
1190+
let bb = fn_value.append_basic_block("entry");
1191+
let void_ptr_param = fn_value .get_first_param().unwrap().into_pointer_value();
1192+
1193+
builder.position_at_end(&bb);
1194+
builder.build_call(void_ptr_param, &[], "call");
1195+
builder.build_return(None);
1196+
1197+
assert!(module.verify().is_ok());
1198+
}

0 commit comments

Comments
 (0)