Skip to content

Commit 485950e

Browse files
committed
Refactor dynptr handling in eBPF C code generation to use DYNPTR_SAFE_WRITE macro for safer memory operations.
1 parent e76cb86 commit 485950e

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/ebpf_c_codegen.ml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,40 +2671,39 @@ let rec generate_c_instruction ctx ir_instr =
26712671
(* Check if this is a dynptr-backed pointer first *)
26722672
(match Hashtbl.find_opt ctx.dynptr_backed_pointers obj_str with
26732673
| Some dynptr_var ->
2674-
(* This is a dynptr-backed pointer - use bpf_dynptr_write *)
2675-
let field_size = calculate_type_size value_val.val_type in
2674+
(* This is a dynptr-backed pointer - use DYNPTR_SAFE_WRITE macro *)
26762675
(match obj_val.val_type with
26772676
| IRPointer (IRStruct (struct_name, _), _) ->
26782677
let full_struct_name = sprintf "struct %s" struct_name in
2679-
emit_line ctx (sprintf "{ %s __tmp_val = %s;" (ebpf_type_from_ir_type value_val.val_type) value_str);
2680-
emit_line ctx (sprintf " bpf_dynptr_write(&%s, __builtin_offsetof(%s, %s), &__tmp_val, %d, 0); }"
2681-
dynptr_var full_struct_name field_name field_size)
2678+
let c_type = ebpf_type_from_ir_type value_val.val_type in
2679+
emit_line ctx (sprintf "DYNPTR_SAFE_WRITE(&%s, __builtin_offsetof(%s, %s), %s, %s);"
2680+
dynptr_var full_struct_name field_name value_str c_type)
26822681
| _ ->
26832682
(* Fallback to direct assignment for non-struct types *)
26842683
emit_line ctx (sprintf "if (%s) { %s->%s = %s; }" obj_str obj_str field_name value_str))
26852684
| None ->
26862685
(* Not a dynptr-backed pointer - use enhanced semantic analysis for field assignment *)
26872686
(match detect_memory_region_enhanced obj_val with
26882687
| PacketData ->
2689-
(* Packet data field assignment - use dynptr API for safe write *)
2688+
(* Packet data field assignment - use DYNPTR_SAFE_WRITE macro *)
26902689
(match obj_val.val_type with
26912690
| IRPointer (IRStruct (struct_name, _), _) ->
2692-
let field_size = calculate_type_size value_val.val_type in
26932691
let full_struct_name = sprintf "struct %s" struct_name in
2692+
let c_type = ebpf_type_from_ir_type value_val.val_type in
26942693
emit_line ctx (sprintf "{ struct bpf_dynptr __pkt_dynptr; bpf_dynptr_from_xdp(&__pkt_dynptr, ctx);");
26952694
emit_line ctx (sprintf " __u32 __field_offset = (%s - ctx->data) + __builtin_offsetof(%s, %s);" obj_str full_struct_name field_name);
2696-
emit_line ctx (sprintf " bpf_dynptr_write(&__pkt_dynptr, __field_offset, &%s, %d, 0); }" value_str field_size)
2695+
emit_line ctx (sprintf " DYNPTR_SAFE_WRITE(&__pkt_dynptr, __field_offset, %s, %s); }" value_str c_type)
26972696
| _ ->
26982697
emit_line ctx (sprintf "if (%s) { %s->%s = %s; }" obj_str obj_str field_name value_str))
26992698

27002699
| _ when is_map_value_parameter obj_val ->
2701-
(* Map value field assignment - use dynptr API *)
2700+
(* Map value field assignment - use DYNPTR_SAFE_WRITE macro *)
27022701
(match obj_val.val_type with
27032702
| IRPointer (IRStruct (struct_name, _), _) ->
2704-
let field_size = calculate_type_size value_val.val_type in
27052703
let full_struct_name = sprintf "struct %s" struct_name in
2704+
let c_type = ebpf_type_from_ir_type value_val.val_type in
27062705
emit_line ctx (sprintf "{ struct bpf_dynptr __mem_dynptr; bpf_dynptr_from_mem(%s, sizeof(%s), 0, &__mem_dynptr);" obj_str full_struct_name);
2707-
emit_line ctx (sprintf " bpf_dynptr_write(&__mem_dynptr, __builtin_offsetof(%s, %s), &%s, %d, 0); }" full_struct_name field_name value_str field_size)
2706+
emit_line ctx (sprintf " DYNPTR_SAFE_WRITE(&__mem_dynptr, __builtin_offsetof(%s, %s), %s, %s); }" full_struct_name field_name value_str c_type)
27082707
| _ ->
27092708
emit_line ctx (sprintf "if (%s) { %s->%s = %s; }" obj_str obj_str field_name value_str))
27102709

0 commit comments

Comments
 (0)