[compiler-rt] Reland "[sanitizer] Add CHECKs to validate calculated TLS range" (PR #108122)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 11 10:54:25 PDT 2024


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

>From b11d7789581ff6694177f6b636dcf614815516b3 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at gmail.com>
Date: Tue, 10 Sep 2024 17:46:05 -0700
Subject: [PATCH 1/7] =?UTF-8?q?Revert=20"Revert=20"[sanitizer]=20Add=20CHE?=
 =?UTF-8?q?CKs=20to=20validate=20calculated=20TLS=20range"=20(#10=E2=80=A6?=
 =?UTF-8?q?"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This reverts commit 5804193e38680683b370cb3ced46c018d4dbd1b2.
---
 compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index 666e6f3b351067..a1107ff7d24737 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -151,6 +151,10 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     // This may happen inside the DTOR of main thread, so just ignore it.
     tls_size = 0;
   }
+  if (tls_size) {
+    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res));
+    CHECK_LT(reinterpret_cast<uptr>(res), tls_beg + tls_size);
+  }
   dtv->beg = tls_beg;
   dtv->size = tls_size;
   return dtv;

>From 2cea9fc35f2a864f2c745641510f16b05c90534a Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 10 Sep 2024 17:51:36 -0700
Subject: [PATCH 2/7] Take into account kDtvOffset

---
 compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index a1107ff7d24737..6e77676fabf091 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -152,8 +152,8 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     tls_size = 0;
   }
   if (tls_size) {
-    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res));
-    CHECK_LT(reinterpret_cast<uptr>(res), tls_beg + tls_size);
+    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) + kDtvOffset);
+    CHECK_LT(reinterpret_cast<uptr>(res) + kDtvOffset, tls_beg + tls_size);
   }
   dtv->beg = tls_beg;
   dtv->size = tls_size;

>From 555ab1fde2079e60bc4c1a571b8185917a65866c Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 10 Sep 2024 17:59:59 -0700
Subject: [PATCH 3/7] apply kDtvOffset correctly

---
 compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index 6e77676fabf091..bf84a2ff60c91c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -152,8 +152,8 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     tls_size = 0;
   }
   if (tls_size) {
-    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) + kDtvOffset);
-    CHECK_LT(reinterpret_cast<uptr>(res) + kDtvOffset, tls_beg + tls_size);
+    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) - kDtvOffset);
+    CHECK_LT(reinterpret_cast<uptr>(res) - kDtvOffset, tls_beg + tls_size);
   }
   dtv->beg = tls_beg;
   dtv->size = tls_size;

>From 9a96068baeb4c6a5e0bfda0232195669dbc3c3bf Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 10 Sep 2024 22:17:06 -0700
Subject: [PATCH 4/7] verbose test

---
 compiler-rt/test/asan/TestCases/Linux/stress_dtls.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
index fd1ce0cd472f99..be0baabde49c17 100644
--- a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
+++ b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
@@ -11,8 +11,8 @@
 // RUN: %clangxx_asan -x c -DSO_NAME=f1 %s -shared -o %t-f1.so -fPIC
 // RUN: %clangxx_asan -x c -DSO_NAME=f2 %s -shared -o %t-f2.so -fPIC
 // RUN: %clangxx_asan %s -ldl -pthread -o %t
-// RUN: %run %t 0 3
-// RUN: %run %t 2 3
+// RUN: %env_asan_opts=verbosity=2 %run %t 0 3
+// RUN: %env_asan_opts=verbosity=2 %run %t 2 3
 // RUN: %env_asan_opts=verbosity=2 %run %t 10 2 2>&1 | FileCheck %s
 // RUN: %env_asan_opts=verbosity=2:intercept_tls_get_addr=1 %run %t 10 2 2>&1 | FileCheck %s
 // RUN: %env_asan_opts=verbosity=2:intercept_tls_get_addr=0 %run %t 10 2 2>&1 | FileCheck %s --check-prefix=CHECK0

>From 730ca7d1ffc904a7ef3f6d5ae566f8d700f9ee31 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Tue, 10 Sep 2024 23:30:00 -0700
Subject: [PATCH 5/7] try

---
 .../lib/sanitizer_common/sanitizer_tls_get_addr.cpp       | 8 ++++----
 compiler-rt/test/asan/TestCases/Linux/stress_dtls.c       | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index bf84a2ff60c91c..1113d515dcb06d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -151,10 +151,10 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     // This may happen inside the DTOR of main thread, so just ignore it.
     tls_size = 0;
   }
-  if (tls_size) {
-    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) - kDtvOffset);
-    CHECK_LT(reinterpret_cast<uptr>(res) - kDtvOffset, tls_beg + tls_size);
-  }
+  // if (tls_size) {
+  //   CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) - kDtvOffset);
+  //   CHECK_LT(reinterpret_cast<uptr>(res) - kDtvOffset, tls_beg + tls_size);
+  // }
   dtv->beg = tls_beg;
   dtv->size = tls_size;
   return dtv;
diff --git a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
index be0baabde49c17..6514456db56f6c 100644
--- a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
+++ b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
@@ -11,7 +11,7 @@
 // RUN: %clangxx_asan -x c -DSO_NAME=f1 %s -shared -o %t-f1.so -fPIC
 // RUN: %clangxx_asan -x c -DSO_NAME=f2 %s -shared -o %t-f2.so -fPIC
 // RUN: %clangxx_asan %s -ldl -pthread -o %t
-// RUN: %env_asan_opts=verbosity=2 %run %t 0 3
+// RUN: %env_asan_opts=verbosity=2 %run not %t 0 3
 // RUN: %env_asan_opts=verbosity=2 %run %t 2 3
 // RUN: %env_asan_opts=verbosity=2 %run %t 10 2 2>&1 | FileCheck %s
 // RUN: %env_asan_opts=verbosity=2:intercept_tls_get_addr=1 %run %t 10 2 2>&1 | FileCheck %s

>From 30515d60fea7eee17739ca8568c86d9bbc79a961 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 11 Sep 2024 08:33:07 -0700
Subject: [PATCH 6/7] touch

---
 compiler-rt/test/asan/TestCases/Linux/stress_dtls.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
index 6514456db56f6c..857bec984c0cb2 100644
--- a/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
+++ b/compiler-rt/test/asan/TestCases/Linux/stress_dtls.c
@@ -29,6 +29,7 @@
 // CHECK-NOT: num_live_dtls 5
 //
 // CHECK0-NOT: __tls_get_addr
+
 /*
 cc=your-compiler
 

>From 44caa39f55fef8778509a8132cd41e62dfcb843c Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 11 Sep 2024 10:54:04 -0700
Subject: [PATCH 7/7] again

---
 .../lib/sanitizer_common/sanitizer_tls_get_addr.cpp       | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
index 1113d515dcb06d..bf84a2ff60c91c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_tls_get_addr.cpp
@@ -151,10 +151,10 @@ DTLS::DTV *DTLS_on_tls_get_addr(void *arg_void, void *res,
     // This may happen inside the DTOR of main thread, so just ignore it.
     tls_size = 0;
   }
-  // if (tls_size) {
-  //   CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) - kDtvOffset);
-  //   CHECK_LT(reinterpret_cast<uptr>(res) - kDtvOffset, tls_beg + tls_size);
-  // }
+  if (tls_size) {
+    CHECK_LE(tls_beg, reinterpret_cast<uptr>(res) - kDtvOffset);
+    CHECK_LT(reinterpret_cast<uptr>(res) - kDtvOffset, tls_beg + tls_size);
+  }
   dtv->beg = tls_beg;
   dtv->size = tls_size;
   return dtv;



More information about the llvm-commits mailing list