fiffi 0.1.0

Rust bindings for libffi, alternative to libffi-rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
//! Functionality to call external FFI functions when the function signature is not necessarily
//! known at compile time.
//!
//! # Example
//!
//! ```
//! use fiffi::function::{Function, arg, ret};
//! use fiffi::types::Type;
//!
//! extern "C" fn add(a: i32, b: i32) -> i32 {
//!     a + b
//! }
//!
//! let function = Function::new(
//!     fiffi::fn_ptrize!(add),
//!     &[Type::I32, Type::I32],
//!     Some(&Type::I32),
//! );
//!
//! // SAFETY: `function` was built from `add` which has the function signature
//! // `extern "C" fn(i32, i32) -> i32`.
//! let mut return_value = 0i32;
//! unsafe {
//!     function.call([arg(&1), arg(&2)], ret(&mut return_value));
//! }
//!
//! assert_eq!(return_value, 3);
//! ```

extern crate alloc;

use alloc::vec::Vec;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::ptr::{self, null_mut};

use libffi_sys::ffi_call;

#[cfg(msan)]
use crate::__msan_unpoison;
use crate::FnPtr;
use crate::abi::Abi;
use crate::function::raw::Cif;
use crate::return_buffer::{ReturnBuffer, ffi_type_requires_return_buffer};
use crate::types::{FfiTypeLayout, Type, VariadicType};

pub(crate) mod raw;

/// Reference to an argument to pass to [`Function::call`].
///
/// Note that while `Arg` ensures that arguments are alive when they are passed, `Arg` does not
/// perform any verification to ensure that the argument is the correct type. It is up to the caller
/// to ensure that valid arguments are passed to functions.
///
/// # Example
///
/// ```
/// use fiffi::function::Arg;
///
/// let value = 123i32;
/// let arg = Arg::new(&value);
///
/// // `Arg` may also be passed a reference to a slice of memory, but it must be large enough and
/// // properly aligned. This is not checked by fiffi or libffi.
/// let arg_buffer = [0u8; 4];
/// let arg = Arg::new(&arg_buffer);
/// ```
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct Arg<'arg>(*mut c_void, PhantomData<&'arg ()>);

impl<'arg> Arg<'arg> {
    /// Creates an `Arg` from a reference.
    ///
    /// It is up to the caller to ensure that the reference points to valid data with proper size
    /// and alignment.
    pub fn new<T>(arg_ref: &'arg T) -> Self
    where
        T: ?Sized,
    {
        let arg_ptr = ptr::from_ref(arg_ref)
            .cast::<c_void>()
            // libffi-sys expects a `*mut c_void`, so we must cast to a `mut` pointer.
            .cast_mut();

        Self(arg_ptr, PhantomData)
    }

    fn into_ptr(self) -> *mut c_void {
        self.0
    }
}

/// Creates an [`Arg`] from a reference.
///
/// This function is an alias for [`Arg::new`].
pub fn arg<T>(arg_ref: &'_ T) -> Arg<'_>
where
    T: ?Sized,
{
    Arg::new(arg_ref)
}

/// Mutable reference to where the result of [`Function::call`] should be stored.
///
/// For functions that do not return any value, [`Ret::void`] can be used to avoid having to provide
/// a mutable reference. Note that [`Ret::void`] must only be used with functions that do not return
/// a value. `Ret` does not perform any validation to ensure that it is valid to write the return
/// value to the provided mutable reference.
///
/// # Example
///
/// ```
/// use std::mem::MaybeUninit;
///
/// use fiffi::function::Ret;
///
/// let mut value = MaybeUninit::<i32>::uninit();
/// let ret = Ret::new(&mut value);
/// // After a `Function` has written to `ret` the return value can be read by calling
/// // `value.assume_init()`.
///
/// // `Ret` may also be passed a mutable reference to a slice of memory, but it must be large
/// // enough and properly aligned. This is not checked by fiffi or libffi.
/// let mut ret_buffer = [0u8; 4];
/// let ret = Ret::new(&mut ret_buffer);
/// ```
#[derive(Debug)]
#[repr(transparent)]
pub struct Ret<'ret>(*mut c_void, PhantomData<&'ret mut ()>);

impl<'ret> Ret<'ret> {
    /// Creates a `Ret` from a mutable reference.
    ///
    /// It is up to the caller to ensure that it is valid to store the result at the referenced
    /// location.
    pub fn new<T>(ret_ref: &'ret mut T) -> Self
    where
        T: ?Sized,
    {
        let ret_ptr = ptr::from_mut(ret_ref).cast::<c_void>();

        Self(ret_ptr, PhantomData)
    }

    /// Used to create a `Ret` for functions that do not return any value.
    ///
    /// Using a `Ret::void()` with a [`Function`] that returns a value will result in a segmentation
    /// fault as libffi attempts to write the result to a NULL pointer.
    pub fn void() -> Self {
        Self(null_mut(), PhantomData)
    }

    fn into_ptr(self) -> *mut c_void {
        self.0
    }
}

/// Creates a [`Ret`] from a mutable reference.
///
/// This function is an alias for [`Ret::new`].
pub fn ret<T>(ret_ref: &'_ mut T) -> Ret<'_>
where
    T: ?Sized,
{
    Ret::new(ret_ref)
}

/// An callable FFI function.
///
/// `Function` can be used to call FFI functions in cases where the signature is not known at
/// compile-time.
///
/// # Example
///
/// ```
/// use fiffi::function::{Function, arg, ret};
/// use fiffi::types::Type;
///
/// extern "C" fn double(value: i32) -> i32 {
///     value * 2
/// }
///
/// let fn_ptr = fiffi::fn_ptrize!(double);
/// let function = Function::new(fn_ptr, &[Type::I32], Some(&Type::I32));
///
/// let input = 21i32;
/// let mut output = 0i32;
///
/// // SAFETY: The function signature used to construct `function` matches `double`.
/// unsafe {
///     function.call([arg(&input)], ret(&mut output));
/// }
///
/// assert_eq!(output, 42);
/// ```
#[derive(Clone, Debug)]
pub struct Function {
    cif: Cif,
    fn_ptr: FnPtr,
}

impl Function {
    /// Create a `Function` using the target's default ABI.
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    pub fn new<'args, I>(fn_ptr: FnPtr, argument_types: I, return_type: Option<&Type>) -> Self
    where
        I: IntoIterator<Item = &'args Type>,
    {
        Self::with_abi(fn_ptr, argument_types, return_type, Abi::default())
    }

    /// Create a variadic `Function` using the target's default ABI.
    ///
    /// `fixed_argument_types` must describe the fixed parameters, and `variadic_argument_types`
    /// must describe the variadic arguments supplied for a call.
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    ///
    /// Fixed arguments are retained before variadic arguments if the signature is truncated.
    ///
    /// # Example
    ///
    /// ```
    /// use std::ffi::c_char;
    ///
    /// use fiffi::function::{Function, arg, ret};
    /// use fiffi::types::{Type, VariadicType};
    ///
    /// #[cfg_attr(target_env = "msvc", link(name = "legacy_stdio_definitions"))]
    /// unsafe extern "C" {
    ///     pub unsafe fn snprintf(s: *mut c_char, n: usize, format: *const c_char, ...) -> i32;
    /// }
    ///
    /// let function = Function::variadic(
    ///     fiffi::fn_ptrize!(snprintf),
    ///     // Fixed arguments
    ///     //Output buffer  Buffer size  Format string
    ///     &[Type::Pointer, Type::Usize, Type::Pointer],
    ///     // Variadic arguments
    ///     &[VariadicType::I32],
    ///     Some(&Type::I32),
    /// );
    ///
    /// let format = c"Num: %d";
    /// let num = 1337i32;
    /// let expected = c"Num: 1337".to_bytes_with_nul();
    ///
    /// let mut output_buffer = [0u8; 128];
    /// let mut return_value = 0i32;
    ///
    /// // SAFETY: `function` was built with a valid `snprintf` function pointer and the correct
    /// // function signature for `snprintf` with a single `i32` variadic argument.
    /// unsafe {
    ///     function.call(
    ///         [
    ///             arg(&output_buffer.as_mut_ptr()),
    ///             arg(&output_buffer.len()),
    ///             arg(&format),
    ///             arg(&num),
    ///         ],
    ///         ret(&mut return_value),
    ///     );
    /// }
    ///
    /// // `snprintf`'s return value return the written length without the final NULL byte.
    /// assert_eq!(return_value as usize, expected.len() - 1);
    /// assert_eq!(expected, &output_buffer[0..expected.len()]);
    /// ```
    pub fn variadic<'fixed_args, 'variadic_args, I1, I2>(
        fn_ptr: FnPtr,
        fixed_argument_types: I1,
        variadic_argument_types: I2,
        return_type: Option<&Type>,
    ) -> Self
    where
        I1: IntoIterator<Item = &'fixed_args Type>,
        I2: IntoIterator<Item = &'variadic_args VariadicType>,
    {
        Self::variadic_with_abi(
            fn_ptr,
            fixed_argument_types,
            variadic_argument_types,
            return_type,
            Abi::default(),
        )
    }

    /// Creates a `Function` using the provided [`Abi`].
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    pub fn with_abi<'args, I>(
        fn_ptr: FnPtr,
        argument_types: I,
        return_type: Option<&Type>,
        abi: Abi,
    ) -> Self
    where
        I: IntoIterator<Item = &'args Type>,
    {
        let cif = Cif::new(abi, argument_types, return_type);

        Self { cif, fn_ptr }
    }

    /// Creates a variadic `Function` using the provided [`Abi`].
    ///
    /// `fixed_argument_types` must describe the fixed parameters, and `variadic_argument_types`
    /// must describe the variadic arguments supplied for a call.
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    ///
    /// Fixed arguments are retained before variadic arguments if the signature is truncated.
    pub fn variadic_with_abi<'fixed_args, 'variadic_args, I1, I2>(
        fn_ptr: FnPtr,
        fixed_argument_types: I1,
        variadic_argument_types: I2,
        return_type: Option<&Type>,
        abi: Abi,
    ) -> Self
    where
        I1: IntoIterator<Item = &'fixed_args Type>,
        I2: IntoIterator<Item = &'variadic_args VariadicType>,
    {
        let cif = Cif::variadic(
            abi,
            fixed_argument_types,
            variadic_argument_types,
            return_type,
        );

        Self { cif, fn_ptr }
    }

    /// Create a [`FunctionBuilder`] used to build a [`Function`].
    ///
    /// # Example
    ///
    /// ```
    /// use fiffi::function::Function;
    /// use fiffi::types::Type;
    ///
    /// extern "C" fn add(a: i32, b: i64) -> i64 {
    ///     i64::from(a) + b
    /// }
    ///
    /// let function = Function::builder()
    ///     .arg(Type::I32)
    ///     .arg(Type::I64)
    ///     .ret(Some(Type::I64))
    ///     .fn_ptr(fiffi::fn_ptrize!(add))
    ///     .build();
    /// ```
    pub fn builder() -> FunctionBuilder<FnPtrNotSet> {
        FunctionBuilder {
            fn_ptr: FnPtrNotSet,
            argument_types: Vec::new(),
            return_type: None,
            abi: Abi::default(),
        }
    }

    /// Create a [`VariadicFunctionBuilder`] used to build a variadic [`Function`].
    pub fn variadic_builder() -> VariadicFunctionBuilder<FnPtrNotSet> {
        VariadicFunctionBuilder {
            fn_ptr: FnPtrNotSet,
            fixed_argument_types: Vec::new(),
            variadic_argument_types: Vec::new(),
            return_type: None,
            abi: Abi::default(),
        }
    }

    /// Calls the wrapped function pointer through libffi.
    ///
    /// # Safety
    ///
    /// * The wrapped [`FnPtr`] must be valid to call with this function's ABI and signature.
    /// * Every [`Arg`] in `args` must point to an initialized value matching the corresponding
    ///   [`Type`] the function expects and remain alive for the duration of the call.
    /// * All arguments expected by the called function must be provided.
    /// * `ret` must be valid to write the return type to, unless the function was created with no
    ///   return type.
    /// * Calling the target function must not violate Rust aliasing rules for any referenced
    ///   memory.
    ///
    /// If more than `c_uint::MAX` argument types were provided during construction, libffi will use
    /// the truncated signature and may not read every argument pointer provided in `args`.
    ///
    /// # Example
    ///
    /// ```
    /// use fiffi::function::{Function, arg, ret};
    /// use fiffi::types::Type;
    ///
    /// extern "C" fn add_one(value: i32) -> i32 {
    ///     value + 1
    /// }
    ///
    /// let function = Function::new(fiffi::fn_ptrize!(add_one), &[Type::I32], Some(&Type::I32));
    /// let input = 41i32;
    /// let mut output = 0i32;
    ///
    /// // SAFETY: The function pointer, argument type, return type, and storage match `add_one`.
    /// unsafe {
    ///     function.call([arg(&input)], ret(&mut output));
    /// }
    ///
    /// assert_eq!(output, 42);
    /// ```
    pub unsafe fn call<'arg, I>(&self, args: I, ret: Ret)
    where
        I: IntoIterator<Item = Arg<'arg>>,
    {
        // libffi may modify the pointers in `avalue` array, so we mark it as `mut`.
        let mut args: Vec<*mut c_void> = args.into_iter().map(Arg::into_ptr).collect();
        let args_ptr = args.as_mut_ptr();

        let fn_ptr = self.fn_ptr.as_libffi_sys_ptr();

        // SAFETY: `rtype` is a pointer to an initialized `ffi_type` that will not change while
        // `self` is alive.
        let return_type = unsafe { &*(*self.cif.as_ffi_cif_ptr()).rtype };
        let ret_ptr = ret.into_ptr();

        if ffi_type_requires_return_buffer(return_type) {
            // For integers smaller than a register, libffi still writes a full register to
            // `rvalue`.
            let mut return_buffer = ReturnBuffer::new();

            // SAFETY: It is up to the caller to ensure that it is safe to call the function. See
            // `call`'s safety section for more details.
            unsafe {
                ffi_call(
                    self.cif.as_ffi_cif_ptr(),
                    fn_ptr,
                    return_buffer.as_mut_ptr(),
                    args_ptr,
                );

                #[cfg(msan)]
                __msan_unpoison(return_buffer.as_mut_ptr(), size_of::<ReturnBuffer>());
            }

            // SAFETY: `ffi_call` initialized `return_buffer`, and `call`'s safety contract requires
            // `ret_ptr` to be valid writable storage for this function's return type.
            unsafe {
                return_buffer.write_result(ret_ptr, return_type.size);
            }
        } else {
            // For sufficiently large integers, floats, structs and void return values, we can
            // simply pass the pointer in `ret` to `ffi_call`.
            //
            // SAFETY: It is up to the caller to ensure that it is safe to call the function. See
            // `call`'s safety section for more details.
            unsafe {
                ffi_call(self.cif.as_ffi_cif_ptr(), fn_ptr, ret_ptr, args_ptr);

                #[cfg(msan)]
                __msan_unpoison(ret_ptr, return_type.size);
            }
        }
    }

    /// Returns the memory layout of this `Function`'s arguments.
    ///
    /// # Example
    ///
    /// ```
    /// use fiffi::function::Function;
    /// use fiffi::types::Type;
    ///
    /// extern "C" fn identity(value: i32) -> i32 {
    ///     value
    /// }
    ///
    /// let function = Function::new(fiffi::fn_ptrize!(identity), &[Type::I32], Some(&Type::I32));
    ///
    /// assert_eq!(function.argument_layouts(), vec![Type::I32.layout()]);
    /// ```
    pub fn argument_layouts(&self) -> Vec<FfiTypeLayout> {
        self.cif.argument_layouts()
    }

    /// Returns the memory layout of this `Function`'s return value.
    ///
    /// # Example
    ///
    /// ```
    /// use fiffi::function::Function;
    /// use fiffi::types::Type;
    ///
    /// extern "C" fn identity(value: i32) -> i32 {
    ///     value
    /// }
    ///
    /// let function = Function::new(fiffi::fn_ptrize!(identity), &[Type::I32], Some(&Type::I32));
    ///
    /// assert_eq!(function.return_layout(), Type::I32.layout());
    /// ```
    pub fn return_layout(&self) -> FfiTypeLayout {
        self.cif.return_layout()
    }
}

// SAFETY: `Function` itself is safe to be sent to a different thread, although it should be noted
// that it might not be safe to call the provided function pointer from another thread.
unsafe impl Send for Function {}

// SAFETY: `Function` itself is safe to be sent to a different thread, although it should be noted
// that it might not be safe to call the provided function pointer from another thread.
unsafe impl Sync for Function {}

/// Builder state used before a function pointer has been set.
///
/// If a `FnPtr` has not been set, the [`Function`] cannot be created.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FnPtrNotSet;

/// Builder state used after a function pointer has been set.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct FnPtrSet(FnPtr);

/// Builder for a non-variadic [`Function`].
///
/// Argument types are appended in call order. [`Function`]s created using `FunctionBuilder` use the
/// default [`Abi`] unless another ABI is explicitly set.
///
/// # Example
///
/// ```
/// use fiffi::function::Function;
/// use fiffi::types::Type;
///
/// extern "C" fn identity(a: i32) -> i32 {
///     a
/// }
///
/// let function = Function::builder()
///     .arg(Type::I32)
///     .ret(Some(Type::I32))
///     .fn_ptr(fiffi::fn_ptrize!(identity))
///     .build();
/// ```
#[derive(Clone, Debug)]
pub struct FunctionBuilder<State> {
    fn_ptr: State,
    argument_types: Vec<Type>,
    return_type: Option<Type>,
    abi: Abi,
}

impl<State> FunctionBuilder<State> {
    /// Set the function pointer.
    #[must_use]
    pub fn fn_ptr(self, fn_ptr: FnPtr) -> FunctionBuilder<FnPtrSet> {
        FunctionBuilder {
            fn_ptr: FnPtrSet(fn_ptr),
            argument_types: self.argument_types,
            return_type: self.return_type,
            abi: self.abi,
        }
    }

    /// Set the function's ABI.
    #[must_use]
    pub fn abi(mut self, abi: Abi) -> Self {
        self.abi = abi;
        self
    }

    /// Set the function's return type.
    #[must_use]
    pub fn ret(mut self, return_type: Option<Type>) -> Self {
        self.return_type = return_type;
        self
    }

    /// Add a single argument to the function signature.
    #[must_use]
    pub fn arg(mut self, ty: Type) -> Self {
        self.argument_types.push(ty);
        self
    }

    /// Add multiple arguments to the function signature.
    #[must_use]
    pub fn args<I>(mut self, types: I) -> Self
    where
        I: IntoIterator<Item = Type>,
    {
        self.argument_types.extend(types);
        self
    }
}

impl FunctionBuilder<FnPtrSet> {
    /// Build the [`Function`].
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    pub fn build(self) -> Function {
        Function::with_abi(
            self.fn_ptr.0,
            &self.argument_types,
            self.return_type.as_ref(),
            self.abi,
        )
    }
}

/// Builder for a variadic [`Function`].
///
/// [`Function`]s created using `VariadicFunctionBuilder` use the default [`Abi`] unless another ABI
/// is explicitly set.
///
/// Fixed and variadic argument types are appended in call order within their respective groups.
/// All fixed arguments are always provided before all variadic arguments when calling the function.
///
/// If the signature is truncated, fixed arguments are retained before variadic arguments.
///
/// # Example
///
/// ```
/// use std::ffi::c_char;
///
/// use fiffi::function::Function;
/// use fiffi::types::{Type, VariadicType};
///
/// #[cfg_attr(target_env = "msvc", link(name = "legacy_stdio_definitions"))]
/// unsafe extern "C" {
///     pub unsafe fn snprintf(s: *mut c_char, n: usize, format: *const c_char, ...) -> i32;
/// }
///
/// // Prepare to call `snprintf` with a single `i32` variadic argument.
/// let function = Function::variadic_builder()
///     .fixed_arg(Type::Pointer)
///     .fixed_arg(Type::Usize)
///     .fixed_arg(Type::Pointer)
///     .variadic_arg(VariadicType::I32)
///     .ret(Some(Type::I32))
///     .fn_ptr(fiffi::fn_ptrize!(snprintf))
///     .build();
/// ```
#[derive(Clone, Debug)]
pub struct VariadicFunctionBuilder<State> {
    fn_ptr: State,
    fixed_argument_types: Vec<Type>,
    variadic_argument_types: Vec<VariadicType>,
    return_type: Option<Type>,
    abi: Abi,
}

impl<State> VariadicFunctionBuilder<State> {
    /// Set the function pointer.
    #[must_use]
    pub fn fn_ptr(self, fn_ptr: FnPtr) -> VariadicFunctionBuilder<FnPtrSet> {
        VariadicFunctionBuilder {
            fn_ptr: FnPtrSet(fn_ptr),
            fixed_argument_types: self.fixed_argument_types,
            variadic_argument_types: self.variadic_argument_types,
            return_type: self.return_type,
            abi: self.abi,
        }
    }

    /// Set the function's ABI.
    #[must_use]
    pub fn abi(mut self, abi: Abi) -> Self {
        self.abi = abi;
        self
    }

    /// Set the function's return type.
    #[must_use]
    pub fn ret(mut self, return_type: Option<Type>) -> Self {
        self.return_type = return_type;
        self
    }

    /// Add a single fixed argument to the function signature.
    #[must_use]
    pub fn fixed_arg(mut self, ty: Type) -> Self {
        self.fixed_argument_types.push(ty);
        self
    }

    /// Add multiple fixed arguments to the function signature.
    #[must_use]
    pub fn fixed_args<I>(mut self, types: I) -> Self
    where
        I: IntoIterator<Item = Type>,
    {
        self.fixed_argument_types.extend(types);
        self
    }

    /// Add a single variadic argument to the function signature.
    #[must_use]
    pub fn variadic_arg(mut self, ty: VariadicType) -> Self {
        self.variadic_argument_types.push(ty);
        self
    }

    /// Add multiple variadic arguments to the function signature.
    #[must_use]
    pub fn variadic_args<I>(mut self, types: I) -> Self
    where
        I: IntoIterator<Item = VariadicType>,
    {
        self.variadic_argument_types.extend(types);
        self
    }
}

impl VariadicFunctionBuilder<FnPtrSet> {
    /// Build the variadic [`Function`].
    ///
    /// # Warning
    ///
    /// libffi stores the number of arguments in a C `unsigned int`. If more than `c_uint::MAX`
    /// argument types are provided, only the first `c_uint::MAX` are retained in the prepared
    /// function signature.
    pub fn build(self) -> Function {
        Function::variadic_with_abi(
            self.fn_ptr.0,
            &self.fixed_argument_types,
            &self.variadic_argument_types,
            self.return_type.as_ref(),
            self.abi,
        )
    }
}

#[cfg(test)]
mod tests {
    use core::cell::UnsafeCell;
    use core::ffi::CStr;
    use core::mem::MaybeUninit;

    use test_callbacks::*;

    use super::*;
    use crate::fn_ptrize;
    use crate::function::raw::CifKind;
    use crate::test_utils::{
        F32_ARG, F64_ARG, I8_ARG, I16_ARG, I32_ARG, I64_ARG, ISIZE_ARG, PTR_ARG, SNPRINTF_ARG_1,
        SNPRINTF_ARG_2, SNPRINTF_ARG_3, SNPRINTF_ARG_4, SNPRINTF_ARG_5, SNPRINTF_ARG_6,
        SNPRINTF_EXPECTED_OUTPUT, SNPRINTF_EXPECTED_RETURN_VALUE, SNPRINTF_FORMAT, STRUCT_ARG,
        TestStruct, U8_ARG, U16_ARG, U32_ARG, U64_ARG, USIZE_ARG, snprintf,
    };
    use crate::types::FfiType;

    macro_rules! test_identity_function {
        ($ty:ty, $identity_fn:ident, $val:expr) => {{
            let ffi_type = <$ty as FfiType>::ffi_type();
            let function = Function::new(
                fn_ptrize!($identity_fn),
                core::slice::from_ref(&ffi_type),
                Some(&ffi_type),
            );

            let mut return_buffer = MaybeUninit::<$ty>::uninit();

            let fn_args = [arg(&$val)];
            let fn_ret = ret(&mut return_buffer);

            // SAFETY: The `function` was built with a valid function pointer and matching
            // signature. `return_buffer` is initialized by the call before it is read.
            let return_value = unsafe {
                function.call(fn_args, fn_ret);
                return_buffer.assume_init()
            };

            assert_eq!(
                return_value,
                $val,
                "Unexpected return while calling identity function {}.",
                stringify!($identity_fn)
            );
        }};
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn call_void_fn() {
        let function = Function::new(fn_ptrize!(void_fn), &[], None);

        // SAFETY: The `function` was built with a valid function pointer and matching signature.
        unsafe {
            function.call([], Ret::void());
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_identity_functions() {
        test_identity_function!(i8, i8_identity, I8_ARG);
        test_identity_function!(i16, i16_identity, I16_ARG);
        test_identity_function!(i32, i32_identity, I32_ARG);
        test_identity_function!(i64, i64_identity, I64_ARG);
        test_identity_function!(isize, isize_identity, ISIZE_ARG);
        test_identity_function!(u8, u8_identity, U8_ARG);
        test_identity_function!(u16, u16_identity, U16_ARG);
        test_identity_function!(u32, u32_identity, U32_ARG);
        test_identity_function!(u64, u64_identity, U64_ARG);
        test_identity_function!(usize, usize_identity, USIZE_ARG);
        test_identity_function!(f32, f32_identity, F32_ARG);
        test_identity_function!(f64, f64_identity, F64_ARG);
        test_identity_function!(*const c_void, ptr_identity, PTR_ARG.0);
        test_identity_function!(TestStruct, test_struct_identity, STRUCT_ARG);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_function_does_not_modify_args() {
        let i8_arg = UnsafeCell::new(I8_ARG);
        let i16_arg = UnsafeCell::new(I16_ARG);
        let i32_arg = UnsafeCell::new(I32_ARG);
        let i64_arg = UnsafeCell::new(I64_ARG);
        let isize_arg = UnsafeCell::new(ISIZE_ARG);
        let u8_arg = UnsafeCell::new(U8_ARG);
        let u16_arg = UnsafeCell::new(U16_ARG);
        let u32_arg = UnsafeCell::new(U32_ARG);
        let u64_arg = UnsafeCell::new(U64_ARG);
        let usize_arg = UnsafeCell::new(USIZE_ARG);
        let f32_arg = UnsafeCell::new(F32_ARG);
        let f64_arg = UnsafeCell::new(F64_ARG);
        let struct_arg = UnsafeCell::new(STRUCT_ARG);
        let ptr_arg = UnsafeCell::new(PTR_ARG);

        #[rustfmt::skip]
        let function = Function::new(
            fn_ptrize!(modifying_fn),
            &[
                Type::I8, Type::I16, Type::I32, Type::I64, Type::Isize, Type::U8, Type::U16,
                Type::U32, Type::U64, Type::Usize, Type::F32, Type::F64,
                <TestStruct as FfiType>::ffi_type(), Type::Pointer,
            ],
            None,
        );

        #[rustfmt::skip]
        let arg_array = [
            arg(&i8_arg), arg(&i16_arg), arg(&i32_arg), arg(&i64_arg), arg(&isize_arg),
            arg(&u8_arg), arg(&u16_arg), arg(&u32_arg), arg(&u64_arg), arg(&usize_arg),
            arg(&f32_arg), arg(&f64_arg), arg(&struct_arg), arg(&ptr_arg),
        ];

        // SAFETY: The `function` was built with a valid function pointer and matching signature.
        unsafe {
            function.call(arg_array, Ret::void());
        }

        assert_eq!(i8_arg.into_inner(), I8_ARG);
        assert_eq!(i16_arg.into_inner(), I16_ARG);
        assert_eq!(i32_arg.into_inner(), I32_ARG);
        assert_eq!(i64_arg.into_inner(), I64_ARG);
        assert_eq!(isize_arg.into_inner(), ISIZE_ARG);
        assert_eq!(u8_arg.into_inner(), U8_ARG);
        assert_eq!(u16_arg.into_inner(), U16_ARG);
        assert_eq!(u32_arg.into_inner(), U32_ARG);
        assert_eq!(u64_arg.into_inner(), U64_ARG);
        assert_eq!(usize_arg.into_inner(), USIZE_ARG);
        assert_eq!(f32_arg.into_inner(), F32_ARG);
        assert_eq!(f64_arg.into_inner(), F64_ARG);
        assert_eq!(struct_arg.into_inner(), STRUCT_ARG);
        assert_eq!(ptr_arg.into_inner(), PTR_ARG);
    }

    #[test]
    fn test_type_layout_fns() {
        #[rustfmt::skip]
        let modifying_fn_arg_types = [
            Type::I8, Type::I16, Type::I32, Type::I64, Type::Isize, Type::U8, Type::U16, Type::U32,
            Type::U64, Type::Usize, Type::F32, Type::F64, <TestStruct as FfiType>::ffi_type(),
            Type::Pointer,
        ];

        let expected_layouts: Vec<FfiTypeLayout> =
            modifying_fn_arg_types.iter().map(Type::layout).collect();

        let function = Function::new(fn_ptrize!(modifying_fn), &modifying_fn_arg_types, None);

        assert_eq!(expected_layouts, function.argument_layouts());

        for ty in &modifying_fn_arg_types {
            // Note that this `function` points to a function with a different signature, so it
            // should never be `call`ed.
            let function = Function::new(fn_ptrize!(void_fn), core::slice::from_ref(ty), Some(ty));

            assert_eq!(function.argument_layouts(), vec![ty.layout()],);
            assert_eq!(function.return_layout(), ty.layout());
        }
    }

    #[test]
    fn function_new_with_abi_works_with_all_abis() {
        for abi in Abi::ABIS {
            Function::with_abi(fn_ptrize!(void_fn), &[], None, abi);
        }
    }

    #[test]
    fn variadic_function_supports_all_variadic_types() {
        for abi in Abi::ABIS {
            // Do not call `void_fn` prepared like this.
            let variadic_fn = Function::variadic_with_abi(
                fn_ptrize!(void_fn),
                &[Type::Pointer],
                &[
                    VariadicType::I32,
                    VariadicType::U32,
                    VariadicType::I64,
                    VariadicType::U64,
                    VariadicType::Isize,
                    VariadicType::Usize,
                    VariadicType::F64,
                    VariadicType::Pointer,
                    VariadicType::create_struct(vec![Type::I8]).unwrap(),
                ],
                None,
                abi,
            );

            assert!(matches!(variadic_fn.cif.kind, CifKind::Variadic { .. }));
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_variadic_snprintf() {
        let snprintf_function = Function::variadic(
            fn_ptrize!(snprintf),
            &[Type::Pointer, Type::Usize, Type::Pointer],
            &[
                VariadicType::I32,
                VariadicType::U32,
                VariadicType::I64,
                VariadicType::U64,
                VariadicType::Pointer,
                VariadicType::F64,
            ],
            Some(&Type::I32),
        );

        let mut buffer = [0u8; 128];
        let buffer_ptr = buffer.as_mut_ptr();
        let buffer_size = buffer.len();
        let mut return_value = 0i32;

        // SAFETY: `snprintf_function` was built with a valid `snprintf` function pointer and
        // matching fixed/variadic signatures expected by the call site.
        unsafe {
            snprintf_function.call(
                [
                    arg(&buffer_ptr),
                    arg(&buffer_size),
                    arg(&SNPRINTF_FORMAT),
                    arg(&SNPRINTF_ARG_1),
                    arg(&SNPRINTF_ARG_2),
                    arg(&SNPRINTF_ARG_3),
                    arg(&SNPRINTF_ARG_4),
                    arg(&SNPRINTF_ARG_5),
                    arg(&SNPRINTF_ARG_6),
                ],
                ret(&mut return_value),
            );
        }

        let output_str = CStr::from_bytes_until_nul(&buffer).unwrap();

        assert_eq!(
            return_value, SNPRINTF_EXPECTED_RETURN_VALUE,
            "`snprintf` did not write the expected number of bytes."
        );

        assert_eq!(
            output_str, SNPRINTF_EXPECTED_OUTPUT,
            "Output from `snprintf` was not as expected."
        );
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn builder_builds_and_calls_void() {
        let function = Function::builder().fn_ptr(fn_ptrize!(void_fn)).build();

        // SAFETY: The `function` was built with a valid function pointer and matching signature.
        unsafe {
            function.call([], Ret::void());
        }
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn builder_with_args_and_multiple_fn_ptr_calls() {
        let builder = Function::builder()
            .arg(Type::I32)
            .ret(Some(Type::I32))
            .fn_ptr(fn_ptrize!(f64_identity))
            .fn_ptr(fn_ptrize!(i32_identity));

        let function = builder.build();

        let mut return_buffer = MaybeUninit::<i32>::uninit();
        let arg_val: i32 = I32_ARG;

        // SAFETY: `function` was built with the correct signature and a valid function pointer.
        unsafe {
            function.call([arg(&arg_val)], ret(&mut return_buffer));
        }

        // SAFETY: `return_buffer` was initialized by the call above.
        let return_value = unsafe { return_buffer.assume_init() };

        assert_eq!(return_value, arg_val);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn variadic_builder_snprintf_works() {
        let snprintf_function = Function::variadic_builder()
            .fixed_args(vec![Type::Pointer, Type::Usize, Type::Pointer])
            .variadic_args(vec![
                VariadicType::I32,
                VariadicType::U32,
                VariadicType::I64,
                VariadicType::U64,
                VariadicType::Pointer,
                VariadicType::F64,
            ])
            .ret(Some(Type::I32))
            .fn_ptr(fn_ptrize!(snprintf))
            .build();

        let mut buffer = [0u8; 128];
        let buffer_ptr = buffer.as_mut_ptr();
        let buffer_size = buffer.len();
        let mut return_value = 0i32;

        // SAFETY: `snprintf_function` was built with a valid `snprintf` function pointer and
        // matching fixed/variadic signatures expected by the snprintf with the given format string.
        unsafe {
            snprintf_function.call(
                [
                    arg(&buffer_ptr),
                    arg(&buffer_size),
                    arg(&SNPRINTF_FORMAT),
                    arg(&SNPRINTF_ARG_1),
                    arg(&SNPRINTF_ARG_2),
                    arg(&SNPRINTF_ARG_3),
                    arg(&SNPRINTF_ARG_4),
                    arg(&SNPRINTF_ARG_5),
                    arg(&SNPRINTF_ARG_6),
                ],
                ret(&mut return_value),
            );
        }

        let output_str = CStr::from_bytes_until_nul(&buffer).unwrap();

        assert_eq!(
            return_value, SNPRINTF_EXPECTED_RETURN_VALUE,
            "`snprintf` did not write the expected number of bytes."
        );

        assert_eq!(
            output_str, SNPRINTF_EXPECTED_OUTPUT,
            "Output from `snprintf` was not as expected."
        );
    }
}

#[cfg(test)]
#[rustfmt::skip]
pub(crate) mod test_callbacks {
    use core::ffi::c_void;
    use core::hint::black_box;

    use crate::test_utils::TestStruct;

    pub extern "C" fn void_fn() {}

    pub extern "C" fn i8_identity(arg: i8) -> i8 { arg }
    pub extern "C" fn i16_identity(arg: i16) -> i16 { arg }
    pub extern "C" fn i32_identity(arg: i32) -> i32 { arg }
    pub extern "C" fn i64_identity(arg: i64) -> i64 { arg }
    pub extern "C" fn isize_identity(arg: isize) -> isize { arg }
    pub extern "C" fn u8_identity(arg: u8) -> u8 { arg }
    pub extern "C" fn u16_identity(arg: u16) -> u16 { arg }
    pub extern "C" fn u32_identity(arg: u32) -> u32 { arg }
    pub extern "C" fn u64_identity(arg: u64) -> u64 { arg }
    pub extern "C" fn usize_identity(arg: usize) -> usize { arg }
    pub extern "C" fn f32_identity(arg: f32) -> f32 { arg }
    pub extern "C" fn f64_identity(arg: f64) -> f64 { arg }
    pub extern "C" fn test_struct_identity(arg: TestStruct) -> TestStruct { arg }
    pub extern "C" fn ptr_identity(arg: *const c_void) -> *const c_void { arg }

    pub extern "C" fn modifying_fn(
        mut i8_arg: i8, mut i16_arg: i16, mut i32_arg: i32, mut i64_arg: i64, mut isize_arg: isize,
        mut u8_arg: u8, mut u16_arg: u16, mut u32_arg: u32, mut u64_arg: u64, mut usize_arg: usize,
        mut f32_arg: f32, mut f64_arg: f64, mut struct_arg: TestStruct, mut ptr_arg: *const c_void,
    ) {
        i8_arg += 1; i16_arg += 1; i32_arg += 1; i64_arg += 1; isize_arg += 1; u8_arg += 1;
        u16_arg += 1; u32_arg += 1; u64_arg += 1; usize_arg += 1; f32_arg += 1.; f64_arg += 1.0;
        struct_arg.0 += 1; struct_arg.1 += 1; struct_arg.2 += 1; struct_arg.3 += 1;

        // SAFETY: `ptr_arg` will not overflow by adding 1.
        ptr_arg = unsafe { ptr_arg.byte_add(1) };

        // Silence lint and possibly avoid Rust optimizing away potential bugs?
        black_box((
            i8_arg, i16_arg, i32_arg, i64_arg, isize_arg, u8_arg, u16_arg, u32_arg, u64_arg,
            usize_arg, f32_arg, f64_arg, struct_arg, ptr_arg
        ));
    }
}