package mopsa
MOPSA: A Modular and Open Platform for Static Analysis using Abstract Interpretation
Install
Dune Dependency
Authors
Maintainers
Sources
mopsa-analyzer-v1.1.tar.gz
md5=fdee20e988343751de440b4f6b67c0f4
sha512=f5cbf1328785d3f5ce40155dada2d95e5de5cce4f084ea30cfb04d1ab10cc9403a26cfb3fa55d0f9da72244482130fdb89c286a9aed0d640bba46b7c00e09500
doc/src/universal_iterators/loops.ml.html
Source file loops.ml
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
(****************************************************************************) (* *) (* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *) (* *) (* Copyright (C) 2017-2019 The MOPSA Project. *) (* *) (* This program is free software: you can redistribute it and/or modify *) (* it under the terms of the GNU Lesser General Public License as published *) (* by the Free Software Foundation, either version 3 of the License, or *) (* (at your option) any later version. *) (* *) (* This program is distributed in the hope that it will be useful, *) (* but WITHOUT ANY WARRANTY; without even the implied warranty of *) (* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *) (* GNU Lesser General Public License for more details. *) (* *) (* You should have received a copy of the GNU Lesser General Public License *) (* along with this program. If not, see <http://www.gnu.org/licenses/>. *) (* *) (****************************************************************************) (** Loops iterator with widening *) open Mopsa open Sig.Abstraction.Stateless open Ast let name = "universal.iterators.loops" (*==========================================================================*) (** {2 Loops flow token} *) (*==========================================================================*) type token += | T_break (** Control flows reaching a break statement *) | T_continue (** Control flows reaching a continue statement *) let () = register_token { compare = (fun next tk1 tk2 -> next tk1 tk2); print = (fun next fmt tk -> match tk with | T_break -> Format.pp_print_string fmt "break" | T_continue -> Format.pp_print_string fmt "cont" | _ -> next fmt tk ); } (*==========================================================================*) (** {2 Unrolling strategy} *) (*==========================================================================*) type unrolling = { mutable unroll_global_nb : int option (* unrolling numbers *); mutable unroll_locals : local_unrolling list; } and local_unrolling = { unroll_local_file : string option; unroll_local_line : int; unroll_local_nb : int option; } let opt_unrolling = { unroll_global_nb = Some 1; unroll_locals = []; } (** Parse local unrolling specification string *) let parse_unroll_local (spec:string) : local_unrolling = if not Str.(string_match (regexp "^\\(\\([a-zA-Z][^:]*\\):\\)?\\([0-9]+\\):\\([0-9]+\\)$") spec 0) then panic "incorrect argument '%s' for option -loop-unrolling-at" spec ; let file = try Some (Str.matched_group 2 spec) with Not_found -> None in let line = Str.matched_group 3 spec |> int_of_string in let nb = Some (Str.matched_group 4 spec |> int_of_string) in { unroll_local_file = file; unroll_local_line = line; unroll_local_nb = nb; } (** Parse local full unrolling specification string *) let parse_full_unroll_local (spec:string) : local_unrolling = if not Str.(string_match (regexp "^\\(\\([a-zA-Z][^:]*\\):\\)?\\([0-9]+\\)$") spec 0) then panic "incorrect argument '%s' for option -loop-full-unrolling-at" spec ; let file = try Some (Str.matched_group 2 spec) with Not_found -> None in let line = Str.matched_group 3 spec |> int_of_string in { unroll_local_file = file; unroll_local_line = line; unroll_local_nb = None; } (** Get the unrolling limit for a given loop location *) let get_range_unrolling range : int option = let range = untag_range range in let is_matching u = match_range_line u.unroll_local_line range && match u.unroll_local_file with | None -> true | Some file -> match_range_file file range in match List.find_opt is_matching opt_unrolling.unroll_locals with | Some u -> u.unroll_local_nb | None -> opt_unrolling.unroll_global_nb (*==========================================================================*) (** {2 Command line options} *) (*==========================================================================*) let opt_loop_widening_delay : int ref = ref 0 (** Number of iterations before applying a widening. *) let opt_loop_use_cache : bool ref = ref false let opt_loop_decreasing_it : bool ref = ref false let () = register_domain_option name { key = "-widening-delay"; category = "Loops"; doc = " number of iterations before applying a widening"; spec = Set_int (opt_loop_widening_delay,ArgExt.empty); default = "0"; }; register_domain_option name { key = "-loop-unrolling"; category = "Loops"; doc = " number of unrolling iterations before joining the environments"; spec = Int ((fun n -> opt_unrolling.unroll_global_nb <- Some n),ArgExt.empty); default = "1"; }; register_domain_option name { key = "-loop-unrolling-at"; category = "Loops"; doc = " number of unrolling iterations at specific program location (syntax: [file.]line:unrolling)"; spec = String (ArgExt.string_list_lifter (fun specs -> opt_unrolling.unroll_locals <- List.map parse_unroll_local specs),ArgExt.empty); default = ""; }; register_domain_option name { key = "-loop-full-unrolling"; category = "Loops"; doc = " unroll loops without applying widening"; spec = Bool (fun b -> opt_unrolling.unroll_global_nb <- (if b then None else opt_unrolling.unroll_global_nb)); default = "false"; }; register_domain_option name { key = "-loop-full-unrolling-at"; category = "Loops"; doc = " fully unroll loop at specific program location (syntax: [file.]line)"; spec = String (ArgExt.string_list_lifter (fun specs -> opt_unrolling.unroll_locals <- List.map parse_full_unroll_local specs),ArgExt.empty); default = ""; }; register_domain_option name { key = "-loop-no-cache"; category = "Loops"; doc = " do not use cache for loops"; spec = Clear opt_loop_use_cache; default = "use cache"; }; register_domain_option name { key = "-loop-decr-it"; category = "Loops"; doc = " enable decreasing iteration"; spec = Set opt_loop_decreasing_it; default = "disabled"; } (*==========================================================================*) (** {2 Domain} *) (*==========================================================================*) let nestedness = ref 0 module Domain = struct (** {3 Domain header} *) (** ***************** *) include GenStatelessDomainId(struct let name = name end) let checks = [] (** {3 Cache of last fixpoint} *) (** ************************** *) (** Fixpoints are attached to the callstack and the location of the loop head *) module LoopHeadMap = MapExt.Make( struct type t = (callstack * range) let compare (cs1, r1) (cs2, r2) = Compare.compose [(fun () -> compare_range r1 r2); (fun () -> compare_callstack cs1 cs2); ] let print fmt (cs, r) = Format.fprintf fmt "[%a, %a]" pp_range r pp_callstack cs end) (** Cache of the last fixpoints at loop heads *) module LastFixpointCtx = GenContextKey( struct type 'a t = 'a flow LoopHeadMap.t let print l fmt ctx = Format.fprintf fmt "Lfp cache context: %a" (LoopHeadMap.fprint MapExt.printer_default (fun fmt (cs, r) -> pp_callstack fmt cs; pp_range fmt r) (fun fmt flow -> format (TokenMap.print l) fmt (Flow.get_token_map flow))) ctx end ) (** Search the last fixpoint attached to a loop *) let search_last_fixpoint (srange, scs) man flow = try let m = find_ctx LastFixpointCtx.key (Flow.get_ctx flow) in let mf = LoopHeadMap.filter (fun (cs, range) _ -> compare_callstack cs scs = 0 && compare_range srange range = 0 ) m in Some (LoopHeadMap.choose mf |> snd |> Flow.join man.lattice flow) with Not_found -> (debug "no ctx found"; None) (** Update the last fixpoint attached to a loop *) let store_fixpoint man flow (range, cs) = let old_lfp_ctx = try let r = find_ctx LastFixpointCtx.key (Flow.get_ctx flow) in if LoopHeadMap.cardinal r > 5 then LoopHeadMap.remove_min_binding r else r with Not_found -> LoopHeadMap.empty in let stripped_flow = Flow.bottom (Flow.get_ctx flow) (Flow.get_report flow) |> Flow.add T_cur (Flow.get T_cur man.lattice flow) man.lattice |> Flow.add T_continue (Flow.get T_continue man.lattice flow) man.lattice |> Flow.add T_break (Flow.get T_break man.lattice flow) man.lattice in Debug.debug ~channel:(name ^ ".cache") "@(%a, %a): adding %a" pp_range range pp_callstack cs (format (Flow.print man.lattice.print)) stripped_flow; let lfp_ctx = LoopHeadMap.add (cs, range) stripped_flow old_lfp_ctx in Flow.set_ctx (add_ctx LastFixpointCtx.key lfp_ctx (Flow.get_ctx flow)) flow let join_w_old_lfp man flow range = debug "searching in cache"; match search_last_fixpoint range man flow with | None -> None | Some old_lfp -> let res = Flow.join man.lattice old_lfp flow in Debug.debug ~channel:"universal.iterators.loops.cache" "cache: %a join %a = %a@\n" (format (Flow.print man.lattice.print)) old_lfp (format (Flow.print man.lattice.print)) flow (format (Flow.print man.lattice.print)) res; Some res (* flow *) (** Merge tokens T_cur and T_continue into T_cur *) let merge_cur_and_continue man flow = let cur = Flow.get T_cur man.lattice flow in let cont = Flow.get T_continue man.lattice flow in let ctx = Flow.get_ctx flow in Flow.set T_cur (man.lattice.join ctx cur cont) man.lattice flow |> Flow.remove T_continue let init prog man flow = Flow.map_ctx (add_ctx LastFixpointCtx.key LoopHeadMap.empty) flow |> Post.return |> Option.some let decr_iteration cond body man flow_init flow = Flow.remove T_continue flow |> Flow.remove T_break |> Flow.remove_report |> man.exec (mk_assume cond cond.erange) >>% man.exec body >>% fun flow -> merge_cur_and_continue man flow |> Flow.join man.lattice flow_init |> Post.return let rec lfp count delay cond body man flow_init flow = debug "lfp called, range = %a, count = %d" pp_range body.srange count; (* Ignore continue and break flows of the previous iterations *) Flow.remove T_continue flow |> Flow.remove T_break |> (* Ignore report of the previous iterations *) Flow.set_report (Flow.get_report flow_init) |> (* Apply condition and execute body *) man.exec (mk_assume cond cond.erange) >>% man.exec body >>% fun f -> (* Merge cur and continue flows *) let flow' = merge_cur_and_continue man f |> Flow.join man.lattice flow_init in (* Check convergence of cur flow *) let cur = Flow.get T_cur man.lattice flow in let cur' = Flow.get T_cur man.lattice flow' in let is_sub = man.lattice.subset (Flow.get_ctx flow') cur' cur in debug "lfp range %a is_sub: %b" pp_range body.srange is_sub; if is_sub then (* Convergence *) Post.return flow' else if delay = 0 then (* Widen *) let wflow = Flow.widen man.lattice flow flow' in lfp (count+1) !opt_loop_widening_delay cond body man flow_init wflow else (* Delay *) lfp (count+1) (delay - 1) cond body man flow_init flow' let rec unroll i cond body man flow = debug "unrolling iteration %a in %a" (OptionExt.print ~none:"" ~some:"" Format.pp_print_int) i pp_range (srange body); if i = Some 0 then Cases.singleton (false, Flow.bottom (Flow.get_ctx flow) (Flow.get_report flow)) flow else let post1 = man.exec {skind = S_assume cond; srange = cond.erange} flow >>% man.exec body >>% fun f -> merge_cur_and_continue man f |> Post.return in let post2 = man.exec (mk_assume (mk_not cond cond.erange) cond.erange) (Flow.set_ctx (Cases.get_ctx post1) flow) in post1 >>% fun flow1 -> post2 >>% fun flow2 -> if Flow.subset man.lattice flow1 flow then let () = debug "stabilisation reached in unrolling!" in Cases.singleton (true, flow2) flow1 else unroll (OptionExt.lift (fun ii -> (ii - 1)) i) cond body man (Flow.copy_ctx flow2 flow1) >>$ fun (flag, flow2') flow1' -> Cases.singleton (flag, Flow.join man.lattice flow2 flow2') flow1' let rec exec stmt man flow = match skind stmt with | S_while(cond, body) -> incr nestedness; Debug.debug ~channel:"nested" "nestedness: %d" !nestedness; debug "while %a:" (* @\nflow = @[%a@] *) pp_range stmt.srange (* (Flow.print man.lattice) flow *); let flow0 = Flow.remove T_continue flow |> Flow.remove T_break in begin if !opt_loop_use_cache then match join_w_old_lfp man flow0 (stmt.srange, Flow.get_callstack flow0) with | Some flow0 -> Cases.singleton (false, Flow.bottom (Flow.get_ctx flow0) (Flow.get_report flow0)) flow0 | None -> unroll (get_range_unrolling stmt.srange) cond body man flow0 else unroll (get_range_unrolling stmt.srange) cond body man flow0 end >>$? fun (is_fp, flow_out) flow_init -> begin if is_fp then Post.return flow_init else lfp 0 !opt_loop_widening_delay cond body man flow_init flow_init >>% fun flow_lfp -> begin if !opt_loop_decreasing_it then decr_iteration cond body man flow_init flow_lfp else Post.return flow_lfp end >>% fun flow_lfp -> let flow_lfp = if !opt_loop_use_cache then store_fixpoint man flow_lfp (stmt.srange, Flow.get_callstack flow_lfp) else flow_lfp in Post.return flow_lfp end >>%? fun flow_lfp -> man.exec (mk_assume (mk_not cond cond.erange) cond.erange) flow_lfp >>%? fun f -> let res0 = Flow.join man.lattice flow_out f in debug "while post abs %a:" (* @\nres0 = @[%a@] *) pp_range stmt.srange (* (Flow.print man.lattice) res0 *); let res1 = Flow.add T_cur (Flow.get T_break man.lattice res0) man.lattice res0 |> Flow.set T_break (Flow.get T_break man.lattice flow) man.lattice |> Flow.set T_continue (Flow.get T_continue man.lattice flow) man.lattice in decr nestedness; Some (Post.return res1) | S_break -> let cur = Flow.get T_cur man.lattice flow in let flow' = Flow.add T_break cur man.lattice flow |> Flow.remove T_cur in Some (Post.return flow') | S_continue -> let cur = Flow.get T_cur man.lattice flow in let flow' = Flow.add T_continue cur man.lattice flow |> Flow.remove T_cur in Some (Post.return flow') | _ -> None let eval _ _ _ = None let ask _ _ _ = None let print_expr man flow printer exp = () end (*==========================================================================*) (** {2 Setup} *) (*==========================================================================*) let () = register_stateless_domain (module Domain)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>