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

Compiler crashes when emitting EnC delta for assembly with synthesized inline array types #69398

Closed
Tracked by #67826
tmat opened this issue Aug 4, 2023 · 5 comments · Fixed by #69485
Closed
Tracked by #67826
Assignees
Labels
Area-Compilers Feature - Collection Expressions Feature - Inline Arrays Tenet-Reliability Customer telemetry indicates that the product is failing in a crash/hang/dataloss manner.
Milestone

Comments

@tmat
Copy link
Member

tmat commented Aug 4, 2023

Repro:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

[CollectionBuilder(typeof(MyCollectionBuilder), nameof(MyCollectionBuilder.Create))]
public struct MyCollection<T> : IEnumerable<T>
{
    private readonly List<T> _list;
    public MyCollection(List<T> list) { _list = list; }
    public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class MyCollectionBuilder
{
    public static MyCollection<T> Create<T>(ReadOnlySpan<T> items)
    {
        return new MyCollection<T>(new List<T>(items.ToArray()));
    }
}

class Program
{
    static void Main()
    {
        MyCollection<int> y = F1();
    }

    static MyCollection<int> F1()
    {
        MyCollection<int> x = [0, 1, 2];
        Console.WriteLine(1);
        return x;
    }
}

Breakpoint to F1, F5, change Console.WriteLine(1); and step.

System.NullReferenceException: 'Object reference not set to an instance of an object.'

>	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.Emit.PEModuleBuilder.EnsureInlineArrayTypeExists(Microsoft.CodeAnalysis.SyntaxNode syntaxNode, Microsoft.CodeAnalysis.CSharp.SyntheticBoundNodeFactory factory, int arrayLength, Microsoft.CodeAnalysis.DiagnosticBag diagnostics) Line 1980	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.CreateAndPopulateInlineArray(Microsoft.CodeAnalysis.SyntaxNode syntax, Microsoft.CodeAnalysis.CSharp.Symbols.TypeWithAnnotations elementType, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CSharp.BoundExpression> elements, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CSharp.Symbols.LocalSymbol> locals, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CSharp.BoundExpression> sideEffects) Line 234	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitCollectionBuilderCollectionExpression(Microsoft.CodeAnalysis.CSharp.BoundCollectionExpression node) Line 191	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitCollectionExpression(Microsoft.CodeAnalysis.CSharp.BoundCollectionExpression node) Line 36	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundCollectionExpression.Accept(Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor visitor) Line 6336	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithoutStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 97	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.VisitExpressionWithStackGuard(ref int recursionDepth, Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 212	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 92	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitExpressionImpl(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 245	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitExpression(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 209	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitConversion(Microsoft.CodeAnalysis.CSharp.BoundConversion node) Line 64	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundConversion.Accept(Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor visitor) Line 2874	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithoutStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 97	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.VisitExpressionWithStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 242	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor.VisitExpressionWithStackGuard(ref int recursionDepth, Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 216	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundTreeRewriterWithStackGuard.VisitExpressionWithStackGuard(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 92	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitExpressionImpl(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 245	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitExpression(Microsoft.CodeAnalysis.CSharp.BoundExpression node) Line 209	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitLocalDeclaration(Microsoft.CodeAnalysis.CSharp.BoundLocalDeclaration node) Line 16	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundLocalDeclaration.Accept(Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor visitor) Line 3315	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatement(Microsoft.CodeAnalysis.CSharp.BoundStatement node) Line 220	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitPossibleUsingDeclaration(Microsoft.CodeAnalysis.CSharp.BoundStatement node, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CSharp.BoundStatement> statements, int statementIndex, out bool replacedLocalDeclarations) Line 98	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatementSubList(Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CSharp.BoundStatement> builder, System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CSharp.BoundStatement> statements, int startIndex) Line 56	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitBlock(Microsoft.CodeAnalysis.CSharp.BoundBlock node) Line 23	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.BoundBlock.Accept(Microsoft.CodeAnalysis.CSharp.BoundTreeVisitor visitor) Line 3219	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.VisitStatement(Microsoft.CodeAnalysis.CSharp.BoundStatement node) Line 220	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.LocalRewriter.Rewrite(Microsoft.CodeAnalysis.CSharp.CSharpCompilation compilation, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol method, int methodOrdinal, Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol containingType, Microsoft.CodeAnalysis.CSharp.BoundStatement statement, Microsoft.CodeAnalysis.CSharp.TypeCompilationState compilationState, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields previousSubmissionFields, bool allowOmissionOfConditionalCalls, Microsoft.CodeAnalysis.Emit.MethodInstrumentation instrumentation, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider debugDocumentProvider, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag diagnostics, out System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CodeGen.SourceSpan> codeCoverageSpans, out bool sawLambdas, out bool sawLocalFunctions, out bool sawAwaitInExceptionHandler) Line 125	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.LowerBodyOrInitializer(Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol method, int methodOrdinal, Microsoft.CodeAnalysis.CSharp.BoundStatement body, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields previousSubmissionFields, Microsoft.CodeAnalysis.CSharp.TypeCompilationState compilationState, Microsoft.CodeAnalysis.Emit.MethodInstrumentation instrumentation, Microsoft.CodeAnalysis.CodeGen.DebugDocumentProvider debugDocumentProvider, out System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.CodeGen.SourceSpan> codeCoverageSpans, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag diagnostics, ref Microsoft.CodeAnalysis.CodeGen.VariableSlotAllocator lazyVariableSlotAllocator, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CodeGen.LambdaDebugInfo> lambdaDebugInfoBuilder, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CodeGen.ClosureDebugInfo> closureDebugInfoBuilder, Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder<Microsoft.CodeAnalysis.CodeGen.StateMachineStateDebugInfo> stateMachineStateDebugInfoBuilder, out Microsoft.CodeAnalysis.CSharp.StateMachineTypeSymbol stateMachineTypeOpt) Line 1433	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethod(Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol methodSymbol, int methodOrdinal, ref Microsoft.CodeAnalysis.CSharp.Binder.ProcessedFieldInitializers processedInitializers, Microsoft.CodeAnalysis.CSharp.SynthesizedSubmissionFields previousSubmissionFields, Microsoft.CodeAnalysis.CSharp.TypeCompilationState compilationState) Line 1226	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol containingType) Line 527	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.VisitNamedType(Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol symbol, Microsoft.CodeAnalysis.CSharp.TypeCompilationState arg) Line 417	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.Symbols.NamedTypeSymbol.Accept<Microsoft.CodeAnalysis.CSharp.TypeCompilationState, object>(Microsoft.CodeAnalysis.CSharp.CSharpSymbolVisitor<Microsoft.CodeAnalysis.CSharp.TypeCompilationState, object> visitor, Microsoft.CodeAnalysis.CSharp.TypeCompilationState argument) Line 727	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileNamespace(Microsoft.CodeAnalysis.CSharp.Symbols.NamespaceSymbol symbol) Line 396	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethodBodies(Microsoft.CodeAnalysis.CSharp.CSharpCompilation compilation, Microsoft.CodeAnalysis.CSharp.Emit.PEModuleBuilder moduleBeingBuiltOpt, bool emittingPdb, bool hasDeclarationErrors, bool emitMethodBodies, Microsoft.CodeAnalysis.CSharp.BindingDiagnosticBag diagnostics, System.Predicate<Microsoft.CodeAnalysis.CSharp.Symbol> filterOpt, System.Threading.CancellationToken cancellationToken) Line 159	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CompileMethods(Microsoft.CodeAnalysis.Emit.CommonPEModuleBuilder moduleBuilder, bool emittingPdb, Microsoft.CodeAnalysis.DiagnosticBag diagnostics, System.Predicate<Microsoft.CodeAnalysis.Symbols.ISymbolInternal> filterOpt, System.Threading.CancellationToken cancellationToken) Line 3402	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.Compilation.Compile(Microsoft.CodeAnalysis.Emit.CommonPEModuleBuilder moduleBuilder, bool emittingPdb, Microsoft.CodeAnalysis.DiagnosticBag diagnostics, System.Predicate<Microsoft.CodeAnalysis.Symbols.ISymbolInternal> filterOpt, System.Threading.CancellationToken cancellationToken) Line 2590	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.Emit.EmitHelpers.EmitDifference(Microsoft.CodeAnalysis.CSharp.CSharpCompilation compilation, Microsoft.CodeAnalysis.Emit.EmitBaseline baseline, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Emit.SemanticEdit> edits, System.Func<Microsoft.CodeAnalysis.ISymbol, bool> isAddedSymbol, System.IO.Stream metadataStream, System.IO.Stream ilStream, System.IO.Stream pdbStream, Microsoft.CodeAnalysis.CodeGen.CompilationTestData testData, System.Threading.CancellationToken cancellationToken) Line 78	C#
 	Microsoft.CodeAnalysis.CSharp.dll!Microsoft.CodeAnalysis.CSharp.CSharpCompilation.EmitDifference(Microsoft.CodeAnalysis.Emit.EmitBaseline baseline, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Emit.SemanticEdit> edits, System.Func<Microsoft.CodeAnalysis.ISymbol, bool> isAddedSymbol, System.IO.Stream metadataStream, System.IO.Stream ilStream, System.IO.Stream pdbStream, Microsoft.CodeAnalysis.CodeGen.CompilationTestData testData, System.Threading.CancellationToken cancellationToken) Line 3635	C#
 	Microsoft.CodeAnalysis.dll!Microsoft.CodeAnalysis.Compilation.EmitDifference(Microsoft.CodeAnalysis.Emit.EmitBaseline baseline, System.Collections.Generic.IEnumerable<Microsoft.CodeAnalysis.Emit.SemanticEdit> edits, System.Func<Microsoft.CodeAnalysis.ISymbol, bool> isAddedSymbol, System.IO.Stream metadataStream, System.IO.Stream ilStream, System.IO.Stream pdbStream, System.Threading.CancellationToken cancellationToken) Line 3101	C#
 	Microsoft.CodeAnalysis.Features.dll!Microsoft.CodeAnalysis.EditAndContinue.EditSession.EmitSolutionUpdateAsync(Microsoft.CodeAnalysis.Solution solution, Microsoft.CodeAnalysis.EditAndContinue.ActiveStatementSpanProvider solutionActiveStatementSpanProvider, Microsoft.CodeAnalysis.EditAndContinue.UpdateId updateId, System.Threading.CancellationToken cancellationToken) Line 1013	C#

Relates to test plan #67826

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 4, 2023
@tmat
Copy link
Member Author

tmat commented Aug 4, 2023

@cston

@cston cston self-assigned this Aug 4, 2023
@tmat
Copy link
Member Author

tmat commented Aug 4, 2023

Simpler repro:

Just accessing inline array elements causes the same crash:

using System.Collections.Generic;
using System.Runtime.CompilerServices;

[InlineArray(2)]
public struct Buffer
{
    private int _element0;
}

class Program
{
    static void Main()
    {
        var b1 = new Buffer();

        b1[0] = 1;
        b1[1] = 2;

        Console.WriteLine(1);
    }
}

@cston cston removed their assignment Aug 4, 2023
@jcouv jcouv added Tenet-Reliability Customer telemetry indicates that the product is failing in a crash/hang/dataloss manner. Feature - Inline Arrays labels Aug 7, 2023
@jcouv jcouv added this to the 17.9 milestone Aug 8, 2023
@jcouv
Copy link
Member

jcouv commented Aug 8, 2023

@tmat If you had bandwidth to help on that one, that'd be great. Otherwise, it will likely have to wait until Aleksey is back and 17.9.

@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 9, 2023
@tmat tmat modified the milestones: 17.9, 17.8 Aug 10, 2023
@tmat tmat assigned tmat and unassigned AlekseyTs Aug 10, 2023
@AlekseyTs
Copy link
Contributor

@jcouv, @cston Judging by the call stack, it looks like the crash is primarily related to Collection Expressions (VisitCollectionBuilderCollectionExpression, EnsureInlineArrayTypeExists). Even if there is an issue with a simpler scenario mentioned at #69398 (comment), I doubt the call stack is the same. If the call stack, however, mentions the same methods, it is also related to changes made for Collection Expressions. Bottom line, I think the issue is either specific to Collection Expressions, or we have two separate and unrelated issues, which should be tracked and triaged separately.

@tmat
Copy link
Member Author

tmat commented Aug 12, 2023

I'm working on a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Collection Expressions Feature - Inline Arrays Tenet-Reliability Customer telemetry indicates that the product is failing in a crash/hang/dataloss manner.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants