Skip to content
Draft
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
29 changes: 27 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ project(monad)

set(CATEGORY_MAIN_DIR ${PROJECT_SOURCE_DIR})

set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -include ${PROJECT_SOURCE_DIR}/category/core/char_traits.hpp")

if(CMAKE_CXX_FLAGS MATCHES "-stdlib=libc\\+\\+")
set(CMAKE_CXX_STANDARD_LIBRARIES
"${CMAKE_CXX_STANDARD_LIBRARIES} -lc++ -lc++abi")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 15)
message(FATAL_ERROR "GCC version 15 or higher is required.")
Expand Down Expand Up @@ -57,6 +65,23 @@ endif()

include(cmake/test.cmake)

set(
LIBFUZZER_RUNTIME_PATH
"$ENV{HOME}/libfuzzer-libcxx/lib/linux/libclang_rt.fuzzer-x86_64.a"
CACHE FILEPATH "Path to libc++ libfuzzer runtime archive")

if(NOT TARGET backtrace)
if(DEFINED LIBBACKTRACE_ROOT
AND EXISTS "${LIBBACKTRACE_ROOT}/lib/libbacktrace.a"
AND EXISTS "${LIBBACKTRACE_ROOT}/include/backtrace.h")
add_library(backtrace STATIC IMPORTED)
set_target_properties(
backtrace PROPERTIES
IMPORTED_LOCATION "${LIBBACKTRACE_ROOT}/lib/libbacktrace.a"
INTERFACE_INCLUDE_DIRECTORIES "${LIBBACKTRACE_ROOT}/include")
endif()
endif()

# ##############################################################################
# deps
# ##############################################################################
Expand Down Expand Up @@ -217,7 +242,7 @@ function(monad_add_test2 target)
${target}
PRIVATE "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/test/unit/common/include")
target_include_directories(${target} PRIVATE "${TOP_CURRENT_BINARY_DIR}/test")
target_link_libraries(${target} monad_execution GTest::GTest GTest::Main)
target_link_libraries(${target} monad_execution GTest::gtest GTest::gtest_main)
if("${target}" MATCHES "test_statesync")
gtest_discover_tests(
${target} DISCOVERY_MODE PRE_TEST
Expand Down Expand Up @@ -287,7 +312,7 @@ add_subdirectory("category/vm")

# Assemble all the object libraries into complete monad runtime library,
# monad_execution
add_library(monad_execution)
add_library(monad_execution STATIC)
target_link_libraries(
monad_execution
PUBLIC monad_core
Expand Down
21 changes: 16 additions & 5 deletions category/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ include("cmake/find_our_dependency.cmake")

find_package(
Boost CONFIG REQUIRED
COMPONENTS context fiber stacktrace_basic
COMPONENTS context fiber stacktrace_basic stacktrace_addr2line
OPTIONAL_COMPONENTS stacktrace_backtrace)

function(check_if_boost_fiber_needs_ucontext_macro)
Expand Down Expand Up @@ -173,6 +173,17 @@ endif()
target_include_directories(monad_core PRIVATE "${THIRD_PARTY_DIR}/openssl")
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
target_sources(monad_core PRIVATE "keccak_impl.S")
string(REGEX MATCH "-m(arch|cpu)=[^ ]+"
MONAD_CORE_ASM_ARCH_FLAG "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
if(MONAD_CORE_ASM_ARCH_FLAG)
set(MONAD_CORE_ASM_FLAGS "${MONAD_CORE_ASM_ARCH_FLAG}")
elseif(DEFINED ENV{ASMFLAGS})
set(MONAD_CORE_ASM_FLAGS "$ENV{ASMFLAGS}")
else()
set(MONAD_CORE_ASM_FLAGS "-march=haswell")
endif()
target_compile_options(
monad_core PRIVATE $<$<COMPILE_LANGUAGE:ASM>:${MONAD_CORE_ASM_FLAGS}>)
else()
target_sources(monad_core
PRIVATE "third_party/openssl/crypto/sha/keccak1600.c")
Expand All @@ -190,6 +201,10 @@ if(TARGET Boost::stacktrace_backtrace)
target_compile_definitions(monad_core
PRIVATE BOOST_STACKTRACE_USE_BACKTRACE=1)
target_link_libraries(monad_core PRIVATE Boost::stacktrace_backtrace)
elseif(TARGET Boost::stacktrace_addr2line)
target_compile_definitions(monad_core
PRIVATE BOOST_STACKTRACE_USE_ADDR2LINE=1)
target_link_libraries(monad_core PRIVATE Boost::stacktrace_addr2line)
else()
message(WARNING "using suboptimal basic backtrace library")
target_link_libraries(monad_core PRIVATE Boost::stacktrace_basic)
Expand Down Expand Up @@ -221,10 +236,6 @@ target_link_libraries(monad_core_disas PUBLIC monad_core)
# tests
# ##############################################################################

enable_testing()

find_package(GTest REQUIRED)

add_subdirectory("test")

monad_add_test(static_lru_test lru/static_lru_test.cpp)
16 changes: 16 additions & 0 deletions category/core/byte_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#pragma once

#include <category/core/config.hpp>
#include <category/core/char_traits.hpp>

#include <evmc/bytes.hpp>

Expand Down Expand Up @@ -48,4 +49,19 @@ inline byte_string_view to_byte_string_view(std::string const &s)
return {reinterpret_cast<unsigned char const *>(&s[0]), s.size()};
}

inline byte_string_view to_byte_string_view(byte_string const &s)
{
return {s.data(), s.size()};
}

inline byte_string to_byte_string(byte_string_view view)
{
return byte_string(view.begin(), view.end());
}

inline void append_bytes(byte_string &dest, byte_string_view view)
{
dest.insert(dest.end(), view.begin(), view.end());
}

MONAD_NAMESPACE_END
11 changes: 11 additions & 0 deletions category/core/bytes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ constexpr bytes32_t to_bytes(byte_string_view const data) noexcept
return byte;
}

inline byte_string_view
to_byte_string_view(bytes32_t const &value) noexcept
{
return {value.bytes, sizeof value.bytes};
}

inline byte_string to_byte_string(bytes32_t const &value)
{
return byte_string(value.bytes, sizeof value.bytes);
}

using namespace evmc::literals;
inline constexpr bytes32_t NULL_HASH{
0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470_bytes32};
Expand Down
2 changes: 2 additions & 0 deletions category/core/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#pragma once

#include <category/core/char_traits.hpp>

#include <bit>
#include <climits>

Expand Down
5 changes: 5 additions & 0 deletions category/core/fiber/priority_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ struct PriorityTask
std::function<void()> task{};
};

#if defined(_LIBCPP_VERSION)
static_assert(sizeof(PriorityTask) == 64);
static_assert(alignof(PriorityTask) == 16);
#else
static_assert(sizeof(PriorityTask) == 40);
static_assert(alignof(PriorityTask) == 8);
#endif

MONAD_FIBER_NAMESPACE_END
1 change: 1 addition & 0 deletions category/core/mem/allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <category/core/config.hpp>

#include <algorithm>
#include <cstdlib>
#include <concepts>
#include <memory>
#include <span>
Expand Down
2 changes: 1 addition & 1 deletion category/core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
function(monad_add_test target)
add_executable(${target} ${ARGN})
monad_compile_options(${target})
target_link_libraries(${target} monad_core GTest::GTest GTest::Main)
target_link_libraries(${target} monad_core GTest::gtest GTest::gtest_main)
add_test(NAME ${target} COMMAND $<TARGET_FILE:${target}>)
endfunction()

Expand Down
44 changes: 32 additions & 12 deletions category/core/test/encode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,25 @@ TEST(rlp, encode_string)

result = monad::rlp::encode_string(buf, byte_string(55, 1));
EXPECT_EQ(result.data() - buf, 56);
EXPECT_TRUE(
byte_string_view(buf, result.data()) ==
byte_string({183}) + byte_string(55, 1));
{
auto expected = byte_string({183});
auto suffix = byte_string(55, 1);
expected.insert(expected.end(), suffix.begin(), suffix.end());
EXPECT_TRUE(byte_string_view(buf, result.data()) == byte_string_view(
expected.data(),
expected.size()));
}

result = monad::rlp::encode_string(buf, byte_string(56, 1));
EXPECT_EQ(result.data() - buf, 58);
EXPECT_TRUE(
byte_string_view(buf, result.data()) ==
byte_string({184, 56}) + byte_string(56, 1));
{
auto expected = byte_string({184, 56});
auto suffix = byte_string(56, 1);
expected.insert(expected.end(), suffix.begin(), suffix.end());
EXPECT_TRUE(byte_string_view(buf, result.data()) == byte_string_view(
expected.data(),
expected.size()));
}
}

TEST(rlp, list_length)
Expand Down Expand Up @@ -191,13 +201,23 @@ TEST(rlp, encode_list)

result = monad::rlp::encode_list(buf, byte_string(55, 1));
EXPECT_EQ(result.data() - buf, 56);
EXPECT_TRUE(
byte_string_view(buf, result.data()) ==
byte_string({247}) + byte_string(55, 1));
{
auto expected = byte_string({247});
auto suffix = byte_string(55, 1);
expected.insert(expected.end(), suffix.begin(), suffix.end());
EXPECT_TRUE(byte_string_view(buf, result.data()) == byte_string_view(
expected.data(),
expected.size()));
}

result = monad::rlp::encode_list(buf, byte_string(56, 1));
EXPECT_EQ(result.data() - buf, 58);
EXPECT_TRUE(
byte_string_view(buf, result.data()) ==
byte_string({248, 56}) + byte_string(56, 1));
{
auto expected = byte_string({248, 56});
auto suffix = byte_string(56, 1);
expected.insert(expected.end(), suffix.begin(), suffix.end());
EXPECT_TRUE(byte_string_view(buf, result.data()) == byte_string_view(
expected.data(),
expected.size()));
}
}
8 changes: 8 additions & 0 deletions category/execution/ethereum/core/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ struct Block
friend bool operator==(Block const &, Block const &) = default;
};

#if defined(_LIBCPP_VERSION)
static_assert(sizeof(BlockHeader) == 752);
#else
static_assert(sizeof(BlockHeader) == 760);
#endif
static_assert(alignof(BlockHeader) == 8);

#if defined(_LIBCPP_VERSION)
static_assert(sizeof(Block) == 832);
#else
static_assert(sizeof(Block) == 840);
#endif
static_assert(alignof(Block) == 8);

MONAD_NAMESPACE_END
26 changes: 18 additions & 8 deletions category/execution/ethereum/core/contract/abi_encode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ constexpr bytes32_t abi_encode_uint(I const &i)
return output;
}

inline void append_bytes32(byte_string &dest, bytes32_t const &value)
{
append_bytes(dest, byte_string_view{value.bytes, sizeof value.bytes});
}

constexpr bytes32_t abi_encode_bool(bool const b)
{
u64_be as_int = b ? 1 : 0;
Expand All @@ -68,8 +73,9 @@ inline byte_string abi_encode_bytes(byte_string_view const input)
u256_be const size{input.size()};
size_t const padding =
round_up(input.size(), sizeof(bytes32_t)) - input.size();
output += abi_encode_uint(size);
output += input;
auto const encoded_size = abi_encode_uint(size);
append_bytes32(output, encoded_size);
append_bytes(output, input);
output = output.append(padding, 0);
return output;
}
Expand All @@ -79,9 +85,11 @@ inline byte_string abi_encode_uint_array(std::vector<I> const &arr)
{
byte_string output{};
u64_be const len_be{arr.size()};
output += abi_encode_uint(len_be);
auto const encoded_len = abi_encode_uint(len_be);
append_bytes32(output, encoded_len);
for (auto const &e : arr) {
output += abi_encode_uint(e);
auto const encoded_elem = abi_encode_uint(e);
append_bytes32(output, encoded_elem);
}

return output;
Expand All @@ -91,9 +99,11 @@ inline byte_string abi_encode_address_array(std::vector<Address> const &arr)
{
byte_string output{};
u64_be const len_be{arr.size()};
output += abi_encode_uint(len_be);
auto const encoded_len = abi_encode_uint(len_be);
append_bytes32(output, encoded_len);
for (Address const &e : arr) {
output += abi_encode_address(e);
auto const encoded_addr = abi_encode_address(e);
append_bytes32(output, encoded_addr);
}

return output;
Expand All @@ -113,13 +123,13 @@ class AbiEncoder

void add_static(bytes32_t data)
{
head_ += data;
append_bytes32(head_, data);
}

void add_dynamic(byte_string data)
{
unresolved_offsets_.emplace_back(head_.size(), tail_.size());
head_ += bytes32_t{};
append_bytes32(head_, bytes32_t{});
tail_ += data;
}

Expand Down
2 changes: 1 addition & 1 deletion category/execution/ethereum/core/contract/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class EventBuilder
// Add a non-indexed parameter
EventBuilder &&add_data(byte_string_view const data) &&
{
event_.data += data;
append_bytes(event_.data, data);
return std::move(*this);
}

Expand Down
2 changes: 2 additions & 0 deletions category/execution/ethereum/core/fmt/address_fmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <category/core/basic_formatter.hpp>
#include <category/execution/ethereum/core/address.hpp>

#include <span>

#include <quill/Quill.h>
#include <quill/bundled/fmt/format.h>

Expand Down
2 changes: 2 additions & 0 deletions category/execution/ethereum/core/fmt/bytes_fmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <category/core/basic_formatter.hpp>
#include <category/core/bytes.hpp>

#include <span>

#include <quill/Quill.h>
#include <quill/bundled/fmt/format.h>

Expand Down
4 changes: 4 additions & 0 deletions category/execution/ethereum/core/receipt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ struct Receipt

void populate_bloom(Receipt::Bloom &, Receipt::Log const &);

#if defined(_LIBCPP_VERSION)
static_assert(sizeof(Receipt::Log) == 72);
#else
static_assert(sizeof(Receipt::Log) == 80);
#endif
static_assert(alignof(Receipt::Log) == 8);

static_assert(sizeof(Receipt) == 304);
Expand Down
3 changes: 2 additions & 1 deletion category/execution/ethereum/core/rlp/block_rlp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ Result<BlockHeader> decode_block_header(byte_string_view &enc)
block_header.gas_used, decode_unsigned<uint64_t>(payload));
BOOST_OUTCOME_TRY(
block_header.timestamp, decode_unsigned<uint64_t>(payload));
BOOST_OUTCOME_TRY(block_header.extra_data, decode_string(payload));
BOOST_OUTCOME_TRY(auto extra_data_view, decode_string(payload));
block_header.extra_data = to_byte_string(extra_data_view);
if (block_header.extra_data.size() > EXTRA_DATA_MAX_LENGTH) {
return DecodeError::Overflow;
}
Expand Down
3 changes: 2 additions & 1 deletion category/execution/ethereum/core/rlp/receipt_rlp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ Result<Receipt::Log> decode_log(byte_string_view &enc)
BOOST_OUTCOME_TRY(auto payload, parse_list_metadata(enc));
BOOST_OUTCOME_TRY(log.address, decode_address(payload));
BOOST_OUTCOME_TRY(log.topics, decode_topics(payload));
BOOST_OUTCOME_TRY(log.data, decode_string(payload));
BOOST_OUTCOME_TRY(auto data_view, decode_string(payload));
log.data = to_byte_string(data_view);

if (MONAD_UNLIKELY(!payload.empty())) {
return DecodeError::InputTooLong;
Expand Down
Loading
Loading