Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashStable caching does not take into account StableHashingContext settings #92266

Closed
Aaron1011 opened this issue Dec 24, 2021 · 0 comments · Fixed by #92278
Closed

HashStable caching does not take into account StableHashingContext settings #92266

Aaron1011 opened this issue Dec 24, 2021 · 0 comments · Fixed by #92278
Labels
A-incr-comp Area: Incremental compilation C-bug Category: This is a bug.

Comments

@Aaron1011
Copy link
Member

Aaron1011 commented Dec 24, 2021

In some places, we have HashStable implementations that cache a computed Fingerprint to avoid re-computing it:

impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
where
T: HashStable<StableHashingContext<'a>>,
{
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
thread_local! {
static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
RefCell::new(Default::default());
}

impl<'a> HashStable<StableHashingContext<'a>> for AdtDef {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
thread_local! {
static CACHE: RefCell<FxHashMap<usize, Fingerprint>> = Default::default();
}

pub fn fresh(mut expn_data: ExpnData, ctx: impl HashStableContext) -> LocalExpnId {
debug_assert_eq!(expn_data.parent.krate, LOCAL_CRATE);
let expn_hash = update_disambiguator(&mut expn_data, ctx);

However, this does not take into account that the StableHashingContext may be configured to hash certain things different. Spans can be skipped using hcx.while_hashing_spans, and HirIds can be skipped using hcx.with_node_id_hashing_mode. These settings are not used in the cache key, so we will end up caching using whatever settings are used the first time the structure is hashed. As a result, later uses of hcx.while_hashing_spans and hcx.with_node_id_hashing_mode may end up using an incorrect cache entry.

In particular, we need to ignore spans while computing the legacy symbol hash:

hcx.while_hashing_spans(false, |hcx| {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
item_type.hash_stable(hcx, &mut hasher);
});
});

If we've already populated one of the caches with a value that included hashed spans, then our symbol hash might actually take spans into account, even though we've explicitly requested that it should not.

We need to either include the relevant setting in the cache key, or enforce that we only ever try to hash the structure (e.g. ExpnData) with the same settings.

Discovered while working on #92210

@Aaron1011 Aaron1011 added A-incr-comp Area: Incremental compilation C-bug Category: This is a bug. labels Dec 24, 2021
@Aaron1011 Aaron1011 changed the title HashStable caching does take into account StableHashingContext settings HashStable caching does not take into account StableHashingContext settings Dec 24, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 10, 2022
…michaelwoerister

Ensure that `Fingerprint` caching respects hashing configuration

Fixes rust-lang#92266

In some `HashStable` impls, we use a cache to avoid re-computing
the same `Fingerprint` from the same structure (e.g. an `AdtDef`).
However, the `StableHashingContext` used can be configured to
perform hashing in different ways (e.g. skipping `Span`s). This
configuration information is not included in the cache key,
which will cause an incorrect `Fingerprint` to be used if
we hash the same structure with different `StableHashingContext`
settings.

To fix this, the configuration settings of `StableHashingContext`
are split out into a separate `HashingControls` struct. This
struct is used as part of the cache key, ensuring that our caches
always produce the correct result for the given settings.

With this in place, we now turn off `Span` hashing during the
entire process of computing the hash included in legacy symbols.
This current has no effect, but will matter when a future PR
starts hashing more `Span`s that we currently skip.
@bors bors closed this as completed in 5580e5e Jan 10, 2022
michaelwoerister pushed a commit to michaelwoerister/rust that referenced this issue Feb 21, 2022
Fixes rust-lang#92266

In some `HashStable` impls, we use a cache to avoid re-computing
the same `Fingerprint` from the same structure (e.g. an `AdtDef`).
However, the `StableHashingContext` used can be configured to
perform hashing in different ways (e.g. skipping `Span`s). This
configuration information is not included in the cache key,
which will cause an incorrect `Fingerprint` to be used if
we hash the same structure with different `StableHashingContext`
settings.

To fix this, the configuration settings of `StableHashingContext`
are split out into a separate `HashingControls` struct. This
struct is used as part of the cache key, ensuring that our caches
always produce the correct result for the given settings.

With this in place, we now turn off `Span` hashing during the
entire process of computing the hash included in legacy symbols.
This current has no effect, but will matter when a future PR
starts hashing more `Span`s that we currently skip.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-incr-comp Area: Incremental compilation C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant