|
| 1 | +(** creation lockset analysis [creationLockset] |
| 2 | + constructs edges on the graph over all threads, where the edges are labelled with must-locksets: |
| 3 | + (t_1) ---L--> (t_0) is represented by global t_1 t_0 = L and means that t_1 is protected by all members of L from t_0 |
| 4 | +
|
| 5 | + @see https://github.com/goblint/analyzer/pull/1865 |
| 6 | +*) |
| 7 | + |
| 8 | +open Analyses |
| 9 | +module TID = ThreadIdDomain.Thread |
| 10 | +module TIDs = ConcDomain.ThreadSet |
| 11 | +module Lock = LockDomain.MustLock |
| 12 | +module Lockset = LockDomain.MustLockset |
| 13 | + |
| 14 | +module Spec = struct |
| 15 | + include IdentityUnitContextsSpec |
| 16 | + module D = Lattice.Unit |
| 17 | + |
| 18 | + module V = struct |
| 19 | + include TID |
| 20 | + include StdV |
| 21 | + end |
| 22 | + |
| 23 | + module G = MapDomain.MapBot (TID) (Lockset) |
| 24 | + |
| 25 | + let name () = "creationLockset" |
| 26 | + let startstate _ = D.bot () |
| 27 | + let exitstate _ = D.bot () |
| 28 | + |
| 29 | + (** register a global contribution: global.[child_tid] \supseteq [to_contribute] |
| 30 | + @param man man at program point |
| 31 | + @param to_contribute new edges from [child_tid] to ego thread to register |
| 32 | + @param child_tid |
| 33 | + *) |
| 34 | + let contribute_locks man to_contribute child_tid = man.sideg child_tid to_contribute |
| 35 | + |
| 36 | + (** reflexive-transitive closure of child relation applied to [tid] |
| 37 | + and filtered to only include threads, where [tid] is a must-ancestor |
| 38 | + @param man any man |
| 39 | + @param tid |
| 40 | + *) |
| 41 | + let must_ancestor_descendants_closure man tid = |
| 42 | + let descendants = man.ask @@ Queries.DescendantThreads tid in |
| 43 | + let must_ancestors_descendants = TIDs.filter (TID.must_be_ancestor tid) descendants in |
| 44 | + TIDs.add tid must_ancestors_descendants |
| 45 | + |
| 46 | + let threadspawn man ~multiple lval f args fman = |
| 47 | + let tid_lifted = man.ask Queries.CurrentThreadId in |
| 48 | + let child_tid_lifted = fman.ask Queries.CurrentThreadId in |
| 49 | + match tid_lifted, child_tid_lifted with |
| 50 | + | `Lifted tid, `Lifted child_tid when TID.must_be_ancestor tid child_tid -> |
| 51 | + let must_ancestor_descendants = must_ancestor_descendants_closure fman child_tid in |
| 52 | + let lockset = man.ask Queries.MustLockset in |
| 53 | + let to_contribute = G.singleton tid lockset in |
| 54 | + TIDs.iter (contribute_locks man to_contribute) must_ancestor_descendants |
| 55 | + | _ -> () |
| 56 | + |
| 57 | + (** compute all descendant threads that may run along with the ego thread at a program point. |
| 58 | + for all of them, tid must be an ancestor |
| 59 | + @param man man of ego thread at the program point |
| 60 | + @param tid ego thread id |
| 61 | + *) |
| 62 | + let get_must_ancestor_running_descendants man tid = |
| 63 | + let may_created_tids = man.ask Queries.CreatedThreads in |
| 64 | + let may_must_ancestor_created_tids = |
| 65 | + TIDs.filter (TID.must_be_ancestor tid) may_created_tids |
| 66 | + in |
| 67 | + let may_transitively_created_tids = |
| 68 | + TIDs.fold |
| 69 | + (fun child_tid acc -> TIDs.union acc (must_ancestor_descendants_closure man child_tid)) |
| 70 | + may_must_ancestor_created_tids |
| 71 | + (TIDs.empty ()) |
| 72 | + in |
| 73 | + let must_joined_tids = man.ask Queries.MustJoinedThreads in |
| 74 | + TIDs.diff may_transitively_created_tids must_joined_tids |
| 75 | + |
| 76 | + (** handle unlock of mutex [lock] *) |
| 77 | + let unlock man tid possibly_running_tids lock = |
| 78 | + let shrink_locksets des_tid = |
| 79 | + let old_creation_lockset = G.find tid (man.global des_tid) in |
| 80 | + (* Bot - {something} = Bot. This is exactly what we want in this case! *) |
| 81 | + let updated_creation_lockset = Lockset.remove lock old_creation_lockset in |
| 82 | + let to_contribute = G.singleton tid updated_creation_lockset in |
| 83 | + man.sideg des_tid to_contribute |
| 84 | + in |
| 85 | + TIDs.iter shrink_locksets possibly_running_tids |
| 86 | + |
| 87 | + (** handle unlock of an unknown mutex. Assumes that any mutex could have been unlocked *) |
| 88 | + let unknown_unlock man tid possibly_running_tids = |
| 89 | + let evaporate_locksets des_tid = |
| 90 | + let to_contribute = G.singleton tid (Lockset.empty ()) in |
| 91 | + man.sideg des_tid to_contribute |
| 92 | + in |
| 93 | + TIDs.iter evaporate_locksets possibly_running_tids |
| 94 | + |
| 95 | + let event man e _ = |
| 96 | + match e with |
| 97 | + | Events.Unlock addr -> |
| 98 | + let tid_lifted = man.ask Queries.CurrentThreadId in |
| 99 | + (match tid_lifted with |
| 100 | + | `Lifted tid -> |
| 101 | + let possibly_running_tids = get_must_ancestor_running_descendants man tid in |
| 102 | + let lock_opt = LockDomain.MustLock.of_addr addr in |
| 103 | + (match lock_opt with |
| 104 | + | Some lock -> unlock man tid possibly_running_tids lock |
| 105 | + | None -> unknown_unlock man tid possibly_running_tids) |
| 106 | + | _ -> ()) |
| 107 | + | _ -> () |
| 108 | + |
| 109 | + module A = struct |
| 110 | + (** ego tid * must-lockset * creation-lockset *) |
| 111 | + include Printable.Prod3 (TID) (Lockset) (G) |
| 112 | + |
| 113 | + let name () = "creationLockset" |
| 114 | + |
| 115 | + (** checks if [cl1] has a mapping ([tp1] |-> [ls1]) |
| 116 | + such that [ls1] and [ls2] are not disjoint and [tp1] != [t2] |
| 117 | + @param cl1 creation-lockset of thread [t1] at first program point |
| 118 | + @param t2 thread id at second program point |
| 119 | + @param ls2 lockset at second program point |
| 120 | + @returns whether [t1] must be running mutually exclusive with second program point |
| 121 | + *) |
| 122 | + let one_protected_inter_threaded_other_intra_threaded cl1 t2 ls2 = |
| 123 | + G.exists (fun tp1 ls1 -> not (Lockset.disjoint ls1 ls2 || TID.equal tp1 t2)) cl1 |
| 124 | + |
| 125 | + (** checks if [cl1] has a member ([tp1] |-> [ls1]) and [cl2] has a member ([tp2] |-> [ls2]) |
| 126 | + such that [ls1] and [ls2] are not disjoint and [tp1] != [tp2] |
| 127 | + @param cl1 creation-lockset of first thread [t1] |
| 128 | + @param cl2 creation-lockset of second thread [t2] |
| 129 | + @returns whether [t1] and [t2] must be running mutually exclusive |
| 130 | + *) |
| 131 | + let both_protected_inter_threaded cl1 cl2 = |
| 132 | + G.exists (one_protected_inter_threaded_other_intra_threaded cl1) cl2 |
| 133 | + |
| 134 | + let may_race (t1, ls1, cl1) (t2, ls2, cl2) = |
| 135 | + not |
| 136 | + (both_protected_inter_threaded cl1 cl2 |
| 137 | + || one_protected_inter_threaded_other_intra_threaded cl1 t2 ls2 |
| 138 | + || one_protected_inter_threaded_other_intra_threaded cl2 t1 ls1) |
| 139 | + |
| 140 | + let should_print (_t, _ls, cl) = not @@ G.is_empty cl |
| 141 | + end |
| 142 | + |
| 143 | + let access man _ = |
| 144 | + let tid_lifted = man.ask Queries.CurrentThreadId in |
| 145 | + match tid_lifted with |
| 146 | + | `Lifted tid -> |
| 147 | + let lockset = man.ask Queries.MustLockset in |
| 148 | + let creation_lockset = man.global tid in |
| 149 | + tid, lockset, creation_lockset |
| 150 | + | _ -> ThreadIdDomain.UnknownThread, Lockset.empty (), G.empty () |
| 151 | +end |
| 152 | + |
| 153 | +let _ = |
| 154 | + MCP.register_analysis |
| 155 | + ~dep:[ "threadid"; "mutex"; "threadJoins"; "threadDescendants" ] |
| 156 | + (module Spec : MCPSpec) |
0 commit comments