Skip to content

Commit

Permalink
Custom data reloc (#260)
Browse files Browse the repository at this point in the history
* Add support for custom data relocation

Signed-off-by: Alan Jowett <alanjo@microsoft.com>

* Fix downlevel builds

Signed-off-by: Alan Jowett <alanjo@microsoft.com>

* PR feedback

Signed-off-by: Alan Jowett <atjowet@gmail.com>

* Fix build break

Signed-off-by: Alan Jowett <atjowet@gmail.com>

* PR Feedback

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>

---------

Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Signed-off-by: Alan Jowett <atjowet@gmail.com>
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: Alan Jowett <atjowet@gmail.com>
  • Loading branch information
3 people committed May 22, 2023
1 parent e17e8bb commit 2249c83
Show file tree
Hide file tree
Showing 10 changed files with 678 additions and 212 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/posix.yml
@@ -1,3 +1,5 @@
# Copyright (c) 2022-present, IO Visor Project
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022-present, IO Visor Project
# SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -217,6 +219,19 @@ jobs:
${BPF_CONFORMANCE_RUNNER} ${BPF_CONFORMANCE_TEST_DIR} ${BPF_CONFORMANCE_TEST_FILTER} ${BPF_CONFORMANCE_PLUGIN_JIT}
${BPF_CONFORMANCE_RUNNER} ${BPF_CONFORMANCE_TEST_DIR} ${BPF_CONFORMANCE_TEST_FILTER} ${BPF_CONFORMANCE_PLUGIN_INTERPRET}
- name: Run the CTest suite
if: inputs.arch != 'arm64'
run: |
export CCACHE_DIR="$(pwd)/ccache"
if [[ "${{ inputs.scan_build }}" == "true" ]] ; then
command_prefix="scan-build -o scan_build_report"
fi
${command_prefix} cmake \
--build build \
--target test
- name: Generate code coverage report
if: inputs.enable_coverage == true
run: |
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -32,6 +32,7 @@ if(UBPF_ENABLE_TESTS)
if (NOT UBPF_SKIP_EXTERNAL)
add_subdirectory("external")
endif()
add_subdirectory("bpf")
endif()

if(UBPF_ENABLE_PACKAGE)
Expand Down
51 changes: 51 additions & 0 deletions bpf/CMakeLists.txt
@@ -0,0 +1,51 @@
# Copyright (c) Microsoft Corporation
# SPDX-License-Identifier: Apache-2.0

find_program(clang_path "clang" REQUIRED)

execute_process(
COMMAND echo "int main() { return 0;}"
COMMAND ${clang_path} --target=bpf -x c - -c -o /dev/null
ERROR_QUIET OUTPUT_QUIET
RESULT_VARIABLE CLANG_RETURN_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if (CLANG_RETURN_CODE EQUAL 0)
message(STATUS "Clang supports BPF target")
set(CLANG_SUPPORTS_BPF TRUE)
else()
message(WARNING "Clang does not support BPF target, skipping BPF tests")
set(CLANG_SUPPORTS_BPF FALSE)
endif()

function(build_bpf file_name)
message(STATUS "Building BPF ${file_name}")

set(bpf_file_name ${file_name}.bpf.c)
set(bpf_file_path ${CMAKE_CURRENT_SOURCE_DIR}/${bpf_file_name})
set(bpf_obj_file_name ${file_name}.bpf.o)
set(bpf_obj_file_path ${CMAKE_CURRENT_BINARY_DIR}/${bpf_obj_file_name})

if (NOT EXISTS ${bpf_file_path})
message(FATAL_ERROR "BPF file ${bpf_file_path} does not exist")
endif()

add_custom_command(
OUTPUT ${bpf_obj_file_path}
COMMAND ${clang_path} -g -O2 -target bpf -c ${bpf_file_path} -o ${bpf_obj_file_path}
DEPENDS ${bpf_file_path}
COMMENT "Building BPF object ${bpf_obj_file_path}"
)

add_custom_target(${file_name}_ELF ALL DEPENDS ${bpf_obj_file_path} SOURCES ${bpf_file_path})

add_test(NAME ${file_name}_TEST_INTERPRET COMMAND "${CMAKE_BINARY_DIR}/bin/ubpf_test" "${bpf_obj_file_path}")
set_tests_properties(${file_name}_TEST_INTERPRET PROPERTIES PASS_REGULAR_EXPRESSION "0x0")
add_test(NAME ${file_name}_TEST_JIT COMMAND "${CMAKE_BINARY_DIR}/bin/ubpf_test" "${bpf_obj_file_path}")
set_tests_properties(${file_name}_TEST_JIT PROPERTIES PASS_REGULAR_EXPRESSION "0x0")
endfunction()

if (CLANG_SUPPORTS_BPF)
build_bpf(map)
endif()
26 changes: 26 additions & 0 deletions bpf/bpf.h
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <stddef.h>

struct bpf_map;

static void* (*bpf_map_lookup_elem)(void* map, const void* key) = (void* (*)(void* map, const void* key))6;
static int (*bpf_map_update_elem)(void* map, const void* key, const void* value, unsigned long flags) =
(int (*)(void*, const void*, const void*, unsigned long))7;
static int (*bpf_map_delete_elem)(void* map, const void* key) = (int (*)(void*, const void*))8;

#define BPF_MAP_TYPE_ARRAY 2

struct bpf_map_def
{
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
unsigned int map_flags;
unsigned int inner_map_idx;
unsigned int numa_node;
};
41 changes: 41 additions & 0 deletions bpf/map.bpf.c
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: Apache-2.0

#include "bpf.h"

struct bpf_map_def map = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(unsigned int),
.value_size = sizeof(unsigned long),
.max_entries = 10,
};

unsigned long
map_test(void* memory, size_t memory_length)
{
unsigned int key = 5;
unsigned long new_value = 0x1234567890ABCDEF;
unsigned long* value = bpf_map_lookup_elem(&map, &key);
if (value == NULL) {
return 1;
}

if (*value != 0) {
return 2;
}

if (bpf_map_update_elem(&map, &key, &new_value, 0) != 0) {
return 3;
}

value = bpf_map_lookup_elem(&map, &key);
if (value == NULL) {
return 4;
}

if (*value != new_value) {
return 5;
}

return 0;
}

0 comments on commit 2249c83

Please sign in to comment.