关于在openresty中使用ffi函数调用的问题

我有一些用C语言写的代码,我用Lua的ffi功能对这些代码进行了封装。当我直接通过luajit调用我的Lua文件时,一切运行正常。但是当我从openresty中调用它时,openresty会输出一个错误日志: exit on signal 11

版本信息:nginx版本:openresty/1.15.8.1 操作系统:debian 9 系统Luajit: LuaJIT 2.1.0-beta3 --版权所有(C) 2005-2017 Mike Pall.

openresty luajit:LuaJIT 2.1.0-beta3 --版权所有(C) 2005-2017 Mike Pall.

我编写了一个测试文件来展示两个平台之间的不同结果:

-- 文件名:gateway/test.lua
local ffi = require "ffi"

ffi.cdef [=[
    typedef struct _TrigxNode {
        int val;
        struct _TrigxNode *char_nodes[64];
        struct _TrigxNode *rgx_next;
        int rgx_raw_len;
        void *re;
        char *rgx;
    } TrigxNode;
    TrigxNode *create_trigx_node();
    void trigx_insert(TrigxNode *root, const char *word, int len_word, int val);
    int trigx_search(TrigxNode *root, const char *word, int len_word);
    void trigx_free(TrigxNode *root);
   ]=];

local C = ffi.load("trigx_tree")

local node = C.create_trigx_node()
C.trigx_insert(node, '/menu/', 6, 10)
local result = C.trigx_search(node, '/menu/', 6)
if ngx ~= nil then
    --  在openresty中测试
    ngx.say("result: " .. result)
else
    -- 在luajit中测试
    print("result: " .. result)
end
return
http {

    init_worker_by_lua_block {
        local uuid = require 'resty.jit-uuid'
        uuid.seed()
        local verbose = false
        if verbose then
            local dump = require "jit.dump"
            dump.on(nil, "./jit.log")
        else
            local v = require "jit.v"
            v.on("./jit.log")
        end
        require "resty.core"
    }

    server {
        listen 9090;

        location / {
            content_by_lua_file "gateway/test.lua";
        }

        location /login {
            default_type 'application/json';
            content_by_lua_file 'gateway/login.lua';
        }

        location /wechat-callback {
            content_by_lua_file 'gateway/wechat_identify.lua';
        }

    }
}

当我直接通过luajit调用ffi函数时,luajit -i gateway/test.lua,我可以看到正确的结果被打印出来

但是当我尝试通过curl 'localhost:9090/abc'来调用test.lua时,就会出现错误

这里有two pictures显示了结果的详细信息。

点赞
用户9968475
用户9968475

我在源文件中使用calloc而非malloc,问题得以解决。但我仍然无法找出为什么这在luajit中可以工作。

2019-07-17 04:15:44