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

Ownership tracking #89329

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dc6356f
Fully qualify return type on UnreachableException methods
jtschuster Jun 15, 2023
0cc937e
Merge branch 'main' of https://github.com/dotnet/runtime
jtschuster Jun 16, 2023
a16a7d6
Merge branch 'main' of https://github.com/dotnet/runtime
jtschuster Jun 19, 2023
cca57d3
Merge branch 'main' of https://github.com/dotnet/runtime
jtschuster Jun 28, 2023
ed7cd71
wip
jtschuster Jul 5, 2023
744a39b
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 5, 2023
788c862
wip
jtschuster Jul 13, 2023
d4aaad5
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 13, 2023
7363299
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 14, 2023
1e1345b
wip
jtschuster Jul 17, 2023
5c0ba6a
builds the interop sln. repo build failing
jtschuster Jul 18, 2023
203807e
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 18, 2023
f2d7921
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 18, 2023
2b99032
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 18, 2023
0a532ad
Revert "Update dependencies from https://github.com/dotnet/roslyn bui…
jtschuster Jul 18, 2023
8128579
Revert "Revert "Update dependencies from https://github.com/dotnet/ro…
jtschuster Jul 18, 2023
acf29cc
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 18, 2023
f8da5a1
Fix changes to other files
jtschuster Jul 18, 2023
8a8f573
fix changes within interop sln
jtschuster Jul 18, 2023
86099a2
Tests pass
jtschuster Jul 18, 2023
8b14784
Remove trailing space
jtschuster Jul 18, 2023
ee7de7f
Remove commented code
jtschuster Jul 18, 2023
cf38596
Refactor com interfaces and rename isMidlOut
jtschuster Jul 18, 2023
29a6bad
PR feedback: rename _guid, be consistent in local vs property use
jtschuster Jul 18, 2023
1627cc9
Remove temp test project
jtschuster Jul 18, 2023
c25b5d7
wip
jtschuster Jul 19, 2023
31d5f1b
wip
jtschuster Jul 19, 2023
5370aa4
Merge branch 'main' of https://github.com/dotnet/runtime into Marshal…
jtschuster Jul 19, 2023
c615eb2
Almost Passing tests, skipping [out] collections cleanup
jtschuster Jul 21, 2023
525f58c
Passing tests
jtschuster Jul 21, 2023
3a4fab9
Don't discard the _out value
jtschuster Jul 21, 2023
4b2716f
Added test type
jtschuster Jul 21, 2023
0adfc5f
Merge branch 'main' of https://github.com/dotnet/runtime into Ownersh…
jtschuster Jul 21, 2023
7665368
Don't init _native for params that don't unmarshal to managed callee
jtschuster Jul 21, 2023
e1603f9
cleaned up a few files
jtschuster Jul 22, 2023
bedd5ba
Cleanup more
jtschuster Jul 25, 2023
a29694e
cleanup
jtschuster Jul 26, 2023
1c115aa
Make sure stateless values clean up correctly
jtschuster Jul 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -220,6 +221,9 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray<FunctionPointerUnm
allStatements.Add(MarshallerHelpers.CreateSetLastPInvokeErrorStatement(LastErrorIdentifier));
}

// ManagedToUnmanaged shouldn't need AssignOut stage
Debug.Assert(statements.AssignOut.IsEmpty);

// Return
if (!_marshallers.IsManagedVoidReturn)
allStatements.Add(ReturnStatement(IdentifierName(_context.GetIdentifiers(_marshallers.ManagedReturnMarshaller.TypeInfo).managed)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Expand Down Expand Up @@ -34,27 +35,32 @@ public ManagedTypeInfo AsNativeType(TypePositionInfo info) =>
IsFunctionPointer: false);
public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context)
{
if (context.CurrentStage != StubCodeContext.Stage.Unmarshal)
switch (context.CurrentStage)
{
yield break;
case StubCodeContext.Stage.Unmarshal:
var (managed, native) = context.GetIdentifiers(info);
// <managed> = ComWrappers.ComInterfaceDispatch.GetInstance<<managedType>>(<native>);
yield return ExpressionStatement(
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
IdentifierName(managed),
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch),
GenericName(
Identifier("GetInstance"),
TypeArgumentList(SingletonSeparatedList(info.ManagedType.Syntax)))),
ArgumentList(
SingletonSeparatedList(
Argument(
IdentifierName(native)))))));
yield break;
case StubCodeContext.Stage.AssignOut:
Debug.Assert(MarshallerHelpers.MarshalsOut(info, context));
yield return this.GeneratePointerAssignOut(info, context);
yield break;
default:
yield break;
}

var (managed, native) = context.GetIdentifiers(info);

// <managed> = ComWrappers.ComInterfaceDispatch.GetInstance<<managedType>>(<native>);
yield return ExpressionStatement(
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
IdentifierName(managed),
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch),
GenericName(
Identifier("GetInstance"),
TypeArgumentList(SingletonSeparatedList(info.ManagedType.Syntax)))),
ArgumentList(
SingletonSeparatedList(
Argument(
IdentifierName(native)))))));
}

public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.NativeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

Expand All @@ -14,7 +13,6 @@ namespace Microsoft.Interop
internal sealed class UnmanagedToManagedStubGenerator
{
private const string ReturnIdentifier = "__retVal";
private const string InvokeSucceededIdentifier = "__invokeSucceeded";

private readonly BoundGenerators _marshallers;

Expand Down Expand Up @@ -60,41 +58,28 @@ public BlockSyntax GenerateStubBody(ExpressionSyntax methodToInvoke)
|| !statements.ManagedExceptionCatchClauses.IsEmpty;
VariableDeclarations declarations = VariableDeclarations.GenerateDeclarationsForUnmanagedToManaged(_marshallers, _context, shouldInitializeVariables);

if (!statements.GuaranteedUnmarshal.IsEmpty)
{
setupStatements.Add(MarshallerHelpers.Declare(PredefinedType(Token(SyntaxKind.BoolKeyword)), InvokeSucceededIdentifier, initializeToDefault: true));
}

setupStatements.AddRange(declarations.Initializations);
setupStatements.AddRange(declarations.Variables);
setupStatements.AddRange(declarations.Initializations);
setupStatements.AddRange(statements.Setup);

List<StatementSyntax> tryStatements = new();
tryStatements.AddRange(statements.Unmarshal);
tryStatements.AddRange(statements.GuaranteedUnmarshal);

tryStatements.Add(statements.InvokeStatement);

if (!statements.GuaranteedUnmarshal.IsEmpty)
{
tryStatements.Add(ExpressionStatement(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
IdentifierName(InvokeSucceededIdentifier),
LiteralExpression(SyntaxKind.TrueLiteralExpression))));
}

tryStatements.AddRange(statements.NotifyForSuccessfulInvoke);
tryStatements.AddRange(statements.PinnedMarshal);
tryStatements.AddRange(statements.Marshal);
tryStatements.AddRange(statements.PinnedMarshal);

tryStatements.AddRange(statements.AssignOut);
tryStatements.AddRange(statements.Cleanup);

List<StatementSyntax> allStatements = setupStatements;
List<StatementSyntax> finallyStatements = new();
if (!statements.GuaranteedUnmarshal.IsEmpty)
{
finallyStatements.Add(IfStatement(IdentifierName(InvokeSucceededIdentifier), Block(statements.GuaranteedUnmarshal)));
}

SyntaxList<CatchClauseSyntax> catchClauses = List(statements.ManagedExceptionCatchClauses);

finallyStatements.AddRange(statements.Cleanup);
if (finallyStatements.Count > 0)
{
allStatements.Add(
Expand All @@ -110,6 +95,7 @@ public BlockSyntax GenerateStubBody(ExpressionSyntax methodToInvoke)
allStatements.AddRange(tryStatements);
}


// Return
if (!_marshallers.IsUnmanagedVoidReturn)
allStatements.Add(ReturnStatement(IdentifierName(_context.GetIdentifiers(_marshallers.NativeReturnMarshaller.TypeInfo).native)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
Expand Down Expand Up @@ -183,6 +184,9 @@ public BlockSyntax GeneratePInvokeBody(string dllImportName)
allStatements.Add(MarshallerHelpers.CreateSetLastPInvokeErrorStatement(LastErrorIdentifier));
}

// ManagedToUnmanaged shouldn't need AssignOut stage
Debug.Assert(statements.AssignOut.IsEmpty);

// Return
if (!_marshallers.IsManagedVoidReturn)
allStatements.Add(ReturnStatement(IdentifierName(_context.GetIdentifiers(_marshallers.ManagedReturnMarshaller.TypeInfo).managed)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;

namespace Microsoft.Interop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct GeneratedStatements
public ImmutableArray<StatementSyntax> NotifyForSuccessfulInvoke { get; init; }
public ImmutableArray<StatementSyntax> GuaranteedUnmarshal { get; init; }
public ImmutableArray<StatementSyntax> Cleanup { get; init; }

public ImmutableArray<StatementSyntax> AssignOut { get; init; }
public ImmutableArray<CatchClauseSyntax> ManagedExceptionCatchClauses { get; init; }

public static GeneratedStatements Create(BoundGenerators marshallers, StubCodeContext context)
Expand All @@ -39,7 +39,8 @@ public static GeneratedStatements Create(BoundGenerators marshallers, StubCodeCo
NotifyForSuccessfulInvoke = GenerateStatementsForStubContext(marshallers, context with { CurrentStage = StubCodeContext.Stage.NotifyForSuccessfulInvoke }),
GuaranteedUnmarshal = GenerateStatementsForStubContext(marshallers, context with { CurrentStage = StubCodeContext.Stage.GuaranteedUnmarshal }),
Cleanup = GenerateStatementsForStubContext(marshallers, context with { CurrentStage = StubCodeContext.Stage.Cleanup }),
ManagedExceptionCatchClauses = GenerateCatchClauseForManagedException(marshallers, context)
ManagedExceptionCatchClauses = GenerateCatchClauseForManagedException(marshallers, context),
AssignOut = GenerateStatementsForStubContext(marshallers, context with { CurrentStage = StubCodeContext.Stage.AssignOut }),
};
}
public static GeneratedStatements Create(BoundGenerators marshallers, StubCodeContext context, ExpressionSyntax expressionToInvoke)
Expand Down Expand Up @@ -71,7 +72,26 @@ private static ImmutableArray<StatementSyntax> GenerateStatementsForStubContext(
ImmutableArray<StatementSyntax>.Builder statementsToUpdate = ImmutableArray.CreateBuilder<StatementSyntax>();
foreach (BoundGenerator marshaller in marshallers.SignatureMarshallers)
{
statementsToUpdate.AddRange(marshaller.Generator.Generate(marshaller.TypeInfo, context));
StubCodeContext localContext = context;
// Modify the context if we need to marshal out
switch (context.CurrentStage)
{
case StubCodeContext.Stage.Marshal:
case StubCodeContext.Stage.PinnedMarshal:
if (MarshallerHelpers.MarshalsOut(marshaller.TypeInfo, context))
localContext = new MarshalOutContext(context);
break;
case StubCodeContext.Stage.AssignOut:
if (!MarshallerHelpers.MarshalsOut(marshaller.TypeInfo, context))
continue;
localContext = new AssignOutContext(context, marshaller.Generator.AsParameter(marshaller.TypeInfo, context).Identifier.ToString());
break;
case StubCodeContext.Stage.CleanupFailure:
if (!MarshallerHelpers.MarshalsOut(marshaller.TypeInfo, context))
continue;
break;
}
statementsToUpdate.AddRange(marshaller.Generator.Generate(marshaller.TypeInfo, localContext));
}

if (statementsToUpdate.Count > 0)
Expand Down Expand Up @@ -150,7 +170,10 @@ private static ImmutableArray<CatchClauseSyntax> GenerateCatchClauseForManagedEx
{
if (!marshallers.HasManagedExceptionMarshaller)
{
return ImmutableArray<CatchClauseSyntax>.Empty;
ImmutableArray.Create(CatchClause(
CatchDeclaration(ParseTypeName(TypeNames.System_Exception)),
filter: null,
Block(List(GenerateStatementsForStubContext(marshallers, new MarshalOutContext(context with { CurrentStage = StubCodeContext.Stage.CleanupFailure }))))));
}
ImmutableArray<StatementSyntax>.Builder catchClauseBuilder = ImmutableArray.CreateBuilder<StatementSyntax>();

Expand All @@ -164,6 +187,15 @@ private static ImmutableArray<CatchClauseSyntax> GenerateCatchClauseForManagedEx
catchClauseBuilder.AddRange(
managedExceptionMarshaller.Generator.Generate(
managedExceptionMarshaller.TypeInfo, context with { CurrentStage = StubCodeContext.Stage.PinnedMarshal }));
catchClauseBuilder.AddRange(GenerateStatementsForStubContext(marshallers, new MarshalOutContext(context with { CurrentStage = StubCodeContext.Stage.CleanupFailure })));
if (!marshallers.IsUnmanagedVoidReturn)
{
catchClauseBuilder.Add(ReturnStatement(IdentifierName(context.GetIdentifiers(marshallers.NativeReturnMarshaller.TypeInfo).native)));
}
else
{
catchClauseBuilder.Add(ReturnStatement());
}
return ImmutableArray.Create(
CatchClause(
CatchDeclaration(ParseTypeName(TypeNames.System_Exception), Identifier(managed)),
Expand All @@ -183,8 +215,10 @@ private static SyntaxTriviaList GenerateStageTrivia(StubCodeContext.Stage stage)
StubCodeContext.Stage.UnmarshalCapture => "Capture the native data into marshaller instances in case conversion to managed data throws an exception.",
StubCodeContext.Stage.Unmarshal => "Convert native data to managed data.",
StubCodeContext.Stage.Cleanup => "Perform required cleanup.",
StubCodeContext.Stage.CleanupFailure => "Perform required cleanup.",
StubCodeContext.Stage.NotifyForSuccessfulInvoke => "Keep alive any managed objects that need to stay alive across the call.",
StubCodeContext.Stage.GuaranteedUnmarshal => "Convert native data to managed data even in the case of an exception during the non-cleanup phases.",
StubCodeContext.Stage.AssignOut => "Assign to parameters",
_ => throw new ArgumentOutOfRangeException(nameof(stage))
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;

namespace Microsoft.Interop
{
public sealed record AssignOutContext : StubCodeContext
{
internal StubCodeContext InnerContext { get; init; }
public string ParameterIdentifier { get; init; }

internal AssignOutContext(StubCodeContext inner, string parameterIdentifier)
{
InnerContext = inner;
Debug.Assert(inner.CurrentStage == Stage.AssignOut);
CurrentStage = Stage.AssignOut;
Direction = inner.Direction;
ParentContext = inner.ParentContext;
ParameterIdentifier = parameterIdentifier;
}

public override (TargetFramework framework, Version version) GetTargetFramework() => InnerContext.GetTargetFramework();

public override bool SingleFrameSpansNativeContext => InnerContext.SingleFrameSpansNativeContext;

public override bool AdditionalTemporaryStateLivesAcrossStages => InnerContext.AdditionalTemporaryStateLivesAcrossStages;

public override (string managed, string native) GetIdentifiers(TypePositionInfo info)
=> (InnerContext.GetIdentifiers(info).managed, InnerContext.GetAdditionalIdentifier(info, "out"));

public override string GetAdditionalIdentifier(TypePositionInfo info, string name) => InnerContext.GetAdditionalIdentifier(info, name);
}
}
Loading