diff options
author | Tlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com> | 2022-02-24 23:22:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-24 23:22:30 +0300 |
commit | 74d69676a9b4fa7022f8bdaf5a574e7063d8c9a1 (patch) | |
tree | a298ad7af3fcc60d23d99576691015461780d691 | |
parent | 4a231cedcaf6b93b04d07e5f7670e88e5fa937fe (diff) |
Add check for pointer types in IsComInterop (#2623) (#2635)
Checks if the param or return type of a function is a pointer type and return early that it is not COM interop if it is.
Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
-rw-r--r-- | src/ILLink.RoslynAnalyzer/COMAnalyzer.cs | 3 | ||||
-rw-r--r-- | test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs | 51 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/ILLink.RoslynAnalyzer/COMAnalyzer.cs b/src/ILLink.RoslynAnalyzer/COMAnalyzer.cs index ea6f2f3f8..9b2c237f6 100644 --- a/src/ILLink.RoslynAnalyzer/COMAnalyzer.cs +++ b/src/ILLink.RoslynAnalyzer/COMAnalyzer.cs @@ -81,6 +81,9 @@ namespace ILLink.RoslynAnalyzer if (symbol is IParameterSymbol parameterSymbol) typeSymbol = parameterSymbol.Type; + if (typeSymbol is IPointerTypeSymbol) + return false; + if (typeSymbol == null) return false; diff --git a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs index d64847908..4c513ba89 100644 --- a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs +++ b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Warnings/ComPInvokeWarning.cs @@ -4,12 +4,14 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Text; using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Warnings { [SkipKeptItemsValidation] [ExpectedNoWarnings] [KeptModuleReference ("Foo")] + [SetupCompileArgument ("/unsafe")] class ComPInvokeWarning { [UnconditionalSuppressMessage ("trim", "IL2026")] @@ -32,6 +34,9 @@ namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Warnings Call_CanSuppressWithRequiresUnreferencedCode (); Call_CanSuppressPInvokeWithRequiresUnreferencedCode (); Call_PInvokeWithRequiresUnreferencedCode (); + Call_PInvokeWithVoidPointerArg (); + Call_PInvokeWithStructPointerArg (); + Call_PInvokeWithSequentialStructPointerArg (); } [ExpectedWarning ("IL2050")] @@ -185,6 +190,38 @@ namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Warnings [DllImport ("Foo")] static extern void PInvokeWithRequiresUnreferencedCode (IFoo foo); + static unsafe void Call_PInvokeWithVoidPointerArg () + { + PInvokeWithVoidPointerArg (null); + } + + [DllImport ("Foo")] + static extern unsafe void PInvokeWithVoidPointerArg (void* arg); + + static unsafe void Call_PInvokeWithStructPointerArg () + { + PInvokeWithStructPointerArg (null); + } + + [DllImport ("Foo")] + static extern unsafe ExplicitLayoutStruct* PInvokeWithStructPointerArg (ExplicitLayoutStruct* arg); + + static unsafe void Call_PInvokeWithSequentialStructPointerArg () + { + PInvokeWithSequentialStructPointerArg (null); + } + + [DllImport ("Foo")] + static extern unsafe SequentialLayoutStruct* PInvokeWithSequentialStructPointerArg (SequentialLayoutStruct* arg); + + static unsafe void Call_PInvokeWithAutoStructPointerArg () + { + PInvokeWithAutoStructPointerArg (null); + } + + [DllImport ("Foo")] + static extern unsafe AutoLayoutStruct* PInvokeWithAutoStructPointerArg (AutoLayoutStruct* arg); + interface IFoo { } class TestSafeHandle : SafeHandle @@ -230,5 +267,19 @@ namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Warnings { } + [StructLayout (LayoutKind.Explicit)] + public struct ExplicitLayoutStruct + { + } + + [StructLayout (LayoutKind.Sequential)] + public struct SequentialLayoutStruct + { + } + + [StructLayout (LayoutKind.Auto)] + public struct AutoLayoutStruct + { + } } } |