Use CRT library to map handles to fds (#513)

* Use CRT library to map handles to fds

Signed-off-by: Alan Jowett <alanjo@microsoft.com>
This commit is contained in:
Alan Jowett 2021-09-14 09:25:56 -06:00 коммит произвёл GitHub
Родитель e553150b1b
Коммит f5ba5c5d7c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
55 изменённых файлов: 451 добавлений и 437 удалений

Просмотреть файл

@ -64,6 +64,12 @@ a kernel debugger (KD) attached and running, or test signing is enabled. (It is
releases of eBPF for Windows will eventually be production signed at some point in the future after
security hardening is completed.)
Ensure that the matching versions of the Microsoft C Runtime are installed on the machine. Versions of the Microsoft C Runtime are available on the developer machine, located in %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\version, where version is updated with each patch of Visual Studio.
With version 14.29.30133, the full path is:
1) Release - ```%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\vc_redist.x64.exe```
2) Debug - ```%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\debug_nonredist\x64\Microsoft.VC142.DebugCRT```
For basic testing, the simplest way to install eBPF for Windows is into a Windows VM with test signing enabled.
Follow the [VM Installation Instructions](vm-setup.md) to do so.

Просмотреть файл

@ -14,13 +14,14 @@ Self-hosted runners are necessary as GitHub-hosted runners don't have the requis
3) ```Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v2.281.1/actions-runner-win-x64-2.281.1.zip -OutFile actions-runner-win-x64-2.281.1.zip```
4) ```if((Get-FileHash -Path actions-runner-win-x64-2.281.1.zip -Algorithm SHA256).Hash.ToUpper() -ne 'b8dccfef39c5d696443d98edd1ee57881075066bb62adef0a344fcb11bd19f1b'.ToUpper()){ throw 'Computed checksum did not match' }```
5) ```Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD/actions-runner-win-x64-2.281.1.zip", "$PWD")```
5) Obtain an [authentication token](https://github.com/microsoft/ebpf-for-windows/settings/actions/runners/new). This requires administrator permissions in the project.
6) Configure action runner.
6) Obtain an [authentication token](https://github.com/microsoft/ebpf-for-windows/settings/actions/runners/new). This requires administrator permissions in the project.
7) Configure action runner.
1) ```./config.cmd --url https://github.com/microsoft/ebpf-for-windows --token <action runner token>```
8) Change action runner service to run as "LocalSystem".
1) Open services.msc.
2) Locate "GitHub Action Runner ...".
3) Right Click -> Properties -> Log On.
4) Change "Log on as:" to "Local System Account".
8) Install the [Visual C++ Runtime Files](https://docs.microsoft.com/en-us/visualstudio/releases/2019/redistribution#visual-c-runtime-files)
9) Reboot the runner.
9) Install the [Visual C++ Runtime Files](https://docs.microsoft.com/en-us/visualstudio/releases/2019/redistribution#visual-c-runtime-files)
10) Install the debug version of the Visual C++ Runtime files from ```"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\debug_nonredist\x64\Microsoft.VC142.DebugCRT"``` on a machine with Visual Studio installed.
11) Reboot the runner.

Просмотреть файл

@ -16,7 +16,7 @@
4. When the Create Virtual Machine dialog appears, select "Windows 10 dev environment".
5. Click the "Create Virtual Machine" button.
6. Once that is complete click the "Edit Settings" button.
7. Select security, clear the "Enable Scure Boot" checkbox, and click OK. (This is a prerequisite for
7. Select security, clear the "Enable Secure Boot" checkbox, and click OK. (This is a prerequisite for
enabling test signed binaries.)
8. Click "Connect" and start the VM.
@ -26,6 +26,13 @@
2. Do `bcdedit.exe -set TESTSIGNING ON`.
3. Restart the VM so that the change will be applied.
4. Install the matching version of the Microsoft Visual C++ Runtime:
Versions of the Microsoft C Runtime are available on the developer machine, located in %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\version, where version is updated with each patch of Visual Studio.
With version 14.29.30133, the full path is:
1) Release - ```%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\vc_redist.x64.exe```
2) Debug - ```%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\debug_nonredist\x64\Microsoft.VC142.DebugCRT```
## Installing eBPF into a VM
Once the one-time setup has been completed, the following steps will

Просмотреть файл

@ -68,7 +68,6 @@ EXPORTS
ebpf_api_close_handle
ebpf_api_get_pinned_map_info
ebpf_api_map_info_free
ebpf_close_fd
ebpf_create_map
ebpf_create_map_name
ebpf_free_string

Просмотреть файл

@ -134,8 +134,8 @@
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\libs\thunk\windows\platform.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="platform.cpp" />
<ClCompile Include="rpc_client.cpp" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -33,10 +33,10 @@
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="platform.cpp">
<ClCompile Include="rpc_client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="rpc_client.cpp">
<ClCompile Include="..\libs\thunk\windows\platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

Просмотреть файл

@ -1,57 +0,0 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "pch.h"
#include <stdint.h>
#include "ebpf_api.h"
namespace Platform {
bool
DeviceIoControl(
_In_ ebpf_handle_t device_handle,
uint32_t io_control_code,
_In_reads_bytes_opt_(input_buffer_size) void* input_buffer,
uint32_t input_buffer_size,
_Out_writes_bytes_to_opt_(output_buffer_size, *count_of_bytes_returned) void* output_buffer,
uint32_t output_buffer_size,
_Out_opt_ uint32_t* count_of_bytes_returned,
_Inout_opt_ OVERLAPPED* overlapped)
{
return ::DeviceIoControl(
device_handle,
io_control_code,
input_buffer,
input_buffer_size,
output_buffer,
output_buffer_size,
(DWORD*)count_of_bytes_returned,
overlapped);
}
ebpf_handle_t
CreateFileW(
_In_z_ PCWSTR file_name,
uint32_t desired_access,
uint32_t share_mode,
_In_opt_ SECURITY_ATTRIBUTES* security_attributes,
uint32_t creation_disposition,
uint32_t flags_and_attributes,
_In_opt_ ebpf_handle_t template_file)
{
return ::CreateFileW(
file_name,
desired_access,
share_mode,
security_attributes,
creation_disposition,
flags_and_attributes,
template_file);
}
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle)
{
return ::CloseHandle(handle);
}
} // namespace Platform

Просмотреть файл

@ -195,8 +195,8 @@
<ClInclude Include="svc_common.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\libs\thunk\windows\platform.cpp" />
<ClCompile Include="rpc_api.cpp" />
<ClCompile Include="platform.cpp" />
<ClCompile Include="rpc_util.cpp" />
<ClCompile Include="svcmain.cpp" />
</ItemGroup>

Просмотреть файл

@ -39,7 +39,7 @@
<ClCompile Include="rpc_api.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="platform.cpp">
<ClCompile Include="..\libs\thunk\windows\platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

Просмотреть файл

@ -1,57 +0,0 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdint.h>
#include "ebpf_api.h"
#include "svc_common.h"
namespace Platform {
bool
DeviceIoControl(
_In_ ebpf_handle_t device_handle,
uint32_t io_control_code,
_In_reads_bytes_opt_(input_buffer_size) void* input_buffer,
uint32_t input_buffer_size,
_Out_writes_bytes_to_opt_(output_buffer_size, *count_of_bytes_returned) void* output_buffer,
uint32_t output_buffer_size,
_Out_opt_ uint32_t* count_of_bytes_returned,
_Inout_opt_ OVERLAPPED* overlapped)
{
return ::DeviceIoControl(
device_handle,
io_control_code,
input_buffer,
input_buffer_size,
output_buffer,
output_buffer_size,
(DWORD*)count_of_bytes_returned,
overlapped);
}
ebpf_handle_t
CreateFileW(
_In_z_ PCWSTR file_name,
uint32_t desired_access,
uint32_t share_mode,
_In_opt_ SECURITY_ATTRIBUTES* security_attributes,
uint32_t creation_disposition,
uint32_t flags_and_attributes,
_In_opt_ ebpf_handle_t template_file)
{
return ::CreateFileW(
file_name,
desired_access,
share_mode,
security_attributes,
creation_disposition,
flags_and_attributes,
template_file);
}
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle)
{
return ::CloseHandle(handle);
}
} // namespace Platform

Просмотреть файл

@ -22,11 +22,11 @@ ebpf_server_verify_and_load_program(
}
// Set the handle of program being verified in thread-local storage.
set_program_under_verification(info->program_handle);
set_program_under_verification(reinterpret_cast<ebpf_handle_t>(info->program_handle));
result = ebpf_verify_and_load_program(
&info->program_type,
info->program_handle,
reinterpret_cast<ebpf_handle_t>(info->program_handle),
info->execution_context,
info->execution_type,
info->map_count,

Просмотреть файл

@ -64,8 +64,7 @@ extern "C"
typedef int32_t fd_t;
const fd_t ebpf_fd_invalid = -1;
typedef void* ebpf_handle_t;
const ebpf_handle_t ebpf_handle_invalid = (ebpf_handle_t)-1;
typedef intptr_t ebpf_handle_t;
typedef struct _tlv_type_length_value tlv_type_length_value_t;
struct bpf_object;

Просмотреть файл

@ -20,3 +20,6 @@ typedef struct _ebpf_map_info
ebpf_map_definition_in_memory_t definition;
_Field_z_ char* pin_path;
} ebpf_map_info_t;
typedef intptr_t ebpf_handle_t;
extern __declspec(selectany) const ebpf_handle_t ebpf_handle_invalid = (ebpf_handle_t)-1;

Просмотреть файл

@ -134,7 +134,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)external\libbpf\src;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)external\ebpf-verifier\external\elfio;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)external\libbpf\src;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)external\ebpf-verifier\external\elfio;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
@ -156,7 +156,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)external\libbpf\src;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)external\ebpf-verifier\external\elfio;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)external\libbpf\src;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)external\ebpf-verifier\external\elfio;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
@ -181,9 +181,9 @@
<ClCompile Include="windows_platform.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\thunk\platform.h" />
<ClInclude Include="api_internal.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="platform.h" />
<ClInclude Include="rpc_client.h" />
<ClInclude Include="tlv.h" />
<ClInclude Include="Verifier.h" />

Просмотреть файл

@ -42,9 +42,6 @@
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="platform.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="tlv.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -60,5 +57,8 @@
<ClInclude Include="rpc_client.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\thunk\platform.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

Просмотреть файл

@ -19,14 +19,14 @@ extern "C"
}
#include "Verifier.h"
using namespace Platform;
#ifndef GUID_NULL
const GUID GUID_NULL = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
#endif
#define MAX_CODE_SIZE (32 * 1024) // 32 KB
static uint64_t _ebpf_file_descriptor_counter = 0;
static std::map<fd_t, ebpf_handle_t> _fd_to_handle_map;
static std::map<ebpf_handle_t, ebpf_program_t*> _ebpf_programs;
static std::map<ebpf_handle_t, ebpf_map_t*> _ebpf_maps;
static std::vector<ebpf_object_t*> _ebpf_objects;
@ -35,26 +35,15 @@ static void
_clean_up_ebpf_objects();
static fd_t
_get_next_file_descriptor(ebpf_handle_t handle) noexcept
_create_file_descriptor_for_handle(ebpf_handle_t handle) noexcept
{
try {
fd_t fd = static_cast<fd_t>(InterlockedIncrement(&_ebpf_file_descriptor_counter));
_fd_to_handle_map.insert(std::pair<fd_t, ebpf_handle_t>(fd, handle));
return fd;
} catch (...) {
return ebpf_fd_invalid;
}
return Platform::_open_osfhandle(handle, 0);
}
inline static ebpf_handle_t
_get_handle_from_fd(fd_t fd)
_get_handle_from_file_descriptor(fd_t fd)
{
std::map<fd_t, ebpf_handle_t>::iterator it = _fd_to_handle_map.find(fd);
if (it != _fd_to_handle_map.end()) {
return it->second;
}
return ebpf_handle_invalid;
return Platform::_get_osfhandle(fd);
}
inline static ebpf_map_t*
@ -120,7 +109,7 @@ ebpf_api_create_map(
uint32_t value_size,
uint32_t max_entries,
uint32_t map_flags,
_Out_ handle_t* handle)
_Out_ ebpf_handle_t* handle)
{
UNREFERENCED_PARAMETER(map_flags);
@ -128,7 +117,7 @@ ebpf_api_create_map(
EBPF_OFFSET_OF(ebpf_operation_create_map_request_t, data),
ebpf_operation_id_t::EBPF_OPERATION_CREATE_MAP,
{sizeof(ebpf_map_definition_in_memory_t), type, key_size, value_size, max_entries},
(uint64_t)ebpf_handle_invalid};
ebpf_handle_invalid};
_ebpf_operation_create_map_reply reply{};
@ -138,7 +127,7 @@ ebpf_api_create_map(
return_value = ERROR_INVALID_PARAMETER;
goto Exit;
}
*handle = INVALID_HANDLE_VALUE;
*handle = ebpf_handle_invalid;
return_value = invoke_ioctl(request, reply);
@ -147,7 +136,7 @@ ebpf_api_create_map(
ebpf_assert(reply.header.id == ebpf_operation_id_t::EBPF_OPERATION_CREATE_MAP);
*handle = reinterpret_cast<ebpf_handle_t>(reply.handle);
*handle = reply.handle;
Exit:
return windows_error_to_ebpf_result(return_value);
@ -191,7 +180,7 @@ _create_map(
goto Exit;
}
ebpf_assert(reply.header.id == ebpf_operation_id_t::EBPF_OPERATION_CREATE_MAP);
*map_handle = reinterpret_cast<ebpf_handle_t>(reply.handle);
*map_handle = reply.handle;
Exit:
return result;
@ -228,7 +217,7 @@ ebpf_create_map_name(
if (result != EBPF_SUCCESS) {
goto Exit;
}
*map_fd = _get_next_file_descriptor(map_handle);
*map_fd = _create_file_descriptor_for_handle(map_handle);
if (*map_fd == ebpf_fd_invalid) {
result = EBPF_NO_MEMORY;
goto Exit;
@ -281,7 +270,7 @@ _map_lookup_element(
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_MAP_FIND_ELEMENT;
request->handle = reinterpret_cast<uint64_t>(handle);
request->handle = handle;
std::copy(key, key + key_size, request->key);
result = windows_error_to_ebpf_result(invoke_ioctl(request_buffer, reply_buffer));
@ -353,7 +342,7 @@ ebpf_map_lookup_element(fd_t map_fd, _In_ const void* key, _Out_ void* value)
}
*((uint8_t*)value) = 0;
map_handle = _get_handle_from_fd(map_fd);
map_handle = _get_handle_from_file_descriptor(map_fd);
if (map_handle == ebpf_handle_invalid) {
result = EBPF_INVALID_FD;
goto Exit;
@ -454,7 +443,7 @@ ebpf_map_update_element(fd_t map_fd, _In_ const void* key, _In_ const void* valu
return EBPF_INVALID_ARGUMENT;
}
map_handle = _get_handle_from_fd(map_fd);
map_handle = _get_handle_from_file_descriptor(map_fd);
if (map_handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -471,7 +460,7 @@ ebpf_map_update_element(fd_t map_fd, _In_ const void* key, _In_ const void* valu
if ((type == BPF_MAP_TYPE_PROG_ARRAY) || (type == BPF_MAP_TYPE_HASH_OF_MAPS) ||
(type == BPF_MAP_TYPE_ARRAY_OF_MAPS)) {
fd_t fd = *(fd_t*)value;
ebpf_handle_t handle = _get_handle_from_fd(fd);
ebpf_handle_t handle = _get_handle_from_file_descriptor(fd);
if (handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -498,7 +487,7 @@ ebpf_map_delete_element(fd_t map_fd, _In_ const void* key)
goto Exit;
}
map_handle = _get_handle_from_fd(map_fd);
map_handle = _get_handle_from_file_descriptor(map_fd);
if (map_handle == ebpf_handle_invalid) {
result = EBPF_INVALID_FD;
goto Exit;
@ -555,7 +544,7 @@ ebpf_map_get_next_key(fd_t map_fd, _In_opt_ const void* previous_key, _Out_ void
goto Exit;
}
map_handle = _get_handle_from_fd(map_fd);
map_handle = _get_handle_from_file_descriptor(map_fd);
if (map_handle == ebpf_handle_invalid) {
result = EBPF_INVALID_FD;
goto Exit;
@ -577,7 +566,7 @@ ebpf_map_get_next_key(fd_t map_fd, _In_opt_ const void* previous_key, _Out_ void
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_MAP_GET_NEXT_KEY;
request->handle = reinterpret_cast<uint64_t>(map_handle);
request->handle = map_handle;
if (previous_key) {
uint8_t* end = (uint8_t*)previous_key + key_size;
std::copy((uint8_t*)previous_key, end, request->previous_key);
@ -643,7 +632,7 @@ _create_program(
if (error != ERROR_SUCCESS) {
goto Exit;
}
*program_handle = reinterpret_cast<ebpf_handle_t>(reply.program_handle);
*program_handle = reply.program_handle;
Exit:
return windows_error_to_ebpf_result(error);
@ -708,7 +697,7 @@ ebpf_api_load_program(
ebpf_handle_t* map_handles,
const char** error_message)
{
ebpf_handle_t program_handle = INVALID_HANDLE_VALUE;
ebpf_handle_t program_handle = ebpf_handle_invalid;
ebpf_protocol_buffer_t request_buffer;
uint32_t error_message_size = 0;
std::vector<ebpf_handle_t> handles;
@ -744,7 +733,7 @@ ebpf_api_load_program(
if (result != EBPF_SUCCESS) {
goto Done;
}
map->map_fd = _get_next_file_descriptor(map->map_handle);
map->map_fd = _create_file_descriptor_for_handle(map->map_handle);
if (map->map_fd == ebpf_fd_invalid) {
result = EBPF_FAILED;
goto Done;
@ -768,7 +757,7 @@ ebpf_api_load_program(
load_info.section_name = const_cast<char*>(section_name);
load_info.program_name = nullptr;
load_info.program_type = program->program_type;
load_info.program_handle = program_handle;
load_info.program_handle = reinterpret_cast<file_handle_t>(program_handle);
load_info.execution_type = execution_type;
load_info.byte_code = program->byte_code;
load_info.byte_code_size = program->byte_code_size;
@ -793,12 +782,12 @@ ebpf_api_load_program(
// Program is verified and loaded.
*count_of_map_handles = 0;
for (auto& map : maps) {
map_handles[*count_of_map_handles] = reinterpret_cast<HANDLE>(map->map_handle);
map_handles[*count_of_map_handles] = map->map_handle;
(*count_of_map_handles)++;
}
*handle = program_handle;
program_handle = INVALID_HANDLE_VALUE;
program_handle = ebpf_handle_invalid;
} catch (const std::bad_alloc&) {
result = EBPF_NO_MEMORY;
goto Done;
@ -830,7 +819,7 @@ Done:
clean_up_ebpf_map(map);
}
if (program_handle != INVALID_HANDLE_VALUE) {
if (program_handle != ebpf_handle_invalid) {
ebpf_api_close_handle(program_handle);
}
@ -851,7 +840,7 @@ ebpf_api_pin_object(ebpf_handle_t handle, const uint8_t* name, uint32_t name_len
request->header.id = EBPF_OPERATION_UPDATE_PINNING;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->handle = reinterpret_cast<uint64_t>(handle);
request->handle = handle;
std::copy(name, name + name_length, request->name);
return invoke_ioctl(request_buffer);
}
@ -865,11 +854,7 @@ ebpf_object_pin(fd_t fd, _In_z_ const char* path)
return EBPF_INVALID_ARGUMENT;
}
// This is a workaround till we start using _open_osfhandle() to generate
// fds for the handles (issue tracked by TODO: Issue# 287). Once this is
// fixed, _get_osfhandle() can be directly used to fetch the corresponding
// handle.
handle = _get_handle_from_fd(fd);
handle = _get_handle_from_file_descriptor(fd);
if (handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -880,7 +865,7 @@ ebpf_object_pin(fd_t fd, _In_z_ const char* path)
request->header.id = EBPF_OPERATION_UPDATE_PINNING;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->handle = reinterpret_cast<uint64_t>(handle);
request->handle = handle;
std::copy(path, path + path_length, request->name);
result = windows_error_to_ebpf_result(invoke_ioctl(request_buffer));
@ -1004,8 +989,8 @@ ebpf_object_get(_In_z_ const char* path)
return ebpf_fd_invalid;
}
ebpf_handle_t handle = reinterpret_cast<ebpf_handle_t>(reply.handle);
fd_t fd = _get_next_file_descriptor(handle);
ebpf_handle_t handle = reply.handle;
fd_t fd = _create_file_descriptor_for_handle(handle);
if (fd == ebpf_fd_invalid) {
Platform::CloseHandle(handle);
}
@ -1021,17 +1006,17 @@ ebpf_get_next_map(fd_t previous_fd, _Out_ fd_t* next_fd)
fd_t local_fd = previous_fd;
*next_fd = ebpf_fd_invalid;
ebpf_handle_t previous_handle = _get_handle_from_fd(local_fd);
ebpf_handle_t previous_handle = _get_handle_from_file_descriptor(local_fd);
ebpf_operation_get_next_map_request_t request{
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_MAP, reinterpret_cast<uint64_t>(previous_handle)};
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_MAP, previous_handle};
ebpf_operation_get_next_map_reply_t reply;
uint32_t retval = invoke_ioctl(request, reply);
if (retval == ERROR_SUCCESS) {
ebpf_handle_t next_handle = reinterpret_cast<ebpf_handle_t>(reply.next_handle);
ebpf_handle_t next_handle = reply.next_handle;
if (next_handle != ebpf_handle_invalid) {
fd_t fd = _get_next_file_descriptor(next_handle);
fd_t fd = _create_file_descriptor_for_handle(next_handle);
if (fd == ebpf_fd_invalid) {
// Some error getting fd for the handle.
Platform::CloseHandle(next_handle);
@ -1055,19 +1040,17 @@ ebpf_get_next_program(fd_t previous_fd, _Out_ fd_t* next_fd)
fd_t local_fd = previous_fd;
*next_fd = ebpf_fd_invalid;
ebpf_handle_t previous_handle = _get_handle_from_fd(local_fd);
ebpf_handle_t previous_handle = _get_handle_from_file_descriptor(local_fd);
ebpf_operation_get_next_program_request_t request{
sizeof(request),
ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_PROGRAM,
reinterpret_cast<uint64_t>(previous_handle)};
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_GET_NEXT_PROGRAM, previous_handle};
ebpf_operation_get_next_program_reply_t reply;
uint32_t retval = invoke_ioctl(request, reply);
if (retval == ERROR_SUCCESS) {
ebpf_handle_t next_handle = reinterpret_cast<ebpf_handle_t>(reply.next_handle);
ebpf_handle_t next_handle = reply.next_handle;
if (next_handle != ebpf_handle_invalid) {
fd_t fd = _get_next_file_descriptor(next_handle);
fd_t fd = _create_file_descriptor_for_handle(next_handle);
if (fd == ebpf_fd_invalid) {
// Some error getting fd for the handle.
Platform::CloseHandle(next_handle);
@ -1092,7 +1075,7 @@ ebpf_map_query_definition(
_Out_ uint32_t* max_entries,
_Out_ ebpf_id_t* inner_map_id)
{
ebpf_handle_t map_handle = _get_handle_from_fd(fd);
ebpf_handle_t map_handle = _get_handle_from_file_descriptor(fd);
if (map_handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -1107,7 +1090,7 @@ ebpf_program_query_info(
_Outptr_result_z_ const char** section_name)
{
ebpf_result_t result;
ebpf_handle_t handle = _get_handle_from_fd(fd);
ebpf_handle_t handle = _get_handle_from_file_descriptor(fd);
if (handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -1118,7 +1101,7 @@ ebpf_program_query_info(
ebpf_protocol_buffer_t reply_buffer(1024);
ebpf_operation_query_program_info_request_t request{
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_QUERY_PROGRAM_INFO, reinterpret_cast<uint64_t>(handle)};
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_QUERY_PROGRAM_INFO, handle};
auto reply = reinterpret_cast<ebpf_operation_query_program_info_reply_t*>(reply_buffer.data());
@ -1159,7 +1142,7 @@ ebpf_api_link_program(ebpf_handle_t program_handle, ebpf_attach_type_t attach_ty
ebpf_operation_link_program_request_t request = {
EBPF_OFFSET_OF(ebpf_operation_link_program_request_t, data),
EBPF_OPERATION_LINK_PROGRAM,
reinterpret_cast<uint64_t>(program_handle),
program_handle,
attach_type};
ebpf_operation_link_program_reply_t reply;
@ -1172,7 +1155,7 @@ ebpf_api_link_program(ebpf_handle_t program_handle, ebpf_attach_type_t attach_ty
return ERROR_INVALID_PARAMETER;
}
*link_handle = reinterpret_cast<ebpf_handle_t>(reply.link_handle);
*link_handle = reply.link_handle;
return retval;
}
@ -1196,7 +1179,7 @@ _link_ebpf_program(
request = reinterpret_cast<ebpf_operation_link_program_request_t*>(request_buffer.data());
request->header.id = EBPF_OPERATION_LINK_PROGRAM;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->program_handle = reinterpret_cast<uint64_t>(program_handle);
request->program_handle = program_handle;
request->attach_type = *attach_type;
if (attach_parameter_size > 0) {
@ -1213,7 +1196,7 @@ _link_ebpf_program(
goto Exit;
}
*link_handle = reinterpret_cast<ebpf_handle_t>(reply.link_handle);
*link_handle = reply.link_handle;
} catch (const std::bad_alloc&) {
result = EBPF_NO_MEMORY;
goto Exit;
@ -1243,8 +1226,7 @@ _clean_up_ebpf_link(_In_opt_ _Post_invalid_ ebpf_link_t* link)
static ebpf_result_t
_detach_link_by_handle(ebpf_handle_t link_handle)
{
ebpf_operation_unlink_program_request_t request = {
sizeof(request), EBPF_OPERATION_UNLINK_PROGRAM, reinterpret_cast<uint64_t>(link_handle)};
ebpf_operation_unlink_program_request_t request = {sizeof(request), EBPF_OPERATION_UNLINK_PROGRAM, link_handle};
return windows_error_to_ebpf_result(invoke_ioctl(request));
}
@ -1252,7 +1234,7 @@ _detach_link_by_handle(ebpf_handle_t link_handle)
ebpf_result_t
ebpf_detach_link_by_fd(fd_t fd)
{
ebpf_handle_t link_handle = _get_handle_from_fd(fd);
ebpf_handle_t link_handle = _get_handle_from_file_descriptor(fd);
if (link_handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -1301,7 +1283,7 @@ ebpf_program_attach(
if (result != EBPF_SUCCESS) {
goto Exit;
}
new_link->fd = _get_next_file_descriptor(new_link->handle);
new_link->fd = _create_file_descriptor_for_handle(new_link->handle);
if (new_link->fd == ebpf_fd_invalid) {
result = EBPF_NO_MEMORY;
goto Exit;
@ -1326,7 +1308,7 @@ ebpf_program_attach_by_fd(
_In_ size_t attach_params_size,
_Outptr_ struct bpf_link** link)
{
ebpf_program_t* program = _get_ebpf_program_from_handle(_get_handle_from_fd(program_fd));
ebpf_program_t* program = _get_ebpf_program_from_handle(_get_handle_from_file_descriptor(program_fd));
if (program == nullptr || link == nullptr) {
return EBPF_INVALID_ARGUMENT;
}
@ -1338,8 +1320,7 @@ ebpf_program_attach_by_fd(
uint32_t
ebpf_api_unlink_program(ebpf_handle_t link_handle)
{
ebpf_operation_unlink_program_request_t request = {
sizeof(request), EBPF_OPERATION_UNLINK_PROGRAM, reinterpret_cast<uint64_t>(link_handle)};
ebpf_operation_unlink_program_request_t request = {sizeof(request), EBPF_OPERATION_UNLINK_PROGRAM, link_handle};
return invoke_ioctl(request);
}
@ -1367,32 +1348,17 @@ ebpf_link_close(_In_ struct bpf_link* link)
ebpf_result_t
ebpf_api_close_handle(ebpf_handle_t handle)
{
ebpf_operation_close_handle_request_t request = {
sizeof(request), EBPF_OPERATION_CLOSE_HANDLE, reinterpret_cast<uint64_t>(handle)};
ebpf_operation_close_handle_request_t request = {sizeof(request), EBPF_OPERATION_CLOSE_HANDLE, handle};
return windows_error_to_ebpf_result(invoke_ioctl(request));
}
ebpf_result_t
ebpf_close_fd(fd_t fd)
{
ebpf_handle_t handle = _get_handle_from_fd(fd);
if (handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
_fd_to_handle_map.erase(fd);
ebpf_api_close_handle(handle);
return EBPF_SUCCESS;
}
ebpf_result_t
ebpf_api_get_pinned_map_info(
_Out_ uint16_t* map_count, _Outptr_result_buffer_maybenull_(*map_count) ebpf_map_info_t** map_info)
{
ebpf_result_t result = EBPF_SUCCESS;
ebpf_operation_get_map_info_request_t request = {
sizeof(request), EBPF_OPERATION_GET_MAP_INFO, reinterpret_cast<uint64_t>(INVALID_HANDLE_VALUE)};
ebpf_operation_get_map_info_request_t request = {sizeof(request), EBPF_OPERATION_GET_MAP_INFO, ebpf_handle_invalid};
ebpf_protocol_buffer_t reply_buffer;
ebpf_operation_get_map_info_reply_t* reply = nullptr;
size_t min_expected_buffer_length = 0;
@ -1495,12 +1461,11 @@ clean_up_ebpf_program(_In_ _Post_invalid_ ebpf_program_t* program)
if (program == nullptr) {
return;
}
if (program->fd != 0) {
_fd_to_handle_map.erase(program->fd);
if (program->fd != ebpf_fd_invalid) {
Platform::_close(program->fd);
}
if (program->handle != ebpf_handle_invalid) {
_ebpf_programs.erase(program->handle);
ebpf_api_close_handle(program->handle);
}
free(program->byte_code);
free(program->program_name);
@ -1525,11 +1490,10 @@ clean_up_ebpf_map(_In_ _Post_invalid_ ebpf_map_t* map)
return;
}
if (map->map_fd != 0) {
_fd_to_handle_map.erase(map->map_fd);
Platform::_close(map->map_fd);
}
if (map->map_handle != ebpf_handle_invalid) {
_ebpf_maps.erase(map->map_handle);
ebpf_api_close_handle(map->map_handle);
}
free(map->name);
@ -1658,6 +1622,9 @@ _get_next_map_to_create(std::vector<ebpf_map_t*>& maps)
if (map->inner_map == nullptr) {
// This map requires an inner map template, look up which one.
for (auto& inner_map : maps) {
if (!inner_map) {
continue;
}
if (inner_map->original_fd == map->inner_map_original_fd) {
map->inner_map = inner_map;
break;
@ -1745,7 +1712,7 @@ ebpf_program_load(
if (result != EBPF_SUCCESS) {
goto Done;
}
map->map_fd = _get_next_file_descriptor(map->map_handle);
map->map_fd = _create_file_descriptor_for_handle(map->map_handle);
}
for (auto& program : new_object->programs) {
@ -1755,17 +1722,14 @@ ebpf_program_load(
goto Done;
}
// TODO: (Issue #287) _open_osfhandle() fails for the program handle.
// Workaround: for now increment a global counter and use that as
// file descriptor.
program->fd = _get_next_file_descriptor(program->handle);
program->fd = _create_file_descriptor_for_handle(program->handle);
// populate load_info.
load_info.file_name = const_cast<char*>(file_name);
load_info.section_name = const_cast<char*>(program->section_name);
load_info.program_name = const_cast<char*>(program->program_name);
load_info.program_type = program->program_type;
load_info.program_handle = program->handle;
load_info.program_handle = reinterpret_cast<file_handle_t>(program->handle);
load_info.execution_type = execution_type;
load_info.byte_code = program->byte_code;
load_info.byte_code_size = program->byte_code_size;
@ -1959,7 +1923,7 @@ _get_fd_by_id(ebpf_operation_id_t operation, ebpf_id_t id, _Out_ int* fd) noexce
return result;
}
*fd = _get_next_file_descriptor((ebpf_handle_t)reply.handle);
*fd = _create_file_descriptor_for_handle((ebpf_handle_t)reply.handle);
return (*fd == ebpf_fd_invalid) ? EBPF_NO_MEMORY : EBPF_SUCCESS;
}
@ -2023,7 +1987,7 @@ ebpf_object_get_info_by_fd(
return EBPF_INVALID_ARGUMENT;
}
ebpf_handle_t handle = _get_handle_from_fd(bpf_fd);
ebpf_handle_t handle = _get_handle_from_file_descriptor(bpf_fd);
if (handle == ebpf_handle_invalid) {
return EBPF_INVALID_FD;
}
@ -2035,7 +1999,7 @@ ebpf_object_get_info_by_fd(
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_GET_OBJECT_INFO;
request->handle = reinterpret_cast<uint64_t>(handle);
request->handle = handle;
ebpf_result_t result = windows_error_to_ebpf_result(invoke_ioctl(request_buffer, reply_buffer));
if (result == EBPF_SUCCESS) {

Просмотреть файл

@ -44,7 +44,7 @@ parse_maps_section_windows(
}
cache_map_handle(
INVALID_HANDLE_VALUE,
ebpf_handle_invalid,
original_fd,
s.type,
s.key_size,

Просмотреть файл

@ -67,7 +67,7 @@ query_map_definition(
_Out_ ebpf_id_t* inner_map_id) noexcept
{
_ebpf_operation_query_map_definition_request request{
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_QUERY_MAP_DEFINITION, reinterpret_cast<uint64_t>(handle)};
sizeof(request), ebpf_operation_id_t::EBPF_OPERATION_QUERY_MAP_DEFINITION, handle};
_ebpf_operation_query_map_definition_reply reply;

Просмотреть файл

@ -133,7 +133,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)tests\sample\ext\inc;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)tests\sample\ext\inc;$(OutDir);$(SolutionDir)libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
@ -155,7 +155,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)tests\sample\ext\inc;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(SolutionDir)tests\sample\ext\inc;$(OutDir);$(SolutionDir)libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
@ -199,4 +199,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\boost.1.75.0.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\boost.1.75.0.0\build\boost.targets'))" />
</Target>
</Project>
</Project>

Просмотреть файл

@ -17,7 +17,7 @@
#include "platform.h"
#include "platform.hpp"
static ebpf_handle_t _device_handle = INVALID_HANDLE_VALUE;
static ebpf_handle_t _device_handle = ebpf_handle_invalid;
static std::mutex _mutex;
ebpf_result_t
@ -25,20 +25,14 @@ initialize_device_handle()
{
std::scoped_lock lock(_mutex);
if (_device_handle != INVALID_HANDLE_VALUE) {
if (_device_handle != ebpf_handle_invalid) {
return EBPF_ALREADY_INITIALIZED;
}
_device_handle = Platform::CreateFile(
EBPF_DEVICE_WIN32_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr);
EBPF_DEVICE_WIN32_NAME, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (_device_handle == INVALID_HANDLE_VALUE) {
if (_device_handle == ebpf_handle_invalid) {
return windows_error_to_ebpf_result(GetLastError());
}
@ -50,16 +44,16 @@ clean_up_device_handle()
{
std::scoped_lock lock(_mutex);
if (_device_handle != INVALID_HANDLE_VALUE) {
if (_device_handle != ebpf_handle_invalid) {
Platform::CloseHandle(_device_handle);
_device_handle = INVALID_HANDLE_VALUE;
_device_handle = ebpf_handle_invalid;
}
}
ebpf_handle_t
get_device_handle()
{
if (_device_handle == INVALID_HANDLE_VALUE) {
if (_device_handle == ebpf_handle_invalid) {
initialize_device_handle();
}

Просмотреть файл

@ -65,7 +65,7 @@ get_program_info_data(ebpf_program_type_t program_type, _Outptr_ ebpf_program_in
sizeof(request),
ebpf_operation_id_t::EBPF_OPERATION_GET_PROGRAM_INFO,
program_type,
reinterpret_cast<uint64_t>(_program_under_verification)};
_program_under_verification};
*program_info = nullptr;

Просмотреть файл

@ -132,7 +132,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\thunk</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
@ -151,7 +151,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\thunk</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
</ClCompile>
<Link>

Просмотреть файл

@ -11,6 +11,7 @@
#include <vector>
#include "ebpf_api.h"
#include "ebpf_windows.h"
#include "platform.h"
#include "maps.h"
#include "tokens.h"
@ -49,7 +50,7 @@ handle_ebpf_show_maps(
}
if (map_fd != ebpf_fd_invalid) {
ebpf_close_fd(map_fd);
Platform::_close(map_fd);
}
map_fd = next_map_fd;
@ -76,7 +77,7 @@ handle_ebpf_show_maps(
<< map_definition.inner_map_idx << "\n";
}
if (map_fd != ebpf_fd_invalid) {
ebpf_close_fd(map_fd);
Platform::_close(map_fd);
}
return result;
}

Просмотреть файл

@ -7,6 +7,7 @@
#include <windows.h>
#include <netsh.h>
#include "ebpf_windows.h"
#include "platform.h"
#include "programs.h"
#include "tokens.h"
@ -24,11 +25,11 @@ class program_state_t final
ebpf_handle_t map_handles[10];
program_state_t(std::string filename, std::string section)
: program_filename(filename), program_section(section), program_handle(INVALID_HANDLE_VALUE),
link_handle(INVALID_HANDLE_VALUE)
: program_filename(filename), program_section(section), program_handle(ebpf_handle_invalid),
link_handle(ebpf_handle_invalid)
{
for (int i = 0; i < _countof(map_handles); i++) {
map_handles[i] = INVALID_HANDLE_VALUE;
map_handles[i] = ebpf_handle_invalid;
}
}
@ -46,9 +47,9 @@ class program_state_t final
void
close_handle(ebpf_handle_t* handle)
{
if (*handle != INVALID_HANDLE_VALUE) {
if (*handle != ebpf_handle_invalid) {
ebpf_api_close_handle(*handle);
*handle = INVALID_HANDLE_VALUE;
*handle = ebpf_handle_invalid;
}
}
};
@ -232,7 +233,7 @@ _find_program_fd(const char* filename, const char* section)
break;
}
if (program_fd != ebpf_fd_invalid) {
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
}
program_fd = next_program_fd;
@ -260,7 +261,7 @@ _find_program_fd(const char* filename, const char* section)
}
if (program_fd != ebpf_fd_invalid) {
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
}
return ebpf_fd_invalid;
}
@ -405,7 +406,7 @@ handle_ebpf_set_program(
return ERROR_SUPPRESS_OUTPUT;
}
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
}
return ERROR_CALL_NOT_IMPLEMENTED;
@ -510,7 +511,7 @@ handle_ebpf_show_programs(
}
if (program_fd != ebpf_fd_invalid) {
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
}
program_fd = next_program_fd;
@ -538,7 +539,7 @@ handle_ebpf_show_programs(
ebpf_free_string(program_section_name);
}
if (program_fd != ebpf_fd_invalid) {
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
}
return status;
}

Просмотреть файл

@ -11,8 +11,6 @@
#include "ebpf_serialize.h"
#include "ebpf_state.h"
const ebpf_handle_t ebpf_handle_invalid = (ebpf_handle_t)-1;
GUID ebpf_general_helper_function_interface_id = {/* 8d2a1d3f-9ce6-473d-b48e-17aa5c5581fe */
0x8d2a1d3f,
0x9ce6,

Просмотреть файл

@ -886,7 +886,7 @@ ebpf_map_create(
goto Exit;
}
if (inner_map_handle != (ebpf_handle_t)-1) {
if (inner_map_handle != ebpf_handle_invalid) {
// Convert value handle to an object pointer.
result = ebpf_reference_object_by_handle(inner_map_handle, EBPF_OBJECT_MAP, &inner_map_template_object);
if (result != EBPF_SUCCESS)

Просмотреть файл

@ -3,6 +3,7 @@
#pragma once
#include "ebpf_core_structs.h"
#include "bpf_helpers.h"
#include "ebpf_platform.h"
@ -28,7 +29,7 @@ extern "C"
ebpf_map_create(
_In_ const ebpf_utf8_string_t* map_name,
_In_ const ebpf_map_definition_in_memory_t* ebpf_map_definition,
uintptr_t inner_map_handle,
ebpf_handle_t inner_map_handle,
_Outptr_ ebpf_map_t** map);
/**

Просмотреть файл

@ -62,27 +62,27 @@ typedef enum _ebpf_ec_function
typedef struct _ebpf_operation_resolve_helper_request
{
struct _ebpf_operation_header header;
uint64_t program_handle;
ebpf_handle_t program_handle;
uint32_t helper_id[1];
} ebpf_operation_resolve_helper_request_t;
typedef struct _ebpf_operation_resolve_helper_reply
{
struct _ebpf_operation_header header;
uint64_t address[1];
uintptr_t address[1];
} ebpf_operation_resolve_helper_reply_t;
typedef struct _ebpf_operation_resolve_map_request
{
struct _ebpf_operation_header header;
uint64_t program_handle;
uint64_t map_handle[1];
ebpf_handle_t program_handle;
ebpf_handle_t map_handle[1];
} ebpf_operation_resolve_map_request_t;
typedef struct _ebpf_operation_resolve_map_reply
{
struct _ebpf_operation_header header;
uint64_t address[1];
uintptr_t address[1];
} ebpf_operation_resolve_map_reply_t;
typedef struct _ebpf_operation_create_program_request
@ -97,13 +97,13 @@ typedef struct _ebpf_operation_create_program_request
typedef struct _ebpf_operation_create_program_reply
{
struct _ebpf_operation_header header;
uint64_t program_handle;
ebpf_handle_t program_handle;
} ebpf_operation_create_program_reply_t;
typedef struct _ebpf_operation_load_code_request
{
struct _ebpf_operation_header header;
uint64_t program_handle;
ebpf_handle_t program_handle;
ebpf_code_type_t code_type;
uint8_t code[1];
} ebpf_operation_load_code_request_t;
@ -112,20 +112,20 @@ typedef struct _ebpf_operation_create_map_request
{
struct _ebpf_operation_header header;
ebpf_map_definition_in_memory_t ebpf_map_definition;
uint64_t inner_map_handle;
ebpf_handle_t inner_map_handle;
uint8_t data[1];
} ebpf_operation_create_map_request_t;
typedef struct _ebpf_operation_create_map_reply
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_create_map_reply_t;
typedef struct _ebpf_operation_map_find_element_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
uint8_t key[1];
} ebpf_operation_map_find_element_request_t;
@ -138,7 +138,7 @@ typedef struct _ebpf_operation_map_find_element_reply
typedef struct _ebpf_operation_map_update_element_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
ebpf_map_option_t option;
uint8_t data[1]; // data is key+value
} epf_operation_map_update_element_request_t;
@ -155,38 +155,38 @@ typedef struct _ebpf_operation_map_update_element_with_handle_request
typedef struct _ebpf_operation_map_delete_element_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
uint8_t key[1];
} ebpf_operation_map_delete_element_request_t;
typedef struct _ebpf_operation_get_next_map_request
{
struct _ebpf_operation_header header;
uint64_t previous_handle;
ebpf_handle_t previous_handle;
} ebpf_operation_get_next_map_request_t;
typedef struct _ebpf_operation_get_next_map_reply
{
struct _ebpf_operation_header header;
uint64_t next_handle;
ebpf_handle_t next_handle;
} ebpf_operation_get_next_map_reply_t;
typedef struct _ebpf_operation_get_next_program_request
{
struct _ebpf_operation_header header;
uint64_t previous_handle;
ebpf_handle_t previous_handle;
} ebpf_operation_get_next_program_request_t;
typedef struct _ebpf_operation_get_next_program_reply
{
struct _ebpf_operation_header header;
uint64_t next_handle;
ebpf_handle_t next_handle;
} ebpf_operation_get_next_program_reply_t;
typedef struct _ebpf_operation_query_map_definition_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_query_map_definition_request;
typedef struct _ebpf_operation_query_map_definition_reply
@ -204,13 +204,13 @@ typedef struct _ebpf_operation_get_handle_by_id_request
typedef struct _ebpf_operation_get_handle_by_id_reply
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_get_handle_by_id_reply_t;
typedef struct _ebpf_operation_query_program_info_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_query_program_info_request_t;
typedef struct _ebpf_operation_query_program_info_reply
@ -225,21 +225,21 @@ typedef struct _ebpf_operation_query_program_info_reply
typedef struct _ebpf_operation_map_get_next_key_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
uint8_t previous_key[1];
} ebpf_operation_map_get_next_key_request_t;
typedef struct _ebpf_operation_map_get_next_key_reply
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
uint8_t next_key[1];
} ebpf_operation_map_get_next_key_reply_t;
typedef struct _ebpf_operation_update_map_pinning_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
uint8_t name[1];
} ebpf_operation_update_pinning_request_t;
@ -252,13 +252,13 @@ typedef struct _ebpf_operation_get_pinning_request
typedef struct _ebpf_operation_get_pinning_reply
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_get_map_pinning_reply_t;
typedef struct _ebpf_operation_link_program_request
{
struct _ebpf_operation_header header;
uint64_t program_handle;
ebpf_handle_t program_handle;
ebpf_attach_type_t attach_type;
uint8_t data[1];
} ebpf_operation_link_program_request_t;
@ -266,19 +266,19 @@ typedef struct _ebpf_operation_link_program_request
typedef struct _ebpf_operation_link_program_reply
{
struct _ebpf_operation_header header;
uint64_t link_handle;
ebpf_handle_t link_handle;
} ebpf_operation_link_program_reply_t;
typedef struct _ebpf_operation_unlink_program_request
{
struct _ebpf_operation_header header;
uint64_t link_handle;
ebpf_handle_t link_handle;
} ebpf_operation_unlink_program_request_t;
typedef struct _ebpf_operation_close_handle_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_close_handle_request_t;
typedef struct _ebpf_operation_get_ec_function_request
@ -290,14 +290,14 @@ typedef struct _ebpf_operation_get_ec_function_request
typedef struct _ebpf_operation_get_ec_function_reply
{
struct _ebpf_operation_header header;
uint64_t address;
uintptr_t address;
} ebpf_operation_get_ec_function_reply_t;
typedef struct _ebpf_operation_get_program_info_request
{
struct _ebpf_operation_header header;
ebpf_program_type_t program_type;
uint64_t program_handle;
ebpf_handle_t program_handle;
} ebpf_operation_get_program_info_request_t;
typedef struct _ebpf_operation_get_program_info_reply
@ -311,7 +311,7 @@ typedef struct _ebpf_operation_get_program_info_reply
typedef struct _ebpf_operation_get_map_info_request
{
struct _ebpf_operation_header header;
uint64_t handle;
ebpf_handle_t handle;
} ebpf_operation_get_map_info_request_t;
typedef struct _ebpf_operation_get_map_info_reply

Просмотреть файл

@ -3,6 +3,7 @@
#pragma once
#include "ebpf_core_structs.h"
#include "ebpf_object.h"
#include "ebpf_platform.h"
@ -10,9 +11,6 @@
extern "C"
{
#endif
typedef uint64_t ebpf_handle_t;
/**
* @brief Initialize the global handle table.
*

Просмотреть файл

@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT
#include "ebpf_handle.h"
#include "framework.h"
extern DEVICE_OBJECT*
ebpf_driver_get_device_object();

Просмотреть файл

@ -13,3 +13,9 @@ typedef UINT8 uint8_t;
typedef UINT16 uint16_t;
typedef UINT32 uint32_t;
typedef UINT64 uint64_t;
#ifdef _WIN64
typedef __int64 intptr_t;
#else
typedef _W64 int intptr_t;
#endif

Просмотреть файл

@ -60,7 +60,7 @@ _build_helper_id_to_address_map(
auto reply = reinterpret_cast<ebpf_operation_resolve_helper_reply_t*>(reply_buffer.data());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_RESOLVE_HELPER;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->program_handle = reinterpret_cast<uint64_t>(program_handle);
request->program_handle = program_handle;
// Build list of helper_ids to resolve and assign new helper id.
// New helper ids are in the range [0,63]
@ -165,7 +165,7 @@ _resolve_maps_in_byte_code(ebpf_handle_t program_handle, ebpf_code_buffer_t& byt
auto reply = reinterpret_cast<ebpf_operation_resolve_map_reply_t*>(reply_buffer.data());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_RESOLVE_MAP;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->program_handle = reinterpret_cast<uint64_t>(program_handle);
request->program_handle = program_handle;
for (size_t index = 0; index < map_handles.size(); index++) {
if (map_handles[index] > get_map_descriptor_size()) {
@ -208,7 +208,7 @@ _query_and_cache_map_descriptors(
descriptor = {0};
ebpf_id_t inner_map_id;
result = query_map_definition(
handle_map[i].handle,
reinterpret_cast<ebpf_handle_t>(handle_map[i].handle),
&size,
&descriptor.type,
&descriptor.key_size,
@ -226,7 +226,7 @@ _query_and_cache_map_descriptors(
descriptor.value_size,
descriptor.max_entries,
handle_map[i].inner_map_original_fd,
handle_map[i].handle,
reinterpret_cast<ebpf_handle_t>(handle_map[i].handle),
0);
}
}
@ -398,7 +398,7 @@ ebpf_verify_and_load_program(
request = reinterpret_cast<ebpf_operation_load_code_request_t*>(request_buffer.data());
request->header.id = ebpf_operation_id_t::EBPF_OPERATION_LOAD_CODE;
request->header.length = static_cast<uint16_t>(request_buffer.size());
request->program_handle = reinterpret_cast<uint64_t>(program_handle);
request->program_handle = program_handle;
request->code_type = execution_type == EBPF_EXECUTION_JIT ? EBPF_CODE_NATIVE : EBPF_CODE_EBPF;
std::copy(

Просмотреть файл

@ -132,7 +132,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)rpc_interface;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)rpc_interface;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(OutDir);$(SolutionDir)\libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
@ -154,7 +154,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)rpc_interface;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)rpc_interface;$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)include;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)external\ebpf-verifier\external;$(OutDir);$(SolutionDir)\libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
@ -198,4 +198,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\boost.1.75.0.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\boost.1.75.0.0\build\boost.targets'))" />
</Target>
</Project>
</Project>

Просмотреть файл

@ -6,9 +6,12 @@
#include "mock.h"
#include "rpc_interface_h.h"
std::function<decltype(_close)> close_handler;
std::function<decltype(CloseHandle)> close_handle_handler;
std::function<decltype(CreateFileW)> create_file_handler;
std::function<decltype(DeviceIoControl)> device_io_control_handler;
std::function<decltype(CloseHandle)> close_handle_handler;
std::function<decltype(_get_osfhandle)> get_osfhandle_handler;
std::function<decltype(_open_osfhandle)> open_osfhandle_handler;
namespace Platform {
bool
@ -23,7 +26,7 @@ DeviceIoControl(
_Inout_opt_ OVERLAPPED* overlapped)
{
return device_io_control_handler(
device_handle,
reinterpret_cast<HANDLE>(device_handle),
(DWORD)io_control_code,
input_buffer,
(DWORD)input_buffer_size,
@ -43,20 +46,38 @@ CreateFileW(
uint32_t flags_and_attributes,
_In_opt_ ebpf_handle_t template_file)
{
return create_file_handler(
return reinterpret_cast<ebpf_handle_t>(create_file_handler(
file_name,
desired_access,
share_mode,
security_attributes,
creation_disposition,
flags_and_attributes,
template_file);
reinterpret_cast<HANDLE>(template_file)));
}
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle)
{
return close_handle_handler(handle);
return close_handle_handler(reinterpret_cast<HANDLE>(handle));
}
int
_open_osfhandle(intptr_t os_file_handle, int flags)
{
return open_osfhandle_handler(os_file_handle, flags);
}
intptr_t
_get_osfhandle(int file_descriptor)
{
return get_osfhandle_handler(file_descriptor);
}
int
_close(int file_handle)
{
return close_handler(file_handle);
}
} // namespace Platform
@ -73,12 +94,12 @@ ebpf_result_t
ebpf_rpc_load_program(ebpf_program_load_info* info, const char** logs, uint32_t* logs_size)
{
// Set the handle of program being verified in thread-local storage.
set_program_under_verification(info->program_handle);
set_program_under_verification(reinterpret_cast<ebpf_handle_t>(info->program_handle));
// Short circuit rpc call to service lib.
ebpf_result_t result = ebpf_verify_and_load_program(
&info->program_type,
info->program_handle,
reinterpret_cast<ebpf_handle_t>(info->program_handle),
info->execution_context,
info->execution_type,
info->map_count,

Просмотреть файл

@ -4,7 +4,11 @@
#pragma once
#include <Windows.h>
#include <functional>
#include <io.h>
extern std::function<decltype(_close)> close_handler;
extern std::function<decltype(CloseHandle)> close_handle_handler;
extern std::function<decltype(CreateFileW)> create_file_handler;
extern std::function<decltype(DeviceIoControl)> device_io_control_handler;
extern std::function<decltype(CloseHandle)> close_handle_handler;
extern std::function<decltype(_get_osfhandle)> get_osfhandle_handler;
extern std::function<decltype(_open_osfhandle)> open_osfhandle_handler;

Просмотреть файл

@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#include "ebpf_core_structs.h"
#pragma once
namespace Platform {
bool
@ -26,4 +28,14 @@ CreateFileW(
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle);
int
_open_osfhandle(intptr_t os_file_handle, int flags);
intptr_t
_get_osfhandle(int file_descriptor);
int
_close(int file_descriptor);
} // namespace Platform

Просмотреть файл

@ -0,0 +1,105 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <Windows.h>
#include <io.h>
#include <stdint.h>
#include <cstdlib>
#include <crtdbg.h> // For _CrtSetReportMode
#include "ebpf_api.h"
class _invalid_parameter_suppression
{
public:
_invalid_parameter_suppression()
{
_CrtSetReportMode(_CRT_ASSERT, 0);
previous_handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
}
~_invalid_parameter_suppression() { _set_invalid_parameter_handler(previous_handler); }
private:
static void
_ignore_invalid_parameter(
const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
{
UNREFERENCED_PARAMETER(expression);
UNREFERENCED_PARAMETER(function);
UNREFERENCED_PARAMETER(file);
UNREFERENCED_PARAMETER(line);
UNREFERENCED_PARAMETER(pReserved);
}
_invalid_parameter_handler previous_handler;
};
namespace Platform {
bool
DeviceIoControl(
_In_ ebpf_handle_t device_handle,
uint32_t io_control_code,
_In_reads_bytes_opt_(input_buffer_size) void* input_buffer,
uint32_t input_buffer_size,
_Out_writes_bytes_to_opt_(output_buffer_size, *count_of_bytes_returned) void* output_buffer,
uint32_t output_buffer_size,
_Out_opt_ uint32_t* count_of_bytes_returned,
_Inout_opt_ OVERLAPPED* overlapped)
{
return ::DeviceIoControl(
reinterpret_cast<HANDLE>(device_handle),
io_control_code,
input_buffer,
input_buffer_size,
output_buffer,
output_buffer_size,
(DWORD*)count_of_bytes_returned,
overlapped);
}
ebpf_handle_t
CreateFileW(
_In_z_ PCWSTR file_name,
uint32_t desired_access,
uint32_t share_mode,
_In_opt_ SECURITY_ATTRIBUTES* security_attributes,
uint32_t creation_disposition,
uint32_t flags_and_attributes,
_In_opt_ ebpf_handle_t template_file)
{
return reinterpret_cast<ebpf_handle_t>(::CreateFileW(
file_name,
desired_access,
share_mode,
security_attributes,
creation_disposition,
flags_and_attributes,
reinterpret_cast<HANDLE>(template_file)));
}
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle)
{
return ::CloseHandle(reinterpret_cast<HANDLE>(handle));
}
int
_open_osfhandle(intptr_t os_file_handle, int flags)
{
_invalid_parameter_suppression suppress;
return ::_open_osfhandle(os_file_handle, flags);
}
intptr_t
_get_osfhandle(int file_descriptor)
{
_invalid_parameter_suppression suppress;
return ::_get_osfhandle(file_descriptor);
}
int
_close(int file_descriptor)
{
_invalid_parameter_suppression suppress;
return ::_close(file_descriptor);
}
} // namespace Platform

Просмотреть файл

@ -9,6 +9,7 @@
#include "api_test.h"
#include "catch_wrapper.hpp"
#include "common_tests.h"
#include <io.h>
#include "service_helper.h"
#include "libbpf.h"
#define SAMPLE_PATH ""
@ -101,7 +102,7 @@ _test_program_load(
REQUIRE(ebpf_get_next_program(previous_fd, &next_fd) == EBPF_SUCCESS);
REQUIRE(next_fd == ebpf_fd_invalid);
ebpf_close_fd(previous_fd);
_close(previous_fd);
previous_fd = ebpf_fd_invalid;
bpf_object__close(object);

Просмотреть файл

@ -125,7 +125,7 @@
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
@ -142,7 +142,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)libs\thunk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<TreatWarningAsError>true</TreatWarningAsError>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
@ -161,9 +161,6 @@
<ProjectReference Include="..\..\ebpfapi\ebpfapi.vcxproj">
<Project>{75fe223a-3e45-4b0e-a2e8-04285e52e440}</Project>
</ProjectReference>
<ProjectReference Include="..\..\libs\service\service.vcxproj">
<Project>{af85c549-57cc-40a5-bdfc-dcf1998de80f}</Project>
</ProjectReference>
<ProjectReference Include="..\..\rpc_interface\rpc_interface.vcxproj">
<Project>{1423245d-0249-40fc-a077-ff7780acfe3f}</Project>
</ProjectReference>

Просмотреть файл

@ -160,8 +160,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\ebpfapi\rpc_client.cpp" />
<ClCompile Include="..\..\libs\thunk\windows\platform.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="platform.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\ebpf-verifier\build\ebpfverifier.vcxproj">

Просмотреть файл

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: MIT
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
@ -15,15 +19,15 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\ebpfapi\rpc_client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\thunk\windows\platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Просмотреть файл

@ -1,57 +0,0 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdint.h>
#include "ebpf_api.h"
#include "framework.h"
namespace Platform {
bool
DeviceIoControl(
_In_ ebpf_handle_t device_handle,
uint32_t io_control_code,
_In_reads_bytes_opt_(input_buffer_size) void* input_buffer,
uint32_t input_buffer_size,
_Out_writes_bytes_to_opt_(output_buffer_size, *count_of_bytes_returned) void* output_buffer,
uint32_t output_buffer_size,
_Out_opt_ uint32_t* count_of_bytes_returned,
_Inout_opt_ OVERLAPPED* overlapped)
{
return ::DeviceIoControl(
device_handle,
io_control_code,
input_buffer,
input_buffer_size,
output_buffer,
output_buffer_size,
(DWORD*)count_of_bytes_returned,
overlapped);
}
ebpf_handle_t
CreateFileW(
_In_z_ PCWSTR file_name,
uint32_t desired_access,
uint32_t share_mode,
_In_opt_ SECURITY_ATTRIBUTES* security_attributes,
uint32_t creation_disposition,
uint32_t flags_and_attributes,
_In_opt_ ebpf_handle_t template_file)
{
return ::CreateFileW(
file_name,
desired_access,
share_mode,
security_attributes,
creation_disposition,
flags_and_attributes,
template_file);
}
bool
CloseHandle(_In_ _Post_ptr_invalid_ ebpf_handle_t handle)
{
return ::CloseHandle(handle);
}
} // namespace Platform

Просмотреть файл

@ -19,6 +19,7 @@
#include "helpers.h"
#include "libbpf.h"
#include "mock.h"
#include "platform.h"
#include "program_helper.h"
#include "sample_test_common.h"
#include "test_helper.hpp"
@ -728,7 +729,7 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]")
REQUIRE(ebpf_get_next_program(program_fd, &next_program_fd) == EBPF_SUCCESS);
REQUIRE(next_program_fd != ebpf_fd_invalid);
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
program_fd = next_program_fd;
REQUIRE(ebpf_program_query_info(program_fd, &type, &file_name, &section_name) == EBPF_SUCCESS);
REQUIRE(type == EBPF_EXECUTION_INTERPRET);
@ -741,7 +742,7 @@ TEST_CASE("enumerate_and_query_programs", "[end_to_end]")
REQUIRE(ebpf_get_next_program(program_fd, &next_program_fd) == EBPF_SUCCESS);
REQUIRE(next_program_fd == ebpf_fd_invalid);
ebpf_close_fd(program_fd);
Platform::_close(program_fd);
for (int i = 0; i < _countof(object); i++) {
bpf_object__close(object[i]);

Просмотреть файл

@ -73,7 +73,7 @@ typedef class _single_instance_hook : public _hook_helper
public:
_single_instance_hook(ebpf_program_type_t program_type, ebpf_attach_type_t attach_type)
: _hook_helper{attach_type}, provider(nullptr), client_binding_context(nullptr), client_data(nullptr),
client_dispatch_table(nullptr), link_handle(nullptr)
client_dispatch_table(nullptr), link_handle(ebpf_handle_invalid)
{
ebpf_guid_create(&client_id);
attach_provider_data.supported_program_type = program_type;

Просмотреть файл

@ -12,6 +12,7 @@
#include "libbpf.h"
#pragma warning(pop)
#include "maps.h"
#include "platform.h"
#include "programs.h"
#include "test_helper.hpp"
@ -279,6 +280,6 @@ TEST_CASE("show maps", "[netsh][maps]")
" Hash of maps 4 4 2 0\n"
" Array 4 4 1 0\n");
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
ebpf_close_fd(outer_map_fd); // TODO(issue #287): change to _close(outer_map_fd);
Platform::_close(inner_map_fd);
Platform::_close(outer_map_fd);
}

Просмотреть файл

@ -1,5 +1,7 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#include <map>
#include "catch_wrapper.hpp"
#include "ebpf_api.h"
#include "ebpf_core.h"
@ -7,7 +9,10 @@
#include "mock.h"
#include "test_helper.hpp"
ebpf_handle_t
static uint64_t _ebpf_file_descriptor_counter = 0;
static std::map<fd_t, ebpf_handle_t> _fd_to_handle_map;
HANDLE
GlueCreateFileW(
PCWSTR lpFileName,
DWORD dwDesiredAccess,
@ -15,7 +20,7 @@ GlueCreateFileW(
PSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
ebpf_handle_t hTemplateFile)
HANDLE hTemplateFile)
{
UNREFERENCED_PARAMETER(lpFileName);
UNREFERENCED_PARAMETER(dwDesiredAccess);
@ -25,11 +30,11 @@ GlueCreateFileW(
UNREFERENCED_PARAMETER(dwFlagsAndAttributes);
UNREFERENCED_PARAMETER(hTemplateFile);
return (ebpf_handle_t)0x12345678;
return (HANDLE)0x12345678;
}
BOOL
GlueCloseHandle(ebpf_handle_t hObject)
GlueCloseHandle(HANDLE hObject)
{
UNREFERENCED_PARAMETER(hObject);
return TRUE;
@ -37,7 +42,7 @@ GlueCloseHandle(ebpf_handle_t hObject)
BOOL
GlueDeviceIoControl(
ebpf_handle_t hDevice,
HANDLE hDevice,
DWORD dwIoControlCode,
PVOID lpInBuffer,
DWORD nInBufferSize,
@ -121,11 +126,63 @@ Fail:
return FALSE;
}
int
Glue_open_osfhandle(intptr_t os_file_handle, int flags)
{
UNREFERENCED_PARAMETER(flags);
try {
fd_t fd = static_cast<fd_t>(InterlockedIncrement(&_ebpf_file_descriptor_counter));
_fd_to_handle_map.insert(std::pair<fd_t, ebpf_handle_t>(fd, os_file_handle));
return fd;
} catch (...) {
return ebpf_fd_invalid;
}
}
intptr_t
Glue_get_osfhandle(int file_descriptor)
{
if (file_descriptor == ebpf_fd_invalid) {
errno = EINVAL;
return ebpf_handle_invalid;
}
std::map<fd_t, ebpf_handle_t>::iterator it = _fd_to_handle_map.find(file_descriptor);
if (it != _fd_to_handle_map.end()) {
return it->second;
}
errno = EINVAL;
return ebpf_handle_invalid;
}
int
Glue_close(int file_descriptor)
{
if (file_descriptor == ebpf_fd_invalid) {
errno = EINVAL;
return ebpf_handle_invalid;
}
std::map<fd_t, ebpf_handle_t>::iterator it = _fd_to_handle_map.find(file_descriptor);
if (it == _fd_to_handle_map.end()) {
errno = EINVAL;
return -1;
} else {
ebpf_api_close_handle(it->second);
_fd_to_handle_map.erase(file_descriptor);
return 0;
}
}
_test_helper_end_to_end::_test_helper_end_to_end()
{
device_io_control_handler = GlueDeviceIoControl;
create_file_handler = GlueCreateFileW;
close_handle_handler = GlueCloseHandle;
open_osfhandle_handler = Glue_open_osfhandle;
get_osfhandle_handler = Glue_get_osfhandle;
close_handler = Glue_close;
REQUIRE(ebpf_core_initiate() == EBPF_SUCCESS);
ec_initialized = true;
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);

Просмотреть файл

@ -275,9 +275,7 @@ typedef class _ebpf_map_test_state
ebpf_map_definition_in_memory_t definition{
sizeof(ebpf_map_definition_in_memory_t), type, sizeof(uint32_t), sizeof(uint64_t), ebpf_get_cpu_count()};
REQUIRE(
ebpf_map_create(&name, &definition, reinterpret_cast<uintptr_t>(ebpf_handle_invalid), &map) ==
EBPF_SUCCESS);
REQUIRE(ebpf_map_create(&name, &definition, ebpf_handle_invalid, &map) == EBPF_SUCCESS);
for (uint32_t i = 0; i < ebpf_get_cpu_count(); i++) {
uint64_t value = 0;

Просмотреть файл

@ -125,7 +125,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)\netebpfext;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)libs\thunk\mock;$(SolutionDir)\netebpfext;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
@ -144,7 +144,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample\ext\inc;;$(SolutionDir)\netebpfext;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)external\ubpf\vm;$(SolutionDir)external\ubpf\vm\inc;$(SolutionDir)libs\thunk\mock;$(SolutionDir)\netebpfext;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -157,7 +157,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\end_to_end\mock.cpp" />
<ClCompile Include="..\..\libs\thunk\mock\mock.cpp" />
<ClCompile Include="..\end_to_end\test_helper.cpp" />
<ClCompile Include="performance.cpp" />
</ItemGroup>

Просмотреть файл

@ -25,7 +25,7 @@
<ClCompile Include="..\end_to_end\test_helper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\end_to_end\mock.cpp">
<ClCompile Include="..\..\libs\thunk\mock\mock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>

Просмотреть файл

@ -10,6 +10,7 @@
#pragma warning(disable : 4200)
#include "libbpf.h"
#pragma warning(pop)
#include "platform.h"
#include "program_helper.h"
#include "test_helper.hpp"
@ -370,7 +371,7 @@ _ebpf_test_tail_call(_In_z_ const char* filename, int expected_result)
// a valid program ID.
int callee_fd2 = bpf_prog_get_fd_by_id(callee_id);
REQUIRE(callee_fd2 > 0);
ebpf_close_fd(callee_fd2); // TODO(issue #287): change to _close(callee_fd2);
Platform::_close(callee_fd2);
bpf_link* link = bpf_program__attach_xdp(caller, 1);
REQUIRE(link != nullptr);
@ -482,7 +483,7 @@ TEST_CASE("disallow prog_array mixed program type values", "[libbpf]")
REQUIRE(error < 0);
REQUIRE(errno == EINVAL);
ebpf_close_fd(map_fd); // TODO(issue #287): change to _close(map_fd);
Platform::_close(map_fd);
bpf_object__close(bind_object);
bpf_object__close(xdp_object);
}
@ -508,13 +509,13 @@ TEST_CASE("enumerate program IDs", "[libbpf]")
REQUIRE(bpf_prog_get_next_id(0, &id1) == 0);
fd_t fd1 = bpf_prog_get_fd_by_id(id1);
REQUIRE(fd1 >= 0);
ebpf_close_fd(fd1); // TODO(issue #287): change to _close(fd1);
Platform::_close(fd1);
uint32_t id2;
REQUIRE(bpf_prog_get_next_id(id1, &id2) == 0);
fd_t fd2 = bpf_prog_get_fd_by_id(id2);
REQUIRE(fd2 >= 0);
ebpf_close_fd(fd2); // TODO(issue #287): change to _close(fd2);
Platform::_close(fd2);
uint32_t id3;
REQUIRE(bpf_prog_get_next_id(id2, &id3) < 0);
@ -547,7 +548,7 @@ TEST_CASE("simple hash of maps", "[libbpf]")
// a valid map ID.
int inner_map_fd2 = bpf_map_get_fd_by_id(inner_map_id);
REQUIRE(inner_map_fd2 > 0);
ebpf_close_fd(inner_map_fd2); // TODO(issue #287): change to _close(inner_map_fd2);
Platform::_close(inner_map_fd2);
// Verify we can't insert an integer into the outer map.
__u32 bad_value = 12345678;
@ -566,8 +567,8 @@ TEST_CASE("simple hash of maps", "[libbpf]")
error = bpf_map_delete_elem(outer_map_fd, &outer_key);
REQUIRE(error == 0);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
ebpf_close_fd(outer_map_fd); // TODO(issue #287): change to _close(outer_map_fd);
Platform::_close(inner_map_fd);
Platform::_close(outer_map_fd);
}
// Verify an app can communicate with an eBPF program via an array of maps.
@ -619,7 +620,7 @@ TEST_CASE("array of maps", "[libbpf]")
// Verify the return value is what we saved in the inner map.
REQUIRE(result == inner_value);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
Platform::_close(inner_map_fd);
bpf_object__close(xdp_object);
}
@ -650,7 +651,7 @@ TEST_CASE("disallow wrong inner map types", "[libbpf]")
REQUIRE(error < 0);
REQUIRE(errno == EINVAL);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
Platform::_close(inner_map_fd);
// Try an inner map with wrong key_size.
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(__u64), sizeof(__u32), 1, 0);
@ -658,7 +659,7 @@ TEST_CASE("disallow wrong inner map types", "[libbpf]")
error = bpf_map_update_elem(outer_map_fd, &outer_key, &inner_map_fd, 0);
REQUIRE(error < 0);
REQUIRE(errno == EINVAL);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
Platform::_close(inner_map_fd);
// Try an inner map of the wrong value size.
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(__u32), sizeof(__u64), 1, 0);
@ -666,7 +667,7 @@ TEST_CASE("disallow wrong inner map types", "[libbpf]")
error = bpf_map_update_elem(outer_map_fd, &outer_key, &inner_map_fd, 0);
REQUIRE(error < 0);
REQUIRE(errno == EINVAL);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
Platform::_close(inner_map_fd);
// Try an inner map with wrong max_entries.
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(__u32), sizeof(__u32), 2, 0);
@ -674,7 +675,7 @@ TEST_CASE("disallow wrong inner map types", "[libbpf]")
error = bpf_map_update_elem(outer_map_fd, &outer_key, &inner_map_fd, 0);
REQUIRE(error < 0);
REQUIRE(errno == EINVAL);
ebpf_close_fd(inner_map_fd); // TODO(issue #287): change to _close(inner_map_fd);
Platform::_close(inner_map_fd);
bpf_object__close(xdp_object);
}
@ -699,22 +700,22 @@ TEST_CASE("enumerate map IDs", "[libbpf]")
REQUIRE(bpf_map_get_next_id(0, &id1) == 0);
fd_t fd1 = bpf_map_get_fd_by_id(id1);
REQUIRE(fd1 >= 0);
ebpf_close_fd(fd1); // TODO(issue #287): change to _close(fd1);
Platform::_close(fd1);
uint32_t id2;
REQUIRE(bpf_map_get_next_id(id1, &id2) == 0);
fd_t fd2 = bpf_map_get_fd_by_id(id2);
REQUIRE(fd2 >= 0);
ebpf_close_fd(fd2); // TODO(issue #287): change to _close(fd2);
Platform::_close(fd2);
uint32_t id3;
REQUIRE(bpf_map_get_next_id(id2, &id3) < 0);
REQUIRE(errno == ENOENT);
ebpf_close_fd(map1_fd); // TODO(issue #287): change to _close(map1_fd);
ebpf_close_fd(map2_fd); // TODO(issue #287): change to _close(map2_fd);
ebpf_close_fd(fd1); // TODO(issue #287): change to _close(fd1);
ebpf_close_fd(fd2); // TODO(issue #287): change to _close(fd2);
Platform::_close(map1_fd);
Platform::_close(map2_fd);
Platform::_close(fd1);
Platform::_close(fd2);
}
TEST_CASE("enumerate link IDs", "[libbpf]")
@ -740,13 +741,13 @@ TEST_CASE("enumerate link IDs", "[libbpf]")
REQUIRE(bpf_link_get_next_id(0, &id1) == 0);
fd_t fd1 = bpf_link_get_fd_by_id(id1);
REQUIRE(fd1 >= 0);
ebpf_close_fd(fd1); // TODO(issue #287): change to _close(fd1);
Platform::_close(fd1);
uint32_t id2;
REQUIRE(bpf_link_get_next_id(id1, &id2) == 0);
fd_t fd2 = bpf_link_get_fd_by_id(id2);
REQUIRE(fd2 >= 0);
ebpf_close_fd(fd2); // TODO(issue #287): change to _close(fd2);
Platform::_close(fd2);
uint32_t id3;
REQUIRE(bpf_link_get_next_id(id2, &id3) < 0);
@ -818,5 +819,5 @@ TEST_CASE("bpf_obj_get_info_by_fd", "[libbpf]")
// This is the flow used by bpftool to detach a link.
REQUIRE(bpf_link_detach(link_fd) == 0);
ebpf_close_fd(link_fd); // TODO(issue #287): change to _close(link_fd);
Platform::_close(link_fd);
}

Просмотреть файл

@ -126,7 +126,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)\netebpfext;$(SolutionDir)\tests\xdp;$(SolutionDir)tools\encode_program_info;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)\tests\xdp;$(SolutionDir)tools\encode_program_info;$(SolutionDir)libs\thunk;$(SolutionDir)libs\thunk\mock;$(SolutionDir)\netebpfext;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
@ -146,7 +146,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)\netebpfext;$(SolutionDir)\tests\xdp;$(SolutionDir)tools\encode_program_info;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)include;$(SolutionDir)libs\api;$(SolutionDir)libs\ebpfnetsh;$(SolutionDir)tests\libs\util;$(SolutionDir)tests\libs\common;$(OutDir);$(SolutionDir)external\ebpf-verifier\src;$(SolutionDir)libs\service;$(SolutionDir)rpc_interface;$(SolutionDir)libs\platform;$(SolutionDir)libs\platform\user;$(SolutionDir)libs\execution_context;$(SolutionDir)tests\end_to_end;$(SolutionDir)tests\sample;$(SolutionDir)tests\sample\ext\inc;$(SolutionDir)\tests\xdp;$(SolutionDir)tools\encode_program_info;$(SolutionDir)libs\thunk;$(SolutionDir)libs\thunk\mock;$(SolutionDir)\netebpfext;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
@ -202,16 +202,16 @@
<ItemGroup>
<ClCompile Include="..\..\libs\execution_context\unit\execution_context_unit_test.cpp" />
<ClCompile Include="..\..\libs\platform\unit\platform_unit_test.cpp" />
<ClCompile Include="..\..\libs\thunk\mock\mock.cpp" />
<ClCompile Include="..\end_to_end\capture_helper.cpp" />
<ClCompile Include="..\end_to_end\end_to_end.cpp" />
<ClCompile Include="..\end_to_end\mock.cpp" />
<ClCompile Include="..\end_to_end\netsh_test.cpp" />
<ClCompile Include="..\end_to_end\test_helper.cpp" />
<ClCompile Include="libbpf_test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\libs\thunk\mock\mock.h" />
<ClInclude Include="..\end_to_end\helpers.h" />
<ClInclude Include="..\end_to_end\mock.h" />
<ClInclude Include="..\end_to_end\test_helper.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

Просмотреть файл

@ -28,9 +28,6 @@
<ClCompile Include="..\..\libs\execution_context\unit\execution_context_unit_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\end_to_end\mock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\platform\unit\platform_unit_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -46,15 +43,18 @@
<ClCompile Include="..\end_to_end\test_helper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\thunk\mock\mock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\end_to_end\helpers.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\end_to_end\mock.h">
<ClInclude Include="..\end_to_end\test_helper.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\end_to_end\test_helper.hpp">
<ClInclude Include="..\..\libs\thunk\mock\mock.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

Просмотреть файл

@ -187,4 +187,4 @@ $(OutputPath)encode_program_info.exe</Command>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

Просмотреть файл

@ -112,6 +112,7 @@
<ClInclude Include="tokens.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\thunk\windows\platform.cpp" />
<ClCompile Include="dllmain.c" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -36,6 +36,9 @@
<ClCompile Include="dllmain.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\thunk\windows\platform.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="ebpfnetsh.rc">