-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmulti_program_analyzer.ml
More file actions
470 lines (427 loc) · 18.3 KB
/
Copy pathmulti_program_analyzer.ml
File metadata and controls
470 lines (427 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
(** Multi-Program Analyzer for KernelScript
This module analyzes multiple eBPF programs together as a coordinated system,
detecting cross-program dependencies, shared map usage patterns, and
optimization opportunities.
*)
open Ast
(** Linux kernel execution context for eBPF programs *)
type execution_context = {
program_type: program_type;
hook_point: string; (* Kernel hook description *)
stack_layer: int; (* Network stack layer (1=earliest, 4=latest, 0=not in packet path) *)
execution_stage: string; (* High-level stage *)
can_drop_packets: bool; (* Whether program can drop packets *)
}
(** Get execution context for each eBPF program type *)
let get_execution_context = function
| Xdp -> {
program_type = Xdp;
hook_point = "netdev_rx (NIC driver level)";
stack_layer = 1; (* EARLIEST - right after NIC hardware *)
execution_stage = "packet_receive_early";
can_drop_packets = true;
}
| Tc -> {
program_type = Tc;
hook_point = "tc_classify (qdisc layer)";
stack_layer = 2; (* LATER - after IP processing *)
execution_stage = "packet_receive_late";
can_drop_packets = true;
}
| Probe _ -> {
program_type = Probe Kprobe; (* Both fprobe and kprobe have similar characteristics *)
hook_point = "kernel_function_entry/exit";
stack_layer = 0; (* Can run anywhere - not in packet path *)
execution_stage = "dynamic_tracing";
can_drop_packets = false;
}
| Tracepoint -> {
program_type = Tracepoint;
hook_point = "static_kernel_tracepoint";
stack_layer = 0; (* Can be anywhere *)
execution_stage = "static_tracing";
can_drop_packets = false;
}
| StructOps -> {
program_type = StructOps;
hook_point = "kernel_struct_ops_callbacks";
stack_layer = 0; (* Can be anywhere - depends on subsystem *)
execution_stage = "struct_ops_callbacks";
can_drop_packets = false;
}
| PerfEvent -> {
program_type = PerfEvent;
hook_point = "perf_event_sampling";
stack_layer = 0;
execution_stage = "perf_sampling";
can_drop_packets = false;
}
(** Check if two programs execute sequentially (not concurrently) *)
let are_sequential prog_type1 prog_type2 =
let ctx1 = get_execution_context prog_type1 in
let ctx2 = get_execution_context prog_type2 in
(* Programs in packet processing path with different layers are sequential *)
if ctx1.stack_layer > 0 && ctx2.stack_layer > 0 && ctx1.stack_layer <> ctx2.stack_layer then
true
(* Tracing programs (stack_layer = 0) are concurrent with everything *)
else if ctx1.stack_layer = 0 || ctx2.stack_layer = 0 then
false
else
false (* Same layer = potentially concurrent *)
(** Enhanced multi-program analysis result *)
type multi_program_analysis = {
programs: program_def list;
global_maps: map_declaration list;
map_usage_patterns: (string * string list) list; (* map_name -> accessing_programs *)
potential_conflicts: string list;
optimization_opportunities: string list;
execution_flow_info: string list; (* NEW: Kernel execution flow insights *)
sequential_dependencies: string list; (* NEW: Sequential access patterns *)
}
(** Extract programs from AST by converting attributed functions to program_def records *)
let extract_programs (ast: declaration list) : program_def list =
List.filter_map (function
| AttributedFunction attr_func ->
(* Convert attributed function to program_def for compatibility *)
(match attr_func.attr_list with
| SimpleAttribute prog_type_str :: _ ->
(match prog_type_str with
| "kfunc" -> None (* Skip kfunc functions - they're not eBPF programs *)
| "private" -> None (* Skip private functions - they're not eBPF programs *)
| "helper" -> None (* Skip helper functions - they're shared eBPF functions, not individual programs *)
| "test" -> None (* Skip test functions - they're userspace test functions, not eBPF programs *)
| _ ->
let prog_type = match prog_type_str with
| "xdp" -> Xdp
| "tc" -> Tc
| "kprobe" -> Probe Kprobe
| "tracepoint" -> Tracepoint
| "struct_ops" -> StructOps
| "perf_event" -> PerfEvent
| _ -> failwith ("Unknown program type: " ^ prog_type_str)
in
Some {
prog_name = attr_func.attr_function.func_name;
prog_type = prog_type;
prog_functions = [attr_func.attr_function];
prog_maps = [];
prog_structs = [];
prog_target = None;
prog_pos = attr_func.attr_pos;
})
| _ -> None)
| _ -> None
) ast
(** Extract global maps from AST *)
let extract_global_maps (ast: declaration list) : map_declaration list =
List.filter_map (function
| MapDecl map_decl when map_decl.is_global -> Some map_decl
| _ -> None
) ast
(** Analyze map usage patterns across programs *)
let analyze_map_usage (programs: program_def list) (global_maps: map_declaration list)
: (string * string list) list =
let map_usage_table = Hashtbl.create 32 in
(* Initialize usage tracking for all global maps *)
List.iter (fun map_decl ->
Hashtbl.add map_usage_table map_decl.name []
) global_maps;
(* Simple map usage analysis - look for map identifiers in expressions *)
let rec analyze_expr_for_maps prog_name expr =
match expr.expr_desc with
| Identifier name ->
(* Check if this identifier is a global map *)
if List.exists (fun m -> m.name = name) global_maps then (
let current_progs =
try Hashtbl.find map_usage_table name
with Not_found -> []
in
if not (List.mem prog_name current_progs) then
Hashtbl.replace map_usage_table name (prog_name :: current_progs)
)
| ArrayAccess (map_expr, key_expr) ->
analyze_expr_for_maps prog_name map_expr;
analyze_expr_for_maps prog_name key_expr
| Call (_, args) ->
List.iter (analyze_expr_for_maps prog_name) args
| BinaryOp (left, _, right) ->
analyze_expr_for_maps prog_name left;
analyze_expr_for_maps prog_name right
| UnaryOp (_, expr) ->
analyze_expr_for_maps prog_name expr
| FieldAccess (obj_expr, _) ->
analyze_expr_for_maps prog_name obj_expr
| _ -> ()
in
let rec analyze_stmt_for_maps prog_name stmt =
match stmt.stmt_desc with
| ExprStmt expr ->
analyze_expr_for_maps prog_name expr
| Assignment (_, expr) ->
analyze_expr_for_maps prog_name expr
| CompoundAssignment (_, _, expr) ->
analyze_expr_for_maps prog_name expr
| CompoundIndexAssignment (map_expr, key_expr, _, value_expr) ->
analyze_expr_for_maps prog_name map_expr;
analyze_expr_for_maps prog_name key_expr;
analyze_expr_for_maps prog_name value_expr
| CompoundFieldIndexAssignment (map_expr, key_expr, _, _, value_expr) ->
analyze_expr_for_maps prog_name map_expr;
analyze_expr_for_maps prog_name key_expr;
analyze_expr_for_maps prog_name value_expr
| FieldAssignment (obj_expr, _, value_expr) ->
analyze_expr_for_maps prog_name obj_expr;
analyze_expr_for_maps prog_name value_expr
| ArrowAssignment (obj_expr, _, value_expr) ->
analyze_expr_for_maps prog_name obj_expr;
analyze_expr_for_maps prog_name value_expr
| IndexAssignment (map_expr, key_expr, value_expr) ->
analyze_expr_for_maps prog_name map_expr;
analyze_expr_for_maps prog_name key_expr;
analyze_expr_for_maps prog_name value_expr
| Declaration (_, _, expr_opt) ->
(match expr_opt with
| Some expr -> analyze_expr_for_maps prog_name expr
| None -> ())
| ConstDeclaration (_, _, expr) ->
analyze_expr_for_maps prog_name expr
| Return (Some expr) ->
analyze_expr_for_maps prog_name expr
| If (cond_expr, then_stmts, else_stmts_opt) ->
analyze_expr_for_maps prog_name cond_expr;
List.iter (analyze_stmt_for_maps prog_name) then_stmts;
(match else_stmts_opt with
| Some else_stmts -> List.iter (analyze_stmt_for_maps prog_name) else_stmts
| None -> ())
| IfLet (_, expr, then_stmts, else_stmts_opt) ->
analyze_expr_for_maps prog_name expr;
List.iter (analyze_stmt_for_maps prog_name) then_stmts;
(match else_stmts_opt with
| Some else_stmts -> List.iter (analyze_stmt_for_maps prog_name) else_stmts
| None -> ())
| For (_, start_expr, end_expr, body_stmts) ->
analyze_expr_for_maps prog_name start_expr;
analyze_expr_for_maps prog_name end_expr;
List.iter (analyze_stmt_for_maps prog_name) body_stmts
| ForIter (_, _, iter_expr, body_stmts) ->
analyze_expr_for_maps prog_name iter_expr;
List.iter (analyze_stmt_for_maps prog_name) body_stmts
| While (cond_expr, body_stmts) ->
analyze_expr_for_maps prog_name cond_expr;
List.iter (analyze_stmt_for_maps prog_name) body_stmts
| Delete target ->
(match target with
| DeleteMapEntry (map_expr, key_expr) ->
analyze_expr_for_maps prog_name map_expr;
analyze_expr_for_maps prog_name key_expr
| DeletePointer ptr_expr ->
analyze_expr_for_maps prog_name ptr_expr)
| Return None -> ()
| Break -> ()
| Continue -> ()
| Try (try_stmts, catch_clauses) ->
List.iter (analyze_stmt_for_maps prog_name) try_stmts;
List.iter (fun clause ->
List.iter (analyze_stmt_for_maps prog_name) clause.catch_body
) catch_clauses
| Throw _ -> () (* Throw statements don't contain map accesses *)
| Defer expr ->
analyze_expr_for_maps prog_name expr
in
(* Analyze all programs *)
List.iter (fun prog ->
List.iter (fun func ->
List.iter (analyze_stmt_for_maps prog.prog_name) func.func_body
) prog.prog_functions
) programs;
(* Convert hashtable to list *)
Hashtbl.fold (fun map_name prog_list acc ->
(map_name, List.rev prog_list) :: acc
) map_usage_table []
(** Enhanced conflict detection with kernel execution order awareness *)
let detect_conflicts_with_execution_order (programs: program_def list)
(map_usage_patterns: (string * string list) list) : string list * string list =
let real_conflicts = ref [] in
let sequential_accesses = ref [] in
List.iter (fun (map_name, accessing_programs) ->
if List.length accessing_programs > 1 then (
(* Get program types for accessing programs *)
let prog_types_with_names = List.filter_map (fun prog_name ->
List.find_map (fun prog ->
if prog.prog_name = prog_name then
Some (prog_name, prog.prog_type)
else None
) programs
) accessing_programs in
(* Analyze each pair of accessing programs *)
let rec analyze_pairs = function
| [] | [_] -> ()
| (name1, type1) :: rest ->
List.iter (fun (name2, type2) ->
if are_sequential type1 type2 then (
(* Sequential access - this is GOOD, not a conflict! *)
let ctx1 = get_execution_context type1 in
let ctx2 = get_execution_context type2 in
let (first_name, first_type, second_name, second_type) =
if ctx1.stack_layer < ctx2.stack_layer then
(name1, type1, name2, type2)
else
(name2, type2, name1, type1)
in
let sequential_msg = Printf.sprintf
"Sequential map access: %s (%s) → %s (%s) via '%s' (no race condition)"
first_name (string_of_program_type first_type)
second_name (string_of_program_type second_type)
map_name in
sequential_accesses := sequential_msg :: !sequential_accesses
) else (
(* Concurrent access - TRUE race condition *)
let conflict_msg = Printf.sprintf
"TRUE RACE CONDITION: Map '%s' accessed concurrently by %s (%s) and %s (%s)"
map_name name1 (string_of_program_type type1)
name2 (string_of_program_type type2) in
real_conflicts := conflict_msg :: !real_conflicts
)
) rest;
analyze_pairs rest
in
analyze_pairs prog_types_with_names
)
) map_usage_patterns;
(!real_conflicts, !sequential_accesses)
(** Generate optimization hints *)
let generate_optimization_hints (map_usage_patterns: (string * string list) list)
(global_maps: map_declaration list) : string list =
let hints = ref [] in
(* Suggest per-CPU maps for high-contention scenarios *)
List.iter (fun (map_name, accessing_programs) ->
if List.length accessing_programs > 1 then (
let map_decl = List.find (fun m -> m.name = map_name) global_maps in
match map_decl.map_type with
| Hash ->
let hint = Printf.sprintf
"Consider using percpu_hash for map '%s' to reduce contention between programs: %s"
map_name (String.concat ", " accessing_programs) in
hints := hint :: !hints
| Array ->
let hint = Printf.sprintf
"Consider using percpu_array for map '%s' to reduce contention between programs: %s"
map_name (String.concat ", " accessing_programs) in
hints := hint :: !hints
| _ -> ()
)
) map_usage_patterns;
!hints
(** Main multi-program analysis function *)
let analyze_multi_program_system (ast: declaration list) : multi_program_analysis =
let programs = extract_programs ast in
let global_maps = extract_global_maps ast in
let map_usage_patterns = analyze_map_usage programs global_maps in
let (real_conflicts, sequential_accesses) =
detect_conflicts_with_execution_order programs map_usage_patterns in
let optimization_opportunities = generate_optimization_hints map_usage_patterns global_maps in
(* Generate execution flow description *)
let execution_flow_info =
let network_programs = List.filter (fun prog ->
let ctx = get_execution_context prog.prog_type in
ctx.stack_layer > 0
) programs in
if List.length network_programs > 1 then (
let sorted_programs = List.sort (fun prog1 prog2 ->
let ctx1 = get_execution_context prog1.prog_type in
let ctx2 = get_execution_context prog2.prog_type in
compare ctx1.stack_layer ctx2.stack_layer
) network_programs in
let flow_desc = List.map (fun prog ->
let ctx = get_execution_context prog.prog_type in
Printf.sprintf "%s@%s" prog.prog_name ctx.hook_point
) sorted_programs in
["🔄 Kernel execution flow: " ^ String.concat " → " flow_desc]
) else []
in
{
programs;
global_maps;
map_usage_patterns;
potential_conflicts = real_conflicts;
optimization_opportunities;
execution_flow_info;
sequential_dependencies = sequential_accesses;
}
(** Print multi-program analysis results *)
let print_analysis_results (analysis: multi_program_analysis) : unit =
Printf.printf "\n=== Multi-Program Analysis Results ===\n";
Printf.printf "\nPrograms analyzed: %d\n" (List.length analysis.programs);
List.iter (fun prog ->
Printf.printf " - %s (%s)\n" prog.prog_name (string_of_program_type prog.prog_type)
) analysis.programs;
Printf.printf "\nGlobal maps: %d\n" (List.length analysis.global_maps);
List.iter (fun map_decl ->
Printf.printf " - %s (%s)\n" map_decl.name (string_of_map_type map_decl.map_type)
) analysis.global_maps;
Printf.printf "\nMap usage patterns:\n";
List.iter (fun (map_name, accessing_programs) ->
Printf.printf " - %s: accessed by %d programs [%s]\n"
map_name (List.length accessing_programs) (String.concat ", " accessing_programs)
) analysis.map_usage_patterns;
if analysis.execution_flow_info <> [] then (
Printf.printf "\n";
List.iter (fun info ->
Printf.printf "%s\n" info
) analysis.execution_flow_info
);
if analysis.sequential_dependencies <> [] then (
Printf.printf "\n✅ Sequential access patterns (no race conditions):\n";
List.iter (fun dep ->
Printf.printf " - %s\n" dep
) analysis.sequential_dependencies
);
if analysis.potential_conflicts <> [] then (
Printf.printf "\n⚠️ True race conditions found:\n";
List.iter (fun conflict ->
Printf.printf " - %s\n" conflict
) analysis.potential_conflicts
);
if analysis.optimization_opportunities <> [] then (
Printf.printf "\n💡 Optimization opportunities:\n";
List.iter (fun hint ->
Printf.printf " - %s\n" hint
) analysis.optimization_opportunities
);
Printf.printf "\n✅ Multi-program analysis completed.\n\n"
(** Extract program types from AST for BTF loading *)
let get_program_types_from_ast (ast: declaration list) : program_type list =
List.fold_left (fun acc decl ->
match decl with
| AttributedFunction attr_func ->
(match attr_func.attr_list with
| SimpleAttribute prog_type_str :: _ ->
(match prog_type_str with
| "xdp" -> Xdp :: acc
| "tc" -> Tc :: acc
| "kprobe" -> Probe Kprobe :: acc
| "tracepoint" -> Tracepoint :: acc
| "perf_event" -> PerfEvent :: acc
| _ -> acc)
| _ -> acc)
| _ -> acc
) [] ast |> List.rev |> fun types ->
(* Remove duplicates *)
List.fold_left (fun acc typ ->
if List.mem typ acc then acc else typ :: acc
) [] types