Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm memory limit #2009

Merged
merged 11 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions core/benchmark/block_execution_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,15 @@ namespace kagome::benchmark {
std::shared_ptr<runtime::Core> core_api,
std::shared_ptr<const blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::ModuleRepository> module_repo,
std::shared_ptr<const runtime::RuntimeCodeProvider> code_provider,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage)
: logger_{log::createLogger("BlockExecutionBenchmark", "benchmark")},
core_api_{core_api},
block_tree_{block_tree},
module_repo_{module_repo},
code_provider_{code_provider},
trie_storage_{trie_storage} {
BOOST_ASSERT(block_tree_ != nullptr);
BOOST_ASSERT(core_api_ != nullptr);
BOOST_ASSERT(module_repo_ != nullptr);
BOOST_ASSERT(code_provider_ != nullptr);
BOOST_ASSERT(trie_storage_ != nullptr);
}

Expand Down
3 changes: 0 additions & 3 deletions core/benchmark/block_execution_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace kagome::blockchain {
namespace kagome::runtime {
class Core;
class ModuleRepository;
class RuntimeCodeProvider;
} // namespace kagome::runtime

namespace kagome::storage::trie {
Expand All @@ -45,7 +44,6 @@ namespace kagome::benchmark {
std::shared_ptr<runtime::Core> core_api,
std::shared_ptr<const blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::ModuleRepository> module_repo,
std::shared_ptr<const runtime::RuntimeCodeProvider> code_provider,
std::shared_ptr<const storage::trie::TrieStorage> trie_storage);

outcome::result<void> run(Config config);
Expand All @@ -55,7 +53,6 @@ namespace kagome::benchmark {
std::shared_ptr<runtime::Core> core_api_;
std::shared_ptr<const blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::ModuleRepository> module_repo_;
std::shared_ptr<const runtime::RuntimeCodeProvider> code_provider_;
std::shared_ptr<const storage::trie::TrieStorage> trie_storage_;
};

Expand Down
1 change: 0 additions & 1 deletion core/host_api/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ add_library(misc_extension
target_link_libraries(misc_extension
scale::scale
logger
constant_code_provider
outcome
blob
)
Expand Down
3 changes: 1 addition & 2 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
#include "runtime/runtime_api/impl/session_keys_api.hpp"
#include "runtime/runtime_api/impl/tagged_transaction_queue.hpp"
#include "runtime/runtime_api/impl/transaction_payment_api.hpp"
#include "runtime/wabt/instrument.hpp"

#if KAGOME_WASM_COMPILER_WASM_EDGE == 1

Expand Down Expand Up @@ -404,7 +405,6 @@ namespace {
injector.template create<sptr<storage::trie::TrieStorage>>(),
injector.template create<sptr<storage::trie::TrieSerializer>>(),
injector.template create<sptr<runtime::wavm::IntrinsicModule>>(),
injector.template create<sptr<runtime::SingleModuleCache>>(),
module_cache_opt,
injector.template create<sptr<crypto::Hasher>>());
}),
Expand Down Expand Up @@ -560,7 +560,6 @@ namespace {
di::bind<runtime::TransactionPaymentApi>.template to<runtime::TransactionPaymentApiImpl>(),
di::bind<runtime::AccountNonceApi>.template to<runtime::AccountNonceApiImpl>(),
di::bind<runtime::AuthorityDiscoveryApi>.template to<runtime::AuthorityDiscoveryApiImpl>(),
di::bind<runtime::SingleModuleCache>.template to<runtime::SingleModuleCache>(),
di::bind<runtime::RuntimePropertiesCache>.template to<runtime::RuntimePropertiesCacheImpl>(),
std::forward<Ts>(args)...);
}
Expand Down
1 change: 0 additions & 1 deletion core/parachain/pvf/kagome_pvf_worker_injector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ namespace kagome::parachain {
injector.template create<sptr<storage::trie::TrieStorage>>(),
injector.template create<sptr<storage::trie::TrieSerializer>>(),
injector.template create<sptr<runtime::wavm::IntrinsicModule>>(),
injector.template create<sptr<runtime::SingleModuleCache>>(),
module_cache_opt,
injector.template create<sptr<crypto::Hasher>>());
}),
Expand Down
7 changes: 6 additions & 1 deletion core/parachain/pvf/session_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
namespace kagome::parachain {
inline outcome::result<runtime::RuntimeContext::ContextParams> sessionParams(
runtime::ParachainHost &api, const primitives::BlockHash &relay_parent) {
constexpr uint32_t kDefaultHeapPagesEstimate = 32;
constexpr uint32_t kExtraHeapPages = 2048;
turuslan marked this conversation as resolved.
Show resolved Hide resolved
OUTCOME_TRY(session_index, api.session_index_for_child(relay_parent));
OUTCOME_TRY(session_params,
api.session_executor_params(relay_parent, session_index));
runtime::RuntimeContext::ContextParams config;
config.memory_limits.max_stack_values_num =
runtime::RuntimeContext::DEFAULT_STACK_MAX;
config.memory_limits.heap_alloc_strategy =
HeapAllocStrategyDynamic{kDefaultHeapPagesEstimate + kExtraHeapPages};
if (session_params) {
for (auto &param : *session_params) {
if (auto *stack_max = get_if<runtime::StackLogicalMax>(&param)) {
config.memory_limits.max_stack_values_num = stack_max->max_values_num;
} else if (auto *pages_max = get_if<runtime::MaxMemoryPages>(&param)) {
config.memory_limits.max_memory_pages_num = pages_max->limit;
config.memory_limits.heap_alloc_strategy = HeapAllocStrategyDynamic{
kDefaultHeapPagesEstimate + pages_max->limit};
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

add_subdirectory(common)
add_subdirectory(binaryen)
add_subdirectory(wabt)

add_library(wasm_compiler INTERFACE)

Expand Down
4 changes: 4 additions & 0 deletions core/runtime/binaryen/memory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ namespace kagome::runtime::binaryen {
memory_->resize(kInitialMemorySize);
}

std::optional<WasmSize> MemoryImpl::pagesMax() const {
return memory_->pagesMax();
}

WasmPointer MemoryImpl::allocate(WasmSize size) {
return allocator_->allocate(size);
}
Expand Down
2 changes: 2 additions & 0 deletions core/runtime/binaryen/memory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace kagome::runtime::binaryen {
return memory_->getSize();
}

std::optional<WasmSize> pagesMax() const override;

outcome::result<BytesOut> view(WasmPointer ptr,
WasmSize size) const override;

Expand Down
3 changes: 3 additions & 0 deletions core/runtime/binaryen/runtime_external_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ namespace kagome::runtime::binaryen {
void RuntimeExternalInterface::init(wasm::Module &wasm,
wasm::ModuleInstance &instance) {
memory.resize(wasm.memory.initial * wasm::Memory::kPageSize);
if (wasm.memory.hasMax()) {
memory.pages_max = wasm.memory.max;
}
// apply memory segments
for (auto &segment : wasm.memory.segments) {
wasm::Address offset =
Expand Down
9 changes: 7 additions & 2 deletions core/runtime/binaryen/runtime_external_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ namespace kagome::runtime::binaryen {
class RuntimeExternalInterface
: public wasm::ModuleInstance::ExternalInterface {
class Memory {
friend class RuntimeExternalInterface;

using Mem = std::vector<char>;
Mem memory;
std::optional<WasmSize> pages_max;
template <typename T>
static bool aligned(const char *address) {
static_assert(!(sizeof(T) & (sizeof(T) - 1)), "must be a power of 2");
Expand Down Expand Up @@ -66,6 +69,9 @@ namespace kagome::runtime::binaryen {
auto getSize() const {
return memory.size();
}
std::optional<WasmSize> pagesMax() const {
return pages_max;
}
template <typename T>
void set(size_t address, T value) {
check(address, sizeof(T));
Expand Down Expand Up @@ -169,8 +175,7 @@ namespace kagome::runtime::binaryen {
memory.resize(newSize);
}

[[noreturn]]
void trap(const char *why) override {
[[noreturn]] void trap(const char *why) override {
logger_->error("Trap: {}", why);
throw wasm::TrapException{};
}
Expand Down
15 changes: 2 additions & 13 deletions core/runtime/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
#

add_library(runtime_common
stack_limiter.cpp
memory_error.cpp
)
target_link_libraries(runtime_common
wabt::wabt
blob
logger
uncompress_if_needed
outcome
)
kagome_install(runtime_common)
Expand All @@ -27,14 +22,6 @@ target_link_libraries(storage_code_provider
)
kagome_install(storage_code_provider)

add_library(constant_code_provider
constant_code_provider.cpp
)
target_link_libraries(constant_code_provider
uncompress_if_needed
)
kagome_install(constant_code_provider)

add_library(uncompress_if_needed
uncompress_code_if_needed.cpp
)
Expand Down Expand Up @@ -76,6 +63,7 @@ add_library(module_repository
target_link_libraries(module_repository
outcome
uncompress_if_needed
wasm_instrument
blob
executor
runtime_common
Expand All @@ -90,6 +78,7 @@ add_library(executor
target_link_libraries(executor
logger
uncompress_if_needed
wasm_instrument
storage
mp_utils
runtime_common
Expand Down
19 changes: 0 additions & 19 deletions core/runtime/common/constant_code_provider.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions core/runtime/common/constant_code_provider.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions core/runtime/common/core_api_factory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace kagome::blockchain {

namespace kagome::runtime {
class ModuleFactory;
class SingleModuleCache;
} // namespace kagome::runtime

namespace kagome::runtime {
Expand All @@ -29,7 +28,8 @@ namespace kagome::runtime {
: public runtime::CoreApiFactory,
public std::enable_shared_from_this<CoreApiFactoryImpl> {
public:
explicit CoreApiFactoryImpl(std::shared_ptr<const ModuleFactory> module_factory);
explicit CoreApiFactoryImpl(
std::shared_ptr<const ModuleFactory> module_factory);
~CoreApiFactoryImpl() = default;

outcome::result<std::unique_ptr<RestrictedCore>> make(
Expand Down
3 changes: 1 addition & 2 deletions core/runtime/common/memory_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ namespace kagome::runtime {
MemoryAllocator::MemoryAllocator(Memory &memory, const MemoryConfig &config)
: memory_{memory},
offset_{roundUpAlign(config.heap_base)},
max_memory_pages_num_{
config.limits.max_memory_pages_num.value_or(kMaxPages)} {
max_memory_pages_num_{memory_.pagesMax().value_or(kMaxPages)} {
BOOST_ASSERT(max_memory_pages_num_ > 0);
}

Expand Down
23 changes: 0 additions & 23 deletions core/runtime/common/module_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,6 @@ namespace kagome::runtime {
.resetMemory(MemoryConfig{heap_base, limits}));
auto &memory = memory_provider->getCurrentMemory()->get();

// TODO: https://github.com/qdrvm/kagome/issues/1962 limit max memory
if (auto &storage = getEnvironment().storage_provider) {
static const auto heappages_key = ":heappages"_buf;
auto batch = storage->getCurrentBatch();
OUTCOME_TRY(heappages, batch->tryGet(heappages_key));
if (heappages) {
if (sizeof(uint64_t) != heappages->size()) {
SL_ERROR(log,
"Unable to read :heappages value. Type size mismatch. "
"Required {} bytes, but {} available",
sizeof(uint64_t),
heappages->size());
} else {
uint64_t pages = common::le_bytes_to_uint64(heappages->view());
memory.resize(pages * kMemoryPageSize);
SL_TRACE(log,
"Creating wasm module with non-default :heappages value set "
"to {}",
pages);
}
}
}

size_t max_data_segment_end = 0;
size_t segments_num = 0;
forDataSegment([&](ModuleInstance::SegmentOffset offset,
Expand Down
Loading
Loading