|
| 1 | +// Copyright Kani Contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +// |
| 4 | +//! Module responsible for implementing a few Rust compiler intrinsics. |
| 5 | +//! |
| 6 | +//! Note that some rustc intrinsics are lowered to MIR instructions. Those can also be handled |
| 7 | +//! here. |
| 8 | +
|
| 9 | +use crate::intrinsics::Intrinsic; |
| 10 | +use crate::kani_middle::kani_functions::{KaniFunction, KaniModel}; |
| 11 | +use crate::kani_middle::transform::body::{MutMirVisitor, MutableBody}; |
| 12 | +use crate::kani_middle::transform::{TransformPass, TransformationType}; |
| 13 | +use crate::kani_queries::QueryDb; |
| 14 | +use rustc_middle::ty::TyCtxt; |
| 15 | +use stable_mir::mir::mono::Instance; |
| 16 | +use stable_mir::mir::{Body, ConstOperand, LocalDecl, Operand, Terminator, TerminatorKind}; |
| 17 | +use stable_mir::ty::{FnDef, MirConst, RigidTy, TyKind}; |
| 18 | +use std::collections::HashMap; |
| 19 | +use tracing::debug; |
| 20 | + |
| 21 | +/// Generate the body for a few Kani intrinsics. |
| 22 | +#[derive(Debug)] |
| 23 | +pub struct RustcIntrinsicsPass { |
| 24 | + /// Used to cache FnDef lookups for intrinsics models. |
| 25 | + models: HashMap<KaniModel, FnDef>, |
| 26 | +} |
| 27 | + |
| 28 | +impl TransformPass for RustcIntrinsicsPass { |
| 29 | + fn transformation_type() -> TransformationType |
| 30 | + where |
| 31 | + Self: Sized, |
| 32 | + { |
| 33 | + TransformationType::Stubbing |
| 34 | + } |
| 35 | + |
| 36 | + fn is_enabled(&self, _query_db: &QueryDb) -> bool |
| 37 | + where |
| 38 | + Self: Sized, |
| 39 | + { |
| 40 | + true |
| 41 | + } |
| 42 | + |
| 43 | + /// Transform the function body by inserting checks one-by-one. |
| 44 | + /// For every unsafe dereference or a transmute operation, we check all values are valid. |
| 45 | + fn transform(&mut self, _tcx: TyCtxt, body: Body, instance: Instance) -> (bool, Body) { |
| 46 | + debug!(function=?instance.name(), "transform"); |
| 47 | + let mut new_body = MutableBody::from(body); |
| 48 | + let mut visitor = ReplaceIntrinsicVisitor::new(&self.models, new_body.locals().to_vec()); |
| 49 | + visitor.visit_body(&mut new_body); |
| 50 | + (visitor.changed, new_body.into()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl RustcIntrinsicsPass { |
| 55 | + pub fn new(queries: &QueryDb) -> Self { |
| 56 | + let models = queries |
| 57 | + .kani_functions() |
| 58 | + .iter() |
| 59 | + .filter_map(|(func, def)| { |
| 60 | + if let KaniFunction::Model(model) = func { Some((*model, *def)) } else { None } |
| 61 | + }) |
| 62 | + .collect(); |
| 63 | + debug!(?models, "RustcIntrinsicsPass::new"); |
| 64 | + RustcIntrinsicsPass { models } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +struct ReplaceIntrinsicVisitor<'a> { |
| 69 | + models: &'a HashMap<KaniModel, FnDef>, |
| 70 | + locals: Vec<LocalDecl>, |
| 71 | + changed: bool, |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a> ReplaceIntrinsicVisitor<'a> { |
| 75 | + fn new(models: &'a HashMap<KaniModel, FnDef>, locals: Vec<LocalDecl>) -> Self { |
| 76 | + ReplaceIntrinsicVisitor { models, locals, changed: false } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl MutMirVisitor for ReplaceIntrinsicVisitor<'_> { |
| 81 | + /// Replace the terminator for some rustc's intrinsics. |
| 82 | + /// |
| 83 | + /// In some cases, we replace a function call to a rustc intrinsic by a call to the |
| 84 | + /// corresponding Kani intrinsic. |
| 85 | + /// |
| 86 | + /// Our models are usually augmented by some trait bounds, or they leverage Kani intrinsics to |
| 87 | + /// implement the given semantics. |
| 88 | + /// |
| 89 | + /// Note that we only need to replace function calls since intrinsics must always be called |
| 90 | + /// directly. I.e., no need to handle function pointers. |
| 91 | + fn visit_terminator(&mut self, term: &mut Terminator) { |
| 92 | + if let TerminatorKind::Call { func, .. } = &mut term.kind { |
| 93 | + if let TyKind::RigidTy(RigidTy::FnDef(def, args)) = |
| 94 | + func.ty(&self.locals).unwrap().kind() |
| 95 | + { |
| 96 | + if def.is_intrinsic() { |
| 97 | + let instance = Instance::resolve(def, &args).unwrap(); |
| 98 | + let intrinsic = Intrinsic::from_instance(&instance); |
| 99 | + debug!(?intrinsic, "handle_terminator"); |
| 100 | + let model = match intrinsic { |
| 101 | + Intrinsic::SizeOfVal => self.models[&KaniModel::SizeOfVal], |
| 102 | + Intrinsic::MinAlignOfVal => self.models[&KaniModel::AlignOfVal], |
| 103 | + // The rest is handled in codegen. |
| 104 | + _ => { |
| 105 | + return self.super_terminator(term); |
| 106 | + } |
| 107 | + }; |
| 108 | + let new_instance = Instance::resolve(model, &args).unwrap(); |
| 109 | + let literal = MirConst::try_new_zero_sized(new_instance.ty()).unwrap(); |
| 110 | + let span = term.span; |
| 111 | + let new_func = ConstOperand { span, user_ty: None, const_: literal }; |
| 112 | + *func = Operand::Constant(new_func); |
| 113 | + self.changed = true; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + self.super_terminator(term); |
| 118 | + } |
| 119 | +} |
0 commit comments