librime icon indicating copy to clipboard operation
librime copied to clipboard

Error when require .so file in lua.

Open BlindingDark opened this issue 4 years ago • 3 comments

I'm trying to write a lua native module extension, here is the code: https://github.com/BlindingDark/wordninja-rs-lua

It's normal to run in the lua interprete

$ lua5.4 -e 'print(require("wordninja").split("iloverust"))'      
i love rust

But when I try to call it in rime-lua, it reports an error

package.cpath = package.cpath .. ";/path/to/wordninja.so"
require("wordninja")
E0325 21:48:14.266957 505823 lua_gears.cc:15] LuaTranslation::Next error(2): error loading module 'wordninja' from file '/home/blindingdark/code/wordninja-rs-lua/wordninja.so':
	/home/blindingdark/code/wordninja-rs-lua/wordninja.so: undefined symbol: lua_getmetatable

According to the code below, is this because rtld_global is disabled (default) in the load_mode?

https://github.com/rime/librime/blob/b2abd094c3e92f3427597d6f40e5cd688f0ea81f/plugins/plugins_module.cc#L53

By the way, I found a hacky way to run it

LD_PRELOAD=/usr/lib/liblua.so fcitx5

But I hope to have a more reliable way.


Related issue https://github.com/hchunhui/librime-lua/issues/97

BlindingDark avatar Mar 26 '21 08:03 BlindingDark

FYI previous discussion on lua plugin and loading dynamic library in fcitx: https://github.com/rime/librime/issues/369 note the thread was before loading plugin library is done by librime itself in librime/plugins/plugins_module.cc.

lotem avatar Mar 27 '21 10:03 lotem

FYI previous discussion on lua plugin and loading dynamic library in fcitx: #369 note the thread was before loading plugin library is done by librime itself in librime/plugins/plugins_module.cc.

does this can fix the following problem?

I use archlinux and all related packages (fcitx5, rime, plasma-meta and sddm-git ) has been updated. But maybe easy_en load environment LD_PRELOAD with some problem with plasma_wayland virtual keyboard ( not run fcitx5 by autostart ). No problem with x11 which not use virtual keyboard.

But if I manually fcitx5 -r -d after logging into plasma in terminal , wordnijia works well. However , code-insiders --ozone-platform-hint=auto --enable-features=WaylandWindowDecorations --enable-wayland-ime can't use fcitx5 now (it can use fcitx5 before fcitx5 -r -d)

Test whether env loaded well: I set the environment in /etc/environment , when I meet with login page in sddm, I open a new tty by 'ctrl+alt+F5' and checked env and whether fcitx5 loaded early than expected there, everything seems to be right.

Then I log into plasma-wayland and check fcitx5-diagnose, I found it loaded env well with XMODIFIERS,etc.

log info in tty5

$ ps aux | grep fcit
user         741  0.0  0.0  10856  2944 tty5     S+   09:19   0:00 grep --color=auto fcit
$ echo $LD_PRELOAD  
/usr/lib/liblua5.4.so
$ ldd /usr/bin/fcitx5 # it seems to preload /usr/lib/liblua5.4.so
        linux-vdso.so.1 (0x00007ffe3b1eb000)
        /usr/lib/liblua5.4.so (0x00007f5397c98000)
        libFcitx5Core.so.7 => /usr/lib/libFcitx5Core.so.7 (0x00007f5397ba1000)
        libFcitx5Config.so.6 => /usr/lib/libFcitx5Config.so.6 (0x00007f5397b80000)
        libFcitx5Utils.so.2 => /usr/lib/libFcitx5Utils.so.2 (0x00007f5397add000)
        libexpat.so.1 => /usr/lib/libexpat.so.1 (0x00007f5397ab2000)
        libxkbcommon.so.0 => /usr/lib/libxkbcommon.so.0 (0x00007f5397a6b000)
        libjson-c.so.5 => /usr/lib/libjson-c.so.5 (0x00007f5397a56000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f5397800000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f53977e0000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f53975f9000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007f5397511000)
        libuuid.so.1 => /usr/lib/libuuid.so.1 (0x00007f5397a4d000)
        libsystemd.so.0 => /usr/lib/libsystemd.so.0 (0x00007f539742e000)
        /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f5397d38000)
        libcap.so.2 => /usr/lib/libcap.so.2 (0x00007f5397a3f000)
        libgcrypt.so.20 => /usr/lib/libgcrypt.so.20 (0x00007f53972e6000)
        liblzma.so.5 => /usr/lib/liblzma.so.5 (0x00007f53972b3000)
        libzstd.so.1 => /usr/lib/libzstd.so.1 (0x00007f53971e3000)
        liblz4.so.1 => /usr/lib/liblz4.so.1 (0x00007f53971c1000)
        libgpg-error.so.0 => /usr/lib/libgpg-error.so.0 (0x00007f539719b000)

error in /tmp/rime.fcitx-rime.ERROR before running fcitx5 -r -d

...
E20230403 09:43:04.625414   789 lua_gears.cc:59] Lua Compoment of initialize  error:( module: easy_en_enhance_filter name_space: easy_en_enhance_filter status: 2 ): error loading module 'wordninja' from file '/usr/lib/lua/5.4/wordninja.so':
    /usr/lib/lua/5.4/wordninja.so: undefined symbol: lua_getmetatable
# duplicate above error some times
E20230403 09:45:43.005071   789 lua_gears.cc:15] LuaTranslation::Next error(2): /home/czg/.local/share/fcitx5/rime/lua/easy_en.lua:59: attempt to call a nil value (upvalue 'wordninja_split')
...

log info in plasma-wayland

$ fcitx5-diagnose
...
## Xim:
1.  `${XMODIFIERS}`:

    Environment variable XMODIFIERS is set to "@im=fcitx" correctly.
    Xim Server Name from Environment variable is fcitx.

2.  XIM_SERVERS on root window:
                                                                                               
    Xim server name is the same with that set in the environment variable.

## Qt:
1.  qt4 - `${QT4_IM_MODULE}`:

    Environment variable QT_IM_MODULE is set to "fcitx" correctly.
...

sci-42ver avatar Apr 03 '23 02:04 sci-42ver

Although not known why the env can't be loaded when ldd output is fine, one workaround that change the config file read by kwinrc works aspired by this wiki

$ cat /usr/share/applications/org.fcitx.Fcitx5.desktop
...
Exec=env LD_PRELOAD=/usr/lib/liblua5.4.so /usr/bin/fcitx5
...

sci-42ver avatar Apr 05 '23 16:04 sci-42ver