From 2c3fe0cbcf89896ad1edcf101def27b652647f10 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Mon, 29 Oct 2018 18:05:36 -0700 Subject: [PATCH] AP_Scripting: Load a sandbox enviorment --- libraries/AP_Scripting/AP_Scripting.cpp | 23 ++++++++++------ libraries/AP_Scripting/scripts/sandbox.lua | 32 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 libraries/AP_Scripting/scripts/sandbox.lua diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index 92ff67ca05..f330e006e8 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "lua_bindings.h" @@ -80,18 +81,24 @@ void AP_Scripting::thread(void) { luaL_openlibs(state); load_lua_bindings(state); -// luaL_loadstring(state, "gcs.send_text(string.format(\"1 + 2 = %d\", 1+2))"); - // load the sandbox creation function - if (luaL_dofile(state, "sandbox.lua")) { - gcs().send_text(MAV_SEVERITY_CRITICAL, "SFATAL: %s", lua_tostring(state, -1)); + uint32_t sandbox_size; + char *sandbox_data = (char *)AP_ROMFS::find_decompress("sandbox.lua", sandbox_size); + if (sandbox_data == nullptr) { + gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Could not find sandbox"); return; } - luaL_loadfile(state, "test.lua"); - lua_getglobal(state, "get_sandbox_env"); //find the sandbox creation function + if (luaL_dostring(state, sandbox_data)) { + gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Loading sandbox: %s", lua_tostring(state, -1)); + return; + } + free(sandbox_data); + + luaL_loadstring(state, "gcs.send_text(string.format(\"math.cos(1 + 2) = %f\", math.cos(1+2)))"); + lua_getglobal(state, "get_sandbox_env"); // find the sandbox creation function if (lua_pcall(state, 0, LUA_MULTRET, 0)) { - gcs().send_text(MAV_SEVERITY_CRITICAL, "SFATAL: %s", lua_tostring(state, -1)); + gcs().send_text(MAV_SEVERITY_CRITICAL, "Scripting: Could not create sandbox: %s", lua_tostring(state, -1)); return; } lua_setupvalue(state, -2, 1); @@ -111,7 +118,7 @@ void AP_Scripting::thread(void) { } const uint32_t runEnd = AP_HAL::micros(); const uint32_t endMem = hal.util->available_memory(); - gcs().send_text(MAV_SEVERITY_INFO, "Execution: %d Memory: %d", runEnd - loadEnd, startMem - endMem); + gcs().send_text(MAV_SEVERITY_INFO, "Time: %d Memory: %d", runEnd - loadEnd, startMem - endMem); } } diff --git a/libraries/AP_Scripting/scripts/sandbox.lua b/libraries/AP_Scripting/scripts/sandbox.lua new file mode 100644 index 0000000000..22a777cbb2 --- /dev/null +++ b/libraries/AP_Scripting/scripts/sandbox.lua @@ -0,0 +1,32 @@ +-- sample sandbox environment +function get_sandbox_env () + return { + ipairs = ipairs, + next = next, + pairs = pairs, + pcall = pcall, + tonumber = tonumber, + tostring = tostring, + type = type, + unpack = unpack, + string = { byte = string.byte, char = string.char, find = string.find, + format = string.format, gmatch = string.gmatch, gsub = string.gsub, + len = string.len, lower = string.lower, match = string.match, + rep = string.rep, reverse = string.reverse, sub = string.sub, + upper = string.upper }, + math = { abs = math.abs, acos = math.acos, asin = math.asin, + atan = math.atan, atan2 = math.atan2, ceil = math.ceil, cos = math.cos, + cosh = math.cosh, deg = math.deg, exp = math.exp, floor = math.floor, + fmod = math.fmod, frexp = math.frexp, huge = math.huge, + ldexp = math.ldexp, log = math.log, log10 = math.log10, max = math.max, + min = math.min, modf = math.modf, pi = math.pi, pow = math.pow, + rad = math.rad, random = math.random, sin = math.sin, sinh = math.sinh, + sqrt = math.sqrt, tan = math.tan, tanh = math.tanh }, + table = { insert = table.insert, maxn = table.maxn, remove = table.remove, + sort = table.sort }, + utf8 = { char = utf8.char, charpattern = utf8.charpattern, codes = utf8.codes, + codepoint = utf8.codepoint, len = utf8.len, offsets = utf8.offsets}, + os = { clock = os.clock, difftime = os.difftime, time = os.time }, + gcs = { send_text = gcs.send_text}, + } +end