-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Check ref safety of arg mixing in collection initializers #76237
Draft
jjonescz
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
jjonescz:75802-RefSafety-CollectionInitializer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9d7a35
Check ref safety of arg mixing in collection initializers
jjonescz 333529c
Simplify the implementation
jjonescz 55fb093
Extend tests
jjonescz a80efd9
Fix missed warning
jjonescz 461ab15
Extend tests
jjonescz f350350
Clarify a condition
jjonescz 7dbbe97
Simplify implementation
jjonescz 601b822
Allow the receiver to affect the scope
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4272,7 +4272,10 @@ internal SafeContext GetValEscape(BoundExpression expr, SafeContext scopeOfTheCo | |||||
var initializerOpt = objectCreation.InitializerExpressionOpt; | ||||||
if (initializerOpt != null) | ||||||
{ | ||||||
escape = escape.Intersect(GetValEscape(initializerOpt, scopeOfTheContainingExpression)); | ||||||
escape = escape.Intersect( | ||||||
initializerOpt is BoundCollectionInitializerExpression colInitExpr | ||||||
? GetValEscapeOfCollectionInitializer(colInitExpr, scopeOfTheContainingExpression, receiverScope: escape) | ||||||
: GetValEscape(initializerOpt, scopeOfTheContainingExpression)); | ||||||
} | ||||||
|
||||||
return escape; | ||||||
|
@@ -4455,11 +4458,7 @@ internal SafeContext GetValEscape(BoundExpression expr, SafeContext scopeOfTheCo | |||||
|
||||||
case BoundKind.CollectionInitializerExpression: | ||||||
var colExpr = (BoundCollectionInitializerExpression)expr; | ||||||
return GetValEscape(colExpr.Initializers, scopeOfTheContainingExpression); | ||||||
|
||||||
case BoundKind.CollectionElementInitializer: | ||||||
var colElement = (BoundCollectionElementInitializer)expr; | ||||||
return GetValEscape(colElement.Arguments, scopeOfTheContainingExpression); | ||||||
return GetValEscapeOfCollectionInitializer(colExpr, scopeOfTheContainingExpression, receiverScope: SafeContext.CallingMethod); | ||||||
|
||||||
case BoundKind.ObjectInitializerMember: | ||||||
// this node generally makes no sense outside of the context of containing initializer | ||||||
|
@@ -4468,11 +4467,18 @@ internal SafeContext GetValEscape(BoundExpression expr, SafeContext scopeOfTheCo | |||||
return scopeOfTheContainingExpression; | ||||||
|
||||||
case BoundKind.ImplicitReceiver: | ||||||
case BoundKind.ObjectOrCollectionValuePlaceholder: | ||||||
// binder uses this as a placeholder when binding members inside an object initializer | ||||||
// just say it does not escape anywhere, so that we do not get false errors. | ||||||
return scopeOfTheContainingExpression; | ||||||
|
||||||
case BoundKind.ObjectOrCollectionValuePlaceholder: | ||||||
if (_placeholderScopes?.TryGetValue((BoundObjectOrCollectionValuePlaceholder)expr, out var scope) == true) | ||||||
{ | ||||||
return scope; | ||||||
} | ||||||
|
||||||
return scopeOfTheContainingExpression; | ||||||
|
||||||
case BoundKind.InterpolatedStringHandlerPlaceholder: | ||||||
// The handler placeholder cannot escape out of the current expression, as it's a compiler-synthesized | ||||||
// location. | ||||||
|
@@ -4589,6 +4595,17 @@ private SafeContext GetValEscapeOfObjectInitializer(BoundObjectInitializerExpres | |||||
return result; | ||||||
} | ||||||
|
||||||
private SafeContext GetValEscapeOfCollectionInitializer(BoundCollectionInitializerExpression colExpr, SafeContext scopeOfTheContainingExpression, SafeContext receiverScope) | ||||||
{ | ||||||
// We are interested in the CheckValEscape's return value, but it can be false only if not in an unsafe region. | ||||||
using var _ = new UnsafeRegion(this, inUnsafeRegion: false); | ||||||
|
||||||
// If arg mixing fails (i.e., some arguments could escape into the receiver), make the result scoped. | ||||||
return CheckValEscapeOfCollectionInitializer(colExpr, escapeFrom: scopeOfTheContainingExpression, escapeTo: receiverScope, BindingDiagnosticBag.Discarded) | ||||||
? receiverScope | ||||||
: scopeOfTheContainingExpression; | ||||||
} | ||||||
|
||||||
private SafeContext GetValEscapeOfObjectMemberInitializer(BoundExpression expr, SafeContext scopeOfTheContainingExpression) | ||||||
{ | ||||||
SafeContext result; | ||||||
|
@@ -4774,6 +4791,18 @@ internal bool CheckValEscape(SyntaxNode node, BoundExpression expr, SafeContext | |||||
} | ||||||
return true; | ||||||
|
||||||
case BoundKind.ObjectOrCollectionValuePlaceholder: | ||||||
if (_placeholderScopes?.TryGetValue((BoundObjectOrCollectionValuePlaceholder)expr, out var scope) == true) | ||||||
{ | ||||||
if (!scope.IsConvertibleTo(escapeTo)) | ||||||
{ | ||||||
Error(diagnostics, inUnsafeRegion ? ErrorCode.WRN_EscapeVariable : ErrorCode.ERR_EscapeVariable, node, expr.Syntax); | ||||||
return inUnsafeRegion; | ||||||
} | ||||||
return true; | ||||||
} | ||||||
goto default; | ||||||
|
||||||
case BoundKind.Local: | ||||||
var localSymbol = ((BoundLocal)expr).LocalSymbol; | ||||||
if (!GetLocalScopes(localSymbol).ValEscapeScope.IsConvertibleTo(escapeTo)) | ||||||
|
@@ -5257,17 +5286,9 @@ internal bool CheckValEscape(SyntaxNode node, BoundExpression expr, SafeContext | |||||
var initExpr = (BoundObjectInitializerExpression)expr; | ||||||
return CheckValEscapeOfObjectInitializer(initExpr, escapeFrom, escapeTo, diagnostics); | ||||||
|
||||||
// this would be correct implementation for CollectionInitializerExpression | ||||||
// however it is unclear if it is reachable since the initialized type must implement IEnumerable | ||||||
case BoundKind.CollectionInitializerExpression: | ||||||
var colExpr = (BoundCollectionInitializerExpression)expr; | ||||||
return CheckValEscape(colExpr.Initializers, escapeFrom, escapeTo, diagnostics); | ||||||
|
||||||
// this would be correct implementation for CollectionElementInitializer | ||||||
// however it is unclear if it is reachable since the initialized type must implement IEnumerable | ||||||
case BoundKind.CollectionElementInitializer: | ||||||
var colElement = (BoundCollectionElementInitializer)expr; | ||||||
return CheckValEscape(colElement.Arguments, escapeFrom, escapeTo, diagnostics); | ||||||
return CheckValEscapeOfCollectionInitializer(colExpr, escapeFrom, escapeTo, diagnostics); | ||||||
|
||||||
case BoundKind.PointerElementAccess: | ||||||
var accessedExpression = ((BoundPointerElementAccess)expr).Expression; | ||||||
|
@@ -5561,21 +5582,53 @@ private bool CheckValEscapeOfObjectInitializer(BoundObjectInitializerExpression | |||||
return true; | ||||||
} | ||||||
|
||||||
#nullable disable | ||||||
|
||||||
private bool CheckValEscape(ImmutableArray<BoundExpression> expressions, SafeContext escapeFrom, SafeContext escapeTo, BindingDiagnosticBag diagnostics) | ||||||
private bool CheckValEscapeOfCollectionInitializer(BoundCollectionInitializerExpression colExpr, SafeContext escapeFrom, SafeContext escapeTo, BindingDiagnosticBag diagnostics) | ||||||
{ | ||||||
foreach (var expression in expressions) | ||||||
// return new C() { element }; | ||||||
// | ||||||
// is equivalent to | ||||||
// | ||||||
// var c = new C(); | ||||||
// c.Add(element); | ||||||
// return c; | ||||||
// | ||||||
// So we check arg mixing of `(receiverPlaceholder).Add(element)` calls | ||||||
// where the placeholder has `escapeTo` scope and the call has `escapeFrom` scope. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
suggest using exactly the same name here to make it just a little bit easier to grasp what this comment is conveying. |
||||||
|
||||||
bool result = true; | ||||||
using var _ = new PlaceholderRegion(this, [(colExpr.Placeholder, escapeTo)]) { ForceRemoveOnDispose = true }; | ||||||
foreach (var initializer in colExpr.Initializers) | ||||||
{ | ||||||
if (!CheckValEscape(expression.Syntax, expression, escapeFrom, escapeTo, checkingReceiver: false, diagnostics: diagnostics)) | ||||||
switch (initializer) | ||||||
{ | ||||||
return false; | ||||||
case BoundCollectionElementInitializer init: | ||||||
result &= CheckInvocationArgMixing( | ||||||
init.Syntax, | ||||||
MethodInfo.Create(init.AddMethod), | ||||||
receiverOpt: colExpr.Placeholder, | ||||||
receiverIsSubjectToCloning: init.InitialBindingReceiverIsSubjectToCloning, | ||||||
parameters: init.AddMethod.Parameters, | ||||||
argsOpt: init.Arguments, | ||||||
argRefKindsOpt: default, | ||||||
argsToParamsOpt: init.ArgsToParamsOpt, | ||||||
scopeOfTheContainingExpression: escapeFrom, | ||||||
diagnostics); | ||||||
break; | ||||||
|
||||||
case BoundDynamicCollectionElementInitializer: | ||||||
break; | ||||||
|
||||||
default: | ||||||
Debug.Fail($"{initializer.Kind} expression of {initializer.Type} type"); | ||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
return true; | ||||||
return result; | ||||||
} | ||||||
|
||||||
#nullable disable | ||||||
|
||||||
private bool CheckInterpolatedStringHandlerConversionEscape(BoundExpression expression, SafeContext escapeFrom, SafeContext escapeTo, BindingDiagnosticBag diagnostics) | ||||||
{ | ||||||
var data = expression.GetInterpolatedStringHandlerData(); | ||||||
|
This file contains 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
This file contains 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
This file contains 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
17 changes: 10 additions & 7 deletions
17
src/Compilers/CSharp/Portable/Generated/BoundNodes.xml.Generated.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we testing the
unsafe { }
region case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
unsafe
test. I'm not sure theWRN_EscapeVariable
here is reachable (at least in the tested scenarios, this is used with discarded diagnostics just for the return value), but at least it's consistent with other similar places (and I imagine it might be reachable with more complex nested scenarios).