fix: potential Array.get!Internal leaks part 1#13147
Merged
Conversation
github-merge-queue bot
pushed a commit
that referenced
this pull request
Mar 27, 2026
Part 2 for #13147, adding the necessary constant semantics to the interpreter.
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.
This PR fixes theoretical leaks in the handling of
Array.get!Internalin the code generator.Currently, the code generator assumes that the value returned by
get!Internalis derived from theArrayargument. However, this does not generally hold up as we might also return theInhabitedvalue in case of an out of bounds access (recall that we continue execution after panics by
default). This means that we sometimes convert an
Array.get!InternaltoArray.get!InternalBorrowedwhen we are not allowed to do so because in the panic case theInhabitedinstance can be returned and if it is an owned value it is going to leak.The fix consists of adapting several components to this change:
PropagateBorrowwill only mark the derived value as forcibly borrowed if both theInhabitedand
Arrayargument are forcibly borrowed.InferBorrowwill do the same for its data flow analysisExplicitRCis extended from a derived value tree to a derivedvalue graph where a value may have more than one parent. We only consider a value borrowed if all
of its parents are still accessible. Then
get!Internalis equipped with both itsInhabitedand its
Arrayparent.These changes are sufficient for correctness on their own. However, they are going to break
get!Internaltoget!InternalBorrowedconversion in most places. This happens because almost allInhabitedinstances are going to be constants. Currently reads from constants yield semanticallyowned values and thus block the
get!InternalBorrowedconversion. We would thus prefer for theseconstants to be treated as borrows instead.
The owned return is implemented in two ways at the moment:
with
inc-ed and thenlater
dec-ed somewhere (potentially using adec[persistent]which is a no-op in C)This PR changes the semantics of constant reads to instead be borrows from the constant (they can be
cutely interpreted as "being borrowed from the world"). This enables many
get!Internalto haveboth their arguments be marked as borrowed and thus still converted to
get!InternalBorrowed. Notethat this PR does not yet change the semantics of the interpreter to account for this
(it will be done in a part 2) and thus introduces (very minor) leaks temporarily.
Furthermore, we observed code with signatures such as the following:
being instantiated with
a := UInt32. This poses a challenge becauseInhabitedis currentlymarked as
nospecialize, meaning that we are sometimes going to end up with code such as:Here
xsitself was inferred as borrowed, however, theUInt32Inhabitedinstance was notspecialized for (as
Inhabitedis markednospecialize) and thus needs to be boxed. This causesthe
instparameter toget!Internalto be owned and thusget!InternalBorrowedconversion fails.This PR marks
Inhabitedasweak_specializewhich will make it get specialized for in this case,yielding code such as:
Fortunately the closed term extractor has support for precisely this feature and thus produces:
As described above reads from constants are now interpreted as borrows and thus the conversion to
get!InternalBorrowedbecomes legal again.