Skip to content

Conversation

@bulbazord
Copy link
Member

When I wrote this previously, I was unaware that the TLS function already adds the offset. The test was working previously because the offset was 0 in this case (only 1 thread-local variable). I added another thread-local variable to the test to make sure the offset is indeed handled correctly.

rdar://156547548

When I wrote this previously, I was unaware that the TLS function
already adds the offset. The test was working previously because the
offset was 0 in this case (only 1 thread-local variable). I added
another thread-local variable to the test to make sure the offset is
indeed handled correctly.

rdar://156547548
@bulbazord bulbazord requested a review from jasonmolenda July 31, 2025 21:52
@bulbazord bulbazord requested a review from JDevlieghere as a code owner July 31, 2025 21:52
@llvmbot llvmbot added the lldb label Jul 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-lldb

Author: Alex Langford (bulbazord)

Changes

When I wrote this previously, I was unaware that the TLS function already adds the offset. The test was working previously because the offset was 0 in this case (only 1 thread-local variable). I added another thread-local variable to the test to make sure the offset is indeed handled correctly.

rdar://156547548


Full diff: https://github.com/llvm/llvm-project/pull/151601.diff

3 Files Affected:

  • (modified) lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp (+3-4)
  • (modified) lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py (+10)
  • (modified) lldb/test/API/lang/c/tls_globals/main.c (+2)
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
index 1270d57423c7b..8deb17c99136e 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
@@ -1159,9 +1159,8 @@ DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
   //  size_t offset;
   // }
   //
-  // The strategy is to take get_addr, call it with the address of the
-  // containing TLS_Thunk structure, and add the offset to the resulting
-  // pointer to get the data block.
+  // The strategy is to take get_addr and call it with the address of the
+  // containing TLS_Thunk structure.
   //
   // On older apple platforms, the key is treated as a pthread_key_t and passed
   // to pthread_getspecific. The pointer returned from that call is added to
@@ -1190,7 +1189,7 @@ DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
       const addr_t tls_data = evaluate_tls_address(
           thunk_load_addr, llvm::ArrayRef<addr_t>(tls_load_addr));
       if (tls_data != LLDB_INVALID_ADDRESS)
-        return tls_data + tls_offset;
+        return tls_data;
     }
   }
 
diff --git a/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py b/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py
index 5c0f304bdb37e..dad2b19a67170 100644
--- a/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py
+++ b/lldb/test/API/lang/c/tls_globals/TestTlsGlobals.py
@@ -73,6 +73,11 @@ def test(self):
             VARIABLES_DISPLAYED_CORRECTLY,
             patterns=[r"\(int\) \$.* = 88"],
         )
+        self.expect(
+            "expr var_static2",
+            VARIABLES_DISPLAYED_CORRECTLY,
+            patterns=[r"\(int\) \$.* = 66"],
+        )
         self.expect(
             "expr var_shared",
             VARIABLES_DISPLAYED_CORRECTLY,
@@ -104,6 +109,11 @@ def test(self):
             VARIABLES_DISPLAYED_CORRECTLY,
             patterns=[r"\(int\) \$.* = 44"],
         )
+        self.expect(
+            "expr var_static2",
+            VARIABLES_DISPLAYED_CORRECTLY,
+            patterns=[r"\(int\) \$.* = 22"],
+        )
         self.expect(
             "expr var_shared",
             VARIABLES_DISPLAYED_CORRECTLY,
diff --git a/lldb/test/API/lang/c/tls_globals/main.c b/lldb/test/API/lang/c/tls_globals/main.c
index bdfd78c1ac34b..890f823c2046b 100644
--- a/lldb/test/API/lang/c/tls_globals/main.c
+++ b/lldb/test/API/lang/c/tls_globals/main.c
@@ -10,10 +10,12 @@ touch_shared();
 
 // Create some TLS storage within the static executable.
 __thread int var_static = 44;
+__thread int var_static2 = 22;
 
 void *fn_static(void *param)
 {
 	var_static *= 2;
+  var_static2 *= 3;
 	shared_check();
 	usleep(1); // thread breakpoint
 	for(;;)

@github-actions
Copy link

github-actions bot commented Jul 31, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions c,cpp -- lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp lldb/test/API/lang/c/tls_globals/main.c
View the diff from clang-format here.
diff --git a/lldb/test/API/lang/c/tls_globals/main.c b/lldb/test/API/lang/c/tls_globals/main.c
index fac760b35..f1efaca05 100644
--- a/lldb/test/API/lang/c/tls_globals/main.c
+++ b/lldb/test/API/lang/c/tls_globals/main.c
@@ -15,8 +15,8 @@ __thread int var_static2 = 22;
 void *fn_static(void *param)
 {
 	var_static *= 2;
-	var_static2 *= 3;
-	shared_check();
+        var_static2 *= 3;
+        shared_check();
 	usleep(1); // thread breakpoint
 	for(;;)
 		usleep(1);

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my reading of the relevant dyld change this makes sense, thanks!

This does mean we won't handle the old TLS format correctly though right?

@bulbazord
Copy link
Member Author

This does mean we won't handle the old TLS format correctly though right?

My understanding is that this should handle the "old" TLS format correctly. When I updated this code 2 years ago, I think I misunderstood the dyld implementation and this worked because of a coincidence.

void *fn_static(void *param)
{
var_static *= 2;
var_static2 *= 3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation ?

@bulbazord bulbazord merged commit a27d34b into llvm:main Aug 4, 2025
8 of 9 checks passed
@bulbazord bulbazord deleted the fix-tls-for-real branch August 4, 2025 18:44
bulbazord added a commit to bulbazord/llvm-project that referenced this pull request Aug 4, 2025
When I wrote this previously, I was unaware that the TLS function
already adds the offset. The test was working previously because the
offset was 0 in this case (only 1 thread-local variable). I added
another thread-local variable to the test to make sure the offset is
indeed handled correctly.

rdar://156547548
(cherry picked from commit a27d34b)
JDevlieghere pushed a commit to swiftlang/llvm-project that referenced this pull request Aug 5, 2025
When I wrote this previously, I was unaware that the TLS function
already adds the offset. The test was working previously because the
offset was 0 in this case (only 1 thread-local variable). I added
another thread-local variable to the test to make sure the offset is
indeed handled correctly.

rdar://156547548
(cherry picked from commit a27d34b)
augusto2112 pushed a commit to augusto2112/llvm-project that referenced this pull request Aug 15, 2025
When I wrote this previously, I was unaware that the TLS function
already adds the offset. The test was working previously because the
offset was 0 in this case (only 1 thread-local variable). I added
another thread-local variable to the test to make sure the offset is
indeed handled correctly.

rdar://156547548
(cherry picked from commit a27d34b)
augusto2112 pushed a commit to augusto2112/llvm-project that referenced this pull request Aug 15, 2025
When I wrote this previously, I was unaware that the TLS function
already adds the offset. The test was working previously because the
offset was 0 in this case (only 1 thread-local variable). I added
another thread-local variable to the test to make sure the offset is
indeed handled correctly.

rdar://156547548
(cherry picked from commit a27d34b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants