rustc_interface/
util.rs

1use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
2use std::path::{Path, PathBuf};
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::{Arc, OnceLock};
5use std::{env, thread};
6
7use rustc_ast as ast;
8use rustc_codegen_ssa::traits::CodegenBackend;
9use rustc_data_structures::jobserver::Proxy;
10use rustc_data_structures::sync;
11use rustc_metadata::{DylibError, load_symbol_from_dylib};
12use rustc_middle::ty::CurrentGcx;
13use rustc_parse::validate_attr;
14use rustc_session::config::{Cfg, OutFileName, OutputFilenames, OutputTypes, Sysroot, host_tuple};
15use rustc_session::lint::{self, BuiltinLintDiag, LintBuffer};
16use rustc_session::output::{CRATE_TYPES, categorize_crate_type};
17use rustc_session::{EarlyDiagCtxt, Session, filesearch};
18use rustc_span::edit_distance::find_best_match_for_name;
19use rustc_span::edition::Edition;
20use rustc_span::source_map::SourceMapInputs;
21use rustc_span::{SessionGlobals, Symbol, sym};
22use rustc_target::spec::Target;
23use tracing::info;
24
25use crate::errors;
26
27/// Function pointer type that constructs a new CodegenBackend.
28type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
29
30/// Adds `target_feature = "..."` cfgs for a variety of platform
31/// specific features (SSE, NEON etc.).
32///
33/// This is performed by checking whether a set of permitted features
34/// is available on the target machine, by querying the codegen backend.
35pub(crate) fn add_configuration(
36    cfg: &mut Cfg,
37    sess: &mut Session,
38    codegen_backend: &dyn CodegenBackend,
39) {
40    let tf = sym::target_feature;
41    let tf_cfg = codegen_backend.target_config(sess);
42
43    sess.unstable_target_features.extend(tf_cfg.unstable_target_features.iter().copied());
44    sess.target_features.extend(tf_cfg.target_features.iter().copied());
45
46    cfg.extend(tf_cfg.target_features.into_iter().map(|feat| (tf, Some(feat))));
47
48    if tf_cfg.has_reliable_f16 {
49        cfg.insert((sym::target_has_reliable_f16, None));
50    }
51    if tf_cfg.has_reliable_f16_math {
52        cfg.insert((sym::target_has_reliable_f16_math, None));
53    }
54    if tf_cfg.has_reliable_f128 {
55        cfg.insert((sym::target_has_reliable_f128, None));
56    }
57    if tf_cfg.has_reliable_f128_math {
58        cfg.insert((sym::target_has_reliable_f128_math, None));
59    }
60
61    if sess.crt_static(None) {
62        cfg.insert((tf, Some(sym::crt_dash_static)));
63    }
64}
65
66/// Ensures that all target features required by the ABI are present.
67/// Must be called after `unstable_target_features` has been populated!
68pub(crate) fn check_abi_required_features(sess: &Session) {
69    let abi_feature_constraints = sess.target.abi_required_features();
70    // We check this against `unstable_target_features` as that is conveniently already
71    // back-translated to rustc feature names, taking into account `-Ctarget-cpu` and `-Ctarget-feature`.
72    // Just double-check that the features we care about are actually on our list.
73    for feature in
74        abi_feature_constraints.required.iter().chain(abi_feature_constraints.incompatible.iter())
75    {
76        assert!(
77            sess.target.rust_target_features().iter().any(|(name, ..)| feature == name),
78            "target feature {feature} is required/incompatible for the current ABI but not a recognized feature for this target"
79        );
80    }
81
82    for feature in abi_feature_constraints.required {
83        if !sess.unstable_target_features.contains(&Symbol::intern(feature)) {
84            sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "enabled" });
85        }
86    }
87    for feature in abi_feature_constraints.incompatible {
88        if sess.unstable_target_features.contains(&Symbol::intern(feature)) {
89            sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "disabled" });
90        }
91    }
92}
93
94pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
95pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
96
97fn init_stack_size(early_dcx: &EarlyDiagCtxt) -> usize {
98    // Obey the environment setting or default
99    *STACK_SIZE.get_or_init(|| {
100        env::var_os("RUST_MIN_STACK")
101            .as_ref()
102            .map(|os_str| os_str.to_string_lossy())
103            // if someone finds out `export RUST_MIN_STACK=640000` isn't enough stack
104            // they might try to "unset" it by running `RUST_MIN_STACK=  rustc code.rs`
105            // this is wrong, but std would nonetheless "do what they mean", so let's do likewise
106            .filter(|s| !s.trim().is_empty())
107            // rustc is a batch program, so error early on inputs which are unlikely to be intended
108            // so no one thinks we parsed them setting `RUST_MIN_STACK="64 megabytes"`
109            // FIXME: we could accept `RUST_MIN_STACK=64MB`, perhaps?
110            .map(|s| {
111                let s = s.trim();
112                // FIXME(workingjubilee): add proper diagnostics when we factor out "pre-run" setup
113                #[allow(rustc::untranslatable_diagnostic, rustc::diagnostic_outside_of_impl)]
114                s.parse::<usize>().unwrap_or_else(|_| {
115                    let mut err = early_dcx.early_struct_fatal(format!(
116                        r#"`RUST_MIN_STACK` should be a number of bytes, but was "{s}""#,
117                    ));
118                    err.note("you can also unset `RUST_MIN_STACK` to use the default stack size");
119                    err.emit()
120                })
121            })
122            // otherwise pick a consistent default
123            .unwrap_or(DEFAULT_STACK_SIZE)
124    })
125}
126
127fn run_in_thread_with_globals<F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send, R: Send>(
128    thread_stack_size: usize,
129    edition: Edition,
130    sm_inputs: SourceMapInputs,
131    extra_symbols: &[&'static str],
132    f: F,
133) -> R {
134    // The "thread pool" is a single spawned thread in the non-parallel
135    // compiler. We run on a spawned thread instead of the main thread (a) to
136    // provide control over the stack size, and (b) to increase similarity with
137    // the parallel compiler, in particular to ensure there is no accidental
138    // sharing of data between the main thread and the compilation thread
139    // (which might cause problems for the parallel compiler).
140    let builder = thread::Builder::new().name("rustc".to_string()).stack_size(thread_stack_size);
141
142    // We build the session globals and run `f` on the spawned thread, because
143    // `SessionGlobals` does not impl `Send` in the non-parallel compiler.
144    thread::scope(|s| {
145        // `unwrap` is ok here because `spawn_scoped` only panics if the thread
146        // name contains null bytes.
147        let r = builder
148            .spawn_scoped(s, move || {
149                rustc_span::create_session_globals_then(
150                    edition,
151                    extra_symbols,
152                    Some(sm_inputs),
153                    || f(CurrentGcx::new(), Proxy::new()),
154                )
155            })
156            .unwrap()
157            .join();
158
159        match r {
160            Ok(v) => v,
161            Err(e) => std::panic::resume_unwind(e),
162        }
163    })
164}
165
166pub(crate) fn run_in_thread_pool_with_globals<
167    F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send,
168    R: Send,
169>(
170    thread_builder_diag: &EarlyDiagCtxt,
171    edition: Edition,
172    threads: usize,
173    extra_symbols: &[&'static str],
174    sm_inputs: SourceMapInputs,
175    f: F,
176) -> R {
177    use std::process;
178
179    use rustc_data_structures::defer;
180    use rustc_data_structures::sync::FromDyn;
181    use rustc_middle::ty::tls;
182    use rustc_query_impl::QueryCtxt;
183    use rustc_query_system::query::{QueryContext, break_query_cycles};
184
185    let thread_stack_size = init_stack_size(thread_builder_diag);
186
187    let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
188
189    if !sync::is_dyn_thread_safe() {
190        return run_in_thread_with_globals(
191            thread_stack_size,
192            edition,
193            sm_inputs,
194            extra_symbols,
195            |current_gcx, jobserver_proxy| {
196                // Register the thread for use with the `WorkerLocal` type.
197                registry.register();
198
199                f(current_gcx, jobserver_proxy)
200            },
201        );
202    }
203
204    let current_gcx = FromDyn::from(CurrentGcx::new());
205    let current_gcx2 = current_gcx.clone();
206
207    let proxy = Proxy::new();
208
209    let proxy_ = Arc::clone(&proxy);
210    let proxy__ = Arc::clone(&proxy);
211    let builder = rustc_thread_pool::ThreadPoolBuilder::new()
212        .thread_name(|_| "rustc".to_string())
213        .acquire_thread_handler(move || proxy_.acquire_thread())
214        .release_thread_handler(move || proxy__.release_thread())
215        .num_threads(threads)
216        .deadlock_handler(move || {
217            // On deadlock, creates a new thread and forwards information in thread
218            // locals to it. The new thread runs the deadlock handler.
219
220            let current_gcx2 = current_gcx2.clone();
221            let registry = rustc_thread_pool::Registry::current();
222            let session_globals = rustc_span::with_session_globals(|session_globals| {
223                session_globals as *const SessionGlobals as usize
224            });
225            thread::Builder::new()
226                .name("rustc query cycle handler".to_string())
227                .spawn(move || {
228                    let on_panic = defer(|| {
229                        eprintln!("internal compiler error: query cycle handler thread panicked, aborting process");
230                        // We need to abort here as we failed to resolve the deadlock,
231                        // otherwise the compiler could just hang,
232                        process::abort();
233                    });
234
235                    // Get a `GlobalCtxt` reference from `CurrentGcx` as we cannot rely on having a
236                    // `TyCtxt` TLS reference here.
237                    current_gcx2.access(|gcx| {
238                        tls::enter_context(&tls::ImplicitCtxt::new(gcx), || {
239                            tls::with(|tcx| {
240                                // Accessing session globals is sound as they outlive `GlobalCtxt`.
241                                // They are needed to hash query keys containing spans or symbols.
242                                let query_map = rustc_span::set_session_globals_then(unsafe { &*(session_globals as *const SessionGlobals) }, || {
243                                    // Ensure there was no errors collecting all active jobs.
244                                    // We need the complete map to ensure we find a cycle to break.
245                                    QueryCtxt::new(tcx).collect_active_jobs().ok().expect("failed to collect active queries in deadlock handler")
246                                });
247                                break_query_cycles(query_map, &registry);
248                            })
249                        })
250                    });
251
252                    on_panic.disable();
253                })
254                .unwrap();
255        })
256        .stack_size(thread_stack_size);
257
258    // We create the session globals on the main thread, then create the thread
259    // pool. Upon creation, each worker thread created gets a copy of the
260    // session globals in TLS. This is possible because `SessionGlobals` impls
261    // `Send` in the parallel compiler.
262    rustc_span::create_session_globals_then(edition, extra_symbols, Some(sm_inputs), || {
263        rustc_span::with_session_globals(|session_globals| {
264            let session_globals = FromDyn::from(session_globals);
265            builder
266                .build_scoped(
267                    // Initialize each new worker thread when created.
268                    move |thread: rustc_thread_pool::ThreadBuilder| {
269                        // Register the thread for use with the `WorkerLocal` type.
270                        registry.register();
271
272                        rustc_span::set_session_globals_then(session_globals.into_inner(), || {
273                            thread.run()
274                        })
275                    },
276                    // Run `f` on the first thread in the thread pool.
277                    move |pool: &rustc_thread_pool::ThreadPool| {
278                        pool.install(|| f(current_gcx.into_inner(), proxy))
279                    },
280                )
281                .unwrap()
282        })
283    })
284}
285
286#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
287fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBackendFn {
288    match unsafe { load_symbol_from_dylib::<MakeBackendFn>(path, "__rustc_codegen_backend") } {
289        Ok(backend_sym) => backend_sym,
290        Err(DylibError::DlOpen(path, err)) => {
291            let err = format!("couldn't load codegen backend {path}{err}");
292            early_dcx.early_fatal(err);
293        }
294        Err(DylibError::DlSym(_path, err)) => {
295            let e = format!(
296                "`__rustc_codegen_backend` symbol lookup in the codegen backend failed{err}",
297            );
298            early_dcx.early_fatal(e);
299        }
300    }
301}
302
303/// Get the codegen backend based on the name and specified sysroot.
304///
305/// A name of `None` indicates that the default backend should be used.
306pub fn get_codegen_backend(
307    early_dcx: &EarlyDiagCtxt,
308    sysroot: &Sysroot,
309    backend_name: Option<&str>,
310    target: &Target,
311) -> Box<dyn CodegenBackend> {
312    static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
313
314    let load = LOAD.get_or_init(|| {
315        let backend = backend_name
316            .or(target.default_codegen_backend.as_deref())
317            .or(option_env!("CFG_DEFAULT_CODEGEN_BACKEND"))
318            .unwrap_or("llvm");
319
320        match backend {
321            filename if filename.contains('.') => {
322                load_backend_from_dylib(early_dcx, filename.as_ref())
323            }
324            #[cfg(feature = "llvm")]
325            "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
326            backend_name => get_codegen_sysroot(early_dcx, sysroot, backend_name),
327        }
328    });
329
330    // SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen
331    // backend we hope that the backend links against the same rustc_driver version. If this is not
332    // the case, we get UB.
333    unsafe { load() }
334}
335
336// This is used for rustdoc, but it uses similar machinery to codegen backend
337// loading, so we leave the code here. It is potentially useful for other tools
338// that want to invoke the rustc binary while linking to rustc as well.
339pub fn rustc_path<'a>(sysroot: &Sysroot) -> Option<&'a Path> {
340    static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
341
342    RUSTC_PATH
343        .get_or_init(|| {
344            let candidate = sysroot
345                .default
346                .join(env!("RUSTC_INSTALL_BINDIR"))
347                .join(if cfg!(target_os = "windows") { "rustc.exe" } else { "rustc" });
348            candidate.exists().then_some(candidate)
349        })
350        .as_deref()
351}
352
353#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
354fn get_codegen_sysroot(
355    early_dcx: &EarlyDiagCtxt,
356    sysroot: &Sysroot,
357    backend_name: &str,
358) -> MakeBackendFn {
359    // For now we only allow this function to be called once as it'll dlopen a
360    // few things, which seems to work best if we only do that once. In
361    // general this assertion never trips due to the once guard in `get_codegen_backend`,
362    // but there's a few manual calls to this function in this file we protect
363    // against.
364    static LOADED: AtomicBool = AtomicBool::new(false);
365    assert!(
366        !LOADED.fetch_or(true, Ordering::SeqCst),
367        "cannot load the default codegen backend twice"
368    );
369
370    let target = host_tuple();
371
372    let sysroot = sysroot
373        .all_paths()
374        .map(|sysroot| {
375            filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
376        })
377        .find(|f| {
378            info!("codegen backend candidate: {}", f.display());
379            f.exists()
380        })
381        .unwrap_or_else(|| {
382            let candidates = sysroot
383                .all_paths()
384                .map(|p| p.display().to_string())
385                .collect::<Vec<_>>()
386                .join("\n* ");
387            let err = format!(
388                "failed to find a `codegen-backends` folder \
389                           in the sysroot candidates:\n* {candidates}"
390            );
391            early_dcx.early_fatal(err);
392        });
393
394    info!("probing {} for a codegen backend", sysroot.display());
395
396    let d = sysroot.read_dir().unwrap_or_else(|e| {
397        let err = format!(
398            "failed to load default codegen backend, couldn't \
399                           read `{}`: {}",
400            sysroot.display(),
401            e
402        );
403        early_dcx.early_fatal(err);
404    });
405
406    let mut file: Option<PathBuf> = None;
407
408    let expected_names = &[
409        format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
410        format!("rustc_codegen_{backend_name}"),
411    ];
412    for entry in d.filter_map(|e| e.ok()) {
413        let path = entry.path();
414        let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue };
415        if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) {
416            continue;
417        }
418        let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
419        if !expected_names.iter().any(|expected| expected == name) {
420            continue;
421        }
422        if let Some(ref prev) = file {
423            let err = format!(
424                "duplicate codegen backends found\n\
425                               first:  {}\n\
426                               second: {}\n\
427            ",
428                prev.display(),
429                path.display()
430            );
431            early_dcx.early_fatal(err);
432        }
433        file = Some(path.clone());
434    }
435
436    match file {
437        Some(ref s) => load_backend_from_dylib(early_dcx, s),
438        None => {
439            let err = format!("unsupported builtin codegen backend `{backend_name}`");
440            early_dcx.early_fatal(err);
441        }
442    }
443}
444
445pub(crate) fn check_attr_crate_type(
446    sess: &Session,
447    attrs: &[ast::Attribute],
448    lint_buffer: &mut LintBuffer,
449) {
450    // Unconditionally collect crate types from attributes to make them used
451    for a in attrs.iter() {
452        if a.has_name(sym::crate_type) {
453            if let Some(n) = a.value_str() {
454                if categorize_crate_type(n).is_some() {
455                    return;
456                }
457
458                if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
459                    let span = spanned.span;
460                    let candidate = find_best_match_for_name(
461                        &CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
462                        n,
463                        None,
464                    );
465                    lint_buffer.buffer_lint(
466                        lint::builtin::UNKNOWN_CRATE_TYPES,
467                        ast::CRATE_NODE_ID,
468                        span,
469                        BuiltinLintDiag::UnknownCrateTypes { span, candidate },
470                    );
471                }
472            } else {
473                // This is here mainly to check for using a macro, such as
474                // `#![crate_type = foo!()]`. That is not supported since the
475                // crate type needs to be known very early in compilation long
476                // before expansion. Otherwise, validation would normally be
477                // caught during semantic analysis via `TyCtxt::check_mod_attrs`,
478                // but by the time that runs the macro is expanded, and it doesn't
479                // give an error.
480                validate_attr::emit_fatal_malformed_builtin_attribute(
481                    &sess.psess,
482                    a,
483                    sym::crate_type,
484                );
485            }
486        }
487    }
488}
489
490fn multiple_output_types_to_stdout(
491    output_types: &OutputTypes,
492    single_output_file_is_stdout: bool,
493) -> bool {
494    use std::io::IsTerminal;
495    if std::io::stdout().is_terminal() {
496        // If stdout is a tty, check if multiple text output types are
497        // specified by `--emit foo=- --emit bar=-` or `-o - --emit foo,bar`
498        let named_text_types = output_types
499            .iter()
500            .filter(|(f, o)| f.is_text_output() && *o == &Some(OutFileName::Stdout))
501            .count();
502        let unnamed_text_types =
503            output_types.iter().filter(|(f, o)| f.is_text_output() && o.is_none()).count();
504        named_text_types > 1 || unnamed_text_types > 1 && single_output_file_is_stdout
505    } else {
506        // Otherwise, all the output types should be checked
507        let named_types =
508            output_types.values().filter(|o| *o == &Some(OutFileName::Stdout)).count();
509        let unnamed_types = output_types.values().filter(|o| o.is_none()).count();
510        named_types > 1 || unnamed_types > 1 && single_output_file_is_stdout
511    }
512}
513
514pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
515    if multiple_output_types_to_stdout(
516        &sess.opts.output_types,
517        sess.io.output_file == Some(OutFileName::Stdout),
518    ) {
519        sess.dcx().emit_fatal(errors::MultipleOutputTypesToStdout);
520    }
521
522    let crate_name = sess
523        .opts
524        .crate_name
525        .clone()
526        .or_else(|| rustc_attr_parsing::find_crate_name(attrs).map(|n| n.to_string()));
527
528    match sess.io.output_file {
529        None => {
530            // "-" as input file will cause the parser to read from stdin so we
531            // have to make up a name
532            // We want to toss everything after the final '.'
533            let dirpath = sess.io.output_dir.clone().unwrap_or_default();
534
535            // If a crate name is present, we use it as the link name
536            let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
537
538            OutputFilenames::new(
539                dirpath,
540                crate_name.unwrap_or_else(|| stem.replace('-', "_")),
541                stem,
542                None,
543                sess.io.temps_dir.clone(),
544                sess.opts.cg.extra_filename.clone(),
545                sess.opts.output_types.clone(),
546            )
547        }
548
549        Some(ref out_file) => {
550            let unnamed_output_types =
551                sess.opts.output_types.values().filter(|a| a.is_none()).count();
552            let ofile = if unnamed_output_types > 1 {
553                sess.dcx().emit_warn(errors::MultipleOutputTypesAdaption);
554                None
555            } else {
556                if !sess.opts.cg.extra_filename.is_empty() {
557                    sess.dcx().emit_warn(errors::IgnoringExtraFilename);
558                }
559                Some(out_file.clone())
560            };
561            if sess.io.output_dir != None {
562                sess.dcx().emit_warn(errors::IgnoringOutDir);
563            }
564
565            let out_filestem =
566                out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
567            OutputFilenames::new(
568                out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
569                crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
570                out_filestem,
571                ofile,
572                sess.io.temps_dir.clone(),
573                sess.opts.cg.extra_filename.clone(),
574                sess.opts.output_types.clone(),
575            )
576        }
577    }
578}
579
580/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" when invoked by an in-tree tool.
581pub macro version_str() {
582    option_env!("CFG_VERSION")
583}
584
585/// Returns the version string for `rustc` itself (which may be different from a tool version).
586pub fn rustc_version_str() -> Option<&'static str> {
587    version_str!()
588}