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

Honour DoNotRelease on SafeHandles #654

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Changes from all commits
Commits
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
19 changes: 12 additions & 7 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Generator : IDisposable
internal const string InteropDecorationNamespace = "Windows.Win32.Interop";
internal const string NativeArrayInfoAttribute = "NativeArrayInfoAttribute";
internal const string RAIIFreeAttribute = "RAIIFreeAttribute";
internal const string DoNotReleaseAttribute = "DoNotReleaseAttribute";
internal const string GlobalNamespacePrefix = "global::";
internal const string GlobalWinmdRootNamespaceAlias = "winmdroot";
internal const string WinRTCustomMarshalerClass = "WinRTCustomMarshaler";
Expand Down Expand Up @@ -1870,14 +1871,17 @@ internal void GetBaseTypeInfo(TypeDefinition typeDef, out StringHandle baseTypeN
return null;
}

internal CustomAttribute? FindInteropDecorativeAttribute(CustomAttributeHandleCollection customAttributeHandles, string attributeName)
internal CustomAttribute? FindInteropDecorativeAttribute(CustomAttributeHandleCollection? customAttributeHandles, string attributeName)
{
foreach (CustomAttributeHandle handle in customAttributeHandles)
if (customAttributeHandles is not null)
{
CustomAttribute att = this.Reader.GetCustomAttribute(handle);
if (this.IsAttribute(att, InteropDecorationNamespace, attributeName))
foreach (CustomAttributeHandle handle in customAttributeHandles)
{
return att;
CustomAttribute att = this.Reader.GetCustomAttribute(handle);
if (this.IsAttribute(att, InteropDecorationNamespace, attributeName))
{
return att;
}
}
}

Expand Down Expand Up @@ -4263,6 +4267,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
static ParameterSyntax StripAttributes(ParameterSyntax parameter) => parameter.WithAttributeLists(List<AttributeListSyntax>());
static ExpressionSyntax GetSpanLength(ExpressionSyntax span) => MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, span, IdentifierName(nameof(Span<int>.Length)));
bool isReleaseMethod = this.MetadataIndex.ReleaseMethods.Contains(externMethodDeclaration.Identifier.ValueText);
bool doNotRelease = this.FindInteropDecorativeAttribute(this.GetReturnTypeCustomAttributes(methodDefinition), DoNotReleaseAttribute) is not null;

TypeSyntaxSettings parameterTypeSyntaxSettings = overloadOf switch
{
Expand Down Expand Up @@ -4341,7 +4346,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
origName,
ObjectCreationExpression(safeHandleType).AddArgumentListArguments(
Argument(typeDefHandleName),
Argument(LiteralExpression(SyntaxKind.TrueLiteralExpression)).WithNameColon(NameColon(IdentifierName("ownsHandle")))))));
Argument(LiteralExpression(doNotRelease ? SyntaxKind.FalseLiteralExpression : SyntaxKind.TrueLiteralExpression)).WithNameColon(NameColon(IdentifierName("ownsHandle")))))));
}
}
else if (this.options.UseSafeHandles && isIn && !isOut && !isReleaseMethod && parameterTypeInfo is HandleTypeHandleInfo parameterHandleTypeInfo && this.TryGetHandleReleaseMethod(parameterHandleTypeInfo.Handle, out string? releaseMethod) && !this.Reader.StringComparer.Equals(methodDefinition.Name, releaseMethod))
Expand Down Expand Up @@ -4733,7 +4738,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
//// return new SafeHandle(result, ownsHandle: true);
body = body.AddStatements(ReturnStatement(ObjectCreationExpression(returnSafeHandleType).AddArgumentListArguments(
Argument(resultLocal),
Argument(LiteralExpression(SyntaxKind.TrueLiteralExpression)).WithNameColon(NameColon(IdentifierName("ownsHandle"))))));
Argument(LiteralExpression(doNotRelease ? SyntaxKind.FalseLiteralExpression : SyntaxKind.TrueLiteralExpression)).WithNameColon(NameColon(IdentifierName("ownsHandle"))))));
}
else if (hasVoidReturn)
{
Expand Down