|
| 1 | +(** Must have waited barriers for Pthread barriers ([pthreadBarriers]). *) |
| 2 | + |
| 3 | +open GoblintCil |
| 4 | +open Analyses |
| 5 | +module LF = LibraryFunctions |
| 6 | + |
| 7 | +module Spec = |
| 8 | +struct |
| 9 | + module Barriers = struct |
| 10 | + include SetDomain.ToppedSet (ValueDomain.Addr) (struct let topname = "All barriers" end) |
| 11 | + let name () = "mayBarriers" |
| 12 | + end |
| 13 | + |
| 14 | + module MustBarriers = struct |
| 15 | + include Lattice.Reverse (Barriers) |
| 16 | + let name () = "mustBarriers" |
| 17 | + end |
| 18 | + |
| 19 | + module Capacity = Queries.ID |
| 20 | + |
| 21 | + include Analyses.IdentitySpec |
| 22 | + module V = VarinfoV |
| 23 | + |
| 24 | + module TID = ThreadIdDomain.ThreadLifted |
| 25 | + |
| 26 | + (* Tracking TID separately, as there is no type-safe way to get it from MCPATuple *) |
| 27 | + module MCPAPlusTID = struct |
| 28 | + include Printable.Prod (MCPAccess.A) (TID) |
| 29 | + let name () = "MCPAPlusTID" |
| 30 | + end |
| 31 | + |
| 32 | + let part_access man: MCPAccess.A.t = |
| 33 | + Obj.obj (man.ask (PartAccess Point)) |
| 34 | + |
| 35 | + module Waiters = SetDomain.ToppedSet (MCPAPlusTID) (struct let topname = "All MHP" end) |
| 36 | + module Multiprocess = BoolDomain.MayBool |
| 37 | + module G = Lattice.Prod3 (Multiprocess) (Capacity) (Waiters) |
| 38 | + |
| 39 | + let name () = "pthreadBarriers" |
| 40 | + |
| 41 | + module MustObserved = MapDomain.MapTop_LiftBot (TID) (MustBarriers) |
| 42 | + module D = Lattice.Prod (Barriers) (MustObserved) |
| 43 | + |
| 44 | + include Analyses.ValueContexts(D) |
| 45 | + |
| 46 | + let possible_vinfos (a: Queries.ask) barrier = |
| 47 | + Queries.AD.to_var_may (a.f (Queries.MayPointTo barrier)) |
| 48 | + |
| 49 | + let special man (lval: lval option) (f:varinfo) (arglist:exp list) : D.t = |
| 50 | + let exists_k pred k waiters = |
| 51 | + let k_product elems = |
| 52 | + let rec doit k = |
| 53 | + if k = 1 then |
| 54 | + Seq.map (Seq.return) elems |
| 55 | + else |
| 56 | + let arg = doit (k-1) in |
| 57 | + Seq.map_product (Seq.cons) elems arg |
| 58 | + in |
| 59 | + doit k |
| 60 | + in |
| 61 | + Seq.exists pred (k_product (Waiters.to_seq waiters)) |
| 62 | + in |
| 63 | + let desc = LF.find f in |
| 64 | + match desc.special arglist with |
| 65 | + | BarrierWait barrier -> |
| 66 | + let ask = Analyses.ask_of_man man in |
| 67 | + let may, must = man.local in |
| 68 | + let mcpa = part_access man in |
| 69 | + let tid = ThreadId.get_current ask in |
| 70 | + let barriers = possible_vinfos ask barrier in |
| 71 | + let handle_one b = |
| 72 | + try |
| 73 | + man.sideg b (Multiprocess.bot (), Capacity.bot (), Waiters.singleton (mcpa, tid)); |
| 74 | + let addr = ValueDomain.Addr.of_var b in |
| 75 | + let (multiprocess, capacity, waiters) = man.global b in |
| 76 | + let may = Barriers.add addr may in |
| 77 | + if multiprocess then |
| 78 | + (may, must) |
| 79 | + else |
| 80 | + let relevant_waiters = Waiters.filter (fun (othermcpa, _) -> MCPAccess.A.may_race mcpa othermcpa) waiters in |
| 81 | + if Waiters.exists (fun (t,_) -> MCPAccess.A.may_race t t) relevant_waiters then |
| 82 | + (may, must) |
| 83 | + else |
| 84 | + match capacity with |
| 85 | + | `Top | `Bot -> (may, must) |
| 86 | + | `Lifted c -> |
| 87 | + let count = Waiters.cardinal relevant_waiters in |
| 88 | + (* Add 1 as the thread calling wait at the moment will not be MHP with itself *) |
| 89 | + let min_cap = (BatOption.default Z.zero (Capacity.I.minimal c)) in |
| 90 | + if Z.leq min_cap Z.one then |
| 91 | + (may, must) |
| 92 | + else if Z.geq (Z.of_int (count + 1)) min_cap then |
| 93 | + (* This is quite a cute problem: Do (min_cap-1) elements exist in the set such that |
| 94 | + MHP is pairwise true? This solution is a sledgehammer, there should be something much |
| 95 | + better algorithmically (beyond just laziness) |
| 96 | + Simmo: This sounds like looking for a (min_cap - 1)-clique in the MHP graph with the set of nodes. |
| 97 | + That seems to be exponential in min_cap though: https://en.wikipedia.org/wiki/Clique_problem#Cliques_of_fixed_size. |
| 98 | + *) |
| 99 | + let must = |
| 100 | + let waiters = Waiters.elements relevant_waiters in |
| 101 | + let min_cap = Z.to_int min_cap in |
| 102 | + let can_proceed_pred seq = |
| 103 | + let rec doit seq = match Seq.uncons seq with |
| 104 | + | None -> true |
| 105 | + | Some ((h,_), t) -> Seq.for_all (fun (x,_) -> MCPAccess.A.may_race h x) t && (doit [@tailcall]) t |
| 106 | + in |
| 107 | + doit seq |
| 108 | + in |
| 109 | + let can_proceed = exists_k can_proceed_pred (min_cap - 1) relevant_waiters in |
| 110 | + if not can_proceed then raise Analyses.Deadcode; |
| 111 | + (* limit to this case to avoid having to construct all permutations above *) |
| 112 | + if BatList.compare_length_with waiters (min_cap - 1) = 0 then |
| 113 | + List.fold_left (fun acc (_,tid) -> |
| 114 | + let curr = MustObserved.find tid acc in |
| 115 | + let must' = MustObserved.add tid (Barriers.add addr curr) acc in |
| 116 | + must' |
| 117 | + ) must waiters |
| 118 | + else |
| 119 | + must |
| 120 | + in |
| 121 | + (may, must) |
| 122 | + else |
| 123 | + raise Analyses.Deadcode; |
| 124 | + with Analyses.Deadcode -> D.bot () |
| 125 | + in |
| 126 | + let (may, must) = List.fold_left (fun acc b-> D.join acc (handle_one b)) (D.bot ()) barriers in |
| 127 | + if Barriers.is_empty may then raise Analyses.Deadcode; |
| 128 | + (may, must) |
| 129 | + | BarrierInit { barrier; attr; count } -> |
| 130 | + let multiprocess = not @@ Queries.AD.is_null @@ man.ask (Queries.MayPointTo attr) in |
| 131 | + if multiprocess then M.warn "Barrier initialized with a non-NULL attr argument. Handled as if PTHREAD_PROCESS_SHARED potentially set."; |
| 132 | + let count = man.ask (Queries.EvalInt count) in |
| 133 | + let publish_one b = man.sideg b (multiprocess, count, Waiters.bot ()) in |
| 134 | + let barriers = possible_vinfos (Analyses.ask_of_man man) barrier in |
| 135 | + List.iter publish_one barriers; |
| 136 | + man.local |
| 137 | + | _ -> man.local |
| 138 | + |
| 139 | + let startstate v = (Barriers.empty (), MustObserved.empty ()) |
| 140 | + let threadenter man ~multiple lval f args = [man.local] |
| 141 | + let exitstate v = (Barriers.empty (), MustObserved.empty ()) |
| 142 | + |
| 143 | + module A = |
| 144 | + struct |
| 145 | + include Lattice.Prod3 (Barriers) (MustObserved) (TID) |
| 146 | + let name () = "barriers" |
| 147 | + let may_race (may_await_t1, must_observed_by_t1, t1) (may_await_t2, must_observed_by_t2, t2) = |
| 148 | + let observed_from_t2 = MustObserved.find t2 must_observed_by_t1 in |
| 149 | + if not (Barriers.subset observed_from_t2 may_await_t2) then |
| 150 | + false |
| 151 | + else |
| 152 | + let observed_from_t1 = MustObserved.find t1 must_observed_by_t2 in |
| 153 | + Barriers.subset observed_from_t1 may_await_t1 |
| 154 | + let should_print f = true |
| 155 | + end |
| 156 | + |
| 157 | + let access man (a: Queries.access) = |
| 158 | + let (may,must) = man.local in |
| 159 | + let mhp = MHP.current (Analyses.ask_of_man man) in |
| 160 | + (may, must, mhp.tid) |
| 161 | +end |
| 162 | + |
| 163 | +let _ = |
| 164 | + MCP.register_analysis (module Spec : MCPSpec) |
0 commit comments