[compiler] fix a bug in NormalizeNames - #14857
Merged
Merged
Conversation
ehigham
approved these changes
Apr 17, 2025
ehigham
left a comment
Member
There was a problem hiding this comment.
For such a simple fix, this is deceptively hard to test well as it requires a number preconditions. This fix will be true by construction when we use object hash instead of strings. Therefore I don't think it's worth adding a complicated or insufficient test.
chrisvittal
approved these changes
Apr 17, 2025
chrisvittal
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the quick fix!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
Fixes a bug that was reported in zulip.
Background: we use a function
freshNameto generate names to use in the IR which are guaranteed to not already be in use in the IR. We do that by maintaining a global variable counting the number of fresh names ever generated, andfreshNamereturns the strings"__iruid_$i".NormalizeNameschanges all the names used in the IR such that any name is defined at most once in the IR. It is also currently required to be deterministic, so that two IRs which are equal up to the choice of variable names (they are alpha equivalent) will be strictly equal after normalizing names. It does this by maintaining a local counter of how many new names have be generated, and each new variable is again the strings"__iruid_$i".Here's the bug: it's possible (though difficult) to run
NormalizeNamesbeforefreshNamehas been called many times. Say the IR has 10 variables which are renamed, so the last variable generated byNormalizeNamesis"__iruid_10", and suppose thatfreshNamehasn't been used yet. Now saySimplifyis run, and a simplification rule needs to generate a fresh name. That name will be"__iruid_1"(since this is the first call tofreshName). But that name is already used in the IR (generated byNormalizeNames), breaking the fundamental requirement offreshName. This is a problem because a reference to the existing use of"__iruid_1"might end up in the scope of the newly introduced binding of"__iruid_1", changing its meaning (and probably its type, if we're lucky).The fix here is simple: use a different prefix in the names generated by
NormalizeNamesandfreshName, so they can never collide.Security Assessment
Delete all except the correct answer:
Impact Description
Non-functional change in the query compiler.
(Reviewers: please confirm the security impact before approving)