From 08d89227bfe2a81fccaa8273e4d5402eba0cca6c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 May 2024 20:41:01 +1000 Subject: [PATCH 1/2] AP_Scripting: added example script that causes a hard fault this exercises rapid fault handling --- .../AP_Scripting/examples/fault_handling.lua | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 libraries/AP_Scripting/examples/fault_handling.lua diff --git a/libraries/AP_Scripting/examples/fault_handling.lua b/libraries/AP_Scripting/examples/fault_handling.lua new file mode 100644 index 0000000000000..0a95e84f7b7ac --- /dev/null +++ b/libraries/AP_Scripting/examples/fault_handling.lua @@ -0,0 +1,56 @@ +--[[ + example script to test fault handling with pcall +--]] + +local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} + +gcs:send_text(MAV_SEVERITY.INFO, "Loading fault test") + +local test_count = 0 +local fault_count = 0 + +--[[ + evaluate a lua function and return nil on fault or the functions return value +--]] +local function evaluate(f) + local ok, s = pcall(f) + eval_func = nil + if ok then + return s + end + fault_count = fault_count + 1 + return nil +end + +local function nil_deref() + local loc = nil + return loc:lat() +end + +local function bad_random() + return math.random(1,0) +end + +local function run_test() + local script_variant = test_count % 2 + if script_variant == 0 then + evaluate(nil_deref) + elseif script_variant == 1 then + evaluate(bad_random) + end +end + +local function update() + if test_count % 100 == 0 then + gcs:send_text(MAV_SEVERITY.INFO,string.format("Test %u fault_count %u", test_count, fault_count)) + end + test_count = test_count + 1 + run_test() + assert(fault_count == test_count, "fault and test counts should match") + return update,1 +end + +gcs:send_text(MAV_SEVERITY.INFO, "Starting fault test in 2 seconds") + +-- wait a while for GCS to connect +return update,2000 From ea5418ecca5d87d78d70f1112b71e32025fff447 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 May 2024 20:55:55 +1000 Subject: [PATCH 2/2] AP_Scripting: fixed float register save/restore in setjmp/longjmp the register save must happen before the setjmp() call, which means outside of the LUAI_TRY() macro. We also should be saving all 32 floating point registers --- libraries/AP_Scripting/lua/src/ldo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/AP_Scripting/lua/src/ldo.c b/libraries/AP_Scripting/lua/src/ldo.c index c07f4fdb32854..6c627dc3a9fe1 100644 --- a/libraries/AP_Scripting/lua/src/ldo.c +++ b/libraries/AP_Scripting/lua/src/ldo.c @@ -142,10 +142,10 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; - LUAI_TRY(L, &lj, #ifdef ARM_MATH_CM7 __asm__("vpush {s16-s31}"); #endif + LUAI_TRY(L, &lj, (*f)(L, ud); ); #ifdef ARM_MATH_CM7