auth: add unit tests for xdp connection reset and fix uninitialized prev_key#1750
auth: add unit tests for xdp connection reset and fix uninitialized prev_key#1750Aryanburnwal05 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request improves the reliability and maintainability of the XDP authorization module by introducing targeted unit tests and fixing a build-time initialization issue. The changes ensure robust handling of connection reset logic and facilitate easier local development and testing workflows. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. The map was waiting for a key, / A pointer set to null, you see. / With tests now in the testing suite, / The code is stable and complete. Footnotes
|
|
Welcome @Aryanburnwal05! It looks like this is your first PR to kmesh-net/kmesh 🎉 |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves robustness around BPF map restoration and adds unit tests to validate XDP auth notification behavior when writing to an eBPF map.
Changes:
- Add Go unit tests for
xdpNotifyConnRstcovering nil-map handling and IPv4/IPv6 key mutation behavior. - Initialize
prev_keytoNULLinmap_restoreto avoid undefined behavior on firstbpf_map_get_next_keycall.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/auth/xdp_auth_handler_test.go | Adds tests verifying xdpNotifyConnRst inserts expected values and mutates IPv4 keys correctly. |
| bpf/deserialization_to_bpf_map/deserialization_to_bpf_map.c | Fixes potential UB by initializing prev_key before use in map iteration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| err := rlimit.RemoveMemlock() | ||
| assert.NoError(t, err) |
| mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{ | ||
| Name: "test_map_of_auth_result", | ||
| Type: ebpf.Hash, | ||
| KeySize: uint32(TUPLE_LEN), | ||
| ValueSize: uint32(unsafe.Sizeof(uint32(0))), | ||
| MaxEntries: 4096, | ||
| }) |
| key := make([]byte, TUPLE_LEN) | ||
| err := xdpNotifyConnRst(nil, constants.MSG_TYPE_IPV4, key) | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "map_of_auth_result is nil", err.Error()) |
| void *prev_key = NULL; | ||
| unsigned int key = 0; | ||
|
|
||
| while (!bpf_map_get_next_key(map_fd, prev_key, &key)) { |
There was a problem hiding this comment.
Code Review
This pull request initializes prev_key to NULL in map_restore to prevent undefined behavior, and introduces a new unit test file xdp_auth_handler_test.go to test xdpNotifyConnRst. The review feedback recommends using t.Fatalf instead of assert.NoError during critical test setup steps (such as removing the memlock limit and creating the eBPF map) to prevent potential nil pointer dereference panics if those setup operations fail.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| err := rlimit.RemoveMemlock() | ||
| assert.NoError(t, err) | ||
|
|
||
| mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{ | ||
| Name: "test_map_of_auth_result", | ||
| Type: ebpf.Hash, | ||
| KeySize: uint32(TUPLE_LEN), | ||
| ValueSize: uint32(unsafe.Sizeof(uint32(0))), | ||
| MaxEntries: 4096, | ||
| }) | ||
| assert.NoError(t, err) | ||
| defer mapOfAuth.Close() |
There was a problem hiding this comment.
Using assert.NoError for critical test setup steps (such as removing the memlock limit and creating the eBPF map) allows the test to continue executing even if these operations fail. If ebpf.NewMap fails, mapOfAuth will be nil, and subsequent calls like mapOfAuth.Lookup in the subtests will cause a nil pointer dereference panic. Use t.Fatalf to stop test execution immediately if the setup fails.
if err := rlimit.RemoveMemlock(); err != nil {
t.Fatalf("Failed to remove memlock: %v", err)
}
mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{
Name: "test_map_of_auth_result",
Type: ebpf.Hash,
KeySize: uint32(TUPLE_LEN),
ValueSize: uint32(unsafe.Sizeof(uint32(0))),
MaxEntries: 4096,
})
if err != nil {
t.Fatalf("Failed to create ebpf map: %v", err)
}
defer mapOfAuth.Close()7c87bca to
d60f053
Compare
| err := rlimit.RemoveMemlock() | ||
| assert.NoError(t, err) | ||
|
|
||
| mapOfAuth, err := ebpf.NewMap(&ebpf.MapSpec{ | ||
| Name: "test_map_of_auth_result", | ||
| Type: ebpf.Hash, | ||
| KeySize: uint32(TUPLE_LEN), | ||
| ValueSize: uint32(unsafe.Sizeof(uint32(0))), | ||
| MaxEntries: 4096, | ||
| }) | ||
| assert.NoError(t, err) | ||
| defer mapOfAuth.Close() |
| err := xdpNotifyConnRst(nil, constants.MSG_TYPE_IPV4, key) | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "map_of_auth_result is nil", err.Error()) |
d60f053 to
50b9581
Compare
…rev_key Signed-off-by: Aryan Burnwal <burnwalaryan922@gmail.com>
50b9581 to
17838ba
Compare
This PR adds unit tests for
xdpNotifyConnRstto verify the connection reset logic for XDP authorization. It also fixes a compilation error in the deserialization module.Changes :-
pkg/auth/xdp_auth_handler_test.goto coverxdpNotifyConnRst:-prev_keytoNULLinmap_restore(deserialization_to_bpf_map.c) to fix a compilation failure (sometimes-uninitializedwarning treated as error under-Werror).rlimitmemory lock adjustments instead of loading the full Kmesh BPF programs, which helps tests run locally on setups like WSL2.Verification :-
Ran tests locally on WSL2 :-
go test -v ./pkg/auth -run Test_xdpNotifyConnRst