Browse Source

Fix PInvokeStackImbalance when calling C++ methods (#1453)

* Fix PInvokeStackImbalance when calling C++ methods
pull/1457/head
josetr 5 years ago committed by GitHub
parent
commit
8c4465bb11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/Generator/Passes/CheckAbiParameters.cs
  2. 13
      tests/CSharp/CSharp.Tests.cs
  3. 1
      tests/CSharp/CSharp.cpp
  4. 12
      tests/CSharp/CSharp.h
  5. 28
      tests/Tests.h

11
src/Generator/Passes/CheckAbiParameters.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using CppSharp.AST;
using CppSharp.AST.Extensions;
using System.Linq;
namespace CppSharp.Passes
@ -29,7 +30,15 @@ namespace CppSharp.Passes @@ -29,7 +30,15 @@ namespace CppSharp.Passes
if (!VisitDeclaration(function))
return false;
if (function.IsReturnIndirect)
var isReturnIndirect = function.IsReturnIndirect || (
Context.ParserOptions.IsMicrosoftAbi &&
function is Method &&
!function.ReturnType.Type.Desugar().IsAddress() &&
function.ReturnType.Type.Desugar().TryGetDeclaration(out Class returnTypeDecl) &&
returnTypeDecl.IsPOD &&
returnTypeDecl.Layout.Size <= 8);
if (isReturnIndirect)
{
var indirectParam = new Parameter()
{

13
tests/CSharp/CSharp.Tests.cs

@ -159,6 +159,19 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -159,6 +159,19 @@ public unsafe class CSharpTests : GeneratorTestFixture
}
}
[Test]
public void TestReturnSmallPOD()
{
using (var f = new Foo())
{
foreach(var pod in new[] { f.SmallPodCdecl, f.SmallPodStdcall, f.SmallPodFastcall, f.SmallPodThiscall, f.SmallPodVectorcall })
{
Assert.That(pod.A, Is.EqualTo(10000));
Assert.That(pod.B, Is.EqualTo(40000));
}
}
}
[Test]
public void TestPropertyAccessModifier()
{

1
tests/CSharp/CSharp.cpp

@ -130,7 +130,6 @@ const Foo& Bar::operator[](int i) const @@ -130,7 +130,6 @@ const Foo& Bar::operator[](int i) const
return m_foo;
}
Quux::Quux() : _setterWithDefaultOverload(0)
{

12
tests/CSharp/CSharp.h

@ -9,6 +9,12 @@ @@ -9,6 +9,12 @@
#include "ExcludedUnit.hpp"
#include "CSharpTemplates.h"
struct SmallPOD
{
int a;
int b;
};
class DLL_API Foo
{
public:
@ -40,6 +46,12 @@ public: @@ -40,6 +46,12 @@ public:
static int propertyCall();
static int getGetPropertyCall();
SmallPOD CDECL getSmallPod_cdecl() { return { 10000, 40000 }; }
SmallPOD STDCALL getSmallPod_stdcall() { return { 10000, 40000 }; }
SmallPOD FASTCALL getSmallPod_fastcall() { return { 10000, 40000 }; }
SmallPOD THISCALL getSmallPod_thiscall() { return { 10000, 40000 }; }
SmallPOD VECTORCALL getSmallPod_vectorcall() { return { 10000, 40000 }; }
int operator ++();
int operator --();
operator const char*() const;

28
tests/Tests.h

@ -15,6 +15,19 @@ @@ -15,6 +15,19 @@
#ifndef CDECL
#define CDECL __cdecl
#endif
#ifndef FASTCALL
#define FASTCALL __fastcall
#endif
#ifndef THISCALL
#define THISCALL __thiscall
#endif
#ifndef VECTORCALL
#define VECTORCALL __vectorcall
#endif
#else
#define DLL_API __attribute__ ((visibility ("default")))
@ -30,6 +43,21 @@ @@ -30,6 +43,21 @@
#ifndef CDECL
#define CDECL __attribute__((cdecl))
#endif
#ifndef FASTCALL
#define FASTCALL
#endif
#ifndef THISCALL
#define THISCALL
#endif
#ifndef VECTORCALL
#define VECTORCALL
#endif
#define
#endif
#define CS_OUT

Loading…
Cancel
Save