Skip to content

Commit 8ba2a33

Browse files
committed
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.4 [skip ci]
2 parents 704a45d + b9f2c16 commit 8ba2a33

File tree

1,238 files changed

+68393
-14640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,238 files changed

+68393
-14640
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5432,6 +5432,17 @@ uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
54325432
if (BD && BD->isMoved())
54335433
return BD->getOutputAddress();
54345434

5435+
if (const BinaryFunction *BF =
5436+
BC->getBinaryFunctionContainingAddress(OldAddress)) {
5437+
if (BF->isEmitted()) {
5438+
BC->errs() << "BOLT-ERROR: unable to get new address corresponding to "
5439+
"input address 0x"
5440+
<< Twine::utohexstr(OldAddress) << " in function " << *BF
5441+
<< ". Consider adding this function to --skip-funcs=...\n";
5442+
exit(1);
5443+
}
5444+
}
5445+
54355446
return 0;
54365447
}
54375448

bolt/test/RISCV/unnamed-sym-no-entry.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// Verify that the binary indeed contains an unnamed symbol at _start
99
// RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-ELF
1010
// CHECK-ELF-DAG: [[#%x,START:]] {{.*}} FUNC GLOBAL DEFAULT [[#%d,SECTION:]] _start{{$}}
11-
// CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] {{$}}
11+
// CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] .L0 {{$}}
1212

1313
/// Verify that BOLT did not create an extra entry point for the unnamed symbol
1414
// RUN: llvm-bolt -o %t.bolt %t --print-cfg | FileCheck %s

bolt/test/X86/indirect-goto-pie.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Check that llvm-bolt fails to process PIC binaries with computed goto, as the
2+
# support is not there yet for correctly updating dynamic relocations
3+
# referencing code inside functions.
4+
5+
REQUIRES: x86_64-linux
6+
7+
RUN: %clang %S/Inputs/indirect_goto.c -o %t -fpic -pie -Wl,-q
8+
RUN: not llvm-bolt %t -o %t.bolt --relocs=1 --print-cfg --print-only=main \
9+
RUN: |& FileCheck %s
10+
11+
# Check that processing works if main() is skipped.
12+
RUN: llvm-bolt %t -o %t.bolt --relocs=1 --skip-funcs=main
13+
14+
CHECK: jmpq *%rax # UNKNOWN CONTROL FLOW
15+
16+
CHECK: BOLT-ERROR: unable to get new address

bolt/test/X86/shrinkwrapping-do-not-pessimize.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ end_if_1:
5353
.size _start, .-_start
5454

5555
.data
56-
rel: .quad end_if_1
56+
rel: .quad _start
5757

5858
# CHECK: BOLT-INFO: Shrink wrapping moved 0 spills inserting load/stores and 0 spills inserting push/pops

bolt/test/runtime/X86/Inputs/indirect_goto.c

Lines changed: 0 additions & 18 deletions
This file was deleted.

bolt/test/runtime/X86/indirect-goto-pie.test

Lines changed: 0 additions & 10 deletions
This file was deleted.

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "PosixReturnCheck.h"
5555
#include "RedundantBranchConditionCheck.h"
5656
#include "ReservedIdentifierCheck.h"
57+
#include "ReturnConstRefFromParameterCheck.h"
5758
#include "SharedPtrArrayMismatchCheck.h"
5859
#include "SignalHandlerCheck.h"
5960
#include "SignedCharMisuseCheck.h"
@@ -137,6 +138,8 @@ class BugproneModule : public ClangTidyModule {
137138
"bugprone-inaccurate-erase");
138139
CheckFactories.registerCheck<IncorrectEnableIfCheck>(
139140
"bugprone-incorrect-enable-if");
141+
CheckFactories.registerCheck<ReturnConstRefFromParameterCheck>(
142+
"bugprone-return-const-ref-from-parameter");
140143
CheckFactories.registerCheck<SwitchMissingDefaultCaseCheck>(
141144
"bugprone-switch-missing-default-case");
142145
CheckFactories.registerCheck<IncDecInConditionsCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_library(clangTidyBugproneModule
2626
ImplicitWideningOfMultiplicationResultCheck.cpp
2727
InaccurateEraseCheck.cpp
2828
IncorrectEnableIfCheck.cpp
29+
ReturnConstRefFromParameterCheck.cpp
2930
SuspiciousStringviewDataUsageCheck.cpp
3031
SwitchMissingDefaultCaseCheck.cpp
3132
IncDecInConditionsCheck.cpp
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- ReturnConstRefFromParameterCheck.cpp - clang-tidy ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ReturnConstRefFromParameterCheck.h"
10+
#include "../utils/Matchers.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang::tidy::bugprone {
17+
18+
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
19+
Finder->addMatcher(
20+
returnStmt(hasReturnValue(declRefExpr(to(parmVarDecl(hasType(
21+
hasCanonicalType(matchers::isReferenceToConst())))))))
22+
.bind("ret"),
23+
this);
24+
}
25+
26+
void ReturnConstRefFromParameterCheck::check(
27+
const MatchFinder::MatchResult &Result) {
28+
const auto *R = Result.Nodes.getNodeAs<ReturnStmt>("ret");
29+
diag(R->getRetValue()->getBeginLoc(),
30+
"returning a constant reference parameter may cause a use-after-free "
31+
"when the parameter is constructed from a temporary");
32+
}
33+
34+
} // namespace clang::tidy::bugprone
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- ReturnConstRefFromParameterCheck.h - clang-tidy --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::bugprone {
15+
16+
/// Detects return statements that return a constant reference parameter as
17+
/// constant reference. This may cause use-after-free errors if the caller uses
18+
/// xvalues as arguments.
19+
///
20+
/// For the user-facing documentation see:
21+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/return-const-ref-from-parameter.html
22+
class ReturnConstRefFromParameterCheck : public ClangTidyCheck {
23+
public:
24+
ReturnConstRefFromParameterCheck(StringRef Name, ClangTidyContext *Context)
25+
: ClangTidyCheck(Name, Context) {}
26+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
27+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
28+
std::optional<TraversalKind> getCheckTraversalKind() const override {
29+
// Use 'AsIs' to make sure the return type is exactly the same as the
30+
// parameter type.
31+
return TK_AsIs;
32+
}
33+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
34+
return LangOpts.CPlusPlus;
35+
}
36+
};
37+
38+
} // namespace clang::tidy::bugprone
39+
40+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H

clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
4343
callee(cxxMethodDecl(hasName("find")).bind("find_fun")),
4444
// ... on a class with a starts_with function.
4545
on(hasType(
46-
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))));
46+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
47+
// Bind search expression.
48+
hasArgument(0, expr().bind("search_expr")));
4749

4850
const auto RFindExpr = cxxMemberCallExpr(
4951
// A method call with a second argument of zero...
@@ -52,15 +54,68 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
5254
callee(cxxMethodDecl(hasName("rfind")).bind("find_fun")),
5355
// ... on a class with a starts_with function.
5456
on(hasType(
55-
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))));
57+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
58+
// Bind search expression.
59+
hasArgument(0, expr().bind("search_expr")));
60+
61+
// Match a string literal and an integer or strlen() call matching the length.
62+
const auto HasStringLiteralAndLengthArgs = [](const auto StringArgIndex,
63+
const auto LengthArgIndex) {
64+
return allOf(
65+
hasArgument(StringArgIndex, stringLiteral().bind("string_literal_arg")),
66+
hasArgument(LengthArgIndex,
67+
anyOf(integerLiteral().bind("integer_literal_size_arg"),
68+
callExpr(callee(functionDecl(parameterCountIs(1),
69+
hasName("strlen"))),
70+
hasArgument(0, stringLiteral().bind(
71+
"strlen_arg"))))));
72+
};
73+
74+
// Match a string variable and a call to length() or size().
75+
const auto HasStringVariableAndSizeCallArgs = [](const auto StringArgIndex,
76+
const auto LengthArgIndex) {
77+
return allOf(
78+
hasArgument(StringArgIndex, declRefExpr(hasDeclaration(
79+
decl().bind("string_var_decl")))),
80+
hasArgument(LengthArgIndex,
81+
cxxMemberCallExpr(
82+
callee(cxxMethodDecl(isConst(), parameterCountIs(0),
83+
hasAnyName("size", "length"))),
84+
on(declRefExpr(
85+
to(decl(equalsBoundNode("string_var_decl"))))))));
86+
};
5687

57-
const auto FindOrRFindExpr =
58-
cxxMemberCallExpr(anyOf(FindExpr, RFindExpr)).bind("find_expr");
88+
// Match either one of the two cases above.
89+
const auto HasStringAndLengthArgs =
90+
[HasStringLiteralAndLengthArgs, HasStringVariableAndSizeCallArgs](
91+
const auto StringArgIndex, const auto LengthArgIndex) {
92+
return anyOf(
93+
HasStringLiteralAndLengthArgs(StringArgIndex, LengthArgIndex),
94+
HasStringVariableAndSizeCallArgs(StringArgIndex, LengthArgIndex));
95+
};
96+
97+
const auto CompareExpr = cxxMemberCallExpr(
98+
// A method call with three arguments...
99+
argumentCountIs(3),
100+
// ... where the first argument is zero...
101+
hasArgument(0, ZeroLiteral),
102+
// ... named compare...
103+
callee(cxxMethodDecl(hasName("compare")).bind("find_fun")),
104+
// ... on a class with a starts_with function...
105+
on(hasType(
106+
hasCanonicalType(hasDeclaration(ClassWithStartsWithFunction)))),
107+
// ... where the third argument is some string and the second a length.
108+
HasStringAndLengthArgs(2, 1),
109+
// Bind search expression.
110+
hasArgument(2, expr().bind("search_expr")));
59111

60112
Finder->addMatcher(
61-
// Match [=!]= with a zero on one side and a string.(r?)find on the other.
62-
binaryOperator(hasAnyOperatorName("==", "!="),
63-
hasOperands(FindOrRFindExpr, ZeroLiteral))
113+
// Match [=!]= with a zero on one side and (r?)find|compare on the other.
114+
binaryOperator(
115+
hasAnyOperatorName("==", "!="),
116+
hasOperands(cxxMemberCallExpr(anyOf(FindExpr, RFindExpr, CompareExpr))
117+
.bind("find_expr"),
118+
ZeroLiteral))
64119
.bind("expr"),
65120
this);
66121
}
@@ -69,23 +124,42 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
69124
const auto *ComparisonExpr = Result.Nodes.getNodeAs<BinaryOperator>("expr");
70125
const auto *FindExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("find_expr");
71126
const auto *FindFun = Result.Nodes.getNodeAs<CXXMethodDecl>("find_fun");
127+
const auto *SearchExpr = Result.Nodes.getNodeAs<Expr>("search_expr");
72128
const auto *StartsWithFunction =
73129
Result.Nodes.getNodeAs<CXXMethodDecl>("starts_with_fun");
74130

131+
const auto *StringLiteralArg =
132+
Result.Nodes.getNodeAs<StringLiteral>("string_literal_arg");
133+
const auto *IntegerLiteralSizeArg =
134+
Result.Nodes.getNodeAs<IntegerLiteral>("integer_literal_size_arg");
135+
const auto *StrlenArg = Result.Nodes.getNodeAs<StringLiteral>("strlen_arg");
136+
137+
// Filter out compare cases where the length does not match string literal.
138+
if (StringLiteralArg && IntegerLiteralSizeArg &&
139+
StringLiteralArg->getLength() !=
140+
IntegerLiteralSizeArg->getValue().getZExtValue()) {
141+
return;
142+
}
143+
144+
if (StringLiteralArg && StrlenArg &&
145+
StringLiteralArg->getLength() != StrlenArg->getLength()) {
146+
return;
147+
}
148+
75149
if (ComparisonExpr->getBeginLoc().isMacroID()) {
76150
return;
77151
}
78152

79153
const bool Neg = ComparisonExpr->getOpcode() == BO_NE;
80154

81155
auto Diagnostic =
82-
diag(FindExpr->getBeginLoc(), "use %0 instead of %1() %select{==|!=}2 0")
156+
diag(FindExpr->getExprLoc(), "use %0 instead of %1() %select{==|!=}2 0")
83157
<< StartsWithFunction->getName() << FindFun->getName() << Neg;
84158

85-
// Remove possible zero second argument and ' [!=]= 0' suffix.
159+
// Remove possible arguments after search expression and ' [!=]= 0' suffix.
86160
Diagnostic << FixItHint::CreateReplacement(
87161
CharSourceRange::getTokenRange(
88-
Lexer::getLocForEndOfToken(FindExpr->getArg(0)->getEndLoc(), 0,
162+
Lexer::getLocForEndOfToken(SearchExpr->getEndLoc(), 0,
89163
*Result.SourceManager, getLangOpts()),
90164
ComparisonExpr->getEndLoc()),
91165
")");
@@ -94,11 +168,12 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
94168
Diagnostic << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
95169
ComparisonExpr->getBeginLoc(), FindExpr->getBeginLoc()));
96170

97-
// Replace '(r?)find' with 'starts_with'.
171+
// Replace method name by 'starts_with'.
172+
// Remove possible arguments before search expression.
98173
Diagnostic << FixItHint::CreateReplacement(
99-
CharSourceRange::getTokenRange(FindExpr->getExprLoc(),
100-
FindExpr->getExprLoc()),
101-
StartsWithFunction->getName());
174+
CharSourceRange::getCharRange(FindExpr->getExprLoc(),
175+
SearchExpr->getBeginLoc()),
176+
(StartsWithFunction->getName() + "(").str());
102177

103178
// Add possible negation '!'.
104179
if (Neg) {

clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace clang::tidy::modernize {
1515

16-
/// Checks whether a ``find`` or ``rfind`` result is compared with 0 and
17-
/// suggests replacing with ``starts_with`` when the method exists in the class.
18-
/// Notably, this will work with ``std::string`` and ``std::string_view``.
16+
/// Checks for common roundabout ways to express ``starts_with`` and
17+
/// ``ends_with`` and suggests replacing with ``starts_with`` when the method is
18+
/// available. Notably, this will work with ``std::string`` and
19+
/// ``std::string_view``.
1920
///
2021
/// For the user-facing documentation see:
2122
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-starts-ends-with.html

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ New checks
117117
Detects error-prone Curiously Recurring Template Pattern usage, when the CRTP
118118
can be constructed outside itself and the derived class.
119119

120+
- New :doc:`bugprone-return-const-ref-from-parameter
121+
<clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check.
122+
123+
Detects return statements that return a constant reference parameter as constant
124+
reference. This may cause use-after-free errors if the caller uses xvalues as
125+
arguments.
126+
120127
- New :doc:`bugprone-suspicious-stringview-data-usage
121128
<clang-tidy/checks/bugprone/suspicious-stringview-data-usage>` check.
122129

@@ -266,6 +273,10 @@ Changes in existing checks
266273
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
267274
whitespace when deleting the ``virtual`` keyword.
268275

276+
- Improved :doc:`modernize-use-starts-ends-with
277+
<clang-tidy/checks/modernize/use-starts-ends-with>` check to also handle
278+
calls to ``compare`` method.
279+
269280
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
270281
check by adding support for detection of typedefs declared on function level.
271282

0 commit comments

Comments
 (0)