-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[lldb] Fix TLS support on Darwin platforms #151601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
|
@llvm/pr-subscribers-lldb Author: Alex Langford (bulbazord) ChangesWhen 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:
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(;;)
|
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.cView 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);
|
Michael137
left a comment
There was a problem hiding this 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?
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation ?
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)
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)
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)
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)
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