[compiler-rt] [asan] Prevent printing invalid parent thread (PR #111916)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 16:21:11 PDT 2024


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/111916

>From e953c9311789391eed5cb52fced9e6dee0cef8e1 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 10 Oct 2024 16:00:54 -0700
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 compiler-rt/lib/asan/asan_descriptions.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/asan/asan_descriptions.cpp b/compiler-rt/lib/asan/asan_descriptions.cpp
index 1c2f20a76343bb..778e134f1cf2a5 100644
--- a/compiler-rt/lib/asan/asan_descriptions.cpp
+++ b/compiler-rt/lib/asan/asan_descriptions.cpp
@@ -63,7 +63,10 @@ void DescribeThread(AsanThreadContext *context) {
   if (flags()->print_full_thread_history) {
     AsanThreadContext *parent_context =
         GetThreadContextByTidLocked(context->parent_tid);
-    DescribeThread(parent_context);
+    // `context->parent_tid` may point to reused slot, double check, `unique_id`
+    // of new user will always be greater then the child.
+    if (context->unique_id > parent_context->unique_id)
+      DescribeThread(parent_context);
   }
 }
 

>From 01ef2eb53019e42e6596de80b3a3df79ed29b6ab Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 10 Oct 2024 16:03:46 -0700
Subject: [PATCH 2/3] comment

Created using spr 1.3.4
---
 compiler-rt/lib/asan/asan_descriptions.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/asan/asan_descriptions.cpp b/compiler-rt/lib/asan/asan_descriptions.cpp
index 778e134f1cf2a5..8bbbd47ba15ca1 100644
--- a/compiler-rt/lib/asan/asan_descriptions.cpp
+++ b/compiler-rt/lib/asan/asan_descriptions.cpp
@@ -63,8 +63,8 @@ void DescribeThread(AsanThreadContext *context) {
   if (flags()->print_full_thread_history) {
     AsanThreadContext *parent_context =
         GetThreadContextByTidLocked(context->parent_tid);
-    // `context->parent_tid` may point to reused slot, double check, `unique_id`
-    // of new user will always be greater then the child.
+    // `context->parent_tid` may point to reused slot. Check `unique_id` which
+    // is always smaller for the parent, always greater for a new user.
     if (context->unique_id > parent_context->unique_id)
       DescribeThread(parent_context);
   }

>From aed6c945a103027ade501ae350010f0ae2b12e9b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 10 Oct 2024 16:20:56 -0700
Subject: [PATCH 3/3] by unknown

Created using spr 1.3.4
---
 compiler-rt/lib/asan/asan_descriptions.cpp | 23 +++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/compiler-rt/lib/asan/asan_descriptions.cpp b/compiler-rt/lib/asan/asan_descriptions.cpp
index 8bbbd47ba15ca1..674fe9c1e90be0 100644
--- a/compiler-rt/lib/asan/asan_descriptions.cpp
+++ b/compiler-rt/lib/asan/asan_descriptions.cpp
@@ -48,9 +48,20 @@ void DescribeThread(AsanThreadContext *context) {
     return;
   }
   context->announced = true;
+
+  AsanThreadContext *parent_context =
+      context->parent_tid == kInvalidTid
+          ? nullptr
+          : GetThreadContextByTidLocked(context->parent_tid);
+
+  // `context->parent_tid` may point to reused slot. Check `unique_id` which
+  // is always smaller for the parent, always greater for a new user.
+  if (context->unique_id <= parent_context->unique_id)
+    parent_context = nullptr;
+
   InternalScopedString str;
   str.AppendF("Thread %s", AsanThreadIdAndName(context).c_str());
-  if (context->parent_tid == kInvalidTid) {
+  if (!parent_context) {
     str.Append(" created by unknown thread\n");
     Printf("%s", str.data());
     return;
@@ -60,14 +71,8 @@ void DescribeThread(AsanThreadContext *context) {
   Printf("%s", str.data());
   StackDepotGet(context->stack_id).Print();
   // Recursively described parent thread if needed.
-  if (flags()->print_full_thread_history) {
-    AsanThreadContext *parent_context =
-        GetThreadContextByTidLocked(context->parent_tid);
-    // `context->parent_tid` may point to reused slot. Check `unique_id` which
-    // is always smaller for the parent, always greater for a new user.
-    if (context->unique_id > parent_context->unique_id)
-      DescribeThread(parent_context);
-  }
+  if (flags()->print_full_thread_history)
+    DescribeThread(parent_context);
 }
 
 // Shadow descriptions



More information about the llvm-commits mailing list