Commit Graph

61 Commits

Author SHA1 Message Date
Peter Barker 777aab6e0c AP_Scripting: no warning if no ./scripts and no real filesystem
it is possible to build for boards without storage (so no Posix, no Fatafs), but still have scripts in ROMFS.

In this case we will use the backend AP_Filesystem_backend base class when doing file operations.  This will alway fail to open directories, so when we try to load scripts from SCRIPTS_DIRECTORY it will always fail.

This leads to a warning being emitted:

Lua: State memory usage: 2796 + 5227
AP: Lua: open directory (./scripts) failed
AP: hello, world
Time has wrapped

Which isn't great.

Detect we are working on this filesystem and don't warn.
2024-09-10 09:10:49 +10:00
Thomas Watson dc4d1bacdd AP_Scripting: adjust string metatable setup to fix sandbox integrity
In Lua, strings are the only type that come with a default metatable.
The metatable must be shared by all string objects, and it is set to be
the `string` library table each time that library is opened. In
Ardupilot's scripting engine, the last script to load then has access to
the string metatable as the library is opened fresh for each script, as
its `string` library will have been set to the metatable.

Therefore, if two scripts are loaded, A first and B second, and script B
executes e.g. `string.byte = "haha"`, then `string.byte()` and
`s:byte()` for script B are broken. Because the metatable is shared,
this also breaks `s:byte()` for script A, which violates the integrity
of the sandbox.

Fix the issue by disabling the metatable setup functionality when the
string libary is opened, then manually opening an additional copy of the
library (which won't be given to any script) and setting it as the
string metatable during intialization.

This will break any script that modifies the string metatable for
constructive purposes, but such a script could have been broken if it
weren't the only script running anyway.
2024-07-30 10:54:00 +10:00
Thomas Watson 3a834e83c7 AP_Scripting: reference script environment directly
Referencing the original function to run is of questionable value and
the only user uses it to grab the script environent from the upvalues.

Instead, use a reference to the script environment table directly.
2024-07-30 10:33:56 +10:00
Thomas Watson 15255a36e4 AP_Scripting: reference function to run separately
Some bits of the code in the require machinery use the `lua_ref` to
access the script environment. However, this can change after the script
is rescheduled and it returns an arbitrary function to run next.

Resolve this by introducing `run_ref` which is specifically a reference
to the function to run next. `lua_ref` is preserved for the script
lifetime.
2024-07-30 10:33:56 +10:00
Thomas Watson 717c5ff863 AP_Scripting: ignore hidden Lua files
On macOS, sometimes ._script.lua is created to store metadata when the
user copies script.lua over to their SD card. Previously, the scripting
engine would barf since the file is not Lua. Now, these files are
ignored.

Also avoids a case where a hidden and valid script might be loaded
without the user's knowledge.
2024-07-02 11:07:40 +10:00
Peter Barker 9bb343938f AP_Scripting: correct use-after-free in script statistics
run_next_script can free the script if the script runs over-time.

... so stop using data from that freed script structure!
2024-06-25 11:07:07 +10:00
Iampete1 f5e7bfcc48 AP_Scripting: remove support for REPL 2024-05-28 10:10:14 +10:00
Iampete1 c7d543f9af AP_Scripting: only try to load from ROMFS if `HAL_HAVE_AP_ROMFS_EMBEDDED_LUA` is defined 2024-02-27 11:09:08 +11:00
Andrew Tridgell 6fb99d6b1e AP_Scripting: fixed use after free bug
found with valgrind
2023-12-29 11:33:58 +11:00
Iampete1 2a3a5b2804 AP_Scripting: add checksum of running and loaded scripts with arming check 2023-12-05 11:03:58 +11:00
Tom Pittenger b5e2f9aa0a AP_Scripting: change some _INFO msgs to _CRITICAL/ERROR 2023-11-22 00:07:05 -08:00
Iampete1 596433a19a AP_Scripting: lua_scripts: add missing HAL_LOGGING_ENABLED define 2023-10-17 10:23:20 +11:00
Iampete1 7bbd4a7c7d AP_Scripting: add some missing dependencies 2023-09-26 11:29:42 +10:00
Peter Barker d519ab604e AP_Scripting: allow AP_SCRIPTING_ENABLED to come from hwdef files 2023-06-09 16:10:52 +10:00
bugobliterator 306b14d8dc AP_Scripting: add require with sandbox inheritence 2023-04-28 12:36:08 +10:00
Andrew Tridgell b45be75a98 AP_Scripting: use MultiHeap class 2023-01-16 09:19:16 +11:00
Iampete1 7e07859ec9 AP_Scripting: add debug print of state memory cost 2022-11-03 09:50:59 +11:00
Iampete1 815adbbc8e AP_Scripting: log memory cost of script load 2022-11-03 09:50:59 +11:00
Iampete1 da5ec973c5 AP_Scripting: add error message buffer semaphore 2022-08-31 17:04:24 +10:00
Iampete1 e2c29b09ab AP_Scripting: add arming check for failed scripts 2022-08-31 17:04:24 +10:00
murata c65425e3ac AP_Scripting: Changed NULL to nullptr 2022-06-07 11:59:23 +09:00
Iampete1 66cdfb015b AP_Scripting: always free the heap and remove scripts 2022-05-18 08:20:32 +10:00
murata 6e91b364e0 AP_Scripting: Console output can be disabled 2022-05-17 09:53:06 +10:00
Iampete1 9da80b45e9 AP_Scripting: load manual bindings via generator 2022-05-04 17:49:14 +10:00
Peter Barker ce312a3750 AP_Scripting: stop libraries including AP_Logger.h in .h files
AP_Logger.h is a nexus of includes; while this is being improved over
time, there's no reason for the library headers to include AP_Logger.h
as the logger itself is access by singleton and the structures are in
LogStructure.h

This necessitated moving The PID_Info structure out of AP_Logger's
namespace.  This cleans up a pretty nasty bit - that structure is
definitely not simply used for logging, but also used to pass pid
information around to controllers!

There are a lot of patches in here because AP_Logger.h, acting as a
nexus, was providing transitive header file inclusion in many (some
unlikely!) places.
2022-04-08 19:18:38 +10:00
Iampete1 427e8d1e24 AP_Scripting: add define option to disable interupts for the duration of script run 2022-02-02 11:33:46 +11:00
Peter Barker d72d0578a3 AP_Scripting: correct compilation when HAL_LOGGER_FILE_CONTENTS_ENABLED is 0 2021-12-07 09:36:48 +11:00
Iampete1 58ef0d0137 AP_Scripting: log files and runtime stats 2021-11-17 19:16:46 +11:00
Iampete1 817864ce74 AP_Scripting: convert DEBUG_LVL to DEBUG_OPTS bitmask 2021-11-17 19:16:46 +11:00
Iampete1 b047ea5c02 AP_Scripting: stop and restart scripting via command int 2021-11-02 17:33:34 +11:00
Iampete1 6aa8319b90 AP_Scripting: re-emmit error messages every 10 seconds if SCR_DEBUG_LVL > 0 2021-09-29 17:11:18 +10:00
murata 731fd7221f AP_Scripting: Only use messages that are useful for debugging 2021-09-10 20:46:33 +10:00
Michael du Breuil 53cbd86cbe AP_Scripting: Fix the lua scheduling rate to be referenced from the
start of the update

This allows specifying a return value like "return update, 10" to run
at a near perfect 100Hz, where as before it would be run 10 ms after the
script had completed it's loop, which can be highly variable as the
script experiences interupts from the system, as well as needing the
script author to take responsibility for calculating the desired update
rate at the end. This was always intended to be fixed, but I pushed it
back during the initial development, however people are begining to run
scripts that have enough processing, or are rate sensitive enough that
we are now needing to start correcting this, or scripts will have to do
their best to guess the time, which will be inferior to us providing it.

As a note if you exceeded the time expected we will be rescheduling the
script immediately, thus it will have a schedule time in the past and
will be slotted in. This can't indefinetly starve other scripts as they
will still be slotted in, but if you request an update in 1 ms, but took
100ms to run we will simply slide you back into the queue 1ms after when
you started running.
2020-11-17 10:19:13 +11:00
Iampete1 97b44dfe71 AP_Scripting: add DIR_DISABLE param 2020-10-28 19:22:45 +11:00
Rishabh bd7190767e AP_Scripting: Create scripts folder automatically 2020-06-20 21:39:06 +10:00
Andrew Tridgell 2c6fd13899 AP_Scripting: auto-build the bindings 2020-05-12 19:55:21 +10:00
Michael du Breuil 75b133926f AP_Scripting: Cleanups to remove false posive warnings 2020-03-28 21:16:09 +11:00
Andrew Tridgell 5047eb88e2 AP_Scripting: use @ROMFS 2020-03-28 15:28:03 +11:00
Andrew Tridgell ebec738b88 AP_Scripting: enable lua scripts in ROMFS 2020-03-28 15:28:03 +11:00
Andrew Tridgell a91c587ef3 AP_Scripting: use new DirHandle for directory listings 2020-03-28 15:28:03 +11:00
Peter Barker 19249a13fd AP_Scripting: tell the user where their scripts directory is on failure 2020-03-10 08:43:03 +09:00
Michael du Breuil d7a59bc449 AP_Scripting: Reduce the sleep time with no scripts
Lowering the sleep time when no scripts are pending, causes starting a
REPL session to respond much faster (this cuts the latency from up to 10
seconds to 1 second before the session is ready to start).

Also lowers the default scripting debug level to hide the statustext
from users, as in the general case this is just a spammy message if it
occurs, and we should be finding better reporting bits to get this to
the user.
2020-02-19 13:24:23 -07:00
Michael du Breuil cf06beb1e6 AP_Scripting: Add support for REPL over MAVLink 2020-02-18 17:23:22 -07:00
Michael du Breuil c1a599d0bd AP_Scripting: Reduce memory associated with the sandbox 2020-02-18 10:38:09 +11:00
Michael du Breuil 0e70eabdb9 AP_Scripting: Send lua errors to the console before status_text
This helps SITL where we frequently panic about to long a statustext,
which means a user never sees the intresting part unless they comment
out the sanity check, or run on a real board.
2020-01-16 17:54:30 -07:00
Michael du Breuil 9fe2c02079 AP_Scripting: Don't restart scripts if it panic'd during the initial VM creation 2019-12-05 15:08:43 -07:00
Andrew Tridgell 83eeca2586 AP_Scripting: fixes for modified AP_ROMFS API
need to use AP_ROMFS::free()
2019-10-24 22:03:43 +11:00
Michael du Breuil cf94a02207 AP_Scripting: Stop running scripts if the enable flag ever goes false 2019-10-15 15:35:12 -06:00
Andrew Tridgell b577787f68 AP_Scripting: use AP_Filesystem API 2019-08-27 15:40:43 +10:00
Michael du Breuil 7fdc9fef12 AP_Scripting: Garbage collect after each run of a script
For some reason this resolves a memory leak in a tight loop of file
open, close. This also extends the memory debug printing to be a bit
more useful.
2019-08-27 13:54:54 +10:00