package goblint
Static analysis framework for C
Install
Dune Dependency
Authors
Maintainers
Sources
goblint-2.2.1.tbz
sha256=ca24f72fa9a87d288affe97c411753f14b7802bab4ca3649b337276b89bf5674
sha512=394b3521ccda0da91540cebb2f433f7525763060be4bbe179edd3b952a3580a8e173c4e410fc6895dc67fe6d17e6699aeddfed600f4692858bec093dd912bf1e
doc/src/goblint.lib/autoTune.ml.html
Source file autoTune.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 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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
(** Autotuning of the configuration based on syntactic heuristics. *) open GobConfig open GoblintCil open AutoTune0 (*Create maps that map each function to the ones called in it and the ones calling it Only considers static calls!*) module FunctionSet = Set.Make(CilType.Varinfo) module FunctionCallMap = Map.Make(CilType.Varinfo) let addOrCreateMap fd = function | Some (set, i) -> Some (FunctionSet.add fd set, i+1) | None -> Some (FunctionSet.singleton fd, 1) class collectFunctionCallsVisitor(callSet, calledBy, argLists, fd) = object inherit nopCilVisitor method! vinst = function | Call (_,Lval ((Var info), NoOffset),args,_,_)-> callSet := FunctionSet.add info !callSet; calledBy := FunctionCallMap.update info (addOrCreateMap fd) !calledBy; (*We collect the argument list so we can use LibraryFunctions.find to classify functions*) argLists := FunctionCallMap.add info args !argLists; DoChildren | _ -> DoChildren end class functionVisitor(calling, calledBy, argLists, dynamicallyCalled) = object inherit nopCilVisitor method! vglob = function | GVarDecl (vinfo,_) -> if vinfo.vaddrof && isFunctionType vinfo.vtype then dynamicallyCalled := FunctionSet.add vinfo !dynamicallyCalled; DoChildren | _ -> DoChildren method! vfunc fd = let callSet = ref FunctionSet.empty in let callVisitor = new collectFunctionCallsVisitor (callSet, calledBy, argLists, fd.svar) in ignore @@ Cil.visitCilFunction callVisitor fd; calling := FunctionCallMap.add fd.svar !callSet !calling; DoChildren end type functionCallMaps = { calling: FunctionSet.t FunctionCallMap.t; calledBy: (FunctionSet.t * int) FunctionCallMap.t; argLists: Cil.exp list FunctionCallMap.t; dynamicallyCalled: FunctionSet.t; } let functionCallMaps = ResettableLazy.from_fun (fun () -> let calling = ref FunctionCallMap.empty in let calledBy = ref FunctionCallMap.empty in let argLists = ref FunctionCallMap.empty in let dynamicallyCalled = ref FunctionSet.empty in let thisVisitor = new functionVisitor(calling,calledBy, argLists, dynamicallyCalled) in visitCilFileSameGlobals thisVisitor (!Cilfacade.current_file); {calling = !calling; calledBy = !calledBy; argLists = !argLists; dynamicallyCalled= !dynamicallyCalled}) (* Only considers static calls!*) let calledFunctions fd = (ResettableLazy.force functionCallMaps).calling |> FunctionCallMap.find_opt fd |> Option.value ~default:FunctionSet.empty let callingFunctions fd = (ResettableLazy.force functionCallMaps).calledBy |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> fst let timesCalled fd = (ResettableLazy.force functionCallMaps).calledBy |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> snd let functionArgs fd = (ResettableLazy.force functionCallMaps).argLists |> FunctionCallMap.find_opt fd let findMallocWrappers () = let isMalloc f = if LibraryFunctions.is_special f then ( let desc = LibraryFunctions.find f in match functionArgs f with | None -> false | Some args -> match desc.special args with | Malloc _ -> true | _ -> false ) else false in (ResettableLazy.force functionCallMaps).calling |> FunctionCallMap.filter (fun _ allCalled -> FunctionSet.exists isMalloc allCalled) |> FunctionCallMap.filter (fun f _ -> timesCalled f > 10) |> FunctionCallMap.bindings |> List.map (fun (v,_) -> v.vname) |> List.iter (fun n -> print_endline ("malloc wrapper: " ^ n); GobConfig.set_auto "ana.malloc.wrappers[+]" n) (*Functions for determining if the congruence analysis should be enabled *) let isExtern = function | Extern -> true | _ -> false let rec setCongruenceRecursive fd depth neigbourFunction = if depth >= 0 then ( fd.svar.vattr <- addAttributes (fd.svar.vattr) [Attr ("goblint_precision",[AStr "congruence"])]; FunctionSet.iter (fun vinfo -> print_endline (" " ^ vinfo.vname); setCongruenceRecursive (Cilfacade.find_varinfo_fundec vinfo) (depth -1) neigbourFunction ) (FunctionSet.filter (*for extern and builtin functions there is no function definition in CIL*) (fun x -> not (isExtern x.vstorage || BatString.starts_with x.vname "__builtin")) (neigbourFunction fd.svar) ) ; ) exception ModFound class modVisitor = object inherit nopCilVisitor method! vexpr = function | BinOp (Mod,_,_,_) -> raise ModFound; | _ -> DoChildren end class modFunctionAnnotatorVisitor = object inherit nopCilVisitor method! vfunc fd = let thisVisitor = new modVisitor in try ignore (visitCilFunction thisVisitor fd) with | ModFound -> print_endline ("function " ^ (CilType.Fundec.show fd) ^" uses mod, enable congruence domain recursively for:"); print_endline (" \"down\":"); setCongruenceRecursive fd 6 calledFunctions; print_endline (" \"up\":"); setCongruenceRecursive fd 3 callingFunctions; ; SkipChildren end let addModAttributes file = set_bool "annotation.int.enabled" true; let thisVisitor = new modFunctionAnnotatorVisitor in ignore (visitCilFileSameGlobals thisVisitor file) let disableIntervalContextsInRecursiveFunctions () = (ResettableLazy.force functionCallMaps).calling |> FunctionCallMap.iter (fun f set -> (*detect direct recursion and recursion with one indirection*) if FunctionSet.mem f set || (not @@ FunctionSet.disjoint (calledFunctions f) (callingFunctions f)) then ( print_endline ("function " ^ (f.vname) ^" is recursive, disable interval and interval_set contexts"); f.vattr <- addAttributes (f.vattr) [Attr ("goblint_context",[AStr "base.no-interval"; AStr "base.no-interval_set"; AStr "relation.no-context"])]; ) ) let hasFunction pred = let relevant_static var = if LibraryFunctions.is_special var then let desc = LibraryFunctions.find var in GobOption.exists (fun args -> pred (desc.special args)) (functionArgs var) else false in let relevant_dynamic var = if LibraryFunctions.is_special var then let desc = LibraryFunctions.find var in (* We don't really have arguments at hand, so we cheat and just feed it a list of MyCFG.unknown_exp of appropriate length *) match unrollType var.vtype with | TFun (_, args, _, _) -> let args = BatOption.map_default (List.map (fun (x,_,_) -> MyCFG.unknown_exp)) [] args in pred (desc.special args) | _ -> false else false in let calls = ResettableLazy.force functionCallMaps in calls.calledBy |> FunctionCallMap.exists (fun var _ -> relevant_static var) || calls.dynamicallyCalled |> FunctionSet.exists relevant_dynamic let disableAnalyses anas = List.iter (GobConfig.set_auto "ana.activated[-]") anas let enableAnalyses anas = List.iter (GobConfig.set_auto "ana.activated[+]") anas (*If only one thread is used in the program, we can disable most thread analyses*) (*The exceptions are analyses that are depended on by others: base -> mutex -> mutexEvents, access*) (*escape is also still enabled, because otherwise we get a warning*) (*does not consider dynamic calls!*) let notNeccessaryThreadAnalyses = ["race"; "deadlock"; "maylocks"; "symb_locks"; "thread"; "threadid"; "threadJoins"; "threadreturn"] let reduceThreadAnalyses () = let isThreadCreate = function | LibraryDesc.ThreadCreate _ -> true | _ -> false in let hasThreadCreate = hasFunction isThreadCreate in if not @@ hasThreadCreate then ( print_endline @@ "no thread creation -> disabling thread analyses \"" ^ (String.concat ", " notNeccessaryThreadAnalyses) ^ "\""; disableAnalyses notNeccessaryThreadAnalyses; ) (* This is run independent of the autotuner being enabled or not to be sound in the presence of setjmp/longjmp *) (* It is done this way around to allow enabling some of these analyses also for programs without longjmp *) let longjmpAnalyses = ["activeLongjmp"; "activeSetjmp"; "taintPartialContexts"; "modifiedSinceLongjmp"; "poisonVariables"; "expsplit"; "vla"] let activateLongjmpAnalysesWhenRequired () = let isLongjmp = function | LibraryDesc.Longjmp _ -> true | _ -> false in if hasFunction isLongjmp then ( print_endline @@ "longjmp -> enabling longjmp analyses \"" ^ (String.concat ", " longjmpAnalyses) ^ "\""; enableAnalyses longjmpAnalyses; ) let focusOnSpecification () = match Svcomp.Specification.of_option () with | UnreachCall s -> () | NoDataRace -> (*enable all thread analyses*) print_endline @@ "Specification: NoDataRace -> enabling thread analyses \"" ^ (String.concat ", " notNeccessaryThreadAnalyses) ^ "\""; enableAnalyses notNeccessaryThreadAnalyses; | NoOverflow -> (*We focus on integer analysis*) set_bool "ana.int.def_exc" true; set_bool "ana.int.interval" true (*Detect enumerations and enable the "ana.int.enums" option*) exception EnumFound class enumVisitor = object inherit nopCilVisitor method! vglob = function | GEnumTag _ | GEnumTagDecl _ -> raise EnumFound; | _ -> SkipChildren; end let hasEnums file = let thisVisitor = new enumVisitor in try ignore (visitCilFileSameGlobals thisVisitor file); false; with EnumFound -> true class addTypeAttributeVisitor = object inherit nopCilVisitor (*large arrays -> partitioned*) (*because unroll only is usefull if most values are actually unrolled*) method! vvdec info = (if is_large_array info.vtype && not @@ hasAttribute "goblint_array_domain" (typeAttrs info.vtype) then info.vattr <- addAttribute (Attr ("goblint_array_domain", [AStr "partitioned"])) info.vattr); DoChildren (*Set arrays with important types for thread analysis to unroll*) method! vtype typ = let is_important_type (t: typ): bool = match t with | TNamed (info, attr) -> List.mem info.tname ["pthread_mutex_t"; "spinlock_t"; "pthread_t"] | TInt (IInt, attr) -> hasAttribute "mutex" attr | _ -> false in if is_important_type typ && not @@ hasAttribute "goblint_array_domain" (typeAttrs typ) then ChangeTo (typeAddAttributes [Attr ("goblint_array_domain", [AStr "unroll"])] typ) else SkipChildren end let selectArrayDomains file = set_bool "annotation.goblint_array_domain" true; let thisVisitor = new addTypeAttributeVisitor in ignore (visitCilFileSameGlobals thisVisitor file) (*small unrolled loops also set domain of accessed arrays to unroll, at the point where loops are unrolled*) (*option that can be selected based on value/cost*) type option = { value:int; cost:int; activate: unit -> unit } (*Option for activating the octagon apron domain on selected vars*) module VariableMap = Map.Make(CilType.Varinfo) module VariableSet = Set.Make(CilType.Varinfo) let isComparison = function | Lt | Gt | Le | Ge | Ne | Eq -> true | _ -> false let isGoblintStub v = List.exists (fun (Attr(s,_)) -> s = "goblint_stub") v.vattr let rec extractVar = function | UnOp (Neg, e, _) -> extractVar e | Lval ((Var info),_) when not (isGoblintStub info) -> Some info | _ -> None let extractOctagonVars = function | BinOp (PlusA, e1,e2, (TInt _)) | BinOp (MinusA, e1,e2, (TInt _)) -> ( match extractVar e1, extractVar e2 with | Some a, Some b -> Some (`Left (a,b)) | Some a, None | None, Some a -> if isConstant e1 then Some (`Right a) else None | _,_ -> None ) | _ -> None let addOrCreateVarMapping varMap key v globals = if key.vglob = globals then varMap := if VariableMap.mem key !varMap then let old = VariableMap.find key !varMap in VariableMap.add key (old + v) !varMap else VariableMap.add key v !varMap let handle varMap v globals = function | Some (`Left (a,b)) -> addOrCreateVarMapping varMap a v globals; addOrCreateVarMapping varMap b v globals; | Some (`Right a) -> addOrCreateVarMapping varMap a v globals; | None -> () class octagonVariableVisitor(varMap, globals) = object inherit nopCilVisitor method! vexpr = function (*an expression of type +/- a +/- b where a,b are either variables or constants*) | BinOp (op, e1,e2, (TInt _)) when isComparison op -> ( handle varMap 5 globals (extractOctagonVars e1) ; handle varMap 5 globals (extractOctagonVars e2) ; DoChildren ) | Lval ((Var info),_) when not (isGoblintStub info) -> handle varMap 1 globals (Some (`Right info)) ; SkipChildren (*Traverse down only operations fitting for linear equations*) | UnOp (Neg, _,_) | BinOp (PlusA,_,_,_) | BinOp (MinusA,_,_,_) | BinOp (Mult,_,_,_) | BinOp (LAnd,_,_,_) | BinOp (LOr,_,_,_) -> DoChildren | _ -> SkipChildren end let topVars n varMap= let compareValueDesc = (fun (_,v1) (_,v2) -> - (compare v1 v2)) in varMap |> VariableMap.bindings |> List.sort compareValueDesc |> BatList.take n |> List.map fst class octagonFunctionVisitor(list, amount) = object inherit nopCilVisitor method! vfunc f = let varMap = ref VariableMap.empty in let visitor = new octagonVariableVisitor(varMap, false) in ignore (visitCilFunction visitor f); list := topVars amount !varMap ::!list; SkipChildren end let congruenceOption factors file = let locals, globals = factors.integralVars in let cost = (locals + globals) * (factors.instructions / 12) + 5 * factors.functionCalls in let value = 5 * locals + globals in let activate () = print_endline @@ "Congruence: " ^ string_of_int cost; set_bool "ana.int.congruence" true; print_endline "Enabled congruence domain."; in { value; cost; activate; } let apronOctagonOption factors file = let locals = if List.mem "specification" (get_string_list "ana.autotune.activated" ) && get_string "ana.specification" <> "" then match Svcomp.Specification.of_option () with | NoOverflow -> 12 | _ -> 8 else 8 in let globals = 2 in let selectedLocals = let list = ref [] in let visitor = new octagonFunctionVisitor(list, locals) in visitCilFileSameGlobals visitor file; List.concat !list in let selectedGlobals = let varMap = ref VariableMap.empty in let visitor = new octagonVariableVisitor(varMap, true) in visitCilFileSameGlobals visitor file; topVars globals !varMap in let allVars = (selectedGlobals @ selectedLocals) in let cost = (Batteries.Int.pow (locals + globals) 3) * (factors.instructions / 70) in let activateVars () = print_endline @@ "Octagon: " ^ string_of_int cost; set_bool "annotation.goblint_relation_track" true; set_string "ana.apron.domain" "octagon"; set_auto "ana.activated[+]" "apron"; set_bool "ana.apron.threshold_widening" true; set_string "ana.apron.threshold_widening_constants" "comparisons"; print_endline "Enabled octagon domain for:"; print_endline @@ String.concat ", " @@ List.map (fun info -> info.vname) allVars; List.iter (fun info -> info.vattr <- addAttribute (Attr("goblint_relation_track",[])) info.vattr) allVars in { value = 50 * (List.length allVars) ; cost = cost; activate = activateVars; } let wideningOption factors file = let amountConsts = List.length @@ WideningThresholds.upper_thresholds () in let cost = amountConsts * (factors.loops * 5 + factors.controlFlowStatements) in { value = amountConsts * (factors.loops * 5 + factors.controlFlowStatements); cost = cost; activate = fun () -> print_endline @@ "Widening: " ^ string_of_int cost; set_bool "ana.int.interval_threshold_widening" true; set_string "ana.int.interval_threshold_widening_constants" "comparisons"; print_endline "Enabled widening thresholds"; } let estimateComplexity factors file = let pathsEstimate = factors.loops + factors.controlFlowStatements / 90 in let operationEstimate = factors.instructions + (factors.expressions / 60) in let callsEstimate = factors.functionCalls * factors.loops / factors.functions / 10 in let globalVars = fst factors.pointerVars * 2 + fst factors.arrayVars * 4 + fst factors.integralVars in let localVars = snd factors.pointerVars * 2 + snd factors.arrayVars * 4 + snd factors.integralVars in let varEstimates = globalVars + localVars / factors.functions in pathsEstimate * operationEstimate * callsEstimate + varEstimates / 10 let totalTarget = 30000 (*A simple greedy approximation to the knapsack problem: take options with the highest use/cost ratio that still fit*) let chooseFromOptions costTarget options = let ratio o = Float.of_int o.value /. Float.of_int o.cost in let compareRatio o1 o2 = Float.compare (ratio o1) (ratio o2) in let rec takeFitting remainingTarget options = if remainingTarget < 0 then (print_endline @@ "Total: " ^ string_of_int (totalTarget - remainingTarget); [] ) else match options with | o::os -> if o.cost < remainingTarget + costTarget / 20 then (*because we are already estimating, we allow overshooting *) o::takeFitting (remainingTarget - o.cost) os else takeFitting (remainingTarget - o.cost) os | [] -> print_endline @@ "Total: " ^ string_of_int (totalTarget - remainingTarget); [] in takeFitting costTarget @@ List.sort compareRatio options let isActivated a = get_bool "ana.autotune.enabled" && List.mem a @@ get_string_list "ana.autotune.activated" let chooseConfig file = let factors = collectFactors visitCilFileSameGlobals file in let fileCompplexity = estimateComplexity factors file in print_endline "Collected factors:"; printFactors factors; print_endline ""; print_endline "Complexity estimates:"; print_endline @@ "File: " ^ string_of_int fileCompplexity; if fileCompplexity < totalTarget && isActivated "congruence" then addModAttributes file; if isActivated "noRecursiveIntervals" then disableIntervalContextsInRecursiveFunctions (); if isActivated "mallocWrappers" then findMallocWrappers (); if isActivated "specification" && get_string "ana.specification" <> "" then focusOnSpecification (); if isActivated "enums" && hasEnums file then set_bool "ana.int.enums" true; if isActivated "singleThreaded" then reduceThreadAnalyses (); if isActivated "arrayDomain" then selectArrayDomains file; let options = [] in let options = if isActivated "congruence" then (congruenceOption factors file)::options else options in let options = if isActivated "octagon" then (apronOctagonOption factors file)::options else options in let options = if isActivated "wideningThresholds" then (wideningOption factors file)::options else options in List.iter (fun o -> o.activate ()) @@ chooseFromOptions (totalTarget - fileCompplexity) options let reset_lazy () = ResettableLazy.reset functionCallMaps
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>