11// Copyright (c) 2022-present INESC-ID.
22// Distributed under the MIT license that can be found in the LICENSE file.
33
4- use crate :: ir:: { Access , FnIr , RuleIr , RulesIR } ;
4+ use crate :: ir:: { Access , FnIr , Model , RuleIr , RulesIR } ;
55use std:: collections:: HashMap ;
66use std:: path:: { Path , PathBuf } ;
77
@@ -77,7 +77,6 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
7777 "libcc2rs" ,
7878 "libcc2rs_macros" ,
7979 "libc" ,
80- "rustls_ffi" ,
8180 "nix" ,
8281 "jiff" ,
8382 "xattr" ,
@@ -196,6 +195,95 @@ impl<'tcx> FnDecl<'tcx> {
196195 }
197196 visitor. visit_expr ( self . body . value , Access :: Borrow ) ;
198197 }
198+
199+ fn unsafe_uses ( & self , tcx : rustc_middle:: ty:: TyCtxt < ' tcx > ) -> UnsafeUses {
200+ use rustc_hir:: intravisit:: Visitor ;
201+ let mut finder = UnsafeFinder {
202+ tcx,
203+ typeck : tcx. typeck_body ( self . body . id ( ) ) ,
204+ uses : UnsafeUses :: default ( ) ,
205+ } ;
206+ finder. visit_expr ( self . body . value ) ;
207+ finder. uses
208+ }
209+ }
210+
211+ /// What a rule body has that needs an unsafe context.
212+ #[ derive( Default ) ]
213+ struct UnsafeUses {
214+ block : bool ,
215+ op : bool ,
216+ }
217+
218+ struct UnsafeFinder < ' tcx > {
219+ tcx : rustc_middle:: ty:: TyCtxt < ' tcx > ,
220+ typeck : & ' tcx rustc_middle:: ty:: TypeckResults < ' tcx > ,
221+ uses : UnsafeUses ,
222+ }
223+
224+ impl < ' tcx > UnsafeFinder < ' tcx > {
225+ fn is_unsafe_op ( & self , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) -> bool {
226+ use rustc_hir:: def:: { DefKind , Res } ;
227+ use rustc_middle:: ty:: TyKind ;
228+
229+ match & expr. kind {
230+ rustc_hir:: ExprKind :: Call ( callee, _) => {
231+ let ty = self . typeck . expr_ty_adjusted ( callee) ;
232+ matches ! ( ty. kind( ) , TyKind :: FnDef ( ..) | TyKind :: FnPtr ( ..) )
233+ && ty. fn_sig ( self . tcx ) . safety ( ) . is_unsafe ( )
234+ }
235+ rustc_hir:: ExprKind :: MethodCall ( ..) => self
236+ . typeck
237+ . type_dependent_def_id ( expr. hir_id )
238+ . is_some_and ( |def_id| self . tcx . fn_sig ( def_id) . skip_binder ( ) . safety ( ) . is_unsafe ( ) ) ,
239+ rustc_hir:: ExprKind :: Unary ( rustc_hir:: UnOp :: Deref , inner) => {
240+ self . typeck . expr_ty_adjusted ( inner) . is_raw_ptr ( )
241+ }
242+ rustc_hir:: ExprKind :: Field ( base, _) => self
243+ . typeck
244+ . expr_ty_adjusted ( base)
245+ . ty_adt_def ( )
246+ . is_some_and ( |adt| adt. is_union ( ) ) ,
247+ rustc_hir:: ExprKind :: Path ( qpath) => match self . typeck . qpath_res ( qpath, expr. hir_id ) {
248+ Res :: Def ( DefKind :: Static { mutability, .. } , def_id) => {
249+ mutability. is_mut ( ) || self . tcx . is_foreign_item ( def_id)
250+ }
251+ _ => false ,
252+ } ,
253+ rustc_hir:: ExprKind :: InlineAsm ( _) => true ,
254+ _ => false ,
255+ }
256+ }
257+ }
258+
259+ impl < ' tcx > rustc_hir:: intravisit:: Visitor < ' tcx > for UnsafeFinder < ' tcx > {
260+ type NestedFilter = rustc_middle:: hir:: nested_filter:: OnlyBodies ;
261+
262+ fn maybe_tcx ( & mut self ) -> rustc_middle:: ty:: TyCtxt < ' tcx > {
263+ self . tcx
264+ }
265+
266+ fn visit_block ( & mut self , block : & ' tcx rustc_hir:: Block < ' tcx > ) {
267+ let rustc_hir:: BlockCheckMode :: UnsafeBlock ( source) = block. rules else {
268+ rustc_hir:: intravisit:: walk_block ( self , block) ;
269+ return ;
270+ } ;
271+ // Like the `unsafe_code` lint, trust the unsafe blocks of the compiler
272+ // and of the macros that allow internal unsafe, e.g., `panic!`.
273+ if source == rustc_hir:: UnsafeSource :: UserProvided && !block. span . allows_unsafe ( ) {
274+ // The unsafe block of a macro is an unsafe operation of the rule.
275+ if block. span . from_expansion ( ) {
276+ self . uses . op = true ;
277+ } else {
278+ self . uses . block = true ;
279+ }
280+ }
281+ }
282+
283+ fn visit_expr ( & mut self , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
284+ self . uses . op |= self . is_unsafe_op ( expr) ;
285+ rustc_hir:: intravisit:: walk_expr ( self , expr) ;
286+ }
199287}
200288
201289struct MethodResolver {
@@ -204,6 +292,13 @@ struct MethodResolver {
204292
205293impl MethodResolver {
206294 fn resolve_rule < ' tcx > ( & mut self , tcx : rustc_middle:: ty:: TyCtxt < ' tcx > , f : & FnDecl < ' tcx > ) {
295+ // The refcount model falls back to the unsafe rules it does not have.
296+ let used_by_refcount = f. source_file . ends_with ( Model :: Refcount . src_filename ( ) )
297+ || !self
298+ . ir
299+ . refcount_ir
300+ . as_ref ( )
301+ . is_some_and ( |ir| ir. contains_key ( & f. name ) ) ;
207302 let Some ( file_ir) = self . ir . get_mut ( & f. source_file ) else {
208303 return ;
209304 } ;
@@ -213,7 +308,33 @@ impl MethodResolver {
213308 let ret_ty = tcx. fn_sig ( f. def_id ) . skip_binder ( ) . output ( ) . skip_binder ( ) ;
214309 type_ir. type_info . derives = type_derives ( tcx, tcx. param_env ( f. def_id ) , ret_ty) ;
215310 }
216- None => { }
311+ None => return ,
312+ }
313+
314+ // A rule is either unsafe as a whole or does nothing unsafe.
315+ let uses = f. unsafe_uses ( tcx) ;
316+ assert ! (
317+ !uses. block,
318+ "{} ({}): rules must not have unsafe blocks; mark the rule unsafe instead" ,
319+ f. name, f. source_file
320+ ) ;
321+ if tcx. fn_sig ( f. def_id ) . skip_binder ( ) . safety ( ) . is_unsafe ( ) {
322+ assert ! (
323+ !used_by_refcount,
324+ "{} ({}): the refcount model uses this rule, so it must not be unsafe" ,
325+ f. name, f. source_file
326+ ) ;
327+ assert ! (
328+ uses. op,
329+ "{} ({}): the rule does no unsafe operation, so it must not be unsafe" ,
330+ f. name, f. source_file
331+ ) ;
332+ } else {
333+ assert ! (
334+ !uses. op,
335+ "{} ({}): the rule does an unsafe operation, so it must be unsafe" ,
336+ f. name, f. source_file
337+ ) ;
217338 }
218339 }
219340
0 commit comments