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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
77 changes: 77 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/PDB/PDBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
using System.Text;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
Expand All @@ -22,6 +23,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 +369,81 @@ public void SymWriterErrors4()
Assert.False(result.Success);
}

[ConditionalTheory(typeof(WindowsOnly), Reason = ConditionalSkipReason.NativePdbRequiresDesktop)]
[WorkItem("https://github.com/dotnet/roslyn/issues/75237")]
[CombinatorialData]
public void NativeWriterLimit_Under([CombinatorialRange(PdbWriter.CustomMetadataByteLimit - 9, 10)] int length)
{
CompileWithMockedCustomMetadata(length).Diagnostics.Verify();
}

[ConditionalTheory(typeof(WindowsOnly), Reason = ConditionalSkipReason.NativePdbRequiresDesktop)]
[WorkItem("https://github.com/dotnet/roslyn/issues/75237")]
[CombinatorialData]
public void NativeWriterLimit_Over([CombinatorialRange(PdbWriter.CustomMetadataByteLimit + 1, 10)] int length)
{
CompileWithMockedCustomMetadata(length).Diagnostics.Verify(
// error CS0041: Unexpected error writing debug information -- 'Insufficient memory to continue the execution of the program.'
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused. This test is linked to #75237 but exhibits the same/current behavior described in the issue.

Is this a different scenario than what customers had reported? Should we report a more specific error here (which limit was hit)?

Copy link
Member Author

@jjonescz jjonescz Oct 1, 2024

Choose a reason for hiding this comment

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

The purpose of these two tests (_Over and _Under) is to verify the exact limit value. To do that, they use a custom writer which intercepts metadata writes and passes a custom buffer with the specified size to the native writer (to see if it fails or not with the buffer). The specific error reporting logic happens before that, so it's not hit. I don't think there's a straightforward way to test that higher-level logic with exact buffer sizes. But NativeWriterLimit_EndToEnd tests the specific error (albeit the buffer size cannot be set exactly).

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 65504.'
Diagnostic(ErrorCode.FTL_DebugEmitFailure).WithArguments(string.Format(CodeAnalysisResources.SymWriterMetadataOverLimit, "N.C.M1()", 69096, 65504)).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 @@ -26,6 +26,7 @@ namespace Microsoft.Cci
internal sealed class PdbWriter : IDisposable
{
internal const uint Age = 1;
internal const int CustomMetadataByteLimit = 65_504;

private readonly HashAlgorithmName _hashAlgorithmNameOpt;
private readonly string _fileName;
Expand Down Expand Up @@ -148,6 +149,15 @@ public void SerializeDebugInfo(IMethodBody methodBody, StandaloneSignatureHandle

if (blob.Length > 0)
{
if (blob.Length > CustomMetadataByteLimit)
{
throw new SymUnmanagedWriterException(string.Format(
CodeAnalysisResources.SymWriterMetadataOverLimit,
methodBody.MethodDefinition,
blob.Length,
CustomMetadataByteLimit));
}
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.


_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 65504.
CreateCompilation(source, options:=TestOptions.DebugDll).VerifyEmitDiagnostics(
Diagnostic(ERRID.ERR_PDBWritingFailed).WithArguments(String.Format(CodeAnalysisResources.SymWriterMetadataOverLimit, "Public Shared Sub M1()", 69328, 65504)).WithLocation(1, 1))
End Sub

#End Region

<ConditionalFact(GetType(WindowsOnly), Reason:=ConditionalSkipReason.NativePdbRequiresDesktop)>
Expand Down
Loading
Loading