Skip to content

Commit c7b3b9d

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:82ebd333a889d2372c8445dc3d5d527ec48537db into amd-gfx:0e8d0cecbbff
Local branch amd-gfx 0e8d0ce Merged main:438ad9f2bf25575c474313de4ad85a5da6f69e4c into amd-gfx:b82566cedfb0 Remote branch main 82ebd33 [LLDB][Minidumps] Read x64 registers as 64b and handle truncation in the file builder (llvm#106473)
2 parents 0e8d0ce + 82ebd33 commit c7b3b9d

File tree

17 files changed

+1025
-112
lines changed

17 files changed

+1025
-112
lines changed

clang-tools-extra/clangd/CollectMacros.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,
3232
if (Loc.isInvalid() || Loc.isMacroID())
3333
return;
3434

35+
assert(isInsideMainFile(Loc, SM));
3536
auto Name = MacroNameTok.getIdentifierInfo()->getName();
3637
Out.Names.insert(Name);
3738
size_t Start = SM.getFileOffset(Loc);

clang-tools-extra/clangd/CollectMacros.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ class CollectMainFileMacros : public PPCallbacks {
8282

8383
void SourceRangeSkipped(SourceRange R, SourceLocation EndifLoc) override;
8484

85+
// Called when the AST build is done to disable further recording
86+
// of macros by this class. This is needed because some clang-tidy
87+
// checks can trigger PP callbacks by calling directly into the
88+
// preprocessor. Such calls are not interleaved with FileChanged()
89+
// in the expected way, leading this class to erroneously process
90+
// macros that are not in the main file.
91+
void doneParse() { InMainFile = false; }
92+
8593
private:
8694
void add(const Token &MacroNameTok, const MacroInfo *MI,
8795
bool IsDefinition = false, bool InConditionalDirective = false);

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,9 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
688688
Marks = Patch->marks();
689689
}
690690
auto &PP = Clang->getPreprocessor();
691-
PP.addPPCallbacks(std::make_unique<CollectMainFileMacros>(PP, Macros));
691+
auto MacroCollector = std::make_unique<CollectMainFileMacros>(PP, Macros);
692+
auto *MacroCollectorPtr = MacroCollector.get(); // so we can call doneParse()
693+
PP.addPPCallbacks(std::move(MacroCollector));
692694

693695
PP.addPPCallbacks(
694696
collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
@@ -709,6 +711,10 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
709711
log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
710712
toString(std::move(Err)));
711713

714+
// Disable the macro collector for the remainder of this function, e.g.
715+
// clang-tidy checkers.
716+
MacroCollectorPtr->doneParse();
717+
712718
// We have to consume the tokens before running clang-tidy to avoid collecting
713719
// tokens from running the preprocessor inside the checks (only
714720
// modernize-use-trailing-return-type does that today).

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,23 @@ TEST(DiagnosticTest, ClangTidySelfContainedDiagsFormatting) {
940940
withFix(equalToFix(ExpectedFix2))))));
941941
}
942942

943+
TEST(DiagnosticsTest, ClangTidyCallingIntoPreprocessor) {
944+
std::string Main = R"cpp(
945+
extern "C" {
946+
#include "b.h"
947+
}
948+
)cpp";
949+
std::string Header = R"cpp(
950+
#define EXTERN extern
951+
EXTERN int waldo();
952+
)cpp";
953+
auto TU = TestTU::withCode(Main);
954+
TU.AdditionalFiles["b.h"] = Header;
955+
TU.ClangTidyProvider = addTidyChecks("modernize-use-trailing-return-type");
956+
// Check that no assertion failures occur during the build
957+
TU.build();
958+
}
959+
943960
TEST(DiagnosticsTest, Preprocessor) {
944961
// This looks like a preamble, but there's an #else in the middle!
945962
// Check that:

lldb/source/Plugins/ObjectFile/Minidump/MinidumpFileBuilder.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,14 @@ GetThreadContext_x86_64(RegisterContext *reg_ctx) {
491491
thread_context.r14 = read_register_u64(reg_ctx, "r14");
492492
thread_context.r15 = read_register_u64(reg_ctx, "r15");
493493
thread_context.rip = read_register_u64(reg_ctx, "rip");
494-
thread_context.eflags = read_register_u32(reg_ctx, "rflags");
495-
thread_context.cs = read_register_u16(reg_ctx, "cs");
496-
thread_context.fs = read_register_u16(reg_ctx, "fs");
497-
thread_context.gs = read_register_u16(reg_ctx, "gs");
498-
thread_context.ss = read_register_u16(reg_ctx, "ss");
499-
thread_context.ds = read_register_u16(reg_ctx, "ds");
494+
// To make our code agnostic to whatever type the register value identifies
495+
// itself as, we read as a u64 and truncate to u32/u16 ourselves.
496+
thread_context.eflags = read_register_u64(reg_ctx, "rflags");
497+
thread_context.cs = read_register_u64(reg_ctx, "cs");
498+
thread_context.fs = read_register_u64(reg_ctx, "fs");
499+
thread_context.gs = read_register_u64(reg_ctx, "gs");
500+
thread_context.ss = read_register_u64(reg_ctx, "ss");
501+
thread_context.ds = read_register_u64(reg_ctx, "ds");
500502
return thread_context;
501503
}
502504

lldb/test/API/functionalities/process_save_core_minidump/TestProcessSaveCoreMinidump.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def verify_core_file(
1717
expected_modules,
1818
expected_threads,
1919
stacks_to_sps_map,
20+
stacks_to_registers_map,
2021
):
2122
# To verify, we'll launch with the mini dump
2223
target = self.dbg.CreateTarget(None)
@@ -62,6 +63,17 @@ def verify_core_file(
6263
# Try to read just past the red zone and fail
6364
process.ReadMemory(sp - red_zone - 1, 1, error)
6465
self.assertTrue(error.Fail(), "No failure when reading past the red zone")
66+
# Verify the registers are the same
67+
self.assertIn(thread_id, stacks_to_registers_map)
68+
register_val_list = stacks_to_registers_map[thread_id]
69+
frame_register_list = frame.GetRegisters()
70+
for x in register_val_list:
71+
self.assertEqual(
72+
x.GetValueAsUnsigned(),
73+
frame_register_list.GetFirstValueByName(
74+
x.GetName()
75+
).GetValueAsUnsigned(),
76+
)
6577

6678
self.dbg.DeleteTarget(target)
6779

@@ -93,12 +105,16 @@ def test_save_linux_mini_dump(self):
93105
expected_number_of_threads = process.GetNumThreads()
94106
expected_threads = []
95107
stacks_to_sp_map = {}
108+
stakcs_to_registers_map = {}
96109

97110
for thread_idx in range(process.GetNumThreads()):
98111
thread = process.GetThreadAtIndex(thread_idx)
99112
thread_id = thread.GetThreadID()
100113
expected_threads.append(thread_id)
101114
stacks_to_sp_map[thread_id] = thread.GetFrameAtIndex(0).GetSP()
115+
stakcs_to_registers_map[thread_id] = thread.GetFrameAtIndex(
116+
0
117+
).GetRegisters()
102118

103119
# save core and, kill process and verify corefile existence
104120
base_command = "process save-core --plugin-name=minidump "
@@ -110,6 +126,7 @@ def test_save_linux_mini_dump(self):
110126
expected_modules,
111127
expected_threads,
112128
stacks_to_sp_map,
129+
stakcs_to_registers_map,
113130
)
114131

115132
self.runCmd(base_command + " --style=modified-memory '%s'" % (core_dirty))
@@ -120,6 +137,7 @@ def test_save_linux_mini_dump(self):
120137
expected_modules,
121138
expected_threads,
122139
stacks_to_sp_map,
140+
stakcs_to_registers_map,
123141
)
124142

125143
self.runCmd(base_command + " --style=full '%s'" % (core_full))
@@ -130,6 +148,7 @@ def test_save_linux_mini_dump(self):
130148
expected_modules,
131149
expected_threads,
132150
stacks_to_sp_map,
151+
stakcs_to_registers_map,
133152
)
134153

135154
options = lldb.SBSaveCoreOptions()
@@ -147,6 +166,7 @@ def test_save_linux_mini_dump(self):
147166
expected_modules,
148167
expected_threads,
149168
stacks_to_sp_map,
169+
stakcs_to_registers_map,
150170
)
151171

152172
options = lldb.SBSaveCoreOptions()
@@ -163,6 +183,7 @@ def test_save_linux_mini_dump(self):
163183
expected_modules,
164184
expected_threads,
165185
stacks_to_sp_map,
186+
stakcs_to_registers_map,
166187
)
167188

168189
# Minidump can now save full core files, but they will be huge and
@@ -181,6 +202,7 @@ def test_save_linux_mini_dump(self):
181202
expected_modules,
182203
expected_threads,
183204
stacks_to_sp_map,
205+
stakcs_to_registers_map,
184206
)
185207

186208
self.assertSuccess(process.Kill())
@@ -276,13 +298,16 @@ def test_save_linux_mini_dump_default_options(self):
276298
expected_threads = []
277299
stacks_to_sp_map = {}
278300
expected_pid = process.GetProcessInfo().GetProcessID()
301+
stacks_to_registers_map = {}
279302

280303
for thread_idx in range(process.GetNumThreads()):
281304
thread = process.GetThreadAtIndex(thread_idx)
282305
thread_id = thread.GetThreadID()
283306
expected_threads.append(thread_id)
284307
stacks_to_sp_map[thread_id] = thread.GetFrameAtIndex(0).GetSP()
285-
308+
stacks_to_registers_map[thread_id] = thread.GetFrameAtIndex(
309+
0
310+
).GetRegisters()
286311

287312
# This is almost identical to the single thread test case because
288313
# minidump defaults to stacks only, so we want to see if the
@@ -294,7 +319,14 @@ def test_save_linux_mini_dump_default_options(self):
294319
error = process.SaveCore(options)
295320
self.assertTrue(error.Success())
296321

297-
self.verify_core_file(default_value_file, expected_pid, expected_modules, expected_threads, stacks_to_sp_map)
322+
self.verify_core_file(
323+
default_value_file,
324+
expected_pid,
325+
expected_modules,
326+
expected_threads,
327+
stacks_to_sp_map,
328+
stacks_to_registers_map,
329+
)
298330

299331
finally:
300332
self.assertTrue(self.dbg.DeleteTarget(target))

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 509964
19+
#define LLVM_MAIN_REVISION 509968
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct SimplifyCFGOptions {
2727
bool ConvertSwitchToLookupTable = false;
2828
bool NeedCanonicalLoop = true;
2929
bool HoistCommonInsts = false;
30+
bool HoistLoadsStoresWithCondFaulting = false;
3031
bool SinkCommonInsts = false;
3132
bool SimplifyCondBranch = true;
3233
bool SpeculateBlocks = true;
@@ -59,6 +60,10 @@ struct SimplifyCFGOptions {
5960
HoistCommonInsts = B;
6061
return *this;
6162
}
63+
SimplifyCFGOptions &hoistLoadsStoresWithCondFaulting(bool B) {
64+
HoistLoadsStoresWithCondFaulting = B;
65+
return *this;
66+
}
6267
SimplifyCFGOptions &sinkCommonInsts(bool B) {
6368
SinkCommonInsts = B;
6469
return *this;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
848848
Result.needCanonicalLoops(Enable);
849849
} else if (ParamName == "hoist-common-insts") {
850850
Result.hoistCommonInsts(Enable);
851+
} else if (ParamName == "hoist-loads-stores-with-cond-faulting") {
852+
Result.hoistLoadsStoresWithCondFaulting(Enable);
851853
} else if (ParamName == "sink-common-insts") {
852854
Result.sinkCommonInsts(Enable);
853855
} else if (ParamName == "speculate-unpredictables") {

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,9 +1534,11 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
15341534

15351535
// LoopSink (and other loop passes since the last simplifyCFG) might have
15361536
// resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
1537-
OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
1538-
.convertSwitchRangeToICmp(true)
1539-
.speculateUnpredictables(true)));
1537+
OptimizePM.addPass(
1538+
SimplifyCFGPass(SimplifyCFGOptions()
1539+
.convertSwitchRangeToICmp(true)
1540+
.speculateUnpredictables(true)
1541+
.hoistLoadsStoresWithCondFaulting(true)));
15401542

15411543
// Add the core optimizing pipeline.
15421544
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM),

llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ static cl::opt<bool> UserHoistCommonInsts(
7373
"hoist-common-insts", cl::Hidden, cl::init(false),
7474
cl::desc("hoist common instructions (default = false)"));
7575

76+
static cl::opt<bool> UserHoistLoadsStoresWithCondFaulting(
77+
"hoist-loads-stores-with-cond-faulting", cl::Hidden, cl::init(false),
78+
cl::desc("Hoist loads/stores if the target supports conditional faulting "
79+
"(default = false)"));
80+
7681
static cl::opt<bool> UserSinkCommonInsts(
7782
"sink-common-insts", cl::Hidden, cl::init(false),
7883
cl::desc("Sink common instructions (default = false)"));
@@ -326,6 +331,9 @@ static void applyCommandLineOverridesToOptions(SimplifyCFGOptions &Options) {
326331
Options.NeedCanonicalLoop = UserKeepLoops;
327332
if (UserHoistCommonInsts.getNumOccurrences())
328333
Options.HoistCommonInsts = UserHoistCommonInsts;
334+
if (UserHoistLoadsStoresWithCondFaulting.getNumOccurrences())
335+
Options.HoistLoadsStoresWithCondFaulting =
336+
UserHoistLoadsStoresWithCondFaulting;
329337
if (UserSinkCommonInsts.getNumOccurrences())
330338
Options.SinkCommonInsts = UserSinkCommonInsts;
331339
if (UserSpeculateUnpredictables.getNumOccurrences())
@@ -354,6 +362,8 @@ void SimplifyCFGPass::printPipeline(
354362
<< "switch-to-lookup;";
355363
OS << (Options.NeedCanonicalLoop ? "" : "no-") << "keep-loops;";
356364
OS << (Options.HoistCommonInsts ? "" : "no-") << "hoist-common-insts;";
365+
OS << (Options.HoistLoadsStoresWithCondFaulting ? "" : "no-")
366+
<< "hoist-loads-stores-with-cond-faulting;";
357367
OS << (Options.SinkCommonInsts ? "" : "no-") << "sink-common-insts;";
358368
OS << (Options.SpeculateBlocks ? "" : "no-") << "speculate-blocks;";
359369
OS << (Options.SimplifyCondBranch ? "" : "no-") << "simplify-cond-branch;";

0 commit comments

Comments
 (0)