Skip to main content

rapx/def_ids/
other_api.rs

1//! Identify `core`, `alloc`, and `std` APIs without depending on MIR availability.
2//! Callers use these identities to avoid costly MIR expansion where a summary
3//! exists, and to preserve facts needed by verification and slicing.
4//!
5//! # Agent Rules
6//!
7//! Keep this module limited to API identity checks based on compiler metadata,
8//! types, signatures, or standard-library paths. Do not depend on RAPx modules
9//! outside `def_ids` or implement call semantics here.
10
11use rustc_hir::def_id::DefId;
12use rustc_middle::ty::{TyCtxt, TyKind, UintTy};
13
14use crate::def_ids::lang_api;
15
16use super::{diagnostic_api, is_std_api, type_ids};
17
18/// Identify standard-library UB and precondition checks. The path builder
19/// skips their costly MIR, while the VM handles them as check calls.
20pub fn is_ub_check(tcx: TyCtxt<'_>, callee: DefId) -> bool {
21    if !is_std_api(tcx, callee) {
22        return false;
23    }
24    let path = tcx.def_path_str(callee);
25    path.contains("ub_checks::check_language_ub")
26        || path.contains("::precondition_check")
27        || path.contains("::runtime_add_nowrap")
28        || path.contains("ub_checks::maybe_is_aligned")
29        || path.contains("ub_checks::is_valid_allocation_size")
30        || path.contains("ub_checks::maybe_is_nonoverlapping")
31}
32
33/// Identify standard `Box::new` and `Box::new_in`. Dependency slicing keeps
34/// all inputs, and constructor modeling avoids treating the input as a
35/// pre-existing raw owner pointer.
36pub fn is_box_new(tcx: TyCtxt<'_>, did: DefId) -> bool {
37    if !tcx
38        .opt_item_name(did)
39        .is_some_and(|name| matches!(name.as_str(), "new" | "new_in"))
40    {
41        return false;
42    }
43    let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
44        return false;
45    };
46    let self_ty = tcx.type_of(impl_id).instantiate_identity().skip_norm_wip();
47    matches!(self_ty.kind(), TyKind::Adt(adt, _) if type_ids::is_std_box(adt.did()))
48}
49
50/// Identify `NonNull::dangling` for its constructor summary when MIR expansion
51/// is disabled. Unlike an arbitrary `NonNull<T>`, its result is aligned for `T`
52/// and has no allocation provenance; the type invariant only guarantees non-nullness.
53pub fn is_nonnull_dangling(tcx: TyCtxt<'_>, did: DefId) -> bool {
54    if !tcx
55        .opt_item_name(did)
56        .is_some_and(|name| name.as_str() == "dangling")
57    {
58        return false;
59    }
60    let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
61        return false;
62    };
63    let self_ty = tcx.type_of(impl_id).instantiate_identity().skip_norm_wip();
64    matches!(self_ty.kind(), TyKind::Adt(adt, _)
65        if type_ids::is_std_nonnull(adt.did()))
66        && tcx.fn_sig(did).skip_binder().inputs().skip_binder().is_empty()
67}
68
69/// Identify `ManuallyDrop::drop` so verification and slicing preserve its
70/// explicit lifetime-ending effect.
71pub fn is_manually_drop(tcx: TyCtxt<'_>, did: DefId) -> bool {
72    let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
73        return false;
74    };
75    let self_ty = tcx.type_of(impl_id).instantiate_identity();
76    let self_ty = self_ty.skip_norm_wip();
77    matches!(self_ty.kind(), TyKind::Adt(adt, _)
78        if tcx.is_lang_item(adt.did(), lang_api::LangItem::ManuallyDrop))
79        && tcx.item_name(did).as_str() == "drop"
80}
81
82/// Identify `drop`, `drop_in_place`, and `ManuallyDrop::drop`. Slicing keeps
83/// these calls because they can end a value's lifetime.
84pub fn is_drop_fn(tcx: TyCtxt<'_>, target: DefId) -> bool {
85    diagnostic_api::is_drop(tcx, target)
86        || diagnostic_api::is_drop_in_place(tcx, target)
87        || is_manually_drop(tcx, target)
88}
89
90/// Identify `*const T::align_offset` and `*mut T::align_offset` for their call
91/// effect, which takes priority over expanding their available MIR. Their
92/// internal helper has costly branches and symbolic bitwise arithmetic:
93/// expanding it is much slower, and the VM loses the guarantee that a result
94/// other than `usize::MAX` aligns `ptr + offset * size_of::<T>()`. Modeling the
95/// bitwise operations exactly also makes branch solving time out.
96pub fn is_align_offset(tcx: TyCtxt<'_>, did: DefId) -> bool {
97    if !is_std_api(tcx, did)
98        || !tcx
99            .opt_item_name(did)
100            .is_some_and(|name| name.as_str() == "align_offset")
101    {
102        return false;
103    }
104    let sig = tcx.fn_sig(did).skip_binder();
105    let inputs = sig.inputs().skip_binder();
106    inputs.len() == 2
107        && matches!(inputs[0].kind(), TyKind::RawPtr(..))
108        && matches!(inputs[1].kind(), TyKind::Uint(UintTy::Usize))
109        && matches!(sig.output().skip_binder().kind(), TyKind::Uint(UintTy::Usize))
110}
111
112/// Identify the standard gcd called by slice alignment's generic const block.
113/// Recursive expansion is costly, and CTFE leaves generic sizes symbolic;
114/// the VM uses this identity to retain the divisibility facts needed for proof.
115pub fn is_gcd(tcx: TyCtxt<'_>, did: DefId) -> bool {
116    is_std_api(tcx, did) && tcx.def_path_str(did).ends_with("::gcd")
117}