Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Scripting: fixed hard fault on handling of lua exception #27056

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions libraries/AP_Scripting/examples/fault_handling.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion libraries/AP_Scripting/lua/src/ldo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading