1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +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 max_ip_value = InstructionPointer { instructions.size() };
auto& current_ip_value = configuration.ip();
u64 executed_instructions = 0;
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 old_ip = current_ip_value;
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)
{
TRAP_IF_NOT(configuration.depth() <= Constants::max_allowed_call_stack_depth);
auto instance = configuration.store().get(address);
TRAP_IF_NOT(instance);
FunctionType const* type { nullptr };