text
stringlengths
12
786k
module Backward ( D : Backward_domain ) ( T : Backward_transfer with type domain = D . t ) : Backward_S with type domain = D . t = struct type domain = D . t type _ map = | Block : domain Label . Tbl . t map | Instr : domain Instr . Tbl . t map module WorkSetElement = struct type t ...
type location = string exception Different of { location : location ; message : string }
let different : location -> string -> _ = fun location message -> raise ( Different { location ; message } )
module type State = sig type t val make : unit -> t val add_to_explore : t -> Label . t -> Label . t -> unit val to_explore : t -> ( Label . t * Label . t ) option val add_seen : t -> Label . t -> unit val has_seen : t -> Label . t -> bool val num_seen : t -> int val add_labels_to_chec...
module type Container = sig type ' a t val make : unit -> ' a t val add : ' a -> ' a t -> unit val get : ' a t -> ' a option end
module StackContainer : Container = struct type ' a t = ' a Stack . t let make = Stack . create let add = Stack . push let get = Stack . pop_opt end
module Make_state ( C : Container ) : State = struct type t = { subst : Label . t Label . Tbl . t ; to_explore : ( Label . t * Label . t ) C . t ; mutable seen : Label . Set . t ; mutable labels_to_check : ( location * Label . t * Label . t ) list ; mutable label_set...
module State = Make_state ( StackContainer )
let equal_raise_kind : Lambda . raise_kind -> Lambda . raise_kind -> bool = fun left right -> match left , right with | Raise_regular , Raise_regular -> true | Raise_reraise , Raise_reraise -> true | Raise_notrace , Raise_notrace -> true | Raise_regular , ( Raise_reraise | Raise_notrace ) ...
let array_equal eq left right = Array . length left = Array . length right && Array . for_all2 eq left right
let is_valid_stack_offset : int -> bool = fun stack_offset -> stack_offset >= 0
let check_external_call_operation : location -> Cfg . external_call_operation -> Cfg . external_call_operation -> unit = fun location expected result -> if not ( String . equal expected . func_symbol result . func_symbol ) then different location " function symbol " ; if not ( Bool . equal...
let check_operation : location -> Cfg . operation -> Cfg . operation -> unit = fun location expected result -> match expected , result with | Move , Move -> ( ) | Spill , Spill -> ( ) | Reload , Reload -> ( ) | Const_int expected , Const_int result when Nativeint . equal expecte...
let check_prim_call_operation : location -> Cfg . prim_call_operation -> Cfg . prim_call_operation -> unit = fun location expected result -> match expected , result with | External expected , External result -> check_external_call_operation location expected result | ( Alloc { bytes = expected_byt...
let check_func_call_operation : location -> Cfg . func_call_operation -> Cfg . func_call_operation -> unit = fun location expected result -> match expected , result with | Indirect , Indirect -> ( ) | ( Direct { func_symbol = expected_func_symbol } , Direct { func_symbol = result_func_s...
let check_tail_call_operation : State . t -> location -> Cfg . tail_call_operation -> Cfg . tail_call_operation -> unit = fun state location expected result -> match expected , result with | ( Self { destination = expected_destination } , Self { destination = result_destination } ) -> S...
let check_call_operation : location -> Cfg . call_operation -> Cfg . call_operation -> unit = fun location expected result -> match expected , result with | P expected , P result -> check_prim_call_operation location expected result | F expected , F result -> check_func_call_operation location expec...
let check_basic : State . t -> location -> Cfg . basic -> Cfg . basic -> unit = fun state location expected result -> match expected , result with | Op expected , Op result -> check_operation location expected result | Call expected , Call result -> check_call_operation location expected result ...
let check_instruction : type a . check_live : bool -> check_dbg : bool -> check_arg : bool -> int -> location -> a Cfg . instruction -> a Cfg . instruction -> unit = fun ~ check_live ~ check_dbg ~ check_arg idx location expected result -> let location = Printf . sprintf " % s ( index % d ...
let check_basic_instruction : State . t -> location -> int -> Cfg . basic Cfg . instruction -> Cfg . basic Cfg . instruction -> unit = fun state location idx expected result -> check_basic state location expected . desc result . desc ; let check_dbg = match expected . desc with Prologue -> f...
let rec check_basic_instruction_list : State . t -> location -> int -> Cfg . basic Cfg . instruction list -> Cfg . basic Cfg . instruction list -> unit = fun state location idx expected result -> match expected , result with | [ ] , [ ] -> ( ) | _ :: _ , [ ] -> different l...
let check_terminator_instruction : State . t -> location -> Cfg . terminator Cfg . instruction -> Cfg . terminator Cfg . instruction -> unit = fun state location expected result -> begin match expected . desc , result . desc with | Never , Never -> ( ) | Always lbl1 , Always lbl2 -> S...
let check_basic_block : State . t -> Cfg . basic_block -> Cfg . basic_block -> unit = fun state expected result -> let location = Printf . sprintf " block % s /% s " ( Label . to_string expected . start ) ( Label . to_string result . start ) in check_basic_instruction_list state loc...
let rec explore_cfg : State . t -> Cfg . t -> Cfg . t -> unit = fun state expected result -> match State . to_explore state with | None -> ( ) | Some ( lbl1 , lbl2 ) -> ( if not ( State . has_seen state lbl1 ) then let expected_block = Label . Tbl . find_opt expected . blocks ...
let check_cfg : State . t -> Cfg . t -> Cfg . t -> unit = fun state expected result -> let expected_num_blocks = Label . Tbl . length expected . blocks in let result_num_blocks = Label . Tbl . length result . blocks in if not ( Int . equal expected_num_blocks result_num_blocks ) then dif...
let _check_layout : State . t -> Label . t list -> Label . t list -> unit = fun state expected result -> let expected_length = List . length expected in let result_length = List . length result in if expected_length <> result_length then different " layout sizes " ( Printf . sprintf " expe...
let save_cfg_as_dot : Cfg_with_layout . t -> string -> unit = fun cfg_with_layout msg -> Cfg_with_layout . save_as_dot cfg_with_layout ~ show_instr : true ~ show_exn : true ~ annotate_succ ( : Printf . sprintf " % d ->% d " ) msg
let check_cfg_with_layout : ? mach : Mach . fundecl -> ? linear : Linear . fundecl -> Cfg_with_layout . t -> Cfg_with_layout . t -> unit = fun ? mach ? linear expected result -> try let state = State . make ( ) in check_cfg state ( Cfg_with_layout . cfg expected ) ( Cfg_with_layout ....
type cfg_item_info = | Cfg of Cfg_with_layout . t | Data of Cmm . data_item list
type cfg_unit_info = { mutable unit_name : string ; mutable items : cfg_item_info list ; mutable for_pack : string option }
type error = | Wrong_format of string | Wrong_version of string | Corrupted of string | Marshal_failed of string
let save filename cfg_unit_info = let ch = open_out_bin filename in Misc . try_finally ( fun ( ) -> output_string ch Config . cfg_magic_number ; output_value ch cfg_unit_info ; output_value ch ( Cmm . cur_label ( ) ) ; flush ch ; let crc = Digest . file filename in Digest . output ch...
let restore filename = let ic = open_in_bin filename in Misc . try_finally ( fun ( ) -> let magic = Config . cfg_magic_number in let buffer = really_input_string ic ( String . length magic ) in if String . equal buffer magic then begin try let cfg_unit_info = ( input_value ic : cfg_unit_info ...
let report_error ppf = function | Wrong_format filename -> fprintf ppf " Expected Cfg format . Incompatible file % a " Location . print_filename filename | Wrong_version filename -> fprintf ppf " % a @ is not compatible with this version of OCaml " Location . print_filename filename | Corrupted f...
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
module S = struct type func_call_operation = | Indirect | Direct of { func_symbol : string } type tail_call_operation = | Self of { destination : Label . t } | Func of func_call_operation type external_call_operation = { func_symbol : string ; alloc : bool ; ty_res : Cmm . machtype ; ty...
type domain = { before : Reg . Set . t ; across : Reg . Set . t }
module Domain : Cfg_dataflow . Backward_domain with type t = domain = struct type t = domain = { before : Reg . Set . t ; across : Reg . Set . t } let bot = { before = Reg . Set . empty ; across = Reg . Set . empty } let compare { before = left_before ; across = _ } { ...
module Transfer : Cfg_dataflow . Backward_transfer with type domain = domain = struct type nonrec domain = domain = { before : Reg . Set . t ; across : Reg . Set . t } let basic : domain -> exn : domain -> Cfg . basic Cfg . instruction -> domain = fun { before ; across = _ } ~ ...
module Liveness = Cfg_dataflow . Backward ( Domain ) ( Transfer )
let to_linear_instr ( ? like : _ Cfg . instruction option ) desc ~ next : L . instruction = let arg , res , dbg , live , fdo = match like with | None -> [ ] , || [ ] , || Debuginfo . none , Reg . Set . empty , Fdo_info . none | Some like -> like . arg , like . ...
let from_basic ( basic : Cfg . basic ) : L . instruction_desc = match basic with | Prologue -> Lprologue | Reloadretaddr -> Lreloadretaddr | Pushtrap { lbl_handler } -> Lpushtrap { lbl_handler } | Poptrap -> Lpoptrap | Call ( F Indirect ) -> Lop Icall_ind | Call ( F ( Direct { fu...
let basic_to_linear ( i : _ Cfg . instruction ) ~ next = let desc = from_basic i . desc in to_linear_instr ~ like : i desc ~ next
let mk_int_test ~ lt ~ eq ~ gt : Cmm . integer_comparison = match eq , lt , gt with | true , false , false -> Ceq | false , true , false -> Clt | false , false , true -> Cgt | false , true , true -> Cne | true , true , false -> Cle | true , false , true -> Cge | true , ...
type float_cond = | Must_be_last | Any of Cmm . float_comparison list
let mk_float_cond ~ lt ~ eq ~ gt ~ uo = match eq , lt , gt , uo with | true , false , false , false -> Any [ CFeq ] | false , true , false , false -> Any [ CFlt ] | false , false , true , false -> Any [ CFgt ] | true , true , false , false -> Any [ CFle ] | tr...
let linearize_terminator cfg ( terminator : Cfg . terminator Cfg . instruction ) ( ~ next : Linear_utils . labelled_insn ) : L . instruction * Label . t option = let branch_or_fallthrough lbl = if Label . equal next . label lbl then [ ] else [ L . Lbranch lbl ] in let emit_bool ( ...
let need_starting_label ( cfg_with_layout : CL . t ) ( block : Cfg . basic_block ) ( ~ prev_block : Cfg . basic_block ) = if block . is_trap_handler then true else match Label . Set . elements block . predecessors with | [ ] | _ :: _ :: _ -> true | [ pred ] when not ( L...
let adjust_stack_offset body ( block : Cfg . basic_block ) ( ~ prev_block : Cfg . basic_block ) = let block_stack_offset = block . stack_offset in let prev_stack_offset = prev_block . terminator . stack_offset in if block_stack_offset = prev_stack_offset then body else let delta_bytes = block_...
let run cfg_with_layout = let cfg = CL . cfg cfg_with_layout in let layout = Array . of_list ( CL . layout cfg_with_layout ) in let len = Array . length layout in let next = ref Linear_utils . labelled_insn_end in let tailrec_label = ref None in for i = len - 1 downto 0 do let label = layout . ...
let print_assembly ( blocks : Cfg . basic_block list ) = let layout = List . map ( fun ( b : Cfg . basic_block ) -> b . start ) blocks in let fun_name = " _fun_start_ " in let cfg = Cfg . create ~ fun_name ~ fun_args [ ] :|| ~ fun_dbg : Debuginfo . none ~ fun_fast : false ...
type t = { cfg : Cfg . t ; mutable layout : Label . t list ; mutable new_labels : Label . Set . t ; preserve_orig_labels : bool }
let create cfg ~ layout ~ preserve_orig_labels ~ new_labels = { cfg ; layout ; new_labels ; preserve_orig_labels }
let cfg t = t . cfg
let layout t = t . layout
let preserve_orig_labels t = t . preserve_orig_labels
let new_labels t = t . new_labels
let set_layout t layout = let cur_layout = Label . Set . of_list t . layout in let new_layout = Label . Set . of_list layout in if not ( Label . Set . equal cur_layout new_layout && Label . equal ( List . hd layout ) t . cfg . entry_label ) then Misc . fatal_error " Cfg set_layout ...
let remove_block t label = Cfg . remove_block_exn t . cfg label ; t . layout <- List . filter ( fun l -> not ( Label . equal l label ) ) t . layout ; t . new_labels <- Label . Set . remove label t . new_labels
let is_trap_handler t label = let block = Cfg . get_block_exn t . cfg label in block . is_trap_handler
let dump ppf t ~ msg = let open Format in fprintf ppf " \ ncfg for % s \ n " msg ; fprintf ppf " % s \ n " t . cfg . fun_name ; fprintf ppf " layout . length =% d \ n " ( List . length t . layout ) ; fprintf ppf " blocks . length =% d \ n " ( Label . Tbl . length t...
let print_dot ( ? show_instr = true ) ( ? show_exn = true ) ? annotate_block ? annotate_succ ppf t = Format . fprintf ppf " strict digraph " \% s " \ { \ n " t . cfg . fun_name ; let annotate_block label = match annotate_block with | None -> " " | Some f -> Printf . sprint...
let save_as_dot t ? show_instr ? show_exn ? annotate_block ? annotate_succ msg = let filename = Printf . sprintf " % s % s % s . dot " ( X86_proc . string_of_symbol " " t . cfg . fun_name ) ( if msg = " " then " " else " . " ) msg in if ! Cfg . verbose then Printf . ...
module Permute = struct external unsafe_set : ' a array -> int -> ' a -> unit = " % array_unsafe_set " external unsafe_get : ' a array -> int -> ' a = " % array_unsafe_get " let default_random_state = Random . State . make_self_init ( ) let array ( ? random_state = default_rando...
let reorder_blocks_random ? random_state t = let original_layout = layout t in let new_layout = List . hd original_layout :: Permute . list ? random_state ( List . tl original_layout ) in set_layout t new_layout
let create ( ) = { id = Runtime . Chan_id . gen ( ) ; channel = Event . new_channel ( ) ; closed_flag = false ; clients_count = 0 ; mutex = Mutex . create ( ) ; } ; ;
let maintain_clients_count chan delta = let will_block = ( delta * chan . clients_count >= 0 ) in ( if will_block then ( chan . clients_count <- chan . clients_count + delta ; Runtime . Fiber . blocked ( ) ) else ( chan . clients_count <- chan . clients_count + delta ; Runtime...
let unblocked_send_impl chan value = if None <> Event . poll ( Event . send chan . channel value ) then ( maintain_clients_count chan 1 ; true ) else false ; ;
let unblocked_send chan value = Mutex . lock chan . mutex ; if chan . closed_flag then ( Mutex . unlock chan . mutex ; false ) else let rst = unblocked_send_impl chan value in Mutex . unlock chan . mutex ; rst ; ;
let send chan value = Mutex . lock chan . mutex ; if chan . closed_flag then ( Mutex . unlock chan . mutex ; closed_chan chan ) ; if unblocked_send_impl chan value then Mutex . unlock chan . mutex else ( maintain_clients_count chan 1 ; Mutex . unlock chan . mutex ; Event . sync ( ...
let unblocked_receive_impl chan = if chan . closed_flag then Some EofObject else ( let rst = Event . poll ( Event . receive chan . channel ) in if None <> rst then maintain_clients_count chan ( - 1 ) ; rst ) ; ;
let unblocked_receive chan = Mutex . lock chan . mutex ; if chan . closed_flag then ( Mutex . unlock chan . mutex ; None ) else let rst = unblocked_receive_impl chan in Mutex . unlock chan . mutex ; rst ; ;
let receive chan = Mutex . lock chan . mutex ; match unblocked_receive_impl chan with | Some value -> ( Mutex . unlock chan . mutex ; value ) | None -> ( maintain_clients_count chan ( - 1 ) ; Mutex . unlock chan . mutex ; Event . sync ( Event . receive chan . channel ) ) ...
let close chan = Mutex . lock chan . mutex ; if chan . closed_flag then ( Mutex . unlock chan . mutex ; closed_chan chan ) else chan . closed_flag <- true ; if chan . clients_count > 0 then closed_chan chan else for i = 1 to ( - chan . clients_count ) do Event . sync ( Event . ...
let chr n = if n < 0 || n > 255 then invalid_arg " Char . chr " else unsafe_chr n = " % bytes_unsafe_set "
let escaped = function | ' ' ' \ -> " ' " \\ | ' ' \\ -> " " \\\\ | ' \ n ' -> " \\ n " | ' \ t ' -> " \\ t " | ' \ r ' -> " \\ r " | ' \ b ' -> " \\ b " | ' ' . . ' ' ~ as c -> let s = bytes_create 1 in bytes_unsafe_set s 0 ...
let lowercase = function | ' A ' . . ' Z ' | ' \ 192 ' . . ' \ 214 ' | ' \ 216 ' . . ' \ 222 ' as c -> unsafe_chr ( code c + 32 ) | c -> c
let uppercase = function | ' a ' . . ' z ' | ' \ 224 ' . . ' \ 246 ' | ' \ 248 ' . . ' \ 254 ' as c -> unsafe_chr ( code c - 32 ) | c -> c
let lowercase_ascii = function | ' A ' . . ' Z ' as c -> unsafe_chr ( code c + 32 ) | c -> c
let uppercase_ascii = function | ' a ' . . ' z ' as c -> unsafe_chr ( code c - 32 ) | c -> c
let compare c1 c2 = code c1 - code c2
let equal ( c1 : t ) ( c2 : t ) = compare c1 c2 = 0
let ( ) = Printexc . record_backtrace true
let filter_map f l = List . rev @@ List . fold_left ( fun a v -> match f v with Some v ' -> v ' :: a | None -> a ) [ ] l
let level_of_string = function | " warning " -> Lwt_log . Warning | " notice " -> Lwt_log . Notice | " debug " -> Lwt_log . Debug | _ -> invalid_arg " Unknown verbosity level "
let go_safe user group = let ( pw , _gr ) = try ( Unix . getpwnam user , Unix . getgrnam group ) with _ -> failwith " No user and / or group _charruad found , please create them . " in Unix . chroot pw . Unix . pw_dir ; Unix . chdir " " ; / let ogid = Unix . getgid ( ...
let read_file f = let ic = open_in f in let n = in_channel_length ic in let buf = Bytes . create n in really_input ic buf 0 n ; close_in ic ; Bytes . to_string buf
let go_daemon ( ) = Lwt_daemon . daemonize ~ syslog : false ( )
let init_log vlevel daemon = Lwt_log_core . Section . ( set_level main vlevel ) ; Lwt_log . default := if daemon then Lwt_log . syslog ~ template " ( :$ date ) ( $ level ) ( $ name ) [ ( $ pid ) ] : ( $ message ) " ~ facility ` : Daemon ~ paths [ " :/ dev / ...
let uptime_in_sec ( ) = Mtime_clock . elapsed ( ) |> Mtime . Span . to_s |> Int . of_float
let maybe_gc db now gbcol = let open Lwt in if ( now - gbcol ) >= 60 then Lwt_log . debug " Garbage collecting . . . " >>= fun ( ) -> return ( Dhcp_server . Lease . garbage_collect db ~ now ( : Int32 . of_int now ) , now + 60 ) else return ( db , gbcol )
let rec input config db link gbcol = let open Dhcp_server . Input in let open Lwt in Lwt_rawlink . read_packet link >>= fun buf -> let now = uptime_in_sec ( ) in maybe_gc db now gbcol >>= fun ( db , gbcol ) -> let t = match Dhcp_wire . pkt_of_buf buf ( Cstruct . length buf ) with | Error e...
let ifname_of_address ip_addr interfaces = let ifnet = List . find ( function _name , cidr -> Ipaddr . V4 . compare ip_addr ( Ipaddr . V4 . Prefix . address cidr ) = 0 ) interfaces in match ifnet with name , _ -> name
let charruad configfile group pidfile user verbosity daemonize = let open Dhcp_server . Config in let open Dhcp_server . Lease in let open Lwt in init_log ( level_of_string verbosity ) daemonize ; let interfaces = Tuntap . getifaddrs_v4 ( ) in let addresses = List . map ( function name , cidr ...
let cmd = let configfile = Arg . ( value & opt string " / etc / charruad . conf " & info [ " c " ; " config " ] ~ doc " : Configuration file path . " ) in let group = Arg . ( value & opt string " _charruad " & info [ " g " ; " group " ] ~ doc " : Gro...
let ( ) = exit ( Cmd . eval cmd )
let get_element ( ? f = Str . search_forward ) ~ regexp ( ? start = 0 ) line = try f ( Str . regexp regexp ) line start |> ignore ; Str . matched_string line with _ -> " "
let get_path = get_element ~ regexp " . . [ ] . . :\\/*</* mli " ?
let get_pos line = let pos = get_element ~ regexp " [ :: 0 - 9 ] " *: line in int_of_string ( if pos <> " " then String . sub pos 1 @@ String . length pos - 2 else pos )
let get_info line = let info = get_element ~ regexp " :: . " *$ ~ f : Str . search_backward ~ start ( : String . length line - 1 ) line in String . sub info 1 ( String . length info - 1 )