From 632e67550db56bd6f8b4eed696d06080eb0b2ca2 Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:14:16 -0800 Subject: [PATCH] Fix virtual destructor vtable lookup Select the destructor variant used by Clang's Microsoft vtable layout instead of always requesting the scalar-deleting destructor. Add regression coverage for large virtual method counts. --- sources/libClangSharp/ClangSharp.cpp | 21 +++--- ...rgeVirtualMethodCountWithDestructorTest.cs | 68 +++++++++++++++++++ 2 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/LargeVirtualMethodCountWithDestructorTest.cs diff --git a/sources/libClangSharp/ClangSharp.cpp b/sources/libClangSharp/ClangSharp.cpp index 1a946a47..1b8f8ec8 100644 --- a/sources/libClangSharp/ClangSharp.cpp +++ b/sources/libClangSharp/ClangSharp.cpp @@ -14,6 +14,7 @@ #endif #include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -108,14 +109,18 @@ int64_t getVtblIdx(const GlobalDecl& d) VTableContextBase* VTC = CMD->getASTContext().getVTableContext(); if (MicrosoftVTableContext* MSVTC = dyn_cast(VTC)) { - MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(d); - - // clang 22's Microsoft vftable layout reports Index == 0 for a virtual destructor - // regardless of its declaration position, so it collides with whatever occupies slot 0 - // whenever the destructor is not declared first. The vftable layout itself is ordered - // correctly, so recover the true slot by counting the function-pointer components that - // precede the deleting-destructor component (non-slot components such as offsets are - // skipped so the count stays in the same space as MethodVFTableLocation::Index). + GlobalDecl method = d; + + if (const CXXDestructorDecl* DD = dyn_cast(CMD)) { + ASTContext& context = CMD->getASTContext(); + CXXDtorType dtorType = context.getTargetInfo().emitVectorDeletingDtors(context.getLangOpts()) ? Dtor_VectorDeleting : Dtor_Deleting; + method = GlobalDecl(DD, dtorType); + } + + MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(method); + + // Recover the destructor slot from the layout, counting only function-pointer + // components so the count stays in the same space as MethodVFTableLocation::Index. if (const CXXDestructorDecl* DD = dyn_cast(CMD)) { const VTableLayout& layout = MSVTC->getVFTableLayout(RD, ML.VFPtrOffset); int64_t slot = 0; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/LargeVirtualMethodCountWithDestructorTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/LargeVirtualMethodCountWithDestructorTest.cs new file mode 100644 index 00000000..1ab2d84b --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/LargeVirtualMethodCountWithDestructorTest.cs @@ -0,0 +1,68 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Globalization; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace ClangSharp.UnitTests; + +[TestFixture] +public sealed class LargeVirtualMethodCountWithDestructorTest : PInvokeGeneratorTest +{ + [TestCase(190, false)] + [TestCase(191, false)] + [TestCase(192, false)] + [TestCase(191, true)] + public async Task GeneratesAllVirtualMethods(int methodCount, bool destructorLast) + { + var input = new StringBuilder("class Example { public:\n"); + + if (!destructorLast) + { + _ = input.AppendLine("virtual ~Example() {}"); + } + + for (var i = 0; i < methodCount; i++) + { + _ = input.AppendLine(CultureInfo.InvariantCulture, $"virtual bool Method{i}() = 0;"); + } + + if (destructorLast) + { + _ = input.AppendLine("virtual ~Example() {}"); + } + + _ = input.AppendLine("};"); + + var actual = await GenerateBindingsAsync( + inputContents: input.ToString(), + outputMode: PInvokeGeneratorOutputMode.CSharp, + configOptions: PInvokeGeneratorConfigurationOptions.None, + excludedNames: null, + remappedNames: null, + withAccessSpecifiers: null, + withAttributes: null, + withCallConvs: null, + withClasses: null, + withLibraryPaths: null, + withNamespaces: null, + withSetLastErrors: null, + withTransparentStructs: null, + withTypes: null, + withUsings: null, + withPackings: null, + expectedDiagnostics: null, + libraryPath: DefaultLibraryPath, + commandLineArgs: null, + language: "c++", + languageStandard: DefaultCppStandard).ConfigureAwait(false); + + Assert.That(actual, Does.Contain("public void Dispose()")); + + for (var i = 0; i < methodCount; i++) + { + Assert.That(actual, Does.Contain($"public bool Method{i}()")); + } + } +}