Skip to content

a problem with lua ffi in openresty #1562

@mike96265

Description

@mike96265

software

nginx version: openresty/1.15.8.1

operating system: debian 9

system luajit: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

openresty luajit: LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/

test case

-- 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
# filename: conf.d/nginx.conf
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';
        }

    }
}

when i call the ffi function by shell, luajit -i gateway/test.lua,I can see the right result are printed

image

but when i try to call test.lua in nginx worker, curl 'localhost:9090/abc',error occured

image

I have no idea about how this problem occured

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions