forked from GPUOpen-Drivers/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged master:49fb4a96e0b into amd-gfx:1613c2ba305
Local branch amd-gfx 1613c2b Merged master:fdf3d1766bb into amd-gfx:82c58d0dd35 Remote branch master 49fb4a9 change LLVM_VERSION_SUFFIX default from svn to git
- Loading branch information
Showing
36 changed files
with
235 additions
and
136 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===--- BadSignalToKillThreadCheck.cpp - clang-tidy ---------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "BadSignalToKillThreadCheck.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace bugprone { | ||
|
||
void BadSignalToKillThreadCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
callExpr(allOf(callee(functionDecl(hasName("::pthread_kill"))), | ||
argumentCountIs(2)), | ||
hasArgument(1, integerLiteral().bind("integer-literal"))) | ||
.bind("thread-kill"), | ||
this); | ||
} | ||
|
||
static Preprocessor *PP; | ||
|
||
void BadSignalToKillThreadCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto IsSigterm = [](const auto &KeyValue) -> bool { | ||
return KeyValue.first->getName() == "SIGTERM"; | ||
}; | ||
const auto TryExpandAsInteger = | ||
[](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; | ||
|
||
const auto *MatchedExpr = Result.Nodes.getNodeAs<Expr>("thread-kill"); | ||
const auto *MatchedIntLiteral = | ||
Result.Nodes.getNodeAs<IntegerLiteral>("integer-literal"); | ||
if (MatchedIntLiteral->getValue() == *SigtermValue) { | ||
diag(MatchedExpr->getBeginLoc(), | ||
"thread should not be terminated by raising the 'SIGTERM' signal"); | ||
} | ||
} | ||
|
||
void BadSignalToKillThreadCheck::registerPPCallbacks( | ||
const SourceManager &SM, Preprocessor *pp, Preprocessor *ModuleExpanderPP) { | ||
PP = pp; | ||
} | ||
|
||
} // namespace bugprone | ||
} // namespace tidy | ||
} // namespace clang |
37 changes: 37 additions & 0 deletions
37
clang-tools-extra/clang-tidy/bugprone/BadSignalToKillThreadCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===--- BadSignalToKillThreadCheck.h - clang-tidy --------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BADSIGNALTOKILLTHREADCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BADSIGNALTOKILLTHREADCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace bugprone { | ||
|
||
/// Finds ``pthread_kill`` function calls when thread is terminated by | ||
/// ``SIGTERM`` signal. | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-bad-signal-to-kill-thread.html | ||
class BadSignalToKillThreadCheck : public ClangTidyCheck { | ||
public: | ||
BadSignalToKillThreadCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, | ||
Preprocessor *ModuleExpanderPP) override; | ||
Optional<unsigned> SigtermValue; | ||
}; | ||
|
||
} // namespace bugprone | ||
} // namespace tidy | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BADSIGNALTOKILLTHREADCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
clang-tools-extra/docs/clang-tidy/checks/bugprone-bad-signal-to-kill-thread.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.. title:: clang-tidy - bugprone-bad-signal-to-kill-thread | ||
|
||
bugprone-bad-signal-to-kill-thread | ||
================================== | ||
|
||
Finds ``pthread_kill`` function calls when a thread is terminated by | ||
raising ``SIGTERM`` signal and the signal kills the entire process, not | ||
just the individual thread. Use any signal except ``SIGTERM``. | ||
|
||
.. code-block: c++ | ||
pthread_kill(thread, SIGTERM); | ||
This check corresponds to the CERT C Coding Standard rule | ||
`POS44-C. Do not use signals to terminate threads | ||
<https://wiki.sei.cmu.edu/confluence/display/c/POS44-C.+Do+not+use+signals+to+terminate+threads>`_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.. title:: clang-tidy - cert-pos44-c | ||
.. meta:: | ||
:http-equiv=refresh: 5;URL=bugprone-bad-signal-to-kill-thread.html | ||
|
||
cert-pos44-c | ||
============ | ||
|
||
The cert-pos44-c check is an alias, please see | ||
`bugprone-bad-signal-to-kill-thread <bugprone-bad-signal-to-kill-thread.html>`_ for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
clang-tools-extra/test/clang-tidy/bugprone-bad-signal-to-kill-thread.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// RUN: %check_clang_tidy %s bugprone-bad-signal-to-kill-thread %t | ||
|
||
#define SIGTERM 15 | ||
#define SIGINT 2 | ||
using pthread_t = int; | ||
using pthread_attr_t = int; | ||
|
||
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, | ||
void *(*start_routine)(void *), void *arg); | ||
|
||
int pthread_kill(pthread_t thread, int sig); | ||
|
||
int pthread_cancel(pthread_t thread); | ||
|
||
void *test_func_return_a_pointer(void *foo); | ||
|
||
int main() { | ||
int result; | ||
pthread_t thread; | ||
|
||
if ((result = pthread_create(&thread, nullptr, test_func_return_a_pointer, 0)) != 0) { | ||
} | ||
if ((result = pthread_kill(thread, SIGTERM)) != 0) { | ||
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: thread should not be terminated by raising the 'SIGTERM' signal [bugprone-bad-signal-to-kill-thread] | ||
} | ||
|
||
//compliant solution | ||
if ((result = pthread_cancel(thread)) != 0) { | ||
} | ||
|
||
if ((result = pthread_kill(thread, SIGINT)) != 0) { | ||
} | ||
if ((result = pthread_kill(thread, 0xF)) != 0) { | ||
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: thread should not be terminated by raising the 'SIGTERM' signal [bugprone-bad-signal-to-kill-thread] | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.