Skip to content

Commit

Permalink
Fix Windows build of ubpf_fuzzer
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan Jowett committed Oct 21, 2024
1 parent f4cd68d commit 717919d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
88 changes: 83 additions & 5 deletions .github/workflows/fuzzing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
workflow_call:

jobs:
build:
posix:
strategy:
matrix:
platform:
Expand Down Expand Up @@ -98,7 +98,6 @@ jobs:
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DUBPF_ENABLE_LIBFUZZER=1 \
-DVERIFIER_ENABLE_TESTS=false \
${arch_flags}
- name: Build uBPF
Expand All @@ -111,7 +110,7 @@ jobs:
- name: Upload fuzzer as artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: fuzzer
name: fuzzer-${{ matrix.platform }}-${{ matrix.arch }}
path: build/bin/ubpf_fuzzer

- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
Expand All @@ -122,7 +121,7 @@ jobs:
- name: Download fuzzer artifacts
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
with:
name: fuzzer
name: fuzzer-${{ matrix.platform }}-${{ matrix.arch }}

- name: Setup directory for fuzzing
run: |
Expand Down Expand Up @@ -152,5 +151,84 @@ jobs:
if: always()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: fuzzing-artifacts
name: fuzzing-artifacts-${{ matrix.platform }}-${{ matrix.arch }}
path: artifacts/

windows:
strategy:
matrix:
platform:
- windows-latest
arch:
- x86_64

runs-on: ${{ matrix.platform }}

steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
submodules: 'recursive'

- name: Configure uBPF
run: |
cmake -S . -B build -DUBPF_ENABLE_LIBFUZZER=1
- name: Build uBPF
run: |
cmake --build build --config RelWithDebInfo
- name: Gather clang-rt files
run: |
$exePath = (Get-Command link.exe).Path
$directory = Split-Path $exePath
Copy-Item -Path "$directory\clang_rt*" -Destination "build/bin/RelWithDebInfo"
- name: Upload fuzzer as artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: fuzzer-${{ matrix.platform }}-${{ matrix.arch }}
path: build/bin/RelWithDebInfo/ubpf_fuzzer.*

- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
submodules: 'recursive'
ref: fuzz/corpus

- name: Download fuzzer artifacts
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
with:
name: fuzzer-${{ matrix.platform }}-${{ matrix.arch }}

- name: Setup directory for fuzzing
run: |
dir
md new_corpus
copy fuzz\corpus\* new_corpus
md artifacts
- name: Run fuzzing
run: |
.\ubpf_fuzzer.exe new_corpus -artifact_prefix=artifacts/ -use_value_profile=1 -max_total_time=300
- name: Merge corpus into fuzz/corpus
if: ${{ github.event_name == 'schedule' }}
run: |
./ubpf_fuzzer -merge=1 fuzz/corpus new_corpus
git add fuzz/corpus
git config --global user.email '[email protected]'
git config --global user.name 'Github Action'
git commit -sm "Update fuzzing corpus"
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{github.repository}}.git
git push
- name: Upload artifacts
if: always()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: fuzzing-artifacts-${{ matrix.platform }}-${{ matrix.arch }}
path: artifacts/
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ jobs:
build_codeql: true
disable_retpolines: true

linux_release_fuzzing:
fuzzing:
uses: ./.github/workflows/fuzzing.yml

# Disabled until https://github.com/iovisor/ubpf/issues/155 is resolved.
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ if(UBPF_ENABLE_PACKAGE)
endif()

if (UBPF_ENABLE_LIBFUZZER)
if (PLATFORM_WINDOWS)
# Set compiler flags for libfuzzer
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /fsanitize=address /fsanitize-coverage=inline-bool-flag /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address /fsanitize-coverage=inline-bool-flag /fsanitize-coverage=edge /fsanitize-coverage=trace-cmp /fsanitize-coverage=trace-div")
endif()
set(VERIFIER_ENABLE_TESTS false)


add_subdirectory("libfuzzer")
add_subdirectory("external/ebpf-verifier")
endif()
13 changes: 11 additions & 2 deletions libfuzzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ if (UBPF_SKIP_EXTERNAL)
return()
endif()

if (PLATFORM_WINDOWS)
set(Boost_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/packages/boost/lib/native/include)
set(Boost_LIBRARY_DIRS ${CMAKE_BINARY_DIR}/packages/boost_filesystem-vc143/lib/native)
endif()

set(UBPF_FUZZER_INCLUDES "${CMAKE_SOURCE_DIR}/vm"
"${CMAKE_BINARY_DIR}/vm"
"${CMAKE_BINARY_DIR}/_deps/gsl-src/include"
Expand All @@ -15,7 +20,8 @@ set(UBPF_FUZZER_INCLUDES "${CMAKE_SOURCE_DIR}/vm"
"${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src"
"${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src/crab"
"${CMAKE_SOURCE_DIR}/external/ebpf-verifier/src/crab_utils"
"${CMAKE_CURRENT_BINARY_DIR}")
"${CMAKE_CURRENT_BINARY_DIR}"
"${Boost_INCLUDE_DIRS}")

set(UBPF_FUZZER_LIBS
ubpf
Expand Down Expand Up @@ -52,5 +58,8 @@ add_executable(

target_include_directories("ubpf_fuzzer" PRIVATE ${UBPF_FUZZER_INCLUDES})

target_link_libraries(ubpf_fuzzer PRIVATE ${UBPF_FUZZER_LIBS})
if (PLATFORM_WINDOWS)
set(CMAKE_EXE_LINKER_FLAGS_FUZZERDEBUG libsancov.lib clang_rt.fuzzer_MDd-x86_64.lib)
endif()

target_link_libraries(ubpf_fuzzer PRIVATE ${UBPF_FUZZER_LIBS})
20 changes: 15 additions & 5 deletions libfuzzer/libfuzz_harness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ extern "C"
#include "test_helpers.h"
#include <cassert>

#if defined(_MSC_VER)
#if defined(DEBUG)
#pragma comment(lib, "clang_rt.fuzzer_MDd-x86_64.lib")
#pragma comment(lib, "libsancov.lib")
#else
#pragma comment(lib, "clang_rt.fuzzer_MD-x86_64.lib")
#pragma comment(lib, "libsancov.lib")
#endif
#endif

/**
* @brief Class to read the options from the environment and provide them to
* the fuzzer.
Expand Down Expand Up @@ -461,13 +471,13 @@ typedef enum class _address_type
address_type_t
ubpf_classify_address(const ubpf_context_t* context, uint64_t register_value)
{
uintptr_t register_value_ptr = reinterpret_cast<uintptr_t>(register_value);
uintptr_t stack_start = reinterpret_cast<uintptr_t>(context->stack_start);
uintptr_t stack_end = reinterpret_cast<uintptr_t>(context->stack_end);
uintptr_t register_value_ptr = static_cast<uintptr_t>(register_value);
uintptr_t stack_start = static_cast<uintptr_t>(context->stack_start);
uintptr_t stack_end = static_cast<uintptr_t>(context->stack_end);
uintptr_t context_start = reinterpret_cast<uintptr_t>(context);
uintptr_t context_end = context_start + sizeof(ubpf_context_t);
uintptr_t packet_start = reinterpret_cast<uintptr_t>(context->data);
uintptr_t packet_end = reinterpret_cast<uintptr_t>(context->data_end);
uintptr_t packet_start = static_cast<uintptr_t>(context->data);
uintptr_t packet_end = static_cast<uintptr_t>(context->data_end);

if (register_value_ptr >= stack_start && register_value_ptr < stack_end) {
return address_type_t::Stack;
Expand Down

0 comments on commit 717919d

Please sign in to comment.