Skip to content

Commit 349a473

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 ce18439 commit 349a473

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
@@ -882,7 +882,20 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
882882
case 3: {
883883
if (!m_child_tasks_sp) {
884884
using task_type = decltype(m_task_info.childTasks)::value_type;
885-
const std::vector<task_type> &tasks = m_task_info.childTasks;
885+
std::vector<task_type> tasks = m_task_info.childTasks;
886+
887+
// Remove any bogus child tasks.
888+
// Very rarely, the child tasks include a bogus task which has an
889+
// invalid task id of 0.
890+
llvm::erase_if(tasks, [&](auto task_ptr) {
891+
if (auto task_info =
892+
expectedToOptional(m_reflection_ctx->asyncTaskInfo(task_ptr)))
893+
return task_info->id == 0;
894+
// Don't filter children with errors here. Let these tasks reach the
895+
// formatter's existing error handling.
896+
return false;
897+
});
898+
886899
std::string mangled_typename =
887900
mangledTypenameForTasksTuple(tasks.size());
888901
CompilerType tasks_tuple_type =
@@ -928,15 +941,14 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
928941

929942
lldb::ChildCacheState Update() override {
930943
if (auto *runtime = SwiftLanguageRuntime::Get(m_backend.GetProcessSP())) {
931-
ThreadSafeReflectionContext reflection_ctx =
932-
runtime->GetReflectionContext();
944+
m_reflection_ctx = runtime->GetReflectionContext();
933945
ValueObjectSP task_obj_sp = m_backend.GetChildMemberWithName("_task");
934946
if (!task_obj_sp)
935947
return ChildCacheState::eRefetch;
936948
m_task_ptr = task_obj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
937949
if (m_task_ptr != LLDB_INVALID_ADDRESS) {
938950
llvm::Expected<ReflectionContextInterface::AsyncTaskInfo> task_info =
939-
reflection_ctx->asyncTaskInfo(m_task_ptr);
951+
m_reflection_ctx->asyncTaskInfo(m_task_ptr);
940952
if (auto err = task_info.takeError()) {
941953
LLDB_LOG_ERROR(
942954
GetLog(LLDBLog::DataFormatters | LLDBLog::Types), std::move(err),
@@ -968,6 +980,7 @@ class TaskSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
968980
}
969981

970982
private:
983+
ThreadSafeReflectionContext m_reflection_ctx;
971984
TypeSystemSwiftTypeRef *m_ts = nullptr;
972985
addr_t m_task_ptr = LLDB_INVALID_ADDRESS;
973986
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)