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

core: address error=array-bounds in gcc14 #2639

Merged
merged 3 commits into from
Jan 10, 2025
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
2 changes: 1 addition & 1 deletion silkworm/core/common/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#pragma once

namespace silkworm {
void abort_due_to_assertion_failure(char const* expr, char const* file, int line);
[[noreturn]] void abort_due_to_assertion_failure(char const* expr, char const* file, int line);
}

// SILKWORM_ASSERT always aborts program execution on assertion failure, even when NDEBUG is defined.
Expand Down
16 changes: 10 additions & 6 deletions silkworm/core/common/small_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
namespace silkworm {

// SmallMap is a constexpr-friendly immutable map suitable for a small number of elements.
template <std::totally_ordered Key, std::default_initializable T, size_t max_size = 8>
template <std::totally_ordered Key, std::default_initializable T, size_t maximum_size = 8>
class SmallMap {
public:
using ValueType = std::pair<Key, T>;

constexpr SmallMap() noexcept = default;

constexpr SmallMap(std::initializer_list<ValueType> init) : size_(init.size()) {
SILKWORM_ASSERT(size_ <= max_size);
SILKWORM_ASSERT(size_ <= maximum_size);
for (size_t i{0}; i < size_; ++i) {
data_[i] = *(std::data(init) + i);
}
Expand All @@ -47,14 +47,14 @@ class SmallMap {
template <std::input_iterator InputIt>
constexpr SmallMap(InputIt first, InputIt last) {
for (InputIt it{first}; it != last; ++it) {
SILKWORM_ASSERT(size_ < max_size);
SILKWORM_ASSERT(size_ < maximum_size);
data_[size_++] = *it;
}
sort();
}

constexpr SmallMap(const SmallMap& other) : size_{other.size_} {
for (size_t i{0}; i < max_size; ++i) {
for (size_t i{0}; i < maximum_size; ++i) {
data_[i] = other.data_[i];
}
}
Expand All @@ -63,7 +63,7 @@ class SmallMap {
return *this;
}
size_ = other.size_;
for (size_t i{0}; i < max_size; ++i) {
for (size_t i{0}; i < maximum_size; ++i) {
data_[i] = other.data_[i];
}
return *this;
Expand All @@ -77,6 +77,10 @@ class SmallMap {
return size_;
}

static constexpr size_t max_size() noexcept {
return maximum_size;
}

constexpr auto begin() const noexcept {
return data_.begin();
}
Expand Down Expand Up @@ -110,7 +114,7 @@ class SmallMap {
[](const ValueType& a, const ValueType& b) { return a.first < b.first; });
}

std::array<ValueType, max_size> data_{};
std::array<ValueType, maximum_size> data_{};
size_t size_{0};
};

Expand Down
10 changes: 10 additions & 0 deletions silkworm/core/protocol/bor/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,23 @@ std::optional<Config> Config::from_json(const nlohmann::json& json) noexcept {
const BlockNum from{std::stoull(item.key(), nullptr, 0)};
period.emplace_back(from, item.value().get<uint64_t>());
}
if (period.size() > config.period.max_size()) {
return std::nullopt;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still needed given the if-check above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still needed given the if-check above?

The error is still emitted unfortunately even with the check.

Copy link
Contributor

@battlmonstr battlmonstr Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you clarify the error? how does a big number like "576460752303423485" or "9223372036854775760" end up there despite the check? who is passing that number? could it be that if is faulty due to some signed-unsigned UB?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know where this number comes from (it's some internal details of the array-bounds check in gcc). The comparison is fine (both arguments are of type size_t).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning comes from optimized GCC builds. After some optimizations related to inlining and constant propagation compiler can convince itself that it knows the access index of an array and compare it with the array size.
Unfortunately, this analysis is known to have false-positives, see e.g.

If we want to be nice and you think it is worth the time I can reduce this example and report an issue. Or I can make a tutorial how to reduce failing compilations.

// silence misdiagnostics in gcc 14
config.period = {period.begin(), period.end()};
#pragma GCC diagnostic pop

std::vector<std::pair<BlockNum, uint64_t>> sprint;
for (const auto& item : json["sprint"].items()) {
const BlockNum from{std::stoull(item.key(), nullptr, 0)};
sprint.emplace_back(from, item.value().get<uint64_t>());
}
if (sprint.size() > config.sprint.max_size()) {
return std::nullopt;
}
config.sprint = {sprint.begin(), sprint.end()};

config.validator_contract = hex_to_address(json["validatorContract"].get<std::string>(), /*return_zero_on_err=*/true);
Expand Down
Loading