@@ -525,6 +525,69 @@ let compile_source input_file output_dir _verbose generate_makefile btf_vmlinux_
525525 let (annotated_ast, _typed_programs) = Type_checker. type_check_and_annotate_ast ~symbol_table: (Some symbol_table) compilation_ast in
526526 Printf. printf " ✅ Type checking completed with multi-program annotations\n\n " ;
527527
528+ (* Phase 4.5: Safety Analysis *)
529+ current_phase := " Safety Analysis" ;
530+ Printf. printf " Phase 4.5: %s\n " ! current_phase;
531+
532+ (* Extract all functions from the AST for safety analysis *)
533+ let all_functions = List. fold_left (fun acc decl ->
534+ match decl with
535+ | Ast. AttributedFunction attr_func -> attr_func.attr_function :: acc
536+ | Ast. GlobalFunction func -> func :: acc
537+ | _ -> acc
538+ ) [] compilation_ast in
539+
540+ (* Create a program structure for safety analysis *)
541+ let safety_program = {
542+ Ast. prog_name = base_name;
543+ prog_type = Xdp ; (* Default - not used by safety checker *)
544+ prog_functions = all_functions;
545+ prog_maps = [] ;
546+ prog_structs = [] ;
547+ prog_pos = Ast. make_position 1 1 input_file;
548+ } in
549+
550+ (* Run safety analysis *)
551+ let safety_analysis = Safety_checker. analyze_safety safety_program in
552+
553+ (* Check for safety violations and report them *)
554+ if not safety_analysis.overall_safe then (
555+ Printf. eprintf " ⚠️ Safety Analysis Issues:\n " ;
556+
557+ (* Report stack overflow issues *)
558+ if safety_analysis.stack_analysis.potential_overflow then (
559+ Printf. eprintf " ❌ Stack overflow detected: %d bytes exceeds eBPF limit of %d bytes\n "
560+ safety_analysis.stack_analysis.max_stack_usage
561+ Safety_checker.EbpfConstraints. max_stack_size;
562+ List. iter (fun warning -> Printf. eprintf " %s\n " warning) safety_analysis.stack_analysis.warnings;
563+ Printf. eprintf " Suggestion: Use BPF per-cpu array maps for large data structures\n " ;
564+ );
565+
566+ (* Report bounds errors *)
567+ if safety_analysis.bounds_errors <> [] then (
568+ Printf. eprintf " ❌ Bounds checking errors:\n " ;
569+ List. iter (fun error ->
570+ Printf. eprintf " %s\n " (Safety_checker. string_of_bounds_error error)
571+ ) safety_analysis.bounds_errors;
572+ );
573+
574+ (* Report pointer safety issues *)
575+ if safety_analysis.pointer_safety.invalid_pointers <> [] then (
576+ Printf. eprintf " ❌ Pointer safety issues:\n " ;
577+ List. iter (fun (ptr , reason ) ->
578+ Printf. eprintf " %s: %s\n " ptr reason
579+ ) safety_analysis.pointer_safety.invalid_pointers;
580+ );
581+
582+ Printf. eprintf " \n ❌ Compilation halted due to safety violations\n " ;
583+ exit 1
584+ ) else (
585+ Printf. printf " ✅ Safety analysis passed - %s stack usage: %d/%d bytes\n\n "
586+ base_name
587+ safety_analysis.stack_analysis.max_stack_usage
588+ Safety_checker.EbpfConstraints. max_stack_size
589+ );
590+
528591 (* Phase 5: IR Optimization *)
529592 current_phase := " IR Optimization" ;
530593 Printf. printf " Phase 5: %s\n " ! current_phase;
0 commit comments