Skip to content

Commit

Permalink
Add runtime check for CPU architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jan 26, 2023
1 parent 19e51d9 commit bce4ad7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/evmone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_library(evmone
baseline_instruction_table.cpp
baseline_instruction_table.hpp
eof.cpp
eof.hpp
eof.hpp
instructions.hpp
instructions_calls.cpp
instructions_opcodes.hpp
Expand All @@ -37,6 +37,13 @@ target_link_libraries(evmone PUBLIC evmc::evmc intx::intx PRIVATE ethash::keccak
target_include_directories(evmone PUBLIC
$<BUILD_INTERFACE:${include_dir}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if(DEFINED EVMONE_X86_64_ARCH_LEVEL)
# Add CPU architecture runtime check. The EVMONE_X86_64_ARCH_LEVEL has a valid value.
target_sources(evmone PRIVATE cpu_check.cpp)
set_source_files_properties(cpu_check.cpp PROPERTIES COMPILE_DEFINITIONS EVMONE_X86_64_ARCH_LEVEL=${EVMONE_X86_64_ARCH_LEVEL})
endif()

if(CABLE_COMPILER_GNULIKE)
target_compile_options(
evmone PRIVATE
Expand Down
28 changes: 28 additions & 0 deletions lib/evmone/cpu_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#define STRINGIFY_HELPER(X) #X
#define STRINGIFY(X) STRINGIFY_HELPER(X)

#ifdef __clang__
// Clang 16 does not support achitecture levels in __builtin_cpu_supports(). Use approximations.
#if EVMONE_X86_64_ARCH_LEVEL == 2
#define CPU_FEATURE "sse4.2"
#endif
#elif __GNUC__
#define CPU_FEATURE "x86-64-v" STRINGIFY(EVMONE_X86_64_ARCH_LEVEL)
#endif

#ifndef CPU_FEATURE
#error "EVMONE_X86_64_ARCH_LEVEL: Unsupported x86-64 architecture level"
#endif

#include <cstdio>
#include <cstdlib>

static bool cpu_check = []() noexcept {
if (!__builtin_cpu_supports(CPU_FEATURE))
{
std::fputs("CPU does not support " CPU_FEATURE "\n", stderr);
std::abort();
}
return false;
}();
16 changes: 8 additions & 8 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ add_executable(evmone-unittests
target_link_libraries(evmone-unittests PRIVATE evmone evmone::state testutils evmc::instructions GTest::gtest GTest::gtest_main)
target_include_directories(evmone-unittests PRIVATE ${evmone_private_include_dir})

gtest_discover_tests(evmone-unittests TEST_PREFIX ${PROJECT_NAME}/unittests/)

option(EVMONE_EVM_TEST_TOOL "Enable EVM unit testing tool for EVMC implementations (not maintained)" OFF)
if(EVMONE_EVM_TEST_TOOL)
# The evm-test tool that contains the all evm-unittests and loads VMs as EVMC modules.
add_executable(evm-test main.cpp)
target_link_libraries(evm-test PRIVATE evm-unittests testutils evmc::evmc evmc::loader GTest::gtest)
endif()
#gtest_discover_tests(evmone-unittests TEST_PREFIX ${PROJECT_NAME}/unittests/)
#
#option(EVMONE_EVM_TEST_TOOL "Enable EVM unit testing tool for EVMC implementations (not maintained)" OFF)
#if(EVMONE_EVM_TEST_TOOL)
# The evm-test tool that contains the all evm-unittests and loads VMs as EVMC modules.
# add_executable(evm-test main.cpp)
# target_link_libraries(evm-test PRIVATE evm-unittests testutils evmc::evmc evmc::loader GTest::gtest)
#endif()

# Provide the project version to selected source files.
set_source_files_properties(
Expand Down

0 comments on commit bce4ad7

Please sign in to comment.