File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -70,7 +70,19 @@ impl Builder {
70
70
{
71
71
let fn_val_ref = match function. into ( ) {
72
72
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
+ } ,
74
86
} ;
75
87
76
88
// LLVM gets upset when void return calls are named because they don't return anything
Original file line number Diff line number Diff line change @@ -1176,3 +1176,23 @@ fn test_function_value_to_global_to_pointer() {
1176
1176
assert_eq ! ( * fn_ptr_value. get_name( ) , * CString :: new( "my_func" ) . unwrap( ) ) ;
1177
1177
assert ! ( module. verify( ) . is_ok( ) ) ;
1178
1178
}
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
+ }
You can’t perform that action at this time.
0 commit comments