1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

LibWasm: Limit the call stack depth and the number of executed insts

These limits are described in the spec, and we're supposed to stop
execution at some point.
The limits are arbitrarily chosen.
This commit is contained in:
Ali Mohammad Pur 2021-06-21 21:03:08 +04:30 committed by Ali Mohammad Pur
parent 9971d13844
commit c4b82ace74
2 changed files with 11 additions and 0 deletions

View file

@ -36,8 +36,13 @@ void BytecodeInterpreter::interpret(Configuration& configuration)
auto& instructions = configuration.frame().expression().instructions(); auto& instructions = configuration.frame().expression().instructions();
auto max_ip_value = InstructionPointer { instructions.size() }; auto max_ip_value = InstructionPointer { instructions.size() };
auto& current_ip_value = configuration.ip(); auto& current_ip_value = configuration.ip();
u64 executed_instructions = 0;
while (current_ip_value < max_ip_value) { while (current_ip_value < max_ip_value) {
if (executed_instructions++ >= Constants::max_allowed_executed_instructions_per_call) [[unlikely]] {
m_do_trap = true;
return;
}
auto& instruction = instructions[current_ip_value.value()]; auto& instruction = instructions[current_ip_value.value()];
auto old_ip = current_ip_value; auto old_ip = current_ip_value;
interpret(configuration, current_ip_value, instruction); interpret(configuration, current_ip_value, instruction);
@ -122,6 +127,8 @@ void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruct
void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address) void BytecodeInterpreter::call_address(Configuration& configuration, FunctionAddress address)
{ {
TRAP_IF_NOT(configuration.depth() <= Constants::max_allowed_call_stack_depth);
auto instance = configuration.store().get(address); auto instance = configuration.store().get(address);
TRAP_IF_NOT(instance); TRAP_IF_NOT(instance);
FunctionType const* type { nullptr }; FunctionType const* type { nullptr };

View file

@ -36,4 +36,8 @@ static constexpr auto extern_global_tag = 0x03;
static constexpr auto page_size = 64 * KiB; static constexpr auto page_size = 64 * KiB;
// Limits
static constexpr auto max_allowed_call_stack_depth = 1000;
static constexpr auto max_allowed_executed_instructions_per_call = 64 * 1024 * 1024;
} }