【问题标题】:a problem about ffi function call in openrestyopenresty中关于ffi函数调用的问题
【发布时间】:2019-07-16 08:49:36
【问题描述】:

我有一些用 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。

我写了一个测试文件来显示两个平台的不同结果

-- filename: 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
    --  test in openresty
    ngx.say("result: " .. result)
else
    -- test in 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' 调用 nginx worker 中的 test.lua 时,发生了错误

twopictures显示结果详情

【问题讨论】:

  • luajit 直接调用测试文件的结果link
  • 好吧,你为什么滥用粗体而不是code作为代码,为什么图片而不是在代码块中复制粘贴给我们?那说signal 11 是SIGSEGV(分段错误),这意味着您试图访问范围之外的内存。因为您实际上并没有自己操作任何指针,所以它要么在 trigx 中,要么在 nginx 中。

标签: luajit openresty


【解决方案1】:

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

【讨论】:

  • callocmalloc 都不在您的源文件中的任何位置(就您所显示的而言),因此我们无法为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多