This is an archive of the discontinued LLVM Phabricator instance.

Thread safety analysis: Handle compound assignment and ->* overloads
ClosedPublic

Authored by aaronpuchert on May 4 2022, 2:48 PM.

Details

Summary

Like regular assignment, compound assignment operators can be assumed to
write to their left-hand side operand. So we strengthen the requirements
there. (Previously only the default read access had been required.)

Just like operator->, operator->* can also be assumed to dereference the
left-hand side argument, so we require read access to the pointee. This
will generate new warnings if the left-hand side has a pt_guarded_by
attribute. This overload is rarely used, but it was trivial to add, so
why not. (Supporting the builtin operator requires changes to the TIL.)

Diff Detail

Event Timeline

aaronpuchert created this revision.May 4 2022, 2:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2022, 2:48 PM
aaronpuchert requested review of this revision.May 4 2022, 2:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2022, 2:48 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

@rupprecht, do you still do integration at Google? This patch might be interesting to try out, since it strengthens some warnings.

clang/test/SemaCXX/warn-thread-safety-analysis.cpp
85–86

In case you're wondering, making this a member function prevents instantiating the class with non-class types:

error: member pointer refers into non-class type 'int'
  U& operator->*(U T::*p) const { return ptr_->*p; }
                      ^
note: in instantiation of template class 'SmartPtr<int>' requested here
SmartPtr<int> p;
              ^
aaron.ballman added inline comments.May 5 2022, 4:19 AM
clang/lib/Analysis/ThreadSafety.cpp
1993

If we're going to be handling these, should we also be handling overloads of ++ and --?

clang/test/SemaCXX/warn-thread-safety-analysis.cpp
4344

We should probably add test coverage for all the newly supported operators.

aaronpuchert marked 2 inline comments as done.May 6 2022, 6:35 AM
aaronpuchert added inline comments.
clang/lib/Analysis/ThreadSafety.cpp
1993

Yes, in some sense these are also compound assignment operators despite their looks. I'll add them as well.

Sadly streams use << and >> instead of <<= and >>=, and we can't really assume the former to write.

aaronpuchert marked an inline comment as done.

Also consider ++ and -- as writing, test all operators.

Use 1 instead of 0 just to be sure. We don't want to trigger warnings about division by zero.

aaron.ballman accepted this revision.May 6 2022, 11:35 AM

LGTM! You should probably add a release note for this change so users know about it. (Do we need to update any documentation from this?)

This revision is now accepted and ready to land.May 6 2022, 11:35 AM

LGTM! You should probably add a release note for this change so users know about it.

Good point, I'll add a note about compound assignment and increment/decrement. I'll leave out operator->* though if you don't mind because it's rarely overloaded.

Do we need to update any documentation from this?

I don't think so, we don't really specify what we consider as write and what not. It's more of a heuristic anyway, unless at some point we feel comfortable enough to do D52395.

Add a release note.

aaronpuchert added inline comments.May 6 2022, 2:43 PM
clang/docs/ReleaseNotes.rst
194–204

Or maybe I should add it here? Not sure why there is a blank line.

aaron.ballman accepted this revision.May 9 2022, 4:24 AM

Do we need to update any documentation from this?

I don't think so, we don't really specify what we consider as write and what not. It's more of a heuristic anyway, unless at some point we feel comfortable enough to do D52395.

SGTM, thanks for verifying! Continues to LGTM.

clang/docs/ReleaseNotes.rst
194–204

Either way is fine -- the blank line is an accident, but it'll go away at some point.

This revision was landed with ongoing or failed builds.May 9 2022, 6:36 AM
This revision was automatically updated to reflect the committed changes.
aaronpuchert marked an inline comment as done.May 9 2022, 6:36 AM