From 1cf81058b6ead7379768acd3b9d81417be50ea44 Mon Sep 17 00:00:00 2001 From: akildemir Date: Wed, 12 Mar 2025 15:34:51 +0300 Subject: [PATCH] merge pr https://github.com/monero-project/monero/pull/9462 --- CMakeLists.txt | 1 + .../serialization/keyvalue_serialization.h | 18 ++++++++------- contrib/epee/include/span.h | 23 +++++++++++-------- contrib/epee/include/string_tools.h | 3 +++ src/crypto/crypto.h | 4 ++-- src/cryptonote_protocol/block_queue.cpp | 13 ++--------- src/daemon/main.cpp | 8 +++---- src/lmdb/util.h | 4 ++-- src/simplewallet/simplewallet.cpp | 4 ++-- src/wallet/api/wallet.cpp | 8 +++---- src/wallet/wallet2.cpp | 2 +- src/wallet/wallet_rpc_server.cpp | 4 ++-- tests/benchmark.cpp | 2 +- tests/functional_tests/make_test_signature.cc | 2 +- tests/unit_tests/multisig.cpp | 2 +- 15 files changed, 49 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 668636b..6b94fa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1084,6 +1084,7 @@ endif() find_package(Boost 1.58 QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS}) add_definitions(-DBOOST_ASIO_ENABLE_SEQUENTIAL_STRAND_ALLOCATION) add_definitions(-DBOOST_NO_AUTO_PTR) +add_definitions(-DBOOST_UUID_DISABLE_ALIGNMENT) # This restores UUID's std::has_unique_object_representations property set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_LIB_SUFFIXES}) if(NOT Boost_FOUND) diff --git a/contrib/epee/include/serialization/keyvalue_serialization.h b/contrib/epee/include/serialization/keyvalue_serialization.h index 06d7432..fbbddc7 100644 --- a/contrib/epee/include/serialization/keyvalue_serialization.h +++ b/contrib/epee/include/serialization/keyvalue_serialization.h @@ -98,16 +98,18 @@ public: \ #define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) \ epee::serialization::selector::serialize_t_val_as_blob(this_ref.varialble, stg, hparent_section, val_name); -#define KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, val_name) \ - static_assert(std::is_pod::value, "t_type must be a POD type."); \ - KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) +#define KV_SERIALIZE_VAL_POD_AS_BLOB_N(variable, val_name) \ + static_assert(std::is_trivially_copyable(), "t_type must be a trivially copyable type."); \ + static_assert(std::is_standard_layout(), "t_type must be a standard layout type."); \ + KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(variable, val_name) -#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, val_name, default_value) \ +#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(variable, val_name, default_value) \ do { \ - static_assert(std::is_pod::value, "t_type must be a POD type."); \ - bool ret = KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name); \ + static_assert(std::is_trivially_copyable(), "t_type must be a trivially copyable type."); \ + static_assert(std::is_standard_layout(), "t_type must be a standard layout type."); \ + bool ret = KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(variable, val_name) \ if (!ret) \ - epee::serialize_default(this_ref.varialble, default_value); \ + epee::serialize_default(this_ref.variable, default_value); \ } while(0); #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, val_name) \ @@ -118,7 +120,7 @@ public: \ #define KV_SERIALIZE(varialble) KV_SERIALIZE_N(varialble, #varialble) #define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, #varialble) #define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT(varialble, def) KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, #varialble, def) -#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_pod compile time check +#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_trivially_copyable and is_standard_layout compile time check #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB(varialble) KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, #varialble) #define KV_SERIALIZE_OPT(variable,default_value) KV_SERIALIZE_OPT_N(variable, #variable, default_value) diff --git a/contrib/epee/include/span.h b/contrib/epee/include/span.h index 23bd51f..01dc387 100644 --- a/contrib/epee/include/span.h +++ b/contrib/epee/include/span.h @@ -133,17 +133,14 @@ namespace epee return {src.data(), src.size()}; } - template - constexpr bool has_padding() noexcept - { - return !std::is_standard_layout() || alignof(T) != 1; - } - //! \return Cast data from `src` as `span`. template span to_byte_span(const span src) noexcept { - static_assert(!has_padding(), "source type may have padding"); + static_assert(!std::is_empty(), "empty value types will not work -> sizeof == 1"); + static_assert(std::is_standard_layout(), "type must have standard layout"); + static_assert(std::is_trivially_copyable(), "type must be trivially copyable"); + static_assert(alignof(T) == 1, "type may have padding"); return {reinterpret_cast(src.data()), src.size_bytes()}; } @@ -153,7 +150,9 @@ namespace epee { using value_type = typename T::value_type; static_assert(!std::is_empty(), "empty value types will not work -> sizeof == 1"); - static_assert(!has_padding(), "source value type may have padding"); + static_assert(std::is_standard_layout(), "value type must have standard layout"); + static_assert(std::is_trivially_copyable(), "value type must be trivially copyable"); + static_assert(alignof(value_type) == 1, "value type may have padding"); return {reinterpret_cast(src.data()), src.size() * sizeof(value_type)}; } @@ -162,7 +161,9 @@ namespace epee span as_byte_span(const T& src) noexcept { static_assert(!std::is_empty(), "empty types will not work -> sizeof == 1"); - static_assert(!has_padding(), "source type may have padding"); + static_assert(std::is_standard_layout(), "type must have standard layout"); + static_assert(std::is_trivially_copyable(), "type must be trivially copyable"); + static_assert(alignof(T) == 1, "type may have padding"); return {reinterpret_cast(std::addressof(src)), sizeof(T)}; } @@ -171,7 +172,9 @@ namespace epee span as_mut_byte_span(T& src) noexcept { static_assert(!std::is_empty(), "empty types will not work -> sizeof == 1"); - static_assert(!has_padding(), "source type may have padding"); + static_assert(std::is_standard_layout(), "type must have standard layout"); + static_assert(std::is_trivially_copyable(), "type must be trivially copyable"); + static_assert(alignof(T) == 1, "type may have padding"); return {reinterpret_cast(std::addressof(src)), sizeof(T)}; } diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index dbbe190..ba59803 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -94,6 +94,7 @@ namespace string_tools std::string pod_to_hex(const t_pod_type& s) { static_assert(std::is_standard_layout(), "expected standard layout type"); + static_assert(alignof(t_pod_type) == 1, "type may have padding"); return to_hex::string(as_byte_span(s)); } //---------------------------------------------------------------------------- @@ -101,6 +102,8 @@ namespace string_tools bool hex_to_pod(const boost::string_ref hex_str, t_pod_type& s) { static_assert(std::is_standard_layout(), "expected standard layout type"); + static_assert(alignof(t_pod_type) == 1, "type may have padding"); + static_assert(std::is_trivially_copyable(), "type must be trivially copyable"); return from_hex::to_buffer(as_mut_byte_span(s), hex_str); } //---------------------------------------------------------------------------- diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 9252017..78529f1 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -172,8 +172,8 @@ namespace crypto { */ template T rand() { - static_assert(std::is_standard_layout_v, "cannot write random bytes into non-standard layout type"); - static_assert(std::is_trivially_copyable_v, "cannot write random bytes into non-trivially copyable type"); + static_assert(std::is_standard_layout(), "cannot write random bytes into non-standard layout type"); + static_assert(std::is_trivially_copyable(), "cannot write random bytes into non-trivially copyable type"); typename std::remove_cv::type res; generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res); return res; diff --git a/src/cryptonote_protocol/block_queue.cpp b/src/cryptonote_protocol/block_queue.cpp index 4e65eaf..e5a4c0c 100644 --- a/src/cryptonote_protocol/block_queue.cpp +++ b/src/cryptonote_protocol/block_queue.cpp @@ -40,15 +40,6 @@ #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "cn.block_queue" -namespace std { - static_assert(sizeof(size_t) <= sizeof(boost::uuids::uuid), "boost::uuids::uuid too small"); - template<> struct hash { - std::size_t operator()(const boost::uuids::uuid &_v) const { - return reinterpret_cast(_v); - } - }; -} - namespace cryptonote { @@ -472,7 +463,7 @@ bool block_queue::has_spans(const boost::uuids::uuid &connection_id) const float block_queue::get_speed(const boost::uuids::uuid &connection_id) const { boost::unique_lock lock(mutex); - std::unordered_map speeds; + std::unordered_map> speeds; for (const auto &span: blocks) { if (span.blocks.empty()) @@ -480,7 +471,7 @@ float block_queue::get_speed(const boost::uuids::uuid &connection_id) const // note that the average below does not average over the whole set, but over the // previous pseudo average and the latest rate: this gives much more importance // to the latest measurements, which is fine here - std::unordered_map::iterator i = speeds.find(span.connection_id); + const auto i = speeds.find(span.connection_id); if (i == speeds.end()) speeds.insert(std::make_pair(span.connection_id, span.rate)); else diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 2b27094..b68f24f 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -141,9 +141,9 @@ void print_genesis_tx_hex(const cryptonote::network_type nettype) { miner_key_file << "Miner account address:" << std::endl; miner_key_file << cryptonote::get_account_address_as_str((network_type)nettype, false, miner_acc1.get_keys().m_account_address); miner_key_file << std::endl<< "Miner spend secret key:" << std::endl; - epee::to_hex::formatted(miner_key_file, epee::as_byte_span(miner_acc1.get_keys().m_spend_secret_key)); + epee::to_hex::formatted(miner_key_file, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_spend_secret_key)))); miner_key_file << std::endl << "Miner view secret key:" << std::endl; - epee::to_hex::formatted(miner_key_file, epee::as_byte_span(miner_acc1.get_keys().m_view_secret_key)); + epee::to_hex::formatted(miner_key_file, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_view_secret_key)))); miner_key_file << std::endl << std::endl; miner_key_file.close(); @@ -157,9 +157,9 @@ void print_genesis_tx_hex(const cryptonote::network_type nettype) { std::cout << "Miner account address:" << std::endl; std::cout << cryptonote::get_account_address_as_str((network_type)nettype, false, miner_acc1.get_keys().m_account_address); std::cout << std::endl << "Miner spend secret key:" << std::endl; - epee::to_hex::formatted(std::cout, epee::as_byte_span(miner_acc1.get_keys().m_spend_secret_key)); + epee::to_hex::formatted(std::cout, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_spend_secret_key)))); std::cout << std::endl << "Miner view secret key:" << std::endl; - epee::to_hex::formatted(std::cout, epee::as_byte_span(miner_acc1.get_keys().m_view_secret_key)); + epee::to_hex::formatted(std::cout, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_view_secret_key)))); std::cout << std::endl << std::endl; std::stringstream ss; diff --git a/src/lmdb/util.h b/src/lmdb/util.h index c6c75bc..392ff20 100644 --- a/src/lmdb/util.h +++ b/src/lmdb/util.h @@ -127,7 +127,7 @@ namespace lmdb /*! A LMDB comparison function that uses `std::memcmp`. - \toaram T is `!epee::has_padding` + \toaram T has standard layout and an alignment of 1 \tparam offset to `T` within the value. \return The result of `std::memcmp` over the value. @@ -135,7 +135,7 @@ namespace lmdb template inline int compare(MDB_val const* left, MDB_val const* right) noexcept { - static_assert(!epee::has_padding(), "memcmp will not work"); + static_assert(std::is_standard_layout() && alignof(T) == 1, "memcmp will not work"); if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset) { assert("invalid use of custom comparison" == 0); diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 0affc25..44e30d8 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -8915,9 +8915,9 @@ bool simple_wallet::submit_transfer(const std::vector &args_) std::string get_tx_key_stream(crypto::secret_key tx_key, std::vector additional_tx_keys) { ostringstream oss; - oss << epee::string_tools::pod_to_hex(tx_key); + oss << epee::string_tools::pod_to_hex(unwrap(unwrap(tx_key))); for (size_t i = 0; i < additional_tx_keys.size(); ++i) - oss << epee::string_tools::pod_to_hex(additional_tx_keys[i]); + oss << epee::string_tools::pod_to_hex(unwrap(unwrap(additional_tx_keys[i]))); return oss.str(); } diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 662658d..63c0233 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -852,7 +852,7 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const std::string WalletImpl::secretViewKey() const { - return epee::string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_view_secret_key); + return epee::string_tools::pod_to_hex(unwrap(unwrap(m_wallet->get_account().get_keys().m_view_secret_key))); } std::string WalletImpl::publicViewKey() const @@ -862,7 +862,7 @@ std::string WalletImpl::publicViewKey() const std::string WalletImpl::secretSpendKey() const { - return epee::string_tools::pod_to_hex(m_wallet->get_account().get_keys().m_spend_secret_key); + return epee::string_tools::pod_to_hex(unwrap(unwrap(m_wallet->get_account().get_keys().m_spend_secret_key))); } std::string WalletImpl::publicSpendKey() const @@ -1886,9 +1886,9 @@ std::string WalletImpl::getTxKey(const std::string &txid_str) const { clearStatus(); std::ostringstream oss; - oss << epee::string_tools::pod_to_hex(tx_key); + oss << epee::string_tools::pod_to_hex(unwrap(unwrap(tx_key))); for (size_t i = 0; i < additional_tx_keys.size(); ++i) - oss << epee::string_tools::pod_to_hex(additional_tx_keys[i]); + oss << epee::string_tools::pod_to_hex(unwrap(unwrap(additional_tx_keys[i]))); return oss.str(); } else diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f3f824e..766c6bc 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -5124,7 +5124,7 @@ boost::optional wallet2::get_keys_file_data(const epee: original_address = get_account_address_as_str(m_nettype, false, m_original_address); value.SetString(original_address.c_str(), original_address.length()); json.AddMember("original_address", value, json.GetAllocator()); - original_view_secret_key = epee::string_tools::pod_to_hex(m_original_view_secret_key); + original_view_secret_key = epee::string_tools::pod_to_hex(unwrap(unwrap(m_original_view_secret_key))); value.SetString(original_view_secret_key.c_str(), original_view_secret_key.length()); json.AddMember("original_view_secret_key", value, json.GetAllocator()); } diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index f6d5589..ce64c46 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -1346,9 +1346,9 @@ namespace tools res.tx_hash_list.push_back(epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx))); if (req.get_tx_keys) { - res.tx_key_list.push_back(epee::string_tools::pod_to_hex(ptx.tx_key)); + res.tx_key_list.push_back(epee::string_tools::pod_to_hex(unwrap(unwrap(ptx.tx_key)))); for (const crypto::secret_key& additional_tx_key : ptx.additional_tx_keys) - res.tx_key_list.back() += epee::string_tools::pod_to_hex(additional_tx_key); + res.tx_key_list.back() += epee::string_tools::pod_to_hex(unwrap(unwrap(additional_tx_key))); } } diff --git a/tests/benchmark.cpp b/tests/benchmark.cpp index 6a845d6..660783d 100644 --- a/tests/benchmark.cpp +++ b/tests/benchmark.cpp @@ -109,7 +109,7 @@ namespace template bool compare(const T& lhs, const T& rhs) noexcept { - static_assert(!epee::has_padding(), "type might have padding"); + static_assert(std::is_standard_layout() && alignof(T) == 1, "type might have padding"); return std::memcmp(std::addressof(lhs), std::addressof(rhs), sizeof(T)) == 0; } diff --git a/tests/functional_tests/make_test_signature.cc b/tests/functional_tests/make_test_signature.cc index e9dab8b..09a3f51 100644 --- a/tests/functional_tests/make_test_signature.cc +++ b/tests/functional_tests/make_test_signature.cc @@ -48,7 +48,7 @@ int main(int argc, const char **argv) crypto::public_key pkey; crypto::random32_unbiased((unsigned char*)skey.data); crypto::secret_key_to_public_key(skey, pkey); - printf("%s %s\n", epee::string_tools::pod_to_hex(skey).c_str(), epee::string_tools::pod_to_hex(pkey).c_str()); + printf("%s %s\n", epee::string_tools::pod_to_hex(unwrap(unwrap(skey))).c_str(), epee::string_tools::pod_to_hex(pkey).c_str()); return 0; } diff --git a/tests/unit_tests/multisig.cpp b/tests/unit_tests/multisig.cpp index 3b3c419..71416aa 100644 --- a/tests/unit_tests/multisig.cpp +++ b/tests/unit_tests/multisig.cpp @@ -80,7 +80,7 @@ static void make_wallet(unsigned int idx, tools::wallet2 &wallet) wallet.generate("", "", spendkey, true, false); ASSERT_TRUE(test_addresses[idx].address == wallet.get_account().get_public_address_str(cryptonote::TESTNET)); wallet.decrypt_keys(""); - ASSERT_TRUE(test_addresses[idx].spendkey == epee::string_tools::pod_to_hex(wallet.get_account().get_keys().m_spend_secret_key)); + ASSERT_TRUE(test_addresses[idx].spendkey == epee::string_tools::pod_to_hex(unwrap(unwrap(wallet.get_account().get_keys().m_spend_secret_key)))); wallet.encrypt_keys(""); } catch (const std::exception &e)