From 6b98a8d3842c8ae89a48fd3457c61247ee6a1432 Mon Sep 17 00:00:00 2001 From: Shankar Seal <74580197+shankarseal@users.noreply.github.com> Date: Fri, 13 May 2022 17:23:18 -0700 Subject: [PATCH] cmakefile (#1075) --- .github/workflows/cicd.yml | 16 ++-- .github/workflows/reusable-cmake-build.yml | 28 ++++++ CMakeLists.txt | 16 ++-- cmake/process_bpf_source_file.cmake | 73 +++++++++++++++ cmake/settings.cmake | 6 ++ libs/api/CMakeLists.txt | 1 + libs/service/CMakeLists.txt | 1 + rpc_interface/CMakeLists.txt | 6 +- scripts/CMakeLists.txt | 2 + scripts/Convert-BpfToNative.ps1 | 11 ++- scripts/setup_build/CMakeLists.txt | 102 +++++++++++++++++++++ tests/CMakeLists.txt | 7 +- tests/bpf2c_tests/CMakeLists.txt | 33 ++++--- tests/bpf2c_tests/elf_bpf.cpp | 11 ++- tests/bpftool_tests/CMakeLists.txt | 24 +++++ tests/cilium/CMakeLists.txt | 30 ++++++ tests/fuzz/CMakeLists.txt | 63 +++++++++++++ tests/sample/CMakeLists.txt | 8 ++ tests/sample/ext/CMakeLists.txt | 5 + tests/sample/ext/app/CMakeLists.txt | 42 +++++++++ tests/sample/ext/drv/CMakeLists.txt | 57 ++++++++++++ tests/sample/sample.vcxproj | 10 +- tests/sample/unsafe/CMakeLists.txt | 5 + tests/socket/CMakeLists.txt | 38 ++++++++ tests/unit/CMakeLists.txt | 1 + tests/xdp/CMakeLists.txt | 1 + tools/bpf2c/CMakeLists.txt | 1 + 27 files changed, 555 insertions(+), 43 deletions(-) create mode 100644 cmake/process_bpf_source_file.cmake create mode 100644 scripts/setup_build/CMakeLists.txt create mode 100644 tests/bpftool_tests/CMakeLists.txt create mode 100644 tests/cilium/CMakeLists.txt create mode 100644 tests/fuzz/CMakeLists.txt create mode 100644 tests/sample/CMakeLists.txt create mode 100644 tests/sample/ext/CMakeLists.txt create mode 100644 tests/sample/ext/app/CMakeLists.txt create mode 100644 tests/sample/ext/drv/CMakeLists.txt create mode 100644 tests/sample/unsafe/CMakeLists.txt create mode 100644 tests/socket/CMakeLists.txt diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 4b05481cd..b9c5f9662 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -38,7 +38,7 @@ jobs: if: github.event_name == 'schedule' || github.event_name == 'pull_request' || github.event_name == 'push' uses: ./.github/workflows/reusable-build.yml with: - build_artifact: Build-x64 + build_artifact: Build-x64-vcxproj generate_release_package: true cmake: @@ -46,7 +46,7 @@ jobs: if: github.event_name == 'schedule' || github.event_name == 'pull_request' || github.event_name == 'push' uses: ./.github/workflows/reusable-cmake-build.yml with: - build_artifact: Build-x64-cmake + build_artifact: Build-x64 # Run the unit tests in GitHub. unit_tests: @@ -56,7 +56,7 @@ jobs: with: name: unit_tests test_command: unit_tests.exe -d yes - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 environment: windows-2019 code_coverage: true @@ -72,7 +72,7 @@ jobs: pre_test: call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" test_command: bpf2c_tests.exe -d yes name: bpf2c - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 environment: windows-2019 code_coverage: true @@ -90,7 +90,7 @@ jobs: test_command: powershell ".\execute_ebpf_cicd_tests.ps1" post_test: powershell ".\cleanup_ebpf_cicd_tests.ps1" name: driver - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 environment: ebpf_cicd_tests # driver test copies dumps to testlog folder. @@ -103,7 +103,7 @@ jobs: if: github.event_name == 'schedule' || github.event_name == 'pull_request' || github.event_name == 'push' uses: ./.github/workflows/ossar-scan.yml with: - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 # Additional jobs to run on pull and schedule only (skip push). @@ -148,7 +148,7 @@ jobs: with: name: cilium_tests test_command: cilium_tests.exe -d yes - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 environment: windows-2019 code_coverage: false @@ -163,7 +163,7 @@ jobs: name: stress # Until there is a dedicated stress test, re-use the perf test. test_command: ebpf_performance.exe - build_job: regular / build + build_job: cmake / build build_artifact: Build-x64 environment: windows-2019 # No code coverage on stress. diff --git a/.github/workflows/reusable-cmake-build.yml b/.github/workflows/reusable-cmake-build.yml index 9bff9203a..79824e41d 100644 --- a/.github/workflows/reusable-cmake-build.yml +++ b/.github/workflows/reusable-cmake-build.yml @@ -38,6 +38,12 @@ jobs: with: submodules: 'recursive' + - name: Install tools + working-directory: ${{env.GITHUB_WORKSPACE}} + run: | + choco install -y llvm --version 11.0.1 --allow-downgrade + echo "C:\Program Files\LLVM\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + - name: Cache nuget packages uses: actions/cache@f63a711791a8e7cc2d5463afc081136e00085800 env: @@ -58,6 +64,28 @@ jobs: run: | cmake --build build --config ${{env.BUILD_CONFIGURATION }} + # Download and extract demo repo artifacts. These then get uploaded along with the build artifacts. + # The subequent tests will directly get these when they download the build artifacts. In case + # a test fails, the .o files used for the test will be available in the uploaded build artifacts. + - name: Download demo repository artifacts + uses: dawidd6/action-download-artifact@b2abf1705491048a2d7074f7d90513044fd25d39 + with: + github_token: ${{secrets.GITHUB_TOKEN}} + workflow: cicd.yml + workflow_conclusion: success + name: x64-${{ matrix.configurations }}-cilium-xdp + path: ${{github.workspace}} + repo: microsoft/ebpf-for-windows-demo + check_artifacts: false + search_artifacts: false + skip_unpack: false + + - name: Extract artifacts to build path + working-directory: ${{env.GITHUB_WORKSPACE}} + run: | + cd ${{github.workspace}}/build/${{env.BUILD_PLATFORM}}/${{env.BUILD_CONFIGURATION}} + tar -xf ..\..\..\x64-${{ matrix.configurations }}-cilium-xdp.zip + - name: Upload Build Output uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index db8f8a4f8..f678636dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,9 @@ cmake_minimum_required(VERSION 3.20) # SDK which corresponds to 19041. set(CMAKE_SYSTEM_VERSION 10.0.19041.0) +# Uncomment the following line for debugging project dependencies. +# set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1) + project("ebpf-for-windows") include("cmake/options.cmake") @@ -15,6 +18,7 @@ include("cmake/settings.cmake") include("cmake/git_commit.cmake") include("cmake/codesign.cmake") include("cmake/midl_compiler_finder.cmake") +include("cmake/process_bpf_source_file.cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/FindWDK/cmake" @@ -23,15 +27,15 @@ list(APPEND CMAKE_MODULE_PATH locateMidlCompiler() find_package(WDK REQUIRED) -add_subdirectory("external") -add_subdirectory("rpc_interface") add_subdirectory("ebpfapi") -add_subdirectory("libs") -add_subdirectory("tools") -add_subdirectory("ebpfsvc") -add_subdirectory("netebpfext") add_subdirectory("ebpfcore") +add_subdirectory("ebpfsvc") +add_subdirectory("external") +add_subdirectory("libs") +add_subdirectory("netebpfext") +add_subdirectory("rpc_interface") add_subdirectory("scripts") +add_subdirectory("tools") if(EBPFFORWINDOWS_ENABLE_TESTS) include("CTest") diff --git a/cmake/process_bpf_source_file.cmake b/cmake/process_bpf_source_file.cmake new file mode 100644 index 000000000..d1894427d --- /dev/null +++ b/cmake/process_bpf_source_file.cmake @@ -0,0 +1,73 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +set (output_dir ${CMAKE_BINARY_DIR}/x64/$<$:Debug>$<$:Release>) + +function(add_bpftonative_command file_name kernel_mode unsafe_program) + find_program(powershell_path "powershell" REQUIRED) + if (${kernel_mode} STREQUAL "$true") + set (output_file ${output_dir}/${file_name}.sys) + else() + set (output_file ${output_dir}/${file_name}_um.dll) + endif() + + set(command_line ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${CMAKE_SOURCE_DIR}/scripts/Convert-BpfToNative.ps1 -ProgramName ${file_name} -SolutionDir ${CMAKE_SOURCE_DIR} -OutputDir ${output_dir} -Platform x64 -Configuration $(Configuration) -KernelMode ${kernel_mode}) + + set (input_file ${output_dir}/${file_name}.o) + if (${unsafe_program}) + set(output_file $<$:${output_file}>$<$:${dummy}>) + string(JOIN " " command_line_string ${command_line}) + set(command_line ${command_line} -SkipVerification $true) + set(input_file $<$:${input_file}>$<$:${dummy}>) + endif() + + add_custom_command( + OUTPUT + ${output_file} + COMMAND + ${command_line} + DEPENDS + ${input_file} + ) +endfunction() + +function(build_bpf_samples unsafe_program) + file(GLOB files *.c) + + get_filename_component(target_name ${CMAKE_CURRENT_SOURCE_DIR} NAME) + + foreach(file ${files}) + get_filename_component(file_name ${file} NAME_WE) + set (sources_list ${sources_list}; ${file}) + set (elf_list ${elf_list};${output_dir}/${file_name}.o) + set (native_driver_list ${native_driver_list}; ${output_dir}/${file_name}.sys; ${output_dir}/${file_name}_um.dll) + if (${unsafe_program}) + set (native_driver_list $<$:${native_driver_list}>$<$:${dummy}>) + endif() + endforeach() + + add_custom_target(${target_name}_elf DEPENDS ${elf_list} SOURCES ${sources_list}) + + add_custom_target(${target_name}_native ALL DEPENDS "${native_driver_list}" SOURCES ${elf_list}) + add_dependencies(${target_name}_native ${target_name}_elf "bpf2c") + + set_target_properties(${target_name}_elf PROPERTIES VS_GLOBAL_ClangFlags "-g -target bpf -O2 -Werror") + set_target_properties(${target_name}_elf PROPERTIES VS_GLOBAL_IncludePath + "-I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/external/bpftool -I${CMAKE_SOURCE_DIR}/tests/xdp -I${CMAKE_SOURCE_DIR}/tests/socket -I${CMAKE_SOURCE_DIR}/tests/sample/ext/inc") + + foreach(file ${files}) + get_filename_component(file_name ${file} NAME_WE) + find_program(clang_path "clang" REQUIRED) + add_custom_command( + OUTPUT + ${output_dir}/${file_name}.o + COMMAND + ${clang_path} "$(ClangFlags)" "$(IncludePath)" -c ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}.c -o ${output_dir}/${file_name}.o + DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}.c + ) + add_bpftonative_command(${file_name} "$true" ${unsafe_program}) + add_bpftonative_command(${file_name} "$false" ${unsafe_program}) + endforeach() + + endfunction() diff --git a/cmake/settings.cmake b/cmake/settings.cmake index de6cc8a67..ad4598999 100644 --- a/cmake/settings.cmake +++ b/cmake/settings.cmake @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation # SPDX-License-Identifier: MIT +set (output_dir PUBLIC ${CMAKE_BINARY_DIR}/x64/$<$:Debug>$<$:Release>) + # Some targets do not play well with the default definitions (such # as bpftool and /DWIN32). Remove them from the variables for now set(settings_variable_list @@ -39,6 +41,10 @@ target_link_libraries("ebpf_for_windows_common_settings" INTERFACE ) +target_link_options("ebpf_for_windows_common_settings" INTERFACE + /DEBUG:Full +) + if(EBPFFORWINDOWS_ENABLE_DISABLE_EBPF_INTERPRETER) target_compile_definitions("ebpf_for_windows_common_settings" INTERFACE CONFIG_BPF_JIT_ALWAYS_ON=1 diff --git a/libs/api/CMakeLists.txt b/libs/api/CMakeLists.txt index 0b7803c71..303606053 100644 --- a/libs/api/CMakeLists.txt +++ b/libs/api/CMakeLists.txt @@ -24,6 +24,7 @@ add_library("api" STATIC ) target_include_directories("api" PRIVATE + "${CMAKE_BINARY_DIR}" "${CMAKE_SOURCE_DIR}/libs/api_common" "${CMAKE_SOURCE_DIR}/libs/api" "${CMAKE_SOURCE_DIR}/rpc_interface" diff --git a/libs/service/CMakeLists.txt b/libs/service/CMakeLists.txt index baf4aaa27..ac292c79f 100644 --- a/libs/service/CMakeLists.txt +++ b/libs/service/CMakeLists.txt @@ -13,6 +13,7 @@ add_library("service" STATIC ) target_include_directories("service" PRIVATE + "${CMAKE_BINARY_DIR}" "${CMAKE_SOURCE_DIR}/rpc_interface" "${CMAKE_SOURCE_DIR}/libs/api_common" "${CMAKE_SOURCE_DIR}/libs/api" diff --git a/rpc_interface/CMakeLists.txt b/rpc_interface/CMakeLists.txt index 972a582c1..a4fb7490a 100644 --- a/rpc_interface/CMakeLists.txt +++ b/rpc_interface/CMakeLists.txt @@ -5,9 +5,9 @@ set(rpc_interface_h_name "rpc_interface_h.h") set(rpc_interface_c_name "rpc_interface_c.c") set(rpc_interface_s_name "rpc_interface_s.c") -set(rpc_interface_h_path "${CMAKE_CURRENT_BINARY_DIR}/${rpc_interface_h_name}") -set(rpc_interface_c_path "${CMAKE_CURRENT_BINARY_DIR}/${rpc_interface_c_name}") -set(rpc_interface_s_path "${CMAKE_CURRENT_BINARY_DIR}/${rpc_interface_s_name}") +set(rpc_interface_h_path "${CMAKE_BINARY_DIR}/${rpc_interface_h_name}") +set(rpc_interface_c_path "${CMAKE_BINARY_DIR}/${rpc_interface_c_name}") +set(rpc_interface_s_path "${CMAKE_BINARY_DIR}/${rpc_interface_s_name}") add_custom_command( OUTPUT diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index aaf4069d7..e558ee140 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation # SPDX-License-Identifier: MIT +add_subdirectory("setup_build") + if(EBPFFORWINDOWS_ENABLE_INSTALL) install( FILES diff --git a/scripts/Convert-BpfToNative.ps1 b/scripts/Convert-BpfToNative.ps1 index a32ec911b..8bde032b1 100644 --- a/scripts/Convert-BpfToNative.ps1 +++ b/scripts/Convert-BpfToNative.ps1 @@ -3,6 +3,7 @@ param([parameter(Mandatory = $true)] [string] $ProgramName, [parameter(Mandatory = $false)] [string] $SolutionDir = $Pwd, + [parameter(Mandatory = $false)] [string] $OutputDir = $Pwd, [parameter(Mandatory = $false)] [string] $Platform = "x64", [ValidateSet("Release", "Debug")][parameter(Mandatory = $false)] [string] $Configuration = "Release", [parameter(Mandatory = $false)] [bool] $SkipVerification = $false, @@ -22,9 +23,9 @@ if ($null -eq (Get-Command 'msbuild.exe' -ErrorAction SilentlyContinue)) { throw "Unable to locate msbuild.exe. This command needs to run within a 'Developer Command Prompt'" } -$fileExists = Test-Path -Path ("$SolutionDir\$Platform\$Configuration\$ProgramName.o") +$fileExists = Test-Path -Path ("$OutputDir\$ProgramName.o") if (!$fileExists) { - $errorString = "Can't find program file: " + "$SolutionDir\$Platform\$Configuration\$ProgramName.o" + $errorString = "Can't find program file: " + "$OutputDir\$ProgramName.o" throw $errorString } @@ -37,8 +38,10 @@ if (!$KernelMode) { $AdditionalOptions = If ($SkipVerification) {"--no-verify"} Else {""} -msbuild /p:SolutionDir="$SolutionDir\" /p:OutDir="$SolutionDir\$Platform\$Configuration\" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile +msbuild /p:SolutionDir="$SolutionDir\" /p:OutDir="$OutputDir" /p:Configuration="$Configuration" /p:Platform="$Platform" /p:ProgramName="$ProgramName" /p:AdditionalOptions="$AdditionalOptions" $ProjectFile if ($LASTEXITCODE -ne 0) { throw "Build failed for $ProgramName.o" -} \ No newline at end of file +} + +Pop-Location \ No newline at end of file diff --git a/scripts/setup_build/CMakeLists.txt b/scripts/setup_build/CMakeLists.txt new file mode 100644 index 000000000..ea1a64592 --- /dev/null +++ b/scripts/setup_build/CMakeLists.txt @@ -0,0 +1,102 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +set (output_dir ${CMAKE_BINARY_DIR}/x64/$<$:Debug>$<$:Release>) + +set (debug_redist_bins ${output_dir}/concrt140d.dll; + ${output_dir}/msvcp140d.dll; + ${output_dir}/msvcp140d_atomic_wait.dll; + ${output_dir}/msvcp140d_codecvt_ids.dll; + ${output_dir}/msvcp140_1d.dll; + ${output_dir}/msvcp140_2d.dll; + ${output_dir}/vccorlib140d.dll; + ${output_dir}/vcruntime140d.dll; + ${output_dir}/vcruntime140_1d.dll; + ) + +set (redist_bins ${output_dir}/concrt140.dll; + ${output_dir}/msvcp140.dll; + ${output_dir}/msvcp140_1.dll; + ${output_dir}/msvcp140_2.dll; + ${output_dir}/msvcp140_atomic_wait.dll; + ${output_dir}/msvcp140_codecvt_ids.dll; + ${output_dir}/vccorlib140.dll; + ${output_dir}/vcruntime140.dll; + ${output_dir}/vcruntime140_1.dll; +) + +set (output_redist_bins $<$:${debug_redist_bins}>$<$:${redist_bins}>) + +set (input_redist_bins $<$:$(VC_DebugCppRuntimeFilesPath_x64)/Microsoft.VC142.DebugCRT>$<$:$(VC_CppRuntimeFilesPath_x64)/Microsoft.VC142.CRT>) + +set (ucrtbased_dll $<$:${output_dir}/ucrtbased.dll>) + +set (output_ucrtbased_dll $<$:${ucrtbased_dll}>) + +set (misc_files "install-ebpf.bat" + "run_tests.bat" + "uninstall-ebpf.bat" + "common.psm1" + "config_test_vm.psm1" + "setup_ebpf_cicd_tests.ps1" + "execute_ebpf_cicd_tests.ps1" + "cleanup_ebpf_cicd_tests.ps1" + "install_ebpf.psm1" + "run_driver_tests.psm1" + "test_execution.json" + "vm_run_tests.psm1" + "vm_list.json" + "ebpfforwindows.wprp" + "ebpf-all.guid" + "ebpf-printk.guid" +) + +foreach(file ${misc_files}) + set( input_misc_files ${input_misc_files} "${CMAKE_SOURCE_DIR}/scripts/${file}") + set( output_misc_files ${output_misc_files} "${output_dir}/${file}") +endforeach() + +set (dependency_list ${CMAKE_SOURCE_DIR}/scripts/pre-commit; + ${CMAKE_SOURCE_DIR}/include/git_commit_id.h; + "${output_redist_bins}" + "${output_ucrtbased_dll}" + "${output_misc_files}") + +add_custom_target(setup_build ALL DEPENDS "${dependency_list}") + +find_program(powershell_path "powershell" REQUIRED) + +add_custom_command( + OUTPUT + ${CMAKE_SOURCE_DIR}/scripts/pre-commit + COMMAND + ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${CMAKE_SOURCE_DIR}/scripts/copy_pre_commit.ps1 ${CMAKE_SOURCE_DIR}/scripts/pre-commit +) + +add_custom_command( + OUTPUT + ${CMAKE_SOURCE_DIR}/include/git_commit_id.h + COMMAND + ${powershell_path} -NonInteractive -ExecutionPolicy Unrestricted ${CMAKE_SOURCE_DIR}/scripts/generate-commitid.ps1 ${CMAKE_SOURCE_DIR}/include +) + +add_custom_command( + OUTPUT + "${output_redist_bins}" + COMMAND + ${CMAKE_COMMAND} -E copy_directory \"${input_redist_bins}\" ${output_dir} +) + +add_custom_command( + OUTPUT + "${output_ucrtbased_dll}" + COMMAND + ${CMAKE_COMMAND} -E copy \"$(UniversalDebugCRT_ExecutablePath_x64)/ucrtbased.dll\" ${output_dir} +) + +add_custom_command ( + OUTPUT + "${output_misc_files}" + COMMAND + ${CMAKE_COMMAND} -E copy ${input_misc_files} ${output_dir} +) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dc1c669bd..1cbf78ae3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,8 +2,13 @@ # SPDX-License-Identifier: MIT add_subdirectory("api_test") -add_subdirectory("libs") add_subdirectory("bpf2c_tests") +add_subdirectory("bpftool_tests") +add_subdirectory("cilium") +add_subdirectory("fuzz") +add_subdirectory("libs") add_subdirectory("performance") +add_subdirectory("sample") +add_subdirectory("socket") add_subdirectory("xdp") add_subdirectory("unit") diff --git a/tests/bpf2c_tests/CMakeLists.txt b/tests/bpf2c_tests/CMakeLists.txt index 9fc25334c..b900c8665 100644 --- a/tests/bpf2c_tests/CMakeLists.txt +++ b/tests/bpf2c_tests/CMakeLists.txt @@ -44,12 +44,6 @@ target_compile_options("bpf2c_tests" PRIVATE "/WX-" ) -configure_file( - bpf_test.cpp - "${CMAKE_CURRENT_BINARY_DIR}/bpf_test.cpp" - COPYONLY -) - add_test( NAME "bpf2c_tests" COMMAND "${CMAKE_COMMAND}" -E env "PATH=$;$ENV{PATH}" "$" @@ -139,10 +133,25 @@ set(expected_file_list test_utility_helpers_sys.c ) -foreach(expected_file ${expected_file_list}) - configure_file( - "expected/${expected_file}" - "${CMAKE_CURRENT_BINARY_DIR}/${expected_file}" - COPYONLY - ) +set (input_bpf_test_cpp ${CMAKE_CURRENT_SOURCE_DIR}/bpf_test.cpp) +set (output_bpf_test_cpp ${output_dir}/bpf_test.cpp) + +foreach(file ${expected_file_list}) + set( input_expected_files ${input_expected_files} ${CMAKE_CURRENT_SOURCE_DIR}/expected/${file}) + set( output_expected_files ${output_expected_files} ${output_dir}/${file}) endforeach() + +add_custom_target(bpf2c_tests_expected_files ALL DEPENDS ${output_bpf_test_cpp} ${output_expected_files} SOURCES ${input_bpf_test_cpp} ${input_expected_files}) + +add_custom_command( + OUTPUT + "${output_expected_files}" + COMMAND + ${CMAKE_COMMAND} -E make_directory "${output_dir}/expected" + COMMAND + ${CMAKE_COMMAND} -E copy ${input_bpf_test_cpp} ${output_bpf_test_cpp} + COMMAND + ${CMAKE_COMMAND} -E copy ${input_expected_files} ${output_dir}/expected + DEPENDS + ${input_bpf_test_cpp} ${input_expected_files} +) diff --git a/tests/bpf2c_tests/elf_bpf.cpp b/tests/bpf2c_tests/elf_bpf.cpp index ea9a11e4c..4fe25a34e 100644 --- a/tests/bpf2c_tests/elf_bpf.cpp +++ b/tests/bpf2c_tests/elf_bpf.cpp @@ -36,6 +36,7 @@ read_contents(const std::string& source, std::vector std::string transform_line_directives(const std::string& string) { @@ -45,12 +46,12 @@ transform_line_directives(const std::string& string) if (string.find("\"") == std::string::npos) { return string; } - if (string.find("\\") == std::string::npos) { + if ((string.find(separator) == std::string::npos)) { // Already trimmed. return string; } - return string.substr(0, string.find("\"") + 1) + string.substr(string.find_last_of("\\") + 1); + return string.substr(0, string.find("\"") + 1) + string.substr(string.find_last_of(separator) + 1); } // Workaround for: https://github.com/microsoft/ebpf-for-windows/issues/1060 @@ -117,8 +118,10 @@ run_test_elf(const std::string& elf_file, _test_mode test_mode) case _test_mode::Verify: case _test_mode::NoVerify: { auto raw_output = read_contents( - std::string("expected\\") + name + suffix, {transform_line_directives, transform_fix_opcode_comment}); - auto raw_result = read_contents(out, {transform_line_directives}); + std::string("expected\\") + name + suffix, + {transform_line_directives<'\\'>, transform_line_directives<'/'>, transform_fix_opcode_comment}); + auto raw_result = read_contents( + out, {transform_line_directives<'\\'>, transform_line_directives<'/'>}); REQUIRE(raw_result.size() == raw_output.size()); for (size_t i = 0; i < raw_result.size(); i++) { diff --git a/tests/bpftool_tests/CMakeLists.txt b/tests/bpftool_tests/CMakeLists.txt new file mode 100644 index 000000000..f876d439a --- /dev/null +++ b/tests/bpftool_tests/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_executable("bpftooltests" + bpftool_tests.cpp +) + +target_include_directories("bpftooltests" PRIVATE + "${CMAKE_SOURCE_DIR}/tests/libs/util" + "${CMAKE_SOURCE_DIR}/external/catch2/src" + "${CMAKE_SOURCE_DIR}/external/catch2/build/generated-includes" +) + +target_link_libraries("bpftooltests" PRIVATE + "ebpf_for_windows_cpp_settings" + "Catch2::Catch2WithMain" + "EbpfApi" + "test_util" +) + +target_compile_definitions("bpftooltests" PRIVATE + _CONSOLE +) + diff --git a/tests/cilium/CMakeLists.txt b/tests/cilium/CMakeLists.txt new file mode 100644 index 000000000..93b3847f0 --- /dev/null +++ b/tests/cilium/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_executable("cilium_tests" + cilium_tests.cpp +) + +target_include_directories("cilium_tests" PRIVATE + "${CMAKE_BINARY_DIR}" + "${CMAKE_SOURCE_DIR}/include" + "${CMAKE_SOURCE_DIR}/tests/libs/common" + "${CMAKE_SOURCE_DIR}/tests/libs/util" + "${CMAKE_SOURCE_DIR}/external/catch2/src" + "${CMAKE_SOURCE_DIR}/external/catch2/build/generated-includes" + "${CMAKE_SOURCE_DIR}/external/bpftool" + "${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src" +) + + +target_link_libraries("cilium_tests" PRIVATE + "ebpf_for_windows_cpp_settings" + "Catch2::Catch2WithMain" + "EbpfApi" + "ws2_32" +) + +target_compile_definitions("cilium_tests" PRIVATE + _CONSOLE + $<$:SKIP_VERIFICATION> +) \ No newline at end of file diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt new file mode 100644 index 000000000..cceec31e3 --- /dev/null +++ b/tests/fuzz/CMakeLists.txt @@ -0,0 +1,63 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_executable("fuzz" + ../../libs/thunk/mock/mock.cpp + ../../libs/thunk/mock/mock.h + + ../end_to_end/test_helper.cpp + ../end_to_end/test_helper.hpp + + execution_context.cpp +) + +target_include_directories("fuzz" PRIVATE + "${CMAKE_BINARY_DIR}" + "${CMAKE_SOURCE_DIR}/external/catch2/src" + "${CMAKE_SOURCE_DIR}/external/catch2/build/generated-includes" + "${CMAKE_SOURCE_DIR}/external/bpftool" + "${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src" + "${CMAKE_SOURCE_DIR}/external/ubpf/vm" + "${CMAKE_SOURCE_DIR}/include" + "${CMAKE_SOURCE_DIR}/libs/api" + "${CMAKE_SOURCE_DIR}/libs/api_common" + "${CMAKE_SOURCE_DIR}/libs/ebpfnetsh" + "${CMAKE_SOURCE_DIR}/libs/execution_context" + "${CMAKE_SOURCE_DIR}/libs/platform" + "${CMAKE_SOURCE_DIR}/libs/platform/user" + "${CMAKE_SOURCE_DIR}/libs/service" + "${CMAKE_SOURCE_DIR}/libs/thunk" + "${CMAKE_SOURCE_DIR}/libs/thunk/mock" + "${CMAKE_SOURCE_DIR}/netebpfext" + "${CMAKE_SOURCE_DIR}/rpc_interface" + "${CMAKE_SOURCE_DIR}/tests/end_to_end" + "${CMAKE_SOURCE_DIR}/tests/libs/common" + "${CMAKE_SOURCE_DIR}/tests/libs/util" + "${CMAKE_SOURCE_DIR}/tests/sample" + "${CMAKE_SOURCE_DIR}/tests/sample/ext/inc" + "${CMAKE_SOURCE_DIR}/tests/xdp" + "${CMAKE_SOURCE_DIR}/tools/encode_program_info" +) + +target_link_libraries("fuzz" PRIVATE + "ebpf_for_windows_cpp_settings" + "api" + "api_common" + "Catch2::Catch2" + "Catch2::Catch2WithMain" + "common_tests" + "EbpfApi" + "execution_context_user" + "external::ebpfverifier" + "mincore.lib" + "pe_parse" + "platform_user" + "service" + "test_util" + "ubpf_user" +) + +target_compile_definitions("fuzz" PRIVATE + _CONSOLE +) + diff --git a/tests/sample/CMakeLists.txt b/tests/sample/CMakeLists.txt new file mode 100644 index 000000000..389a4b635 --- /dev/null +++ b/tests/sample/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_subdirectory("ext") +add_subdirectory("unsafe") + +# build eBPF Samples. +build_bpf_samples(0) \ No newline at end of file diff --git a/tests/sample/ext/CMakeLists.txt b/tests/sample/ext/CMakeLists.txt new file mode 100644 index 000000000..2561ae579 --- /dev/null +++ b/tests/sample/ext/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_subdirectory("app") +add_subdirectory("drv") \ No newline at end of file diff --git a/tests/sample/ext/app/CMakeLists.txt b/tests/sample/ext/app/CMakeLists.txt new file mode 100644 index 000000000..b112ad8b9 --- /dev/null +++ b/tests/sample/ext/app/CMakeLists.txt @@ -0,0 +1,42 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +add_executable("sample_ext_app" + sample_ext_app.cpp +) + +target_include_directories("sample_ext_app" PRIVATE + "${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src" + "${CMAKE_SOURCE_DIR}/include" + "${CMAKE_SOURCE_DIR}/libs/api" + "${CMAKE_SOURCE_DIR}/libs/ebpfnetsh" + "${CMAKE_SOURCE_DIR}/libs/execution_context" + "${CMAKE_SOURCE_DIR}/libs/platform" + "${CMAKE_SOURCE_DIR}/libs/platform/user" + "${CMAKE_SOURCE_DIR}/netebpfext" + "${CMAKE_SOURCE_DIR}/tests/end_to_end" + "${CMAKE_SOURCE_DIR}/tests/libs/util" + "${CMAKE_SOURCE_DIR}/tests/libs/common" + "${CMAKE_SOURCE_DIR}/tests/sample" + "${CMAKE_SOURCE_DIR}/tests/sample/ext/inc" +) + +target_link_libraries("sample_ext_app" PRIVATE + "ebpf_for_windows_cpp_settings" + "common_tests" + "iphlpapi" + "EbpfApi" + "netsh_static" + "test_util" + "Catch2::Catch2WithMain" +) + +target_compile_definitions("sample_ext_app" PRIVATE + _CONSOLE + _WINSOCK_DEPRECATED_NO_WARNINGS +) + +add_test( + NAME "sample_ext_app" + COMMAND "${CMAKE_COMMAND}" -E env "PATH=$;$ENV{PATH}" "$" +) diff --git a/tests/sample/ext/drv/CMakeLists.txt b/tests/sample/ext/drv/CMakeLists.txt new file mode 100644 index 000000000..bf80d7d1e --- /dev/null +++ b/tests/sample/ext/drv/CMakeLists.txt @@ -0,0 +1,57 @@ +# Copyright (c) Microsoft Corporation +# SPDX-License-Identifier: MIT + +wdk_add_driver("sample_ebpf_ext" WINVER "${EBPFFORWINDOWS_WDK_WINVER}" KMDF "${EBPFFORWINDOWS_WDK_KMDF_VERSION}" + sample_ext.h + sample_ext.c + + sample_ext_drv.c +) + +target_include_directories("sample_ebpf_ext" PRIVATE + "${CMAKE_SOURCE_DIR}/include" + "${CMAKE_SOURCE_DIR}/libs/platform" + "${CMAKE_SOURCE_DIR}/libs/platform/kernel" + "${CMAKE_SOURCE_DIR}/tests/sample" + "${CMAKE_SOURCE_DIR}/tests/sample/ext/inc" +) + +target_link_directories("sample_ebpf_ext" PRIVATE + "${WDK_ROOT}/Lib/${WDK_VERSION}/km/x64" +) + +target_link_libraries("sample_ebpf_ext" + "ebpf_for_windows_common_settings" + "external::ebpfverifier_headers" + "platform_kernel" + "netio.lib" + "wdmsec.lib" +) + +target_compile_definitions("sample_ebpf_ext" PRIVATE + BINARY_COMPATIBLE=0 + NT + POOL_NX_OPTIN_AUTO +) + +# api_common includes sample_ebpf_ext's headers. Since this is a driver +# and it does not make sense to link against it, for now create +# an interface target that just exports the include directory. +add_library("sample_ebpf_ext_headers" INTERFACE) +target_include_directories("sample_ebpf_ext_headers" SYSTEM INTERFACE + "${CMAKE_CURRENT_SOURCE_DIR}" +) + +codeSign("sample_ebpf_ext") + +if(EBPFFORWINDOWS_ENABLE_INSTALL) + install( + TARGETS "sample_ebpf_ext" + DESTINATION "drivers" + ) + + install( + FILES "sample_ebpf_ext.inf" + DESTINATION "drivers" + ) +endif() \ No newline at end of file diff --git a/tests/sample/sample.vcxproj b/tests/sample/sample.vcxproj index c011e2b26..3a5d35f88 100644 --- a/tests/sample/sample.vcxproj +++ b/tests/sample/sample.vcxproj @@ -1,4 +1,4 @@ - +