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