Skip to content

Commit 337e023

Browse files
committed
[lldb] Prevent Task formatter from showing bogus children (#10495)
Very rarely, we've seen a task include a bogus child task. When this has occurred, the task id has been zero, which is invalid. This change filters out a Task's children if they have a task id of 0. (cherry-picked from commit 529abca)
1 parent 57af8f3 commit 337e023

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

lldb/source/Plugins/Language/Swift/SwiftFormatters.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,20 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
875875
case 3: {
876876
if (!m_child_tasks_sp) {
877877
using task_type = decltype(m_task_info.childTasks)::value_type;
878-
const std::vector<task_type> &tasks = m_task_info.childTasks;
878+
std::vector<task_type> tasks = m_task_info.childTasks;
879+
880+
// Remove any bogus child tasks.
881+
// Very rarely, the child tasks include a bogus task which has an
882+
// invalid task id of 0.
883+
llvm::erase_if(tasks, [&](auto task_ptr) {
884+
if (auto task_info =
885+
expectedToOptional(m_reflection_ctx->asyncTaskInfo(task_ptr)))
886+
return task_info->id == 0;
887+
// Don't filter children with errors here. Let these tasks reach the
888+
// formatter's existing error handling.
889+
return false;
890+
});
891+
879892
std::string mangled_typename =
880893
mangledTypenameForTasksTuple(tasks.size());
881894
CompilerType tasks_tuple_type =
@@ -921,15 +934,14 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
921934

922935
lldb::ChildCacheState Update() override {
923936
if (auto *runtime = SwiftLanguageRuntime::Get(m_backend.GetProcessSP())) {
924-
ThreadSafeReflectionContext reflection_ctx =
925-
runtime->GetReflectionContext();
937+
m_reflection_ctx = runtime->GetReflectionContext();
926938
ValueObjectSP task_obj_sp = m_backend.GetChildMemberWithName("_task");
927939
if (!task_obj_sp)
928940
return ChildCacheState::eRefetch;
929941
m_task_ptr = task_obj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
930942
if (m_task_ptr != LLDB_INVALID_ADDRESS) {
931943
llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
932-
reflection_ctx->asyncTaskInfo(m_task_ptr);
944+
m_reflection_ctx->asyncTaskInfo(m_task_ptr);
933945
if (auto err = task_info.takeError()) {
934946
LLDB_LOG_ERROR(
935947
GetLog(LLDBLog::DataFormatters | LLDBLog::Types), std::move(err),
@@ -962,6 +974,7 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
962974
}
963975

964976
private:
977+
ThreadSafeReflectionContext m_reflection_ctx;
965978
TypeSystemSwiftTypeRef *m_ts = nullptr;
966979
addr_t m_task_ptr = LLDB_INVALID_ADDRESS;
967980
ReflectionContextInterface::AsyncTaskInfo m_task_info;

lldb/test/API/lang/swift/async/continuations/TestSwiftContinuationSynthetic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_unsafe_continuation_printing(self):
2020
textwrap.dedent(
2121
r"""
2222
\(UnsafeContinuation<Void, Never>\) cont = \{
23-
task = id:(\d+) flags:(?:running|enqueued) \{
23+
task = id:([1-9]\d*) flags:(?:running|enqueued) \{
2424
address = 0x[0-9a-f]+
2525
id = \1
2626
enqueuePriority = 0
@@ -45,7 +45,7 @@ def test_checked_continuation_printing(self):
4545
textwrap.dedent(
4646
r"""
4747
\(CheckedContinuation<Int, Never>\) cont = \{
48-
task = id:(\d+) flags:(?:running|enqueued) \{
48+
task = id:([1-9]\d*) flags:(?:running|enqueued) \{
4949
address = 0x[0-9a-f]+
5050
id = \1
5151
enqueuePriority = 0

lldb/test/API/lang/swift/async/formatters/task/TestSwiftTaskSyntheticProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_top_level_task(self):
2121
patterns=[
2222
textwrap.dedent(
2323
r"""
24-
\(Task<\(\), Error>\) task = id:(\d+) flags:(?:running|enqueued) \{
24+
\(Task<\(\), Error>\) task = id:([1-9]\d*) flags:(?:running|enqueued) \{
2525
address = 0x[0-9a-f]+
2626
id = \1
2727
enqueuePriority = \.medium
@@ -45,7 +45,7 @@ def test_current_task(self):
4545
patterns=[
4646
textwrap.dedent(
4747
r"""
48-
\(UnsafeCurrentTask\) currentTask = id:(\d+) flags:(?:running\|)?(?:enqueued\|)?asyncLetTask \{
48+
\(UnsafeCurrentTask\) currentTask = id:([1-9]\d*) flags:(?:running\|)?(?:enqueued\|)?asyncLetTask \{
4949
address = 0x[0-9a-f]+
5050
id = \1
5151
enqueuePriority = \.medium

lldb/test/API/lang/swift/async/taskgroups/TestSwiftTaskGroupSynthetic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ def do_test_print(self):
3232
textwrap.dedent(
3333
r"""
3434
\((?:Throwing)?TaskGroup<\(\)\??(?:, Error)?>\) group = \{
35-
\[0\] = id:(\d+) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
35+
\[0\] = id:([1-9]\d*) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
3636
address = 0x[0-9a-f]+
3737
id = \1
3838
enqueuePriority = \.medium
3939
children = \{\}
4040
\}
41-
\[1\] = id:(\d+) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
41+
\[1\] = id:([1-9]\d*) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
4242
address = 0x[0-9a-f]+
4343
id = \2
4444
enqueuePriority = \.medium
4545
children = \{\}
4646
\}
47-
\[2\] = id:(\d+) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
47+
\[2\] = id:([1-9]\d*) flags:(?:running\|)?(?:enqueued\|)?groupChildTask \{
4848
address = 0x[0-9a-f]+
4949
id = \3
5050
enqueuePriority = \.medium

0 commit comments

Comments
 (0)