From 5fad12adee6682af584f7c60787e518cc77783dd Mon Sep 17 00:00:00 2001 From: Rafael Rivera Date: Wed, 11 Dec 2024 00:30:08 -0800 Subject: [PATCH 1/2] Add additional invalid handle value metadata --- generation/WinSDK/autoTypes.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/generation/WinSDK/autoTypes.json b/generation/WinSDK/autoTypes.json index f7fd6f52..db00656e 100644 --- a/generation/WinSDK/autoTypes.json +++ b/generation/WinSDK/autoTypes.json @@ -1599,6 +1599,7 @@ "Name": "PROCESSTRACE_HANDLE", "ValueType": "ulong", "CloseApi": "CloseTrace", + "InvalidHandleValues": [ -1 ], "NativeTypedef": false }, { @@ -2759,6 +2760,7 @@ "Name": "REGHANDLE", "ValueType": "long", "CloseApi": "EventUnregister", + "InvalidHandleValues": [ 0 ], "NativeTypedef": true }, { @@ -2771,12 +2773,14 @@ "Name": "PDH_HLOG", "ValueType": "DECLARE_HANDLE", "CloseApi": "PdhCloseLog", + "InvalidHandleValues": [ -1, 0 ], "NativeTypedef": true }, { "Name": "PDH_HQUERY", "ValueType": "DECLARE_HANDLE", "CloseApi": "PdhCloseQuery", + "InvalidHandleValues": [ -1, 0 ], "NativeTypedef": true }, { @@ -2789,6 +2793,7 @@ "Name": "PRINTER_HANDLE", "ValueType": "void*", "CloseApi": "ClosePrinter", + "InvalidHandleValues": [ -1, 0 ], "NativeTypedef": false }, { From 456f80725a177d26d70d3640c16f98074d547ac5 Mon Sep 17 00:00:00 2001 From: Rafael Rivera Date: Wed, 11 Dec 2024 00:30:34 -0800 Subject: [PATCH 2/2] Tests --- .../RaiiFreeAttributeTests.cs | 36 +++++++++++++++++++ .../Windows.Win32.Tests.csproj | 4 +++ 2 files changed, 40 insertions(+) create mode 100644 tests/Windows.Win32.Tests/RaiiFreeAttributeTests.cs diff --git a/tests/Windows.Win32.Tests/RaiiFreeAttributeTests.cs b/tests/Windows.Win32.Tests/RaiiFreeAttributeTests.cs new file mode 100644 index 00000000..05042340 --- /dev/null +++ b/tests/Windows.Win32.Tests/RaiiFreeAttributeTests.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using ICSharpCode.Decompiler.TypeSystem; +using MetadataUtils; +using Xunit; + +namespace Windows.Win32.Tests +{ + + public class RaiiFreeAttributeTests + { + [Fact] + public void AllRaiiTypesHaveInvalidHandleValueAttribute() + { + var typeSystem = DecompilerUtils.LoadDecompilerTypeSystem(TestCommon.TestUtils.Win32WinmdPath); + var excludedTypes = new HashSet + { + "Windows.Win32.Foundation.BSTR", // Not a handle type + "Windows.Win32.System.WinRT.HSTRING", // Has no invalid handle value + "Windows.Win32.System.Threading.LPPROC_THREAD_ATTRIBUTE_LIST", // Not a handle type + }; + + var structsMissingMetadata = typeSystem.MainModule.TypeDefinitions + .Where(type => type.Kind == TypeKind.Struct) + .Where(type => type.GetAttributes().Any(attr => attr.AttributeType.Name == "RAIIFreeAttribute")) + .Where(type => !type.GetAttributes().Any(attr => attr.AttributeType.Name == "InvalidHandleValueAttribute")) + .Where(type => !excludedTypes.Contains(type.FullName)) + .Select(type => type.FullName); + + Assert.True(!structsMissingMetadata.Any(), + $"RAII structs missing InvalidHandleValue attribute:{Environment.NewLine}" + + string.Join(Environment.NewLine, structsMissingMetadata.Select(structName => $"- {structName}"))); + } + } +} diff --git a/tests/Windows.Win32.Tests/Windows.Win32.Tests.csproj b/tests/Windows.Win32.Tests/Windows.Win32.Tests.csproj index 3b6f0e9f..c00fd2ef 100644 --- a/tests/Windows.Win32.Tests/Windows.Win32.Tests.csproj +++ b/tests/Windows.Win32.Tests/Windows.Win32.Tests.csproj @@ -6,6 +6,10 @@ false + + + +