Skip to content

Commit

Permalink
Move size_t overflow check to a generic helper
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jan 11, 2022
1 parent 5f47418 commit 85cf7a8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
11 changes: 2 additions & 9 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,8 @@ inline uint32_t grow_memory(
const auto new_pages = static_cast<uint32_t>(new_pages_u64);

const uint64_t new_bytes = memory_pages_to_bytes(new_pages);
if constexpr (sizeof(size_t) < sizeof(uint64_t))
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-type-limit-compare"
// NOLINTNEXTLINE(clang-diagnostic-tautological-type-limit-compare)
if (new_bytes > std::numeric_limits<size_t>::max())
return static_cast<uint32_t>(-1);
#pragma clang diagnostic pop
}
if (!can_narrow_cast<size_t>(new_bytes))
return static_cast<uint32_t>(-1);

try
{
Expand Down
14 changes: 3 additions & 11 deletions lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,10 @@ std::tuple<bytes_ptr, Limits> allocate_memory(const std::vector<Memory>& module_
}

const uint64_t memory_min_bytes = memory_pages_to_bytes(memory_min);
if constexpr (sizeof(size_t) < sizeof(uint64_t))
if (!can_narrow_cast<size_t>(memory_min_bytes))
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-type-limit-compare"
// NOLINTNEXTLINE(clang-diagnostic-tautological-type-limit-compare)
if (memory_min_bytes > std::numeric_limits<size_t>::max())
{
throw instantiate_error{"cannot allocate more than " +
std::to_string(std::numeric_limits<size_t>::max()) +
" bytes"};
}
#pragma clang diagnostic pop
throw instantiate_error{"cannot allocate more than " +
std::to_string(std::numeric_limits<size_t>::max()) + " bytes"};
}
// NOTE: fill it with zeroes
assert(memory_min_bytes <= std::numeric_limits<size_t>::max());
Expand Down
17 changes: 17 additions & 0 deletions lib/fizzy/limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#pragma once

#include <cstdint>
#include <limits>
#include <type_traits>

namespace fizzy
{
Expand All @@ -17,6 +19,21 @@ inline constexpr uint64_t memory_pages_to_bytes(uint32_t pages) noexcept
return uint64_t{pages} * PageSize;
}

/// Check if the integral value can be casted without an overflow to a narrower type.
template <typename TypeTo, typename TypeFrom,
typename =
typename std::enable_if_t<std::is_integral_v<TypeFrom> && std::is_integral_v<TypeTo> &&
sizeof(TypeFrom) >= sizeof(TypeTo)>>
inline constexpr bool can_narrow_cast(TypeFrom value) noexcept
{
if constexpr (sizeof(TypeFrom) > sizeof(TypeTo))
{
return value <= std::numeric_limits<TypeTo>::max();
}
else
return true;
}

/// The maximum memory page limit as defined by the specification.
/// It is only possible to address 4 GB (32-bit) of memory.
constexpr uint32_t MaxMemoryPagesLimit = (4 * 1024 * 1024 * 1024ULL) / PageSize;
Expand Down
20 changes: 20 additions & 0 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,26 @@ TEST(instantiate, memory_pages_to_bytes)
EXPECT_EQ(memory_pages_to_bytes(MaxMemoryPagesLimit), 4 * 1024 * 1024 * 1024ULL);
}

TEST(instantiate, can_narrow_cast)
{
EXPECT_TRUE(can_narrow_cast<size_t>(std::numeric_limits<size_t>::max()));
EXPECT_TRUE(can_narrow_cast<size_t>(uint64_t{std::numeric_limits<size_t>::max()}));

EXPECT_TRUE(can_narrow_cast<size_t>(uint64_t{0}));
EXPECT_TRUE(can_narrow_cast<size_t>(uint64_t{1}));
EXPECT_TRUE(can_narrow_cast<size_t>(uint64_t{std::numeric_limits<size_t>::max()}));

if constexpr (sizeof(size_t) < sizeof(uint64_t))
{
EXPECT_FALSE(can_narrow_cast<size_t>(uint64_t{std::numeric_limits<size_t>::max()} + 1));
EXPECT_FALSE(can_narrow_cast<size_t>(std::numeric_limits<uint64_t>::max()));
}
else
{
EXPECT_TRUE(can_narrow_cast<size_t>(std::numeric_limits<uint64_t>::max()));
}
}

TEST(instantiate, element_section)
{
/* wat2wasm
Expand Down

0 comments on commit 85cf7a8

Please sign in to comment.