Skip to content

Include rustc version in crate disambiguator #85142

Closed
@bjorn3

Description

@bjorn3
Member

Otherwise with -Zsymbol-mangling-version=v0 and no -Cmetadata, symbols from the same crate compiled with different versions of rustc are named the same. This makes it possible to replace a rust dylib compiled with one version of rustc with one compiled by another version of rustc even if the ABI doesn't match.

The crate disambiguator is calculated at

pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
use std::hash::Hasher;
// The crate_disambiguator is a 128 bit hash. The disambiguator is fed
// into various other hashes quite a bit (symbol hashes, incr. comp. hashes,
// debuginfo type IDs, etc), so we don't want it to be too wide. 128 bits
// should still be safe enough to avoid collisions in practice.
let mut hasher = StableHasher::new();
let mut metadata = session.opts.cg.metadata.clone();
// We don't want the crate_disambiguator to dependent on the order
// -C metadata arguments, so sort them:
metadata.sort();
// Every distinct -C metadata value is only incorporated once:
metadata.dedup();
hasher.write(b"metadata");
for s in &metadata {
// Also incorporate the length of a metadata string, so that we generate
// different values for `-Cmetadata=ab -Cmetadata=c` and
// `-Cmetadata=a -Cmetadata=bc`
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}
// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
let is_exe = session.crate_types().contains(&CrateType::Executable);
hasher.write(if is_exe { b"exe" } else { b"lib" });
CrateDisambiguator::from(hasher.finish::<Fingerprint>())
}
This should include something like option_env!("CFG_VERSION").unwrap_or("unknown version") in the hash. This matches what the version info included in the rustc metadata header.

Activity

added
A-linkageArea: linking into static, shared libraries and binaries
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on May 10, 2021
pierwill

pierwill commented on Oct 12, 2021

@pierwill
Member

@rustbot claim

pierwill

pierwill commented on Oct 12, 2021

@pierwill
Member

Looks like this code has moved. Still looking into what happened.

pierwill

pierwill commented on Oct 13, 2021

@pierwill
Member

The crate disambiguator was moved to rustc_span::StableCrateId in #85804.

pierwill

pierwill commented on Oct 13, 2021

@pierwill
Member

This looks like the relevant code:

pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec<String>) -> StableCrateId {
use std::hash::Hash;
use std::hash::Hasher;
let mut hasher = StableHasher::new();
crate_name.hash(&mut hasher);
// We don't want the stable crate id to dependent on the order
// -C metadata arguments, so sort them:
metadata.sort();
// Every distinct -C metadata value is only incorporated once:
metadata.dedup();
hasher.write(b"metadata");
for s in &metadata {
// Also incorporate the length of a metadata string, so that we generate
// different values for `-Cmetadata=ab -Cmetadata=c` and
// `-Cmetadata=a -Cmetadata=bc`
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}
// Also incorporate crate type, so that we don't get symbol conflicts when
// linking against a library of the same name, if this is an executable.
hasher.write(if is_exe { b"exe" } else { b"lib" });
StableCrateId(hasher.finish())
}

added a commit that references this issue on Nov 21, 2021

Rollup merge of rust-lang#89836 - pierwill:fix-85142-crate-hash, r=bj…

88a40a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-linkageArea: linking into static, shared libraries and binariesC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @bjorn3@pierwill

    Issue actions

      Include rustc version in crate disambiguator · Issue #85142 · rust-lang/rust