Skip to content

Commit

Permalink
add bpf2c support for non-inlined local function calls (#3506)
Browse files Browse the repository at this point in the history
* Skip subprograms when enumerating programs in an object

Fixes #3885

Signed-off-by: Dave Thaler <[email protected]>

* Work in progress

Signed-off-by: Dave Thaler <[email protected]>

* Temporarily use dthaler fork to get verifier fix

Signed-off-by: Dave Thaler <[email protected]>

* Pull some changes in from the calllocal branch

Signed-off-by: Dave Thaler <[email protected]>

* Update bpf2c

Signed-off-by: Dave Thaler <[email protected]>

* Cleanup

Signed-off-by: Dave Thaler <[email protected]>

* Use subprogram names as function names

Signed-off-by: Dave Thaler <[email protected]>

* Output subprograms

Signed-off-by: Dave Thaler <[email protected]>

* Fix output

Signed-off-by: Dave Thaler <[email protected]>

* Fixes for multiple programs per section

Signed-off-by: Dave Thaler <[email protected]>

* Temporarily use dthaler fork of ebpf-verifier

Signed-off-by: Dave Thaler <[email protected]>

* Fix bpf2c_fuzzer compilation

Signed-off-by: Dave Thaler <[email protected]>

* Update expected files for bpf2c_tests

Signed-off-by: Dave Thaler <[email protected]>

* Fix bug hit by test

Signed-off-by: Dave Thaler <[email protected]>

* Fix test

Signed-off-by: Dave Thaler <[email protected]>

* Fix test

Signed-off-by: Dave Thaler <[email protected]>

* Fix test

Signed-off-by: Dave Thaler <[email protected]>

* Fix fuzzer

Signed-off-by: Dave Thaler <[email protected]>

* Add support for calling helper functions from subprograms

Signed-off-by: Dave Thaler <[email protected]>

* Update comment per PR feedback

Signed-off-by: Dave Thaler <[email protected]>

* Add more comments

Signed-off-by: Dave Thaler <[email protected]>

---------

Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler authored Oct 31, 2024
1 parent 2780c0f commit e71e7f7
Show file tree
Hide file tree
Showing 166 changed files with 2,195 additions and 962 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd-release-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
uses: ./.github/workflows/reusable-test.yml
with:
pre_test: Invoke-WebRequest https://github.com/Alan-Jowett/bpf_conformance/releases/download/v0.0.6/bpf_conformance_runner.exe -OutFile bpf_conformance_runner.exe
test_command: .\bpf_conformance_runner.exe --test_file_directory %SOURCE_ROOT%\external\ebpf-verifier\external\bpf_conformance\tests --cpu_version v4 --exclude_regex local --plugin_path bpf2c_plugin.exe --debug true --plugin_options "--include %SOURCE_ROOT%\include"
test_command: .\bpf_conformance_runner.exe --test_file_directory %SOURCE_ROOT%\external\ebpf-verifier\external\bpf_conformance\tests --cpu_version v4 --plugin_path bpf2c_plugin.exe --debug true --plugin_options "--include %SOURCE_ROOT%\include"
name: bpf2c_conformance
build_artifact: Build-x64
environment: windows-2022
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ jobs:
uses: ./.github/workflows/reusable-test.yml
with:
pre_test: Invoke-WebRequest https://github.com/Alan-Jowett/bpf_conformance/releases/download/v0.0.6/bpf_conformance_runner.exe -OutFile bpf_conformance_runner.exe
test_command: .\bpf_conformance_runner.exe --test_file_directory %SOURCE_ROOT%\external\ebpf-verifier\external\bpf_conformance\tests --cpu_version v4 --exclude_regex local --plugin_path bpf2c_plugin.exe --debug true --plugin_options "--include %SOURCE_ROOT%\include"
test_command: .\bpf_conformance_runner.exe --test_file_directory %SOURCE_ROOT%\external\ebpf-verifier\external\bpf_conformance\tests --cpu_version v4 --plugin_path bpf2c_plugin.exe --debug true --plugin_options "--include %SOURCE_ROOT%\include"
name: bpf2c_conformance
build_artifact: Build-x64
environment: windows-2022
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "external/ebpf-verifier"]
path = external/ebpf-verifier
url = https://github.com/vbpf/ebpf-verifier.git
url = https://github.com/dthaler/ebpf-verifier.git
[submodule "external/ubpf"]
path = external/ubpf
url = https://github.com/iovisor/ubpf.git
Expand Down
4 changes: 2 additions & 2 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Alternative install steps (for *basic* Visual Studio Community edition):
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
```

1. Run the following command to automatically setup the dev environment:
1. Run the following command to automatically set up the dev environment:

```ps
Invoke-WebRequest 'https://raw.githubusercontent.com/microsoft/ebpf-for-windows/main/scripts/Setup-DevEnv.ps1' -OutFile $env:TEMP\Setup-DeveEnv.ps1
Expand Down Expand Up @@ -596,4 +596,4 @@ development cycle. See the [Windows Hardware Developer documentation](https://l
Extensions that integrate with eBPF for Windows:
- XDP extension: [xdp-for-windows](https://github.com/microsoft/xdp-for-windows)
- ntosebpfext extension: [ntosebpfext](https://github.com/microsoft/ntosebpfext)
- Network event extension: [neteventebpfext](https://github.com/microsoft/ntosebpfext/blob/main/docs/neteventebpfext.md)
- Network event extension: [neteventebpfext](https://github.com/microsoft/ntosebpfext/blob/main/docs/neteventebpfext.md)
2 changes: 1 addition & 1 deletion docs/isa-support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ opcode src_reg offset imm description
0x7f any 0 0x00 dst >>= src Y Y Y rsh-reg
0x84 0x0 0 0x00 dst = (u32)-dst Y Y Y neg
0x85 0x0 0 any call helper function by static ID Y Y Y call_unwind_fail
0x85 0x1 0 any call PC += imm no no no call_local
0x85 0x1 0 any call PC += imm Y part Y call_local
0x85 0x2 0 any call helper function by BTF ID no no no ???
0x87 0x0 0 0x00 dst = -dst Y Y Y neg64
0x8d 0x0 0 0x00 call helper function by static ID in register Y Y no callx
Expand Down
2 changes: 1 addition & 1 deletion external/ebpf-verifier
Submodule ebpf-verifier updated 57 files
+1 −1 .gitmodules
+79 −66 CMakeLists.txt
+1 −1 ebpf-samples
+1 −1 external/bpf_conformance
+1 −1 external/libbtf
+1 −1 external/radix_tree
+49 −46 src/asm_cfg.cpp
+9 −9 src/asm_files.cpp
+4 −3 src/asm_files.hpp
+62 −73 src/asm_marshal.cpp
+25 −50 src/asm_ostream.cpp
+29 −24 src/asm_ostream.hpp
+36 −88 src/asm_parse.cpp
+0 −2 src/asm_parse.hpp
+7 −22 src/asm_syntax.hpp
+101 −102 src/asm_unmarshal.cpp
+52 −54 src/assertions.cpp
+45 −44 src/crab/array_domain.cpp
+7 −9 src/crab/cfg.hpp
+10 −0 src/crab/dsl_syntax.hpp
+79 −326 src/crab/ebpf_domain.cpp
+2 −52 src/crab/ebpf_domain.hpp
+17 −17 src/crab/fwd_analyzer.cpp
+37 −26 src/crab/interval.hpp
+10 −8 src/crab/linear_constraint.hpp
+42 −49 src/crab/split_dbm.cpp
+2 −2 src/crab/thresholds.cpp
+1 −1 src/crab/thresholds.hpp
+376 −0 src/crab/type_domain.cpp
+79 −0 src/crab/type_domain.hpp
+70 −0 src/crab/type_encoding.hpp
+13 −27 src/crab/var_factory.cpp
+11 −31 src/crab/variable.hpp
+192 −44 src/crab/wto.cpp
+68 −206 src/crab/wto.hpp
+0 −81 src/crab/wto_cycle.hpp
+0 −43 src/crab/wto_nesting.hpp
+95 −178 src/crab_utils/graph_ops.hpp
+28 −40 src/crab_utils/heap.hpp
+9 −26 src/crab_utils/lazy_allocator.hpp
+9 −9 src/crab_utils/num_big.hpp
+13 −0 src/crab_utils/num_extended.hpp
+28 −29 src/crab_utils/num_safeint.hpp
+25 −0 src/crab_utils/num_safety.hpp
+2 −1 src/crab_utils/stats.cpp
+2 −2 src/crab_utils/stats.hpp
+9 −10 src/crab_verifier.cpp
+2 −2 src/crab_verifier.hpp
+8 −2 src/ebpf_vm_isa.hpp
+15 −16 src/ebpf_yaml.cpp
+2 −2 src/main/check.cpp
+2 −18 src/string_constraints.hpp
+53 −52 src/test/test_marshal.cpp
+3 −3 src/test/test_print.cpp
+42 −44 src/test/test_verify.cpp
+55 −0 test-data/calllocal.yaml
+1 −1 test-data/jump.yaml
12 changes: 6 additions & 6 deletions libs/api/api.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(SolutionDir)external\ebpf-verifier\build\_deps\gsl-src\include;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -99,7 +99,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(OutDir);$(OutDir)..\Debug;%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(SolutionDir)external\ebpf-verifier\build\_deps\gsl-src\include;$(OutDir);$(OutDir)..\Debug;%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -113,7 +113,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(SolutionDir)external\ebpf-verifier\build\_deps\gsl-src\include;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -130,7 +130,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(SolutionDir)external\ebpf-verifier\build\_deps\gsl-src\include;$(OutDir);%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -146,7 +146,7 @@
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(OutDir);$(OutDir)..\Release;%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)libs\api_common;$(SolutionDir)libs\api;$(SolutionDir)rpc_interface;$(SolutionDir)libs\service;$(SolutionDir)include;$(SolutionDir)include\bpf;$(SolutionDir)libs\shared;$(SolutionDir)libs\shared\user;$(SolutionDir)external\usersim\cxplat\inc;$(SolutionDir)external\usersim\cxplat\inc\winuser;$(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\bpf_conformance\external\elfio;$(SolutionDir)external\ebpf-verifier\external\libbtf;$(SolutionDir)external\ebpf-verifier\build\_deps\gsl-src\include;$(OutDir);$(OutDir)..\Release;%(AdditionalIncludeDirectories);$(SolutionDir)libs\thunk;$(SolutionDir)external\bpftool;$(SolutionDIr)external\pe-parse\pe-parser-library\include;$(SolutionDir)\external\ubpf\build\vm</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -185,4 +185,4 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
</Project>
6 changes: 6 additions & 0 deletions libs/api/api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,9 @@ ebpf_api_thread_local_cleanup() noexcept;
*/
void
ebpf_api_thread_local_initialize() noexcept;

static inline bool
prog_is_subprog(const struct bpf_object* obj, const struct bpf_program* prog)
{
return (strcmp(prog->section_name, ".text") == 0) && (obj->programs.size() > 1);
}
82 changes: 42 additions & 40 deletions libs/api/ebpf_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ _Guarded_by_(_ebpf_state_mutex) static std::vector<ebpf_object_t*> _ebpf_objects
#define SERVICE_PARAMETERS L"Parameters"
#define NPI_MODULE_ID L"NpiModuleId"

#define NO_EXCEPT_TRY \
noexcept \
try
#define NO_EXCEPT_TRY noexcept try

#define CATCH_NO_MEMORY_FD \
catch (const std::bad_alloc&) { EBPF_RETURN_FD(ebpf_fd_invalid); }
Expand Down Expand Up @@ -3256,10 +3254,13 @@ _Requires_lock_not_held_(_ebpf_state_mutex) static ebpf_result_t
ebpf_result_t result = EBPF_SUCCESS;
std::vector<original_fd_handle_map_t> handle_map;

for (auto& program : object->programs) {
for (ebpf_program_t* program : object->programs) {
if (!program->autoload) {
continue;
}
if (prog_is_subprog(object, program)) {
continue;
}
result = _create_program(
program->program_type, object->object_name, program->section_name, program->program_name, &program->handle);
if (result != EBPF_SUCCESS) {
Expand Down Expand Up @@ -3788,28 +3789,42 @@ _Requires_lock_not_held_(_ebpf_state_mutex) _Ret_maybenull_
}
CATCH_NO_MEMORY_PTR(struct bpf_object*)

_Ret_maybenull_ struct bpf_program*
ebpf_program_next(_In_opt_ const struct bpf_program* previous, _In_ const struct bpf_object* object) NO_EXCEPT_TRY
// This function is derived from libbpf's function of the same name.
static _Ret_maybenull_ struct bpf_program*
__bpf_program__iter(_In_opt_ const struct bpf_program* program, _In_ const struct bpf_object* object, bool forward)
{
EBPF_LOG_ENTRY();
ebpf_program_t* program = nullptr;
ebpf_assert(object);
if (previous != nullptr && previous->object != object) {
goto Exit;
size_t nr_programs = object->programs.size();

if (nr_programs == 0) {
return nullptr;
}
if (previous == nullptr) {
program = (object->programs.size() > 0) ? object->programs[0] : nullptr;
} else {
size_t programs_count = object->programs.size();
for (size_t i = 0; i < programs_count; i++) {
if (object->programs[i] == previous && i < programs_count - 1) {
program = object->programs[i + 1];
break;
}
}
if (program == nullptr) {
// Iterate from the beginning.
return forward ? object->programs[0] : object->programs[nr_programs - 1];
}
if (program->object != object) {
errno = EINVAL;
return nullptr;
}
auto iterator = std::find(object->programs.begin(), object->programs.end(), program);
ptrdiff_t idx = std::distance(object->programs.begin(), iterator) + (forward ? 1 : -1);
if ((size_t)idx >= nr_programs || idx < 0) {
return nullptr;
}
return object->programs[idx];
}

_Ret_maybenull_ struct bpf_program*
ebpf_program_next(_In_opt_ const struct bpf_program* previous, _In_ const struct bpf_object* object) NO_EXCEPT_TRY
{
EBPF_LOG_ENTRY();
const ebpf_program_t* program = previous;

do {
program = __bpf_program__iter(program, object, true);
} while (program && prog_is_subprog(object, program));

Exit:
EBPF_RETURN_POINTER(bpf_program*, program);
}
CATCH_NO_MEMORY_PTR(bpf_program*)
Expand All @@ -3818,24 +3833,12 @@ _Ret_maybenull_ struct bpf_program*
ebpf_program_previous(_In_opt_ const struct bpf_program* next, _In_ const struct bpf_object* object) NO_EXCEPT_TRY
{
EBPF_LOG_ENTRY();
ebpf_program_t* program = nullptr;
ebpf_assert(object);
if (next != nullptr && next->object != object) {
goto Exit;
}
if (next == nullptr) {
program = object->programs[object->programs.size() - 1];
} else {
size_t programs_count = object->programs.size();
for (auto i = programs_count - 1; i > 0; i--) {
if (object->programs[i] == next) {
program = object->programs[i - 1];
break;
}
}
}
const ebpf_program_t* program = next;

do {
program = __bpf_program__iter(program, object, false);
} while (program && prog_is_subprog(object, program));

Exit:
EBPF_RETURN_POINTER(bpf_program*, program);
}
CATCH_NO_MEMORY_PTR(struct bpf_program*)
Expand Down Expand Up @@ -4160,8 +4163,7 @@ typedef struct _ebpf_ring_buffer_subscription
: unsubscribed(false), ring_buffer_map_handle(ebpf_handle_invalid), sample_callback_context(nullptr),
sample_callback(nullptr), buffer(nullptr), reply({}), async_ioctl_completion(nullptr),
async_ioctl_failed(false)
{
}
{}
~_ebpf_ring_buffer_subscription()
{
EBPF_LOG_ENTRY();
Expand Down
Loading

0 comments on commit e71e7f7

Please sign in to comment.