aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/sys_windows_arm64.s
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-03-19 20:19:33 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2021-03-22 17:50:42 +0000
commit78afca22c9485dbd44da3b909866bbb9c1eaf440 (patch)
tree955225a5cbd184e6f7d233c270ee06c7752fb700 /src/runtime/sys_windows_arm64.s
parent8fd0f83552d3ef9ca38c031bec93a36b189e3e11 (diff)
downloadgo-78afca22c9485dbd44da3b909866bbb9c1eaf440.tar.gz
go-78afca22c9485dbd44da3b909866bbb9c1eaf440.zip
runtime: fix bogus NtCurrentTeb()->TlsSlots[n] calculation on windows/arm64
runtime.save_g adds X18 to runtime.tls_g in order to have a pointer to thread local storage. X18 represents a pointer to the TEB on ARM64 and runtime.tls_g is set in runtime.wintls at initialization time. This function calls TlsAlloc to allocate a "TLS slot", which is supposed to index into NtCurrentTeb()->TlsSlots. So the full calculation we want is: X18 + offsetof(TEB, TlsSlots) + 8*TlsAllocReturnValue It makes sense to store the complete value of "offsetof(TEB, TlsSlots) + TlsAllocReturnValue" into runtime.tls_g so that the calculation can simplify to: X18 + runtime.tls_g But, instead of computing that, we're currently doing something kind of strange, in which we: - call TlsAlloc, which puts its return value into X0 - make sure X0 is less than 64, so we don't overflow - set runtime.tls_g to 8*X1 + offsetof(TEB, TlsSlots) The question is: why are we using X1 instead of X0? What is in X1? Probably it was, by luck, zero before, and TlsAlloc returned zero, so there was no problem. But on recent versions of Windows, X1 is some other garbage value and not zero, so we eventually crash when trying to dereference X18 + runtime.tls_g. This commit fixes the problem by just computing: runtime.tls_g = 8*X0 + offsetof(TEB, TlsSlots) Fixes #45138. Change-Id: I560426bae7468217bd183ac6c6eb4b56a3815b09 Reviewed-on: https://go-review.googlesource.com/c/go/+/303273 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
Diffstat (limited to 'src/runtime/sys_windows_arm64.s')
-rw-r--r--src/runtime/sys_windows_arm64.s6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/runtime/sys_windows_arm64.s b/src/runtime/sys_windows_arm64.s
index c8c96acd06..0b77e6d048 100644
--- a/src/runtime/sys_windows_arm64.s
+++ b/src/runtime/sys_windows_arm64.s
@@ -573,7 +573,7 @@ TEXT runtime·wintls(SB),NOSPLIT,$0
ok:
// Save offset from R18 into tls_g.
- LSL $3, R1
- ADD $TEB_TlsSlots, R1
- MOVD R1, runtime·tls_g(SB)
+ LSL $3, R0
+ ADD $TEB_TlsSlots, R0
+ MOVD R0, runtime·tls_g(SB)
RET