This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Adding misc-signal-terminated-thread check
ClosedPublic

Authored by abelkocsis on Oct 18 2019, 10:05 AM.

Details

Summary

According to
https://wiki.sei.cmu.edu/confluence/display/c/POS44-C.+Do+not+use+signals+to+terminate+threads
misc-signal-terminated-thread check is created. The check warn if
pthread_kill function is called with SIGTERM signal.

Diff Detail

Event Timeline

abelkocsis created this revision.Oct 18 2019, 10:05 AM
Herald added a project: Restricted Project. · View Herald Transcript
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.cpp
28 ↗(On Diff #225641)

static is enough, no need for anonymous namespace.

33 ↗(On Diff #225641)

Unnecessary empty line.

37 ↗(On Diff #225641)

Please don't use auto unless type is spelled in same statement or iterator.

67 ↗(On Diff #225641)

Please add new line.

clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.h
40 ↗(On Diff #225641)

Please add new line.

clang-tools-extra/docs/ReleaseNotes.rst
82

Please keep alphabetical order in new checks list.

85

Please synchronize with first sentence in documentation. Link belongs to documentation.

clang-tools-extra/docs/clang-tidy/checks/misc-bad-signal-to-kill-thread.rst
8 ↗(On Diff #225641)

Please separate with empty line.

11 ↗(On Diff #225641)

Please add new line.

clang-tools-extra/test/clang-tidy/misc-bad-signal-to-kill-thread.cpp
31 ↗(On Diff #225641)

Please add new line.

abelkocsis removed a project: Restricted Project.Oct 19 2019, 2:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 19 2019, 2:39 AM
steakhal added inline comments.
clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.cpp
34–48 ↗(On Diff #225641)

I think using while loop here makes this less readable compared to using an algorithm like llvm::find_if.

I think a more declarative approach would help here.

  • Handles properly if the definition of SIGTERM uses other than decimal base.
  • Checks if ValueStr.getAsInteger() succeeded.

Like.:

const auto IsSigterm = [](const auto &KeyValue) -> bool {
  return KeyValue.first->getName() == "SIGTERM";
};
const auto TryExpandAsInteger =
    [PP = PP](Preprocessor::macro_iterator It) -> Optional<unsigned> {
  if (It == PP->macro_end())
    return llvm::None;
  const MacroInfo *MI = PP->getMacroInfo(It->first);
  const Token &T = MI->tokens().back();
  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());

  llvm::APInt IntValue;
  constexpr unsigned AutoSenseRadix = 0;
  if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
    return llvm::None;
  return IntValue.getZExtValue();
};

const auto SigtermMacro = llvm::find_if(PP->macros(), IsSigterm);

if (!SigtermValue && !(SigtermValue = TryExpandAsInteger(SigtermMacro)))
  return;
55 ↗(On Diff #225641)

I think it would be valuable to mention the name of the signal.

clang-tools-extra/docs/ReleaseNotes.rst
82–83

Your checker's name is different from these. Could you check these two lines please?

clang-tools-extra/docs/clang-tidy/checks/misc-bad-signal-to-kill-thread.rst
7 ↗(On Diff #225641)

It would be kind to offer a compliant solution for fixing the warning.

clang-tools-extra/test/clang-tidy/misc-bad-signal-to-kill-thread.cpp
26 ↗(On Diff #225641)

Maybe worth to note here that this would be the compliant solution.

Also check if it doesn't warn for different signals, but warns for the pthread_kill(thread, 0xF))

Thank you for the reviews!

Auto types replaced on checker files. New lines added when necessary.
Reordered ReleaseNotes by alphabetical order. Checker name is
updated on ReleaseNotes, compliant solution is wrote to the
documentation. Another two test is written.

abelkocsis marked 14 inline comments as done.Oct 19 2019, 3:45 PM
Eugene.Zelenko added inline comments.Oct 19 2019, 6:56 PM
clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.cpp
31 ↗(On Diff #225769)

In this case auto is reasonable, because this is iterator.

clang-tools-extra/docs/ReleaseNotes.rst
127

Please synchronize with first statement in documentation.

129

Please move this statement at the end of documentation.

mgehre removed a subscriber: mgehre.Oct 20 2019, 12:38 AM
abelkocsis marked an inline comment as done.

Header documentation and Release Notes synchronised. Documentation of
checker updated. Type changed to auto type when necessary.

abelkocsis marked 2 inline comments as done.Oct 20 2019, 7:47 AM
Eugene.Zelenko added inline comments.Oct 20 2019, 7:57 AM
clang-tools-extra/docs/clang-tidy/checks/misc-bad-signal-to-kill-thread.rst
9 ↗(On Diff #225784)

Usually links to documentation are located at the end of documentation. See other checks documentation as examples.

Test clang-formatted. Uncaught word removed from documentations.

The documentation of the checker updated.

abelkocsis marked an inline comment as done.Oct 20 2019, 8:07 AM

Findig SIGTERM replaced on checker

abelkocsis marked an inline comment as done.Oct 21 2019, 1:28 AM

Thank you for this! I don't think that the check should live in misc. It should have an alias in the CERT module, but maybe we want to have this live in a posix module? If not, this should probably live in bugprone.

clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.cpp
31 ↗(On Diff #225834)

You can drop the trailing return type on the lambda.

35 ↗(On Diff #225834)

This lambda capture looks suspicious -- why do you need to initialize the capture?

59 ↗(On Diff #225834)

Clang-tidy diagnostics are not supposed to be grammatically correct. I think a better way to phrase it might be something like: thread should not be terminated by raising the 'SIGTERM' signal

clang-tools-extra/docs/clang-tidy/checks/misc-bad-signal-to-kill-thread.rst
6 ↗(On Diff #225834)

when thread is terminated by -> when a thread is terminated by raising the

8 ↗(On Diff #225834)

Why does the check not look for SIGKILL as well as SIGTERM?

steakhal added inline comments.Oct 24 2019, 12:48 PM
clang-tools-extra/clang-tidy/misc/BadSignalToKillThreadCheck.cpp
28 ↗(On Diff #225834)

Can't we place this PP variable to the checker class?
Like the modernize/UseTrailingReturnTypeCheck does?

35 ↗(On Diff #225834)

You are right, I forgot that PP is a global variable when I offered this solution.
Global variables should not be captured, so empty capture is good enough.

abelkocsis marked 4 inline comments as done.

Checker moved to bugprone module, cert alias added. Small fixes in checker
file, warning message updated.

abelkocsis marked 3 inline comments as done.Oct 27 2019, 4:52 PM
abelkocsis added inline comments.
clang-tools-extra/docs/clang-tidy/checks/misc-bad-signal-to-kill-thread.rst
8 ↗(On Diff #225834)

In my opinion SIGKILL is used only in cases when the programmer wants the entire process to terminate. I updated the docs not mentioning SIGKILL.

abelkocsis marked an inline comment as done.

Small fixes

Eugene.Zelenko added inline comments.Oct 27 2019, 6:09 PM
clang-tools-extra/docs/clang-tidy/checks/bugprone-bad-signal-to-kill-thread.rst
5

Please make same length as title above.

Small fix in documentation

abelkocsis marked an inline comment as done.Oct 28 2019, 5:55 AM

You should also add actual alias code to cert module.

Alias added to cert module

Will be good idea to mention alias in Release Notes too (in aliases section, in alphabetical order).

aaron.ballman accepted this revision.Oct 29 2019, 1:27 PM

LGTM aside from the release notes alias request.

This revision is now accepted and ready to land.Oct 29 2019, 1:27 PM