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

memory check #1911

Merged
merged 8 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ hunter_config(
if (APPLE AND (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL "15.0.0")
hunter_config(
binaryen
URL https://github.com/qdrvm/binaryen/archive/0744f64a584cae5b9255b1c2f0a4e0b5e06d7038.zip
SHA1 f953c5f38a0417e494901e15ab6f5d8267388d18
URL https://github.com/qdrvm/binaryen/archive/e6a2fea157bde503f07f28444b350512374cf5bf.zip
SHA1 301f8b1775904179cb552c12be237b4aa076981e
)
endif ()
47 changes: 47 additions & 0 deletions core/common/span_adl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <algorithm>
#include <cstring>
#include <span>

#include "common/lexicographical_compare_three_way.hpp"

// `std::span` doesn't have comparison operator functions.
// Can't add function to neither `std::span` nor `namespace std`.
// `SpanAdl{span}` wraps span and allows writing functions for `SpanAdl`.
// `SpanAdl` overload will be selected by ADL.
template <typename T>
struct SpanAdl {
turuslan marked this conversation as resolved.
Show resolved Hide resolved
std::span<T> v;
};
template <typename T>
SpanAdl(T &&t) -> SpanAdl<typename decltype(std::span{t})::element_type>;

template <typename T>
auto operator<=>(const SpanAdl<T> &l_, const auto &r_)
requires(requires { std::span<const T>{r_}; })
{
auto &[l] = l_;
std::span r{r_};
if constexpr (std::is_same_v<std::remove_cvref_t<T>, uint8_t>) {
auto n = std::min(l.size(), r.size());
auto c = std::memcmp(l.data(), r.data(), n) <=> 0;
return c != 0 ? c : l.size() <=> r.size();
} else {
return cxx20::lexicographical_compare_three_way(
l.begin(), l.end(), r.begin(), r.end());
}
}

template <typename T>
bool operator==(const SpanAdl<T> &l_, const auto &r)
requires(requires { std::span<const T>{r}; })
{
return (l_ <=> r) == 0;
}
26 changes: 16 additions & 10 deletions core/host_api/impl/crypto_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ namespace kagome::host_api {
using ResultType = std::vector<crypto::Ed25519PublicKey>;
static const auto error_result(scale::encode(ResultType{}).value());

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = crypto_store_->getEd25519PublicKeys(key_type);
Expand All @@ -195,7 +195,7 @@ namespace kagome::host_api {

runtime::WasmPointer CryptoExtension::ext_crypto_ed25519_generate_version_1(
runtime::WasmPointer key_type_ptr, runtime::WasmSpan seed) {
crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto [seed_ptr, seed_len] = runtime::PtrSize(seed);
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace kagome::host_api {
runtime::WasmSpan msg) {
using ResultType = std::optional<crypto::Ed25519Signature>;

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_buffer =
Expand Down Expand Up @@ -305,7 +305,7 @@ namespace kagome::host_api {
using ResultType = std::vector<crypto::Sr25519PublicKey>;
static const auto error_result(scale::encode(ResultType{}).value());

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = crypto_store_->getSr25519PublicKeys(key_type);
Expand All @@ -322,7 +322,7 @@ namespace kagome::host_api {

runtime::WasmPointer CryptoExtension::ext_crypto_sr25519_generate_version_1(
runtime::WasmPointer key_type_ptr, runtime::WasmSpan seed) {
crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto [seed_ptr, seed_len] = runtime::PtrSize(seed);
Expand Down Expand Up @@ -362,7 +362,7 @@ namespace kagome::host_api {
static const auto error_result =
scale::encode(ResultType(std::nullopt)).value();

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_buffer =
Expand Down Expand Up @@ -575,7 +575,7 @@ namespace kagome::host_api {
using ResultType = std::vector<crypto::EcdsaPublicKey>;
static const auto error_result(scale::encode(ResultType{}).value());

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = crypto_store_->getEcdsaPublicKeys(key_type);
Expand All @@ -596,7 +596,7 @@ namespace kagome::host_api {
runtime::WasmSpan msg) {
using ResultType = std::optional<crypto::EcdsaSignature>;

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_buffer = getMemory().loadN(key, sizeof(crypto::EcdsaPublicKey));
Expand Down Expand Up @@ -629,7 +629,7 @@ namespace kagome::host_api {
runtime::WasmSpan msg) {
using ResultType = std::optional<crypto::EcdsaSignature>;

crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_buffer = getMemory().loadN(key, sizeof(crypto::EcdsaPublicKey));
Expand Down Expand Up @@ -661,7 +661,7 @@ namespace kagome::host_api {

runtime::WasmPointer CryptoExtension::ext_crypto_ecdsa_generate_version_1(
runtime::WasmPointer key_type_ptr, runtime::WasmSpan seed) const {
crypto::KeyType key_type = getMemory().load32u(key_type_ptr);
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto [seed_ptr, seed_len] = runtime::PtrSize(seed);
Expand Down Expand Up @@ -775,4 +775,10 @@ namespace kagome::host_api {
}
return ok;
}

crypto::KeyType CryptoExtension::loadKeyType(runtime::WasmPointer ptr) const {
return scale::decode<crypto::KeyType>(
getMemory().loadN(ptr, sizeof(crypto::KeyType)))
.value();
}
} // namespace kagome::host_api
2 changes: 2 additions & 0 deletions core/host_api/impl/crypto_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace kagome::crypto {
class Secp256k1Provider;
class Hasher;
class CryptoStore;
struct KeyType;
} // namespace kagome::crypto

namespace kagome::host_api {
Expand Down Expand Up @@ -261,6 +262,7 @@ namespace kagome::host_api {
runtime::WasmPointer sig,
runtime::WasmPointer msg);
runtime::WasmSize batchVerify(runtime::WasmSize ok);
crypto::KeyType loadKeyType(runtime::WasmPointer ptr) const;

std::shared_ptr<const runtime::MemoryProvider> memory_provider_;
std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider_;
Expand Down
9 changes: 5 additions & 4 deletions core/host_api/impl/host_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "host_api/impl/host_api_impl.hpp"

#include "common/bytestr.hpp"
#include "crypto/ecdsa/ecdsa_provider_impl.hpp"
#include "crypto/ed25519/ed25519_provider_impl.hpp"
#include "crypto/hasher/hasher_impl.hpp"
Expand Down Expand Up @@ -152,7 +153,7 @@ namespace kagome::host_api {
return storage_ext_.ext_trie_blake2_256_ordered_root_version_2(
values_data, state_version);
}

runtime::WasmPointer HostApiImpl::ext_trie_keccak_256_ordered_root_version_2(
runtime::WasmSpan values_data, runtime::WasmI32 state_version) {
return storage_ext_.ext_trie_keccak_256_ordered_root_version_2(
Expand Down Expand Up @@ -586,9 +587,9 @@ namespace kagome::host_api {

void HostApiImpl::ext_panic_handler_abort_on_panic_version_1(
runtime::WasmSpan message) {
auto [ptr, addr] = runtime::PtrSize{message};
auto msg = memory_provider_->getCurrentMemory()->get().loadStr(ptr, addr);
throw std::runtime_error{msg};
auto msg = byte2str(
memory_provider_->getCurrentMemory()->get().view(message).value());
throw std::runtime_error{std::string{msg}};
}

} // namespace kagome::host_api
5 changes: 3 additions & 2 deletions core/host_api/impl/io_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <boost/assert.hpp>

#include "common/bytestr.hpp"
#include "runtime/memory.hpp"
#include "runtime/memory_provider.hpp"
#include "runtime/ptr_size.hpp"
Expand Down Expand Up @@ -44,8 +45,8 @@ namespace kagome::host_api {
using runtime::WasmLogLevel;

auto read_str_from_position = [&](runtime::PtrSize location) {
return memory_provider_->getCurrentMemory()->get().loadStr(location.ptr,
location.size);
return byte2str(
memory_provider_->getCurrentMemory()->get().view(location).value());
};

const auto target_str = read_str_from_position(runtime::PtrSize(target));
Expand Down
4 changes: 2 additions & 2 deletions core/host_api/impl/offchain_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ namespace kagome::host_api {
runtime::WasmPointer OffchainExtension::ext_offchain_random_seed_version_1() {
auto worker = getWorker();
auto &memory = memory_provider_->getCurrentMemory()->get();
auto result = worker->timestamp();
return memory.storeBuffer(scale::encode(result).value());
auto result = worker->randomSeed();
return memory.storeBuffer(result);
}

void OffchainExtension::ext_offchain_local_storage_set_version_1(
Expand Down
13 changes: 11 additions & 2 deletions core/offchain/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,24 @@ namespace kagome::offchain {
constexpr HttpStatus DeadlineHasReached(10);
constexpr HttpStatus ErrorHasOccurred(20);

struct NoPayload {};
struct NoPayload {
bool operator==(const NoPayload &) const {
return true;
}
};
SCALE_EMPTY_CODER(NoPayload);

struct Success : public NoPayload {};
struct Failure : public NoPayload {};

template <typename S, typename F>
struct Result final : boost::variant<S, F> {
using boost::variant<S, F>::variant;
using Base = boost::variant<S, F>;
using Base::Base;
bool operator==(const Result &r_) const {
const Base &l = *this, &r = r_;
return l == r;
}
bool isSuccess() const {
return boost::variant<S, F>::which() == 0;
}
Expand Down
1 change: 1 addition & 0 deletions core/runtime/binaryen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(binaryen_wasm_memory
)
target_link_libraries(binaryen_wasm_memory
logger
memory_error
binaryen::binaryen
memory_allocator
)
Expand Down
Loading
Loading