Skip to content

[Bug]: JitCall fails when func arg takes a reference to funcs #1007

@Vipul-Cariappa

Description

@Vipul-Cariappa

Description of bug

The following test fails:

TYPED_TEST(CPPINTEROP_TEST_MODE, FunctionReflection_FunctionPointerReferences) {
    std::vector<Decl*> Decls;
    std::string code = R"(
        bool callme_ptr(bool (f)()) { return f(); }
        bool callme_ref(bool (&f)()) { return f(); }
        bool f() { return true; }
      )";

    GetAllTopLevelDecls(code, Decls);

    auto f1 = Cpp::MakeFunctionCallable(Decls[0]);
    EXPECT_EQ(f1.getKind(), Cpp::JitCall::Kind::kGenericCall);

    auto f2 = Cpp::MakeFunctionCallable(Decls[2]);
    EXPECT_EQ(f2.getKind(), Cpp::JitCall::Kind::kGenericCall); // fails
}

Note the pointer function works fine, i.e. bool callme_ptr(bool (f)()) { return f(); }. The reference version does not compile.

Code generated:

extern "C" void __jc_52(void* obj, unsigned long nargs, void** args, void* ret)
{
   if (ret) {
      new (ret) (bool) (((bool (&)(bool (&)()))callme_ref)((bool ()&)*(bool ()*)args[0]));
      return;
   }
   else {
      (void)(((bool (&)(bool (&)()))callme_ref)((bool ()&)*(bool ()*)args[0]));
      return;
   }
}

Required code:

-        new (ret) (bool) (((bool (&)(bool (&)()))callme_ref)((bool ()&)*(bool ()*)args[0]));
+        new (ret) (bool) (((bool (&)(bool (&)()))callme_ref)((bool(&)())*(bool(*)())args[0])); 

What operating system was you using when the bug occured?

No response

What is the architechture of the cpu on your system?

No response

What did you build CppInterOp against?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions