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::{AllocatorKind, alloc_error_handler_name, 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.opts, 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.opts, 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.opts, 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.opts, 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.opts, 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 tcx.sess,
687 self,
688 metadata,
689 crate_root,
690 raw_proc_macros,
691 cnum,
692 cnum_map,
693 dep_kind,
694 source,
695 private_dep,
696 host_hash,
697 );
698
699 self.set_crate_data(cnum, crate_metadata);
700
701 Ok(cnum)
702 }
703
704 fn load_proc_macro<'a, 'b>(
705 &self,
706 sess: &'a Session,
707 locator: &mut CrateLocator<'b>,
708 crate_rejections: &mut CrateRejections,
709 path_kind: PathKind,
710 host_hash: Option<Svh>,
711 ) -> Result<Option<(LoadResult, Option<Library>)>, CrateError>
712 where
713 'a: 'b,
714 {
715 if sess.opts.unstable_opts.dual_proc_macros {
716 let mut proc_macro_locator = locator.clone();
719
720 proc_macro_locator.for_target_proc_macro(sess, path_kind);
722
723 let target_result =
725 match self.load(&mut proc_macro_locator, &mut CrateRejections::default())? {
726 Some(LoadResult::Previous(cnum)) => {
727 return Ok(Some((LoadResult::Previous(cnum), None)));
728 }
729 Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)),
730 None => return Ok(None),
731 };
732
733 *crate_rejections = CrateRejections::default();
736
737 locator.for_proc_macro(sess, path_kind);
739
740 locator.hash = host_hash;
741
742 let Some(host_result) = self.load(locator, crate_rejections)? else {
743 return Ok(None);
744 };
745
746 let host_result = match host_result {
747 LoadResult::Previous(..) => {
748 panic!("host and target proc macros must be loaded in lock-step")
749 }
750 LoadResult::Loaded(library) => library,
751 };
752 Ok(Some((target_result.unwrap(), Some(host_result))))
753 } else {
754 let mut proc_macro_locator = locator.clone();
757
758 proc_macro_locator.for_proc_macro(sess, path_kind);
760
761 let Some(host_result) =
762 self.load(&mut proc_macro_locator, &mut CrateRejections::default())?
763 else {
764 return Ok(None);
765 };
766
767 Ok(Some((host_result, None)))
768 }
769 }
770
771 fn resolve_crate<'tcx>(
772 &mut self,
773 tcx: TyCtxt<'tcx>,
774 name: Symbol,
775 span: Span,
776 dep_kind: CrateDepKind,
777 origin: CrateOrigin<'_>,
778 ) -> Option<CrateNum> {
779 self.used_extern_options.insert(name);
780 match self.maybe_resolve_crate(tcx, name, dep_kind, origin) {
781 Ok(cnum) => {
782 self.set_used_recursively(cnum);
783 Some(cnum)
784 }
785 Err(err) => {
786 debug!("failed to resolve crate {} {:?}", name, dep_kind);
787 let missing_core = self
788 .maybe_resolve_crate(
789 tcx,
790 sym::core,
791 CrateDepKind::Explicit,
792 CrateOrigin::Extern,
793 )
794 .is_err();
795 err.report(tcx.sess, span, missing_core);
796 None
797 }
798 }
799 }
800
801 fn maybe_resolve_crate<'b, 'tcx>(
802 &'b mut self,
803 tcx: TyCtxt<'tcx>,
804 name: Symbol,
805 mut dep_kind: CrateDepKind,
806 origin: CrateOrigin<'b>,
807 ) -> Result<CrateNum, CrateError> {
808 info!("resolving crate `{}`", name);
809 if !name.as_str().is_ascii() {
810 return Err(CrateError::NonAsciiName(name));
811 }
812
813 let dep_root = origin.dep_root();
814 let dep = origin.dep();
815 let hash = dep.map(|d| d.hash);
816 let host_hash = dep.map(|d| d.host_hash).flatten();
817 let extra_filename = dep.map(|d| &d.extra_filename[..]);
818 let path_kind = if dep.is_some() { PathKind::Dependency } else { PathKind::Crate };
819 let private_dep = origin.private_dep();
820
821 let result = if let Some(cnum) =
822 self.existing_match(&tcx.sess.opts.externs, name, hash, path_kind)
823 {
824 (LoadResult::Previous(cnum), None)
825 } else {
826 info!("falling back to a load");
827 let mut locator = CrateLocator::new(
828 tcx.sess,
829 &*self.metadata_loader,
830 name,
831 tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
834 hash,
835 extra_filename,
836 path_kind,
837 );
838 let mut crate_rejections = CrateRejections::default();
839
840 match self.load(&mut locator, &mut crate_rejections)? {
841 Some(res) => (res, None),
842 None => {
843 info!("falling back to loading proc_macro");
844 dep_kind = CrateDepKind::MacrosOnly;
845 match self.load_proc_macro(
846 tcx.sess,
847 &mut locator,
848 &mut crate_rejections,
849 path_kind,
850 host_hash,
851 )? {
852 Some(res) => res,
853 None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
854 }
855 }
856 }
857 };
858
859 match result {
860 (LoadResult::Previous(cnum), None) => {
861 info!("library for `{}` was loaded previously, cnum {cnum}", name);
862 let private_dep =
867 self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
868 let data = self.get_crate_data_mut(cnum);
869 if data.is_proc_macro_crate() {
870 dep_kind = CrateDepKind::MacrosOnly;
871 }
872 data.set_dep_kind(cmp::max(data.dep_kind(), dep_kind));
873 data.update_and_private_dep(private_dep);
874 Ok(cnum)
875 }
876 (LoadResult::Loaded(library), host_library) => {
877 info!("register newly loaded library for `{}`", name);
878 self.register_crate(tcx, host_library, origin, library, dep_kind, name, private_dep)
879 }
880 _ => panic!(),
881 }
882 }
883
884 fn load(
885 &self,
886 locator: &CrateLocator<'_>,
887 crate_rejections: &mut CrateRejections,
888 ) -> Result<Option<LoadResult>, CrateError> {
889 let Some(library) = locator.maybe_load_library_crate(crate_rejections)? else {
890 return Ok(None);
891 };
892
893 let root = library.metadata.get_root();
898 let mut result = LoadResult::Loaded(library);
899 for (cnum, data) in self.iter_crate_data() {
900 if data.name() == root.name() && root.hash() == data.hash() {
901 assert!(locator.hash.is_none());
902 info!("load success, going to previous cnum: {}", cnum);
903 result = LoadResult::Previous(cnum);
904 break;
905 }
906 }
907 Ok(Some(result))
908 }
909
910 fn resolve_crate_deps(
912 &mut self,
913 tcx: TyCtxt<'_>,
914 dep_root: &CratePaths,
915 crate_root: &CrateRoot,
916 metadata: &MetadataBlob,
917 krate: CrateNum,
918 dep_kind: CrateDepKind,
919 parent_is_private: bool,
920 ) -> Result<CrateNumMap, CrateError> {
921 debug!(
922 "resolving deps of external crate `{}` with dep root `{}`",
923 crate_root.name(),
924 dep_root.name
925 );
926 if crate_root.is_proc_macro_crate() {
927 return Ok(CrateNumMap::new());
928 }
929
930 let deps = crate_root.decode_crate_deps(metadata);
934 let mut crate_num_map = CrateNumMap::with_capacity(1 + deps.len());
935 crate_num_map.push(krate);
936 for dep in deps {
937 info!(
938 "resolving dep `{}`->`{}` hash: `{}` extra filename: `{}` private {}",
939 crate_root.name(),
940 dep.name,
941 dep.hash,
942 dep.extra_filename,
943 dep.is_private,
944 );
945 let dep_kind = match dep_kind {
946 CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly,
947 _ => dep.kind,
948 };
949 let cnum = self.maybe_resolve_crate(
950 tcx,
951 dep.name,
952 dep_kind,
953 CrateOrigin::IndirectDependency {
954 dep_root,
955 parent_private: parent_is_private,
956 dep: &dep,
957 },
958 )?;
959 crate_num_map.push(cnum);
960 }
961
962 debug!("resolve_crate_deps: cnum_map for {:?} is {:?}", krate, crate_num_map);
963 Ok(crate_num_map)
964 }
965
966 fn dlsym_proc_macros(
967 &self,
968 sess: &Session,
969 path: &Path,
970 stable_crate_id: StableCrateId,
971 ) -> Result<&'static [ProcMacro], CrateError> {
972 let sym_name = sess.generate_proc_macro_decls_symbol(stable_crate_id);
973 debug!("trying to dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
974
975 unsafe {
976 let result = load_symbol_from_dylib::<*const &[ProcMacro]>(path, &sym_name);
977 match result {
978 Ok(result) => {
979 debug!("loaded dlsym proc_macros {} for symbol `{}`", path.display(), sym_name);
980 Ok(*result)
981 }
982 Err(err) => {
983 debug!(
984 "failed to dlsym proc_macros {} for symbol `{}`",
985 path.display(),
986 sym_name
987 );
988 Err(err.into())
989 }
990 }
991 }
992 }
993
994 fn inject_panic_runtime(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
995 let only_rlib = tcx.crate_types().iter().all(|ct| *ct == CrateType::Rlib);
998 if only_rlib {
999 info!("panic runtime injection skipped, only generating rlib");
1000 return;
1001 }
1002
1003 let mut needs_panic_runtime = attr::contains_name(&krate.attrs, sym::needs_panic_runtime);
1007 for (_cnum, data) in self.iter_crate_data() {
1008 needs_panic_runtime |= data.needs_panic_runtime();
1009 }
1010
1011 if !needs_panic_runtime {
1014 return;
1015 }
1016
1017 let desired_strategy = tcx.sess.panic_strategy();
1027 let name = match desired_strategy {
1028 PanicStrategy::Unwind => sym::panic_unwind,
1029 PanicStrategy::Abort => sym::panic_abort,
1030 };
1031 info!("panic runtime not found -- loading {}", name);
1032
1033 let Some(cnum) =
1034 self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1035 else {
1036 return;
1037 };
1038 let data = self.get_crate_data(cnum);
1039
1040 if !data.is_panic_runtime() {
1043 tcx.dcx().emit_err(errors::CrateNotPanicRuntime { crate_name: name });
1044 }
1045 if data.required_panic_strategy() != Some(desired_strategy) {
1046 tcx.dcx()
1047 .emit_err(errors::NoPanicStrategy { crate_name: name, strategy: desired_strategy });
1048 }
1049
1050 self.injected_panic_runtime = Some(cnum);
1051 }
1052
1053 fn inject_profiler_runtime(&mut self, tcx: TyCtxt<'_>) {
1054 let needs_profiler_runtime =
1055 tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled();
1056 if !needs_profiler_runtime || tcx.sess.opts.unstable_opts.no_profiler_runtime {
1057 return;
1058 }
1059
1060 info!("loading profiler");
1061
1062 let name = Symbol::intern(&tcx.sess.opts.unstable_opts.profiler_runtime);
1063 let Some(cnum) =
1064 self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1065 else {
1066 return;
1067 };
1068 let data = self.get_crate_data(cnum);
1069
1070 if !data.is_profiler_runtime() {
1072 tcx.dcx().emit_err(errors::NotProfilerRuntime { crate_name: name });
1073 }
1074 }
1075
1076 fn inject_allocator_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1077 self.has_global_allocator =
1078 match &*fn_spans(krate, Symbol::intern(&global_fn_name(sym::alloc))) {
1079 [span1, span2, ..] => {
1080 tcx.dcx()
1081 .emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
1082 true
1083 }
1084 spans => !spans.is_empty(),
1085 };
1086 self.has_alloc_error_handler = match &*fn_spans(
1087 krate,
1088 Symbol::intern(alloc_error_handler_name(AllocatorKind::Global)),
1089 ) {
1090 [span1, span2, ..] => {
1091 tcx.dcx()
1092 .emit_err(errors::NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 });
1093 true
1094 }
1095 spans => !spans.is_empty(),
1096 };
1097
1098 if !attr::contains_name(&krate.attrs, sym::needs_allocator)
1102 && !self.iter_crate_data().any(|(_, data)| data.needs_allocator())
1103 {
1104 return;
1105 }
1106
1107 let all_rlib = tcx.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
1111 if all_rlib {
1112 return;
1113 }
1114
1115 #[allow(rustc::symbol_intern_string_literal)]
1123 let this_crate = Symbol::intern("this crate");
1124
1125 let mut global_allocator = self.has_global_allocator.then_some(this_crate);
1126 for (_, data) in self.iter_crate_data() {
1127 if data.has_global_allocator() {
1128 match global_allocator {
1129 Some(other_crate) => {
1130 tcx.dcx().emit_err(errors::ConflictingGlobalAlloc {
1131 crate_name: data.name(),
1132 other_crate_name: other_crate,
1133 });
1134 }
1135 None => global_allocator = Some(data.name()),
1136 }
1137 }
1138 }
1139 let mut alloc_error_handler = self.has_alloc_error_handler.then_some(this_crate);
1140 for (_, data) in self.iter_crate_data() {
1141 if data.has_alloc_error_handler() {
1142 match alloc_error_handler {
1143 Some(other_crate) => {
1144 tcx.dcx().emit_err(errors::ConflictingAllocErrorHandler {
1145 crate_name: data.name(),
1146 other_crate_name: other_crate,
1147 });
1148 }
1149 None => alloc_error_handler = Some(data.name()),
1150 }
1151 }
1152 }
1153
1154 if global_allocator.is_some() {
1155 self.allocator_kind = Some(AllocatorKind::Global);
1156 } else {
1157 if !attr::contains_name(&krate.attrs, sym::default_lib_allocator)
1162 && !self.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator())
1163 {
1164 tcx.dcx().emit_err(errors::GlobalAllocRequired);
1165 }
1166 self.allocator_kind = Some(AllocatorKind::Default);
1167 }
1168
1169 if alloc_error_handler.is_some() {
1170 self.alloc_error_handler_kind = Some(AllocatorKind::Global);
1171 } else {
1172 self.alloc_error_handler_kind = Some(AllocatorKind::Default);
1175 }
1176 }
1177
1178 fn inject_forced_externs(&mut self, tcx: TyCtxt<'_>) {
1179 for (name, entry) in tcx.sess.opts.externs.iter() {
1180 if entry.force {
1181 let name_interned = Symbol::intern(name);
1182 if !self.used_extern_options.contains(&name_interned) {
1183 self.resolve_crate(
1184 tcx,
1185 name_interned,
1186 DUMMY_SP,
1187 CrateDepKind::Explicit,
1188 CrateOrigin::Extern,
1189 );
1190 }
1191 }
1192 }
1193 }
1194
1195 fn inject_compiler_builtins(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1197 if attr::contains_name(&krate.attrs, sym::compiler_builtins)
1199 || attr::contains_name(&krate.attrs, sym::no_core)
1200 {
1201 info!("`compiler_builtins` unneeded");
1202 return;
1203 }
1204
1205 for (cnum, cmeta) in self.iter_crate_data() {
1208 if cmeta.is_compiler_builtins() {
1209 info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
1210 return;
1211 }
1212 }
1213
1214 let Some(cnum) = self.resolve_crate(
1216 tcx,
1217 sym::compiler_builtins,
1218 krate.spans.inner_span.shrink_to_lo(),
1219 CrateDepKind::Explicit,
1220 CrateOrigin::Injected,
1221 ) else {
1222 info!("`compiler_builtins` not resolved");
1223 return;
1224 };
1225
1226 let cmeta = self.get_crate_data(cnum);
1228 if !cmeta.is_compiler_builtins() {
1229 tcx.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
1230 }
1231 }
1232
1233 fn report_unused_deps_in_crate(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1234 let span = krate.spans.inner_span.shrink_to_lo();
1236 for (name, entry) in tcx.sess.opts.externs.iter() {
1238 if let ExternLocation::FoundInLibrarySearchDirectories = entry.location {
1239 continue;
1241 }
1242 if entry.nounused_dep || entry.force {
1243 continue;
1245 }
1246 let name_interned = Symbol::intern(name);
1247 if self.used_extern_options.contains(&name_interned) {
1248 continue;
1249 }
1250
1251 if tcx.sess.opts.json_unused_externs.is_enabled() {
1253 self.unused_externs.push(name_interned);
1254 continue;
1255 }
1256
1257 tcx.sess.psess.buffer_lint(
1258 lint::builtin::UNUSED_CRATE_DEPENDENCIES,
1259 span,
1260 ast::CRATE_NODE_ID,
1261 BuiltinLintDiag::UnusedCrateDependency {
1262 extern_crate: name_interned,
1263 local_crate: tcx.crate_name(LOCAL_CRATE),
1264 },
1265 );
1266 }
1267 }
1268
1269 fn report_future_incompatible_deps(&self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1270 let name = tcx.crate_name(LOCAL_CRATE);
1271
1272 if name.as_str() == "wasm_bindgen" {
1273 let major = env::var("CARGO_PKG_VERSION_MAJOR")
1274 .ok()
1275 .and_then(|major| u64::from_str(&major).ok());
1276 let minor = env::var("CARGO_PKG_VERSION_MINOR")
1277 .ok()
1278 .and_then(|minor| u64::from_str(&minor).ok());
1279 let patch = env::var("CARGO_PKG_VERSION_PATCH")
1280 .ok()
1281 .and_then(|patch| u64::from_str(&patch).ok());
1282
1283 match (major, minor, patch) {
1284 (Some(1..), _, _) => return,
1286 (Some(0), Some(3..), _) => return,
1288 (Some(0), Some(2), Some(88..)) => return,
1290 (None, None, None) => return,
1292 _ => (),
1293 }
1294
1295 let span = krate.spans.inner_span.shrink_to_lo();
1297
1298 tcx.sess.dcx().emit_err(errors::WasmCAbi { span });
1299 }
1300 }
1301
1302 pub fn postprocess(&mut self, tcx: TyCtxt<'_>, krate: &ast::Crate) {
1303 self.inject_compiler_builtins(tcx, krate);
1304 self.inject_forced_externs(tcx);
1305 self.inject_profiler_runtime(tcx);
1306 self.inject_allocator_crate(tcx, krate);
1307 self.inject_panic_runtime(tcx, krate);
1308
1309 self.report_unused_deps_in_crate(tcx, krate);
1310 self.report_future_incompatible_deps(tcx, krate);
1311
1312 info!("{:?}", CrateDump(self));
1313 }
1314
1315 pub fn process_extern_crate(
1317 &mut self,
1318 tcx: TyCtxt<'_>,
1319 item: &ast::Item,
1320 def_id: LocalDefId,
1321 definitions: &Definitions,
1322 ) -> Option<CrateNum> {
1323 match item.kind {
1324 ast::ItemKind::ExternCrate(orig_name, ident) => {
1325 debug!("resolving extern crate stmt. ident: {} orig_name: {:?}", ident, orig_name);
1326 let name = match orig_name {
1327 Some(orig_name) => {
1328 validate_crate_name(tcx.sess, orig_name, Some(item.span));
1329 orig_name
1330 }
1331 None => ident.name,
1332 };
1333 let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
1334 CrateDepKind::MacrosOnly
1335 } else {
1336 CrateDepKind::Explicit
1337 };
1338
1339 let cnum =
1340 self.resolve_crate(tcx, name, item.span, dep_kind, CrateOrigin::Extern)?;
1341
1342 let path_len = definitions.def_path(def_id).data.len();
1343 self.update_extern_crate(
1344 cnum,
1345 name,
1346 ExternCrate {
1347 src: ExternCrateSource::Extern(def_id.to_def_id()),
1348 span: item.span,
1349 path_len,
1350 dependency_of: LOCAL_CRATE,
1351 },
1352 );
1353 Some(cnum)
1354 }
1355 _ => bug!(),
1356 }
1357 }
1358
1359 pub fn process_path_extern(
1360 &mut self,
1361 tcx: TyCtxt<'_>,
1362 name: Symbol,
1363 span: Span,
1364 ) -> Option<CrateNum> {
1365 let cnum =
1366 self.resolve_crate(tcx, name, span, CrateDepKind::Explicit, CrateOrigin::Extern)?;
1367
1368 self.update_extern_crate(
1369 cnum,
1370 name,
1371 ExternCrate {
1372 src: ExternCrateSource::Path,
1373 span,
1374 path_len: usize::MAX,
1376 dependency_of: LOCAL_CRATE,
1377 },
1378 );
1379
1380 Some(cnum)
1381 }
1382
1383 pub fn maybe_process_path_extern(&mut self, tcx: TyCtxt<'_>, name: Symbol) -> Option<CrateNum> {
1384 self.maybe_resolve_crate(tcx, name, CrateDepKind::Explicit, CrateOrigin::Extern).ok()
1385 }
1386}
1387
1388fn fn_spans(krate: &ast::Crate, name: Symbol) -> Vec<Span> {
1389 struct Finder {
1390 name: Symbol,
1391 spans: Vec<Span>,
1392 }
1393 impl<'ast> visit::Visitor<'ast> for Finder {
1394 fn visit_item(&mut self, item: &'ast ast::Item) {
1395 if let Some(ident) = item.kind.ident()
1396 && ident.name == self.name
1397 && attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
1398 {
1399 self.spans.push(item.span);
1400 }
1401 visit::walk_item(self, item)
1402 }
1403 }
1404
1405 let mut f = Finder { name, spans: Vec::new() };
1406 visit::walk_crate(&mut f, krate);
1407 f.spans
1408}
1409
1410fn format_dlopen_err(e: &(dyn std::error::Error + 'static)) -> String {
1411 e.sources().map(|e| format!(": {e}")).collect()
1412}
1413
1414fn attempt_load_dylib(path: &Path) -> Result<libloading::Library, libloading::Error> {
1415 #[cfg(target_os = "aix")]
1416 if let Some(ext) = path.extension()
1417 && ext.eq("a")
1418 {
1419 let library_name = path.file_stem().expect("expect a library name");
1423 let mut archive_member = std::ffi::OsString::from("a(");
1424 archive_member.push(library_name);
1425 archive_member.push(".so)");
1426 let new_path = path.with_extension(archive_member);
1427
1428 let flags = libc::RTLD_LAZY | libc::RTLD_LOCAL | libc::RTLD_MEMBER;
1430 return unsafe { libloading::os::unix::Library::open(Some(&new_path), flags) }
1431 .map(|lib| lib.into());
1432 }
1433
1434 unsafe { libloading::Library::new(&path) }
1435}
1436
1437fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, String> {
1442 assert!(max_attempts > 0);
1443
1444 let mut last_error = None;
1445
1446 for attempt in 0..max_attempts {
1447 debug!("Attempt to load proc-macro `{}`.", path.display());
1448 match attempt_load_dylib(path) {
1449 Ok(lib) => {
1450 if attempt > 0 {
1451 debug!(
1452 "Loaded proc-macro `{}` after {} attempts.",
1453 path.display(),
1454 attempt + 1
1455 );
1456 }
1457 return Ok(lib);
1458 }
1459 Err(err) => {
1460 if !matches!(err, libloading::Error::LoadLibraryExW { .. }) {
1462 debug!("Failed to load proc-macro `{}`. Not retrying", path.display());
1463 let err = format_dlopen_err(&err);
1464 if let Some(err) = err.strip_prefix(&format!(": {}", path.display())) {
1467 return Err(err.to_string());
1468 }
1469 return Err(err);
1470 }
1471
1472 last_error = Some(err);
1473 std::thread::sleep(Duration::from_millis(100));
1474 debug!("Failed to load proc-macro `{}`. Retrying.", path.display());
1475 }
1476 }
1477 }
1478
1479 debug!("Failed to load proc-macro `{}` even after {} attempts.", path.display(), max_attempts);
1480
1481 let last_error = last_error.unwrap();
1482 let message = if let Some(src) = last_error.source() {
1483 format!("{} ({src}) (retried {max_attempts} times)", format_dlopen_err(&last_error))
1484 } else {
1485 format!("{} (retried {max_attempts} times)", format_dlopen_err(&last_error))
1486 };
1487 Err(message)
1488}
1489
1490pub enum DylibError {
1491 DlOpen(String, String),
1492 DlSym(String, String),
1493}
1494
1495impl From<DylibError> for CrateError {
1496 fn from(err: DylibError) -> CrateError {
1497 match err {
1498 DylibError::DlOpen(path, err) => CrateError::DlOpen(path, err),
1499 DylibError::DlSym(path, err) => CrateError::DlSym(path, err),
1500 }
1501 }
1502}
1503
1504pub unsafe fn load_symbol_from_dylib<T: Copy>(
1505 path: &Path,
1506 sym_name: &str,
1507) -> Result<T, DylibError> {
1508 let path = try_canonicalize(path).unwrap();
1510 let lib =
1511 load_dylib(&path, 5).map_err(|err| DylibError::DlOpen(path.display().to_string(), err))?;
1512
1513 let sym = unsafe { lib.get::<T>(sym_name.as_bytes()) }
1514 .map_err(|err| DylibError::DlSym(path.display().to_string(), format_dlopen_err(&err)))?;
1515
1516 let sym = unsafe { sym.into_raw() };
1519 std::mem::forget(lib);
1520
1521 Ok(*sym)
1522}