@@ -207,9 +207,9 @@ let rec calculate_type_size ir_type =
207207 (* These types should never appear in field assignments due to type checking *)
208208 | IRVoid ->
209209 failwith " calculate_type_size: IRVoid should not appear in field assignments"
210- | IRStruct (struct_name , _ , _ ) ->
210+ | IRStruct (struct_name , _ ) ->
211211 failwith (" calculate_type_size: IRStruct should not appear in field assignments, got: " ^ struct_name)
212- | IREnum (enum_name , _ , _ ) ->
212+ | IREnum (enum_name , _ ) ->
213213 failwith (" calculate_type_size: IREnum should not appear in field assignments, got: " ^ enum_name)
214214 | IRResult (_ , _ ) ->
215215 failwith " calculate_type_size: IRResult should not appear in field assignments"
@@ -315,8 +315,8 @@ let rec ebpf_type_from_ir_type = function
315315 | IRStr size -> sprintf " str_%d_t" size
316316 | IRPointer (inner_type , _ ) -> sprintf " %s*" (ebpf_type_from_ir_type inner_type)
317317 | IRArray (inner_type , size , _ ) -> sprintf " %s[%d]" (ebpf_type_from_ir_type inner_type) size
318- | IRStruct (name , _ , _ ) -> sprintf " struct %s" name
319- | IREnum (name , _ , _ ) -> sprintf " enum %s" name
318+ | IRStruct (name , _ ) -> sprintf " struct %s" name
319+ | IREnum (name , _ ) -> sprintf " enum %s" name
320320
321321 | IRResult (ok_type , _err_type ) -> ebpf_type_from_ir_type ok_type (* simplified to ok type *)
322322 | IRTypeAlias (name , _ ) -> name (* Use the alias name directly *)
@@ -567,7 +567,7 @@ let collect_enum_definitions ?symbol_table ir_multi_prog =
567567 let enum_map = Hashtbl. create 16 in
568568
569569 let rec collect_from_type = function
570- | IREnum (name , values , _ ) -> Hashtbl. replace enum_map name values
570+ | IREnum (name , values ) -> Hashtbl. replace enum_map name values
571571 | IRPointer (inner_type , _ ) -> collect_from_type inner_type
572572 | IRArray (inner_type , _ , _ ) -> collect_from_type inner_type
573573
@@ -655,7 +655,7 @@ let collect_enum_definitions ?symbol_table ir_multi_prog =
655655 let global_symbols = Symbol_table. get_global_symbols st in
656656 List. iter (fun symbol ->
657657 match symbol.Symbol_table. kind with
658- | Symbol_table. TypeDef (Ast. EnumDef (enum_name , enum_values , _kernel_defined )) ->
658+ | Symbol_table. TypeDef (Ast. EnumDef (enum_name , enum_values )) ->
659659 let processed_values = List. map (fun (const_name , opt_value ) ->
660660 (const_name, Option. value ~default: 0 opt_value)
661661 ) enum_values in
@@ -726,7 +726,7 @@ let collect_struct_definitions_from_multi_program ir_multi_prog =
726726
727727 let rec collect_from_type ir_type =
728728 match ir_type with
729- | IRStruct (name , struct_fields , _ ) ->
729+ | IRStruct (name , struct_fields ) ->
730730 if not (List. mem_assoc name ! struct_defs) then (
731731 (* Only collect structs that actually have fields - ignore empty structs that are likely type aliases *)
732732 if struct_fields <> [] then
@@ -852,19 +852,21 @@ let collect_struct_definitions_from_multi_program ir_multi_prog =
852852
853853(* * Generate struct definitions *)
854854let generate_struct_definitions ctx struct_defs =
855- (* Filter out kernel-defined structs based on IR kernel_defined flag *)
856- let user_defined_structs = List. filter (fun (_struct_name , fields ) ->
857- (* Check if this struct itself is kernel-defined or has kernel-defined fields *)
855+ (* Filter out kernel-defined structs using centralized kernel type knowledge *)
856+ let user_defined_structs = List. filter (fun (struct_name , fields ) ->
857+ (* Check if this struct name is a well-known kernel type *)
858+ let is_kernel_type = Kernel_types. is_well_known_ebpf_type struct_name in
859+
860+ (* Check if this struct has kernel-defined field types *)
858861 let has_kernel_field = List. exists (fun (_field_name , field_type ) ->
859862 match field_type with
860- | IRStruct (_ , _ , kernel_defined ) -> kernel_defined
861- | IREnum (_ , _ , kernel_defined ) -> kernel_defined
863+ | IRStruct (name , _ ) -> Kernel_types. is_well_known_ebpf_type name
864+ | IREnum (name , _ ) -> Kernel_types. is_well_known_ebpf_type name
862865 | _ -> false
863866 ) fields in
864867
865- (* Only filter based on kernel_defined flag from IR, not struct name *)
866- (* User-defined structs should be generated regardless of their name *)
867- not has_kernel_field
868+ (* Filter out kernel types and structs with kernel-defined fields *)
869+ not is_kernel_type && not has_kernel_field
868870 ) struct_defs in
869871
870872 if user_defined_structs <> [] then (
@@ -1741,7 +1743,7 @@ let generate_c_expression ctx ir_expr =
17411743 | PacketData ->
17421744 (* Packet data field access - use bpf_dynptr_from_xdp *)
17431745 (match obj_val.val_type with
1744- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
1746+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
17451747 (* Note: For field ACCESS (not assignment), we use sizeof(__typeof(field))
17461748 which is calculated by the C compiler, so we don't need calculate_type_size here *)
17471749 let field_size = sprintf " sizeof(__typeof(((%s*)0)->%s))"
@@ -1754,7 +1756,7 @@ let generate_c_expression ctx ir_expr =
17541756 | _ when is_map_value_parameter obj_val ->
17551757 (* Map value field access - use bpf_dynptr_from_mem *)
17561758 (match obj_val.val_type with
1757- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
1759+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
17581760 (* Note: For field ACCESS (not assignment), we use sizeof(__typeof(field))
17591761 which is calculated by the C compiler, so we don't need calculate_type_size here *)
17601762 let field_size = sprintf " sizeof(__typeof(((%s*)0)->%s))"
@@ -2054,7 +2056,7 @@ let generate_ringbuf_operation ctx ringbuf_val op =
20542056
20552057 (* Calculate proper size based on the result type *)
20562058 let size = match result_val.val_type with
2057- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
2059+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
20582060 (* Use sizeof for struct types *)
20592061 sprintf " sizeof(struct %s)" struct_name
20602062 | IRPointer (elem_type , _ ) ->
@@ -2066,7 +2068,7 @@ let generate_ringbuf_operation ctx ringbuf_val op =
20662068 (match other_type with
20672069 | IRU32 -> " IRU32"
20682070 | IRU64 -> " IRU64"
2069- | IRStruct (name , _ , _ ) -> " IRStruct " ^ name
2071+ | IRStruct (name , _ ) -> " IRStruct " ^ name
20702072 | IRVoid -> " IRVoid"
20712073 | _ -> " unknown type" ))
20722074 in
@@ -2318,7 +2320,7 @@ let generate_truthy_conversion ctx ir_value =
23182320 | IRPointer (_ , _ ) ->
23192321 (* Pointers: null is falsy, non-null is truthy *)
23202322 sprintf " (%s != NULL)" (generate_c_value ctx ir_value)
2321- | IREnum (_ , _ , _ ) ->
2323+ | IREnum (_ , _ ) ->
23222324 (* Enums: based on numeric value *)
23232325 sprintf " (%s != 0)" (generate_c_value ctx ir_value)
23242326 | _ ->
@@ -2508,10 +2510,10 @@ let rec generate_c_instruction ctx ir_instr =
25082510 (* Check if this is a dynptr-backed pointer first *)
25092511 (match Hashtbl. find_opt ctx.dynptr_backed_pointers obj_str with
25102512 | Some dynptr_var ->
2511- (* This is a dynptr-backed pointer - use bpf_dynptr_write *)
2512- let field_size = calculate_type_size value_val.val_type in
2513- (match obj_val.val_type with
2514- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
2513+ (* This is a dynptr-backed pointer - use bpf_dynptr_write *)
2514+ let field_size = calculate_type_size value_val.val_type in
2515+ (match obj_val.val_type with
2516+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
25152517 let full_struct_name = sprintf " struct %s" struct_name in
25162518 emit_line ctx (sprintf " { %s __tmp_val = %s;" (ebpf_type_from_ir_type value_val.val_type) value_str);
25172519 emit_line ctx (sprintf " bpf_dynptr_write(&%s, __builtin_offsetof(%s, %s), &__tmp_val, %d, 0); }"
@@ -2524,8 +2526,8 @@ let rec generate_c_instruction ctx ir_instr =
25242526 (match detect_memory_region_enhanced obj_val with
25252527 | PacketData ->
25262528 (* Packet data field assignment - use dynptr API for safe write *)
2527- (match obj_val.val_type with
2528- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
2529+ (match obj_val.val_type with
2530+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
25292531 let field_size = calculate_type_size value_val.val_type in
25302532 let full_struct_name = sprintf " struct %s" struct_name in
25312533 emit_line ctx (sprintf " { struct bpf_dynptr __pkt_dynptr; bpf_dynptr_from_xdp(&__pkt_dynptr, ctx);" );
@@ -2537,7 +2539,7 @@ let rec generate_c_instruction ctx ir_instr =
25372539 | _ when is_map_value_parameter obj_val ->
25382540 (* Map value field assignment - use dynptr API *)
25392541 (match obj_val.val_type with
2540- | IRPointer (IRStruct (struct_name , _ , _ ), _ ) ->
2542+ | IRPointer (IRStruct (struct_name , _ ), _ ) ->
25412543 let field_size = calculate_type_size value_val.val_type in
25422544 let full_struct_name = sprintf " struct %s" struct_name in
25432545 emit_line ctx (sprintf " { struct bpf_dynptr __mem_dynptr; bpf_dynptr_from_mem(%s, sizeof(%s), 0, &__mem_dynptr);" obj_str full_struct_name);
@@ -3336,8 +3338,8 @@ let generate_c_function ctx ir_func =
33363338 | (_ , IRPointer (IRContext XdpCtx, _ )) :: _ -> Some " xdp"
33373339 | (_ , IRPointer (IRContext TcCtx, _ )) :: _ -> Some " tc"
33383340 | (_ , IRPointer (IRContext KprobeCtx, _ )) :: _ -> Some " kprobe"
3339- | (_ , IRPointer (IRStruct ("__sk_buff" , _ , _ ), _ )) :: _ -> Some " tc" (* Handle __sk_buff as TC context *)
3340- | (_ , IRPointer (IRStruct ("xdp_md" , _ , _ ), _ )) :: _ -> Some " xdp" (* Handle xdp_md as XDP context *)
3341+ | (_ , IRPointer (IRStruct ("__sk_buff" , _ ), _ )) :: _ -> Some " tc" (* Handle __sk_buff as TC context *)
3342+ | (_ , IRPointer (IRStruct ("xdp_md" , _ ), _ )) :: _ -> Some " xdp" (* Handle xdp_md as XDP context *)
33413343 | _ -> None ));
33423344
33433345 let return_type_str =
@@ -3383,7 +3385,7 @@ let generate_c_function ctx ir_func =
33833385 | (_ , IRPointer (IRContext TcCtx, _ )) :: _ -> " SEC(\" tc\" )"
33843386 | (_ , IRPointer (IRContext KprobeCtx, _ )) :: _ -> " SEC(\" kprobe\" )"
33853387 | (_ , IRPointer (IRContext TracepointCtx, _ )) :: _ -> " SEC(\" tracepoint\" )"
3386- | (_ , IRPointer (IRStruct ("__sk_buff" , _ , _ ), _ )) :: _ -> " SEC(\" tc\" )" (* Handle __sk_buff as TC context *)
3388+ | (_ , IRPointer (IRStruct ("__sk_buff" , _ ), _ )) :: _ -> " SEC(\" tc\" )" (* Handle __sk_buff as TC context *)
33873389 | _ -> " SEC(\" prog\" )"
33883390 else " "
33893391 in
0 commit comments