Skip to content

Commit c6e96f0

Browse files
committed
Implement auto-dereferencing for map access in return position and add a test case to verify the fix for map access return bug.
1 parent c819ad3 commit c6e96f0

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/ebpf_c_codegen.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,9 @@ let rec generate_c_instruction ctx ir_instr =
24512451
(match Kernelscript_context.Context_codegen.map_context_action_constant "tc" i with
24522452
| Some action -> action
24532453
| None -> string_of_int i)
2454+
| IRMapAccess (_, _, _) ->
2455+
(* For map access in return position, auto-dereference to return the value *)
2456+
generate_c_value ~auto_deref_map_access:true ctx ret_val
24542457
| _ -> generate_c_value ctx ret_val
24552458
in
24562459

tests/test_map_integration.ml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,48 @@ map<u32, u64> tc_stats : HashMap(1024)
406406
| exn -> fail ("Error in " ^ prog_type ^ " test: " ^ Printexc.to_string exn)
407407
) programs
408408

409+
(** Test the fix for map access return bug *)
410+
let test_map_access_return_bug_fix () =
411+
let source = {|
412+
enum TestEnum {
413+
VALUE_A = 1,
414+
VALUE_B = 2
415+
}
416+
417+
map<u32, TestEnum> test_map : HashMap(64)
418+
419+
@helper
420+
fn get_value(key: u32) -> TestEnum {
421+
var result = test_map[key]
422+
if (result != none) {
423+
return result // This should return the dereferenced value, not pointer
424+
} else {
425+
return VALUE_A
426+
}
427+
}
428+
429+
@xdp fn test_program(ctx: *xdp_md) -> xdp_action {
430+
var value = get_value(42)
431+
return 2
432+
}
433+
|} in
434+
435+
(* Parse and compile *)
436+
let ast = parse_string source in
437+
438+
match compile_to_c_code ast with
439+
| Some c_code ->
440+
(* Check that the generated code contains the correct dereferencing pattern *)
441+
let has_deref_pattern = string_contains_substring c_code "__val" &&
442+
string_contains_substring c_code "*(ptr_" in
443+
let has_bad_pointer_return = string_contains_substring c_code "return ptr_" in
444+
445+
(* Verify the fix is applied: should have dereferencing but not direct pointer returns *)
446+
check bool "Map access return should be dereferenced" true has_deref_pattern;
447+
check bool "Should not return raw pointers" false has_bad_pointer_return
448+
| None ->
449+
fail "Failed to compile map access return test"
450+
409451
let map_integration_tests = [
410452
"complete_map_compilation", `Quick, test_complete_map_compilation;
411453
"multiple_map_types", `Quick, test_multiple_map_types;
@@ -414,6 +456,7 @@ let map_integration_tests = [
414456
"map_operations_in_conditionals", `Quick, test_map_operations_in_conditionals;
415457
"memory_safety", `Quick, test_memory_safety;
416458
"different_context_types", `Quick, test_different_context_types;
459+
"map_access_return_bug_fix", `Quick, test_map_access_return_bug_fix;
417460
]
418461

419462
let () =

0 commit comments

Comments
 (0)