rustc_interface/
util.rs

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