@@ -200,6 +200,12 @@ let emit_line ctx line =
200
200
let emit_blank_line ctx =
201
201
ctx.output_lines < - ctx.output_lines @ [" " ]
202
202
203
+ let concat = List. concat
204
+ let concat_map f l = List. concat (List. map f l)
205
+ let concat_map_opt f = function
206
+ | Some l -> concat_map f l
207
+ | None -> []
208
+
203
209
let increase_indent ctx = ctx.indent_level < - ctx.indent_level + 1
204
210
205
211
let decrease_indent ctx = ctx.indent_level < - ctx.indent_level - 1
@@ -356,13 +362,9 @@ let rec collect_string_sizes_from_instr ir_instr =
356
362
in
357
363
var_type_sizes @ init_sizes
358
364
| IRCall (_ , args , ret_opt ) ->
359
- let args_sizes = List. fold_left (fun acc arg ->
360
- acc @ (collect_string_sizes_from_value arg)) [] args in
361
- let ret_sizes = match ret_opt with
362
- | Some ret_val -> collect_string_sizes_from_value ret_val
363
- | None -> []
364
- in
365
- args_sizes @ ret_sizes
365
+ let args_sizes = concat_map collect_string_sizes_from_value args in
366
+ let ret_sizes = match ret_opt with Some ret_val -> collect_string_sizes_from_value ret_val | None -> [] in
367
+ args_sizes @ ret_sizes
366
368
| IRMapLoad (map_val , key_val , dest_val , _ ) ->
367
369
(collect_string_sizes_from_value map_val) @
368
370
(collect_string_sizes_from_value key_val) @
@@ -392,24 +394,17 @@ let rec collect_string_sizes_from_instr ir_instr =
392
394
collect_string_sizes_from_value cond_val
393
395
| IRIf (cond_val , then_instrs , else_instrs_opt ) ->
394
396
let cond_sizes = collect_string_sizes_from_value cond_val in
395
- let then_sizes = List. fold_left (fun acc instr ->
396
- acc @ (collect_string_sizes_from_instr instr)) [] then_instrs in
397
- let else_sizes = match else_instrs_opt with
398
- | Some else_instrs -> List. fold_left (fun acc instr ->
399
- acc @ (collect_string_sizes_from_instr instr)) [] else_instrs
400
- | None -> []
401
- in
397
+ let then_sizes = concat_map collect_string_sizes_from_instr then_instrs in
398
+ let else_sizes = concat_map_opt collect_string_sizes_from_instr else_instrs_opt in
402
399
cond_sizes @ then_sizes @ else_sizes
403
400
| IRIfElseChain (conditions_and_bodies , final_else ) ->
404
- let cond_sizes = List. fold_left (fun acc (cond_val , then_instrs ) ->
405
- let cond_sizes = collect_string_sizes_from_value cond_val in
406
- let then_sizes = List. fold_left (fun acc instr ->
407
- acc @ (collect_string_sizes_from_instr instr)) [] then_instrs in
408
- acc @ cond_sizes @ then_sizes
409
- ) [] conditions_and_bodies in
401
+ let cond_sizes = concat_map (fun (cond_val , then_instrs ) ->
402
+ let cond_sz = collect_string_sizes_from_value cond_val in
403
+ let then_sz = concat_map collect_string_sizes_from_instr then_instrs in
404
+ cond_sz @ then_sz
405
+ ) conditions_and_bodies in
410
406
let else_sizes = match final_else with
411
- | Some else_instrs -> List. fold_left (fun acc instr ->
412
- acc @ (collect_string_sizes_from_instr instr)) [] else_instrs
407
+ | Some else_instrs -> concat_map collect_string_sizes_from_instr else_instrs
413
408
| None -> []
414
409
in
415
410
cond_sizes @ else_sizes
@@ -440,8 +435,7 @@ let rec collect_string_sizes_from_instr ir_instr =
440
435
(collect_string_sizes_from_value end_val) @
441
436
(collect_string_sizes_from_value counter_val) @
442
437
(collect_string_sizes_from_value ctx_val) @
443
- (List. fold_left (fun acc instr ->
444
- acc @ (collect_string_sizes_from_instr instr)) [] body_instructions)
438
+ (concat_map collect_string_sizes_from_instr body_instructions)
445
439
| IRBreak -> []
446
440
| IRContinue -> []
447
441
| IRCondReturn (cond_val , ret_if_true , ret_if_false ) ->
@@ -456,16 +450,13 @@ let rec collect_string_sizes_from_instr ir_instr =
456
450
in
457
451
cond_sizes @ true_sizes @ false_sizes
458
452
| IRTry (try_instructions , _catch_clauses ) ->
459
- List. fold_left (fun acc instr ->
460
- acc @ (collect_string_sizes_from_instr instr)) [] try_instructions
453
+ concat_map collect_string_sizes_from_instr try_instructions
461
454
| IRThrow _error_code ->
462
455
[] (* Throw statements don't contain values to collect *)
463
456
| IRDefer defer_instructions ->
464
- List. fold_left (fun acc instr ->
465
- acc @ (collect_string_sizes_from_instr instr)) [] defer_instructions
457
+ concat_map collect_string_sizes_from_instr defer_instructions
466
458
| IRTailCall (_ , args , _ ) ->
467
- List. fold_left (fun acc arg ->
468
- acc @ (collect_string_sizes_from_value arg)) [] args
459
+ concat_map collect_string_sizes_from_value args
469
460
| IRStructOpsRegister (instance_val , struct_ops_val ) ->
470
461
(collect_string_sizes_from_value instance_val) @ (collect_string_sizes_from_value struct_ops_val)
471
462
| IRObjectNew (dest_val , _ ) ->
@@ -478,23 +469,13 @@ let rec collect_string_sizes_from_instr ir_instr =
478
469
collect_string_sizes_from_value ringbuf_val
479
470
480
471
let collect_string_sizes_from_function ir_func =
481
- List. fold_left (fun acc block ->
482
- List. fold_left (fun acc instr ->
483
- acc @ (collect_string_sizes_from_instr instr)
484
- ) acc block.instructions
485
- ) [] ir_func.basic_blocks
472
+ concat_map (fun block -> concat_map collect_string_sizes_from_instr block.instructions) ir_func.basic_blocks
486
473
487
474
let collect_string_sizes_from_multi_program ir_multi_prog =
488
- let program_sizes = List. fold_left (fun acc ir_prog ->
489
- let entry_sizes = collect_string_sizes_from_function ir_prog.entry_function in
490
- acc @ entry_sizes
491
- ) [] ir_multi_prog.programs in
492
-
475
+ let program_sizes = concat_map (fun ir_prog -> collect_string_sizes_from_function ir_prog.entry_function) ir_multi_prog.programs in
476
+
493
477
(* Also collect from kernel functions *)
494
- let kernel_func_sizes = List. fold_left (fun acc ir_func ->
495
- let func_sizes = collect_string_sizes_from_function ir_func in
496
- acc @ func_sizes
497
- ) [] ir_multi_prog.kernel_functions in
478
+ let kernel_func_sizes = concat_map (fun ir_func -> collect_string_sizes_from_function ir_func) ir_multi_prog.kernel_functions in
498
479
499
480
(* NOTE: We used to collect string sizes from all userspace structs here, but this was incorrect.
500
481
Only structs that are actually used by eBPF programs should be considered.
0 commit comments