From f8097379cb70e68f2e2f1e544d7574448842793e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 7 Feb 2024 09:35:42 +1100 Subject: [PATCH] AP_Scripting: prevent a code path to abort() in scripting if scripting can't find an error handler it can call abort(). We don't ever want to do that in ArduPilot --- libraries/AP_Scripting/lua/src/ldo.c | 2 +- libraries/AP_Scripting/lua_bindings.cpp | 19 +++++++++++++++++++ libraries/AP_Scripting/lua_common_defs.h | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libraries/AP_Scripting/lua/src/ldo.c b/libraries/AP_Scripting/lua/src/ldo.c index 88b9d921ddc27..c07f4fdb32854 100644 --- a/libraries/AP_Scripting/lua/src/ldo.c +++ b/libraries/AP_Scripting/lua/src/ldo.c @@ -128,7 +128,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } - abort(); + lua_abort(); } } } diff --git a/libraries/AP_Scripting/lua_bindings.cpp b/libraries/AP_Scripting/lua_bindings.cpp index f4cf9bb30735b..6e234130eb0e1 100644 --- a/libraries/AP_Scripting/lua_bindings.cpp +++ b/libraries/AP_Scripting/lua_bindings.cpp @@ -956,4 +956,23 @@ int lua_range_finder_handle_script_msg(lua_State *L) { } #endif +/* + lua wants to abort, and doesn't have access to a panic function + */ +void lua_abort() +{ + INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control); +#if AP_SIM_ENABLED + AP_HAL::panic("lua_abort called"); +#else + if (!hal.util->get_soft_armed()) { + AP_HAL::panic("lua_abort called"); + } + // abort while flying, all we can do is loop + while (true) { + hal.scheduler->delay(1000); + } +#endif +} + #endif // AP_SCRIPTING_ENABLED diff --git a/libraries/AP_Scripting/lua_common_defs.h b/libraries/AP_Scripting/lua_common_defs.h index cfd047d06001c..664541ddfa003 100644 --- a/libraries/AP_Scripting/lua_common_defs.h +++ b/libraries/AP_Scripting/lua_common_defs.h @@ -27,3 +27,5 @@ #endif // REPL_OUT int lua_get_current_ref(); +void lua_abort(void) __attribute__((noreturn)); +