Skip to content

Commit 2afdfbb

Browse files
authored
Merge branch 'main' into fix_clang_repl_in_browser
2 parents b6becb5 + 352f868 commit 2afdfbb

File tree

321 files changed

+10246
-8440
lines changed

Some content is hidden

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

321 files changed

+10246
-8440
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,23 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
29272927
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring relocation from data to data\n");
29282928
}
29292929

2930+
static BinaryFunction *getInitFunctionIfStaticBinary(BinaryContext &BC) {
2931+
// Workaround for https://github.com/llvm/llvm-project/issues/100096
2932+
// ("[BOLT] GOT array pointer incorrectly rewritten"). In aarch64
2933+
// static glibc binaries, the .init section's _init function pointer can
2934+
// alias with a data pointer for the end of an array. GOT rewriting
2935+
// currently can't detect this and updates the data pointer to the
2936+
// moved _init, causing a runtime crash. Skipping _init on the other
2937+
// hand should be harmless.
2938+
if (!BC.IsStaticExecutable)
2939+
return nullptr;
2940+
const BinaryData *BD = BC.getBinaryDataByName("_init");
2941+
if (!BD || BD->getSectionName() != ".init")
2942+
return nullptr;
2943+
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: skip _init in for GOT workaround.\n");
2944+
return BC.getBinaryFunctionAtAddress(BD->getAddress());
2945+
}
2946+
29302947
void RewriteInstance::selectFunctionsToProcess() {
29312948
// Extend the list of functions to process or skip from a file.
29322949
auto populateFunctionNames = [](cl::opt<std::string> &FunctionNamesFile,
@@ -3047,6 +3064,9 @@ void RewriteInstance::selectFunctionsToProcess() {
30473064
return true;
30483065
};
30493066

3067+
if (BinaryFunction *Init = getInitFunctionIfStaticBinary(*BC))
3068+
Init->setIgnored();
3069+
30503070
for (auto &BFI : BC->getBinaryFunctions()) {
30513071
BinaryFunction &Function = BFI.second;
30523072

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Regression test for https://github.com/llvm/llvm-project/issues/100096
2+
# static glibc binaries crash on startup because _init is moved and
3+
# shares its address with an array end pointer. The GOT rewriting can't
4+
# tell the two pointers apart and incorrectly updates the _array_end
5+
# address. Test checks that _init is not moved.
6+
7+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
8+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -static -Wl,--section-start=.data=0x1000 -Wl,--section-start=.init=0x1004
9+
# RUN: llvm-bolt %t.exe -o %t.bolt
10+
# RUN: llvm-nm %t.exe | FileCheck --check-prefix=CHECK-ORIGINAL %s
11+
# RUN: llvm-nm %t.bolt | FileCheck --check-prefix=CHECK-BOLTED %s
12+
13+
.section .data
14+
.globl _array_end
15+
_array_start:
16+
.word 0x0
17+
18+
_array_end:
19+
.section .init,"ax",@progbits
20+
.globl _init
21+
22+
# Check that bolt doesn't move _init.
23+
#
24+
# CHECK-ORIGINAL: 0000000000001004 T _init
25+
# CHECK-BOLTED: 0000000000001004 T _init
26+
_init:
27+
ret
28+
29+
.section .text,"ax",@progbits
30+
.globl _start
31+
32+
# Check that bolt is moving some other functions.
33+
#
34+
# CHECK-ORIGINAL: 0000000000001008 T _start
35+
# CHECK-BOLTED-NOT: 0000000000001008 T _start
36+
_start:
37+
bl _init
38+
adrp x0, #:got:_array_end
39+
ldr x0, [x0, #:gotpage_lo15:_array_end]
40+
adrp x0, #:got:_init
41+
ldr x0, [x0, #:gotpage_lo15:_init]
42+
ret
43+

clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer,
116116
return Results;
117117
}
118118

119+
/// Returns the full source range for the field declaration up to (not
120+
/// including) the trailing semicolumn, including potential macro invocations,
121+
/// e.g. `int a GUARDED_BY(mu);`.
122+
static SourceRange getFullFieldSourceRange(const FieldDecl &Field,
123+
const ASTContext &Context) {
124+
SourceRange Range = Field.getSourceRange();
125+
SourceLocation End = Range.getEnd();
126+
const SourceManager &SM = Context.getSourceManager();
127+
const LangOptions &LangOpts = Context.getLangOpts();
128+
while (true) {
129+
std::optional<Token> CurrentToken = Lexer::findNextToken(End, SM, LangOpts);
130+
131+
if (!CurrentToken || CurrentToken->is(tok::semi))
132+
break;
133+
134+
if (CurrentToken->is(tok::eof))
135+
return Range; // Something is wrong, return the original range.
136+
End = CurrentToken->getLastLoc();
137+
}
138+
return SourceRange(Range.getBegin(), End);
139+
}
140+
119141
/// Reorders fields in the definition of a struct/class.
120142
///
121143
/// At the moment reordering of fields with
@@ -145,9 +167,10 @@ static bool reorderFieldsInDefinition(
145167
const auto FieldIndex = Field->getFieldIndex();
146168
if (FieldIndex == NewFieldsOrder[FieldIndex])
147169
continue;
148-
addReplacement(Field->getSourceRange(),
149-
Fields[NewFieldsOrder[FieldIndex]]->getSourceRange(),
150-
Context, Replacements);
170+
addReplacement(
171+
getFullFieldSourceRange(*Field, Context),
172+
getFullFieldSourceRange(*Fields[NewFieldsOrder[FieldIndex]], Context),
173+
Context, Replacements);
151174
}
152175
return true;
153176
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: clang-reorder-fields -record-name Foo -fields-order y,x %s -- | FileCheck %s
2+
3+
#define GUARDED_BY(x) __attribute__((guarded_by(x)))
4+
5+
class Foo {
6+
int x GUARDED_BY(x); // CHECK: {{^ int y;}}
7+
int y; // CHECK-NEXT: {{^ int x GUARDED_BY\(x\);}}
8+
};
9+

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3759,9 +3759,9 @@ the configuration (without a prefix: ``Auto``).
37593759
lists.
37603760

37613761
Important differences:
3762-
- No spaces inside the braced list.
3763-
- No line break before the closing brace.
3764-
- Indentation with the continuation indent, not with the block indent.
3762+
* No spaces inside the braced list.
3763+
* No line break before the closing brace.
3764+
* Indentation with the continuation indent, not with the block indent.
37653765

37663766
Fundamentally, C++11 braced lists are formatted exactly like function
37673767
calls would be formatted in their place. If the braced list follows a name
@@ -4104,10 +4104,10 @@ the configuration (without a prefix: ``Auto``).
41044104
When guessing whether a #include is the "main" include (to assign
41054105
category 0, see above), use this regex of allowed suffixes to the header
41064106
stem. A partial match is done, so that:
4107-
- "" means "arbitrary suffix"
4108-
- "$" means "no suffix"
4107+
* ``""`` means "arbitrary suffix"
4108+
* ``"$"`` means "no suffix"
41094109

4110-
For example, if configured to "(_test)?$", then a header a.h would be seen
4110+
For example, if configured to ``"(_test)?$"``, then a header a.h would be seen
41114111
as the "main" include in both a.cc and a_test.cc.
41124112

41134113
.. _IncludeIsMainSourceRegex:
@@ -5313,21 +5313,21 @@ the configuration (without a prefix: ``Auto``).
53135313

53145314
**QualifierOrder** (``List of Strings``) :versionbadge:`clang-format 14` :ref:`<QualifierOrder>`
53155315
The order in which the qualifiers appear.
5316-
Order is an array that can contain any of the following:
5316+
The order is an array that can contain any of the following:
53175317

5318-
* const
5319-
* inline
5320-
* static
5321-
* friend
5322-
* constexpr
5323-
* volatile
5324-
* restrict
5325-
* type
5318+
* ``const``
5319+
* ``inline``
5320+
* ``static``
5321+
* ``friend``
5322+
* ``constexpr``
5323+
* ``volatile``
5324+
* ``restrict``
5325+
* ``type``
53265326

53275327

53285328
.. note::
53295329

5330-
It **must** contain ``type``.
5330+
It must contain ``type``.
53315331

53325332
Items to the left of ``type`` will be placed to the left of the type and
53335333
aligned in the order supplied. Items to the right of ``type`` will be
@@ -6645,12 +6645,11 @@ the configuration (without a prefix: ``Auto``).
66456645
.. _StatementMacros:
66466646

66476647
**StatementMacros** (``List of Strings``) :versionbadge:`clang-format 8` :ref:`<StatementMacros>`
6648-
A vector of macros that should be interpreted as complete
6649-
statements.
6648+
A vector of macros that should be interpreted as complete statements.
66506649

6651-
Typical macros are expressions, and require a semi-colon to be
6652-
added; sometimes this is not the case, and this allows to make
6653-
clang-format aware of such cases.
6650+
Typical macros are expressions and require a semicolon to be added.
6651+
Sometimes this is not the case, and this allows to make clang-format aware
6652+
of such cases.
66546653

66556654
For example: Q_UNUSED
66566655

clang/docs/ReleaseNotes.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,10 @@ Modified Compiler Flags
436436
to utilize these vector libraries. The behavior for all other vector function
437437
libraries remains unchanged.
438438

439-
- The ``-Wnontrivial-memaccess`` warning has been updated to also warn about
439+
- The ``-Wnontrivial-memcall`` warning has been added to warn about
440440
passing non-trivially-copyable destrination parameter to ``memcpy``,
441441
``memset`` and similar functions for which it is a documented undefined
442-
behavior.
442+
behavior. It is implied by ``-Wnontrivial-memaccess``
443443

444444
Removed Compiler Flags
445445
-------------------------
@@ -771,6 +771,7 @@ Bug Fixes to AST Handling
771771
and ``relatedalso`` comment commands.
772772
- Clang now uses the location of the begin of the member expression for ``CallExpr``
773773
involving deduced ``this``. (#GH116928)
774+
- Fixed printout of AST that uses pack indexing expression. (#GH116486)
774775

775776
Miscellaneous Bug Fixes
776777
^^^^^^^^^^^^^^^^^^^^^^^
@@ -1023,6 +1024,16 @@ Moved checkers
10231024
original checkers were implemented only using AST matching and make more
10241025
sense as a single clang-tidy check.
10251026

1027+
- The checker ``alpha.unix.Chroot`` was modernized, improved and moved to
1028+
``unix.Chroot``. Testing was done on open source projects that use chroot(),
1029+
and false issues addressed in the improvements based on real use cases. Open
1030+
source projects used for testing include nsjail, lxroot, dive and ruri.
1031+
This checker conforms to SEI Cert C recommendation `POS05-C. Limit access to
1032+
files by creating a jail
1033+
<https://wiki.sei.cmu.edu/confluence/display/c/POS05-C.+Limit+access+to+files+by+creating+a+jail>`_.
1034+
Fixes (#GH34697).
1035+
(#GH117791) [Documentation](https://clang.llvm.org/docs/analyzer/checkers.html#unix-chroot-c).
1036+
10261037
.. _release-notes-sanitizers:
10271038

10281039
Sanitizers

clang/docs/analyzer/checkers.rst

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,37 @@ Critical section handling functions modeled by this checker:
17501750
}
17511751
}
17521752
1753+
.. _unix-Chroot:
1754+
1755+
unix.Chroot (C)
1756+
"""""""""""""""
1757+
Check improper use of chroot described by SEI Cert C recommendation `POS05-C.
1758+
Limit access to files by creating a jail
1759+
<https://wiki.sei.cmu.edu/confluence/display/c/POS05-C.+Limit+access+to+files+by+creating+a+jail>`_.
1760+
The checker finds usage patterns where ``chdir("/")`` is not called immediately
1761+
after a call to ``chroot(path)``.
1762+
1763+
.. code-block:: c
1764+
1765+
void f();
1766+
1767+
void test_bad() {
1768+
chroot("/usr/local");
1769+
f(); // warn: no call of chdir("/") immediately after chroot
1770+
}
1771+
1772+
void test_bad_path() {
1773+
chroot("/usr/local");
1774+
chdir("/usr"); // warn: no call of chdir("/") immediately after chroot
1775+
f();
1776+
}
1777+
1778+
void test_good() {
1779+
chroot("/usr/local");
1780+
chdir("/"); // no warning
1781+
f();
1782+
}
1783+
17531784
.. _unix-Errno:
17541785
17551786
unix.Errno (C)
@@ -3298,21 +3329,6 @@ SEI CERT checkers which tries to find errors based on their `C coding rules <htt
32983329
alpha.unix
32993330
^^^^^^^^^^
33003331
3301-
.. _alpha-unix-Chroot:
3302-
3303-
alpha.unix.Chroot (C)
3304-
"""""""""""""""""""""
3305-
Check improper use of chroot.
3306-
3307-
.. code-block:: c
3308-
3309-
void f();
3310-
3311-
void test() {
3312-
chroot("/usr/local");
3313-
f(); // warn: no call of chdir("/") immediately after chroot
3314-
}
3315-
33163332
.. _alpha-unix-PthreadLock:
33173333
33183334
alpha.unix.PthreadLock (C)

clang/include/clang/Basic/AttrDocs.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,8 @@ The capturing entity ``X`` can be one of the following:
39853985
std::set<std::string_view> s;
39863986
};
39873987

3988+
Note: When applied to a constructor parameter, `[[clang::lifetime_capture_by(this)]]` is just an alias of `[[clang::lifetimebound]]`.
3989+
39883990
- `global`, `unknown`.
39893991

39903992
.. code-block:: c++

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ def SizeofArrayDecay : DiagGroup<"sizeof-array-decay">;
683683
def SizeofPointerMemaccess : DiagGroup<"sizeof-pointer-memaccess">;
684684
def MemsetTransposedArgs : DiagGroup<"memset-transposed-args">;
685685
def DynamicClassMemaccess : DiagGroup<"dynamic-class-memaccess">;
686-
def NonTrivialMemaccess : DiagGroup<"nontrivial-memaccess">;
686+
def NonTrivialMemcall : DiagGroup<"nontrivial-memcall">;
687+
def NonTrivialMemaccess : DiagGroup<"nontrivial-memaccess", [NonTrivialMemcall]>;
687688
def SuspiciousBzero : DiagGroup<"suspicious-bzero">;
688689
def SuspiciousMemaccess : DiagGroup<"suspicious-memaccess",
689690
[SizeofPointerMemaccess, DynamicClassMemaccess,

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ def warn_cstruct_memaccess : Warning<
798798
def warn_cxxstruct_memaccess : Warning<
799799
"first argument in call to "
800800
"%0 is a pointer to non-trivially copyable type %1">,
801-
InGroup<NonTrivialMemaccess>;
801+
InGroup<NonTrivialMemcall>;
802802
def note_nontrivial_field : Note<
803803
"field is non-trivial to %select{copy|default-initialize}0">;
804804
def err_non_trivial_c_union_in_invalid_context : Error<

0 commit comments

Comments
 (0)