Skip to content

Commit

Permalink
Free returned by value instances, not object ones
Browse files Browse the repository at this point in the history
This was a dangerous bug by as the object remained in a deleted state to be used later.

Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Sep 18, 2021
1 parent e2d0dce commit 50feab0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,16 @@ public override bool VisitClassDecl(Class @class)
if (Context.Context.ParserOptions.IsMicrosoftAbi)
vtableIndex = @class.Layout.VFTables.IndexOf(@class.Layout.VFTables.First(
v => v.Layout.Components.Any(c => c.Method == dtor)));
string instance = $"new {typePrinter.IntPtrType}(&{Context.ReturnVarName})";
Context.Before.WriteLine($@"var __vtables = new IntPtr[] {{ {
string.Join(", ", originalClass.Layout.VTablePointers.Select(
x => $" * (IntPtr*) ({ Helpers.InstanceIdentifier} + {x.Offset})"))} }};");
Context.Before.WriteLine($"var __slot = *(IntPtr*) (__vtables[{vtableIndex}] + {i} * sizeof(IntPtr));");
Context.Before.Write($"Marshal.GetDelegateForFunctionPointer<{dtor.FunctionType}>(__slot)({Helpers.InstanceIdentifier}");
x => $"*({typePrinter.IntPtrType}*) ({instance} + {x.Offset})"))} }};");
Context.Before.WriteLine($@"var __slot = *({typePrinter.IntPtrType}*) (__vtables[{
vtableIndex}] + {i} * sizeof({typePrinter.IntPtrType}));");
Context.Before.Write($"Marshal.GetDelegateForFunctionPointer<{dtor.FunctionType}>(__slot)({instance}");
if (dtor.GatherInternalParams(Context.Context.ParserOptions.IsItaniumLikeAbi).Count > 1)
{
Context.Before.WriteLine(", 0");
Context.Before.Write(", 0");
}
Context.Before.WriteLine(");");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2689,7 +2689,7 @@ public void GenerateMethod(Method method, Class @class)
}

private bool GenerateMethodBody(Class @class, Method method,
QualifiedType returnType = default(QualifiedType))
QualifiedType returnType = default)
{
var specialization = @class as ClassTemplateSpecialization;
if (specialization != null)
Expand Down
3 changes: 3 additions & 0 deletions tests/Common/Common.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public unsafe void TestCodeGeneration()
{
hasPropertyNamedAsParent.hasPropertyNamedAsParent.GetHashCode();
}
using (Common.FreeFunctionReturnsVirtualDtor)
{
}
EnumWithUnderscores.lOWER_BEFORE_CAPITAL.GetHashCode();
EnumWithUnderscores.UnderscoreAtEnd.GetHashCode();
EnumWithUnderscores.CAPITALS_More.GetHashCode();
Expand Down
17 changes: 17 additions & 0 deletions tests/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,18 @@ AbstractFoo::~AbstractFoo()
{
}

ImplementsAbstractFoo::ImplementsAbstractFoo()
{
}

ImplementsAbstractFoo::ImplementsAbstractFoo(const ImplementsAbstractFoo& other)
{
}

ImplementsAbstractFoo::~ImplementsAbstractFoo()
{
}

int ImplementsAbstractFoo::pureFunction(typedefInOverride i)
{
return 5;
Expand Down Expand Up @@ -1349,6 +1361,11 @@ ReturnByValueWithReturnParam ReturnByValueWithReturnParamFactory::generate()
return ReturnByValueWithReturnParam();
}

ImplementsAbstractFoo freeFunctionReturnsVirtualDtor()
{
return ImplementsAbstractFoo();
}

void integerOverload(int i)
{
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ class DLL_API AbstractFoo
class DLL_API ImplementsAbstractFoo : public AbstractFoo
{
public:
ImplementsAbstractFoo();
ImplementsAbstractFoo(const ImplementsAbstractFoo& other);
~ImplementsAbstractFoo();
typedef int typedefInOverride;
virtual int pureFunction(typedefInOverride i = 0);
virtual int pureFunction1();
Expand Down Expand Up @@ -1455,6 +1458,7 @@ template<int N> using TypeAlias = InvokeGenSeq<DerivedTypeAlias<N>>;
template<int N>
struct DerivedTypeAlias : TypeAlias<N / 2> {};

DLL_API ImplementsAbstractFoo freeFunctionReturnsVirtualDtor();
DLL_API void integerOverload(int i);
DLL_API void integerOverload(unsigned int i);
DLL_API void integerOverload(long i);
Expand Down

0 comments on commit 50feab0

Please sign in to comment.