Skip to content
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

runtime: fixup darwin/arm64 framepointer assembly #39524

Open
eliasnaur opened this issue Jun 11, 2020 · 18 comments
Open

runtime: fixup darwin/arm64 framepointer assembly #39524

eliasnaur opened this issue Jun 11, 2020 · 18 comments
Labels
arch-arm64 compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@eliasnaur
Copy link
Contributor

Quoting @cherrymui from CL 220588:

For example, in runtime/sys_darwin_arm64.s, function open_trampoline, the frame pointer is not saved. One possibility is to change the frame size (the $0 on the TEXT line) to 16, so the assembler will automatically allocate a frame and save the frame pointer, and remove the SUB and ADD to RSP. Do the same to other functions as well.

Another example is runtime/preempt_arm64.s, currently the frame pointer is only saved #ifdef GOOS_linux. We may want to do this for #ifdef GOOS_darwin as well.

@steeve
Copy link
Contributor

steeve commented Jun 11, 2020

That may explain why we saw weird unwinding on iOS.

@cherrymui
Copy link
Member

I'll try to do this for Go 1.16. Thanks.

@cherrymui cherrymui added this to the Go1.16 milestone Jun 11, 2020
@toothrot toothrot added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 12, 2020
@steeve
Copy link
Contributor

steeve commented Jul 2, 2020

Hey @cherrymui, I just tried to do what you said, and ended up with a crash, using Go 1.13.8.

Here is the diff, just in case I'm missing something from your directives:

diff --git a/src/runtime/sys_darwin_arm64.s b/src/runtime/sys_darwin_arm64.s
index ac3ca74f63..63becfb630 100644
--- a/src/runtime/sys_darwin_arm64.s
+++ b/src/runtime/sys_darwin_arm64.s
@@ -15,14 +15,12 @@ TEXT notok<>(SB),NOSPLIT,$0
 	MOVD	R8, (R8)
 	B	0(PC)
 
-TEXT runtime·open_trampoline(SB),NOSPLIT,$0
-	SUB	$16, RSP
+TEXT runtime·open_trampoline(SB),NOSPLIT,$16
 	MOVW	8(R0), R1	// arg 2 flags
 	MOVW	12(R0), R2	// arg 3 mode
 	MOVW	R2, (RSP)	// arg 3 is variadic, pass on stack
 	MOVD	0(R0), R0	// arg 1 pathname
 	BL	libc_open(SB)
-	ADD	$16, RSP
 	RET
 
 TEXT runtime·close_trampoline(SB),NOSPLIT,$0

Also, what was the reason for the frame to be manually allocated, rather than via declaration?

@steeve
Copy link
Contributor

steeve commented Jul 2, 2020

Also, do you think asmcgocall should have it? We saw unwinders unable to go past it (main thread starting at this function).

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/241158 mentions this issue: runtime: adjust frame pointer on stack copy on ARM64

@aclements
Copy link
Member

@cherrymui , you have some experimental CLs out for this. Should we still try to fix this in 1.16?

@cherrymui
Copy link
Member

I could give it a try. More work needs to be done (besides the CL above), e.g. saving frame pointers in syscall wrappers.

Question: how can I test it? how do I know if I got everything?

There is also an open question about what to do at stack switch (e.g. systemstack, asmcgocall, etc.).

@randall77
Copy link
Contributor

Not really a test per se, but there's now a vet check for this for amd64, and a TODO in that check for arm64.

https://github.com/golang/go/blob/master/src/cmd/vendor/golang.org/x/tools/go/analysis/passes/framepointer/framepointer.go#L34

Not an exhaustive check to be sure, but using it with go vet std cmd found some cases that needed to be fixed.

@randall77
Copy link
Contributor

(And I have a really invasive test-only CL that rebases all the stack frame references to use FP instead of SP. That finds problems with the FP mighty quickly. Let me know if you want me to drag that CL back to life.)

@cherrymui
Copy link
Member

Thanks, I could give that a try. There are probably not many cases that clobbers the frame pointer register on ARM64, because ARM64 has a lot of registers and frame pointer is R29.

Other case may be: manually allocate a frame without saving the frame pointer, or smash the frame pointer slot on stack.

@randall77
Copy link
Contributor

smash the frame pointer slot on stack

There was one of these on amd64: https://go-review.googlesource.com/c/go/+/248262

@steeve
Copy link
Contributor

steeve commented Dec 9, 2020

@cherrymui I have a repro on amd64 at #40044 if that's any help, as the issue seems caused when the stack is swapped in asmcgocall and not forwarding the frame pointer list for some reason

not that I'm using libunwind but backtrace() does expose the issue also

@cherrymui
Copy link
Member

when the stack is swapped in asmcgocall and not forwarding the frame pointer list for some reason

That is the "open question" I mentioned earlier. I don't know what is the correct answer here.

@aclements
Copy link
Member

There's still a bunch of work to do here and we're late in the freeze, so don't want to introduce risk. Pushing to 1.17.

@aclements aclements modified the milestones: Go1.16, Go1.17 Dec 15, 2020
@ianlancetaylor
Copy link
Member

Are we going to do anything on this issue for 1.17? Should we move this to Backlog? Thanks.

@cherrymui
Copy link
Member

No, nothing is done for 1.17. Thanks.

@cherrymui cherrymui modified the milestones: Go1.17, Backlog May 4, 2021
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
@mknyszek mknyszek moved this to Triage Backlog in Go Compiler / Runtime Jul 15, 2022
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/481635 mentions this issue: runtime: save frame pointer to the stack in signal handlers for arm64

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/481636 mentions this issue: runtime: zero saved frame pointer when reusing goroutine stack on ARM

gopherbot pushed a commit that referenced this issue Apr 5, 2023
When taking over the goroutine stack in the panic or preemption signal
handlers on arm64, the frame pointer should be saved on the stack (like
the link register) so that frame-pointer unwinding from a panic stack
works properly. Otherwise, tests like TestStackWrapperStackPanic will
fail with the frame pointer check in adjustframe (enabled with
debugCheckBP) when checking the sigpanic frame.

Updates #39524, #58432

Change-Id: I8b89e6fc4877af29b1b81e55e591e6398159855c
Reviewed-on: https://go-review.googlesource.com/c/go/+/481635
Reviewed-by: Felix Geisendörfer <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Nick Ripley <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
Reviewed-by: Michael Knyszek <[email protected]>
gopherbot pushed a commit that referenced this issue Apr 18, 2023
Frame pointer is enabled on ARM64. When copying stacks, the
saved frame pointers need to be adjusted.

Updates #39524, #40044.
Fixes #58432.

Change-Id: I73651fdfd1a6cccae26a5ce02e7e86f6c2fb9bf7
Reviewed-on: https://go-review.googlesource.com/c/go/+/241158
Reviewed-by: Felix Geisendörfer <[email protected]>
Run-TryBot: Cherry Mui <[email protected]>
Reviewed-by: Michael Knyszek <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
gopherbot pushed a commit that referenced this issue Aug 15, 2023
When a goroutine stack is reused on arm64, the spot on the stack where
the "caller's" frame pointer goes for the topmost frame should be
explicitly zeroed. Otherwise, the frame pointer check in adjustframe
with debugCheckBP enabled will fail on the topmost frame of a call stack
the first time a reused stack is grown.

Updates #39524, #58432

Change-Id: Ic1210dc005e3ecdbf9cd5d7b98846566e56df8f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/481636
Reviewed-by: Felix Geisendörfer <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Dmitri Shuralyov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-arm64 compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Status: Triage Backlog
Development

No branches or pull requests

9 participants