From 65355c388b45308154ead6d59de030150d43fff5 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sun, 11 Jul 2021 13:02:42 +0430 Subject: [PATCH] LibWasm: Use AK::StackInfo to track stack size This way, we can make sure that it doesn't overflow when ASAN is enabled. --- .../Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp | 3 ++- .../Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h | 2 ++ Userland/Libraries/LibWasm/Constants.h | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp index 0c7ce0c6a2..695f4fa227 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.cpp @@ -32,6 +32,7 @@ namespace Wasm { void BytecodeInterpreter::interpret(Configuration& configuration) { + m_stack_info = {}; m_trap.clear(); auto& instructions = configuration.frame().expression().instructions(); auto max_ip_value = InstructionPointer { instructions.size() }; @@ -129,7 +130,7 @@ 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); + TRAP_IF_NOT(m_stack_info.size_free() >= Constants::minimum_stack_space_to_keep_free); auto instance = configuration.store().get(address); TRAP_IF_NOT(instance); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h index f853f1f08d..41d2caacdb 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/BytecodeInterpreter.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include @@ -57,6 +58,7 @@ protected: } Optional m_trap; + StackInfo m_stack_info; }; struct DebuggerBytecodeInterpreter : public BytecodeInterpreter { diff --git a/Userland/Libraries/LibWasm/Constants.h b/Userland/Libraries/LibWasm/Constants.h index d35a27adbc..459a4a95a9 100644 --- a/Userland/Libraries/LibWasm/Constants.h +++ b/Userland/Libraries/LibWasm/Constants.h @@ -38,7 +38,7 @@ static constexpr auto page_size = 64 * KiB; // Implementation-defined limits // These are not concretely defined by the spec, so the values are only defined by us. -static constexpr auto max_allowed_call_stack_depth = 512; +static constexpr auto minimum_stack_space_to_keep_free = 256 * KiB; // Note: Value is arbitrary and chosen by testing with ASAN static constexpr auto max_allowed_executed_instructions_per_call = 256 * 1024 * 1024; }