microsoft / microsoft/CsWin32

Exclude Foundation.Metadata From NativeMethods.txt To Prevent Stack Overflows

Open
#1,818 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Actual behavior

It might be a good idea to exclude any API found in the Windows.Win32.Foundation.Metadata namespace in the Windows.Win32.winmd metadata package from being used in the NativeMethods.txt files. Entering the attribute classes causes stack overflows in CsWin32 compilation.

On looking over the contents of the Windows.Win32.Foundation.Metadata namespace, it is doubtful for them to be intended to be used as Windows API, so they should likely be excluded from being used in the NativeMethods.txt files.

Expected behavior

Either warn or ignore the types and members found in the Windows.Win32.Foundation.Metadata namespace in the Windows.Win32.winmd file.

Repro steps

  1. NativeMethods.txt content:
// Foundation.Metadata
// Enums
Architecture
// Classes
AgileAttribute
AlsoUsableForAttribute
AnsiAttribute
AssociatedConstantAttribute
AssociatedEnumAttribute
CanReturnErrorsAsSuccessAttribute
CanReturnMultipleSuccessValuesAttribute
ComOutPtrAttribute
ConstantAttribute
ConstAttribute
CppAttributeList
DocumentationAttribute
DoNotReleaseAttribute
FlexibleArrayAttribute
FreeWithAttribute
GuidAttribute
IgnoreIfReturnAttribute
InvalidHandleValueAttribute
MemorySizeAttribute
MetadataTypedefAttribute
NativeArrayInfoAttribute
NativeBitfieldAttribute
NativeEncodingAttribute
NativeInheritanceAttribute
NativeTypedefAttribute
NativeTypeNameAttribute
NotNullTerminatedAttribute
NullNullTerminatedAttribute
RAIIFreeAttribute
ReservedAttribute
RetainedAttribute
RetValAttribute
ScopedEnumAttribute
StaticLibraryAttribute
StructSizeFieldAttribute
SupportedArchitectureAttribute
SupportedOSPlatformAttribute
UnicodeAttribute
  1. NativeMethods.json content (if present):
  1. Any of your own code that should be shared?
Context

Error:

   at Microsoft.CodeAnalysis.CommonCompiler.CompileAndEmit(Microsoft.CodeAnalysis.TouchedFileLogger, Microsoft.CodeAnalysis.Compilation ByRef, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.ISourceGenerator>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.AdditionalText>, Microsoft.CodeAnalysis.AnalyzerConfigSet, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.AnalyzerConfigOptionsResult>, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.EmbeddedText>, Microsoft.CodeAnalysis.DiagnosticBag, Microsoft.CodeAnalysis.ErrorLogger, System.Threading.CancellationToken, System.Threading.CancellationTokenSource ByRef, Microsoft.CodeAnalysis.Diagnostics.AnalyzerDriver ByRef, System.Nullable`1<Microsoft.CodeAnalysis.GeneratorDriverTimingInfo> ByRef)
Stack overflow.
Repeated 146 times:
--------------------------------
   at Microsoft.Windows.CsWin32.Generator.IsManagedType(System.Reflection.Metadata.TypeDefinitionHandle)
   at Microsoft.Windows.CsWin32.Generator.IsManagedType(Microsoft.Windows.CsWin32.TypeHandleInfo)
   at Microsoft.Windows.CsWin32.HandleTypeHandleInfo.ToTypeSyntax(Microsoft.Windows.CsWin32.TypeSyntaxSettings, GeneratingElement, System.Nullable`1<Microsoft.Windows.CsWin32.QualifiedCustomAttributeHandleCollection>, System.Reflection.ParameterAttributes)
   at Microsoft.Windows.CsWin32.Generator.<IsManagedType>g__Helper|385_0(System.Reflection.Metadata.TypeDefinitionHandle, <>c__DisplayClass385_0 ByRef)
   at Microsoft.Windows.CsWin32.Generator+<>c__DisplayClass158_0.<RequestInteropType>b__0()
   at Microsoft.Windows.CsWin32.Generator+GeneratedCode.GenerationTransaction(System.Action)
   at Microsoft.Windows.CsWin32.Generator.TryGenerateType(System.String, System.Collections.Generic.IReadOnlyCollection`1<System.String> ByRef)
   at Microsoft.Windows.CsWin32.Generator.TryGenerate(System.String, System.Collections.Generic.IReadOnlyCollection`1<System.String> ByRef, System.Threading.CancellationToken)
   at Microsoft.Windows.CsWin32.SuperGenerator.TryGenerate(System.String, System.Collections.Generic.IReadOnlyCollection`1<System.String> ByRef, System.Collections.Generic.IReadOnlyCollection`1<System.String> ByRef, System.Threading.CancellationToken)
   at Microsoft.Windows.CsWin32.SourceGenerator.Execute(Microsoft.CodeAnalysis.GeneratorExecutionContext)
   at Microsoft.CodeAnalysis.SourceGeneratorAdaptor.<Initialize>b__6_5(Microsoft.CodeAnalysis.SourceProductionContext, GeneratorContextBuilder)
   at Microsoft.CodeAnalysis.UserFunctionExtensions+<>c__DisplayClass3_0`2[[Microsoft.CodeAnalysis.SourceProductionContext, Microsoft.CodeAnalysis, Version=5.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35],[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<WrapUserAction>b__0(Microsoft.CodeAnalysis.SourceProductionContext, System.__Canon, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.SourceOutputNode`1[[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].InvokeUserAction(Microsoft.CodeAnalysis.AdditionalSourcesCollection, Microsoft.CodeAnalysis.DiagnosticBag, Builder, System.__Canon, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.AbstractSourceOutputNode`1[[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].UpdateStateTable(Builder, Microsoft.CodeAnalysis.NodeStateTable`1<System.ValueTuple`2<System.Collections.Generic.IEnumerable`1<Microsoft.CodeAnalysis.GeneratedSourceText>,System.Collections.Generic.IEnumerable`1<Microsoft.CodeAnalysis.Diagnostic>>>, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.DriverStateTable+Builder.GetLatestStateTableForNode[[System.ValueTuple`2[[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Microsoft.CodeAnalysis.IIncrementalGeneratorNode`1<System.ValueTuple`2<System.__Canon,System.__Canon>>)
   at Microsoft.CodeAnalysis.AbstractSourceOutputNode`1[[System.__Canon, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].AppendOutputs(Microsoft.CodeAnalysis.IncrementalExecutionContext, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.GeneratorDriver.UpdateOutputs(System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.IIncrementalGeneratorOutputNode>, Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind, Builder, System.Collections.Immutable.ImmutableHashSet`1<System.String>, System.Threading.CancellationToken, Builder)
   at Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsCore(Microsoft.CodeAnalysis.Compilation, Microsoft.CodeAnalysis.DiagnosticBag, System.Func`2<Microsoft.CodeAnalysis.GeneratorFilterContext,Boolean>, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.GeneratorDriver.RunGeneratorsAndUpdateCompilation(Microsoft.CodeAnalysis.Compilation, Microsoft.CodeAnalysis.Compilation ByRef, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.Diagnostic> ByRef, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.CommonCompiler.RunGenerators(Microsoft.CodeAnalysis.Compilation, System.String, Microsoft.CodeAnalysis.ParseOptions, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.ISourceGenerator>, Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider, System.Collections.Immutable.ImmutableArray`1<Microsoft.CodeAnalysis.AdditionalText>, Microsoft.CodeAnalysis.DiagnosticBag)
   at Microsoft.CodeAnalysis.CommonCompiler.<RunCore>g__compileAndReport|73_0(<>c__DisplayClass73_0 ByRef)
   at Microsoft.CodeAnalysis.CommonCompiler.RunCore(System.IO.TextWriter, Microsoft.CodeAnalysis.ErrorLogger, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.CommonCompiler.Run(System.IO.TextWriter, System.Threading.CancellationToken)
   at Microsoft.CodeAnalysis.CommandLine.ConsoleUtil.RunWithUtf8Output[[System.Int32, System.Private.CoreLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Func`2<System.IO.TextWriter,Int32>)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.Run(System.String[], Microsoft.CodeAnalysis.BuildPaths, System.IO.TextWriter, Microsoft.CodeAnalysis.IAnalyzerAssemblyLoader)
   at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunCompilation(System.Collections.Generic.IEnumerable`1<System.String>, Microsoft.CodeAnalysis.BuildPaths, System.IO.TextWriter, System.String)
   at Microsoft.CodeAnalysis.CommandLine.BuildClient.Run(System.Collections.Generic.IEnumerable`1<System.String>, Microsoft.CodeAnalysis.CommandLine.RequestLanguage, Microsoft.CodeAnalysis.CommandLine.CompileFunc, Microsoft.CodeAnalysis.CommandLine.CompileOnServerFunc, Microsoft.CodeAnalysis.CommandLine.ICompilerServerLogger)
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.MainCore(System.String[])
   at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[])
  • CsWin32 version: 0.3.333
  • Win32Metadata version (if explicitly set by project): 0.1.42-alpha
  • Target Framework: NET11.0
  • LangVersion Preview

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the shown NativeMethods.txt entries and start by tracing Generator.IsManagedType, HandleTypeHandleInfo.ToTypeSyntax, and TryGenerate in the stack trace. The change is complete when types and members in Windows.Win32.Foundation.Metadata are either ignored or reported without causing a stack overflow during CsWin32 compilation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.