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
@@ -14,6 +14,8 @@ option(VOID_ENABLE_ASAN "Enable AddressSanitizer (Clang/GCC)" OFF)
|
||||
option(VOID_ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (Clang/GCC)" OFF)
|
||||
option(VOID_ENABLE_TSAN "Enable ThreadSanitizer (Clang/GCC)" OFF)
|
||||
option(VOID_ENABLE_COVERAGE "Enable code coverage instrumentation" OFF)
|
||||
option(VOID_SCRIPT_ENABLE_LUA "Build the optional bounded Lua adapter" ON)
|
||||
option(VOID_FETCH_LUA "Fetch Lua 5.4 when no system package is found" ON)
|
||||
|
||||
# C++26 is the default. Cross/toolchain-limited builds (e.g. MinGW-w64
|
||||
# with an older GCC) may pin a lower standard explicitly.
|
||||
|
||||
@@ -280,6 +280,10 @@ function(void_fetch_dependency DEP_NAME)
|
||||
return()
|
||||
endif()
|
||||
|
||||
if(NOT VOID_FETCH_LUA)
|
||||
message(FATAL_ERROR
|
||||
"[Void] Lua 5.4 not found and VOID_FETCH_LUA=OFF")
|
||||
endif()
|
||||
message(STATUS "[Void] Lua 5.4 not found - falling back to FetchContent")
|
||||
FetchContent_Declare(Lua
|
||||
URL https://www.lua.org/ftp/lua-5.4.9.tar.gz
|
||||
|
||||
+66
-11
@@ -11,6 +11,11 @@
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
// The adapter's public interface is always declared so consumers can compile
|
||||
// against it regardless of the build configuration. When Void is built without
|
||||
// Lua (`VOID_SCRIPT_HAS_LUA` undefined), `LuaEvaluator::evaluate` returns a
|
||||
// structured `Error` instead of running the chunk.
|
||||
|
||||
/// Host function: receives integer arguments, returns an integer or an error
|
||||
/// message. It must not commit externally visible state itself; the evaluator
|
||||
/// records each call in `Result::host_calls` so the caller can commit after a
|
||||
@@ -23,27 +28,78 @@ struct Host {
|
||||
HostFunction function;
|
||||
};
|
||||
|
||||
/// Standard Lua libraries the evaluator may open. Every library is opt-in
|
||||
/// through this explicit allow-list; `os`, `io`, `package` and `debug` are
|
||||
/// never opened, and even the base library has `load`, `loadfile`, `dofile`,
|
||||
/// `collectgarbage` and `print` removed so a script cannot reach the ambient
|
||||
/// filesystem, process, dynamic loader or host output.
|
||||
struct Bindings {
|
||||
bool base{true};
|
||||
bool table{true};
|
||||
bool string{true};
|
||||
bool math{true};
|
||||
bool coroutine{true};
|
||||
|
||||
[[nodiscard]] bool operator==(const Bindings&) const = default;
|
||||
};
|
||||
|
||||
/// Returns true when evaluation must stop. It is polled from the instruction
|
||||
/// hook and before every host call, so a long-running or host-heavy script is
|
||||
/// interrupted cooperatively. An exception thrown by the callback is treated
|
||||
/// as a cancellation request.
|
||||
using CancelFunction = std::function<bool()>;
|
||||
|
||||
/// Complete evaluator policy. The defaults are a small, safe allow-list: no
|
||||
/// host functions, no cancellation source, and only the hardened standard
|
||||
/// libraries selected by `Bindings`.
|
||||
struct Configuration {
|
||||
Limits limits{};
|
||||
Bindings bindings{};
|
||||
std::vector<Host> hosts{};
|
||||
CancelFunction cancelled{};
|
||||
};
|
||||
|
||||
/// Bounded Lua text evaluator.
|
||||
///
|
||||
/// Each `evaluate` call creates a fresh, isolated Lua state, applies the
|
||||
/// allocator, instruction and host-call limits, exposes the integer-keyed
|
||||
/// inputs as `input[key]`, registers the host functions as globals, and runs
|
||||
/// the chunk with a protected call. Only integer return values are accepted
|
||||
/// and collected into an owned result. On failure the state is discarded, so
|
||||
/// inputs and the caller's host state are untouched; a later valid evaluation
|
||||
/// starts clean.
|
||||
/// allocator, instruction, host-call and cancellation limits, exposes the
|
||||
/// integer-keyed inputs as `input[key]`, registers the host functions as
|
||||
/// globals, and runs the chunk with a protected call. Only integer return
|
||||
/// values are accepted and collected into an owned result. On failure the
|
||||
/// state is discarded, so inputs and the caller's host state are untouched; a
|
||||
/// later valid evaluation starts clean.
|
||||
///
|
||||
/// The Lua state alone is not a security boundary: run untrusted scripts in a
|
||||
/// separately isolated process. This evaluator only removes the obvious
|
||||
/// ambient capabilities and enforces resource budgets.
|
||||
class LuaEvaluator
|
||||
{
|
||||
public:
|
||||
|
||||
explicit LuaEvaluator(Limits limits = Limits{},
|
||||
std::vector<Host> hosts = {})
|
||||
: limits_(limits)
|
||||
, hosts_(std::move(hosts))
|
||||
: configuration_{
|
||||
.limits = limits,
|
||||
.bindings = Bindings{},
|
||||
.hosts = std::move(hosts),
|
||||
.cancelled = CancelFunction{}}
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept { return limits_; }
|
||||
explicit LuaEvaluator(Configuration configuration)
|
||||
: configuration_(std::move(configuration))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept
|
||||
{
|
||||
return configuration_.limits;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Bindings& bindings() const noexcept
|
||||
{
|
||||
return configuration_.bindings;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Result, Error>
|
||||
evaluate(std::string_view source,
|
||||
@@ -51,8 +107,7 @@ class LuaEvaluator
|
||||
|
||||
private:
|
||||
|
||||
Limits limits_;
|
||||
std::vector<Host> hosts_;
|
||||
Configuration configuration_;
|
||||
};
|
||||
|
||||
} // namespace Void::Script
|
||||
@@ -20,6 +20,7 @@ enum class ErrorKind : std::uint8_t {
|
||||
result_limit,
|
||||
host_error,
|
||||
invalid_input,
|
||||
cancelled,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
|
||||
+153
-44
@@ -1,7 +1,10 @@
|
||||
#include <Void/Script/Lua.hpp>
|
||||
|
||||
#if defined(VOID_SCRIPT_HAS_LUA)
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -21,6 +24,7 @@ namespace
|
||||
struct Context {
|
||||
const Limits* limits{nullptr};
|
||||
const std::vector<Host>* hosts{nullptr};
|
||||
const CancelFunction* cancelled{nullptr};
|
||||
std::size_t used_bytes{0};
|
||||
std::size_t executed{0};
|
||||
std::size_t hook_step{1};
|
||||
@@ -28,7 +32,9 @@ struct Context {
|
||||
bool allocator_limit_hit{false};
|
||||
bool instruction_limit_hit{false};
|
||||
bool host_call_limit_hit{false};
|
||||
bool cancel_requested{false};
|
||||
std::string host_error;
|
||||
std::vector<Integer> scratch_arguments;
|
||||
std::vector<HostInvocation> invocations;
|
||||
};
|
||||
|
||||
@@ -37,27 +43,51 @@ struct Context {
|
||||
return *static_cast<Context**>(lua_getextraspace(state));
|
||||
}
|
||||
|
||||
/// Polls the cancellation callback. Exceptions thrown by the callback count as
|
||||
/// a cancellation request so a misbehaving policy can never escape into Lua's
|
||||
/// C error machinery.
|
||||
[[nodiscard]] bool cancellation_pending(Context& context)
|
||||
{
|
||||
if (context.cancel_requested) {
|
||||
return true;
|
||||
}
|
||||
if (context.cancelled == nullptr || !*context.cancelled) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if ((*context.cancelled)()) {
|
||||
context.cancel_requested = true;
|
||||
}
|
||||
} catch (...) {
|
||||
context.cancel_requested = true;
|
||||
}
|
||||
return context.cancel_requested;
|
||||
}
|
||||
|
||||
void* limited_alloc(void* user, void* pointer, std::size_t old_size,
|
||||
std::size_t new_size)
|
||||
{
|
||||
auto* context = static_cast<Context*>(user);
|
||||
if (new_size == 0) {
|
||||
if (pointer != nullptr) {
|
||||
context->used_bytes -= old_size;
|
||||
context->used_bytes = old_size >= context->used_bytes
|
||||
? 0
|
||||
: context->used_bytes - old_size;
|
||||
std::free(pointer);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
const std::size_t current = pointer != nullptr ? old_size : 0;
|
||||
const std::size_t limit = context->limits->max_allocator_bytes;
|
||||
const std::size_t used = context->used_bytes;
|
||||
if (new_size > current &&
|
||||
new_size - current >
|
||||
context->limits->max_allocator_bytes - context->used_bytes) {
|
||||
(used > limit || new_size - current > limit - used)) {
|
||||
context->allocator_limit_hit = true;
|
||||
return nullptr;
|
||||
}
|
||||
void* result = std::realloc(pointer, new_size);
|
||||
if (result != nullptr) {
|
||||
context->used_bytes = context->used_bytes - current + new_size;
|
||||
context->used_bytes = used - current + new_size;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -65,6 +95,9 @@ void* limited_alloc(void* user, void* pointer, std::size_t old_size,
|
||||
void instruction_hook(lua_State* state, lua_Debug*)
|
||||
{
|
||||
Context* context = context_of(state);
|
||||
if (cancellation_pending(*context)) {
|
||||
luaL_error(state, "evaluation cancelled");
|
||||
}
|
||||
context->executed += context->hook_step;
|
||||
if (context->executed >= context->limits->max_instructions) {
|
||||
context->instruction_limit_hit = true;
|
||||
@@ -72,18 +105,61 @@ void instruction_hook(lua_State* state, lua_Debug*)
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the base-library entry points that reach outside the sandbox.
|
||||
void harden_base(lua_State* state)
|
||||
{
|
||||
const char* const removed[] = {"load", "loadfile", "dofile",
|
||||
"print", "collectgarbage"};
|
||||
for (const char* name : removed) {
|
||||
lua_pushnil(state);
|
||||
lua_setglobal(state, name);
|
||||
}
|
||||
}
|
||||
|
||||
void open_bindings(lua_State* state, const Bindings& bindings)
|
||||
{
|
||||
struct Library {
|
||||
bool enabled;
|
||||
const char* name;
|
||||
lua_CFunction open;
|
||||
};
|
||||
const Library libraries[] = {
|
||||
{bindings.base, LUA_GNAME, luaopen_base},
|
||||
{bindings.table, LUA_TABLIBNAME, luaopen_table},
|
||||
{bindings.string, LUA_STRLIBNAME, luaopen_string},
|
||||
{bindings.math, LUA_MATHLIBNAME, luaopen_math},
|
||||
{bindings.coroutine, LUA_COLIBNAME, luaopen_coroutine},
|
||||
};
|
||||
for (const auto& library : libraries) {
|
||||
if (!library.enabled) {
|
||||
continue;
|
||||
}
|
||||
luaL_requiref(state, library.name, library.open, 1);
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
if (bindings.base) {
|
||||
harden_base(state);
|
||||
}
|
||||
}
|
||||
|
||||
int host_trampoline(lua_State* state)
|
||||
{
|
||||
Context* context = context_of(state);
|
||||
if (cancellation_pending(*context)) {
|
||||
lua_pushliteral(state, "evaluation cancelled");
|
||||
return lua_error(state);
|
||||
}
|
||||
|
||||
std::vector<Integer>& arguments = context->scratch_arguments;
|
||||
arguments.clear();
|
||||
const int argument_count = lua_gettop(state);
|
||||
std::vector<Integer> arguments;
|
||||
arguments.reserve(static_cast<std::size_t>(argument_count));
|
||||
for (int index = 1; index <= argument_count; ++index) {
|
||||
int is_integer = 0;
|
||||
const lua_Integer value =
|
||||
lua_tointegerx(state, index, &is_integer);
|
||||
const lua_Integer value = lua_tointegerx(state, index, &is_integer);
|
||||
if (is_integer == 0) {
|
||||
return luaL_error(state, "host arguments must be integers");
|
||||
lua_pushliteral(state, "host arguments must be integers");
|
||||
return lua_error(state);
|
||||
}
|
||||
arguments.push_back(static_cast<Integer>(value));
|
||||
}
|
||||
@@ -93,21 +169,43 @@ int host_trampoline(lua_State* state)
|
||||
const Host& host = (*context->hosts)[host_index];
|
||||
if (context->host_calls >= context->limits->max_host_calls) {
|
||||
context->host_call_limit_hit = true;
|
||||
return luaL_error(state, "host call limit exceeded");
|
||||
lua_pushliteral(state, "host call limit exceeded");
|
||||
return lua_error(state);
|
||||
}
|
||||
++context->host_calls;
|
||||
|
||||
auto result = host.function(arguments);
|
||||
if (!result.has_value()) {
|
||||
context->host_error = result.error();
|
||||
return luaL_error(state, "host '%s' failed: %s",
|
||||
host.name.c_str(), result.error().c_str());
|
||||
bool succeeded = false;
|
||||
Integer value = 0;
|
||||
try {
|
||||
auto result = host.function(arguments);
|
||||
if (result.has_value()) {
|
||||
succeeded = true;
|
||||
value = *result;
|
||||
} else {
|
||||
context->host_error = result.error();
|
||||
}
|
||||
} catch (const std::exception& exception) {
|
||||
context->host_error = exception.what();
|
||||
} catch (...) {
|
||||
context->host_error = "host threw an unknown exception";
|
||||
}
|
||||
context->invocations.push_back(
|
||||
HostInvocation{.host = host.name,
|
||||
.arguments = std::move(arguments),
|
||||
.result = *result});
|
||||
lua_pushinteger(state, static_cast<lua_Integer>(*result));
|
||||
|
||||
if (!succeeded) {
|
||||
lua_pushfstring(state, "host '%s' failed: %s", host.name.c_str(),
|
||||
context->host_error.c_str());
|
||||
return lua_error(state);
|
||||
}
|
||||
|
||||
try {
|
||||
context->invocations.push_back(
|
||||
HostInvocation{.host = host.name,
|
||||
.arguments = std::move(arguments),
|
||||
.result = value});
|
||||
} catch (...) {
|
||||
lua_pushliteral(state, "host result allocation failed");
|
||||
return lua_error(state);
|
||||
}
|
||||
lua_pushinteger(state, static_cast<lua_Integer>(value));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -124,14 +222,16 @@ std::expected<Result, Error>
|
||||
LuaEvaluator::evaluate(std::string_view source,
|
||||
std::span<const Input> inputs) const
|
||||
{
|
||||
if (inputs.size() > limits_.max_inputs) {
|
||||
const Limits& limits = configuration_.limits;
|
||||
if (inputs.size() > limits.max_inputs) {
|
||||
return std::unexpected{Error{.kind = ErrorKind::invalid_input,
|
||||
.message = "too many inputs"}};
|
||||
}
|
||||
|
||||
Context context;
|
||||
context.limits = &limits_;
|
||||
context.hosts = &hosts_;
|
||||
context.limits = &limits;
|
||||
context.hosts = &configuration_.hosts;
|
||||
context.cancelled = &configuration_.cancelled;
|
||||
|
||||
lua_State* state = lua_newstate(limited_alloc, &context);
|
||||
if (state == nullptr) {
|
||||
@@ -141,21 +241,7 @@ std::expected<Result, Error>
|
||||
}
|
||||
*static_cast<Context**>(lua_getextraspace(state)) = &context;
|
||||
|
||||
struct Library {
|
||||
const char* name;
|
||||
lua_CFunction open;
|
||||
};
|
||||
const Library libraries[] = {
|
||||
{LUA_GNAME, luaopen_base},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_COLIBNAME, luaopen_coroutine},
|
||||
};
|
||||
for (const auto& library : libraries) {
|
||||
luaL_requiref(state, library.name, library.open, 1);
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
open_bindings(state, configuration_.bindings);
|
||||
|
||||
lua_createtable(state, 0, static_cast<int>(inputs.size()));
|
||||
for (const auto& input : inputs) {
|
||||
@@ -164,14 +250,13 @@ std::expected<Result, Error>
|
||||
}
|
||||
lua_setglobal(state, "input");
|
||||
|
||||
for (std::size_t index = 0; index < hosts_.size(); ++index) {
|
||||
for (std::size_t index = 0; index < configuration_.hosts.size(); ++index) {
|
||||
lua_pushinteger(state, static_cast<lua_Integer>(index));
|
||||
lua_pushcclosure(state, host_trampoline, 1);
|
||||
lua_setglobal(state, hosts_[index].name.c_str());
|
||||
lua_setglobal(state, configuration_.hosts[index].name.c_str());
|
||||
}
|
||||
|
||||
context.hook_step =
|
||||
std::clamp<std::size_t>(limits_.max_instructions, 1, 1000);
|
||||
context.hook_step = std::clamp<std::size_t>(limits.max_instructions, 1, 1000);
|
||||
lua_sethook(state, instruction_hook, LUA_MASKCOUNT,
|
||||
static_cast<int>(context.hook_step));
|
||||
|
||||
@@ -183,11 +268,19 @@ std::expected<Result, Error>
|
||||
Error{.kind = ErrorKind::compile, .message = std::move(message)}};
|
||||
}
|
||||
|
||||
if (cancellation_pending(context)) {
|
||||
lua_close(state);
|
||||
return std::unexpected{
|
||||
Error{.kind = ErrorKind::cancelled, .message = "evaluation cancelled"}};
|
||||
}
|
||||
|
||||
if (lua_pcall(state, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||
std::string message = last_message(state, "runtime error");
|
||||
ErrorKind kind = ErrorKind::runtime;
|
||||
if (context.allocator_limit_hit) {
|
||||
kind = ErrorKind::allocator_limit;
|
||||
} else if (context.cancel_requested) {
|
||||
kind = ErrorKind::cancelled;
|
||||
} else if (context.instruction_limit_hit) {
|
||||
kind = ErrorKind::instruction_limit;
|
||||
} else if (context.host_call_limit_hit) {
|
||||
@@ -204,15 +297,14 @@ std::expected<Result, Error>
|
||||
const int result_count = lua_gettop(state);
|
||||
for (int index = 1; index <= result_count; ++index) {
|
||||
int is_integer = 0;
|
||||
const lua_Integer value =
|
||||
lua_tointegerx(state, index, &is_integer);
|
||||
const lua_Integer value = lua_tointegerx(state, index, &is_integer);
|
||||
if (is_integer == 0) {
|
||||
lua_close(state);
|
||||
return std::unexpected{
|
||||
Error{.kind = ErrorKind::runtime,
|
||||
.message = "script results must be integers"}};
|
||||
}
|
||||
if (result.integers.size() >= limits_.max_result_integers) {
|
||||
if (result.integers.size() >= limits.max_result_integers) {
|
||||
lua_close(state);
|
||||
return std::unexpected{
|
||||
Error{.kind = ErrorKind::result_limit,
|
||||
@@ -226,3 +318,20 @@ std::expected<Result, Error>
|
||||
}
|
||||
|
||||
} // namespace Void::Script
|
||||
|
||||
#else
|
||||
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
std::expected<Result, Error>
|
||||
LuaEvaluator::evaluate(std::string_view, std::span<const Input>) const
|
||||
{
|
||||
return std::unexpected{
|
||||
Error{.kind = ErrorKind::runtime,
|
||||
.message = "Lua adapter not compiled in"}};
|
||||
}
|
||||
|
||||
} // namespace Void::Script
|
||||
|
||||
#endif // VOID_SCRIPT_HAS_LUA
|
||||
+17
-3
@@ -1,3 +1,17 @@
|
||||
void_add_module(Script
|
||||
EXTERNAL_DEPS Lua::Lua
|
||||
)
|
||||
set(_void_script_lua OFF)
|
||||
if(VOID_SCRIPT_ENABLE_LUA)
|
||||
find_package(Lua 5.4 QUIET)
|
||||
if(LUA_FOUND OR VOID_FETCH_LUA)
|
||||
set(_void_script_lua ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(_void_script_lua)
|
||||
void_add_module(Script
|
||||
EXTERNAL_DEPS Lua::Lua
|
||||
)
|
||||
target_compile_definitions(Void_Script PUBLIC VOID_SCRIPT_HAS_LUA=1)
|
||||
else()
|
||||
message(STATUS "[Void] Lua 5.4 not available - building Script without the Lua adapter")
|
||||
void_add_module(Script)
|
||||
endif()
|
||||
@@ -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