Skip to content

Commit

Permalink
Target modifiers (special marked options) are recorded in metainfo an…
Browse files Browse the repository at this point in the history
…d compared to be equal in different crates
  • Loading branch information
azhogin committed Nov 18, 2024
1 parent bca5fde commit bd52a23
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 6 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ fn configure_and_expand(

resolver.resolve_crate(&krate);

CStore::from_tcx(tcx).report_incompatible_target_modifiers(tcx, &krate, resolver.lint_buffer());
krate
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ lint_improper_ctypes_union_layout_help = consider adding a `#[repr(C)]` or `#[re
lint_improper_ctypes_union_layout_reason = this union has unspecified layout
lint_improper_ctypes_union_non_exhaustive = this union is non-exhaustive
lint_incompatible_target_modifiers = crate `{$local_crate}` has incompatible target modifier with extern crate `{$extern_crate}`: `{$tmod}`
lint_incomplete_include =
include macro expected single expression in source
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag)
}
BuiltinLintDiag::WasmCAbi => lints::WasmCAbi.decorate_lint(diag),
BuiltinLintDiag::IncompatibleTargetModifiers { extern_crate, local_crate, tmod } => {
lints::IncompatibleTargetModifiers { extern_crate, local_crate, tmod }
.decorate_lint(diag)
}
BuiltinLintDiag::IllFormedAttributeInput { suggestions } => {
lints::IllFormedAttributeInput {
num_suggestions: suggestions.len(),
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,14 @@ pub(crate) struct UnusedCrateDependency {
pub local_crate: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(lint_incompatible_target_modifiers)]
pub(crate) struct IncompatibleTargetModifiers {
pub extern_crate: Symbol,
pub local_crate: Symbol,
pub tmod: String,
}

#[derive(LintDiagnostic)]
#[diag(lint_wasm_c_abi)]
pub(crate) struct WasmCAbi;
Expand Down
32 changes: 32 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,38 @@ declare_lint! {
crate_level_only
}

declare_lint! {
/// The `incompatible_target_modifiers` lint detects crates with incompatible target modifiers
/// (abi-changing or vulnerability-affecting flags).
///
/// ### Example
///
/// When main and dependency crates are compiled with `-Zregparm=1` and `-Zregparm=2` correspondingly.
///
/// This will produce:
///
/// ```text
/// error: crate `incompatible_regparm` has incompatible target modifier with extern crate `wrong_regparm`: `regparm = ( Some(1) | Some(2) )`
/// --> $DIR/incompatible_regparm.rs:5:1
/// |
/// 1 | #![no_core]
/// | ^
/// |
/// = note: `#[deny(incompatible_target_modifiers)]` on by default
/// ```
///
/// ### Explanation
///
/// `Target modifiers` are compilation flags that affects abi or vulnerability resistance.
/// Linking together crates with incompatible target modifiers would produce incorrect code
/// or degradation of vulnerability resistance.
/// So this lint should find such inconsistency.
///
pub INCOMPATIBLE_TARGET_MODIFIERS,
Deny,
"Incompatible target modifiers"
}

declare_lint! {
/// The `unused_qualifications` lint detects unnecessarily qualified
/// names.
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ pub enum BuiltinLintDiag {
AvoidUsingIntelSyntax,
AvoidUsingAttSyntax,
IncompleteInclude,
IncompatibleTargetModifiers {
extern_crate: Symbol,
local_crate: Symbol,
tmod: String,
},
UnnameableTestItems,
DuplicateMacroAttribute,
CfgAttrNoAttributes,
Expand Down
100 changes: 98 additions & 2 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_middle::bug;
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
use rustc_session::config::{self, CrateType, ExternLocation};
use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateSource};
use rustc_session::lint::{self, BuiltinLintDiag};
use rustc_session::lint::{self, BuiltinLintDiag, LintBuffer};
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_span::edition::Edition;
Expand All @@ -35,7 +35,9 @@ use tracing::{debug, info, trace};

use crate::errors;
use crate::locator::{CrateError, CrateLocator, CratePaths};
use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob};
use crate::rmeta::{
CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob, TargetModifiers,
};

/// The backend's way to give the crate store access to the metadata in a library.
/// Note that it returns the raw metadata bytes stored in the library file, whether
Expand Down Expand Up @@ -290,6 +292,79 @@ impl CStore {
}
}

pub fn report_incompatible_target_modifiers(
&self,
tcx: TyCtxt<'_>,
krate: &Crate,
lints: &mut LintBuffer,
) {
if tcx.crate_types().contains(&CrateType::ProcMacro) {
return;
}
let sess = tcx.sess;
let span = krate.spans.inner_span.shrink_to_lo();

let splitter = |v: &String| {
let splitted: Vec<_> = v.split("=").collect();
(splitted[0].to_string(), splitted[1].to_string())
};
let name = tcx.crate_name(LOCAL_CRATE);
let mods = sess.opts.gather_target_modifiers();
for (_cnum, data) in self.iter_crate_data() {
if data.is_proc_macro_crate() {
continue;
}
let mut report_diff = |tmod: String| {
lints.buffer_lint(
lint::builtin::INCOMPATIBLE_TARGET_MODIFIERS,
ast::CRATE_NODE_ID,
span,
BuiltinLintDiag::IncompatibleTargetModifiers {
extern_crate: data.name(),
local_crate: name,
tmod,
},
);
};
let mut it1 = mods.iter().map(splitter);
let mut it2 = data.target_modifiers().map(splitter);
let mut left_name_val: Option<(String, String)> = None;
let mut right_name_val: Option<(String, String)> = None;
loop {
left_name_val = left_name_val.or_else(|| it1.next());
right_name_val = right_name_val.or_else(|| it2.next());
match (&left_name_val, &right_name_val) {
(Some(l), Some(r)) => match l.0.cmp(&r.0) {
cmp::Ordering::Equal => {
if l.1 != r.1 {
report_diff(format!("{} = ( {} | {} )", l.0, l.1, r.1));
}
left_name_val = None;
right_name_val = None;
}
cmp::Ordering::Greater => {
report_diff(format!("{} = ( * | {} )", r.0, r.1));
right_name_val = None;
}
cmp::Ordering::Less => {
report_diff(format!("{} = ( {} | * )", l.0, l.1));
left_name_val = None;
}
},
(Some(l), None) => {
report_diff(format!("{} = ( {} | * )", l.0, l.1));
left_name_val = None;
}
(None, Some(r)) => {
report_diff(format!("{} = ( * | {} )", r.0, r.1));
right_name_val = None;
}
(None, None) => break,
}
}
}
}

pub fn new(metadata_loader: Box<MetadataLoaderDyn>) -> CStore {
CStore {
metadata_loader,
Expand Down Expand Up @@ -432,6 +507,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
};

let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, dep_kind)?;
let target_modifiers = self.resolve_target_modifiers(&crate_root, &metadata, cnum)?;

let raw_proc_macros = if crate_root.is_proc_macro_crate() {
let temp_root;
Expand All @@ -456,6 +532,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
raw_proc_macros,
cnum,
cnum_map,
target_modifiers,
dep_kind,
source,
private_dep,
Expand Down Expand Up @@ -689,6 +766,25 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
Ok(crate_num_map)
}

fn resolve_target_modifiers(
&mut self,
crate_root: &CrateRoot,
metadata: &MetadataBlob,
krate: CrateNum,
) -> Result<TargetModifiers, CrateError> {
debug!("resolving target modifiers of external crate");
if crate_root.is_proc_macro_crate() {
return Ok(TargetModifiers::new());
}
let mods = crate_root.decode_target_modifiers(metadata);
let mut target_modifiers = TargetModifiers::with_capacity(mods.len());
for modifier in mods {
target_modifiers.push(modifier);
}
debug!("resolve_target_modifiers: target mods for {:?} is {:?}", krate, target_modifiers);
Ok(target_modifiers)
}

fn dlsym_proc_macros(
&self,
path: &Path,
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl MetadataBlob {
/// own crate numbers.
pub(crate) type CrateNumMap = IndexVec<CrateNum, CrateNum>;

/// Target modifiers - abi / vulnerability-resist affecting flags
pub(crate) type TargetModifiers = Vec<String>;

pub(crate) struct CrateMetadata {
/// The primary crate data - binary metadata blob.
blob: MetadataBlob,
Expand Down Expand Up @@ -110,6 +113,8 @@ pub(crate) struct CrateMetadata {
cnum_map: CrateNumMap,
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
dependencies: Vec<CrateNum>,
/// Target modifiers - abi and vulnerability-resist affecting flags the crate was compiled with
target_modifiers: TargetModifiers,
/// How to link (or not link) this crate to the currently compiled crate.
dep_kind: CrateDepKind,
/// Filesystem location of this crate.
Expand Down Expand Up @@ -960,6 +965,13 @@ impl CrateRoot {
) -> impl ExactSizeIterator<Item = CrateDep> + Captures<'a> {
self.crate_deps.decode(metadata)
}

pub(crate) fn decode_target_modifiers<'a>(
&self,
metadata: &'a MetadataBlob,
) -> impl ExactSizeIterator<Item = String> + Captures<'a> {
self.target_modifiers.decode(metadata)
}
}

impl<'a> CrateMetadataRef<'a> {
Expand Down Expand Up @@ -1815,6 +1827,7 @@ impl CrateMetadata {
raw_proc_macros: Option<&'static [ProcMacro]>,
cnum: CrateNum,
cnum_map: CrateNumMap,
target_modifiers: TargetModifiers,
dep_kind: CrateDepKind,
source: CrateSource,
private_dep: bool,
Expand Down Expand Up @@ -1846,6 +1859,7 @@ impl CrateMetadata {
cnum,
cnum_map,
dependencies,
target_modifiers,
dep_kind,
source: Lrc::new(source),
private_dep,
Expand Down Expand Up @@ -1875,6 +1889,10 @@ impl CrateMetadata {
self.dependencies.push(cnum);
}

pub(crate) fn target_modifiers(&self) -> impl Iterator<Item = &String> + '_ {
self.target_modifiers.iter()
}

pub(crate) fn update_extern_crate(&mut self, new_extern_crate: ExternCrate) -> bool {
let update =
Some(new_extern_crate.rank()) > self.extern_crate.as_ref().map(ExternCrate::rank);
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// Encode source_map. This needs to be done last, because encoding `Span`s tells us which
// `SourceFiles` we actually need to encode.
let source_map = stat!("source-map", || self.encode_source_map());
let target_modifiers = stat!("target-modifiers", || self.encode_target_modifiers());

let root = stat!("final", || {
let attrs = tcx.hir().krate_attrs();
Expand Down Expand Up @@ -732,6 +733,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
native_libraries,
foreign_modules,
source_map,
target_modifiers,
traits,
impls,
incoherent_impls,
Expand Down Expand Up @@ -1978,6 +1980,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.lazy_array(deps.iter().map(|(_, dep)| dep))
}

fn encode_target_modifiers(&mut self) -> LazyArray<String> {
empty_proc_macro!(self);
let tcx = self.tcx;
self.lazy_array(tcx.sess.opts.gather_target_modifiers())
}

fn encode_lib_features(&mut self) -> LazyArray<(Symbol, FeatureStability)> {
empty_proc_macro!(self);
let tcx = self.tcx;
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::marker::PhantomData;
use std::num::NonZero;

pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob};
pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob, TargetModifiers};
use decoder::{DecodeContext, Metadata};
use def_path_hash_map::DefPathHashMapRef;
use encoder::EncodeContext;
Expand Down Expand Up @@ -283,6 +283,7 @@ pub(crate) struct CrateRoot {
def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,

source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
target_modifiers: LazyArray<String>,

compiler_builtins: bool,
needs_allocator: bool,
Expand Down
Loading

0 comments on commit bd52a23

Please sign in to comment.