@@ -1480,7 +1480,7 @@ mod prim_ref {}
1480
1480
///
1481
1481
/// ### Casting to and from integers
1482
1482
///
1483
- /// You cast function pointers directly to integers:
1483
+ /// You can cast function pointers directly to integers:
1484
1484
///
1485
1485
/// ```rust
1486
1486
/// let fnptr: fn(i32) -> i32 = |x| x+2;
@@ -1506,6 +1506,110 @@ mod prim_ref {}
1506
1506
/// Note that all of this is not portable to platforms where function pointers and data pointers
1507
1507
/// have different sizes.
1508
1508
///
1509
+ /// ### ABI compatibility
1510
+ ///
1511
+ /// Generally, when a function is declared with one signature and called via a function pointer with
1512
+ /// a different signature, the two signatures must be *ABI-compatible* or else calling the function
1513
+ /// via that function pointer is Undefined Behavior. ABI compatibility is a lot stricter than merely
1514
+ /// having the same memory layout; for example, even if `i32` and `f32` have the same size and
1515
+ /// alignment, they might be passed in different registers and hence not be ABI-compatible.
1516
+ ///
1517
+ /// ABI compatibility as a concern only arises in code that alters the type of function pointers,
1518
+ /// and in code that combines `#[target_feature]` with `extern fn`. Altering the type of
1519
+ /// function pointers is wildly unsafe (as in, a lot more unsafe than even
1520
+ /// [`transmute_copy`][mem::transmute_copy]), and should only occur in the most exceptional
1521
+ /// circumstances. `#[target_feature]` is also used rarely. But assuming such circumstances, what
1522
+ /// are the rules?
1523
+ ///
1524
+ /// For two signatures to be considered *ABI-compatible*, they must use a compatible ABI string,
1525
+ /// must take the same number of arguments, the individual argument types and the return types must
1526
+ /// be ABI-compatible, and the target feature requirements must be met (see the subsection below for
1527
+ /// the last point). The ABI string is declared via `extern "ABI" fn(...) -> ...`; note that
1528
+ /// `fn name(...) -> ...` implicitly uses the `"Rust"` ABI string and `extern fn name(...) -> ...`
1529
+ /// implicitly uses the `"C"` ABI string.
1530
+ ///
1531
+ /// The ABI strings are guaranteed to be compatible if they are the same, or if the caller ABI
1532
+ /// string is `$X-unwind` and the callee ABI string is `$X`, where `$X` is one of the following:
1533
+ /// "C", "aapcs", "fastcall", "stdcall", "system", "sysv64", "thiscall", "vectorcall", "win64".
1534
+ ///
1535
+ /// The following types are guaranteed to be ABI-compatible:
1536
+ ///
1537
+ /// - `*const T`, `*mut T`, `&T`, `&mut T`, `Box<T>` (specifically, only `Box<T, Global>`),
1538
+ /// `NonNull<T>` are all ABI-compatible with each other for all `T`. Two of these pointer types
1539
+ /// with different `T` are ABI-compatible if they have the same metadata type (`<T as
1540
+ /// Pointee>::Metadata`).
1541
+ /// - `usize` is ABI-compatible with the `uN` integer type of the same size, and likewise `isize` is
1542
+ /// ABI-compatible with the `iN` integer type of the same size.
1543
+ /// - Any two `fn` types are ABI-compatible with each other if they have the same ABI string or the
1544
+ /// ABI string only differs in a trailing `-unwind`, independent of the rest of their signature.
1545
+ /// (Note that this is about the case of passing a function pointer as an argument to a function.
1546
+ /// The two pointers being ABI-compatible here means that the call successfully passes the
1547
+ /// pointer. When actually calling the pointer, of course the rest of the signature becomes
1548
+ /// relevant as well, according to the rules in this section.)
1549
+ /// - Any two types with size 0 and alignment 1 are ABI-compatible.
1550
+ /// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the
1551
+ /// unique field that doesn't have size 0 and alignment 1 (if there is such a field).
1552
+ /// - `i32` is ABI-compatible with `NonZeroI32`, and similar for all other integer types with their
1553
+ /// matching `NonZero*` type.
1554
+ /// - If `T` is guaranteed to be subject to the [null pointer
1555
+ /// optimization](option/index.html#representation), then `T` and `Option<T>` are ABI-compatible.
1556
+ ///
1557
+ /// Furthermore, ABI compatibility satisfies the following general properties:
1558
+ ///
1559
+ /// - Every type is ABI-compatible with itself.
1560
+ /// - If `T1` and `T2` are ABI-compatible, then two `repr(C)` types that only differ because one
1561
+ /// field type was changed from `T1` to `T2` are ABI-compatible.
1562
+ /// - If `T1` and `T2` are ABI-compatible and `T2` and `T3` are ABI-compatible, then so are `T1` and
1563
+ /// `T3` (i.e., ABI-compatibility is transitive).
1564
+ /// - If `T1` and `T2` are ABI-compatible, then so are `T2` and `T1` (i.e., ABI-compatibility is
1565
+ /// symmetric).
1566
+ ///
1567
+ /// More signatures can be ABI-compatible on specific targets, but that should not be relied upon
1568
+ /// since it is not portable and not a stable guarantee.
1569
+ ///
1570
+ /// Noteworthy cases of types *not* being ABI-compatible in general are:
1571
+ /// * `bool` vs `u8`, and `i32` vs `u32`: on some targets, the calling conventions for these types
1572
+ /// differ in terms of what they guarantee for the remaining bits in the register that are not
1573
+ /// used by the value.
1574
+ /// * `i32` vs `f32` are not compatible either, as has already been mentioned above.
1575
+ /// * `struct Foo(u32)` and `u32` are not compatible (without `repr(transparent)`) since structs are
1576
+ /// aggregate types and often passed in a different way than primitives like `i32`.
1577
+ ///
1578
+ /// Note that these rules describe when two completely known types are ABI-compatible. When
1579
+ /// considering ABI compatibility of a type declared in another crate (including the standard
1580
+ /// library), consider that any type that has a private field or the `#[non_exhaustive]` attribute
1581
+ /// may change its layout as a non-breaking update unless documented otherwise -- so for instance,
1582
+ /// even if such a type is a 1-ZST or `repr(transparent)` right now, this might change with any
1583
+ /// library version bump.
1584
+ ///
1585
+ /// If the declared signature and the signature of the function pointer are ABI-compatible, then the
1586
+ /// function call behaves as if every argument was [`transmute`d][mem::transmute] from the
1587
+ /// type in the function pointer to the type at the function declaration, and the return value is
1588
+ /// [`transmute`d][mem::transmute] from the type in the declaration to the type in the
1589
+ /// pointer. All the usual caveats and concerns around transmutation apply; for instance, if the
1590
+ /// function expects a `NonNullI32` and the function pointer uses the ABI-compatible type
1591
+ /// `Option<NonNullI32>`, and the value used for the argument is `None`, then this call is Undefined
1592
+ /// Behavior since transmuting `None::<NonNullI32>` to `NonNullI32` violates the non-null
1593
+ /// requirement.
1594
+ ///
1595
+ /// #### Requirements concerning target features
1596
+ ///
1597
+ /// Under some conditions, the signature used by the caller and the callee can be ABI-incompatible
1598
+ /// even if the exact same ABI string and types are being used. As an example, the
1599
+ /// `std::arch::x86_64::__m256` type has a different `extern "C"` ABI when the `avx` feature is
1600
+ /// enabled vs when it is not enabled.
1601
+ ///
1602
+ /// Therefore, to ensure ABI compatibility when code using different target features is combined
1603
+ /// (such as via `#[target_feature]`), we further require that one of the following conditions is
1604
+ /// met:
1605
+ ///
1606
+ /// - The function uses the `"Rust"` ABI string (which is the default without `extern`).
1607
+ /// - Caller and callee are using the exact same set of target features. For the callee we consider
1608
+ /// the features enabled (via `#[target_feature]` and `-C target-feature`/`-C target-cpu`) at the
1609
+ /// declaration site; for the caller we consider the features enabled at the call site.
1610
+ /// - Neither any argument nor the return value involves a SIMD type (`#[repr(simd)]`) that is not
1611
+ /// behind a pointer indirection (i.e., `*mut __m256` is fine, but `(i32, __m256)` is not).
1612
+ ///
1509
1613
/// ### Trait implementations
1510
1614
///
1511
1615
/// In this documentation the shorthand `fn (T₁, T₂, …, Tₙ)` is used to represent non-variadic
0 commit comments