【发布时间】: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 调用约定调用的。到目前为止,我似乎很幸运,我的代码在没有正确约定的情况下仍然可以正常运行。无论如何,感谢您花时间消除我的误解。