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

Fix torn write of struct causing crash in symbol finder. #69927

Merged
merged 10 commits into from
Sep 13, 2023

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Sep 13, 2023

Fixes #69919

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner September 13, 2023 16:25
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 13, 2023
@@ -8,146 +8,145 @@
using Roslyn.Utilities;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

view with whitespace off.

Contract.ThrowIfNull(value, nameof(value));
Contract.ThrowIfNull(_concatenatedLowerCaseWords, nameof(_concatenatedLowerCaseWords));
Contract.ThrowIfTrue(_nodes.IsDefault, $"{nameof(_nodes)}.{nameof(_nodes.IsDefault)}");
Contract.ThrowIfTrue(_edges.IsDefault, $"{nameof(_edges)}.{nameof(_edges.IsDefault)}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the important part. ideally so we get a dump that tells us which of these is the problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to stay in the PR since you think you've figured out the problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i don't mind it :)

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@CyrusNajmabadi CyrusNajmabadi changed the title Add contract calls to help trace down an internally reported crash Fix torn write of struct causing crash in symbol finder. Sep 13, 2023
{
get
{
_spellChecker ??= CreateSpellChecker(Checksum, _nodes);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpellChecker? was a Nullable<SpellChecker>. This is just a pure struct which can have torn writes. By moving to a StrongBox<SpellChecker> we have teh runtime guarantee that if _spllChecker is not null then _spellChecker.Value is 'completely written'.

{
Logger.Log(FunctionId.SymbolTreeInfo_ExceptionInCacheRead);
var rawSpellChecker = SpellChecker.TryReadFrom(reader);
spellChecker = rawSpellChecker is null ? null : new(rawSpellChecker.Value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spellChecker = rawSpellChecker is null ? null : new(rawSpellChecker.Value);

Intentional behavior change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. if we can't read in teh spell checker (should never happen, but we're resilient to it anyways), we can still proceed as the new spell checker can be lazily created.

/// <summary>
/// Explicitly boxed so that we can safely initialize/read this across threads without the need for a lock.
/// </summary>
private StrongBox<SpellChecker>? _spellChecker;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ Since _spellChecker.Value is never written after construction, and there are no other instantiations of the SpellChecker type, this change is functionally equivalent to (but much more complicated than) just making SpellChecker a reference type. You could further simplify things by using the helpers from #69017 to initialize the field on first use.

Copy link
Member

@sharwell sharwell Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other option if you want to keep this a value type is to use this helper:

public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNullIfNotNull(nameof(syncLock))] ref object? syncLock, Func<T> valueFactory)
=> LazyInitializer.EnsureInitialized<T>(ref target!, ref initialized, ref syncLock, valueFactory);

The implementation of EnsureInitialized contains a lock-free fast path for the case where initialized is true (set at the end of initialization). You may need to use PooledDelegates to avoid a delegate allocation on the fast path as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Moved to just be a refernece type. That fits best here for me :)

SpellChecker.FindSimilarWords(ref similarNames.AsRef(), name, substringsAreSimilar: false);
// Ensure the spell checker is initialized. This is concurrency safe. Technically multiple threads may end
// up overwriting the field, but even if that happens, we are sure to see a fully written spell checker as
// the runtime guarantees that the initialize of the SpellChecker instnace completely written when we read
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the runtime guarantees that the initialize of the SpellChecker instnace completely written when we read
// the runtime guarantees that the initialize of the SpellChecker instance completely written when we read

@CyrusNajmabadi CyrusNajmabadi merged commit 0aec763 into dotnet:main Sep 13, 2023
22 of 24 checks passed
@ghost ghost added this to the Next milestone Sep 13, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the asserts branch September 13, 2023 22:11
@Cosifne Cosifne modified the milestones: Next, 17.8 P3 Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NRE from spell checker while working in Roslyn
4 participants