* Fix code analysis annotations for _In_ and _In_opt_ parameters,
add const where appropriate or change to _Inout_ where appropriate.
The following regex expressions were used to find the problems:
Find _In_ or _In_opt_ that are not const and are not followed by _Post_invalid_ or _Frees_ptr_ or _Post_ptr_invalid_:
```_In_[ ]+(?!.*(const|_Post_invalid_|_Frees_ptr_|_Post_ptr_invalid_))```
```_In_opt_[ ]+(?!.*(const|_Post_invalid_|_Frees_ptr_|_Post_ptr_invalid_))```
Some of the _In_ and _In_opt_ parameters are not const due to required compatibility with the Windows API.
Pointers to functions are const by definition, so the const qualifier is not required.
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix code analysis failure
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* kernel and platform changes
* fix
* user mode changes, other fixes
* fix test failure
* change function name
* add tests
* fix failing test
* cr comments
* CR comments
* fix cmake build
* fix
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* fix assert
* cr comments
* fix analysis failure
* fix test to avoid double close of fd
* test case fixes
* test fix
* fix unit test, analysis failure
* fix tests
* fix memory leak, possible race condition
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>
* Annotate execution context API's with _Must_inspect_result_
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix rebase
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Code analysis failure
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: Alan Jowett <alan.jowett@microsoft.com>
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* Annotate platform API's with _Must_inspect_result_
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix code analysis failure
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix up rebase
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix rebase failure
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: Alan Jowett <alan.jowett@microsoft.com>
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* Use netebpfext_user in fuzz testing
Address the helper function part of #1325
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix winsock inclusion issues
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix nmr implementation
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Update WDK path for more projects
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Disable ubpf warnings
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Cleanup
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix unit tests
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Make build install execution context fuzzer corpus
Filed #1505 to improve this
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix libfuzz harness
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix MBL use by core helper fuzzer
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix cmake build
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix user-mode NMR locking issue
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Extra debug statements
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* More fixes
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Cleanup
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix bad merge
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Co-authored-by: saxena-anurag <43585259+saxena-anurag@users.noreply.github.com>
* Add map ids test
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Enumerate maps associated with a program
Fixes#1339
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Initialize info before calling
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Update bpftool
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Make "netsh show prog l=v" show map ids for program
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Return EFAULT if map_ids is not a valid pointer
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix warning
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* More test fixes
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix test
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix socket test
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Add more checks in the socket test
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix socket test
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix netsh
Also fix GettingStarted.md and a missing space in the logs
that were found while testing this fix.
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Set map_ids on output
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Add SAL
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Test helper's _net_ebpf_xdp_adjust_head shouldn't permit unbounded memory allocations
This bug only affected the tests, not the actual runtime.
Also fix some annotations found while debugging this
Fixes#1218
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Increase bound on instruction count
And align more with other platforms
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix compiler error
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Adding negative tests for ebpf_core.c
Fixed bugs in ebpf_core.c around handling bad data.
Fixed incorrect helper function numbering for bpf_ktime_get_ns.
Canonacalizes names of protocol structs and handlers.
Simply protocol dispatch table and remove redundancy.
Add the ability to unregister for async notification (for failure path).
Remove redundant checks (internal calls with SAL annotations).
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
Co-authored-by: Alan Jowett <alan.jowett@microsoft.com>
Co-authored-by: saxena-anurag <43585259+saxena-anurag@users.noreply.github.com>
* initial_commit
* fix build
* fix build
* fix build break due to merge
* debug build
* api changes, other changes
* bpf2c change to enmit program type, other fixes
* hydrate UM ebpf_object, other fixes
* remove logic to disable programs
* fixes
* fix sal
* build break
* build break
* fix sal errors
* fixes
* fix bpf2c_tests failure
* unload driver when program ref count becomes 0, other minor fixes
* fixes
* tail_call fixes, add test cases, other fixes
* build break
* build break
* code cleanup
* fix bad merge
* code cleanup
* code cleanup
* cleanup
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* cr comments
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* cr comments
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* cr comments
* cr comments
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* address cr comments
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* add tracing in ebpfcore, other fixes
* tracing
* add section for each map in sample
* do not delete the native service
* fix bad merge
* remove code to delete service, other fixes
* cr comments
* bpf2c should read and populate all the maps in ELF file
* add test case for creating map-in-map from native driver
* Apply suggestions from code review
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* cr comments
* add tracing for api code changes
* rename epbf_native_t to ebpf_native_module_t
* fix bad merge
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* Add test to trigger division by zero handler
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Fix bugcheck in interpreter on division by zero
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Add option EBPF_JIT_ALWAYS_ON to permit building EC with no interpreter
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Add documentation on using compile time options
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Set EBPF_JIT_ALWAYS_ON for release builds
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Tests should expect interpret to fail if EBPF_JIT_ALWAYS_ON is defined
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Rename EBPF_JIT_ALWAYS_ON to CONFIG_BPF_JIT_ALWAYS_ON
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Tests should expect interpret to fail if EBPF_JIT_ALWAYS_ON is defined
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Update docs/GettingStarted.md
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* Load returns EBPF_PROGRAM_LOAD_FAILED
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Split up load into individual test cases
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Program load fails with EBPF_PROGRAM_LOAD_FAILED
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* Skip tests that depend on interpret mode when it's disabled
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
* PR feedback
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* Add libbpf bpf_prog_bind_map() API
Fixes#379
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix SAL annotation
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Fix leak
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Address feedback
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Make program lock protect the set of maps associated with the program
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>
Co-authored-by: saxena-anurag <43585259+saxena-anurag@users.noreply.github.com>
* Improve netsh show programs functionality
* Rename ebpf_get_next_pinned_program_name to ebpf_get_next_pinned_program_path for clarity
* Show link count and pinned path count
* Support "level=verbose" format
* Support filtering by attached and/or pinned
Fixes#188
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Add libpf's libbpf_prog_type_by_name() API
And add an ebpf_get_program_type_by_name() that returns the GUIDs
instead of ints.
This also removes the hard-coding of GUIDs or ints from the netsh
helper.
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Add support for libbpf bpf_obj_get_info_by_fd API
This is the last libbpf api needed to enable the bpftool
flow to detach an already loaded program.
The count of maps is changed from size_t to uint32_t for
consistency with libbpf and because it's not actually a size
in the sizeof() sense, it's a count in the countof() sense.
Also fix bug where map_name was never being set in the kernel
Fixes#372
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Add object IDs
Add support for the following libbpf APIs:
* bpf_{link,map,prog}_get_fd_by_id
* bpf_{link,map,prog}_get_next_id
Addresses the main part of #396.
A subsequent PR will handle the rest of 396 which includes:
* remove "extra_value" complexity from maps
The changes in api_common.hpp and libbpf_internal.h are from PR 482 and so will go
away in a rebase once that PR is merged.
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Address PR feedback
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Address PR feedback
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Address PR feedback
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Libbpf has bpf_helpers.h which is mostly platform-agnostic, and
bpf_helper_defs.h which is platform-specific but is included
by bpf_helpers.h. Until libbpf is made more platform-agnostic
(issue #351), the workaround is to have a separate pair of files.
Our bpf_helpers.h and our own bpf_helper_defs.h, both of which
would ideally be merged into libbpf's in the future.
Platform-specific defines are in ebpf_struct.h, though that
name may need to change later on. Linux uses "linux/bpf.h"
(e.g., as used in the https://docs.cilium.io/en/v1.8/bpf/ and
https://developers.redhat.com/blog/2021/04/01/get-started-with-xdp
articles) or "vmlinux.h" (e.g., as used in the
https://ruderich.org/simon/notes/xdp-minimal-example article),
and these filenames are hard coded in eBPF programs. In the future,
we should probably settle on a cross-platform name and use include
paths to distinguish them, as opposed to requiring ifdefs in eBPF
programs. However, all of that is part of issue 351 and not this
issue.
Also removed obsolete/unused "repro.c" from tests/samples
Fixes#426
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>
* Add map-in-map support
Replace UM ebpf_map_update/delete_element with libbpf-compliant
bpf_map_update/delete_elem
This adds the basic functionality needed for #375
Not in this PR, but in a subsequent PR:
* ensure that all inner maps match the one specified by inner_map_idx,
much like prog_types have to match in a prog_array.
* ensure that putting a prog_array in an array of maps adheres to the
prog_array contract that any associated progs have to match the
type of the calling program.
* read a map id not fd when UM reads the value (will be done together
with issue #396 since also affects prog_arrays)
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Prevent mismatched program types in PROG_ARRAY maps
* Each prog array map has a natural progtype, determined when
asociating it from a program, or when adding the first program
to it, if not associated with any program.
* Trying to add a program with mismatching type will fail
* Added libbpf bpf_create_map() API
* Fixed error returns from several libbpf APIs to be negative
* For efficiency, ebpf_program_get_properties now returns a
pointer rather than copying the data inside the execution
context, and is renamed to ebpf_program_get_parameters()
to match what its return type always was.
* Fixed a bug in map size calculation that resulted in a huge
amount of memory being allocated
* Updated return type of bpf_tail_call to the value meant
to signal stack unwind needed
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Test Extension Part 2
* rename test extension to sample extension and update Getting-started doc.
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
* add UNLINK operation
* Some fixes:
1. Added attach_lock to synchronize multiple detach calls on same link object.
2. ebpf_extension_unload() should be called from ebpf_link_detach_program()
3. Changed return type of ebpf_program_get_properties to void.
* Update libs/execution_context/ebpf_program.c
Co-authored-by: Dave Thaler <dthaler@microsoft.com>
Previously some places had "info" and some had "information".
Both appear in dictionaries, so guidance to avoid abbreviations does not apply.
Fixes#314
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
* Make a couple of `_In_` arguments be const
* Add `_opt_` to a number of arguments that can be NULL
* Add SAL annotation to a few more APIs that were missing it
* Remove annotations like
`_Pre_readable_byte_size_(hash_table->key_size)` since they just give
code analysis warnings such as:
```
c:\git\dthaler\ebpf-for-windows\libs\platform\ebpf_platform.h(445):
warning C28230: The type of '_Param_(1)' has no member 'key_size'.
c:\git\dthaler\ebpf-for-windows\libs\platform\ebpf_platform.h(445):
warning C28285: For function 'ebpf_hash_table_delete' '_Param_(2)'
syntax error in
'SAL_readableTo(byteCount(__formal(0,hash_table)->key_size))' near
'key_size))'.
```
Signed-off-by: Dave Thaler <dthaler@ntdev.microsoft.com>