Script: add optional bounded Lua runtime adapter (#39)
This commit is contained in:
1 parent
b3c566ed4d
commit
ae7bedb755
7 files changed
+418
-58
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -12,6 +13,8 @@
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::Script::Bindings;
|
||||
using Void::Script::Configuration;
|
||||
using Void::Script::ErrorKind;
|
||||
using Void::Script::Host;
|
||||
using Void::Script::HostInvocation;
|
||||
@@ -20,6 +23,12 @@ using Void::Script::Integer;
|
||||
using Void::Script::Limits;
|
||||
using Void::Script::LuaEvaluator;
|
||||
|
||||
#if defined(VOID_SCRIPT_HAS_LUA)
|
||||
constexpr bool has_lua = true;
|
||||
#else
|
||||
constexpr bool has_lua = false;
|
||||
#endif
|
||||
|
||||
[[nodiscard]] Host add_host()
|
||||
{
|
||||
return Host{
|
||||
@@ -46,10 +55,23 @@ using Void::Script::LuaEvaluator;
|
||||
}};
|
||||
}
|
||||
|
||||
[[nodiscard]] Host throwing_host()
|
||||
{
|
||||
return Host{
|
||||
.name = "boom",
|
||||
.function = [](std::span<const Integer>)
|
||||
-> std::expected<Integer, std::string> {
|
||||
throw std::runtime_error{"boom from binding"};
|
||||
}};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test lua_evaluates_integer_results_and_inputs()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
const Input inputs[] = {Input{.key = 1, .value = 2},
|
||||
Input{.key = 2, .value = 3}};
|
||||
@@ -60,6 +82,9 @@ Void::Testing::Test lua_evaluates_integer_results_and_inputs()
|
||||
|
||||
Void::Testing::Test lua_host_functions_are_recorded()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator{Limits{}, {add_host()}};
|
||||
auto result = evaluator.evaluate("return add(2, 3)");
|
||||
Void::Testing::require(result.has_value());
|
||||
@@ -73,6 +98,9 @@ Void::Testing::Test lua_host_functions_are_recorded()
|
||||
|
||||
Void::Testing::Test lua_host_error_is_structured_and_recovers()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator{Limits{}, {fail_host()}};
|
||||
auto failed = evaluator.evaluate("return fail(-1)");
|
||||
Void::Testing::require(!failed.has_value());
|
||||
@@ -84,8 +112,30 @@ Void::Testing::Test lua_host_error_is_structured_and_recovers()
|
||||
Void::Testing::expect(recovered->integers == std::vector<Integer>{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_host_exception_is_contained()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator{Limits{}, {throwing_host()}};
|
||||
auto failed = evaluator.evaluate("return boom(1)");
|
||||
Void::Testing::require(!failed.has_value());
|
||||
Void::Testing::expect(failed.error().kind == ErrorKind::host_error);
|
||||
Void::Testing::expect(failed.error().message.find("boom") !=
|
||||
std::string::npos);
|
||||
|
||||
// The C++ exception never crosses Lua's C boundary: the evaluator is
|
||||
// still usable afterwards.
|
||||
auto recovered = evaluator.evaluate("return 7");
|
||||
Void::Testing::require(recovered.has_value());
|
||||
Void::Testing::expect(recovered->integers == std::vector<Integer>{7});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_instruction_limit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_instructions = 1'000;
|
||||
LuaEvaluator evaluator{limits};
|
||||
@@ -96,6 +146,9 @@ Void::Testing::Test lua_instruction_limit()
|
||||
|
||||
Void::Testing::Test lua_allocator_limit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_allocator_bytes = 128 * 1024;
|
||||
LuaEvaluator evaluator{limits};
|
||||
@@ -107,6 +160,9 @@ Void::Testing::Test lua_allocator_limit()
|
||||
|
||||
Void::Testing::Test lua_host_call_limit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_host_calls = 2;
|
||||
LuaEvaluator evaluator{limits, {fail_host()}};
|
||||
@@ -117,6 +173,9 @@ Void::Testing::Test lua_host_call_limit()
|
||||
|
||||
Void::Testing::Test lua_compile_error()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate("return (");
|
||||
Void::Testing::require(!result.has_value());
|
||||
@@ -125,6 +184,9 @@ Void::Testing::Test lua_compile_error()
|
||||
|
||||
Void::Testing::Test lua_result_limit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_result_integers = 2;
|
||||
LuaEvaluator evaluator{limits};
|
||||
@@ -135,6 +197,9 @@ Void::Testing::Test lua_result_limit()
|
||||
|
||||
Void::Testing::Test lua_rejects_non_integer_results()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate("return 'text'");
|
||||
Void::Testing::require(!result.has_value());
|
||||
@@ -143,6 +208,9 @@ Void::Testing::Test lua_rejects_non_integer_results()
|
||||
|
||||
Void::Testing::Test lua_input_limit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_inputs = 1;
|
||||
LuaEvaluator evaluator{limits};
|
||||
@@ -152,3 +220,110 @@ Void::Testing::Test lua_input_limit()
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::invalid_input);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_forbidden_libraries_are_absent()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate(
|
||||
"local names = {'os', 'io', 'package', 'debug', 'require'}\n"
|
||||
"for _, name in ipairs(names) do\n"
|
||||
" if _G[name] ~= nil then return 1 end\n"
|
||||
"end\n"
|
||||
"return 0");
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect(result->integers == std::vector<Integer>{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_base_library_is_hardened()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
auto result = evaluator.evaluate(
|
||||
"local names = {'load', 'loadfile', 'dofile', 'print'}\n"
|
||||
"for _, name in ipairs(names) do\n"
|
||||
" if _G[name] ~= nil then return 1 end\n"
|
||||
"end\n"
|
||||
"return 0");
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect(result->integers == std::vector<Integer>{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_bindings_are_explicit()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
const Configuration configuration{
|
||||
.limits = Limits{},
|
||||
.bindings = Bindings{.math = false},
|
||||
.hosts = {},
|
||||
.cancelled = {},
|
||||
};
|
||||
LuaEvaluator evaluator{configuration};
|
||||
auto result = evaluator.evaluate("if math ~= nil then return 1 end return 0");
|
||||
Void::Testing::require(result.has_value());
|
||||
Void::Testing::expect(result->integers == std::vector<Integer>{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_cancellation_stops_runaway()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_instructions = 10'000'000;
|
||||
const Configuration configuration{
|
||||
.limits = limits,
|
||||
.bindings = Bindings{},
|
||||
.hosts = {},
|
||||
.cancelled = [checks = 0]() mutable { return ++checks > 4; },
|
||||
};
|
||||
LuaEvaluator evaluator{configuration};
|
||||
auto result = evaluator.evaluate("while true do end");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::cancelled);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_cancellation_callback_exception_is_cancellation()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
Limits limits;
|
||||
limits.max_instructions = 10'000'000;
|
||||
const Configuration configuration{
|
||||
.limits = limits,
|
||||
.bindings = Bindings{},
|
||||
.hosts = {},
|
||||
.cancelled = []() -> bool { throw std::runtime_error{"cancel"}; },
|
||||
};
|
||||
LuaEvaluator evaluator{configuration};
|
||||
auto result = evaluator.evaluate("while true do end");
|
||||
Void::Testing::require(!result.has_value());
|
||||
Void::Testing::expect(result.error().kind == ErrorKind::cancelled);
|
||||
}
|
||||
|
||||
Void::Testing::Test lua_equivalent_fixtures_agree()
|
||||
{
|
||||
if constexpr (!has_lua) {
|
||||
Void::Testing::skip("Lua adapter not compiled in");
|
||||
}
|
||||
LuaEvaluator evaluator;
|
||||
const Input inputs[] = {Input{.key = 1, .value = 3},
|
||||
Input{.key = 2, .value = 7}};
|
||||
auto iterative = evaluator.evaluate(
|
||||
"local total = 0\n"
|
||||
"for _ = 1, input[2] do total = total + input[1] end\n"
|
||||
"return total",
|
||||
inputs);
|
||||
auto closed_form = evaluator.evaluate("return input[1] * input[2]", inputs);
|
||||
Void::Testing::require(iterative.has_value());
|
||||
Void::Testing::require(closed_form.has_value());
|
||||
Void::Testing::expect(iterative->integers == closed_form->integers);
|
||||
Void::Testing::expect(iterative->integers == std::vector<Integer>{21});
|
||||
}
|
||||
Reference in new issue
Block a user