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

Report error for native PDB limit #75293

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 78 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/PDB/PDBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Roslyn.Test.PdbUtilities;
using Roslyn.Test.Utilities;
using Roslyn.Test.Utilities.TestGenerators;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.PDB
Expand Down Expand Up @@ -367,6 +368,83 @@ public void SymWriterErrors4()
Assert.False(result.Success);
}

[ConditionalTheory(typeof(WindowsOnly), Reason = ConditionalSkipReason.NativePdbRequiresDesktop)]
[WorkItem("https://github.com/dotnet/roslyn/issues/75237")]
[InlineData(0x9_999)]
[InlineData(0x9_000)]
public void NativeWriterLimit_Under(int length)
{
CompileWithMockedCustomMetadata(length).Diagnostics.Verify();
}

[ConditionalTheory(typeof(WindowsOnly), Reason = ConditionalSkipReason.NativePdbRequiresDesktop)]
[WorkItem("https://github.com/dotnet/roslyn/issues/75237")]
[InlineData(0x10_000)]
[InlineData(0x20_000)]
public void NativeWriterLimit_Over(int length)
{
CompileWithMockedCustomMetadata(length).Diagnostics.Verify(
// error CS0041: Unexpected error writing debug information -- 'Insufficient memory to continue the execution of the program.'
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
Diagnostic(ErrorCode.FTL_DebugEmitFailure).WithArguments(new OutOfMemoryException().Message).WithLocation(1, 1));
}

private static EmitResult CompileWithMockedCustomMetadata(int length)
{
var comp = CreateCompilation("""
class C
{
void M() { }
}
""");
return comp.Emit(
peStream: new MemoryStream(),
metadataPEStream: null,
pdbStream: new MemoryStream(),
xmlDocumentationStream: null,
cancellationToken: default,
win32Resources: null,
manifestResources: null,
options: null,
debugEntryPoint: null,
sourceLinkStream: null,
embeddedTexts: null,
rebuildData: null,
testData: new CompilationTestData
{
SymWriterFactory = metadataProvider =>
{
var writer = SymWriterTestUtilities.CreateUnmanagedWriter(metadataProvider);
return new CustomMetadataSymUnmanagedWriter(writer, new byte[length]);
},
});
}

[ConditionalFact(typeof(WindowsOnly), Reason = ConditionalSkipReason.NativePdbRequiresDesktop)]
[WorkItem("https://github.com/dotnet/roslyn/issues/75237")]
public void NativeWriterLimit_EndToEnd()
{
var locals = Enumerable.Range(0, 14_000)
.Select(i => $"""
var local{i} = {i};
M2(local{i});
""")
.Join(Environment.NewLine);
var source = $$"""
namespace N;
class C
{
void M1()
{
{{locals}}
}
void M2(int x) { }
}
""";
CreateCompilation(source, options: TestOptions.DebugDll).VerifyEmitDiagnostics(
// error CS0041: Unexpected error writing debug information -- 'Cannot emit native PDB for method 'N.C.M1()' because its debug metadata size 69096 is over the limit 65536.'
Diagnostic(ErrorCode.FTL_DebugEmitFailure).WithArguments(string.Format(CodeAnalysisResources.SymWriterMetadataOverLimit, "N.C.M1()", 69096, 65536)).WithLocation(1, 1));
}

[WorkItem(1067635, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1067635")]
[Fact]
public void SuppressDynamicAndEncCDIForWinRT()
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/Core/Portable/CodeAnalysisResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@
<data name="SymWriterDoesNotSupportSourceLink" xml:space="preserve">
<value>Windows PDB writer doesn't support SourceLink feature: '{0}'</value>
</data>
<data name="SymWriterMetadataOverLimit" xml:space="preserve">
<value>Cannot emit native PDB for method '{0}' because its debug metadata size {1} is over the limit {2}.</value>
</data>
<data name="RuleSetBadAttributeValue" xml:space="preserve">
<value>The attribute {0} has an invalid value of {1}.</value>
</data>
Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/Core/Portable/NativePdbWriter/PdbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public void SerializeDebugInfo(IMethodBody methodBody, StandaloneSignatureHandle

if (blob.Length > 0)
{
const int limit = 0x10_000;
if (blob.Length > limit)
{
throw new SymUnmanagedWriterException(string.Format(
CodeAnalysisResources.SymWriterMetadataOverLimit,
methodBody.MethodDefinition,
blob.Length,
limit));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the name of the attribute is part of the limit length. Is that included in the blob?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the name is not included in the blob. You are right the limit is wrong now, will fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you put the check here vs. inside DefineCustomMetadata? Yes this is the only call today but in the future if we added more calls we wouldn't get this protection.

Copy link
Member Author

@jjonescz jjonescz Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it seems better to move the check into DefineCustomMetadata. I originally chose this location because it's where I have access to the method name, but I can probably pass that down or rethrow an exception...


_symWriter.DefineCustomMetadata(blob);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/Compilers/VisualBasic/Test/Emit/PDB/PDBTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,31 @@ End Class
result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DebugEntryPointNotSourceMethodDefinition))
End Sub

<ConditionalFact(GetType(WindowsOnly), Reason:=ConditionalSkipReason.NativePdbRequiresDesktop)>
<WorkItem("https://github.com/dotnet/roslyn/issues/75237")>
Public Sub NativeWriterLimit()
Dim locals = Enumerable.Range(0, 14_000).
Select(Function(i) $"
Dim local{i} As Integer = {i}
M2(local{i})
").
Join(Environment.NewLine)
Dim source = $"
Namespace N
Class C
Shared Sub M1()
{locals}
End Sub
Shared Sub M2(x As Integer)
End Sub
End Class
End Namespace
"
' Cannot emit native PDB for method 'Public Shared Sub M1()' because its debug metadata size 69328 is over the limit 65536.
CreateCompilation(source, options:=TestOptions.DebugDll).VerifyEmitDiagnostics(
Diagnostic(ERRID.ERR_PDBWritingFailed).WithArguments(String.Format(CodeAnalysisResources.SymWriterMetadataOverLimit, "Public Shared Sub M1()", 69328, 65536)).WithLocation(1, 1))
End Sub

#End Region

<ConditionalFact(GetType(WindowsOnly), Reason:=ConditionalSkipReason.NativePdbRequiresDesktop)>
Expand Down
17 changes: 17 additions & 0 deletions src/Test/PdbUtilities/Writer/CustomMetadataSymUnmanagedWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.DiaSymReader;

namespace Roslyn.Test.PdbUtilities;

internal sealed class CustomMetadataSymUnmanagedWriter(SymUnmanagedWriter target, byte[] customMetadata) : DelegatingSymUnmanagedWriter(target)
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly byte[] _customMetadata = customMetadata;

public override void DefineCustomMetadata(byte[] metadata)
{
base.DefineCustomMetadata(_customMetadata);
}
}
Loading