【问题标题】:Why doesn't LuaJIT's FFI module require declared calling conventions?为什么 LuaJIT 的 FFI 模块不需要声明调用约定?
【发布时间】:2015-02-07 10:20:58
【问题描述】:

这是我一直好奇的事情:我想知道 LuaJIT 的 FFI 模块如何设法使用正确的调用约定来调用外部本地函数,而无需在用户原型中进行任何声明。

我尝试通读源代码以自己解决这个问题,但事实证明,要找到我要找的东西太难了,因此我们将不胜感激。

编辑

为了验证调用约定在未声明时是自动确定的,我编写了以下 32 位测试 DLL 以使用 MSVC 的 C 编译器进行编译:

// Use multibyte characters for our default char type
#define _MBCS 1

// Speed up build process with minimal headers.
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN

// System includes
#include <windows.h>
#include <stdio.h>

#define CALLCONV_TEST(CCONV) \
    int __##CCONV test_##CCONV(int arg1, float arg2, const char* arg3) \
    { \
        return CALLCONV_WORK(arg1, arg2, arg3); \
        __pragma(comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__ )) \
    }

#define CALLCONV_WORK(arg1,arg2,arg3) \
    test_calls_work(__FUNCTION__, arg1, arg2, arg3, __COUNTER__);

static int test_calls_work(const char* funcname, int arg1, float arg2, const char* arg3, int retcode)
{
    printf("[%s call]\n", funcname);
    printf("  arg1 => %d\n", arg1);
    printf("  arg2 => %f\n", arg2);
    printf("  arg3 => \"%s\"\n", arg3);
    printf("  <= return %d\n", retcode);
    return retcode;
}

CALLCONV_TEST(cdecl)     // => int __cdecl    test_cdecl(int arg1, float arg2, const char* arg3);
CALLCONV_TEST(stdcall)   // => int __stdcall  test_stdcall(int arg1, float arg2, const char* arg3);
CALLCONV_TEST(fastcall)  // => int __fastcall test_fastcall(int arg1, float arg2, const char* arg3);

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    if(dwReason == DLL_PROCESS_ATTACH) {
        DisableThreadLibraryCalls(hInstance);
    }
    return TRUE;
}

然后我编写了一个 LUA 脚本,用于使用 ffi 模块调用导出的函数:

local ffi = require('ffi')
local testdll = ffi.load('ljffi-test.dll')

ffi.cdef[[
int test_cdecl(int arg1, float arg2, const char* arg3);
int test_stdcall(int arg1, float arg2, const char* arg3);
int test_fastcall(int arg1, float arg2, const char* arg3);
]]

local function run_tests(arg1, arg2, arg3)
    local function cconv_test(name)
        local funcname = 'test_' .. name
        local handler = testdll[funcname]
        local ret = tonumber(handler(arg1, arg2, arg3))
        print(string.format('  => got %d\n', ret))
    end

    cconv_test('cdecl')
    cconv_test('stdcall')
    cconv_test('fastcall')
end

run_tests(3, 1.33, 'string value')

编译 DLL 并运行脚本后,我收到以下输出:

[test_cdecl call]
  arg1 => 3
  arg2 => 1.330000
  arg3 => "string value"
  <= return 0
  => got 0

[test_stdcall call]
  arg1 => 3
  arg2 => 1.330000
  arg3 => "string value"
  <= return 1
  => got 1

[test_fastcall call]
  arg1 => 0
  arg2 => 0.000000
  arg3 => "(null)"
  <= return 2
  => got 2

如您所见,ffi 模块准确地解析了__cdecl 调用约定和__stdcall 调用约定的调用约定。 (但似乎错误地调用了 __fastcall 函数)

最后,我包含了 dumpbin 的输出以显示所有函数都以未修饰的名称导出。

> dumpbin.exe /EXPORTS ljffi-test.dll
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file ljffi-test.dll

File Type: DLL

  Section contains the following exports for ljffi-test.dll

    00000000 characteristics
    548838D4 time date stamp Wed Dec 10 04:13:08 2014
        0.00 version
           1 ordinal base
           3 number of functions
           3 number of names

    ordinal hint RVA      name

          1    0 00001000 test_cdecl
          2    1 000010C0 test_fastcall
          3    2 00001060 test_stdcall

  Summary

        1000 .data
        1000 .rdata
        1000 .reloc
        1000 .text

编辑 2

澄清一下,因为调用约定只与 32 位 Windows 编译器真正相关,所以这是这个问题的主要焦点。 (除非我弄错了,针对 Win64 平台的编译器只使用 FASTCALL 调用约定,而 GCC 对 LuaJIT 支持的所有其他平台使用 CDECL 调用约定)

据我所知,唯一可以找到从 PE 文件中导出的函数信息的地方是 IMAGE_EXPORT_DIRECTORY,如果在没有修饰符的情况下导出函数名称,则没有剩余信息表明特定的调用约定函数。

按照这个逻辑,我能想到的唯一确定函数调用约定的方法是分析导出函数的汇编,并根据堆栈使用情况确定约定。不过,当我考虑到不同编译器和优化级别产生的差异时,这似乎有点多。

【问题讨论】:

  • LuaJIT 对所有测试调用都使用默认的 __cdecl 的第一印象。第二次调用看起来是正确的,但它会损坏堆栈。所以下一个调用会产生不好的输出,而你很幸运没有崩溃。我目前无法对其进行测试,请尝试重新排序呼叫。
  • 是的,我继续通过调试器运行调用,就像您想象的那样,所有三个函数都是使用 __cdecl 调用约定调用的。到目前为止,我似乎很幸运,我的代码在没有正确约定的情况下仍然可以正常运行。无论如何,感谢您花时间消除我的误解。

标签: ffi luajit


【解决方案1】:

调用约定取决于平台。 通常有一个平台的默认值,您可以指定其他平台。

来自http://luajit.org/ext_ffi_semantics.html

C 解析器符合 C99 语言标准以及以下扩展:

...

GCC 属性,具有以下属性:aligned、packed、mode、vector_size、cdecl、fastcall、stdcall、thiscall。

...

MSVC __cdecl、__fastcall、__stdcall、__thiscall、__ptr32、__ptr64、

最有趣的是Win32。这里的调用约定可能用装饰器编码Win32 calling conventions

LuaJIT 有识别装饰器的代码。

此外,LuaJIT 默认使用 __stdcall 调用约定 WinAPI Dll:kernel32.dll、user32.dll 和 gdi32.dll。

【讨论】:

  • (对于以下内容,我们假设我只讨论 Windows 上的 32 位 DLL)如果我正确理解您的答案,您是说 FFI 模块默认为 STDCALL 约定对于提到的三个系统 DLL,并尝试根据从加载的 DLL 导出的名称解析其他所有内容的调用约定。这似乎是真的,但它并没有说明当函数名称在没有装饰器的情况下导出时,luajit 如何仍然设法使用正确的调用约定。 (通过使用 .def 文件或链接器参数)参见我上面的示例。
猜你喜欢
  • 2011-10-09
  • 2011-08-20
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
相关资源
最近更新 更多