================
@@ -2739,6 +3470,398 @@ bool ThreadSafetyAnalyzer::join(const FactEntry &A,
const FactEntry &B,
}
}
+namespace {
+
+/// The join of one predecessor's exit set into a block's entry set: the
+/// intersection of the two locksets, with warnings for what it loses.
+///
+/// Facts are paired by form: a capability's definite facts join through
join(),
+/// and its try facts join per identity -- two conditional try facts of one
call
+/// are identical, and try facts of different calls coexist, each still
resolved
+/// by the branch on its own result. The interplay is where one side holds the
+/// capability definitely and the other only conditionally (a "mixed" join):
the
+/// merged state keeps the conditional side's facts, and the definite side's
+/// extra level is diagnosed as lost, or -- when both sides also hold definite
+/// levels -- as a reentrancy-depth mismatch.
+///
+/// A one-sided definite hold is kept, demoted to the try-acquire's conditional
+/// try fact, under the rebranch exemption: the joining block's terminator
+/// branches on the result of the call whose ProvedHeld try fact stands beside
+/// the hold, so the paths re-diverge there and the outgoing edges re-resolve
+/// the try fact. A ProvedHeld try fact on one side only is otherwise not
+/// carried: the merged hold, if any, is not proved by that call.
+///
+/// A loop join compares against an entry set analyzed previously: it
diagnoses,
+/// but must not rewrite that set.
+class LocksetJoin {
+ ThreadSafetyAnalyzer &Analyzer;
+ FactManager &FactMan;
+ ThreadSafetyHandler &Handler;
+ const JoinContext &Ctx;
+ /// The entry set being computed, and a copy of it as it was before this
join:
+ /// the pairing reads the original while the merged set is rewritten in
place.
+ FactSet &EntrySet;
+ const FactSet EntrySetOrig;
+ const FactSet &ExitSet;
+
+public:
+ LocksetJoin(ThreadSafetyAnalyzer &Analyzer, FactSet &EntrySet,
+ const FactSet &ExitSet, const JoinContext &Ctx)
+ : Analyzer(Analyzer), FactMan(Analyzer.FactMan),
+ Handler(Analyzer.Handler), Ctx(Ctx), EntrySet(EntrySet),
+ EntrySetOrig(EntrySet), ExitSet(ExitSet) {}
+
+ void run();
+
+private:
+ /// \name Exemptions
+ /// \{
+ bool isTrylockRebranched(const TryFactEntry &W) const {
+ return Ctx.RebranchTryLock && W.origin() == Ctx.RebranchTryLock;
+ }
+ const TryFactEntry *rebranchProof(const FactSet &Set,
+ const FactEntry &Def) const;
+ bool mixedJoinExempt(const FactSet &DefSet, const FactEntry &DefSide,
+ const FactSet &CondSet) const;
+ /// \}
+
+ /// \name Diagnostics
+ /// \{
+ void warnRemovedEntryFact(const FactEntry &EntryFact) const;
+ void warnRemovedExitFact(const FactEntry &ExitFact) const;
+ void warnReentrancyMismatch(const FactEntry &FE, LockErrorKind LEK) const;
+ static unsigned depth(const FactEntry *Def, bool HasCond);
+ /// \}
+
+ /// \name Rewriting the merged set
+ /// \{
+ const FactEntry *demoteToConditional(const FactEntry &Def, LockErrorKind
LEK);
+ void demoteExitFact(const FactEntry &Def, LockErrorKind LEK);
+ void demoteEntryFactInPlace(const FactEntry &Def, LockErrorKind LEK);
+ /// \}
+
+ /// \name The joins, by form
+ /// A fact of the exit set is paired with its counterpart in the entry
+ /// set (a "pair"), or is one-sided ("from exit"); a fact of the entry
+ /// set without a counterpart is one-sided the other way ("from entry").
+ /// \{
+ void joinTryFactPair(FactSet::iterator EntryIt, const TryFactEntry &ExitW);
+ void joinTryFactFromExit(FactID Fact, const TryFactEntry &ExitW);
+ void joinTryFactFromEntry(const TryFactEntry &EntryW);
+ void joinDefinitePair(FactSet::iterator EntryIt, FactID Fact,
+ const FactEntry &ExitFact);
+ void joinDefiniteFromExit(FactID Fact, const FactEntry &ExitFact);
+ void joinDefiniteFromEntry(const FactEntry &EntryFact);
+ void joinMixedPair(FactSet::iterator EntryIt, FactID Fact,
+ const FactEntry &EntryFact, const FactEntry &ExitFact,
+ bool ExitHasCond);
+ void joinMixedFromExit(FactID Fact, const FactEntry &ExitFact,
+ bool ExitHasCond);
+ void joinMixedFromEntry(const FactEntry &EntryFact, bool EntryHasCond);
+ /// \}
+};
+
+} // namespace
+
+// The rebranched call's ProvedHeld try fact beside the hold \p Def in
+// \p Set, the side \p Def belongs to: the hold was proved by the call the
+// terminator branches on, so the edges can re-resolve it.
+const TryFactEntry *LocksetJoin::rebranchProof(const FactSet &Set,
+ const FactEntry &Def) const {
+ if (!Ctx.RebranchTryLock)
+ return nullptr;
+ const TryFactEntry *W =
+ Set.findTryFact(FactMan, Def, Ctx.RebranchTryLock, Def.kind());
+ return W && W->provedHeld() ? W : nullptr;
+}
+
+// The mixed join is forgiven when the definite side's extra level was
+// proved by the call the terminator rebranches on and the other side
+// holds that call's conditional try fact: the merged state re-resolves it.
+bool LocksetJoin::mixedJoinExempt(const FactSet &DefSet,
+ const FactEntry &DefSide,
+ const FactSet &CondSet) const {
+ if (!rebranchProof(DefSet, DefSide))
+ return false;
+ const TryFactEntry *W = CondSet.findTryFact(
+ FactMan, DefSide, Ctx.RebranchTryLock, DefSide.kind());
+ return W && W->conditional();
+}
+
+// Warn about a fact the intersection removes (or weakens to conditional).
+// However, a capability managed by a scoped object is exempt -- the
+// scoped fact still knows to release it -- except where the scope itself
+// ends or repeats.
+void LocksetJoin::warnRemovedEntryFact(const FactEntry &EntryFact) const {
+ if (!EntryFact.managed() || Ctx.ExitLEK == LEK_LockedSomeLoopIterations ||
+ Ctx.ExitLEK == LEK_NotLockedAtEndOfFunction)
+ EntryFact.handleRemovalFromIntersection(EntrySetOrig, FactMan, Ctx.JoinLoc,
+ Ctx.ExitLEK, Handler);
+}
+
+void LocksetJoin::warnRemovedExitFact(const FactEntry &ExitFact) const {
+ if (!ExitFact.managed() || Ctx.EntryLEK == LEK_LockedAtEndOfFunction)
+ ExitFact.handleRemovalFromIntersection(ExitSet, FactMan, Ctx.JoinLoc,
+ Ctx.EntryLEK, Handler);
+}
+
+// Diagnose a join where only the guaranteed depth of the hold differs.
+void LocksetJoin::warnReentrancyMismatch(const FactEntry &FE,
+ LockErrorKind LEK) const {
+ Handler.handleMutexHeldEndOfScope(FE.getKind(), FE.toString(), FE.loc(),
+ Ctx.JoinLoc, LEK,
+ /*ReentrancyMismatch=*/true);
+}
+
+// The total number of levels a side holds, definite or conditional: what
+// a reentrancy-depth comparison of the two sides sees. A ProvedHeld
+// try fact adds nothing: its level is the definite fact's.
+unsigned LocksetJoin::depth(const FactEntry *Def, bool HasCond) {
+ return (Def ? reentrancyDepth(*Def) + 1 : 0) + (HasCond ? 1 : 0);
+}
+
+// Demote the definite hold \p Def, proved by the rebranched call, to that
+// call's conditional try fact in the entry set, which a branch on the
+// result resolves. A mismatched reentrancy depth is diagnosed here but
+// kept -- after the warning, the deeper fact guards more of the releases
+// downstream than a stripped one would -- as the levels below the demoted
+// one, no longer proved by any call: returned for the caller to place,
+// since \p Def itself may belong to the other side.
+const FactEntry *LocksetJoin::demoteToConditional(const FactEntry &Def,
+ LockErrorKind LEK) {
+ const auto &LDef = cast<LockableFactEntry>(Def);
+ const Expr *Origin = Ctx.RebranchTryLock;
+ if (LDef.getReentrancyDepth() != 0)
+ warnReentrancyMismatch(Def, LEK);
+ // The call's try fact in the merged set is conditional: the proof it
+ // gave on its side does not hold on the other.
+ if (FactSet::iterator It =
+ EntrySet.findTryFactIter(FactMan, Def, Origin, Def.kind());
+ It != EntrySet.end()) {
+ const auto &W = cast<TryFactEntry>(FactMan[*It]);
+ if (!W.conditional())
+ EntrySet.replaceFact(
+ FactMan, It, W.withState(FactMan, TryFactEntry::State::Conditional));
+ } else {
+ EntrySet.addLock(FactMan, LDef.asConditional(FactMan, Origin));
+ }
+ return LDef.leaveReentrant(FactMan);
+}
+
+// Demote the exit set's hold \p Def into the entry set, adding the levels
+// below it beside the conditional try fact.
+void LocksetJoin::demoteExitFact(const FactEntry &Def, LockErrorKind LEK) {
+ if (const FactEntry *Rest = demoteToConditional(Def, LEK))
+ EntrySet.addLock(FactMan, Rest);
+}
+
+// Demote the entry set's own hold \p Def in place: the levels below it
+// take its slot, or it is removed.
+void LocksetJoin::demoteEntryFactInPlace(const FactEntry &Def,
+ LockErrorKind LEK) {
+ if (const FactEntry *Rest = demoteToConditional(Def, LEK))
+ EntrySet.replaceFact(FactMan, Def, Rest);
+ else
+ EntrySet.removeFact(FactMan, Def);
+}
+
+// Two try facts of one identity: the merged state is their join.
+void LocksetJoin::joinTryFactPair(FactSet::iterator EntryIt,
+ const TryFactEntry &ExitW) {
+ const auto &EntryW = cast<TryFactEntry>(FactMan[*EntryIt]);
+ const TryFactEntry::State Merged =
+ TryFactEntry::joinStates(EntryW.state(), ExitW.state());
+ if (Merged != EntryW.state() && Ctx.canModify())
+ EntrySet.replaceFact(FactMan, EntryIt, EntryW.withState(FactMan, Merged));
+}
+
+// A try fact of the exit set without a counterpart. A conditional one is
+// carried into the merged state whenever the other side holds the capability
in
+// any form (the other side's extra is what gets diagnosed), or the terminator
+// rebranches on its call. Missing from a path that does not hold the
capability
+// at all, the analysis loses track of it: this predecessor carries a
+// try-acquire result into the join (or to the end of the function) without its
+// result having been checked. A ProvedHeld try fact is never carried
one-sided:
+// the merged hold, if any, is not proved by its call, and the definite join
+// demotes it to conditional where the rebranch exemption applies.
+void LocksetJoin::joinTryFactFromExit(FactID Fact, const TryFactEntry &ExitW) {
+ if (!ExitW.conditional() || !Ctx.canModify())
+ return;
+ if (EntrySetOrig.findDefinite(FactMan, ExitW) ||
+ EntrySetOrig.anyConditional(FactMan, ExitW) ||
isTrylockRebranched(ExitW))
+ EntrySet.addLockByID(Fact);
+}
+
+// A try fact of the entry set without a counterpart: as above, a conditional
+// one is kept wherever the other side holds the capability in any form or the
+// terminator rebranches on its call, and lost otherwise -- with the unchecked
+// try-acquire on an earlier predecessor. A one-sided ProvedHeld try fact
proves
+// nothing on the other path and is dropped (unless the definite join already
+// turned it conditional, in which case it is no longer here to drop).
+void LocksetJoin::joinTryFactFromEntry(const TryFactEntry &EntryW) {
+ if (Ctx.ExitLEK != LEK_LockedSomePredecessors)
+ return; // Only a branch join rewrites the entry set.
+ if (EntryW.conditional() &&
+ (ExitSet.findDefinite(FactMan, EntryW) ||
+ ExitSet.anyConditional(FactMan, EntryW) || isTrylockRebranched(EntryW)))
+ return;
+ EntrySet.removeFact(FactMan, EntryW);
+}
+
+// Two definite facts of a capability: a mixed join if exactly one side also
+// holds it conditionally, else joined. A ProvedHeld try fact on one side only
+// is dropped by the try fact joins: the merged hold is not proved by that
call.
+void LocksetJoin::joinDefinitePair(FactSet::iterator EntryIt, FactID Fact,
+ const FactEntry &ExitFact) {
+ const FactEntry &EntryFact = FactMan[*EntryIt];
+ const bool ExitHasCond = ExitSet.anyConditional(FactMan, ExitFact);
+ const bool EntryHasCond = EntrySetOrig.anyConditional(FactMan, ExitFact);
+ if (EntryHasCond != ExitHasCond)
+ joinMixedPair(EntryIt, Fact, EntryFact, ExitFact, ExitHasCond);
+ else if (Analyzer.join(EntryFact, ExitFact, Ctx.JoinLoc, Ctx.EntryLEK))
+ *EntryIt = Fact;
+}
+
+// Mixed, both sides definite: one side's hold is one conditional level deeper.
+// Forgiven when the definite side's extra level was proved by the call the
+// terminator rebranches on and the other side holds that call's conditional
try
+// fact: the merged state re-resolves it. Otherwise the definite side's fact is
+// diagnosed as not held on the other path. The merged state is the conditional
+// side's.
+void LocksetJoin::joinMixedPair(FactSet::iterator EntryIt, FactID Fact,
+ const FactEntry &EntryFact,
+ const FactEntry &ExitFact, bool ExitHasCond) {
+ const FactEntry &DefSide = ExitHasCond ? EntryFact : ExitFact;
+ const FactSet &DefSet = ExitHasCond ? EntrySetOrig : ExitSet;
+ const FactSet &CondSet = ExitHasCond ? ExitSet : EntrySetOrig;
+ if (!mixedJoinExempt(DefSet, DefSide, CondSet)) {
+ if (ExitHasCond)
+ warnRemovedEntryFact(EntryFact);
+ else
+ warnRemovedExitFact(ExitFact);
+ } else if (Ctx.canModify() &&
+ depth(&EntryFact, !ExitHasCond) != depth(&ExitFact, ExitHasCond))
{
+ warnReentrancyMismatch(ExitFact, Ctx.EntryLEK);
+ }
+ if (Ctx.canModify() && ExitHasCond)
+ *EntryIt = Fact;
+}
+
+// A definite hold of this predecessor only: a mixed join if the other side
+// holds the capability conditionally; else demoted to conditional under the
+// rebranch exemption; else lost -- silently when its own side also holds the
+// capability conditionally (this predecessor carries a try-acquire result into
+// the join without its result having been checked), and with the default
+// lost-capability diagnostic otherwise.
+void LocksetJoin::joinDefiniteFromExit(FactID Fact, const FactEntry &ExitFact)
{
+ const bool ExitHasCond = ExitSet.anyConditional(FactMan, ExitFact);
+ if (EntrySetOrig.anyConditional(FactMan, ExitFact)) {
+ joinMixedFromExit(Fact, ExitFact, ExitHasCond);
+ return;
+ }
+ if (rebranchProof(ExitSet, ExitFact)) {
+ // Held on this predecessor only, but the terminator rebranches on
+ // the try-acquire that proved it: demote it to conditional without
+ // warning, as getEdgeLockset() will re-resolve it on the outgoing
+ // edges.
+ if (Ctx.canModify())
+ demoteExitFact(ExitFact, Ctx.EntryLEK);
+ return;
+ }
+ // The hold is lost on the other path, whether or not the capability is
+ // also conditionally held here: a possible hold beside it does not make a
+ // certain one any less lost.
+ warnRemovedExitFact(ExitFact);
+}
+
+// A definite hold of the entry set only: as above, but a demotion keeps the
+// fact in the intersection in its demoted conditionally held form (except at a
+// loop join, where the entry set is left unmodified).
+void LocksetJoin::joinDefiniteFromEntry(const FactEntry &EntryFact) {
+ const bool EntryHasCond = EntrySetOrig.anyConditional(FactMan, EntryFact);
+ if (ExitSet.anyConditional(FactMan, EntryFact)) {
+ joinMixedFromEntry(EntryFact, EntryHasCond);
+ return;
+ }
+ if (rebranchProof(EntrySetOrig, EntryFact)) {
+ if (Ctx.canModify())
+ demoteEntryFactInPlace(EntryFact, Ctx.ExitLEK);
+ return;
+ }
+ warnRemovedEntryFact(EntryFact);
+ if (Ctx.ExitLEK == LEK_LockedSomePredecessors)
+ EntrySet.removeFact(FactMan, EntryFact);
+}
+
+// Mixed, the exit side's hold meeting only conditional try facts on the entry
+// side. With the exit side holding definitely alone: forgiven under
+// mixedJoinExempt(), else diagnosed as not held on the other path; the merged
+// state is the other side's. With both sides holding conditionally and this
one
+// definitely as well: a reentrancy-depth mismatch; keep the deeper state, to
+// minimize follow-on warnings.
+void LocksetJoin::joinMixedFromExit(FactID Fact, const FactEntry &ExitFact,
+ bool ExitHasCond) {
+ if (!ExitHasCond) {
+ if (!mixedJoinExempt(ExitSet, ExitFact, EntrySetOrig))
+ warnRemovedExitFact(ExitFact);
+ else if (Ctx.canModify() && reentrancyDepth(ExitFact) != 0)
+ warnReentrancyMismatch(ExitFact, Ctx.EntryLEK);
+ return;
+ }
+ warnReentrancyMismatch(ExitFact, Ctx.EntryLEK);
+ if (Ctx.canModify())
+ EntrySet.addLockByID(Fact);
+}
+
+// Mixed, the entry side's hold meeting only conditional try facts on the exit
+// side (kept by joinTryFactFromExit()). With the entry side holding definitely
+// alone: diagnosed unless forgiven, and giving way to them. With both sides
+// conditional and this one definitely deeper: diagnosed from the exit side's
+// view by joinMixedFromExit() too; the deeper state is kept.
+void LocksetJoin::joinMixedFromEntry(const FactEntry &EntryFact,
+ bool EntryHasCond) {
+ if (EntryHasCond) {
+ warnReentrancyMismatch(EntryFact, Ctx.ExitLEK);
+ return;
+ }
+ if (!mixedJoinExempt(EntrySetOrig, EntryFact, ExitSet))
+ warnRemovedEntryFact(EntryFact);
+ else if (Ctx.canModify() && reentrancyDepth(EntryFact) != 0)
+ warnReentrancyMismatch(EntryFact, Ctx.ExitLEK);
+ if (Ctx.ExitLEK == LEK_LockedSomePredecessors)
+ EntrySet.removeFact(FactMan, EntryFact);
+}
+
+void LocksetJoin::run() {
+ // Facts of the exit set: paired with their counterpart in the entry set, and
+ // joined or handled one-sided.
+ for (FactID Fact : ExitSet) {
+ const FactEntry &ExitFact = FactMan[Fact];
+ FactSet::iterator EntryIt = EntrySet.findCounterpartIter(FactMan,
ExitFact);
+ if (const auto *W = dyn_cast<TryFactEntry>(&ExitFact)) {
+ if (EntryIt != EntrySet.end())
+ joinTryFactPair(EntryIt, *W);
+ else
+ joinTryFactFromExit(Fact, *W);
+ } else if (EntryIt != EntrySet.end()) {
+ joinDefinitePair(EntryIt, Fact, ExitFact);
+ } else {
+ joinDefiniteFromExit(Fact, ExitFact);
+ }
+ }
+
+ // Facts of the entry set without a counterpart in the exit set.
+ for (FactID Fact : EntrySetOrig) {
+ const FactEntry &EntryFact = FactMan[Fact];
+ if (ExitSet.findCounterpart(FactMan, EntryFact))
+ continue;
+ if (const auto *W = dyn_cast<TryFactEntry>(&EntryFact))
+ joinTryFactFromEntry(*W);
+ else
+ joinDefiniteFromEntry(EntryFact);
+ }
+}
----------------
aaronpuchert wrote:
Also here: the join logic seems extremely complex. Perhaps we should organize
this around some form of ternary logic instead of writing down all cases by
hand.
https://github.com/llvm/llvm-project/pull/241550
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits