From 725a68fde6d6f415c0aa42d18330e84dee740a6c Mon Sep 17 00:00:00 2001 From: Huang-Ming Huang Date: Fri, 15 May 2026 01:32:26 +0000 Subject: [PATCH] Use fetched Catch2 and simplify vcpkg deps --- CMakeLists.txt | 2 +- cmake/dependencies.cmake | 11 ----------- include/sysio/vm/signals.hpp | 3 ++- tests/CMakeLists.txt | 19 ++++++++++++++++--- tests/fuzz/CMakeLists.txt | 29 ++++++++++++++++++++++------- tests/fuzz/fuzz_driver.cpp | 18 ++++++++++++++---- tests/reentry_tests.cpp | 4 ++-- tests/signals_tests.cpp | 10 ++++++++-- vcpkg-configuration.json | 10 +--------- vcpkg.json | 25 ++----------------------- 10 files changed, 68 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e4adcda..185eb1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ # build sys-vm or integrate with another system with CMake. # ################################################################################################## cmake_minimum_required(VERSION 3.12) + set(VERSION_MAJOR 1) set(VERSION_MINOR 1) set(VERSION_PATCH 0) @@ -145,4 +146,3 @@ if(ENABLE_INSTALL) endif() endif() endif() - diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index 4e34c29..a4b6216 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -3,17 +3,6 @@ include(GNUInstallDirs) -# Catch2 -find_package(Catch2 CONFIG REQUIRED) -list(APPEND CMAKE_MODULE_PATH "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/catch2") -file(MAKE_DIRECTORY "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/catch2") -file(COPY "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/catch.hpp" DESTINATION "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/catch2") - # Softfloat find_package(Threads REQUIRED) find_package(softfloat CONFIG REQUIRED) - -# LLVM -find_package(LLVM CONFIG REQUIRED) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake at: ${LLVM_DIR}") \ No newline at end of file diff --git a/include/sysio/vm/signals.hpp b/include/sysio/vm/signals.hpp index c248894..3f40c1e 100644 --- a/include/sysio/vm/signals.hpp +++ b/include/sysio/vm/signals.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -156,7 +157,7 @@ namespace sysio { namespace vm { sigjmp_buf dest; sigjmp_buf* volatile old_signal_handler = nullptr; code_memory_range = code_allocator.get_code_span(); - memory_range = mem_allocator->get_span(); + memory_range = mem_allocator ? mem_allocator->get_span() : std::span{}; int sig; if((sig = sigsetjmp(dest, 1)) == 0) { // Note: Cannot use RAII, as non-trivial destructors w/ longjmp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5cb875e..dacc92e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,6 +3,20 @@ # ############ enable_testing() +include(FetchContent) + +set(CATCH_BUILD_TESTING OFF CACHE BOOL "Disable Catch2 self-tests") +set(CATCH_INSTALL_DOCS OFF CACHE BOOL "Disable Catch2 documentation install") +set(CATCH_INSTALL_HELPERS OFF CACHE BOOL "Disable Catch2 helper install") + +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG 9c741fe96073ed620ffc032afbed1f3c789d2b68 +) +FetchContent_MakeAvailable(Catch2) + +list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/contrib") include(Catch) # OPTIONS @@ -88,7 +102,7 @@ if(ENABLE_SPEC_TESTS) add_executable(sys_vm_spec_tests main.cpp ${SPEC_TESTS}) setup_vm_target(sys_vm_spec_tests) - target_link_libraries(sys_vm_spec_tests sys-vm Catch2::Catch2WithMain) + target_link_libraries(sys_vm_spec_tests sys-vm Catch2::Catch2) target_include_directories( sys_vm_spec_tests PUBLIC @@ -166,9 +180,8 @@ add_executable(unit_tests main.cpp backend_tests.cpp vector_tests.cpp) setup_vm_target(unit_tests) -target_link_libraries(unit_tests sys-vm Catch2::Catch2WithMain) +target_link_libraries(unit_tests sys-vm Catch2::Catch2) target_compile_definitions(unit_tests PUBLIC -DCATCH_CONFIG_NO_POSIX_SIGNALS) # UNIT_TESTS: Discover catch_discover_tests(unit_tests) - diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt index 395fa42..8487832 100644 --- a/tests/fuzz/CMakeLists.txt +++ b/tests/fuzz/CMakeLists.txt @@ -1,8 +1,23 @@ -set(CMAKE_CXX_FLAGS "-g -O1 -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp,trace-div,trace-gep -gline-tables-only") +include(CheckCXXSourceCompiles) -find_package(LLVM REQUIRED CONFIG) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake at: ${LLVM_DIR}") +set(SYS_VM_FUZZ_SANITIZER_FLAGS "-fsanitize=fuzzer,address") +set(SYS_VM_FUZZ_DEBUG_FLAGS "-g" "-O1" "-gline-tables-only") + +set(CMAKE_REQUIRED_FLAGS_SAVE "${CMAKE_REQUIRED_FLAGS}") +set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${SYS_VM_FUZZ_SANITIZER_FLAGS}") +check_cxx_source_compiles([=[ + #include + #include + + extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t*, std::size_t) { + return 0; + } +]=] SYS_VM_HAS_FUZZER_ADDRESS_SANITIZER) +set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_SAVE}") + +if(NOT SYS_VM_HAS_FUZZER_ADDRESS_SANITIZER) + message(FATAL_ERROR "ENABLE_FUZZ_TESTS requires compiler/linker support for ${SYS_VM_FUZZ_SANITIZER_FLAGS}") +endif() function(add_libfuzzer_test name) set(multi_arg_options "SOURCES") @@ -15,10 +30,10 @@ function(add_libfuzzer_test name) ${add_libfuzzer_test_SOURCES} ) setup_vm_target(LLVMFuzzer-${name}) - target_compile_options(LLVMFuzzer-${name} PUBLIC "-fsanitize=fuzzer,address") - target_link_libraries(LLVMFuzzer-${name} PUBLIC sys-vm "-fsanitize=fuzzer,address") + target_compile_options(LLVMFuzzer-${name} PUBLIC ${SYS_VM_FUZZ_DEBUG_FLAGS} ${SYS_VM_FUZZ_SANITIZER_FLAGS}) + target_link_libraries(LLVMFuzzer-${name} sys-vm ${SYS_VM_FUZZ_SANITIZER_FLAGS}) endfunction() add_libfuzzer_test(wasm_fuzz_test SOURCES fuzz_driver.cpp) -add_test(wasm_libfuzzer COMMAND ${CMAKE_CURRENT_BINARY_DIR}/LLVMFuzzer-wasm_fuzz_test -jobs=40 ${CMAKE_CURRENT_SOURCE_DIR}/corpus) +add_test(NAME wasm_libfuzzer COMMAND ${CMAKE_CURRENT_BINARY_DIR}/LLVMFuzzer-wasm_fuzz_test -runs=1 -jobs=1 -rss_limit_mb=6144 -timeout=30 ${CMAKE_CURRENT_SOURCE_DIR}/corpus) diff --git a/tests/fuzz/fuzz_driver.cpp b/tests/fuzz/fuzz_driver.cpp index f55527e..eb835c2 100644 --- a/tests/fuzz/fuzz_driver.cpp +++ b/tests/fuzz/fuzz_driver.cpp @@ -1,14 +1,24 @@ -#include +#include #include +#include +#include +#include + using namespace sysio; -using namespace sysio::wasm_backend; +using namespace sysio::vm; extern "C" int LLVMFuzzerTestOneInput( const uint8_t* data, size_t size ) { wasm_allocator wa; wasm_code wc; wc.resize(size); memcpy((uint8_t*)wc.data(), data, size); - backend bkend( wc ); - bkend.execute_all(null_watchdog()); + try { + backend bkend( wc, &wa ); + bkend.execute_all(watchdog{std::chrono::milliseconds(100)}); + } catch(const std::exception&) { + // Invalid or long-running fuzz inputs are expected to throw VM exceptions. + // Returning 0 tells libFuzzer the input was rejected cleanly. + } + return 0; } diff --git a/tests/reentry_tests.cpp b/tests/reentry_tests.cpp index 0187086..3d74708 100644 --- a/tests/reentry_tests.cpp +++ b/tests/reentry_tests.cpp @@ -53,10 +53,10 @@ BACKEND_TEST_CASE("test reentry", "[reentry]") { struct test_runner { backend_t* bkend; uint32_t test_func_0(uint32_t val) { - return bkend->call_with_return(*this, "env", "bar", val)->to_ui32() + 50; + return bkend->call_with_return(*this, "env", "bar", static_cast(val))->to_ui32() + 50; } uint32_t test_func_1(uint32_t val) { - return bkend->call_with_return(*this, "env", "testbar", val)->to_ui32() + 50; + return bkend->call_with_return(*this, "env", "testbar", static_cast(val))->to_ui32() + 50; } }; diff --git a/tests/signals_tests.cpp b/tests/signals_tests.cpp index 50d3e7f..04229f5 100644 --- a/tests/signals_tests.cpp +++ b/tests/signals_tests.cpp @@ -10,12 +10,16 @@ struct test_exception {}; TEST_CASE("Testing signals", "[invoke_with_signal_handler]") { bool okay = false; + // These tests call the signal handler directly, outside a real VM execution. + // A null memory allocator intentionally leaves memory_range empty so the + // handler exercises its legacy catch-all path. + sysio::vm::growable_allocator code_allocator; try { sysio::vm::invoke_with_signal_handler([]() { std::raise(SIGSEGV); }, [](int sig) { throw test_exception{}; - }, {}, {}); + }, code_allocator, nullptr); } catch(test_exception&) { okay = true; } @@ -23,9 +27,11 @@ TEST_CASE("Testing signals", "[invoke_with_signal_handler]") { } TEST_CASE("Testing throw", "[signal_handler_throw]") { + // See the signal test above: no wasm memory is involved in this direct path. + sysio::vm::growable_allocator code_allocator; CHECK_THROWS_AS(sysio::vm::invoke_with_signal_handler([](){ sysio::vm::throw_( "Exiting" ); - }, [](int){}, {}, {}), sysio::vm::wasm_exit_exception); + }, [](int){}, code_allocator, nullptr), sysio::vm::wasm_exit_exception); } static volatile sig_atomic_t sig_handled; diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index e94d9e2..ad53491 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -11,15 +11,7 @@ "repository": "https://github.com/wire-network/wire-vcpkg-registry", "baseline": "add9228a61ce5eb1dc41f89d07254b7531b2bdfc", "packages": [ - "softfloat", - "libsodium", - "cli11", - "catch2", - "boost", - "boringssl-custom", - "secp256k1-internal", - "bn256", - "bls12-381" + "softfloat" ] } ] diff --git a/vcpkg.json b/vcpkg.json index 8e8e50d..47ea290 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,27 +2,6 @@ "name": "wire-sys-vm", "version": "5.2.0-rc1", "dependencies": [ - "softfloat", - { - "name": "catch2", - "features": [ - "no-posix-signals" - ] - }, - { - "name": "llvm", - "default-features": true, - "features": [ - "enable-rtti", - "target-aarch64", - "target-x86" - ] - } - ], - "overrides": [ - { - "name": "llvm", - "version": "18.1.6#5" - } + "softfloat" ] -} \ No newline at end of file +}