From e6996ff22f74f41e42a03192ef6aa206c7d78e41 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 14 Sep 2022 22:14:25 +0200 Subject: [PATCH] Fix missing virtual C++ function override in C# if the thunk points to vtable index at offset=0 --- Source/Engine/Scripting/ScriptingType.h | 5 ++++- Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Scripting/ScriptingType.h b/Source/Engine/Scripting/ScriptingType.h index 118c81e89..747a52c59 100644 --- a/Source/Engine/Scripting/ScriptingType.h +++ b/Source/Engine/Scripting/ScriptingType.h @@ -487,8 +487,11 @@ FORCE_INLINE int32 GetVTableIndex(void** vtable, int32 entriesCount, void* func) // where XXX is the actual vtable offset we need to read. byte* funcJmp = *(byte*)func == 0x48 ? (byte*)func : (byte*)func + 5 + *(int32*)((byte*)func + 1); funcJmp += 5; - if (*(funcJmp - 1) == 0xa0) + const byte op = *(funcJmp - 1); + if (op == 0xa0) return *(int32*)funcJmp / sizeof(void*); + if (op == 0x20) + return 0; return *(byte*)funcJmp / sizeof(void*); #elif defined(__clang__) // On Clang member function pointer represents the offset from the vtable begin. diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs index dc5190f36..16c2673ff 100644 --- a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs +++ b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs @@ -1351,7 +1351,7 @@ namespace Flax.Build.Bindings contents.AppendLine(" {"); contents.AppendLine($" {functionInfo.UniqueName}_Signature funcPtr = &{classInfo.NativeName}::{functionInfo.Name};"); contents.AppendLine(" const int32 vtableIndex = GetVTableIndex(vtable, entriesCount, *(void**)&funcPtr);"); - contents.AppendLine(" if (vtableIndex > 0 && vtableIndex < entriesCount)"); + contents.AppendLine(" if (vtableIndex >= 0 && vtableIndex < entriesCount)"); contents.AppendLine(" {"); contents.AppendLine($" scriptVTableBase[{scriptVTableIndex} + 2] = vtable[vtableIndex];"); for (int i = 0, count = 0; i < ScriptingLangInfos.Count; i++)