<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/98479>98479</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Addresses of TLS variables are kept alive across fiber/stack-full coroutine context switches which may result in a crash
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          omern1
      </td>
    </tr>
</table>

<pre>
    ``` c++
thread_local void* tls_foo;

extern void do_stuff(void*);

int test() {
 do_stuff(tls_foo);
    do_stuff(tls_foo);
    return 0;
}
```

In our (Sony's) private repro `test` is executing on a fiber created by an M:N fiber library (stack-full coroutines) and `do_stuff` causes the fiber to be suspended.
When `test` resumes execution it's likely that it won't be running on the same OS thread as it was before it was suspended. 
This means that the TLS address loaded by `call load_address_for_tls@PLT` won't be valid on the new thread and will cause a crash.

``` asm
        push    rbp
        mov     rbp, rsp
 push    rbx
        push    rax
        lea     rdi, [rip + tls_foo@TLSLD]
        call    load_address_for_tls@PLT
        mov rdi, qword ptr [rax + tls_foo@DTPOFF]
        mov     rbx, rax
; When using fibers, do_stuff could return on a different thread.
        call _Z8do_stuffPv@PLT
; We want the TLS address to be reloaded here.
 mov     rdi, qword ptr [rbx + tls_foo@DTPOFF]
        call _Z8do_stuffPv@PLT
        xor     eax, eax
        add     rsp, 8
 pop     rbx
        pop     rbp
        ret
```

[MSVC has the `/GT` commandline option](https://learn.microsoft.com/en-us/cpp/build/reference/gt-support-fiber-safe-thread-local-storage) which makes it reload the TLS address before each load of a TLS variable and I think we probably need something similar in LLVM.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVVuP2zgP_TXKC5HAkXN9yMNMB_lQIP1aYAZdYF8CWqJj7ciSV5Jz-fcLyXaSubTowBgnIUWeQx5S6L06GKINmz-y-dMI21BZt7E1OTMdFVZeNmyRdQ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