1use std::error::Error;
4use std::path::Path;
5use std::str::FromStr;
6use std::time::Duration;
7use std::{cmp, env, iter};
8
9use rustc_ast::expand::allocator::{ALLOC_ERROR_HANDLER, AllocatorKind, global_fn_name};
10use rustc_ast::{self as ast, *};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_data_structures::owned_slice::OwnedSlice;
13use rustc_data_structures::svh::Svh;
14use rustc_data_structures::sync::{self, FreezeReadGuard, FreezeWriteGuard};
15use rustc_data_structures::unord::UnordMap;
16use rustc_expand::base::SyntaxExtension;
17use rustc_fs_util::try_canonicalize;
18use rustc_hir as hir;
19use rustc_hir::def_id::{CrateNum, LOCAL_CRATE, LocalDefId, StableCrateId};
20use rustc_hir::definitions::Definitions;
21use rustc_index::IndexVec;
22use rustc_middle::bug;
23use rustc_middle::ty::data_structures::IndexSet;
24use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
25use rustc_proc_macro::bridge::client::ProcMacro;
26use rustc_session::Session;
27use rustc_session::config::{
28 CrateType, ExtendedTargetModifierInfo, ExternLocation, Externs, OptionsTargetModifiers,
29 TargetModifier,
30};
31use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
32use rustc_session::lint::{self, BuiltinLintDiag};
33use rustc_session::output::validate_crate_name;
34use rustc_session::search_paths::PathKind;
35use rustc_span::def_id::DefId;
36use rustc_span::edition::Edition;
37use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
38use rustc_target::spec::{PanicStrategy, Target};
39use tracing::{debug, info, trace};
40
41use crate::errors;
42use crate::locator::{CrateError, CrateLocator, CratePaths, CrateRejections};
43use crate::rmeta::{
44 CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob, TargetModifiers,
45};
46
47pub trait MetadataLoader {
52 fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
53 fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<OwnedSlice, String>;
54}
55
56pub type MetadataLoaderDyn = dyn MetadataLoader + Send + Sync + sync::DynSend + sync::DynSync;
57
58pub struct CStore {
59 metadata_loader: Box<MetadataLoaderDyn>,
60
61 metas: IndexVec<CrateNum, Option<Box<CrateMetadata>>>,
62 injected_panic_runtime: Option<CrateNum>,
63 allocator_kind: Option<AllocatorKind>,
66 alloc_error_handler_kind: Option<AllocatorKind>,
69 has_global_allocator: bool,
71 has_alloc_error_handler: bool,
73
74 resolved_externs: UnordMap<Symbol, CrateNum>,
76
77 unused_externs: Vec<Symbol>,
79
80 used_extern_options: FxHashSet<Symbol>,
81}
82
83impl std::fmt::Debug for CStore {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 f.debug_struct("CStore").finish_non_exhaustive()
86 }
87}
88
89pub enum LoadedMacro {
90 MacroDef {
91 def: MacroDef,
92 ident: Ident,
93 attrs: Vec<hir::Attribute>,
94 span: Span,
95 edition: Edition,
96 },
97 ProcMacro(SyntaxExtension),
98}
99
100pub(crate) struct Library {
101 pub source: CrateSource,
102 pub metadata: MetadataBlob,
103}
104
105enum LoadResult {
106 Previous(CrateNum),
107 Loaded(Library),
108}
109
110#[derive(Clone, Copy)]
112pub(crate) struct CrateMetadataRef<'a> {
113 pub cdata: &'a CrateMetadata,
114 pub cstore: &'a CStore,
115}
116
117impl std::ops::Deref for CrateMetadataRef<'_> {
118 type Target = CrateMetadata;
119
120 fn deref(&self) -> &Self::Target {
121 self.cdata
122 }
123}
124
125struct CrateDump<'a>(&'a CStore);
126
127impl<'a> std::fmt::Debug for CrateDump<'a> {
128 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129 writeln!(fmt, "resolved crates:")?;
130 for (cnum, data) in self.0.iter_crate_data() {
131 writeln!(fmt, " name: {}", data.name())?;
132 writeln!(fmt, " cnum: {cnum}")?;
133 writeln!(fmt, " hash: {}", data.hash())?;
134 writeln!(fmt, " reqd: {:?}", data.dep_kind())?;
135 writeln!(fmt, " priv: {:?}", data.is_private_dep())?;
136 let CrateSource { dylib, rlib, rmeta, sdylib_interface } = data.source();
137 if let Some(dylib) = dylib {
138 writeln!(fmt, " dylib: {}", dylib.0.display())?;
139 }
140 if let Some(rlib) = rlib {
141 writeln!(fmt, " rlib: {}", rlib.0.display())?;
142 }
143 if let Some(rmeta) = rmeta {
144 writeln!(fmt, " rmeta: {}", rmeta.0.display())?;
145 }
146 if let Some(sdylib_interface) = sdylib_interface {
147 writeln!(fmt, " sdylib interface: {}", sdylib_interface.0.display())?;
148 }
149 }
150 Ok(())
151 }
152}
153
154#[derive(Clone, Copy)]
156enum CrateOrigin<'a> {
157 IndirectDependency {
159 dep_root: &'a CratePaths,
161 parent_private: bool,
163 dep: &'a CrateDep,
165 },
166 Injected,
168 Extern,
170}
171
172impl<'a> CrateOrigin<'a> {
173 fn dep_root(&self) -> Option<&'a CratePaths> {
175 match self {
176 CrateOrigin::IndirectDependency { dep_root, .. } => Some(dep_root),
177 _ => None,
178 }
179 }
180
181 fn dep(&self) -> Option<&'a CrateDep> {
183 match self {
184 CrateOrigin::IndirectDependency { dep, .. } => Some(dep),
185 _ => None,
186 }
187 }
188
189 fn private_dep(&self) -> Option<bool> {
192 match self {
193 CrateOrigin::IndirectDependency { parent_private, dep, .. } => {
194 Some(dep.is_private || *parent_private)
195 }
196 _ => None,
197 }
198 }
199}
200
201impl CStore {
202 pub fn from_tcx(tcx: TyCtxt<'_>) -> FreezeReadGuard<'_, CStore> {
203 FreezeReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
204 cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
205 })
206 }
207
208 pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> FreezeWriteGuard<'_, CStore> {
209 FreezeWriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
210 cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
211 })
212 }
213
214 fn intern_stable_crate_id<'tcx>(
215 &mut self,
216 tcx: TyCtxt<'tcx>,
217 root: &CrateRoot,
218 ) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateError> {
219 assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
220 let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
221 if existing == LOCAL_CRATE {
223 CrateError::SymbolConflictsCurrent(root.name())
224 } else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
225 {
226 let crate_name0 = root.name();
227 CrateError::StableCrateIdCollision(crate_name0, crate_name1)
228 } else {
229 CrateError::NotFound(root.name())
230 }
231 })?;
232
233 self.metas.push(None);
234 Ok(num)
235 }
236
237 pub fn has_crate_data(&self, cnum: CrateNum) -> bool {
238 self.metas[cnum].is_some()
239 }
240
241 pub(crate) fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> {
242 let cdata = self.metas[cnum]
243 .as_ref()
244 .unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"));
245 CrateMetadataRef { cdata, cstore: self }
246 }
247
248 pub(crate) fn get_crate_data_mut(&mut self, cnum: CrateNum) -> &mut CrateMetadata {
249 self.metas[cnum].as_mut().unwrap_or_else(|| panic!("Failed to get crate data for {cnum:?}"))
250 }
251
252 fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) {
253 assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
254 self.metas[cnum] = Some(Box::new(data));
255 }
256
257 pub(crate) fn set_resolved_extern_crate_name(&mut self, name: Symbol, extern_crate: CrateNum) {
262 self.resolved_externs.insert(name, extern_crate);
263 }
264
265 pub fn resolved_extern_crate(&self, externs_name: Symbol) -> Option<CrateNum> {
270 self.resolved_externs.get(&externs_name).copied()
271 }
272
273 pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> {
274 self.metas
275 .iter_enumerated()
276 .filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
277 }
278
279 pub fn all_proc_macro_def_ids(&self) -> impl Iterator<Item = DefId> {
280 self.iter_crate_data().flat_map(|(krate, data)| data.proc_macros_for_crate(krate, self))
281 }
282
283 fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
284 if !deps.contains(&cnum) {
285 let data = self.get_crate_data(cnum);
286 for dep in data.dependencies() {
287 if dep != cnum {
288 self.push_dependencies_in_postorder(deps, dep);
289 }
290 }
291
292 deps.insert(cnum);
293 }
294 }
295
296 pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> IndexSet<CrateNum> {
297 let mut deps = IndexSet::default();
298 if cnum == LOCAL_CRATE {
299 for (cnum, _) in self.iter_crate_data() {
300 self.push_dependencies_in_postorder(&mut deps, cnum);
301 }
302 } else {
303 self.push_dependencies_in_postorder(&mut deps, cnum);
304 }
305 deps
306 }
307
308 pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
309 self.injected_panic_runtime
310 }
311
312 pub(crate) fn allocator_kind(&self) -> Option<AllocatorKind> {
313 self.allocator_kind
314 }
315
316 pub(crate) fn alloc_error_handler_kind(&self) -> Option<AllocatorKind> {
317 self.alloc_error_handler_kind
318 }
319
320 pub(crate) fn has_global_allocator(&self) -> bool {
321 self.has_global_allocator
322 }
323
324 pub(crate) fn has_alloc_error_handler(&self) -> bool {
325 self.has_alloc_error_handler
326 }
327
328 pub fn report_unused_deps(&self, tcx: TyCtxt<'_>) {
329 let json_unused_externs = tcx.sess.opts.json_unused_externs;
330
331 if !json_unused_externs.is_enabled() {
335 return;
336 }
337 let level = tcx
338 .lint_level_at_node(lint::builtin::UNUSED_CRATE_DEPENDENCIES, rustc_hir::CRATE_HIR_ID)
339 .level;
340 if level != lint::Level::Allow {
341 let unused_externs =
342 self.unused_externs.iter().map(|ident| ident.to_ident_string()).collect::<Vec<_>>();
343 let unused_externs = unused_externs.iter().map(String::as_str).collect::<Vec<&str>>();
344 tcx.dcx().emit_unused_externs(level, json_unused_externs.is_loud(), &unused_externs);
345 }
346 }
347
348 fn report_target_modifiers_extended(
349 tcx: TyCtxt<'_>,
350 krate: &Crate,
351 mods: &TargetModifiers,
352 dep_mods: &TargetModifiers,
353 data: &CrateMetadata,
354 ) {
355 let span = krate.spans.inner_span.shrink_to_lo();
356 let allowed_flag_mismatches = &tcx.sess.opts.cg.unsafe_allow_abi_mismatch;
357 let local_crate = tcx.crate_name(LOCAL_CRATE);
358 let tmod_extender = |tmod: &TargetModifier| (tmod.extend(), tmod.clone());
359 let report_diff = |prefix: &String,
360 opt_name: &String,
361 flag_local_value: Option<&String>,
362 flag_extern_value: Option<&String>| {
363 if allowed_flag_mismatches.contains(&opt_name) {
364 return;
365 }
366 let extern_crate = data.name();
367 let flag_name = opt_name.clone();
368 let flag_name_prefixed = format!("-{}{}", prefix, opt_name);
369
370 match (flag_local_value, flag_extern_value) {
371 (Some(local_value), Some(extern_value)) => {
372 tcx.dcx().emit_err(errors::IncompatibleTargetModifiers {
373 span,
374 extern_crate,
375 local_crate,
376 flag_name,
377 flag_name_prefixed,
378 local_value: local_value.to_string(),
379 extern_value: extern_value.to_string(),
380 })
381 }
382 (None, Some(extern_value)) => {
383 tcx.dcx().emit_err(errors::IncompatibleTargetModifiersLMissed {
384 span,
385 extern_crate,
386 local_crate,
387 flag_name,
388 flag_name_prefixed,
389 extern_value: extern_value.to_string(),
390 })
391 }
392 (Some(local_value), None) => {
393 tcx.dcx().emit_err(errors::IncompatibleTargetModifiersRMissed {
394 span,
395 extern_crate,
396 local_crate,
397 flag_name,
398 flag_name_prefixed,
399 local_value: local_value.to_string(),
400 })
401 }
402 (None, None) => panic!("Incorrect target modifiers report_diff(None, None)"),
403 };
404 };
405 let mut it1 = mods.iter().map(tmod_extender);
406 let mut it2 = dep_mods.iter().map(tmod_extender);
407 let mut left_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
408 let mut right_name_val: Option<(ExtendedTargetModifierInfo, TargetModifier)> = None;
409 loop {
410 left_name_val = left_name_val.or_else(|| it1.next());
411 right_name_val = right_name_val.or_else(|| it2.next());
412 match (&left_name_val, &right_name_val) {
413 (Some(l), Some(r)) => match l.1.opt.cmp(&r.1.opt) {
414 cmp::Ordering::Equal => {
415 if !l.1.consistent(&tcx.sess, Some(&r.1)) {
416 report_diff(
417 &l.0.prefix,
418 &l.0.name,
419 Some(&l.1.value_name),
420 Some(&r.1.value_name),
421 );
422 }
423 left_name_val = None;
424 right_name_val = None;
425 }
426 cmp::Ordering::Greater => {
427 if !r.1.consistent(&tcx.sess, None) {
428 report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
429 }
430 right_name_val = None;
431 }
432 cmp::Ordering::Less => {
433 if !l.1.consistent(&tcx.sess, None) {
434 report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
435 }
436 left_name_val = None;
437 }
438 },
439 (Some(l), None) => {
440 if !l.1.consistent(&tcx.sess, None) {
441 report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
442 }
443 left_name_val = None;
444 }
445 (None, Some(r)) => {
446 if !r.1.consistent(&tcx.sess, None) {
447 report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
448 }
449 right_name_val = None;
450 }
451 (None, None) => break,
452 }
453 }
454 }
455
456 pub fn report_incompatible_target_modifiers(&self, tcx: TyCtxt<'_>, krate: &Crate) {
457 for flag_name in &tcx.sess.opts.cg.unsafe_allow_abi_mismatch {
458 if !OptionsTargetModifiers::is_target_modifier(flag_name) {
459 tcx.dcx().emit_err(errors::UnknownTargetModifierUnsafeAllowed {
460 span: krate.spans.inner_span.shrink_to_lo(),
461 flag_name: flag_name.clone(),
462 });
463 }
464 }
465 let mods = tcx.sess.opts.gather_target_modifiers();
466 for (_cnum, data) in self.iter_crate_data() {
467 if data.is_proc_macro_crate() {
468 continue;
469 }
470 let dep_mods = data.target_modifiers();
471 if mods != dep_mods {
472 Self::report_target_modifiers_extended(tcx, krate, &mods, &dep_mods, data);
473 }
474 }
475 }
476
477 pub fn report_incompatible_async_drop_feature(&self, tcx: TyCtxt<'_>, krate: &Crate) {
479 if tcx.features().async_drop() {
480 return;
481 }
482 for (_cnum, data) in self.iter_crate_data() {
483 if data.is_proc_macro_crate() {
484 continue;
485 }
486 if data.has_async_drops() {
487 let extern_crate = data.name();
488 let local_crate = tcx.crate_name(LOCAL_CRATE);
489 tcx.dcx().emit_warn(errors::AsyncDropTypesInDependency {
490 span: krate.spans.inner_span.shrink_to_lo(),
491 extern_crate,
492 local_crate,
493 });
494 }
495 }
496 }
497
498 pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
499 CStore {
500 metadata_loader,
501 metas: IndexVec::from_iter(iter::once(None)),
506 injected_panic_runtime: None,
507 allocator_kind: None,
508 alloc_error_handler_kind: None,
509 has_global_allocator: false,
510 has_alloc_error_handler: false,
511 resolved_externs: UnordMap::default(),
512 unused_externs: Vec::new(),
513 used_extern_options: Default::default(),
514 }
515 }
516
517 fn existing_match(
518 &self,
519 externs: &Externs,
520 name: Symbol,
521 hash: Option<Svh>,
522 kind: PathKind,
523 ) -> Option<CrateNum> {
524 for (cnum, data) in self.iter_crate_data() {
525 if data.name() != name {
526 trace!("{} did not match {}", data.name(), name);
527 continue;
528 }
529
530 match hash {
531 Some(hash) if hash == data.hash() => return Some(cnum),
532 Some(hash) => {
533 debug!("actual hash {} did not match expected {}", hash, data.hash());
534 continue;
535 }
536 None => {}
537 }
538
539 let source = data.source();
549 if let Some(entry) = externs.get(name.as_str()) {
550 if let Some(mut files) = entry.files() {
552 if files.any(|l| {
553 let l = l.canonicalized();
554 source.dylib.as_ref().map(|(p, _)| p) == Some(l)
555 || source.rlib.as_ref().map(|(p, _)| p) == Some(l)
556 || source.rmeta.as_ref().map(|(p, _)| p) == Some(l)
557 }) {
558 return Some(cnum);
559 }
560 }
561 continue;
562 }
563
564 let prev_kind = source
571 .dylib
572 .as_ref()
573 .or(source.rlib.as_ref())
574 .or(source.rmeta.as_ref())
575 .expect("No sources for crate")
576 .1;
577 if kind.matches(prev_kind) {
578 return Some(cnum);
579 } else {
580 debug!(
581 "failed to load existing crate {}; kind {:?} did not match prev_kind {:?}",
582 name, kind, prev_kind
583 );
584 }
585 }
586
587 None
588 }
589
590 fn is_private_dep(
601 &self,
602 externs: &Externs,
603 name: Symbol,
604 private_dep: Option<bool>,
605 origin: CrateOrigin<'_>,
606 ) -> bool {
607 if matches!(origin, CrateOrigin::Injected) {
608 return true;
609 }
610
611 let extern_private = externs.get(name.as_str()).map(|e| e.is_private_dep);
612 match (extern_private, private_dep) {
613 (Some(false), _) | (_, Some(false)) | (None, None) => false,
616 (Some(true) | None, Some(true) | None) => true,
618 }
619 }
620
621 fn register_crate<'tcx>(
622 &mut self,
623 tcx: TyCtxt<'tcx>,
624 host_lib: Option<Library>,
625 origin: CrateOrigin<'_>,
626 lib: Library,
627 dep_kind: CrateDepKind,
628 name: Symbol,
629 private_dep: Option<bool>,
630 ) -> Result<CrateNum, CrateError> {
631 let _prof_timer =
632 tcx.sess.prof.generic_activity_with_arg("metadata_register_crate", name.as_str());
633
634 let Library { source, metadata } = lib;
635 let crate_root = metadata.get_root();
636 let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
637 let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
638
639 let feed = self.intern_stable_crate_id(tcx, &crate_root)?;
641 let cnum = feed.key();
642
643 info!(
644 "register crate `{}` (cnum = {}. private_dep = {})",
645 crate_root.name(),
646 cnum,
647 private_dep
648 );
649
650 let crate_paths;
653 let dep_root = if let Some(dep_root) = origin.dep_root() {
654 dep_root
655 } else {
656 crate_paths = CratePaths::new(crate_root.name(), source.clone());
657 &crate_paths
658 };
659
660 let cnum_map = self.resolve_crate_deps(
661 tcx,
662 dep_root,
663 &crate_root,
664 &metadata,
665 cnum,
666 dep_kind,
667 private_dep,
668 )?;
669
670 let raw_proc_macros = if crate_root.is_proc_macro_crate() {
671 let temp_root;
672 let (dlsym_source, dlsym_root) = match &host_lib {
673 Some(host_lib) => (&host_lib.source, {
674 temp_root = host_lib.metadata.get_root();
675 &temp_root
676 }),
677 None => (&source, &crate_root),
678 };
679 let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
680 Some(self.dlsym_proc_macros(tcx.sess, &dlsym_dylib.0, dlsym_root.stable_crate_id())?)
681 } else {
682 None
683 };
684
685 let crate_metadata = CrateMetadata::new(
686 self,
687 metadata,
688 crate_root,
689 raw_proc_macros,
690 cnum,
691 cnum_map,
692 dep_kind,
693 source,
694 private_dep,
695 host_hash,
696 );
697
698 self.set_crate_data(cnum, crate_metadata);
699
700 Ok(cnum)
701 }
702
703 fn load_proc_macro<'a, 'b>(
704 &self,
705 sess: &'a Session,
706 locator: &mut CrateLocator<'b>,
707 crate_rejections: &mut CrateRejections,
708 path_kind: PathKind,
709 host_hash: Option<Svh>,
710 ) -> Result<Option<(LoadResult, Option<Library>)>, CrateError>
711 where
712 'a: 'b,
713 {
714 if sess.opts.unstable_opts.dual_proc_macros {
715 let mut proc_macro_locator = locator.clone();
718
719 proc_macro_locator.for_target_proc_macro(sess, path_kind);
721
722 let target_result =
724 match self.load(&mut proc_macro_locator, &mut CrateRejections::default())? {
725 Some(LoadResult::Previous(cnum)) => {
726 return Ok(Some((LoadResult::Previous(cnum), None)));
727 }
728 Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
729 None => return Ok(None),
730 };
731
732 *crate_rejections = CrateRejections::default();
735
736 locator.for_proc_macro(sess, path_kind);
738
739 locator.hash = host_hash;
740
741 let Some(host_result) = self.load(locator, crate_rejections)? else {
742 return Ok(None);
743 };
744
745 let host_result = match host_result {
746 LoadResult::Previous(..) => {
747 panic!("host and target proc macros must be loaded in lock-step")
748 }
749 LoadResult::Loaded(library) => library,
750 };
751 Ok(Some((target_result.unwrap(), Some(host_result))))
752 } else {
753 let mut proc_macro_locator = locator.clone();
756
757 proc_macro_locator.for_proc_macro(sess, path_kind);
759
760 let Some(host_result) =
761 self.load(&mut proc_macro_locator, &mut CrateRejections::default())?
762 else {
763 return Ok(None);
764 };
765
766 Ok(Some((host_result, None)))
767 }
768 }
769
770 fn resolve_crate<'tcx>(
771 &mut self,
772 tcx: TyCtxt<'tcx>,
773 name: Symbol,
774 span: Span,
775 dep_kind: CrateDepKind,
776 origin: CrateOrigin<'_>,
777 ) -> Option<CrateNum> {
778 self.used_extern_options.insert(name);
779 match self.maybe_resolve_crate(tcx, name, dep_kind, origin) {
780 Ok(cnum) => {
781 self.set_used_recursively(cnum);
782 Some(cnum)
783 }
784 Err(err) => {
785 debug!("failed to resolve crate {} {:?}", name, dep_kind);
786 let missing_core = self
787 .maybe_resolve_crate(
788 tcx,
789 sym::core,
790 CrateDepKind::Explicit,
791 CrateOrigin::Extern,
792 )
793 .is_err();
794 err.report(tcx.sess, span, missing_core);
795 None
796 }
797 }
798 }
799
800 fn maybe_resolve_crate<'b, 'tcx>(
801 &'b mut self,
802 tcx: TyCtxt<'tcx>,
803 name: Symbol,
804 mut dep_kind: CrateDepKind,
805 origin: CrateOrigin<'b>,
806 ) -> Result<CrateNum, CrateError> {
807 info!("resolving crate `{}`", name);
808 if !name.as_str().is_ascii() {
809 return Err(CrateError::NonAsciiName(name));
810 }
811
812 let dep_root = origin.dep_root();
813 let dep = origin.dep();
814 let hash = dep.map(|d| d.hash);
815 let host_hash = dep.map(|d| d.host_hash).flatten();
816 let extra_filename = dep.map(|d| &d.extra_filename[..]);
817 let path_kind = if dep.is_some() { PathKind::Dependency } else { PathKind::Crate };
818 let private_dep = origin.private_dep();
819
820 let result = if let Some(cnum) =
821 self.existing_match(&tcx.sess.opts.externs, name, hash, path_kind)
822 {
823 (LoadResult::Previous(cnum), None)
824 } else {
825 info!("falling back to a load");
826 let mut locator = CrateLocator::new(
827 tcx.sess,
828 &*self.metadata_loader,
829 name,
830 tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
833 hash,
834 extra_filename,
835 path_kind,
836 );
837 let mut crate_rejections = CrateRejections::default();
838
839 match self.load(&mut locator, &mut crate_rejections)? {
840 Some(res) => (res, None),
841 None => {
842 info!("falling back to loading proc_macro");
843 dep_kind = CrateDepKind::MacrosOnly;
844 match self.load_proc_macro(
845 tcx.sess,
846 &mut locator,
847 &mut crate_rejections,
848 path_kind,
849 host_hash,
850 )? {
851 Some(res) => res,
852 None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
853 }
854 }
855 }
856 };
857
858 match result {
859 (LoadResult::Previous(cnum), None) => {
860 info!("library for `{}` was loaded previously, cnum {cnum}", name);
861 let private_dep =
866 self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
867 let data = self.get_crate_data_mut(cnum);
868 if data.is_proc_macro_crate() {
869 dep_kind = CrateDepKind::MacrosOnly;
870 }
871 data.set_dep_kind(cmp::max(data.dep_kind(), dep_kind));
872 data.update_and_private_dep(private_dep);
873 Ok(cnum)
874 }
875 (LoadResult::Loaded(library), host_library) => {
876 info!("register newly loaded library for `{}`", name);
877 self.register_crate(tcx, host_library, origin, library, dep_kind, name, private_dep)
878 }
879 _ => panic!(),
880 }
881 }
882
883 fn load(
884 &self,
885 locator: &CrateLocator<'_>,
886 crate_rejections: &mut CrateRejections,
887 ) -> Result<Option<LoadResult>, CrateError> {
888 let Some(library) = locator.maybe_load_library_crate(crate_rejections)? else {
889 return Ok(None);
890 };
891
892 let root = library.metadata.get_root();
897 let mut result = LoadResult::Loaded(library);
898 for (cnum, data) in self.iter_crate_data() {
899 if data.name() == root.name() && root.hash() == data.hash() {
900 assert!(locator.hash.is_none());
901 info!("load success, going to previous cnum: {}", cnum);
902 result = LoadResult::Previous(cnum);
903 break;
904 }
905 }
906 Ok(Some(result))
907 }
908
909 fn resolve_crate_deps(
911 &mut self,
912 tcx: TyCtxt<'_>,
913 dep_root: &CratePaths,
914 crate_root: &CrateRoot,
915 metadata: &MetadataBlob,
916 krate: CrateNum,
917 dep_kind: CrateDepKind,
918 parent_is_private: bool,
919 ) -> Result<CrateNumMap, CrateError> {
920 debug!(
921 "resolving deps of external crate `{}` with dep root `{}`",
922 crate_root.name(),
923 dep_root.name
924 );
925 if crate_root.is_proc_macro_crate() {
926 return Ok(CrateNumMap::new());
927 }
928
929 let deps = crate_root.decode_crate_deps(metadata);
933 let mut crate_num_map = CrateNumMap::with_capacity(1 + deps.len());
934 crate_num_map.push(krate);
935 for dep in deps {
936 info!(
937 "resolving dep `{}`->`{}` hash: `{}` extra filename: `{}` private {}",
938 crate_root.name(),
939 dep.name,
940 dep.hash,
941 dep.extra_filename,
942 dep.is_private,
943 );
944 let dep_kind = match dep_kind {
945 CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
946 _ => dep.kind,
947 };
948 let cnum = self.maybe_resolve_crate(
949 tcx,
950 dep.name,
951 dep_kind,
952 CrateOrigin::IndirectDependency {
953 dep_root,
954 parent_private: parent_is_private,
955 dep: &dep,
956 },
957 )?;
958 crate_num_map.push(cnum);
959 }
960
961 debug!("resolve_crate_deps: cnum_map for {:?} is {:?}", krate, crate_num_map);
962 Ok(crate_num_map)
963 }
964
965 fn dlsym_proc_macros(
966 &self,
967 sess: &Session,
968 path: &Path,
969 stable_crate_id: StableCrateId,
970 ) -> Result<&'static [ProcMacro], CrateError> {
971 let sym_name = sess.generate_proc_macro_decls_symbol(stable_crate_id);
972 debug!("trying to dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
973
974 unsafe {
975 let result = load_symbol_from_dylib::<*const &[ProcMacro]>(path, &sym_name);
976 match result {
977 Ok(result) => {
978 debug!("loaded dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
979 Ok(*result)
980 }
981 Err(err) => {
982 debug!(
983 "failed to dlsym proc_macros {} for symbol `{}`",
984 path.display(),
985 sym_name
986 );
987 Err(err.into())
988 }
989 }
990 }
991 }
992
993 fn inject_panic_runtime(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
994 let only_rlib = tcx.crate_types().iter().all(|ct| *ct == CrateType::Rlib);
997 if only_rlib {
998 info!("panic runtime injection skipped, only generating rlib");
999 return;
1000 }
1001
1002 let mut needs_panic_runtime = attr::contains_name(&krate.attrs, sym::needs_panic_runtime);
1006 for (_cnum, data) in self.iter_crate_data() {
1007 needs_panic_runtime |= data.needs_panic_runtime();
1008 }
1009
1010 if !needs_panic_runtime {
1013 return;
1014 }
1015
1016 let desired_strategy = tcx.sess.panic_strategy();
1026 let name = match desired_strategy {
1027 PanicStrategy::Unwind => sym::panic_unwind,
1028 PanicStrategy::Abort => sym::panic_abort,
1029 PanicStrategy::ImmediateAbort => {
1030 return;
1032 }
1033 };
1034 info!("panic runtime not found -- loading {}", name);
1035
1036 let Some(cnum) =
1037 self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1038 else {
1039 return;
1040 };
1041 let data = self.get_crate_data(cnum);
1042
1043 if !data.is_panic_runtime() {
1046 tcx.dcx().emit_err(errors::CrateNotPanicRuntime { crate_name: name });
1047 }
1048 if data.required_panic_strategy() != Some(desired_strategy) {
1049 tcx.dcx()
1050 .emit_err(errors::NoPanicStrategy { crate_name: name, strategy: desired_strategy });
1051 }
1052
1053 self.injected_panic_runtime = Some(cnum);
1054 }
1055
1056 fn inject_profiler_runtime(&mut self, tcx: TyCtxt<'_>) {
1057 let needs_profiler_runtime =
1058 tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled();
1059 if !needs_profiler_runtime || tcx.sess.opts.unstable_opts.no_profiler_runtime {
1060 return;
1061 }
1062
1063 info!("loading profiler");
1064
1065 let name = Symbol::intern(&tcx.sess.opts.unstable_opts.profiler_runtime);
1066 let Some(cnum) =
1067 self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1068 else {
1069 return;
1070 };
1071 let data = self.get_crate_data(cnum);
1072
1073 if !data.is_profiler_runtime() {
1075 tcx.dcx().emit_err(errors::NotProfilerRuntime { crate_name: name });
1076 }
1077 }
1078
1079 fn inject_allocator_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1080 self.has_global_allocator =
1081 match &*fn_spans(krate, Symbol::intern(&global_fn_name(sym::alloc))) {
1082 [span1, span2, ..] => {
1083 tcx.dcx()
1084 .emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
1085 true
1086 }
1087 spans => !spans.is_empty(),
1088 };
1089 let alloc_error_handler = Symbol::intern(&global_fn_name(ALLOC_ERROR_HANDLER));
1090 self.has_alloc_error_handler = match &*fn_spans(krate, alloc_error_handler) {
1091 [span1, span2, ..] => {
1092 tcx.dcx()
1093 .emit_err(errors::NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 });
1094 true
1095 }
1096 spans => !spans.is_empty(),
1097 };
1098
1099 if !attr::contains_name(&krate.attrs, sym::needs_allocator)
1103 && !self.iter_crate_data().any(|(_, data)| data.needs_allocator())
1104 {
1105 return;
1106 }
1107
1108 let all_rlib = tcx.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
1112 if all_rlib {
1113 return;
1114 }
1115
1116 #[allow(rustc::symbol_intern_string_literal)]
1124 let this_crate = Symbol::intern("this crate");
1125
1126 let mut global_allocator = self.has_global_allocator.then_some(this_crate);
1127 for (_, data) in self.iter_crate_data() {
1128 if data.has_global_allocator() {
1129 match global_allocator {
1130 Some(other_crate) => {
1131 tcx.dcx().emit_err(errors::ConflictingGlobalAlloc {
1132 crate_name: data.name(),
1133 other_crate_name: other_crate,
1134 });
1135 }
1136 None => global_allocator = Some(data.name()),
1137 }
1138 }
1139 }
1140 let mut alloc_error_handler = self.has_alloc_error_handler.then_some(this_crate);
1141 for (_, data) in self.iter_crate_data() {
1142 if data.has_alloc_error_handler() {
1143 match alloc_error_handler {
1144 Some(other_crate) => {
1145 tcx.dcx().emit_err(errors::ConflictingAllocErrorHandler {
1146 crate_name: data.name(),
1147 other_crate_name: other_crate,
1148 });
1149 }
1150 None => alloc_error_handler = Some(data.name()),
1151 }
1152 }
1153 }
1154
1155 if global_allocator.is_some() {
1156 self.allocator_kind = Some(AllocatorKind::Global);
1157 } else {
1158 if !attr::contains_name(&krate.attrs, sym::default_lib_allocator)
1163 && !self.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
1164 {
1165 tcx.dcx().emit_err(errors::GlobalAllocRequired);
1166 }
1167 self.allocator_kind = Some(AllocatorKind::Default);
1168 }
1169
1170 if alloc_error_handler.is_some() {
1171 self.alloc_error_handler_kind = Some(AllocatorKind::Global);
1172 } else {
1173 self.alloc_error_handler_kind = Some(AllocatorKind::Default);
1176 }
1177 }
1178
1179 fn inject_forced_externs(&mut self, tcx: TyCtxt<'_>) {
1180 for (name, entry) in tcx.sess.opts.externs.iter() {
1181 if entry.force {
1182 let name_interned = Symbol::intern(name);
1183 if !self.used_extern_options.contains(&name_interned) {
1184 self.resolve_crate(
1185 tcx,
1186 name_interned,
1187 DUMMY_SP,
1188 CrateDepKind::Explicit,
1189 CrateOrigin::Extern,
1190 );
1191 }
1192 }
1193 }
1194 }
1195
1196 fn inject_compiler_builtins(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1198 if attr::contains_name(&krate.attrs, sym::compiler_builtins)
1200 || attr::contains_name(&krate.attrs, sym::no_core)
1201 {
1202 info!("`compiler_builtins` unneeded");
1203 return;
1204 }
1205
1206 for (cnum, cmeta) in self.iter_crate_data() {
1209 if cmeta.is_compiler_builtins() {
1210 info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
1211 return;
1212 }
1213 }
1214
1215 let Some(cnum) = self.resolve_crate(
1217 tcx,
1218 sym::compiler_builtins,
1219 krate.spans.inner_span.shrink_to_lo(),
1220 CrateDepKind::Explicit,
1221 CrateOrigin::Injected,
1222 ) else {
1223 info!("`compiler_builtins` not resolved");
1224 return;
1225 };
1226
1227 let cmeta = self.get_crate_data(cnum);
1229 if !cmeta.is_compiler_builtins() {
1230 tcx.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
1231 }
1232 }
1233
1234 fn report_unused_deps_in_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1235 let span = krate.spans.inner_span.shrink_to_lo();
1237 for (name, entry) in tcx.sess.opts.externs.iter() {
1239 if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
1240 continue;
1242 }
1243 if entry.nounused_dep || entry.force {
1244 continue;
1246 }
1247 let name_interned = Symbol::intern(name);
1248 if self.used_extern_options.contains(&name_interned) {
1249 continue;
1250 }
1251
1252 if tcx.sess.opts.json_unused_externs.is_enabled() {
1254 self.unused_externs.push(name_interned);
1255 continue;
1256 }
1257
1258 tcx.sess.psess.buffer_lint(
1259 lint::builtin::UNUSED_CRATE_DEPENDENCIES,
1260 span,
1261 ast::CRATE_NODE_ID,
1262 BuiltinLintDiag::UnusedCrateDependency {
1263 extern_crate: name_interned,
1264 local_crate: tcx.crate_name(LOCAL_CRATE),
1265 },
1266 );
1267 }
1268 }
1269
1270 fn report_future_incompatible_deps(&self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1271 let name = tcx.crate_name(LOCAL_CRATE);
1272
1273 if name.as_str() == "wasm_bindgen" {
1274 let major = env::var("CARGO_PKG_VERSION_MAJOR")
1275 .ok()
1276 .and_then(|major| u64::from_str(&major).ok());
1277 let minor = env::var("CARGO_PKG_VERSION_MINOR")
1278 .ok()
1279 .and_then(|minor| u64::from_str(&minor).ok());
1280 let patch = env::var("CARGO_PKG_VERSION_PATCH")
1281 .ok()
1282 .and_then(|patch| u64::from_str(&patch).ok());
1283
1284 match (major, minor, patch) {
1285 (Some(1..), _, _) => return,
1287 (Some(0), Some(3..), _) => return,
1289 (Some(0), Some(2), Some(88..)) => return,
1291 (None, None, None) => return,
1293 _ => (),
1294 }
1295
1296 let span = krate.spans.inner_span.shrink_to_lo();
1298
1299 tcx.sess.dcx().emit_err(errors::WasmCAbi { span });
1300 }
1301 }
1302
1303 pub fn postprocess(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1304 self.inject_compiler_builtins(tcx, krate);
1305 self.inject_forced_externs(tcx);
1306 self.inject_profiler_runtime(tcx);
1307 self.inject_allocator_crate(tcx, krate);
1308 self.inject_panic_runtime(tcx, krate);
1309
1310 self.report_unused_deps_in_crate(tcx, krate);
1311 self.report_future_incompatible_deps(tcx, krate);
1312
1313 info!("{:?}", CrateDump(self));
1314 }
1315
1316 pub fn process_extern_crate(
1318 &mut self,
1319 tcx: TyCtxt<'_>,
1320 item: &ast::Item,
1321 def_id: LocalDefId,
1322 definitions: &Definitions,
1323 ) -> Option<CrateNum> {
1324 match item.kind {
1325 ast::ItemKind::ExternCrate(orig_name, ident) => {
1326 debug!("resolving extern crate stmt. ident: {} orig_name: {:?}", ident, orig_name);
1327 let name = match orig_name {
1328 Some(orig_name) => {
1329 validate_crate_name(tcx.sess, orig_name, Some(item.span));
1330 orig_name
1331 }
1332 None => ident.name,
1333 };
1334 let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
1335 CrateDepKind::MacrosOnly
1336 } else {
1337 CrateDepKind::Explicit
1338 };
1339
1340 let cnum =
1341 self.resolve_crate(tcx, name, item.span, dep_kind, CrateOrigin::Extern)?;
1342
1343 let path_len = definitions.def_path(def_id).data.len();
1344 self.update_extern_crate(
1345 cnum,
1346 name,
1347 ExternCrate {
1348 src: ExternCrateSource::Extern(def_id.to_def_id()),
1349 span: item.span,
1350 path_len,
1351 dependency_of: LOCAL_CRATE,
1352 },
1353 );
1354 Some(cnum)
1355 }
1356 _ => bug!(),
1357 }
1358 }
1359
1360 pub fn process_path_extern(
1361 &mut self,
1362 tcx: TyCtxt<'_>,
1363 name: Symbol,
1364 span: Span,
1365 ) -> Option<CrateNum> {
1366 let cnum =
1367 self.resolve_crate(tcx, name, span, CrateDepKind::Explicit, CrateOrigin::Extern)?;
1368
1369 self.update_extern_crate(
1370 cnum,
1371 name,
1372 ExternCrate {
1373 src: ExternCrateSource::Path,
1374 span,
1375 path_len: usize::MAX,
1377 dependency_of: LOCAL_CRATE,
1378 },
1379 );
1380
1381 Some(cnum)
1382 }
1383
1384 pub fn maybe_process_path_extern(&mut self, tcx: TyCtxt<'_>, name: Symbol) -> Option<CrateNum> {
1385 self.maybe_resolve_crate(tcx, name, CrateDepKind::Explicit, CrateOrigin::Extern).ok()
1386 }
1387}
1388
1389fn fn_spans(krate: &ast::Crate, name: Symbol) -> Vec<Span> {
1390 struct Finder {
1391 name: Symbol,
1392 spans: Vec<Span>,
1393 }
1394 impl<'ast> visit::Visitor<'ast> for Finder {
1395 fn visit_item(&mut self, item: &'ast ast::Item) {
1396 if let Some(ident) = item.kind.ident()
1397 && ident.name == self.name
1398 && attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
1399 {
1400 self.spans.push(item.span);
1401 }
1402 visit::walk_item(self, item)
1403 }
1404 }
1405
1406 let mut f = Finder { name, spans: Vec::new() };
1407 visit::walk_crate(&mut f, krate);
1408 f.spans
1409}
1410
1411fn format_dlopen_err(e: &(dyn std::error::Error + 'static)) -> String {
1412 e.sources().map(|e| format!(": {e}")).collect()
1413}
1414
1415fn attempt_load_dylib(path: &Path) -> Result<libloading::Library, libloading::Error> {
1416 #[cfg(target_os = "aix")]
1417 if let Some(ext) = path.extension()
1418 && ext.eq("a")
1419 {
1420 let library_name = path.file_stem().expect("expect a library name");
1424 let mut archive_member = std::ffi::OsString::from("a(");
1425 archive_member.push(library_name);
1426 archive_member.push(".so)");
1427 let new_path = path.with_extension(archive_member);
1428
1429 let flags = libc::RTLD_LAZY | libc::RTLD_LOCAL | libc::RTLD_MEMBER;
1431 return unsafe { libloading::os::unix::Library::open(Some(&new_path), flags) }
1432 .map(|lib| lib.into());
1433 }
1434
1435 unsafe { libloading::Library::new(&path) }
1436}
1437
1438fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, String> {
1443 assert!(max_attempts > 0);
1444
1445 let mut last_error = None;
1446
1447 for attempt in 0..max_attempts {
1448 debug!("Attempt to load proc-macro `{}`.", path.display());
1449 match attempt_load_dylib(path) {
1450 Ok(lib) => {
1451 if attempt > 0 {
1452 debug!(
1453 "Loaded proc-macro `{}` after {} attempts.",
1454 path.display(),
1455 attempt + 1
1456 );
1457 }
1458 return Ok(lib);
1459 }
1460 Err(err) => {
1461 if !matches!(err, libloading::Error::LoadLibraryExW { .. }) {
1463 debug!("Failed to load proc-macro `{}`. Not retrying", path.display());
1464 let err = format_dlopen_err(&err);
1465 if let Some(err) = err.strip_prefix(&format!(": {}", path.display())) {
1468 return Err(err.to_string());
1469 }
1470 return Err(err);
1471 }
1472
1473 last_error = Some(err);
1474 std::thread::sleep(Duration::from_millis(100));
1475 debug!("Failed to load proc-macro `{}`. Retrying.", path.display());
1476 }
1477 }
1478 }
1479
1480 debug!("Failed to load proc-macro `{}` even after {} attempts.", path.display(), max_attempts);
1481
1482 let last_error = last_error.unwrap();
1483 let message = if let Some(src) = last_error.source() {
1484 format!("{} ({src}) (retried {max_attempts} times)", format_dlopen_err(&last_error))
1485 } else {
1486 format!("{} (retried {max_attempts} times)", format_dlopen_err(&last_error))
1487 };
1488 Err(message)
1489}
1490
1491pub enum DylibError {
1492 DlOpen(String, String),
1493 DlSym(String, String),
1494}
1495
1496impl From<DylibError> for CrateError {
1497 fn from(err: DylibError) -> CrateError {
1498 match err {
1499 DylibError::DlOpen(path, err) => CrateError::DlOpen(path, err),
1500 DylibError::DlSym(path, err) => CrateError::DlSym(path, err),
1501 }
1502 }
1503}
1504
1505pub unsafe fn load_symbol_from_dylib<T: Copy>(
1506 path: &Path,
1507 sym_name: &str,
1508) -> Result<T, DylibError> {
1509 let path = try_canonicalize(path).unwrap();
1511 let lib =
1512 load_dylib(&path, 5).map_err(|err| DylibError::DlOpen(path.display().to_string(), err))?;
1513
1514 let sym = unsafe { lib.get::<T>(sym_name.as_bytes()) }
1515 .map_err(|err| DylibError::DlSym(path.display().to_string(), format_dlopen_err(&err)))?;
1516
1517 let sym = unsafe { sym.into_raw() };
1520 std::mem::forget(lib);
1521
1522 Ok(*sym)
1523}