From 0e42c81c7fd429529d89458349c7cdde254d5406 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 12 Oct 2024 08:07:05 +0800 Subject: fix(lua): avoid recursive vim.on_key() callback (#30753) --- src/nvim/lua/executor.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index d4940f3add..9392765f40 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2065,6 +2065,13 @@ char *nlua_register_table_as_callable(const typval_T *const arg) void nlua_execute_on_key(int c, char *typed_buf) { + static bool recursive = false; + + if (recursive) { + return; + } + recursive = true; + char buf[MB_MAXBYTES * 3 + 4]; size_t buf_len = special_to_buf(c, mod_mask, false, buf); vim_unescape_ks(typed_buf); @@ -2103,6 +2110,8 @@ void nlua_execute_on_key(int c, char *typed_buf) // [ ] assert(top == lua_gettop(lstate)); #endif + + recursive = false; } // Sets the editor "script context" during Lua execution. Used by :verbose. -- cgit From b34e137e43d359c8db4fb76028dea3b410842aff Mon Sep 17 00:00:00 2001 From: errael Date: Thu, 31 Oct 2024 18:11:15 -0700 Subject: feat(lua): allow vim.on_key() callback to consume the key (#30939) --- src/nvim/lua/executor.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9392765f40..27ebfacc5f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2063,12 +2063,13 @@ char *nlua_register_table_as_callable(const typval_T *const arg) return name; } -void nlua_execute_on_key(int c, char *typed_buf) +/// @return true to discard the key +bool nlua_execute_on_key(int c, char *typed_buf) { static bool recursive = false; if (recursive) { - return; + return false; } recursive = true; @@ -2097,9 +2098,15 @@ void nlua_execute_on_key(int c, char *typed_buf) int save_got_int = got_int; got_int = false; // avoid interrupts when the key typed is Ctrl-C - if (nlua_pcall(lstate, 2, 0)) { + bool discard = false; + if (nlua_pcall(lstate, 2, 1)) { nlua_error(lstate, _("Error executing vim.on_key Lua callback: %.*s")); + } else { + if (lua_isboolean(lstate, -1)) { + discard = lua_toboolean(lstate, -1); + } + lua_pop(lstate, 1); } got_int |= save_got_int; @@ -2112,6 +2119,7 @@ void nlua_execute_on_key(int c, char *typed_buf) #endif recursive = false; + return discard; } // Sets the editor "script context" during Lua execution. Used by :verbose. -- cgit From 3688a333544251c887d78e6501eec55f0fb685f8 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 2 Nov 2024 10:11:06 +0800 Subject: fix(lua): show stacktrace for error in vim.on_key() callback (#31021) --- src/nvim/lua/executor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 27ebfacc5f..e4da274204 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2099,9 +2099,9 @@ bool nlua_execute_on_key(int c, char *typed_buf) int save_got_int = got_int; got_int = false; // avoid interrupts when the key typed is Ctrl-C bool discard = false; - if (nlua_pcall(lstate, 2, 1)) { - nlua_error(lstate, - _("Error executing vim.on_key Lua callback: %.*s")); + // Do not use nlua_pcall here to avoid duplicate stack trace information + if (lua_pcall(lstate, 2, 1, 0)) { + nlua_error(lstate, _("Error executing vim.on_key() callbacks: %.*s")); } else { if (lua_isboolean(lstate, -1)) { discard = lua_toboolean(lstate, -1); -- cgit From de48fbbd5f8800bd7f1909a6fb41e53e871cf74c Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Thu, 20 Jun 2024 14:48:06 +0200 Subject: fix(messages)!: vim.ui_attach message callbacks are unsafe Problem: Lua callbacks for "msg_show" events with vim.ui_attach() are executed when it is not safe. Solution: Disallow non-fast API calls for "msg_show" event callbacks. Automatically detach callback after excessive errors. Make sure fast APIs do not modify Nvim state. --- src/nvim/lua/executor.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index e4da274204..15f70fb725 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -187,7 +187,7 @@ static void nlua_luv_error_event(void **argv) msg_ext_set_kind("lua_error"); switch (type) { case kCallback: - semsg_multiline("Error executing luv callback:\n%s", error); + semsg_multiline("Error executing callback:\n%s", error); break; case kThread: semsg_multiline("Error in luv thread:\n%s", error); @@ -201,13 +201,13 @@ static void nlua_luv_error_event(void **argv) xfree(error); } -static int nlua_luv_cfpcall(lua_State *lstate, int nargs, int nresult, int flags) +/// Execute callback in "fast" context. Used for luv and some vim.ui_event +/// callbacks where using the API directly is not safe. +static int nlua_fast_cfpcall(lua_State *lstate, int nargs, int nresult, int flags) FUNC_ATTR_NONNULL_ALL { int retval; - // luv callbacks might be executed at any os_breakcheck/line_breakcheck - // call, so using the API directly here is not safe. in_fast_callback++; int top = lua_gettop(lstate); @@ -366,11 +366,13 @@ static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg static void nlua_schedule_event(void **argv) { LuaRef cb = (LuaRef)(ptrdiff_t)argv[0]; + uint32_t ns_id = (uint32_t)(ptrdiff_t)argv[1]; lua_State *const lstate = global_lstate; nlua_pushref(lstate, cb); nlua_unref_global(lstate, cb); if (nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("Error executing vim.schedule lua callback: %.*s")); + ui_remove_cb(ns_id, true); } } @@ -392,8 +394,9 @@ static int nlua_schedule(lua_State *const lstate) } LuaRef cb = nlua_ref_global(lstate, 1); - - multiqueue_put(main_loop.events, nlua_schedule_event, (void *)(ptrdiff_t)cb); + // Pass along UI event handler to disable on error. + multiqueue_put(main_loop.events, nlua_schedule_event, (void *)(ptrdiff_t)cb, + (void *)(ptrdiff_t)ui_event_ns_id); return 0; } @@ -425,7 +428,7 @@ static int nlua_wait(lua_State *lstate) FUNC_ATTR_NONNULL_ALL { if (in_fast_callback) { - return luaL_error(lstate, e_luv_api_disabled, "vim.wait"); + return luaL_error(lstate, e_fast_api_disabled, "vim.wait"); } intptr_t timeout = luaL_checkinteger(lstate, 1); @@ -598,7 +601,7 @@ static void nlua_common_vim_init(lua_State *lstate, bool is_thread, bool is_stan luv_set_cthread(lstate, nlua_luv_thread_cfcpcall); } else { luv_set_loop(lstate, &main_loop.uv); - luv_set_callback(lstate, nlua_luv_cfpcall); + luv_set_callback(lstate, nlua_fast_cfpcall); } luaopen_luv(lstate); lua_pushvalue(lstate, -1); @@ -724,7 +727,7 @@ static int nlua_ui_detach(lua_State *lstate) return luaL_error(lstate, "invalid ns_id"); } - ui_remove_cb(ns_id); + ui_remove_cb(ns_id, false); return 0; } @@ -1174,7 +1177,7 @@ int nlua_call(lua_State *lstate) size_t name_len; const char *name = luaL_checklstring(lstate, 1, &name_len); if (!nlua_is_deferred_safe() && !viml_func_is_fast(name)) { - return luaL_error(lstate, e_luv_api_disabled, "Vimscript function"); + return luaL_error(lstate, e_fast_api_disabled, "Vimscript function"); } int nargs = lua_gettop(lstate) - 1; @@ -1231,7 +1234,7 @@ free_vim_args: static int nlua_rpcrequest(lua_State *lstate) { if (!nlua_is_deferred_safe()) { - return luaL_error(lstate, e_luv_api_disabled, "rpcrequest"); + return luaL_error(lstate, e_fast_api_disabled, "rpcrequest"); } return nlua_rpc(lstate, true); } @@ -1593,6 +1596,12 @@ bool nlua_ref_is_function(LuaRef ref) /// @return Return value of function, as per mode Object nlua_call_ref(LuaRef ref, const char *name, Array args, LuaRetMode mode, Arena *arena, Error *err) +{ + return nlua_call_ref_ctx(false, ref, name, args, mode, arena, err); +} + +Object nlua_call_ref_ctx(bool fast, LuaRef ref, const char *name, Array args, LuaRetMode mode, + Arena *arena, Error *err) { lua_State *const lstate = global_lstate; nlua_pushref(lstate, ref); @@ -1605,7 +1614,13 @@ Object nlua_call_ref(LuaRef ref, const char *name, Array args, LuaRetMode mode, nlua_push_Object(lstate, &args.items[i], 0); } - if (nlua_pcall(lstate, nargs, 1)) { + if (fast) { + if (nlua_fast_cfpcall(lstate, nargs, 1, -1) < 0) { + // error is already scheduled, set anyways to convey failure. + api_set_error(err, kErrorTypeException, "fast context failure"); + } + return NIL; + } else if (nlua_pcall(lstate, nargs, 1)) { // if err is passed, the caller will deal with the error. if (err) { size_t len; -- cgit From e025f5a5b30a1ef92e88fed0f0c548d2240d30c0 Mon Sep 17 00:00:00 2001 From: luukvbaal Date: Sun, 17 Nov 2024 19:21:50 +0100 Subject: fix(messages): proper multiline Lua print() messages #31205 Problem: Separate message emitted for each newline present in Lua print() arguments. Solution: Make msg_multiline() handle NUL bytes. Refactor print() to use msg_multiline(). Refactor vim.print() to use print(). --- src/nvim/lua/executor.c | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 15f70fb725..c4fa8b0fff 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -954,41 +954,10 @@ static void nlua_common_free_all_mem(lua_State *lstate) static void nlua_print_event(void **argv) { - char *str = argv[0]; - const size_t len = (size_t)(intptr_t)argv[1] - 1; // exclude final NUL - - for (size_t i = 0; i < len;) { - if (got_int) { - break; - } - const size_t start = i; - while (i < len) { - switch (str[i]) { - case NUL: - str[i] = NL; - i++; - continue; - case NL: - // TODO(bfredl): use proper multiline msg? Probably should implement - // print() in lua in terms of nvim_message(), when it is available. - str[i] = NUL; - i++; - break; - default: - i++; - continue; - } - break; - } - msg(str + start, 0); - if (msg_silent == 0) { - msg_didout = true; // Make blank lines work properly - } - } - if (len && str[len - 1] == NUL) { // Last was newline - msg("", 0); - } - xfree(str); + HlMessage msg = KV_INITIAL_VALUE; + HlMessageChunk chunk = { { .data = argv[0], .size = (size_t)(intptr_t)argv[1] - 1 }, 0 }; + kv_push(msg, chunk); + msg_multihl(msg, "lua_print", true); } /// Print as a Vim message -- cgit